Chromium Code Reviews| 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" |
| 8 #include "components/offline_pages/client_policy_controller.h" | 10 #include "components/offline_pages/client_policy_controller.h" |
| 9 #include "components/offline_pages/offline_page_client_policy.h" | 11 #include "components/offline_pages/offline_page_client_policy.h" |
| 10 #include "components/offline_pages/offline_page_item.h" | 12 #include "components/offline_pages/offline_page_item.h" |
| 11 #include "components/offline_pages/offline_page_types.h" | 13 #include "components/offline_pages/offline_page_types.h" |
| 12 | 14 |
| 13 namespace offline_pages { | 15 namespace offline_pages { |
| 14 | 16 |
| 15 OfflinePageStorageManager::OfflinePageStorageManager( | 17 OfflinePageStorageManager::OfflinePageStorageManager( |
| 16 Client* client, | 18 Client* client, |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 39 GetExpiredPageIds(pages, offline_ids); | 41 GetExpiredPageIds(pages, offline_ids); |
| 40 client_->DeletePagesByOfflineId( | 42 client_->DeletePagesByOfflineId( |
| 41 offline_ids, | 43 offline_ids, |
| 42 base::Bind(&OfflinePageStorageManager::OnExpiredPagesDeleted, | 44 base::Bind(&OfflinePageStorageManager::OnExpiredPagesDeleted, |
| 43 weak_ptr_factory_.GetWeakPtr(), callback, offline_ids.size())); | 45 weak_ptr_factory_.GetWeakPtr(), callback, offline_ids.size())); |
| 44 } | 46 } |
| 45 | 47 |
| 46 void OfflinePageStorageManager::GetExpiredPageIds( | 48 void OfflinePageStorageManager::GetExpiredPageIds( |
| 47 const MultipleOfflinePageItemResult& pages, | 49 const MultipleOfflinePageItemResult& pages, |
| 48 std::vector<int64_t>& offline_ids) { | 50 std::vector<int64_t>& offline_ids) { |
| 51 base::Time now = base::Time::Now(); | |
|
fgorski
2016/05/12 14:49:32
use base::Clock for tracking time. ability to inje
romax
2016/05/12 22:20:03
Done.
| |
| 52 | |
| 53 // Creating a map from namespace to a vector of page items. | |
| 54 // Sort each vector based on last accessed time and all pages after index | |
| 55 // min{size(), page_limit} should be expired. And then start iterating | |
|
fgorski
2016/05/12 14:49:32
did you consider an alternative, where you sort by
romax
2016/05/12 22:20:03
I thought it's more convenient and intuitive to us
| |
| 56 // backwards to expire pages. | |
| 57 std::map<std::string, std::vector<OfflinePageItem>> pages_map; | |
| 58 pages_map.clear(); | |
|
fgorski
2016/05/12 14:49:32
is this necessary?
romax
2016/05/12 22:20:03
it's not
| |
| 59 | |
| 49 for (const auto& page : pages) { | 60 for (const auto& page : pages) { |
|
fgorski
2016/05/12 14:49:32
nit: single condition with single line if/for/whil
romax
2016/05/12 22:20:03
Done.
| |
| 50 if (IsPageExpired(page)) | 61 pages_map[page.client_id.name_space].push_back(page); |
| 51 offline_ids.push_back(page.offline_id); | 62 } |
| 63 for (auto& iter : pages_map) { | |
| 64 std::string name_space = iter.first; | |
| 65 std::vector<OfflinePageItem>& page_list = iter.second; | |
| 66 | |
| 67 LifetimePolicy policy = | |
| 68 policy_controller_->GetPolicy(name_space).lifetime_policy; | |
| 69 | |
| 70 std::sort(page_list.begin(), page_list.end(), | |
| 71 [](const OfflinePageItem& a, const OfflinePageItem& b) -> bool { | |
| 72 return a.last_access_time > b.last_access_time; | |
| 73 }); | |
|
fgorski
2016/05/12 14:49:32
nit: put an empty line here, and remove the one be
romax
2016/05/12 22:20:03
Done.
| |
| 74 int page_list_size = page_list.size(); | |
| 75 int pos = 0; | |
| 76 | |
| 77 while (pos < page_list_size && | |
| 78 (policy.page_limit == kUnlimitedPages || pos < policy.page_limit) && | |
| 79 !IsPageExpired(now, page_list.at(pos))) | |
| 80 pos++; | |
| 81 | |
| 82 for (int i = pos; i < page_list_size; i++) { | |
| 83 offline_ids.push_back(page_list.at(i).offline_id); | |
| 84 } | |
| 52 } | 85 } |
| 53 } | 86 } |
| 54 | 87 |
| 55 void OfflinePageStorageManager::OnExpiredPagesDeleted( | 88 void OfflinePageStorageManager::OnExpiredPagesDeleted( |
| 56 const ClearPageCallback& callback, | 89 const ClearPageCallback& callback, |
| 57 int pages_cleared, | 90 int pages_cleared, |
| 58 DeletePageResult result) { | 91 DeletePageResult result) { |
| 59 in_progress_ = false; | 92 in_progress_ = false; |
| 60 callback.Run(pages_cleared, result); | 93 callback.Run(pages_cleared, result); |
| 61 } | 94 } |
| 62 | 95 |
| 63 bool OfflinePageStorageManager::ShouldClearPages() { | 96 bool OfflinePageStorageManager::ShouldClearPages() { |
| 64 return !in_progress_; | 97 return !in_progress_; |
| 65 } | 98 } |
| 66 | 99 |
| 67 bool OfflinePageStorageManager::IsPageExpired(const OfflinePageItem& page) { | 100 bool OfflinePageStorageManager::IsPageExpired(const base::Time& now, |
|
fgorski
2016/05/12 14:49:32
Can you change the name to ShouldBeExpired? This w
romax
2016/05/12 22:20:03
Done.
| |
| 68 base::Time now = base::Time::Now(); | 101 const OfflinePageItem& page) { |
| 69 const LifetimePolicy& policy = | 102 const LifetimePolicy& policy = |
| 70 policy_controller_->GetPolicy(page.client_id.name_space).lifetime_policy; | 103 policy_controller_->GetPolicy(page.client_id.name_space).lifetime_policy; |
| 71 return now - page.last_access_time > policy.expiration_period; | 104 return now - page.last_access_time > policy.expiration_period; |
| 72 } | 105 } |
| 73 | |
| 74 } // namespace offline_pages | 106 } // namespace offline_pages |
| OLD | NEW |