| 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_storage_manager.h" | 5 #include "components/offline_pages/offline_page_storage_manager.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/time/clock.h" | 10 #include "base/time/clock.h" |
| 11 #include "base/time/default_clock.h" | 11 #include "base/time/default_clock.h" |
| 12 #include "base/time/time.h" | |
| 13 #include "components/offline_pages/client_policy_controller.h" | 12 #include "components/offline_pages/client_policy_controller.h" |
| 14 #include "components/offline_pages/offline_page_client_policy.h" | 13 #include "components/offline_pages/offline_page_client_policy.h" |
| 15 #include "components/offline_pages/offline_page_item.h" | 14 #include "components/offline_pages/offline_page_item.h" |
| 16 #include "components/offline_pages/offline_page_types.h" | |
| 17 | 15 |
| 18 namespace offline_pages { | 16 namespace offline_pages { |
| 19 | 17 |
| 20 OfflinePageStorageManager::OfflinePageStorageManager( | 18 OfflinePageStorageManager::OfflinePageStorageManager( |
| 21 Client* client, | 19 Client* client, |
| 22 ClientPolicyController* policy_controller) | 20 ClientPolicyController* policy_controller, |
| 21 ArchiveManager* archive_manager) |
| 23 : client_(client), | 22 : client_(client), |
| 24 policy_controller_(policy_controller), | 23 policy_controller_(policy_controller), |
| 24 archive_manager_(archive_manager), |
| 25 in_progress_(false), | 25 in_progress_(false), |
| 26 clock_(new base::DefaultClock()), | 26 clock_(new base::DefaultClock()), |
| 27 weak_ptr_factory_(this) {} | 27 weak_ptr_factory_(this) {} |
| 28 | 28 |
| 29 OfflinePageStorageManager::~OfflinePageStorageManager() {} | 29 OfflinePageStorageManager::~OfflinePageStorageManager() {} |
| 30 | 30 |
| 31 void OfflinePageStorageManager::ClearPagesIfNeeded( | 31 void OfflinePageStorageManager::ClearPagesIfNeeded( |
| 32 const ClearPageCallback& callback) { | 32 const ClearPagesCallback& callback) { |
| 33 if (!ShouldClearPages()) | 33 if (in_progress_) |
| 34 return; | 34 return; |
| 35 in_progress_ = true; | 35 in_progress_ = true; |
| 36 client_->GetAllPages(base::Bind(&OfflinePageStorageManager::ClearExpiredPages, | 36 archive_manager_->GetStorageStats( |
| 37 weak_ptr_factory_.GetWeakPtr(), callback)); | 37 base::Bind(&OfflinePageStorageManager::OnGetStorageStatsDone, |
| 38 } | 38 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 39 | |
| 40 void OfflinePageStorageManager::ClearExpiredPages( | |
| 41 const ClearPageCallback& callback, | |
| 42 const MultipleOfflinePageItemResult& pages) { | |
| 43 DCHECK(in_progress_); | |
| 44 std::vector<int64_t> offline_ids; | |
| 45 GetExpiredPageIds(pages, offline_ids); | |
| 46 client_->DeletePagesByOfflineId( | |
| 47 offline_ids, | |
| 48 base::Bind(&OfflinePageStorageManager::OnExpiredPagesDeleted, | |
| 49 weak_ptr_factory_.GetWeakPtr(), callback, offline_ids.size())); | |
| 50 } | 39 } |
| 51 | 40 |
| 52 void OfflinePageStorageManager::SetClockForTesting( | 41 void OfflinePageStorageManager::SetClockForTesting( |
| 53 std::unique_ptr<base::Clock> clock) { | 42 std::unique_ptr<base::Clock> clock) { |
| 54 clock_ = std::move(clock); | 43 clock_ = std::move(clock); |
| 55 } | 44 } |
| 56 | 45 |
| 46 void OfflinePageStorageManager::OnGetStorageStatsDone( |
| 47 const ClearPagesCallback& callback, |
| 48 const ArchiveManager::StorageStats& stats) { |
| 49 DCHECK(in_progress_); |
| 50 ClearMode mode = ShouldClearPages(stats); |
| 51 if (mode == ClearMode::NOT_NEEDED) { |
| 52 in_progress_ = false; |
| 53 last_clear_time_ = clock_->Now(); |
| 54 callback.Run(0, ClearStorageResult::UNNECESSARY); |
| 55 return; |
| 56 } |
| 57 client_->GetAllPages(base::Bind(&OfflinePageStorageManager::OnGetAllPagesDone, |
| 58 weak_ptr_factory_.GetWeakPtr(), callback, |
| 59 stats)); |
| 60 } |
| 61 |
| 62 void OfflinePageStorageManager::OnGetAllPagesDone( |
| 63 const ClearPagesCallback& callback, |
| 64 const ArchiveManager::StorageStats& stats, |
| 65 const MultipleOfflinePageItemResult& pages) { |
| 66 std::vector<int64_t> offline_ids; |
| 67 GetExpiredPageIds(pages, stats, offline_ids); |
| 68 client_->DeletePagesByOfflineId( |
| 69 offline_ids, |
| 70 base::Bind(&OfflinePageStorageManager::OnExpiredPagesDeleted, |
| 71 weak_ptr_factory_.GetWeakPtr(), callback, offline_ids.size())); |
| 72 } |
| 73 |
| 74 void OfflinePageStorageManager::OnExpiredPagesDeleted( |
| 75 const ClearPagesCallback& callback, |
| 76 int pages_cleared, |
| 77 DeletePageResult result) { |
| 78 last_clear_time_ = clock_->Now(); |
| 79 ClearStorageResult clear_result = result == DeletePageResult::SUCCESS |
| 80 ? ClearStorageResult::SUCCESS |
| 81 : ClearStorageResult::DELETE_FAILURE; |
| 82 in_progress_ = false; |
| 83 callback.Run(pages_cleared, clear_result); |
| 84 } |
| 85 |
| 57 void OfflinePageStorageManager::GetExpiredPageIds( | 86 void OfflinePageStorageManager::GetExpiredPageIds( |
| 58 const MultipleOfflinePageItemResult& pages, | 87 const MultipleOfflinePageItemResult& pages, |
| 88 const ArchiveManager::StorageStats& stats, |
| 59 std::vector<int64_t>& offline_ids) { | 89 std::vector<int64_t>& offline_ids) { |
| 60 base::Time now = clock_->Now(); | 90 base::Time now = clock_->Now(); |
| 61 | 91 |
| 62 // Creating a map from namespace to a vector of page items. | 92 // Creating a map from namespace to a vector of page items. |
| 63 // Sort each vector based on last accessed time and all pages after index | 93 // Sort each vector based on last accessed time and all pages after index |
| 64 // min{size(), page_limit} should be expired. And then start iterating | 94 // min{size(), page_limit} should be expired. And then start iterating |
| 65 // backwards to expire pages. | 95 // backwards to expire pages. |
| 66 std::map<std::string, std::vector<OfflinePageItem>> pages_map; | 96 std::map<std::string, std::vector<OfflinePageItem>> pages_map; |
| 97 std::vector<OfflinePageItem> kept_pages; |
| 98 int64_t kept_pages_size = 0; |
| 67 | 99 |
| 68 for (const auto& page : pages) | 100 for (const auto& page : pages) |
| 69 pages_map[page.client_id.name_space].push_back(page); | 101 pages_map[page.client_id.name_space].push_back(page); |
| 70 | 102 |
| 71 for (auto& iter : pages_map) { | 103 for (auto& iter : pages_map) { |
| 72 std::string name_space = iter.first; | 104 std::string name_space = iter.first; |
| 73 std::vector<OfflinePageItem>& page_list = iter.second; | 105 std::vector<OfflinePageItem>& page_list = iter.second; |
| 74 | 106 |
| 75 LifetimePolicy policy = | 107 LifetimePolicy policy = |
| 76 policy_controller_->GetPolicy(name_space).lifetime_policy; | 108 policy_controller_->GetPolicy(name_space).lifetime_policy; |
| 77 | 109 |
| 78 std::sort(page_list.begin(), page_list.end(), | 110 std::sort(page_list.begin(), page_list.end(), |
| 79 [](const OfflinePageItem& a, const OfflinePageItem& b) -> bool { | 111 [](const OfflinePageItem& a, const OfflinePageItem& b) -> bool { |
| 80 return a.last_access_time > b.last_access_time; | 112 return a.last_access_time > b.last_access_time; |
| 81 }); | 113 }); |
| 82 | 114 |
| 83 int page_list_size = page_list.size(); | 115 int page_list_size = page_list.size(); |
| 84 int pos = 0; | 116 int pos = 0; |
| 85 while (pos < page_list_size && | 117 while (pos < page_list_size && |
| 86 (policy.page_limit == kUnlimitedPages || pos < policy.page_limit) && | 118 (policy.page_limit == kUnlimitedPages || pos < policy.page_limit) && |
| 87 !ShouldBeExpired(now, page_list.at(pos))) { | 119 !ShouldBeExpired(now, page_list.at(pos))) { |
| 120 kept_pages_size += page_list.at(pos).file_size; |
| 121 kept_pages.push_back(page_list.at(pos)); |
| 88 pos++; | 122 pos++; |
| 89 } | 123 } |
| 90 | 124 |
| 91 for (int i = pos; i < page_list_size; i++) | 125 for (; pos < page_list_size; pos++) |
| 92 offline_ids.push_back(page_list.at(i).offline_id); | 126 offline_ids.push_back(page_list.at(pos).offline_id); |
| 127 } |
| 128 |
| 129 // If we're still over the clear threshold, we're going to clear remaining |
| 130 // pages from oldest last access time. |
| 131 int64_t free_space = stats.free_disk_space; |
| 132 int64_t total_size = stats.total_archives_size; |
| 133 int64_t space_to_release = |
| 134 kept_pages_size - |
| 135 (total_size + free_space) * kOfflinePageStorageClearThreshold; |
| 136 if (space_to_release > 0) { |
| 137 // Here we're sorting the |kept_pages| with oldest first. |
| 138 std::sort(kept_pages.begin(), kept_pages.end(), |
| 139 [](const OfflinePageItem& a, const OfflinePageItem& b) -> bool { |
| 140 return a.last_access_time < b.last_access_time; |
| 141 }); |
| 142 int kept_pages_size = kept_pages.size(); |
| 143 int pos = 0; |
| 144 while (pos < kept_pages_size && space_to_release > 0) { |
| 145 space_to_release -= kept_pages.at(pos).file_size; |
| 146 offline_ids.push_back(kept_pages.at(pos).offline_id); |
| 147 pos++; |
| 148 } |
| 93 } | 149 } |
| 94 } | 150 } |
| 95 | 151 |
| 96 void OfflinePageStorageManager::OnExpiredPagesDeleted( | 152 OfflinePageStorageManager::ClearMode |
| 97 const ClearPageCallback& callback, | 153 OfflinePageStorageManager::ShouldClearPages( |
| 98 int pages_cleared, | 154 const ArchiveManager::StorageStats& storage_stats) { |
| 99 DeletePageResult result) { | 155 int64_t total_size = storage_stats.total_archives_size; |
| 100 in_progress_ = false; | 156 int64_t free_space = storage_stats.free_disk_space; |
| 101 callback.Run(pages_cleared, result); | 157 if (total_size == 0) |
| 102 } | 158 return ClearMode::NOT_NEEDED; |
| 103 | 159 |
| 104 bool OfflinePageStorageManager::ShouldClearPages() { | 160 // If the size of all offline pages is more than limit, or it's larger than a |
| 105 return !in_progress_; | 161 // specified percentage of all available storage space on the disk we'll clear |
| 162 // all offline pages. |
| 163 if (total_size >= (total_size + free_space) * kOfflinePageStorageLimit) |
| 164 return ClearMode::DEFAULT; |
| 165 // If it's been more than the pre-defined interval since the last time we |
| 166 // clear the storage, we should clear pages. |
| 167 if (last_clear_time_ == base::Time() || |
| 168 clock_->Now() - last_clear_time_ >= kClearStorageInterval) { |
| 169 return ClearMode::DEFAULT; |
| 170 } |
| 171 |
| 172 // Otherwise there's no need to clear storage right now. |
| 173 return ClearMode::NOT_NEEDED; |
| 106 } | 174 } |
| 107 | 175 |
| 108 bool OfflinePageStorageManager::ShouldBeExpired(const base::Time& now, | 176 bool OfflinePageStorageManager::ShouldBeExpired(const base::Time& now, |
| 109 const OfflinePageItem& page) { | 177 const OfflinePageItem& page) { |
| 110 const LifetimePolicy& policy = | 178 const LifetimePolicy& policy = |
| 111 policy_controller_->GetPolicy(page.client_id.name_space).lifetime_policy; | 179 policy_controller_->GetPolicy(page.client_id.name_space).lifetime_policy; |
| 112 return now - page.last_access_time > policy.expiration_period; | 180 return now - page.last_access_time >= policy.expiration_period; |
| 113 } | 181 } |
| 114 | 182 |
| 115 } // namespace offline_pages | 183 } // namespace offline_pages |
| OLD | NEW |