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

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

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

Powered by Google App Engine
This is Rietveld 408576698