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/sys_info.h" | |
| 10 #include "base/time/clock.h" | 11 #include "base/time/clock.h" |
| 11 #include "base/time/default_clock.h" | 12 #include "base/time/default_clock.h" |
| 12 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 13 #include "components/offline_pages/client_policy_controller.h" | 14 #include "components/offline_pages/client_policy_controller.h" |
| 14 #include "components/offline_pages/offline_page_client_policy.h" | 15 #include "components/offline_pages/offline_page_client_policy.h" |
| 15 #include "components/offline_pages/offline_page_item.h" | 16 #include "components/offline_pages/offline_page_item.h" |
| 16 #include "components/offline_pages/offline_page_types.h" | 17 #include "components/offline_pages/offline_page_types.h" |
| 18 #include "components/prefs/pref_service.h" | |
| 17 | 19 |
| 18 namespace offline_pages { | 20 namespace offline_pages { |
| 19 | 21 |
| 20 OfflinePageStorageManager::OfflinePageStorageManager( | 22 OfflinePageStorageManager::OfflinePageStorageManager( |
| 21 Client* client, | 23 Client* client, |
| 22 ClientPolicyController* policy_controller) | 24 ClientPolicyController* policy_controller, |
| 25 PrefService* prefs) | |
| 23 : client_(client), | 26 : client_(client), |
| 24 policy_controller_(policy_controller), | 27 policy_controller_(policy_controller), |
| 28 prefs_(prefs), | |
| 25 in_progress_(false), | 29 in_progress_(false), |
| 26 clock_(new base::DefaultClock()), | 30 clock_(new base::DefaultClock()), |
| 27 weak_ptr_factory_(this) {} | 31 weak_ptr_factory_(this) {} |
| 28 | 32 |
| 29 OfflinePageStorageManager::~OfflinePageStorageManager() {} | 33 OfflinePageStorageManager::~OfflinePageStorageManager() {} |
| 30 | 34 |
| 31 void OfflinePageStorageManager::ClearPagesIfNeeded( | 35 void OfflinePageStorageManager::ClearPagesIfNeeded( |
| 32 const ClearPageCallback& callback) { | 36 const ClearPageCallback& callback) { |
| 33 if (!ShouldClearPages()) | 37 if (in_progress_) |
| 34 return; | 38 return; |
| 35 in_progress_ = true; | 39 in_progress_ = true; |
| 36 client_->GetAllPages(base::Bind(&OfflinePageStorageManager::ClearExpiredPages, | 40 client_->GetAllPages(base::Bind(&OfflinePageStorageManager::ClearExpiredPages, |
| 37 weak_ptr_factory_.GetWeakPtr(), callback)); | 41 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 38 } | 42 } |
| 39 | 43 |
| 40 void OfflinePageStorageManager::ClearExpiredPages( | 44 void OfflinePageStorageManager::ClearExpiredPages( |
| 41 const ClearPageCallback& callback, | 45 const ClearPageCallback& callback, |
| 42 const MultipleOfflinePageItemResult& pages) { | 46 const MultipleOfflinePageItemResult& pages) { |
| 43 DCHECK(in_progress_); | 47 DCHECK(in_progress_); |
| 48 | |
| 49 ClearMode mode = ShouldClearPages(pages); | |
| 44 std::vector<int64_t> offline_ids; | 50 std::vector<int64_t> offline_ids; |
| 45 GetExpiredPageIds(pages, offline_ids); | 51 |
| 52 switch (mode) { | |
| 53 case ClearMode::CLEAR_ALL: | |
| 54 for (const auto& page : pages) | |
| 55 offline_ids.push_back(page.offline_id); | |
| 56 break; | |
| 57 case ClearMode::DEFAULT: | |
| 58 GetExpiredPageIds(pages, offline_ids); | |
| 59 break; | |
| 60 case ClearMode::NO_NEED: | |
| 61 in_progress_ = false; | |
| 62 if (prefs_) { | |
| 63 prefs_->SetInt64(kOfflinePageStorageLastClearedTime, | |
| 64 clock_->Now().ToInternalValue()); | |
| 65 } | |
| 66 callback.Run(0, StorageClearResult::UNNECESSARY); | |
| 67 return; | |
| 68 } | |
| 69 | |
| 46 client_->DeletePagesByOfflineId( | 70 client_->DeletePagesByOfflineId( |
| 47 offline_ids, | 71 offline_ids, |
| 48 base::Bind(&OfflinePageStorageManager::OnExpiredPagesDeleted, | 72 base::Bind(&OfflinePageStorageManager::OnExpiredPagesDeleted, |
| 49 weak_ptr_factory_.GetWeakPtr(), callback, offline_ids.size())); | 73 weak_ptr_factory_.GetWeakPtr(), callback, offline_ids.size())); |
| 50 } | 74 } |
| 51 | 75 |
| 52 void OfflinePageStorageManager::SetClockForTesting( | 76 void OfflinePageStorageManager::SetClockForTesting( |
| 53 std::unique_ptr<base::Clock> clock) { | 77 std::unique_ptr<base::Clock> clock) { |
| 54 clock_ = std::move(clock); | 78 clock_ = std::move(clock); |
| 55 } | 79 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 90 | 114 |
| 91 for (int i = pos; i < page_list_size; i++) | 115 for (int i = pos; i < page_list_size; i++) |
| 92 offline_ids.push_back(page_list.at(i).offline_id); | 116 offline_ids.push_back(page_list.at(i).offline_id); |
| 93 } | 117 } |
| 94 } | 118 } |
| 95 | 119 |
| 96 void OfflinePageStorageManager::OnExpiredPagesDeleted( | 120 void OfflinePageStorageManager::OnExpiredPagesDeleted( |
| 97 const ClearPageCallback& callback, | 121 const ClearPageCallback& callback, |
| 98 int pages_cleared, | 122 int pages_cleared, |
| 99 DeletePageResult result) { | 123 DeletePageResult result) { |
| 124 if (prefs_) { | |
| 125 prefs_->SetInt64(kOfflinePageStorageLastClearedTime, | |
| 126 clock_->Now().ToInternalValue()); | |
| 127 } | |
| 128 StorageClearResult clear_result = result == DeletePageResult::SUCCESS | |
| 129 ? StorageClearResult::SUCCESS | |
| 130 : StorageClearResult::DELETE_FAIL; | |
| 100 in_progress_ = false; | 131 in_progress_ = false; |
| 101 callback.Run(pages_cleared, result); | 132 callback.Run(pages_cleared, clear_result); |
| 102 } | 133 } |
| 103 | 134 |
| 104 bool OfflinePageStorageManager::ShouldClearPages() { | 135 OfflinePageStorageManager::ClearMode |
| 105 return !in_progress_; | 136 OfflinePageStorageManager::ShouldClearPages( |
| 137 const MultipleOfflinePageItemResult& pages) { | |
| 138 if (pages.size() == 0) | |
| 139 return ClearMode::NO_NEED; | |
| 140 | |
| 141 // If it's been more than the pre-defined gap since the last time we clear the | |
|
fgorski
2016/05/17 05:33:29
interval
romax
2016/05/18 02:36:19
Done.
| |
| 142 // storage, we should clear pages. | |
| 143 if (prefs_) { | |
| 144 int64_t last_clear_time = | |
| 145 prefs_->GetInt64(kOfflinePageStorageLastClearedTime); | |
| 146 if (last_clear_time == 0 || | |
| 147 clock_->Now() - base::Time::FromInternalValue(last_clear_time) >= | |
| 148 kClearStorageTimeGap) { | |
| 149 return ClearMode::DEFAULT; | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 // If the size of all offline pages is more than limit, or it's larger than a | |
| 154 // specified percentage of all available storage space on the disk we'll clear | |
| 155 // all offline pages. | |
| 156 int64_t total_size = 0; | |
| 157 for (const auto& page : pages) | |
| 158 total_size += page.file_size; | |
| 159 | |
| 160 int64_t free_space = base::SysInfo::AmountOfFreeDiskSpace(pages[0].file_path); | |
|
fgorski
2016/05/17 05:33:29
This requires IO Thread and to my knowledge you ar
| |
| 161 if (total_size > kOfflinePageStorageLimit || | |
| 162 total_size > | |
| 163 (total_size + free_space) * kOfflinePageStorageLimitPercentage) { | |
| 164 return ClearMode::CLEAR_ALL; | |
|
fgorski
2016/05/17 05:33:29
This is too aggressive. Why would you remove every
romax
2016/05/18 02:36:19
will change per discussion
| |
| 165 } | |
| 166 return ClearMode::NO_NEED; | |
| 106 } | 167 } |
| 107 | 168 |
| 108 bool OfflinePageStorageManager::ShouldBeExpired(const base::Time& now, | 169 bool OfflinePageStorageManager::ShouldBeExpired(const base::Time& now, |
| 109 const OfflinePageItem& page) { | 170 const OfflinePageItem& page) { |
| 110 const LifetimePolicy& policy = | 171 const LifetimePolicy& policy = |
| 111 policy_controller_->GetPolicy(page.client_id.name_space).lifetime_policy; | 172 policy_controller_->GetPolicy(page.client_id.name_space).lifetime_policy; |
| 112 return now - page.last_access_time > policy.expiration_period; | 173 return now - page.last_access_time >= policy.expiration_period; |
| 113 } | 174 } |
| 114 | 175 |
| 115 } // namespace offline_pages | 176 } // namespace offline_pages |
| OLD | NEW |