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 OLD_PAGE_INTERVAL = base::TimeDelta::FromDays(30); | |
|
jianli
2015/08/21 21:06:27
We should use C++ style constant here. Also it is
fgorski
2015/08/21 22:09:39
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 { | |
| 147 DCHECK(is_loaded_); | |
| 148 std::vector<OfflinePageItem> offline_pages; | |
| 149 for (const auto& id_page : offline_pages_) { | |
| 150 if (base::Time::Now() - id_page.second.creation_time > OLD_PAGE_INTERVAL) | |
| 151 offline_pages.push_back(id_page.second); | |
|
jianli
2015/08/21 21:06:27
nit: it is better to store Now time before for loo
fgorski
2015/08/21 22:09:39
Done.
| |
| 152 } | |
| 153 return offline_pages; | |
| 154 } | |
| 155 | |
| 144 bool OfflinePageModel::GetPageByBookmarkId( | 156 bool OfflinePageModel::GetPageByBookmarkId( |
| 145 int64 bookmark_id, | 157 int64 bookmark_id, |
| 146 OfflinePageItem* offline_page) const { | 158 OfflinePageItem* offline_page) const { |
| 147 DCHECK(offline_page); | 159 DCHECK(offline_page); |
| 148 | 160 |
| 149 const auto iter = offline_pages_.find(bookmark_id); | 161 const auto iter = offline_pages_.find(bookmark_id); |
| 150 if (iter != offline_pages_.end()) { | 162 if (iter != offline_pages_.end()) { |
| 151 *offline_page = iter->second; | 163 *offline_page = iter->second; |
| 152 return true; | 164 return true; |
| 153 } | 165 } |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 270 for (int64 bookmark_id : bookmark_ids) | 282 for (int64 bookmark_id : bookmark_ids) |
| 271 offline_pages_.erase(bookmark_id); | 283 offline_pages_.erase(bookmark_id); |
| 272 // Deleting multiple pages always succeeds when it gets to this point. | 284 // Deleting multiple pages always succeeds when it gets to this point. |
| 273 if (success || bookmark_ids.size() > 1) | 285 if (success || bookmark_ids.size() > 1) |
| 274 callback.Run(DeletePageResult::SUCCESS); | 286 callback.Run(DeletePageResult::SUCCESS); |
| 275 else | 287 else |
| 276 callback.Run(DeletePageResult::STORE_FAILURE); | 288 callback.Run(DeletePageResult::STORE_FAILURE); |
| 277 } | 289 } |
| 278 | 290 |
| 279 } // namespace offline_pages | 291 } // namespace offline_pages |
| OLD | NEW |