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

Side by Side Diff: components/offline_pages/offline_page_storage_manager.cc

Issue 2489443002: Move all components/offline_pages/ files into component/offline_pages/core (Closed)
Patch Set: more rebase Created 4 years 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
OLDNEW
(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_expire;
73 std::vector<int64_t> page_ids_to_remove;
74 GetPageIdsToClear(pages, stats, &page_ids_to_expire, &page_ids_to_remove);
75 model_->ExpirePages(
76 page_ids_to_expire, clear_time_,
77 base::Bind(&OfflinePageStorageManager::OnPagesExpired,
78 weak_ptr_factory_.GetWeakPtr(), callback,
79 page_ids_to_expire.size(), page_ids_to_remove));
80 }
81
82 void OfflinePageStorageManager::OnPagesExpired(
83 const ClearStorageCallback& callback,
84 size_t pages_expired,
85 const std::vector<int64_t>& page_ids_to_remove,
86 bool expiration_succeeded) {
87 // We want to delete the outdated page records regardless the expiration
88 // succeeded or not.
89 model_->DeletePagesByOfflineId(
90 page_ids_to_remove,
91 base::Bind(&OfflinePageStorageManager::OnOutdatedPagesCleared,
92 weak_ptr_factory_.GetWeakPtr(), callback, pages_expired,
93 expiration_succeeded));
94 }
95
96 void OfflinePageStorageManager::OnOutdatedPagesCleared(
97 const ClearStorageCallback& callback,
98 size_t pages_cleared,
99 bool expiration_succeeded,
100 DeletePageResult result) {
101 ClearStorageResult clear_result = ClearStorageResult::SUCCESS;
102 if (!expiration_succeeded) {
103 clear_result = ClearStorageResult::EXPIRE_FAILURE;
104 if (result != DeletePageResult::SUCCESS)
105 clear_result = ClearStorageResult::EXPIRE_AND_DELETE_FAILURES;
106 } else if (result != DeletePageResult::SUCCESS) {
107 clear_result = ClearStorageResult::DELETE_FAILURE;
108 }
109 last_clear_time_ = clear_time_;
110 callback.Run(pages_cleared, clear_result);
111 }
112
113 void OfflinePageStorageManager::GetPageIdsToClear(
114 const MultipleOfflinePageItemResult& pages,
115 const ArchiveManager::StorageStats& stats,
116 std::vector<int64_t>* page_ids_to_expire,
117 std::vector<int64_t>* page_ids_to_remove) {
118 // TODO(romax): See how persistent should be considered here.
119 // Creating a map from namespace to a vector of page items.
120 // Sort each vector based on last accessed time and all pages after index
121 // min{size(), page_limit} should be expired. And then start iterating
122 // backwards to expire pages.
123 std::map<std::string, std::vector<OfflinePageItem>> pages_map;
124 std::vector<OfflinePageItem> kept_pages;
125 int64_t kept_pages_size = 0;
126
127 for (const auto& page : pages) {
128 if (!page.IsExpired()) {
129 pages_map[page.client_id.name_space].push_back(page);
130 } else if (clear_time_ - page.expiration_time >=
131 constants::kRemovePageItemInterval) {
132 page_ids_to_remove->push_back(page.offline_id);
133 }
134 }
135
136 for (auto& iter : pages_map) {
137 std::string name_space = iter.first;
138 std::vector<OfflinePageItem>& page_list = iter.second;
139
140 LifetimePolicy policy =
141 policy_controller_->GetPolicy(name_space).lifetime_policy;
142
143 std::sort(page_list.begin(), page_list.end(),
144 [](const OfflinePageItem& a, const OfflinePageItem& b) -> bool {
145 return a.last_access_time > b.last_access_time;
146 });
147
148 size_t page_list_size = page_list.size();
149 size_t pos = 0;
150 while (pos < page_list_size &&
151 (policy.page_limit == kUnlimitedPages || pos < policy.page_limit) &&
152 !ShouldBeExpired(page_list.at(pos))) {
153 kept_pages_size += page_list.at(pos).file_size;
154 kept_pages.push_back(page_list.at(pos));
155 pos++;
156 }
157
158 for (; pos < page_list_size; pos++)
159 page_ids_to_expire->push_back(page_list.at(pos).offline_id);
160 }
161
162 // If we're still over the clear threshold, we're going to clear remaining
163 // pages from oldest last access time.
164 int64_t free_space = stats.free_disk_space;
165 int64_t total_size = stats.total_archives_size;
166 int64_t space_to_release =
167 kept_pages_size -
168 (total_size + free_space) * constants::kOfflinePageStorageClearThreshold;
169 if (space_to_release > 0) {
170 // Here we're sorting the |kept_pages| with oldest first.
171 std::sort(kept_pages.begin(), kept_pages.end(),
172 [](const OfflinePageItem& a, const OfflinePageItem& b) -> bool {
173 return a.last_access_time < b.last_access_time;
174 });
175 size_t kept_pages_size = kept_pages.size();
176 size_t pos = 0;
177 while (pos < kept_pages_size && space_to_release > 0) {
178 space_to_release -= kept_pages.at(pos).file_size;
179 page_ids_to_expire->push_back(kept_pages.at(pos).offline_id);
180 pos++;
181 }
182 }
183 }
184
185 OfflinePageStorageManager::ClearMode
186 OfflinePageStorageManager::ShouldClearPages(
187 const ArchiveManager::StorageStats& storage_stats) {
188 int64_t total_size = storage_stats.total_archives_size;
189 int64_t free_space = storage_stats.free_disk_space;
190 if (total_size == 0)
191 return ClearMode::NOT_NEEDED;
192
193 // If the size of all offline pages is more than limit, or it's larger than a
194 // specified percentage of all available storage space on the disk we'll clear
195 // all offline pages.
196 if (total_size >=
197 (total_size + free_space) * constants::kOfflinePageStorageLimit)
198 return ClearMode::DEFAULT;
199 // If it's been more than the pre-defined interval since the last time we
200 // clear the storage, we should clear pages.
201 if (last_clear_time_ == base::Time() ||
202 clear_time_ - last_clear_time_ >= constants::kClearStorageInterval) {
203 return ClearMode::DEFAULT;
204 }
205 // Otherwise there's no need to clear storage right now.
206 return ClearMode::NOT_NEEDED;
207 }
208
209 bool OfflinePageStorageManager::ShouldBeExpired(
210 const OfflinePageItem& page) const {
211 const LifetimePolicy& policy =
212 policy_controller_->GetPolicy(page.client_id.name_space).lifetime_policy;
213 return policy.lifetime_type == LifetimeType::TEMPORARY &&
214 clear_time_ - page.last_access_time >= policy.expiration_period;
215 }
216
217 bool OfflinePageStorageManager::IsInProgress() const {
218 return clear_time_ > last_clear_time_;
219 }
220
221 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698