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

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

Issue 1307753002: [Offline pages] Adding capability to free up space used by Offline pages (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Converting jlongarray to std::vector<int64> Created 5 years, 3 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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_model.h" 5 #include "components/offline_pages/offline_page_model.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/files/file_util.h" 10 #include "base/files/file_util.h"
11 #include "base/location.h" 11 #include "base/location.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/sequenced_task_runner.h" 13 #include "base/sequenced_task_runner.h"
14 #include "components/bookmarks/browser/bookmark_model.h" 14 #include "components/bookmarks/browser/bookmark_model.h"
15 #include "components/bookmarks/browser/bookmark_node.h" 15 #include "components/bookmarks/browser/bookmark_node.h"
16 #include "components/offline_pages/offline_page_item.h" 16 #include "components/offline_pages/offline_page_item.h"
17 #include "components/offline_pages/offline_page_metadata_store.h" 17 #include "components/offline_pages/offline_page_metadata_store.h"
18 #include "url/gurl.h" 18 #include "url/gurl.h"
19 19
20 using ArchiverResult = offline_pages::OfflinePageArchiver::ArchiverResult; 20 using ArchiverResult = offline_pages::OfflinePageArchiver::ArchiverResult;
21 using SavePageResult = offline_pages::OfflinePageModel::SavePageResult; 21 using SavePageResult = offline_pages::OfflinePageModel::SavePageResult;
22 22
23 namespace offline_pages { 23 namespace offline_pages {
24 24
25 namespace { 25 namespace {
26 26
27 // Threshold for how old offline copy of a page should be before we offer to
28 // delete it to free up space.
29 const base::TimeDelta kPageCleanUpThreshold = base::TimeDelta::FromDays(30);
30
27 SavePageResult ToSavePageResult(ArchiverResult archiver_result) { 31 SavePageResult ToSavePageResult(ArchiverResult archiver_result) {
28 SavePageResult result; 32 SavePageResult result;
29 switch (archiver_result) { 33 switch (archiver_result) {
30 case ArchiverResult::SUCCESSFULLY_CREATED: 34 case ArchiverResult::SUCCESSFULLY_CREATED:
31 result = SavePageResult::SUCCESS; 35 result = SavePageResult::SUCCESS;
32 break; 36 break;
33 case ArchiverResult::ERROR_DEVICE_FULL: 37 case ArchiverResult::ERROR_DEVICE_FULL:
34 result = SavePageResult::DEVICE_FULL; 38 result = SavePageResult::DEVICE_FULL;
35 break; 39 break;
36 case ArchiverResult::ERROR_CONTENT_UNAVAILABLE: 40 case ArchiverResult::ERROR_CONTENT_UNAVAILABLE:
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 } 149 }
146 150
147 const std::vector<OfflinePageItem> OfflinePageModel::GetAllPages() const { 151 const std::vector<OfflinePageItem> OfflinePageModel::GetAllPages() const {
148 DCHECK(is_loaded_); 152 DCHECK(is_loaded_);
149 std::vector<OfflinePageItem> offline_pages; 153 std::vector<OfflinePageItem> offline_pages;
150 for (const auto& id_page_pair : offline_pages_) 154 for (const auto& id_page_pair : offline_pages_)
151 offline_pages.push_back(id_page_pair.second); 155 offline_pages.push_back(id_page_pair.second);
152 return offline_pages; 156 return offline_pages;
153 } 157 }
154 158
159 const std::vector<OfflinePageItem> OfflinePageModel::GetPagesToCleanUp() const {
160 DCHECK(is_loaded_);
161 std::vector<OfflinePageItem> offline_pages;
162 base::Time now = base::Time::Now();
163 for (const auto& id_page_pair : offline_pages_) {
164 if (now - id_page_pair.second.creation_time > kPageCleanUpThreshold)
165 offline_pages.push_back(id_page_pair.second);
166 }
167 return offline_pages;
168 }
169
155 bool OfflinePageModel::GetPageByBookmarkId( 170 bool OfflinePageModel::GetPageByBookmarkId(
156 int64 bookmark_id, 171 int64 bookmark_id,
157 OfflinePageItem* offline_page) const { 172 OfflinePageItem* offline_page) const {
158 DCHECK(offline_page); 173 DCHECK(offline_page);
159 174
160 const auto iter = offline_pages_.find(bookmark_id); 175 const auto iter = offline_pages_.find(bookmark_id);
161 if (iter != offline_pages_.end()) { 176 if (iter != offline_pages_.end()) {
162 *offline_page = iter->second; 177 *offline_page = iter->second;
163 return true; 178 return true;
164 } 179 }
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 for (int64 bookmark_id : bookmark_ids) 321 for (int64 bookmark_id : bookmark_ids)
307 offline_pages_.erase(bookmark_id); 322 offline_pages_.erase(bookmark_id);
308 // Deleting multiple pages always succeeds when it gets to this point. 323 // Deleting multiple pages always succeeds when it gets to this point.
309 if (success || bookmark_ids.size() > 1) 324 if (success || bookmark_ids.size() > 1)
310 callback.Run(DeletePageResult::SUCCESS); 325 callback.Run(DeletePageResult::SUCCESS);
311 else 326 else
312 callback.Run(DeletePageResult::STORE_FAILURE); 327 callback.Run(DeletePageResult::STORE_FAILURE);
313 } 328 }
314 329
315 } // namespace offline_pages 330 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/offline_page_model.h ('k') | components/offline_pages/offline_page_model_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698