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