Chromium Code Reviews| 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 const base::TimeDelta kPageCleanUpThreshold = base::TimeDelta::FromDays(30); | |
|
jianli
2015/08/21 23:39:21
nit: add comment like: pages which are created 30
fgorski
2015/08/24 18:47:14
Done.
| |
| 26 | |
| 25 SavePageResult ToSavePageResult(ArchiverResult archiver_result) { | 27 SavePageResult ToSavePageResult(ArchiverResult archiver_result) { |
| 26 SavePageResult result; | 28 SavePageResult result; |
| 27 switch (archiver_result) { | 29 switch (archiver_result) { |
| 28 case ArchiverResult::SUCCESSFULLY_CREATED: | 30 case ArchiverResult::SUCCESSFULLY_CREATED: |
| 29 result = SavePageResult::SUCCESS; | 31 result = SavePageResult::SUCCESS; |
| 30 break; | 32 break; |
| 31 case ArchiverResult::ERROR_DEVICE_FULL: | 33 case ArchiverResult::ERROR_DEVICE_FULL: |
| 32 result = SavePageResult::DEVICE_FULL; | 34 result = SavePageResult::DEVICE_FULL; |
| 33 break; | 35 break; |
| 34 case ArchiverResult::ERROR_CONTENT_UNAVAILABLE: | 36 case ArchiverResult::ERROR_CONTENT_UNAVAILABLE: |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 } | 136 } |
| 135 | 137 |
| 136 const std::vector<OfflinePageItem> OfflinePageModel::GetAllPages() const { | 138 const std::vector<OfflinePageItem> OfflinePageModel::GetAllPages() const { |
| 137 DCHECK(is_loaded_); | 139 DCHECK(is_loaded_); |
| 138 std::vector<OfflinePageItem> offline_pages; | 140 std::vector<OfflinePageItem> offline_pages; |
| 139 for (const auto& id_page_pair : offline_pages_) | 141 for (const auto& id_page_pair : offline_pages_) |
| 140 offline_pages.push_back(id_page_pair.second); | 142 offline_pages.push_back(id_page_pair.second); |
| 141 return offline_pages; | 143 return offline_pages; |
| 142 } | 144 } |
| 143 | 145 |
| 146 const std::vector<OfflinePageItem> OfflinePageModel::GetPagesToCleanUp() const { | |
|
jianli
2015/08/21 23:39:21
add test
fgorski
2015/08/24 18:47:14
Done.
| |
| 147 DCHECK(is_loaded_); | |
| 148 std::vector<OfflinePageItem> offline_pages; | |
| 149 base::Time now = base::Time::Now(); | |
| 150 for (const auto& id_page_pair : offline_pages_) { | |
| 151 if (now - id_page_pair.second.creation_time > kPageCleanUpThreshold) | |
| 152 offline_pages.push_back(id_page_pair.second); | |
| 153 } | |
| 154 return offline_pages; | |
| 155 } | |
| 156 | |
| 144 bool OfflinePageModel::GetPageByBookmarkId( | 157 bool OfflinePageModel::GetPageByBookmarkId( |
| 145 int64 bookmark_id, | 158 int64 bookmark_id, |
| 146 OfflinePageItem* offline_page) const { | 159 OfflinePageItem* offline_page) const { |
| 147 DCHECK(offline_page); | 160 DCHECK(offline_page); |
| 148 | 161 |
| 149 const auto iter = offline_pages_.find(bookmark_id); | 162 const auto iter = offline_pages_.find(bookmark_id); |
| 150 if (iter != offline_pages_.end()) { | 163 if (iter != offline_pages_.end()) { |
| 151 *offline_page = iter->second; | 164 *offline_page = iter->second; |
| 152 return true; | 165 return true; |
| 153 } | 166 } |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 270 for (int64 bookmark_id : bookmark_ids) | 283 for (int64 bookmark_id : bookmark_ids) |
| 271 offline_pages_.erase(bookmark_id); | 284 offline_pages_.erase(bookmark_id); |
| 272 // Deleting multiple pages always succeeds when it gets to this point. | 285 // Deleting multiple pages always succeeds when it gets to this point. |
| 273 if (success || bookmark_ids.size() > 1) | 286 if (success || bookmark_ids.size() > 1) |
| 274 callback.Run(DeletePageResult::SUCCESS); | 287 callback.Run(DeletePageResult::SUCCESS); |
| 275 else | 288 else |
| 276 callback.Run(DeletePageResult::STORE_FAILURE); | 289 callback.Run(DeletePageResult::STORE_FAILURE); |
| 277 } | 290 } |
| 278 | 291 |
| 279 } // namespace offline_pages | 292 } // namespace offline_pages |
| OLD | NEW |