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

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

Powered by Google App Engine
This is Rietveld 408576698