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> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/time/clock.h" | 10 #include "base/time/clock.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 44 } | 44 } |
| 45 | 45 |
| 46 void OfflinePageStorageManager::OnGetStorageStatsDone( | 46 void OfflinePageStorageManager::OnGetStorageStatsDone( |
| 47 const ClearPagesCallback& callback, | 47 const ClearPagesCallback& callback, |
| 48 const ArchiveManager::StorageStats& stats) { | 48 const ArchiveManager::StorageStats& stats) { |
| 49 DCHECK(in_progress_); | 49 DCHECK(in_progress_); |
| 50 ClearMode mode = ShouldClearPages(stats); | 50 ClearMode mode = ShouldClearPages(stats); |
| 51 if (mode == ClearMode::NOT_NEEDED) { | 51 if (mode == ClearMode::NOT_NEEDED) { |
| 52 in_progress_ = false; | 52 in_progress_ = false; |
| 53 last_clear_time_ = clock_->Now(); | 53 last_clear_time_ = clock_->Now(); |
| 54 callback.Run(0, ClearStorageResult::UNNECESSARY); | 54 callback.Run(0, ClearStorageResult::UNNECESSARY, true); |
| 55 return; | 55 return; |
| 56 } | 56 } |
| 57 client_->GetAllPages(base::Bind(&OfflinePageStorageManager::OnGetAllPagesDone, | 57 client_->GetAllPages(base::Bind(&OfflinePageStorageManager::OnGetAllPagesDone, |
| 58 weak_ptr_factory_.GetWeakPtr(), callback, | 58 weak_ptr_factory_.GetWeakPtr(), callback, |
| 59 stats)); | 59 stats)); |
| 60 } | 60 } |
| 61 | 61 |
| 62 void OfflinePageStorageManager::OnGetAllPagesDone( | 62 void OfflinePageStorageManager::OnGetAllPagesDone( |
| 63 const ClearPagesCallback& callback, | 63 const ClearPagesCallback& callback, |
| 64 const ArchiveManager::StorageStats& stats, | 64 const ArchiveManager::StorageStats& stats, |
| 65 const MultipleOfflinePageItemResult& pages) { | 65 const MultipleOfflinePageItemResult& pages) { |
| 66 std::vector<int64_t> offline_ids; | 66 std::vector<int64_t> page_ids_to_expire; |
| 67 GetExpiredPageIds(pages, stats, offline_ids); | 67 std::vector<int64_t> page_ids_to_remove; |
| 68 client_->DeletePagesByOfflineId( | 68 GetExpiredPageIds(pages, stats, page_ids_to_expire, page_ids_to_remove); |
| 69 offline_ids, base::Bind(&OfflinePageStorageManager::OnExpiredPagesDeleted, | 69 client_->ExpirePages( |
| 70 weak_ptr_factory_.GetWeakPtr(), callback, | 70 page_ids_to_expire, clock_->Now(), |
|
jianli
2016/05/24 23:43:32
"clock_->Now()" passed here will be different from
romax
2016/05/25 20:05:23
Done.
| |
| 71 static_cast<int>(offline_ids.size()))); | 71 base::Bind(&OfflinePageStorageManager::OnExpiredPagesDeleted, |
|
jianli
2016/05/24 23:43:32
I think OnExpiredPagesDeleted should be OnPagesExp
romax
2016/05/25 20:05:23
Done.
| |
| 72 weak_ptr_factory_.GetWeakPtr(), callback, | |
| 73 static_cast<int>(page_ids_to_expire.size()), | |
|
jianli
2016/05/24 23:43:32
Why doing all these casts, instead of simply using
romax
2016/05/25 20:05:23
Done.
| |
| 74 page_ids_to_remove)); | |
| 72 } | 75 } |
| 73 | 76 |
| 74 void OfflinePageStorageManager::OnExpiredPagesDeleted( | 77 void OfflinePageStorageManager::OnExpiredPagesDeleted( |
| 75 const ClearPagesCallback& callback, | 78 const ClearPagesCallback& callback, |
| 79 int pages_expired, | |
| 80 const std::vector<int64_t>& page_ids_to_remove, | |
| 81 DeletePageResult result) { | |
| 82 client_->RemovePageItems( | |
| 83 page_ids_to_remove, | |
| 84 base::Bind(&OfflinePageStorageManager::OnExpiredPagesCleared, | |
|
jianli
2016/05/24 23:43:32
OnExpiredPagesCleared => OnOlderExpiredPagesDelete
romax
2016/05/25 20:05:23
I'd prefer to assign names to pages in different l
| |
| 85 weak_ptr_factory_.GetWeakPtr(), callback, pages_expired, | |
| 86 result)); | |
| 87 } | |
| 88 | |
| 89 void OfflinePageStorageManager::OnExpiredPagesCleared( | |
| 90 const ClearPagesCallback& callback, | |
| 76 int pages_cleared, | 91 int pages_cleared, |
| 77 DeletePageResult result) { | 92 DeletePageResult result, |
| 93 bool success) { | |
| 78 last_clear_time_ = clock_->Now(); | 94 last_clear_time_ = clock_->Now(); |
| 79 ClearStorageResult clear_result = result == DeletePageResult::SUCCESS | 95 ClearStorageResult clear_result = result == DeletePageResult::SUCCESS |
| 80 ? ClearStorageResult::SUCCESS | 96 ? ClearStorageResult::SUCCESS |
| 81 : ClearStorageResult::DELETE_FAILURE; | 97 : ClearStorageResult::DELETE_FAILURE; |
| 82 in_progress_ = false; | 98 in_progress_ = false; |
| 83 callback.Run(pages_cleared, clear_result); | 99 callback.Run(pages_cleared, clear_result, success); |
| 84 } | 100 } |
| 85 | 101 |
| 86 void OfflinePageStorageManager::GetExpiredPageIds( | 102 void OfflinePageStorageManager::GetExpiredPageIds( |
|
jianli
2016/05/24 23:43:32
Since now we return not just expired page ids, it
romax
2016/05/25 20:05:23
Done.
| |
| 87 const MultipleOfflinePageItemResult& pages, | 103 const MultipleOfflinePageItemResult& pages, |
| 88 const ArchiveManager::StorageStats& stats, | 104 const ArchiveManager::StorageStats& stats, |
| 89 std::vector<int64_t>& offline_ids) { | 105 std::vector<int64_t>& page_ids_to_expire, |
|
jianli
2016/05/24 23:43:32
I don't think passing as reference is allowed in o
romax
2016/05/25 20:05:23
Done.
| |
| 106 std::vector<int64_t>& page_ids_to_remove) { | |
|
jianli
2016/05/24 23:43:32
ditto
romax
2016/05/25 20:05:23
Done.
| |
| 90 base::Time now = clock_->Now(); | 107 base::Time now = clock_->Now(); |
| 91 | 108 |
| 92 // Creating a map from namespace to a vector of page items. | 109 // Creating a map from namespace to a vector of page items. |
| 93 // Sort each vector based on last accessed time and all pages after index | 110 // Sort each vector based on last accessed time and all pages after index |
| 94 // min{size(), page_limit} should be expired. And then start iterating | 111 // min{size(), page_limit} should be expired. And then start iterating |
| 95 // backwards to expire pages. | 112 // backwards to expire pages. |
| 96 std::map<std::string, std::vector<OfflinePageItem>> pages_map; | 113 std::map<std::string, std::vector<OfflinePageItem>> pages_map; |
| 97 std::vector<OfflinePageItem> kept_pages; | 114 std::vector<OfflinePageItem> kept_pages; |
| 98 int64_t kept_pages_size = 0; | 115 int64_t kept_pages_size = 0; |
| 99 | 116 |
| 100 for (const auto& page : pages) | 117 for (const auto& page : pages) { |
| 101 pages_map[page.client_id.name_space].push_back(page); | 118 if (!page.IsExpired()) { |
| 119 pages_map[page.client_id.name_space].push_back(page); | |
| 120 } else if (clock_->Now() - page.expiration_time >= | |
|
jianli
2016/05/24 23:43:32
nit: cache clock_->Now() before for loop, to be mo
romax
2016/05/25 20:05:23
Done.
| |
| 121 kRemovePageItemInterval) { | |
| 122 page_ids_to_remove.push_back(page.offline_id); | |
| 123 } | |
| 124 } | |
| 102 | 125 |
| 103 for (auto& iter : pages_map) { | 126 for (auto& iter : pages_map) { |
| 104 std::string name_space = iter.first; | 127 std::string name_space = iter.first; |
| 105 std::vector<OfflinePageItem>& page_list = iter.second; | 128 std::vector<OfflinePageItem>& page_list = iter.second; |
| 106 | 129 |
| 107 LifetimePolicy policy = | 130 LifetimePolicy policy = |
| 108 policy_controller_->GetPolicy(name_space).lifetime_policy; | 131 policy_controller_->GetPolicy(name_space).lifetime_policy; |
| 109 | 132 |
| 110 std::sort(page_list.begin(), page_list.end(), | 133 std::sort(page_list.begin(), page_list.end(), |
| 111 [](const OfflinePageItem& a, const OfflinePageItem& b) -> bool { | 134 [](const OfflinePageItem& a, const OfflinePageItem& b) -> bool { |
| 112 return a.last_access_time > b.last_access_time; | 135 return a.last_access_time > b.last_access_time; |
| 113 }); | 136 }); |
| 114 | 137 |
| 115 int page_list_size = static_cast<int>(page_list.size()); | 138 int page_list_size = static_cast<int>(page_list.size()); |
| 116 int pos = 0; | 139 int pos = 0; |
| 117 while (pos < page_list_size && | 140 while (pos < page_list_size && |
| 118 (policy.page_limit == kUnlimitedPages || pos < policy.page_limit) && | 141 (policy.page_limit == kUnlimitedPages || pos < policy.page_limit) && |
| 119 !ShouldBeExpired(now, page_list.at(pos))) { | 142 !ShouldBeExpired(now, page_list.at(pos))) { |
| 120 kept_pages_size += page_list.at(pos).file_size; | 143 kept_pages_size += page_list.at(pos).file_size; |
| 121 kept_pages.push_back(page_list.at(pos)); | 144 kept_pages.push_back(page_list.at(pos)); |
| 122 pos++; | 145 pos++; |
| 123 } | 146 } |
| 124 | 147 |
| 125 for (; pos < page_list_size; pos++) | 148 for (; pos < page_list_size; pos++) |
| 126 offline_ids.push_back(page_list.at(pos).offline_id); | 149 page_ids_to_expire.push_back(page_list.at(pos).offline_id); |
| 127 } | 150 } |
| 128 | 151 |
| 129 // If we're still over the clear threshold, we're going to clear remaining | 152 // If we're still over the clear threshold, we're going to clear remaining |
| 130 // pages from oldest last access time. | 153 // pages from oldest last access time. |
| 131 int64_t free_space = stats.free_disk_space; | 154 int64_t free_space = stats.free_disk_space; |
| 132 int64_t total_size = stats.total_archives_size; | 155 int64_t total_size = stats.total_archives_size; |
| 133 int64_t space_to_release = | 156 int64_t space_to_release = |
| 134 kept_pages_size - | 157 kept_pages_size - |
| 135 (total_size + free_space) * kOfflinePageStorageClearThreshold; | 158 (total_size + free_space) * kOfflinePageStorageClearThreshold; |
| 136 if (space_to_release > 0) { | 159 if (space_to_release > 0) { |
| 137 // Here we're sorting the |kept_pages| with oldest first. | 160 // Here we're sorting the |kept_pages| with oldest first. |
| 138 std::sort(kept_pages.begin(), kept_pages.end(), | 161 std::sort(kept_pages.begin(), kept_pages.end(), |
| 139 [](const OfflinePageItem& a, const OfflinePageItem& b) -> bool { | 162 [](const OfflinePageItem& a, const OfflinePageItem& b) -> bool { |
| 140 return a.last_access_time < b.last_access_time; | 163 return a.last_access_time < b.last_access_time; |
| 141 }); | 164 }); |
| 142 int kept_pages_size = static_cast<int>(kept_pages.size()); | 165 int kept_pages_size = static_cast<int>(kept_pages.size()); |
| 143 int pos = 0; | 166 int pos = 0; |
| 144 while (pos < kept_pages_size && space_to_release > 0) { | 167 while (pos < kept_pages_size && space_to_release > 0) { |
| 145 space_to_release -= kept_pages.at(pos).file_size; | 168 space_to_release -= kept_pages.at(pos).file_size; |
| 146 offline_ids.push_back(kept_pages.at(pos).offline_id); | 169 page_ids_to_expire.push_back(kept_pages.at(pos).offline_id); |
| 147 pos++; | 170 pos++; |
| 148 } | 171 } |
| 149 } | 172 } |
| 150 } | 173 } |
| 151 | 174 |
| 152 OfflinePageStorageManager::ClearMode | 175 OfflinePageStorageManager::ClearMode |
| 153 OfflinePageStorageManager::ShouldClearPages( | 176 OfflinePageStorageManager::ShouldClearPages( |
| 154 const ArchiveManager::StorageStats& storage_stats) { | 177 const ArchiveManager::StorageStats& storage_stats) { |
| 155 int64_t total_size = storage_stats.total_archives_size; | 178 int64_t total_size = storage_stats.total_archives_size; |
| 156 int64_t free_space = storage_stats.free_disk_space; | 179 int64_t free_space = storage_stats.free_disk_space; |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 174 } | 197 } |
| 175 | 198 |
| 176 bool OfflinePageStorageManager::ShouldBeExpired(const base::Time& now, | 199 bool OfflinePageStorageManager::ShouldBeExpired(const base::Time& now, |
| 177 const OfflinePageItem& page) { | 200 const OfflinePageItem& page) { |
| 178 const LifetimePolicy& policy = | 201 const LifetimePolicy& policy = |
| 179 policy_controller_->GetPolicy(page.client_id.name_space).lifetime_policy; | 202 policy_controller_->GetPolicy(page.client_id.name_space).lifetime_policy; |
| 180 return now - page.last_access_time >= policy.expiration_period; | 203 return now - page.last_access_time >= policy.expiration_period; |
| 181 } | 204 } |
| 182 | 205 |
| 183 } // namespace offline_pages | 206 } // namespace offline_pages |
| OLD | NEW |