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

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: Comments. 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
« no previous file with comments | « components/offline_pages/offline_page_model_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 if (result == SavePageResult::SUCCESS) {
744 DeleteExistingPagesWithSameURL(offline_page);
745 } else {
746 PostClearStorageIfNeededTask();
747 }
748
743 DeletePendingArchiver(archiver); 749 DeletePendingArchiver(archiver);
744
745 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this)); 750 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this));
746 } 751 }
747 752
748 void OfflinePageModelImpl::OnMarkPageAccesseDone( 753 void OfflinePageModelImpl::OnMarkPageAccesseDone(
749 const OfflinePageItem& offline_page_item, 754 const OfflinePageItem& offline_page_item,
750 bool success) { 755 bool success) {
751 // Update the item in the cache only upon success. 756 // Update the item in the cache only upon success.
752 if (success) 757 if (success)
753 offline_pages_[offline_page_item.offline_id] = offline_page_item; 758 offline_pages_[offline_page_item.offline_id] = offline_page_item;
754 759
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
801 kStorageManagerStartingDelay); 806 kStorageManagerStartingDelay);
802 } 807 }
803 808
804 void OfflinePageModelImpl::InformSavePageDone(const SavePageCallback& callback, 809 void OfflinePageModelImpl::InformSavePageDone(const SavePageCallback& callback,
805 SavePageResult result, 810 SavePageResult result,
806 const ClientId& client_id, 811 const ClientId& client_id,
807 int64_t offline_id) { 812 int64_t offline_id) {
808 ReportSavePageResultHistogramAfterSave(client_id, result); 813 ReportSavePageResultHistogramAfterSave(client_id, result);
809 archive_manager_->GetStorageStats( 814 archive_manager_->GetStorageStats(
810 base::Bind(&ReportStorageHistogramsAfterSave)); 815 base::Bind(&ReportStorageHistogramsAfterSave));
811 // Remove existing pages generated by the same policy and with same url.
812 size_t pages_allowed =
813 policy_controller_->GetPolicy(client_id.name_space).pages_allowed_per_url;
814 if (result == SavePageResult::SUCCESS && pages_allowed != kUnlimitedPages) {
815 GetPagesByOnlineURL(
816 offline_pages_[offline_id].url,
817 base::Bind(&OfflinePageModelImpl::OnPagesFoundWithSameURL,
818 weak_ptr_factory_.GetWeakPtr(), client_id, offline_id,
819 pages_allowed));
820 } else {
821 PostClearStorageIfNeededTask();
822 }
823 callback.Run(result, offline_id); 816 callback.Run(result, offline_id);
824 } 817 }
825 818
819 void OfflinePageModelImpl::DeleteExistingPagesWithSameURL(
820 const OfflinePageItem& offline_page) {
821 // Remove existing pages generated by the same policy and with same url.
822 size_t pages_allowed =
823 policy_controller_->GetPolicy(offline_page.client_id.name_space)
824 .pages_allowed_per_url;
825 if (pages_allowed != kUnlimitedPages) {
fgorski 2016/07/07 17:03:46 nit: if (pages_allowed == kUnlimited...) return
826 GetPagesByOnlineURL(
827 offline_page.url,
828 base::Bind(&OfflinePageModelImpl::OnPagesFoundWithSameURL,
829 weak_ptr_factory_.GetWeakPtr(), offline_page,
830 pages_allowed));
831 }
832 }
833
826 void OfflinePageModelImpl::OnPagesFoundWithSameURL( 834 void OfflinePageModelImpl::OnPagesFoundWithSameURL(
827 const ClientId& client_id, 835 const OfflinePageItem& offline_page,
828 int64_t offline_id,
829 size_t pages_allowed, 836 size_t pages_allowed,
830 const MultipleOfflinePageItemResult& items) { 837 const MultipleOfflinePageItemResult& items) {
831 std::vector<OfflinePageItem> pages_to_delete; 838 std::vector<OfflinePageItem> pages_to_delete;
832 for (const auto& item : items) { 839 for (const auto& item : items) {
833 if (item.offline_id != offline_id && 840 if (item.offline_id != offline_page.offline_id &&
834 item.client_id.name_space == client_id.name_space) { 841 item.client_id.name_space == offline_page.client_id.name_space) {
835 pages_to_delete.push_back(item); 842 pages_to_delete.push_back(item);
836 } 843 }
837 } 844 }
838 // Only keep |pages_allowed|-1 of most fresh pages and delete others, by 845 // 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. 846 // sorting pages with the oldest ones first and resize the vector.
840 if (pages_to_delete.size() >= pages_allowed) { 847 if (pages_to_delete.size() >= pages_allowed) {
841 sort(pages_to_delete.begin(), pages_to_delete.end(), 848 sort(pages_to_delete.begin(), pages_to_delete.end(),
842 [](const OfflinePageItem& a, const OfflinePageItem& b) -> bool { 849 [](const OfflinePageItem& a, const OfflinePageItem& b) -> bool {
843 return a.last_access_time < b.last_access_time; 850 return a.last_access_time < b.last_access_time;
844 }); 851 });
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
1053 void OfflinePageModelImpl::RunWhenLoaded(const base::Closure& task) { 1060 void OfflinePageModelImpl::RunWhenLoaded(const base::Closure& task) {
1054 if (!is_loaded_) { 1061 if (!is_loaded_) {
1055 delayed_tasks_.push_back(task); 1062 delayed_tasks_.push_back(task);
1056 return; 1063 return;
1057 } 1064 }
1058 1065
1059 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); 1066 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task);
1060 } 1067 }
1061 1068
1062 } // namespace offline_pages 1069 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/offline_page_model_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698