| OLD | NEW |
| 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 <algorithm> |
| 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/time/clock.h" |
| 11 #include "base/time/default_clock.h" |
| 12 #include "base/time/time.h" |
| 8 #include "components/offline_pages/client_policy_controller.h" | 13 #include "components/offline_pages/client_policy_controller.h" |
| 9 #include "components/offline_pages/offline_page_client_policy.h" | 14 #include "components/offline_pages/offline_page_client_policy.h" |
| 10 #include "components/offline_pages/offline_page_item.h" | 15 #include "components/offline_pages/offline_page_item.h" |
| 11 #include "components/offline_pages/offline_page_types.h" | 16 #include "components/offline_pages/offline_page_types.h" |
| 12 | 17 |
| 13 namespace offline_pages { | 18 namespace offline_pages { |
| 14 | 19 |
| 15 OfflinePageStorageManager::OfflinePageStorageManager( | 20 OfflinePageStorageManager::OfflinePageStorageManager( |
| 16 Client* client, | 21 Client* client, |
| 17 ClientPolicyController* policy_controller) | 22 ClientPolicyController* policy_controller) |
| 18 : client_(client), | 23 : client_(client), |
| 19 policy_controller_(policy_controller), | 24 policy_controller_(policy_controller), |
| 20 in_progress_(false), | 25 in_progress_(false), |
| 26 clock_(new base::DefaultClock()), |
| 21 weak_ptr_factory_(this) {} | 27 weak_ptr_factory_(this) {} |
| 22 | 28 |
| 23 OfflinePageStorageManager::~OfflinePageStorageManager() {} | 29 OfflinePageStorageManager::~OfflinePageStorageManager() {} |
| 24 | 30 |
| 25 void OfflinePageStorageManager::ClearPagesIfNeeded( | 31 void OfflinePageStorageManager::ClearPagesIfNeeded( |
| 26 const ClearPageCallback& callback) { | 32 const ClearPageCallback& callback) { |
| 27 if (!ShouldClearPages()) | 33 if (!ShouldClearPages()) |
| 28 return; | 34 return; |
| 29 in_progress_ = true; | 35 in_progress_ = true; |
| 30 client_->GetAllPages(base::Bind(&OfflinePageStorageManager::ClearExpiredPages, | 36 client_->GetAllPages(base::Bind(&OfflinePageStorageManager::ClearExpiredPages, |
| 31 weak_ptr_factory_.GetWeakPtr(), callback)); | 37 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 32 } | 38 } |
| 33 | 39 |
| 34 void OfflinePageStorageManager::ClearExpiredPages( | 40 void OfflinePageStorageManager::ClearExpiredPages( |
| 35 const ClearPageCallback& callback, | 41 const ClearPageCallback& callback, |
| 36 const MultipleOfflinePageItemResult& pages) { | 42 const MultipleOfflinePageItemResult& pages) { |
| 37 DCHECK(in_progress_); | 43 DCHECK(in_progress_); |
| 38 std::vector<int64_t> offline_ids; | 44 std::vector<int64_t> offline_ids; |
| 39 GetExpiredPageIds(pages, offline_ids); | 45 GetExpiredPageIds(pages, offline_ids); |
| 40 client_->DeletePagesByOfflineId( | 46 client_->DeletePagesByOfflineId( |
| 41 offline_ids, | 47 offline_ids, |
| 42 base::Bind(&OfflinePageStorageManager::OnExpiredPagesDeleted, | 48 base::Bind(&OfflinePageStorageManager::OnExpiredPagesDeleted, |
| 43 weak_ptr_factory_.GetWeakPtr(), callback, offline_ids.size())); | 49 weak_ptr_factory_.GetWeakPtr(), callback, offline_ids.size())); |
| 44 } | 50 } |
| 45 | 51 |
| 52 void OfflinePageStorageManager::SetClockForTesting( |
| 53 std::unique_ptr<base::Clock> clock) { |
| 54 clock_ = std::move(clock); |
| 55 } |
| 56 |
| 46 void OfflinePageStorageManager::GetExpiredPageIds( | 57 void OfflinePageStorageManager::GetExpiredPageIds( |
| 47 const MultipleOfflinePageItemResult& pages, | 58 const MultipleOfflinePageItemResult& pages, |
| 48 std::vector<int64_t>& offline_ids) { | 59 std::vector<int64_t>& offline_ids) { |
| 49 for (const auto& page : pages) { | 60 base::Time now = clock_->Now(); |
| 50 if (IsPageExpired(page)) | 61 |
| 51 offline_ids.push_back(page.offline_id); | 62 // 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 |
| 64 // min{size(), page_limit} should be expired. And then start iterating |
| 65 // backwards to expire pages. |
| 66 std::map<std::string, std::vector<OfflinePageItem>> pages_map; |
| 67 |
| 68 for (const auto& page : pages) |
| 69 pages_map[page.client_id.name_space].push_back(page); |
| 70 |
| 71 for (auto& iter : pages_map) { |
| 72 std::string name_space = iter.first; |
| 73 std::vector<OfflinePageItem>& page_list = iter.second; |
| 74 |
| 75 LifetimePolicy policy = |
| 76 policy_controller_->GetPolicy(name_space).lifetime_policy; |
| 77 |
| 78 std::sort(page_list.begin(), page_list.end(), |
| 79 [](const OfflinePageItem& a, const OfflinePageItem& b) -> bool { |
| 80 return a.last_access_time > b.last_access_time; |
| 81 }); |
| 82 |
| 83 int page_list_size = page_list.size(); |
| 84 int pos = 0; |
| 85 while (pos < page_list_size && |
| 86 (policy.page_limit == kUnlimitedPages || pos < policy.page_limit) && |
| 87 !ShouldBeExpired(now, page_list.at(pos))) { |
| 88 pos++; |
| 89 } |
| 90 |
| 91 for (int i = pos; i < page_list_size; i++) |
| 92 offline_ids.push_back(page_list.at(i).offline_id); |
| 52 } | 93 } |
| 53 } | 94 } |
| 54 | 95 |
| 55 void OfflinePageStorageManager::OnExpiredPagesDeleted( | 96 void OfflinePageStorageManager::OnExpiredPagesDeleted( |
| 56 const ClearPageCallback& callback, | 97 const ClearPageCallback& callback, |
| 57 int pages_cleared, | 98 int pages_cleared, |
| 58 DeletePageResult result) { | 99 DeletePageResult result) { |
| 59 in_progress_ = false; | 100 in_progress_ = false; |
| 60 callback.Run(pages_cleared, result); | 101 callback.Run(pages_cleared, result); |
| 61 } | 102 } |
| 62 | 103 |
| 63 bool OfflinePageStorageManager::ShouldClearPages() { | 104 bool OfflinePageStorageManager::ShouldClearPages() { |
| 64 return !in_progress_; | 105 return !in_progress_; |
| 65 } | 106 } |
| 66 | 107 |
| 67 bool OfflinePageStorageManager::IsPageExpired(const OfflinePageItem& page) { | 108 bool OfflinePageStorageManager::ShouldBeExpired(const base::Time& now, |
| 68 base::Time now = base::Time::Now(); | 109 const OfflinePageItem& page) { |
| 69 const LifetimePolicy& policy = | 110 const LifetimePolicy& policy = |
| 70 policy_controller_->GetPolicy(page.client_id.name_space).lifetime_policy; | 111 policy_controller_->GetPolicy(page.client_id.name_space).lifetime_policy; |
| 71 return now - page.last_access_time > policy.expiration_period; | 112 return now - page.last_access_time > policy.expiration_period; |
| 72 } | 113 } |
| 73 | 114 |
| 74 } // namespace offline_pages | 115 } // namespace offline_pages |
| OLD | NEW |