| OLD | NEW |
| 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/offline_pages/offline_page_item.h" | 14 #include "components/offline_pages/offline_page_item.h" |
| 15 #include "components/offline_pages/offline_page_metadata_store.h" | 15 #include "components/offline_pages/offline_page_metadata_store.h" |
| 16 #include "url/gurl.h" | 16 #include "url/gurl.h" |
| 17 | 17 |
| 18 using ArchiverResult = offline_pages::OfflinePageArchiver::ArchiverResult; | 18 using ArchiverResult = offline_pages::OfflinePageArchiver::ArchiverResult; |
| 19 using SavePageResult = offline_pages::OfflinePageModel::SavePageResult; | 19 using SavePageResult = offline_pages::OfflinePageModel::SavePageResult; |
| 20 | 20 |
| 21 namespace offline_pages { | 21 namespace offline_pages { |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 // Threshold for how old offline copy of a page should be before we offer to |
| 26 // delete it to free up space. |
| 27 const base::TimeDelta kPageCleanUpThreshold = base::TimeDelta::FromDays(30); |
| 28 |
| 25 SavePageResult ToSavePageResult(ArchiverResult archiver_result) { | 29 SavePageResult ToSavePageResult(ArchiverResult archiver_result) { |
| 26 SavePageResult result; | 30 SavePageResult result; |
| 27 switch (archiver_result) { | 31 switch (archiver_result) { |
| 28 case ArchiverResult::SUCCESSFULLY_CREATED: | 32 case ArchiverResult::SUCCESSFULLY_CREATED: |
| 29 result = SavePageResult::SUCCESS; | 33 result = SavePageResult::SUCCESS; |
| 30 break; | 34 break; |
| 31 case ArchiverResult::ERROR_DEVICE_FULL: | 35 case ArchiverResult::ERROR_DEVICE_FULL: |
| 32 result = SavePageResult::DEVICE_FULL; | 36 result = SavePageResult::DEVICE_FULL; |
| 33 break; | 37 break; |
| 34 case ArchiverResult::ERROR_CONTENT_UNAVAILABLE: | 38 case ArchiverResult::ERROR_CONTENT_UNAVAILABLE: |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 } | 138 } |
| 135 | 139 |
| 136 const std::vector<OfflinePageItem> OfflinePageModel::GetAllPages() const { | 140 const std::vector<OfflinePageItem> OfflinePageModel::GetAllPages() const { |
| 137 DCHECK(is_loaded_); | 141 DCHECK(is_loaded_); |
| 138 std::vector<OfflinePageItem> offline_pages; | 142 std::vector<OfflinePageItem> offline_pages; |
| 139 for (const auto& id_page_pair : offline_pages_) | 143 for (const auto& id_page_pair : offline_pages_) |
| 140 offline_pages.push_back(id_page_pair.second); | 144 offline_pages.push_back(id_page_pair.second); |
| 141 return offline_pages; | 145 return offline_pages; |
| 142 } | 146 } |
| 143 | 147 |
| 148 const std::vector<OfflinePageItem> OfflinePageModel::GetPagesToCleanUp() const { |
| 149 DCHECK(is_loaded_); |
| 150 std::vector<OfflinePageItem> offline_pages; |
| 151 base::Time now = base::Time::Now(); |
| 152 for (const auto& id_page_pair : offline_pages_) { |
| 153 if (now - id_page_pair.second.creation_time > kPageCleanUpThreshold) |
| 154 offline_pages.push_back(id_page_pair.second); |
| 155 } |
| 156 return offline_pages; |
| 157 } |
| 158 |
| 144 bool OfflinePageModel::GetPageByBookmarkId( | 159 bool OfflinePageModel::GetPageByBookmarkId( |
| 145 int64 bookmark_id, | 160 int64 bookmark_id, |
| 146 OfflinePageItem* offline_page) const { | 161 OfflinePageItem* offline_page) const { |
| 147 DCHECK(offline_page); | 162 DCHECK(offline_page); |
| 148 | 163 |
| 149 const auto iter = offline_pages_.find(bookmark_id); | 164 const auto iter = offline_pages_.find(bookmark_id); |
| 150 if (iter != offline_pages_.end()) { | 165 if (iter != offline_pages_.end()) { |
| 151 *offline_page = iter->second; | 166 *offline_page = iter->second; |
| 152 return true; | 167 return true; |
| 153 } | 168 } |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 for (int64 bookmark_id : bookmark_ids) | 285 for (int64 bookmark_id : bookmark_ids) |
| 271 offline_pages_.erase(bookmark_id); | 286 offline_pages_.erase(bookmark_id); |
| 272 // Deleting multiple pages always succeeds when it gets to this point. | 287 // Deleting multiple pages always succeeds when it gets to this point. |
| 273 if (success || bookmark_ids.size() > 1) | 288 if (success || bookmark_ids.size() > 1) |
| 274 callback.Run(DeletePageResult::SUCCESS); | 289 callback.Run(DeletePageResult::SUCCESS); |
| 275 else | 290 else |
| 276 callback.Run(DeletePageResult::STORE_FAILURE); | 291 callback.Run(DeletePageResult::STORE_FAILURE); |
| 277 } | 292 } |
| 278 | 293 |
| 279 } // namespace offline_pages | 294 } // namespace offline_pages |
| OLD | NEW |