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 "components/offline_pages/client_policy_controller.h" | 12 #include "components/offline_pages/client_policy_controller.h" |
13 #include "components/offline_pages/offline_page_client_policy.h" | 13 #include "components/offline_pages/offline_page_client_policy.h" |
14 #include "components/offline_pages/offline_page_item.h" | 14 #include "components/offline_pages/offline_page_item.h" |
15 #include "components/offline_pages/offline_page_model.h" | 15 #include "components/offline_pages/offline_page_model.h" |
16 | 16 |
17 using LifetimeType = offline_pages::LifetimePolicy::LifetimeType; | 17 using LifetimeType = offline_pages::LifetimePolicy::LifetimeType; |
18 | 18 |
19 namespace offline_pages { | 19 namespace offline_pages { |
20 | 20 |
21 constexpr double constants::kOfflinePageStorageLimit; | |
fgorski
2016/11/17 22:10:15
just to double-check: are these necessary?
brucedawson
2016/11/17 22:19:39
Yeah, they are needed in order to reserve storage
| |
22 constexpr double constants::kOfflinePageStorageClearThreshold; | |
23 constexpr base::TimeDelta constants::kClearStorageInterval; | |
24 constexpr base::TimeDelta constants::kRemovePageItemInterval; | |
25 | |
21 OfflinePageStorageManager::OfflinePageStorageManager( | 26 OfflinePageStorageManager::OfflinePageStorageManager( |
22 OfflinePageModel* model, | 27 OfflinePageModel* model, |
23 ClientPolicyController* policy_controller, | 28 ClientPolicyController* policy_controller, |
24 ArchiveManager* archive_manager) | 29 ArchiveManager* archive_manager) |
25 : model_(model), | 30 : model_(model), |
26 policy_controller_(policy_controller), | 31 policy_controller_(policy_controller), |
27 archive_manager_(archive_manager), | 32 archive_manager_(archive_manager), |
28 clock_(new base::DefaultClock()), | 33 clock_(new base::DefaultClock()), |
29 weak_ptr_factory_(this) {} | 34 weak_ptr_factory_(this) {} |
30 | 35 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
115 // Sort each vector based on last accessed time and all pages after index | 120 // Sort each vector based on last accessed time and all pages after index |
116 // min{size(), page_limit} should be expired. And then start iterating | 121 // min{size(), page_limit} should be expired. And then start iterating |
117 // backwards to expire pages. | 122 // backwards to expire pages. |
118 std::map<std::string, std::vector<OfflinePageItem>> pages_map; | 123 std::map<std::string, std::vector<OfflinePageItem>> pages_map; |
119 std::vector<OfflinePageItem> kept_pages; | 124 std::vector<OfflinePageItem> kept_pages; |
120 int64_t kept_pages_size = 0; | 125 int64_t kept_pages_size = 0; |
121 | 126 |
122 for (const auto& page : pages) { | 127 for (const auto& page : pages) { |
123 if (!page.IsExpired()) { | 128 if (!page.IsExpired()) { |
124 pages_map[page.client_id.name_space].push_back(page); | 129 pages_map[page.client_id.name_space].push_back(page); |
125 } else if (clear_time_ - page.expiration_time >= kRemovePageItemInterval) { | 130 } else if (clear_time_ - page.expiration_time >= |
131 constants::kRemovePageItemInterval) { | |
126 page_ids_to_remove->push_back(page.offline_id); | 132 page_ids_to_remove->push_back(page.offline_id); |
127 } | 133 } |
128 } | 134 } |
129 | 135 |
130 for (auto& iter : pages_map) { | 136 for (auto& iter : pages_map) { |
131 std::string name_space = iter.first; | 137 std::string name_space = iter.first; |
132 std::vector<OfflinePageItem>& page_list = iter.second; | 138 std::vector<OfflinePageItem>& page_list = iter.second; |
133 | 139 |
134 LifetimePolicy policy = | 140 LifetimePolicy policy = |
135 policy_controller_->GetPolicy(name_space).lifetime_policy; | 141 policy_controller_->GetPolicy(name_space).lifetime_policy; |
(...skipping 16 matching lines...) Expand all Loading... | |
152 for (; pos < page_list_size; pos++) | 158 for (; pos < page_list_size; pos++) |
153 page_ids_to_expire->push_back(page_list.at(pos).offline_id); | 159 page_ids_to_expire->push_back(page_list.at(pos).offline_id); |
154 } | 160 } |
155 | 161 |
156 // If we're still over the clear threshold, we're going to clear remaining | 162 // If we're still over the clear threshold, we're going to clear remaining |
157 // pages from oldest last access time. | 163 // pages from oldest last access time. |
158 int64_t free_space = stats.free_disk_space; | 164 int64_t free_space = stats.free_disk_space; |
159 int64_t total_size = stats.total_archives_size; | 165 int64_t total_size = stats.total_archives_size; |
160 int64_t space_to_release = | 166 int64_t space_to_release = |
161 kept_pages_size - | 167 kept_pages_size - |
162 (total_size + free_space) * kOfflinePageStorageClearThreshold; | 168 (total_size + free_space) * constants::kOfflinePageStorageClearThreshold; |
163 if (space_to_release > 0) { | 169 if (space_to_release > 0) { |
164 // Here we're sorting the |kept_pages| with oldest first. | 170 // Here we're sorting the |kept_pages| with oldest first. |
165 std::sort(kept_pages.begin(), kept_pages.end(), | 171 std::sort(kept_pages.begin(), kept_pages.end(), |
166 [](const OfflinePageItem& a, const OfflinePageItem& b) -> bool { | 172 [](const OfflinePageItem& a, const OfflinePageItem& b) -> bool { |
167 return a.last_access_time < b.last_access_time; | 173 return a.last_access_time < b.last_access_time; |
168 }); | 174 }); |
169 size_t kept_pages_size = kept_pages.size(); | 175 size_t kept_pages_size = kept_pages.size(); |
170 size_t pos = 0; | 176 size_t pos = 0; |
171 while (pos < kept_pages_size && space_to_release > 0) { | 177 while (pos < kept_pages_size && space_to_release > 0) { |
172 space_to_release -= kept_pages.at(pos).file_size; | 178 space_to_release -= kept_pages.at(pos).file_size; |
173 page_ids_to_expire->push_back(kept_pages.at(pos).offline_id); | 179 page_ids_to_expire->push_back(kept_pages.at(pos).offline_id); |
174 pos++; | 180 pos++; |
175 } | 181 } |
176 } | 182 } |
177 } | 183 } |
178 | 184 |
179 OfflinePageStorageManager::ClearMode | 185 OfflinePageStorageManager::ClearMode |
180 OfflinePageStorageManager::ShouldClearPages( | 186 OfflinePageStorageManager::ShouldClearPages( |
181 const ArchiveManager::StorageStats& storage_stats) { | 187 const ArchiveManager::StorageStats& storage_stats) { |
182 int64_t total_size = storage_stats.total_archives_size; | 188 int64_t total_size = storage_stats.total_archives_size; |
183 int64_t free_space = storage_stats.free_disk_space; | 189 int64_t free_space = storage_stats.free_disk_space; |
184 if (total_size == 0) | 190 if (total_size == 0) |
185 return ClearMode::NOT_NEEDED; | 191 return ClearMode::NOT_NEEDED; |
186 | 192 |
187 // If the size of all offline pages is more than limit, or it's larger than a | 193 // If the size of all offline pages is more than limit, or it's larger than a |
188 // specified percentage of all available storage space on the disk we'll clear | 194 // specified percentage of all available storage space on the disk we'll clear |
189 // all offline pages. | 195 // all offline pages. |
190 if (total_size >= (total_size + free_space) * kOfflinePageStorageLimit) | 196 if (total_size >= |
197 (total_size + free_space) * constants::kOfflinePageStorageLimit) | |
191 return ClearMode::DEFAULT; | 198 return ClearMode::DEFAULT; |
192 // If it's been more than the pre-defined interval since the last time we | 199 // If it's been more than the pre-defined interval since the last time we |
193 // clear the storage, we should clear pages. | 200 // clear the storage, we should clear pages. |
194 if (last_clear_time_ == base::Time() || | 201 if (last_clear_time_ == base::Time() || |
195 clear_time_ - last_clear_time_ >= kClearStorageInterval) { | 202 clear_time_ - last_clear_time_ >= constants::kClearStorageInterval) { |
196 return ClearMode::DEFAULT; | 203 return ClearMode::DEFAULT; |
197 } | 204 } |
198 // Otherwise there's no need to clear storage right now. | 205 // Otherwise there's no need to clear storage right now. |
199 return ClearMode::NOT_NEEDED; | 206 return ClearMode::NOT_NEEDED; |
200 } | 207 } |
201 | 208 |
202 bool OfflinePageStorageManager::ShouldBeExpired( | 209 bool OfflinePageStorageManager::ShouldBeExpired( |
203 const OfflinePageItem& page) const { | 210 const OfflinePageItem& page) const { |
204 const LifetimePolicy& policy = | 211 const LifetimePolicy& policy = |
205 policy_controller_->GetPolicy(page.client_id.name_space).lifetime_policy; | 212 policy_controller_->GetPolicy(page.client_id.name_space).lifetime_policy; |
206 return policy.lifetime_type == LifetimeType::TEMPORARY && | 213 return policy.lifetime_type == LifetimeType::TEMPORARY && |
207 clear_time_ - page.last_access_time >= policy.expiration_period; | 214 clear_time_ - page.last_access_time >= policy.expiration_period; |
208 } | 215 } |
209 | 216 |
210 bool OfflinePageStorageManager::IsInProgress() const { | 217 bool OfflinePageStorageManager::IsInProgress() const { |
211 return clear_time_ > last_clear_time_; | 218 return clear_time_ > last_clear_time_; |
212 } | 219 } |
213 | 220 |
214 } // namespace offline_pages | 221 } // namespace offline_pages |
OLD | NEW |