Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/offline_pages/offline_page_storage_manager.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "components/offline_pages/offline_page_client_policy.h" | |
| 9 #include "components/offline_pages/offline_page_item.h" | |
| 10 | |
| 11 using DeletePageResult = offline_pages::OfflinePageModel::DeletePageResult; | |
| 12 using MultipleOfflinePageItemResult = | |
| 13 offline_pages::OfflinePageModel::MultipleOfflinePageItemResult; | |
| 14 | |
| 15 namespace offline_pages { | |
| 16 | |
| 17 OfflinePageStorageManager::OfflinePageStorageManager(OfflinePageModel* model, | |
| 18 int64_t totalSize) | |
| 19 : model_(model), | |
| 20 policy_controller_(model->GetPolicyController()), | |
| 21 in_progress_(false), | |
| 22 size_(totalSize), | |
| 23 weak_ptr_factory_(this) { | |
| 24 } | |
| 25 | |
| 26 OfflinePageStorageManager::~OfflinePageStorageManager() { | |
| 27 } | |
| 28 | |
| 29 void OfflinePageStorageManager::ClearPagesIfNeeded( | |
| 30 const ClearPageCallback& callback) { | |
| 31 if (in_progress_ || !ShouldClearPages()) | |
| 32 return; | |
| 33 in_progress_ = true; | |
| 34 model_->GetAllPages(base::Bind(&OfflinePageStorageManager::PurgePages, | |
| 35 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 36 } | |
| 37 | |
| 38 void OfflinePageStorageManager::PurgePages( | |
| 39 const ClearPageCallback& callback, | |
| 40 const MultipleOfflinePageItemResult& pages) { | |
| 41 std::vector<int64_t> offline_ids; | |
| 42 int pages_to_clear = GetExpiredPageIds(pages, offline_ids); | |
|
fgorski
2016/05/05 04:53:08
what is the problem you are trying to solve with a
romax
2016/05/05 21:00:02
Acknowledged.
| |
| 43 model_->DeletePagesByOfflineId( | |
| 44 offline_ids, | |
| 45 base::Bind(&OfflinePageStorageManager::OnExpiredPagesDeleted, | |
| 46 weak_ptr_factory_.GetWeakPtr(), callback, pages_to_clear)); | |
| 47 } | |
| 48 | |
| 49 int OfflinePageStorageManager::GetExpiredPageIds( | |
| 50 const MultipleOfflinePageItemResult& pages, | |
| 51 std::vector<int64_t>& offline_ids) { | |
| 52 base::Time now = base::Time::Now(); | |
|
fgorski
2016/05/05 04:53:08
this will be more flexible and testable using base
romax
2016/05/05 21:00:03
Acknowledged.
| |
| 53 for (const auto& page : pages) { | |
| 54 if (now - page.last_access_time > | |
| 55 policy_controller_->GetPolicy(page.client_id.name_space) | |
| 56 .lifetime_policy.expiration_period) { | |
| 57 offline_ids.push_back(page.offline_id); | |
| 58 } | |
| 59 } | |
| 60 return offline_ids.size(); | |
|
fgorski
2016/05/05 04:53:08
this information is already available outside thro
romax
2016/05/05 21:00:02
Done.
| |
| 61 } | |
| 62 | |
| 63 void OfflinePageStorageManager::OnExpiredPagesDeleted( | |
| 64 const ClearPageCallback& callback, | |
| 65 int pages_to_clear, | |
| 66 DeletePageResult result) { | |
| 67 if (result != DeletePageResult::SUCCESS) | |
|
fgorski
2016/05/05 04:53:08
this may not be exactly what you want here.
romax
2016/05/05 21:00:02
let's talk about this...
| |
| 68 pages_to_clear = 0; | |
| 69 callback.Run(pages_to_clear); | |
| 70 } | |
| 71 | |
| 72 bool OfflinePageStorageManager::ShouldClearPages() { | |
| 73 return model_->is_loaded(); | |
|
fgorski
2016/05/05 04:53:08
We should pretty much always clear pages?
Where is
romax
2016/05/05 21:00:03
I also mentioned it's not implemented yet in the C
| |
| 74 } | |
| 75 | |
| 76 bool OfflinePageStorageManager::isPageExpired(const OfflinePageItem& page) { | |
|
fgorski
2016/05/05 04:53:08
I think you meant to use this method above?
romax
2016/05/05 21:00:02
yeah.. red bull killed me...
| |
| 77 DCHECK(model_); | |
|
fgorski
2016/05/05 04:53:08
why?
romax
2016/05/05 21:00:02
Done.
| |
| 78 base::Time now = base::Time::Now(); | |
| 79 LifetimePolicy policy = | |
| 80 policy_controller_->GetPolicy(page.client_id.name_space).lifetime_policy; | |
| 81 if (now - page.last_access_time > policy.expiration_period) | |
| 82 return true; | |
| 83 return false; | |
|
fgorski
2016/05/05 04:53:08
three lines above is a one-liner (return is bool)
romax
2016/05/05 21:00:02
Done.
| |
| 84 } | |
| 85 | |
| 86 } // namespace offline_pages | |
| OLD | NEW |