Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Side by Side Diff: components/offline_pages/offline_page_model_impl.cc

Issue 2112183002: [Offline Pages] Refactoring deletion of pages with same urls. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/offline_pages/offline_page_model_impl.h" 5 #include "components/offline_pages/offline_page_model_impl.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <limits> 8 #include <limits>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 722 matching lines...) Expand 10 before | Expand all | Expand 10 after
733 result = SavePageResult::SUCCESS; 733 result = SavePageResult::SUCCESS;
734 ReportPageHistogramAfterSave(offline_page); 734 ReportPageHistogramAfterSave(offline_page);
735 offline_event_logger_.RecordPageSaved( 735 offline_event_logger_.RecordPageSaved(
736 offline_page.client_id.name_space, offline_page.url.spec(), 736 offline_page.client_id.name_space, offline_page.url.spec(),
737 std::to_string(offline_page.offline_id)); 737 std::to_string(offline_page.offline_id));
738 } else { 738 } else {
739 result = SavePageResult::STORE_FAILURE; 739 result = SavePageResult::STORE_FAILURE;
740 } 740 }
741 InformSavePageDone(callback, result, offline_page.client_id, 741 InformSavePageDone(callback, result, offline_page.client_id,
742 offline_page.offline_id); 742 offline_page.offline_id);
743 DeleteExistingPageWithSameURL(result, offline_page);
fgorski 2016/07/01 17:43:22 Did you consider this? if (result == SavePageResu
romax 2016/07/06 19:44:50 Done.
743 DeletePendingArchiver(archiver); 744 DeletePendingArchiver(archiver);
744 745
745 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this)); 746 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this));
746 } 747 }
747 748
748 void OfflinePageModelImpl::OnMarkPageAccesseDone( 749 void OfflinePageModelImpl::OnMarkPageAccesseDone(
749 const OfflinePageItem& offline_page_item, 750 const OfflinePageItem& offline_page_item,
750 bool success) { 751 bool success) {
751 // Update the item in the cache only upon success. 752 // Update the item in the cache only upon success.
752 if (success) 753 if (success)
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
801 kStorageManagerStartingDelay); 802 kStorageManagerStartingDelay);
802 } 803 }
803 804
804 void OfflinePageModelImpl::InformSavePageDone(const SavePageCallback& callback, 805 void OfflinePageModelImpl::InformSavePageDone(const SavePageCallback& callback,
805 SavePageResult result, 806 SavePageResult result,
806 const ClientId& client_id, 807 const ClientId& client_id,
807 int64_t offline_id) { 808 int64_t offline_id) {
808 ReportSavePageResultHistogramAfterSave(client_id, result); 809 ReportSavePageResultHistogramAfterSave(client_id, result);
809 archive_manager_->GetStorageStats( 810 archive_manager_->GetStorageStats(
810 base::Bind(&ReportStorageHistogramsAfterSave)); 811 base::Bind(&ReportStorageHistogramsAfterSave));
812 callback.Run(result, offline_id);
813 }
814
815 void OfflinePageModelImpl::DeleteExistingPageWithSameURL(
816 SavePageResult result,
817 const OfflinePageItem& offline_page) {
811 // Remove existing pages generated by the same policy and with same url. 818 // Remove existing pages generated by the same policy and with same url.
812 size_t pages_allowed = 819 size_t pages_allowed =
813 policy_controller_->GetPolicy(client_id.name_space).pages_allowed_per_url; 820 policy_controller_->GetPolicy(offline_page.client_id.name_space)
821 .pages_allowed_per_url;
814 if (result == SavePageResult::SUCCESS && pages_allowed != kUnlimitedPages) { 822 if (result == SavePageResult::SUCCESS && pages_allowed != kUnlimitedPages) {
815 GetPagesByOnlineURL( 823 GetPagesByOnlineURL(
816 offline_pages_[offline_id].url, 824 offline_page.url,
817 base::Bind(&OfflinePageModelImpl::OnPagesFoundWithSameURL, 825 base::Bind(&OfflinePageModelImpl::OnPagesFoundWithSameURL,
818 weak_ptr_factory_.GetWeakPtr(), client_id, offline_id, 826 weak_ptr_factory_.GetWeakPtr(), offline_page,
819 pages_allowed)); 827 pages_allowed));
820 } else { 828 } else {
821 PostClearStorageIfNeededTask(); 829 PostClearStorageIfNeededTask();
822 } 830 }
823 callback.Run(result, offline_id);
824 } 831 }
825 832
826 void OfflinePageModelImpl::OnPagesFoundWithSameURL( 833 void OfflinePageModelImpl::OnPagesFoundWithSameURL(
827 const ClientId& client_id, 834 const OfflinePageItem& offline_page,
828 int64_t offline_id,
829 size_t pages_allowed, 835 size_t pages_allowed,
830 const MultipleOfflinePageItemResult& items) { 836 const MultipleOfflinePageItemResult& items) {
831 std::vector<OfflinePageItem> pages_to_delete; 837 std::vector<OfflinePageItem> pages_to_delete;
832 for (const auto& item : items) { 838 for (const auto& item : items) {
833 if (item.offline_id != offline_id && 839 if (item.offline_id != offline_page.offline_id &&
834 item.client_id.name_space == client_id.name_space) { 840 item.client_id.name_space == offline_page.client_id.name_space) {
835 pages_to_delete.push_back(item); 841 pages_to_delete.push_back(item);
836 } 842 }
837 } 843 }
838 // Only keep |pages_allowed|-1 of most fresh pages and delete others, by 844 // Only keep |pages_allowed|-1 of most fresh pages and delete others, by
839 // sorting pages with the oldest ones first and resize the vector. 845 // sorting pages with the oldest ones first and resize the vector.
840 if (pages_to_delete.size() >= pages_allowed) { 846 if (pages_to_delete.size() >= pages_allowed) {
841 sort(pages_to_delete.begin(), pages_to_delete.end(), 847 sort(pages_to_delete.begin(), pages_to_delete.end(),
842 [](const OfflinePageItem& a, const OfflinePageItem& b) -> bool { 848 [](const OfflinePageItem& a, const OfflinePageItem& b) -> bool {
843 return a.last_access_time < b.last_access_time; 849 return a.last_access_time < b.last_access_time;
844 }); 850 });
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
1053 void OfflinePageModelImpl::RunWhenLoaded(const base::Closure& task) { 1059 void OfflinePageModelImpl::RunWhenLoaded(const base::Closure& task) {
1054 if (!is_loaded_) { 1060 if (!is_loaded_) {
1055 delayed_tasks_.push_back(task); 1061 delayed_tasks_.push_back(task);
1056 return; 1062 return;
1057 } 1063 }
1058 1064
1059 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); 1065 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task);
1060 } 1066 }
1061 1067
1062 } // namespace offline_pages 1068 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698