| OLD | NEW |
| 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 779 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 790 kStorageManagerStartingDelay); | 790 kStorageManagerStartingDelay); |
| 791 } | 791 } |
| 792 | 792 |
| 793 void OfflinePageModelImpl::InformSavePageDone(const SavePageCallback& callback, | 793 void OfflinePageModelImpl::InformSavePageDone(const SavePageCallback& callback, |
| 794 SavePageResult result, | 794 SavePageResult result, |
| 795 const ClientId& client_id, | 795 const ClientId& client_id, |
| 796 int64_t offline_id) { | 796 int64_t offline_id) { |
| 797 ReportSavePageResultHistogramAfterSave(client_id, result); | 797 ReportSavePageResultHistogramAfterSave(client_id, result); |
| 798 archive_manager_->GetStorageStats( | 798 archive_manager_->GetStorageStats( |
| 799 base::Bind(&ReportStorageHistogramsAfterSave)); | 799 base::Bind(&ReportStorageHistogramsAfterSave)); |
| 800 base::ThreadTaskRunnerHandle::Get()->PostTask( | 800 // Remove existing pages generated by the same policy and with same url. |
| 801 FROM_HERE, base::Bind(&OfflinePageModelImpl::ClearStorageIfNeeded, | 801 size_t pages_allowed = |
| 802 weak_ptr_factory_.GetWeakPtr(), | 802 policy_controller_->GetPolicy(client_id.name_space).pages_allowed_per_url; |
| 803 base::Bind(&OfflinePageModelImpl::OnStorageCleared, | 803 if (result == SavePageResult::SUCCESS && pages_allowed != kUnlimitedPages) { |
| 804 weak_ptr_factory_.GetWeakPtr()))); | 804 GetPagesByOnlineURL( |
| 805 offline_pages_[offline_id].url, |
| 806 base::Bind(&OfflinePageModelImpl::OnPagesFoundWithSameURL, |
| 807 weak_ptr_factory_.GetWeakPtr(), client_id, offline_id, |
| 808 pages_allowed)); |
| 809 } else { |
| 810 PostClearStorageIfNeededTask(); |
| 811 } |
| 805 callback.Run(result, offline_id); | 812 callback.Run(result, offline_id); |
| 806 } | 813 } |
| 807 | 814 |
| 815 void OfflinePageModelImpl::OnPagesFoundWithSameURL( |
| 816 const ClientId& client_id, |
| 817 int64_t offline_id, |
| 818 size_t pages_allowed, |
| 819 const MultipleOfflinePageItemResult& items) { |
| 820 std::vector<OfflinePageItem> pages_to_delete; |
| 821 for (const auto& item : items) { |
| 822 if (item.offline_id != offline_id && |
| 823 item.client_id.name_space == client_id.name_space) { |
| 824 pages_to_delete.push_back(item); |
| 825 } |
| 826 } |
| 827 // Only keep |pages_allowed|-1 of most fresh pages and delete others, by |
| 828 // sorting pages with the oldest ones first and resize the vector. |
| 829 if (pages_to_delete.size() >= pages_allowed) { |
| 830 sort(pages_to_delete.begin(), pages_to_delete.end(), |
| 831 [](const OfflinePageItem& a, const OfflinePageItem& b) -> bool { |
| 832 return a.last_access_time < b.last_access_time; |
| 833 }); |
| 834 pages_to_delete.resize(pages_to_delete.size() - pages_allowed + 1); |
| 835 } |
| 836 std::vector<int64_t> page_ids_to_delete; |
| 837 for (const auto& item : pages_to_delete) |
| 838 page_ids_to_delete.push_back(item.offline_id); |
| 839 DeletePagesByOfflineId( |
| 840 page_ids_to_delete, |
| 841 base::Bind(&OfflinePageModelImpl::OnDeleteOldPagesWithSameURL, |
| 842 weak_ptr_factory_.GetWeakPtr())); |
| 843 } |
| 844 |
| 845 void OfflinePageModelImpl::OnDeleteOldPagesWithSameURL( |
| 846 DeletePageResult result) { |
| 847 // TODO(romax) Add UMAs for failure cases. |
| 848 PostClearStorageIfNeededTask(); |
| 849 } |
| 850 |
| 808 void OfflinePageModelImpl::DeletePendingArchiver( | 851 void OfflinePageModelImpl::DeletePendingArchiver( |
| 809 OfflinePageArchiver* archiver) { | 852 OfflinePageArchiver* archiver) { |
| 810 pending_archivers_.erase(std::find(pending_archivers_.begin(), | 853 pending_archivers_.erase(std::find(pending_archivers_.begin(), |
| 811 pending_archivers_.end(), archiver)); | 854 pending_archivers_.end(), archiver)); |
| 812 } | 855 } |
| 813 | 856 |
| 814 void OfflinePageModelImpl::OnDeleteArchiveFilesDone( | 857 void OfflinePageModelImpl::OnDeleteArchiveFilesDone( |
| 815 const std::vector<int64_t>& offline_ids, | 858 const std::vector<int64_t>& offline_ids, |
| 816 const DeletePageCallback& callback, | 859 const DeletePageCallback& callback, |
| 817 bool success) { | 860 bool success) { |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 973 ClearStorageResult result) { | 1016 ClearStorageResult result) { |
| 974 UMA_HISTOGRAM_ENUMERATION("OfflinePages.ClearStorageResult", | 1017 UMA_HISTOGRAM_ENUMERATION("OfflinePages.ClearStorageResult", |
| 975 static_cast<int>(result), | 1018 static_cast<int>(result), |
| 976 static_cast<int>(ClearStorageResult::RESULT_COUNT)); | 1019 static_cast<int>(ClearStorageResult::RESULT_COUNT)); |
| 977 if (expired_page_count > 0) { | 1020 if (expired_page_count > 0) { |
| 978 UMA_HISTOGRAM_COUNTS("OfflinePages.ExpirePage.BatchSize", | 1021 UMA_HISTOGRAM_COUNTS("OfflinePages.ExpirePage.BatchSize", |
| 979 static_cast<int32_t>(expired_page_count)); | 1022 static_cast<int32_t>(expired_page_count)); |
| 980 } | 1023 } |
| 981 } | 1024 } |
| 982 | 1025 |
| 1026 void OfflinePageModelImpl::PostClearStorageIfNeededTask() { |
| 1027 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 1028 FROM_HERE, base::Bind(&OfflinePageModelImpl::ClearStorageIfNeeded, |
| 1029 weak_ptr_factory_.GetWeakPtr(), |
| 1030 base::Bind(&OfflinePageModelImpl::OnStorageCleared, |
| 1031 weak_ptr_factory_.GetWeakPtr()))); |
| 1032 } |
| 1033 |
| 983 void OfflinePageModelImpl::RunWhenLoaded(const base::Closure& task) { | 1034 void OfflinePageModelImpl::RunWhenLoaded(const base::Closure& task) { |
| 984 if (!is_loaded_) { | 1035 if (!is_loaded_) { |
| 985 delayed_tasks_.push_back(task); | 1036 delayed_tasks_.push_back(task); |
| 986 return; | 1037 return; |
| 987 } | 1038 } |
| 988 | 1039 |
| 989 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); | 1040 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); |
| 990 } | 1041 } |
| 991 | 1042 |
| 992 } // namespace offline_pages | 1043 } // namespace offline_pages |
| OLD | NEW |