| OLD | NEW |
| 1 // Copyright 2015 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_model.h" | 5 #include "components/offline_pages/offline_page_model_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
| 15 #include "base/optional.h" | 15 #include "base/optional.h" |
| 16 #include "base/rand_util.h" | 16 #include "base/rand_util.h" |
| 17 #include "base/sequenced_task_runner.h" | 17 #include "base/sequenced_task_runner.h" |
| 18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 43 // be deleted or re-ordered and new ones should be added to the end. | 43 // be deleted or re-ordered and new ones should be added to the end. |
| 44 enum ClearAllStatus { | 44 enum ClearAllStatus { |
| 45 CLEAR_ALL_SUCCEEDED, | 45 CLEAR_ALL_SUCCEEDED, |
| 46 STORE_RESET_FAILED, | 46 STORE_RESET_FAILED, |
| 47 STORE_RELOAD_FAILED, | 47 STORE_RELOAD_FAILED, |
| 48 | 48 |
| 49 // NOTE: always keep this entry at the end. | 49 // NOTE: always keep this entry at the end. |
| 50 CLEAR_ALL_STATUS_COUNT | 50 CLEAR_ALL_STATUS_COUNT |
| 51 }; | 51 }; |
| 52 | 52 |
| 53 int64_t GenerateOfflineId() { |
| 54 return base::RandGenerator(std::numeric_limits<int64_t>::max()) + 1; |
| 55 } |
| 56 |
| 53 // The maximum histogram size for the metrics that measure time between views of | 57 // The maximum histogram size for the metrics that measure time between views of |
| 54 // a given page. | 58 // a given page. |
| 55 const base::TimeDelta kMaxOpenedPageHistogramBucket = | 59 const base::TimeDelta kMaxOpenedPageHistogramBucket = |
| 56 base::TimeDelta::FromDays(90); | 60 base::TimeDelta::FromDays(90); |
| 57 | 61 |
| 58 SavePageResult ToSavePageResult(ArchiverResult archiver_result) { | 62 SavePageResult ToSavePageResult(ArchiverResult archiver_result) { |
| 59 SavePageResult result; | 63 SavePageResult result; |
| 60 switch (archiver_result) { | 64 switch (archiver_result) { |
| 61 case ArchiverResult::SUCCESSFULLY_CREATED: | 65 case ArchiverResult::SUCCESSFULLY_CREATED: |
| 62 result = SavePageResult::SUCCESS; | 66 result = SavePageResult::SUCCESS; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 (storage_stats.total_archives_size + storage_stats.free_disk_space) * | 130 (storage_stats.total_archives_size + storage_stats.free_disk_space) * |
| 127 100); | 131 100); |
| 128 UMA_HISTOGRAM_PERCENTAGE( | 132 UMA_HISTOGRAM_PERCENTAGE( |
| 129 "OfflinePages.DeletePage.TotalPageSizeAsPercentageOfFreeSpace", | 133 "OfflinePages.DeletePage.TotalPageSizeAsPercentageOfFreeSpace", |
| 130 percentage_of_free); | 134 percentage_of_free); |
| 131 } | 135 } |
| 132 } | 136 } |
| 133 | 137 |
| 134 } // namespace | 138 } // namespace |
| 135 | 139 |
| 136 // static | 140 // protected |
| 137 bool OfflinePageModel::CanSavePage(const GURL& url) { | 141 OfflinePageModelImpl::OfflinePageModelImpl() |
| 138 return url.SchemeIsHTTPOrHTTPS(); | 142 : OfflinePageModel(), is_loaded_(false), weak_ptr_factory_(this) {} |
| 139 } | |
| 140 | 143 |
| 141 // protected | 144 OfflinePageModelImpl::OfflinePageModelImpl( |
| 142 OfflinePageModel::OfflinePageModel() | |
| 143 : is_loaded_(false), weak_ptr_factory_(this) {} | |
| 144 | |
| 145 OfflinePageModel::OfflinePageModel( | |
| 146 std::unique_ptr<OfflinePageMetadataStore> store, | 145 std::unique_ptr<OfflinePageMetadataStore> store, |
| 147 const base::FilePath& archives_dir, | 146 const base::FilePath& archives_dir, |
| 148 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | 147 const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
| 149 : store_(std::move(store)), | 148 : store_(std::move(store)), |
| 150 archives_dir_(archives_dir), | 149 archives_dir_(archives_dir), |
| 151 is_loaded_(false), | 150 is_loaded_(false), |
| 152 policy_controller_(new ClientPolicyController()), | 151 policy_controller_(new ClientPolicyController()), |
| 153 archive_manager_(new ArchiveManager(archives_dir, task_runner)), | 152 archive_manager_(new ArchiveManager(archives_dir, task_runner)), |
| 154 weak_ptr_factory_(this) { | 153 weak_ptr_factory_(this) { |
| 155 archive_manager_->EnsureArchivesDirCreated( | 154 archive_manager_->EnsureArchivesDirCreated( |
| 156 base::Bind(&OfflinePageModel::OnEnsureArchivesDirCreatedDone, | 155 base::Bind(&OfflinePageModelImpl::OnEnsureArchivesDirCreatedDone, |
| 157 weak_ptr_factory_.GetWeakPtr(), base::TimeTicks::Now())); | 156 weak_ptr_factory_.GetWeakPtr(), base::TimeTicks::Now())); |
| 158 } | 157 } |
| 159 | 158 |
| 160 OfflinePageModel::~OfflinePageModel() { | 159 OfflinePageModelImpl::~OfflinePageModelImpl() {} |
| 161 } | |
| 162 | 160 |
| 163 void OfflinePageModel::AddObserver(Observer* observer) { | 161 void OfflinePageModelImpl::AddObserver(Observer* observer) { |
| 164 observers_.AddObserver(observer); | 162 observers_.AddObserver(observer); |
| 165 } | 163 } |
| 166 | 164 |
| 167 void OfflinePageModel::RemoveObserver(Observer* observer) { | 165 void OfflinePageModelImpl::RemoveObserver(Observer* observer) { |
| 168 observers_.RemoveObserver(observer); | 166 observers_.RemoveObserver(observer); |
| 169 } | 167 } |
| 170 | 168 |
| 171 void OfflinePageModel::SavePage(const GURL& url, | 169 void OfflinePageModelImpl::SavePage( |
| 172 const ClientId& client_id, | 170 const GURL& url, |
| 173 std::unique_ptr<OfflinePageArchiver> archiver, | 171 const ClientId& client_id, |
| 174 const SavePageCallback& callback) { | 172 std::unique_ptr<OfflinePageArchiver> archiver, |
| 173 const SavePageCallback& callback) { |
| 175 DCHECK(is_loaded_); | 174 DCHECK(is_loaded_); |
| 176 | 175 |
| 177 // Skip saving the page that is not intended to be saved, like local file | 176 // Skip saving the page that is not intended to be saved, like local file |
| 178 // page. | 177 // page. |
| 179 if (url.is_valid() && !CanSavePage(url)) { | 178 if (url.is_valid() && !CanSavePage(url)) { |
| 180 InformSavePageDone(callback, SavePageResult::SKIPPED, client_id, | 179 InformSavePageDone(callback, SavePageResult::SKIPPED, client_id, |
| 181 kInvalidOfflineId); | 180 kInvalidOfflineId); |
| 182 return; | 181 return; |
| 183 } | 182 } |
| 184 | 183 |
| 185 // The web contents is not available if archiver is not created and passed. | 184 // The web contents is not available if archiver is not created and passed. |
| 186 if (!archiver.get()) { | 185 if (!archiver.get()) { |
| 187 InformSavePageDone(callback, SavePageResult::CONTENT_UNAVAILABLE, client_id, | 186 InformSavePageDone(callback, SavePageResult::CONTENT_UNAVAILABLE, client_id, |
| 188 kInvalidOfflineId); | 187 kInvalidOfflineId); |
| 189 return; | 188 return; |
| 190 } | 189 } |
| 191 | 190 |
| 192 int64_t offline_id = GenerateOfflineId(); | 191 int64_t offline_id = GenerateOfflineId(); |
| 193 | 192 |
| 194 archiver->CreateArchive( | 193 archiver->CreateArchive( |
| 195 archives_dir_, | 194 archives_dir_, offline_id, |
| 196 offline_id, | 195 base::Bind(&OfflinePageModelImpl::OnCreateArchiveDone, |
| 197 base::Bind(&OfflinePageModel::OnCreateArchiveDone, | 196 weak_ptr_factory_.GetWeakPtr(), url, offline_id, client_id, |
| 198 weak_ptr_factory_.GetWeakPtr(), url, offline_id, | 197 base::Time::Now(), callback)); |
| 199 client_id, base::Time::Now(), callback)); | |
| 200 pending_archivers_.push_back(std::move(archiver)); | 198 pending_archivers_.push_back(std::move(archiver)); |
| 201 } | 199 } |
| 202 | 200 |
| 203 void OfflinePageModel::MarkPageAccessed(int64_t offline_id) { | 201 void OfflinePageModelImpl::MarkPageAccessed(int64_t offline_id) { |
| 204 DCHECK(is_loaded_); | 202 DCHECK(is_loaded_); |
| 205 auto iter = offline_pages_.find(offline_id); | 203 auto iter = offline_pages_.find(offline_id); |
| 206 if (iter == offline_pages_.end()) | 204 if (iter == offline_pages_.end()) |
| 207 return; | 205 return; |
| 208 | 206 |
| 209 // Make a copy of the cached item and update it. The cached item should only | 207 // Make a copy of the cached item and update it. The cached item should only |
| 210 // be updated upon the successful store operation. | 208 // be updated upon the successful store operation. |
| 211 OfflinePageItem offline_page_item = iter->second; | 209 OfflinePageItem offline_page_item = iter->second; |
| 212 | 210 |
| 213 base::Time now = base::Time::Now(); | 211 base::Time now = base::Time::Now(); |
| 214 base::TimeDelta time_since_last_accessed = | 212 base::TimeDelta time_since_last_accessed = |
| 215 now - offline_page_item.last_access_time; | 213 now - offline_page_item.last_access_time; |
| 216 | 214 |
| 217 // When the access account is still zero, the page is opened for the first | 215 // When the access account is still zero, the page is opened for the first |
| 218 // time since its creation. | 216 // time since its creation. |
| 219 UMA_HISTOGRAM_CUSTOM_COUNTS( | 217 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 220 AddHistogramSuffix( | 218 AddHistogramSuffix(offline_page_item.client_id, |
| 221 offline_page_item.client_id, | 219 (offline_page_item.access_count == 0) |
| 222 (offline_page_item.access_count == 0) ? | 220 ? "OfflinePages.FirstOpenSinceCreated" |
| 223 "OfflinePages.FirstOpenSinceCreated" : | 221 : "OfflinePages.OpenSinceLastOpen") |
| 224 "OfflinePages.OpenSinceLastOpen").c_str(), | 222 .c_str(), |
| 225 time_since_last_accessed.InMinutes(), 1, | 223 time_since_last_accessed.InMinutes(), 1, |
| 226 kMaxOpenedPageHistogramBucket.InMinutes(), 50); | 224 kMaxOpenedPageHistogramBucket.InMinutes(), 50); |
| 227 | 225 |
| 228 offline_page_item.last_access_time = now; | 226 offline_page_item.last_access_time = now; |
| 229 offline_page_item.access_count++; | 227 offline_page_item.access_count++; |
| 230 | 228 |
| 231 store_->AddOrUpdateOfflinePage( | 229 store_->AddOrUpdateOfflinePage( |
| 232 offline_page_item, | 230 offline_page_item, |
| 233 base::Bind(&OfflinePageModel::OnMarkPageAccesseDone, | 231 base::Bind(&OfflinePageModelImpl::OnMarkPageAccesseDone, |
| 234 weak_ptr_factory_.GetWeakPtr(), offline_page_item)); | 232 weak_ptr_factory_.GetWeakPtr(), offline_page_item)); |
| 235 } | 233 } |
| 236 | 234 |
| 237 void OfflinePageModel::DeletePagesByOfflineId( | 235 void OfflinePageModelImpl::DeletePagesByOfflineId( |
| 238 const std::vector<int64_t>& offline_ids, | 236 const std::vector<int64_t>& offline_ids, |
| 239 const DeletePageCallback& callback) { | 237 const DeletePageCallback& callback) { |
| 240 if (!is_loaded_) { | 238 if (!is_loaded_) { |
| 241 delayed_tasks_.push_back( | 239 delayed_tasks_.push_back( |
| 242 base::Bind(&OfflinePageModel::DoDeletePagesByOfflineId, | 240 base::Bind(&OfflinePageModelImpl::DoDeletePagesByOfflineId, |
| 243 weak_ptr_factory_.GetWeakPtr(), offline_ids, callback)); | 241 weak_ptr_factory_.GetWeakPtr(), offline_ids, callback)); |
| 244 | 242 |
| 245 return; | 243 return; |
| 246 } | 244 } |
| 247 DoDeletePagesByOfflineId(offline_ids, callback); | 245 DoDeletePagesByOfflineId(offline_ids, callback); |
| 248 } | 246 } |
| 249 | 247 |
| 250 void OfflinePageModel::DoDeletePagesByOfflineId( | 248 void OfflinePageModelImpl::DoDeletePagesByOfflineId( |
| 251 const std::vector<int64_t>& offline_ids, | 249 const std::vector<int64_t>& offline_ids, |
| 252 const DeletePageCallback& callback) { | 250 const DeletePageCallback& callback) { |
| 253 DCHECK(is_loaded_); | 251 DCHECK(is_loaded_); |
| 254 | 252 |
| 255 std::vector<base::FilePath> paths_to_delete; | 253 std::vector<base::FilePath> paths_to_delete; |
| 256 for (const auto& offline_id : offline_ids) { | 254 for (const auto& offline_id : offline_ids) { |
| 257 auto iter = offline_pages_.find(offline_id); | 255 auto iter = offline_pages_.find(offline_id); |
| 258 if (iter != offline_pages_.end() && !iter->second.IsExpired()) { | 256 if (iter != offline_pages_.end() && !iter->second.IsExpired()) { |
| 259 paths_to_delete.push_back(iter->second.file_path); | 257 paths_to_delete.push_back(iter->second.file_path); |
| 260 } | 258 } |
| 261 } | 259 } |
| 262 | 260 |
| 263 if (paths_to_delete.empty()) { | 261 if (paths_to_delete.empty()) { |
| 264 InformDeletePageDone(callback, DeletePageResult::NOT_FOUND); | 262 InformDeletePageDone(callback, DeletePageResult::NOT_FOUND); |
| 265 return; | 263 return; |
| 266 } | 264 } |
| 267 | 265 |
| 268 archive_manager_->DeleteMultipleArchives( | 266 archive_manager_->DeleteMultipleArchives( |
| 269 paths_to_delete, | 267 paths_to_delete, |
| 270 base::Bind(&OfflinePageModel::OnDeleteArchiveFilesDone, | 268 base::Bind(&OfflinePageModelImpl::OnDeleteArchiveFilesDone, |
| 271 weak_ptr_factory_.GetWeakPtr(), offline_ids, callback)); | 269 weak_ptr_factory_.GetWeakPtr(), offline_ids, callback)); |
| 272 } | 270 } |
| 273 | 271 |
| 274 void OfflinePageModel::ClearAll(const base::Closure& callback) { | 272 void OfflinePageModelImpl::ClearAll(const base::Closure& callback) { |
| 275 DCHECK(is_loaded_); | 273 DCHECK(is_loaded_); |
| 276 | 274 |
| 277 std::vector<int64_t> offline_ids; | 275 std::vector<int64_t> offline_ids; |
| 278 for (const auto& id_page_pair : offline_pages_) | 276 for (const auto& id_page_pair : offline_pages_) |
| 279 offline_ids.push_back(id_page_pair.first); | 277 offline_ids.push_back(id_page_pair.first); |
| 280 DeletePagesByOfflineId( | 278 DeletePagesByOfflineId( |
| 281 offline_ids, | 279 offline_ids, |
| 282 base::Bind(&OfflinePageModel::OnRemoveAllFilesDoneForClearAll, | 280 base::Bind(&OfflinePageModelImpl::OnRemoveAllFilesDoneForClearAll, |
| 283 weak_ptr_factory_.GetWeakPtr(), callback)); | 281 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 284 } | 282 } |
| 285 | 283 |
| 286 void OfflinePageModel::DeletePagesByURLPredicate( | 284 void OfflinePageModelImpl::DeletePagesByURLPredicate( |
| 287 const UrlPredicate& predicate, | 285 const UrlPredicate& predicate, |
| 288 const DeletePageCallback& callback) { | 286 const DeletePageCallback& callback) { |
| 289 if (!is_loaded_) { | 287 if (!is_loaded_) { |
| 290 delayed_tasks_.push_back( | 288 delayed_tasks_.push_back( |
| 291 base::Bind(&OfflinePageModel::DoDeletePagesByURLPredicate, | 289 base::Bind(&OfflinePageModelImpl::DoDeletePagesByURLPredicate, |
| 292 weak_ptr_factory_.GetWeakPtr(), predicate, callback)); | 290 weak_ptr_factory_.GetWeakPtr(), predicate, callback)); |
| 293 | 291 |
| 294 return; | 292 return; |
| 295 } | 293 } |
| 296 DoDeletePagesByURLPredicate(predicate, callback); | 294 DoDeletePagesByURLPredicate(predicate, callback); |
| 297 } | 295 } |
| 298 | 296 |
| 299 void OfflinePageModel::DoDeletePagesByURLPredicate( | 297 void OfflinePageModelImpl::DoDeletePagesByURLPredicate( |
| 300 const UrlPredicate& predicate, | 298 const UrlPredicate& predicate, |
| 301 const DeletePageCallback& callback) { | 299 const DeletePageCallback& callback) { |
| 302 DCHECK(is_loaded_); | 300 DCHECK(is_loaded_); |
| 303 | 301 |
| 304 std::vector<int64_t> offline_ids; | 302 std::vector<int64_t> offline_ids; |
| 305 for (const auto& id_page_pair : offline_pages_) { | 303 for (const auto& id_page_pair : offline_pages_) { |
| 306 if (predicate.Run(id_page_pair.second.url)) | 304 if (predicate.Run(id_page_pair.second.url)) |
| 307 offline_ids.push_back(id_page_pair.first); | 305 offline_ids.push_back(id_page_pair.first); |
| 308 } | 306 } |
| 309 DoDeletePagesByOfflineId(offline_ids, callback); | 307 DoDeletePagesByOfflineId(offline_ids, callback); |
| 310 } | 308 } |
| 311 | 309 |
| 312 void OfflinePageModel::HasPages(const std::string& name_space, | 310 void OfflinePageModelImpl::HasPages(const std::string& name_space, |
| 313 const HasPagesCallback& callback) { | 311 const HasPagesCallback& callback) { |
| 314 RunWhenLoaded(base::Bind(&OfflinePageModel::HasPagesAfterLoadDone, | 312 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::HasPagesAfterLoadDone, |
| 315 weak_ptr_factory_.GetWeakPtr(), name_space, | 313 weak_ptr_factory_.GetWeakPtr(), name_space, |
| 316 callback)); | 314 callback)); |
| 317 } | 315 } |
| 318 | 316 |
| 319 void OfflinePageModel::HasPagesAfterLoadDone( | 317 void OfflinePageModelImpl::HasPagesAfterLoadDone( |
| 320 const std::string& name_space, | 318 const std::string& name_space, |
| 321 const HasPagesCallback& callback) const { | 319 const HasPagesCallback& callback) const { |
| 322 DCHECK(is_loaded_); | 320 DCHECK(is_loaded_); |
| 323 | 321 |
| 324 bool has_pages = false; | 322 bool has_pages = false; |
| 325 | 323 |
| 326 for (const auto& id_page_pair : offline_pages_) { | 324 for (const auto& id_page_pair : offline_pages_) { |
| 327 if (id_page_pair.second.client_id.name_space == name_space) { | 325 if (id_page_pair.second.client_id.name_space == name_space) { |
| 328 has_pages = true; | 326 has_pages = true; |
| 329 break; | 327 break; |
| 330 } | 328 } |
| 331 } | 329 } |
| 332 | 330 |
| 333 callback.Run(has_pages); | 331 callback.Run(has_pages); |
| 334 } | 332 } |
| 335 | 333 |
| 336 void OfflinePageModel::CheckPagesExistOffline( | 334 void OfflinePageModelImpl::CheckPagesExistOffline( |
| 337 const std::set<GURL>& urls, | 335 const std::set<GURL>& urls, |
| 338 const CheckPagesExistOfflineCallback& callback) { | 336 const CheckPagesExistOfflineCallback& callback) { |
| 339 RunWhenLoaded( | 337 RunWhenLoaded( |
| 340 base::Bind(&OfflinePageModel::CheckPagesExistOfflineAfterLoadDone, | 338 base::Bind(&OfflinePageModelImpl::CheckPagesExistOfflineAfterLoadDone, |
| 341 weak_ptr_factory_.GetWeakPtr(), urls, callback)); | 339 weak_ptr_factory_.GetWeakPtr(), urls, callback)); |
| 342 } | 340 } |
| 343 | 341 |
| 344 void OfflinePageModel::CheckPagesExistOfflineAfterLoadDone( | 342 void OfflinePageModelImpl::CheckPagesExistOfflineAfterLoadDone( |
| 345 const std::set<GURL>& urls, | 343 const std::set<GURL>& urls, |
| 346 const CheckPagesExistOfflineCallback& callback) { | 344 const CheckPagesExistOfflineCallback& callback) { |
| 347 DCHECK(is_loaded_); | 345 DCHECK(is_loaded_); |
| 348 CheckPagesExistOfflineResult result; | 346 CheckPagesExistOfflineResult result; |
| 349 for (const auto& id_page_pair : offline_pages_) { | 347 for (const auto& id_page_pair : offline_pages_) { |
| 350 auto iter = urls.find(id_page_pair.second.url); | 348 auto iter = urls.find(id_page_pair.second.url); |
| 351 if (iter != urls.end()) | 349 if (iter != urls.end()) |
| 352 result.insert(*iter); | 350 result.insert(*iter); |
| 353 } | 351 } |
| 354 callback.Run(result); | 352 callback.Run(result); |
| 355 } | 353 } |
| 356 | 354 |
| 357 void OfflinePageModel::GetAllPages( | 355 void OfflinePageModelImpl::GetAllPages( |
| 358 const MultipleOfflinePageItemCallback& callback) { | 356 const MultipleOfflinePageItemCallback& callback) { |
| 359 RunWhenLoaded(base::Bind(&OfflinePageModel::GetAllPagesAfterLoadDone, | 357 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::GetAllPagesAfterLoadDone, |
| 360 weak_ptr_factory_.GetWeakPtr(), callback)); | 358 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 361 } | 359 } |
| 362 | 360 |
| 363 void OfflinePageModel::GetAllPagesAfterLoadDone( | 361 void OfflinePageModelImpl::GetAllPagesAfterLoadDone( |
| 364 const MultipleOfflinePageItemCallback& callback) { | 362 const MultipleOfflinePageItemCallback& callback) { |
| 365 DCHECK(is_loaded_); | 363 DCHECK(is_loaded_); |
| 366 | 364 |
| 367 MultipleOfflinePageItemResult offline_pages; | 365 MultipleOfflinePageItemResult offline_pages; |
| 368 for (const auto& id_page_pair : offline_pages_) | 366 for (const auto& id_page_pair : offline_pages_) |
| 369 offline_pages.push_back(id_page_pair.second); | 367 offline_pages.push_back(id_page_pair.second); |
| 370 | 368 |
| 371 callback.Run(offline_pages); | 369 callback.Run(offline_pages); |
| 372 } | 370 } |
| 373 | 371 |
| 374 void OfflinePageModel::GetOfflineIdsForClientId( | 372 void OfflinePageModelImpl::GetOfflineIdsForClientId( |
| 375 const ClientId& client_id, | 373 const ClientId& client_id, |
| 376 const MultipleOfflineIdCallback& callback) { | 374 const MultipleOfflineIdCallback& callback) { |
| 377 RunWhenLoaded( | 375 RunWhenLoaded( |
| 378 base::Bind(&OfflinePageModel::GetOfflineIdsForClientIdWhenLoadDone, | 376 base::Bind(&OfflinePageModelImpl::GetOfflineIdsForClientIdWhenLoadDone, |
| 379 weak_ptr_factory_.GetWeakPtr(), client_id, callback)); | 377 weak_ptr_factory_.GetWeakPtr(), client_id, callback)); |
| 380 } | 378 } |
| 381 | 379 |
| 382 void OfflinePageModel::GetOfflineIdsForClientIdWhenLoadDone( | 380 void OfflinePageModelImpl::GetOfflineIdsForClientIdWhenLoadDone( |
| 383 const ClientId& client_id, | 381 const ClientId& client_id, |
| 384 const MultipleOfflineIdCallback& callback) const { | 382 const MultipleOfflineIdCallback& callback) const { |
| 385 callback.Run(MaybeGetOfflineIdsForClientId(client_id)); | 383 callback.Run(MaybeGetOfflineIdsForClientId(client_id)); |
| 386 } | 384 } |
| 387 | 385 |
| 388 const std::vector<int64_t> OfflinePageModel::MaybeGetOfflineIdsForClientId( | 386 const std::vector<int64_t> OfflinePageModelImpl::MaybeGetOfflineIdsForClientId( |
| 389 const ClientId& client_id) const { | 387 const ClientId& client_id) const { |
| 390 DCHECK(is_loaded_); | 388 DCHECK(is_loaded_); |
| 391 std::vector<int64_t> results; | 389 std::vector<int64_t> results; |
| 392 | 390 |
| 393 // We want only all pages, including those marked for deletion. | 391 // We want only all pages, including those marked for deletion. |
| 394 // TODO(bburns): actually use an index rather than linear scan. | 392 // TODO(bburns): actually use an index rather than linear scan. |
| 395 for (const auto& id_page_pair : offline_pages_) { | 393 for (const auto& id_page_pair : offline_pages_) { |
| 396 if (id_page_pair.second.client_id == client_id) | 394 if (id_page_pair.second.client_id == client_id) |
| 397 results.push_back(id_page_pair.second.offline_id); | 395 results.push_back(id_page_pair.second.offline_id); |
| 398 } | 396 } |
| 399 return results; | 397 return results; |
| 400 } | 398 } |
| 401 | 399 |
| 402 void OfflinePageModel::GetPageByOfflineId( | 400 void OfflinePageModelImpl::GetPageByOfflineId( |
| 403 int64_t offline_id, | 401 int64_t offline_id, |
| 404 const SingleOfflinePageItemCallback& callback) { | 402 const SingleOfflinePageItemCallback& callback) { |
| 405 RunWhenLoaded(base::Bind(&OfflinePageModel::GetPageByOfflineIdWhenLoadDone, | 403 RunWhenLoaded( |
| 406 weak_ptr_factory_.GetWeakPtr(), offline_id, | 404 base::Bind(&OfflinePageModelImpl::GetPageByOfflineIdWhenLoadDone, |
| 407 callback)); | 405 weak_ptr_factory_.GetWeakPtr(), offline_id, callback)); |
| 408 } | 406 } |
| 409 | 407 |
| 410 void OfflinePageModel::GetPageByOfflineIdWhenLoadDone( | 408 void OfflinePageModelImpl::GetPageByOfflineIdWhenLoadDone( |
| 411 int64_t offline_id, | 409 int64_t offline_id, |
| 412 const SingleOfflinePageItemCallback& callback) const { | 410 const SingleOfflinePageItemCallback& callback) const { |
| 413 SingleOfflinePageItemResult result; | 411 SingleOfflinePageItemResult result; |
| 414 const OfflinePageItem* match = MaybeGetPageByOfflineId(offline_id); | 412 const OfflinePageItem* match = MaybeGetPageByOfflineId(offline_id); |
| 415 if (match != nullptr) | 413 if (match != nullptr) |
| 416 result = *match; | 414 result = *match; |
| 417 callback.Run(result); | 415 callback.Run(result); |
| 418 } | 416 } |
| 419 | 417 |
| 420 const OfflinePageItem* OfflinePageModel::MaybeGetPageByOfflineId( | 418 const OfflinePageItem* OfflinePageModelImpl::MaybeGetPageByOfflineId( |
| 421 int64_t offline_id) const { | 419 int64_t offline_id) const { |
| 422 const auto iter = offline_pages_.find(offline_id); | 420 const auto iter = offline_pages_.find(offline_id); |
| 423 return iter != offline_pages_.end() ? &(iter->second) : nullptr; | 421 return iter != offline_pages_.end() ? &(iter->second) : nullptr; |
| 424 } | 422 } |
| 425 | 423 |
| 426 void OfflinePageModel::GetPageByOfflineURL( | 424 void OfflinePageModelImpl::GetPageByOfflineURL( |
| 427 const GURL& offline_url, | 425 const GURL& offline_url, |
| 428 const SingleOfflinePageItemCallback& callback) { | 426 const SingleOfflinePageItemCallback& callback) { |
| 429 RunWhenLoaded(base::Bind(&OfflinePageModel::GetPageByOfflineURLWhenLoadDone, | 427 RunWhenLoaded( |
| 430 weak_ptr_factory_.GetWeakPtr(), offline_url, | 428 base::Bind(&OfflinePageModelImpl::GetPageByOfflineURLWhenLoadDone, |
| 431 callback)); | 429 weak_ptr_factory_.GetWeakPtr(), offline_url, callback)); |
| 432 } | 430 } |
| 433 | 431 |
| 434 void OfflinePageModel::GetPageByOfflineURLWhenLoadDone( | 432 void OfflinePageModelImpl::GetPageByOfflineURLWhenLoadDone( |
| 435 const GURL& offline_url, | 433 const GURL& offline_url, |
| 436 const SingleOfflinePageItemCallback& callback) const { | 434 const SingleOfflinePageItemCallback& callback) const { |
| 437 base::Optional<OfflinePageItem> result; | 435 base::Optional<OfflinePageItem> result; |
| 438 | 436 |
| 439 for (const auto& id_page_pair : offline_pages_) { | 437 for (const auto& id_page_pair : offline_pages_) { |
| 440 if (id_page_pair.second.GetOfflineURL() == offline_url) { | 438 if (id_page_pair.second.GetOfflineURL() == offline_url) { |
| 441 callback.Run(base::make_optional(id_page_pair.second)); | 439 callback.Run(base::make_optional(id_page_pair.second)); |
| 442 return; | 440 return; |
| 443 } | 441 } |
| 444 } | 442 } |
| 445 | 443 |
| 446 callback.Run(base::nullopt); | 444 callback.Run(base::nullopt); |
| 447 } | 445 } |
| 448 | 446 |
| 449 const OfflinePageItem* OfflinePageModel::MaybeGetPageByOfflineURL( | 447 const OfflinePageItem* OfflinePageModelImpl::MaybeGetPageByOfflineURL( |
| 450 const GURL& offline_url) const { | 448 const GURL& offline_url) const { |
| 451 for (const auto& id_page_pair : offline_pages_) { | 449 for (const auto& id_page_pair : offline_pages_) { |
| 452 if (id_page_pair.second.GetOfflineURL() == offline_url) | 450 if (id_page_pair.second.GetOfflineURL() == offline_url) |
| 453 return &(id_page_pair.second); | 451 return &(id_page_pair.second); |
| 454 } | 452 } |
| 455 return nullptr; | 453 return nullptr; |
| 456 } | 454 } |
| 457 | 455 |
| 458 void OfflinePageModel::GetBestPageForOnlineURL( | 456 void OfflinePageModelImpl::GetBestPageForOnlineURL( |
| 459 const GURL& online_url, | 457 const GURL& online_url, |
| 460 const SingleOfflinePageItemCallback callback) { | 458 const SingleOfflinePageItemCallback callback) { |
| 461 RunWhenLoaded( | 459 RunWhenLoaded( |
| 462 base::Bind(&OfflinePageModel::GetBestPageForOnlineURLWhenLoadDone, | 460 base::Bind(&OfflinePageModelImpl::GetBestPageForOnlineURLWhenLoadDone, |
| 463 weak_ptr_factory_.GetWeakPtr(), online_url, callback)); | 461 weak_ptr_factory_.GetWeakPtr(), online_url, callback)); |
| 464 } | 462 } |
| 465 | 463 |
| 466 void OfflinePageModel::GetBestPageForOnlineURLWhenLoadDone( | 464 void OfflinePageModelImpl::GetBestPageForOnlineURLWhenLoadDone( |
| 467 const GURL& online_url, | 465 const GURL& online_url, |
| 468 const SingleOfflinePageItemCallback& callback) const { | 466 const SingleOfflinePageItemCallback& callback) const { |
| 469 SingleOfflinePageItemResult result; | 467 SingleOfflinePageItemResult result; |
| 470 | 468 |
| 471 const OfflinePageItem* best_page = MaybeGetBestPageForOnlineURL(online_url); | 469 const OfflinePageItem* best_page = MaybeGetBestPageForOnlineURL(online_url); |
| 472 if (best_page != nullptr) | 470 if (best_page != nullptr) |
| 473 result = *best_page; | 471 result = *best_page; |
| 474 | 472 |
| 475 callback.Run(result); | 473 callback.Run(result); |
| 476 } | 474 } |
| 477 | 475 |
| 478 void OfflinePageModel::GetPagesByOnlineURL( | 476 void OfflinePageModelImpl::GetPagesByOnlineURL( |
| 479 const GURL& online_url, | 477 const GURL& online_url, |
| 480 const MultipleOfflinePageItemCallback& callback) { | 478 const MultipleOfflinePageItemCallback& callback) { |
| 481 RunWhenLoaded(base::Bind(&OfflinePageModel::GetPagesByOnlineURLWhenLoadDone, | 479 RunWhenLoaded( |
| 482 weak_ptr_factory_.GetWeakPtr(), online_url, | 480 base::Bind(&OfflinePageModelImpl::GetPagesByOnlineURLWhenLoadDone, |
| 483 callback)); | 481 weak_ptr_factory_.GetWeakPtr(), online_url, callback)); |
| 484 } | 482 } |
| 485 | 483 |
| 486 void OfflinePageModel::GetPagesByOnlineURLWhenLoadDone( | 484 void OfflinePageModelImpl::GetPagesByOnlineURLWhenLoadDone( |
| 487 const GURL& online_url, | 485 const GURL& online_url, |
| 488 const MultipleOfflinePageItemCallback& callback) const { | 486 const MultipleOfflinePageItemCallback& callback) const { |
| 489 std::vector<OfflinePageItem> result; | 487 std::vector<OfflinePageItem> result; |
| 490 | 488 |
| 491 for (const auto& id_page_pair : offline_pages_) { | 489 for (const auto& id_page_pair : offline_pages_) { |
| 492 if (id_page_pair.second.url == online_url) | 490 if (id_page_pair.second.url == online_url) |
| 493 result.push_back(id_page_pair.second); | 491 result.push_back(id_page_pair.second); |
| 494 } | 492 } |
| 495 | 493 |
| 496 callback.Run(result); | 494 callback.Run(result); |
| 497 } | 495 } |
| 498 | 496 |
| 499 const OfflinePageItem* OfflinePageModel::MaybeGetBestPageForOnlineURL( | 497 const OfflinePageItem* OfflinePageModelImpl::MaybeGetBestPageForOnlineURL( |
| 500 const GURL& online_url) const { | 498 const GURL& online_url) const { |
| 501 const OfflinePageItem* result = nullptr; | 499 const OfflinePageItem* result = nullptr; |
| 502 for (const auto& id_page_pair : offline_pages_) { | 500 for (const auto& id_page_pair : offline_pages_) { |
| 503 if (id_page_pair.second.url == online_url) { | 501 if (id_page_pair.second.url == online_url) { |
| 504 if (!result || id_page_pair.second.creation_time > result->creation_time) | 502 if (!result || id_page_pair.second.creation_time > result->creation_time) |
| 505 result = &(id_page_pair.second); | 503 result = &(id_page_pair.second); |
| 506 } | 504 } |
| 507 } | 505 } |
| 508 return result; | 506 return result; |
| 509 } | 507 } |
| 510 | 508 |
| 511 void OfflinePageModel::CheckForExternalFileDeletion() { | 509 void OfflinePageModelImpl::CheckForExternalFileDeletion() { |
| 512 DCHECK(is_loaded_); | 510 DCHECK(is_loaded_); |
| 513 | 511 |
| 514 archive_manager_->GetAllArchives( | 512 archive_manager_->GetAllArchives( |
| 515 base::Bind(&OfflinePageModel::ScanForMissingArchiveFiles, | 513 base::Bind(&OfflinePageModelImpl::ScanForMissingArchiveFiles, |
| 516 weak_ptr_factory_.GetWeakPtr())); | 514 weak_ptr_factory_.GetWeakPtr())); |
| 517 } | 515 } |
| 518 | 516 |
| 519 void OfflinePageModel::ExpirePages(const std::vector<int64_t>& offline_ids, | 517 void OfflinePageModelImpl::ExpirePages( |
| 520 const base::Time& expiration_time, | 518 const std::vector<int64_t>& offline_ids, |
| 521 const base::Callback<void(bool)>& callback) { | 519 const base::Time& expiration_time, |
| 520 const base::Callback<void(bool)>& callback) { |
| 522 std::vector<base::FilePath> paths_to_delete; | 521 std::vector<base::FilePath> paths_to_delete; |
| 523 for (int64_t offline_id : offline_ids) { | 522 for (int64_t offline_id : offline_ids) { |
| 524 auto iter = offline_pages_.find(offline_id); | 523 auto iter = offline_pages_.find(offline_id); |
| 525 if (iter == offline_pages_.end()) | 524 if (iter == offline_pages_.end()) |
| 526 continue; | 525 continue; |
| 527 | 526 |
| 528 OfflinePageItem offline_page = iter->second; | 527 OfflinePageItem offline_page = iter->second; |
| 529 paths_to_delete.push_back(offline_page.file_path); | 528 paths_to_delete.push_back(offline_page.file_path); |
| 530 offline_page.expiration_time = expiration_time; | 529 offline_page.expiration_time = expiration_time; |
| 531 | 530 |
| 532 store_->AddOrUpdateOfflinePage( | 531 store_->AddOrUpdateOfflinePage( |
| 533 offline_page, base::Bind(&OfflinePageModel::OnExpirePageDone, | 532 offline_page, base::Bind(&OfflinePageModelImpl::OnExpirePageDone, |
| 534 weak_ptr_factory_.GetWeakPtr(), offline_id, | 533 weak_ptr_factory_.GetWeakPtr(), offline_id, |
| 535 expiration_time)); | 534 expiration_time)); |
| 536 } | 535 } |
| 537 if (paths_to_delete.empty()) { | 536 if (paths_to_delete.empty()) { |
| 538 callback.Run(true); | 537 callback.Run(true); |
| 539 return; | 538 return; |
| 540 } | 539 } |
| 541 archive_manager_->DeleteMultipleArchives(paths_to_delete, callback); | 540 archive_manager_->DeleteMultipleArchives(paths_to_delete, callback); |
| 542 } | 541 } |
| 543 | 542 |
| 544 void OfflinePageModel::OnExpirePageDone(int64_t offline_id, | 543 void OfflinePageModelImpl::OnExpirePageDone(int64_t offline_id, |
| 545 const base::Time& expiration_time, | 544 const base::Time& expiration_time, |
| 546 bool success) { | 545 bool success) { |
| 547 UMA_HISTOGRAM_BOOLEAN("OfflinePages.ExpirePage.StoreUpdateResult", success); | 546 UMA_HISTOGRAM_BOOLEAN("OfflinePages.ExpirePage.StoreUpdateResult", success); |
| 548 if (!success) | 547 if (!success) |
| 549 return; | 548 return; |
| 550 const auto& iter = offline_pages_.find(offline_id); | 549 const auto& iter = offline_pages_.find(offline_id); |
| 551 if (iter != offline_pages_.end()) { | 550 if (iter != offline_pages_.end()) { |
| 552 iter->second.expiration_time = expiration_time; | 551 iter->second.expiration_time = expiration_time; |
| 553 ClientId client_id = iter->second.client_id; | 552 ClientId client_id = iter->second.client_id; |
| 554 UMA_HISTOGRAM_CUSTOM_COUNTS( | 553 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 555 AddHistogramSuffix(client_id, "OfflinePages.ExpirePage.PageLifetime") | 554 AddHistogramSuffix(client_id, "OfflinePages.ExpirePage.PageLifetime") |
| 556 .c_str(), | 555 .c_str(), |
| 557 (expiration_time - iter->second.creation_time).InMinutes(), 1, | 556 (expiration_time - iter->second.creation_time).InMinutes(), 1, |
| 558 base::TimeDelta::FromDays(30).InMinutes(), 50); | 557 base::TimeDelta::FromDays(30).InMinutes(), 50); |
| 559 UMA_HISTOGRAM_CUSTOM_COUNTS( | 558 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 560 AddHistogramSuffix(client_id, | 559 AddHistogramSuffix(client_id, |
| 561 "OfflinePages.ExpirePage.TimeSinceLastAccess") | 560 "OfflinePages.ExpirePage.TimeSinceLastAccess") |
| 562 .c_str(), | 561 .c_str(), |
| 563 (expiration_time - iter->second.last_access_time).InMinutes(), 1, | 562 (expiration_time - iter->second.last_access_time).InMinutes(), 1, |
| 564 base::TimeDelta::FromDays(30).InMinutes(), 50); | 563 base::TimeDelta::FromDays(30).InMinutes(), 50); |
| 565 } | 564 } |
| 566 } | 565 } |
| 567 | 566 |
| 568 ClientPolicyController* OfflinePageModel::GetPolicyController() { | 567 ClientPolicyController* OfflinePageModelImpl::GetPolicyController() { |
| 569 return policy_controller_.get(); | 568 return policy_controller_.get(); |
| 570 } | 569 } |
| 571 | 570 |
| 572 OfflinePageMetadataStore* OfflinePageModel::GetStoreForTesting() { | 571 OfflinePageMetadataStore* OfflinePageModelImpl::GetStoreForTesting() { |
| 573 return store_.get(); | 572 return store_.get(); |
| 574 } | 573 } |
| 575 | 574 |
| 576 OfflinePageStorageManager* OfflinePageModel::GetStorageManager() { | 575 OfflinePageStorageManager* OfflinePageModelImpl::GetStorageManager() { |
| 577 return storage_manager_.get(); | 576 return storage_manager_.get(); |
| 578 } | 577 } |
| 579 | 578 |
| 580 bool OfflinePageModel::is_loaded() const { | 579 bool OfflinePageModelImpl::is_loaded() const { |
| 581 return is_loaded_; | 580 return is_loaded_; |
| 582 } | 581 } |
| 583 | 582 |
| 584 void OfflinePageModel::OnCreateArchiveDone(const GURL& requested_url, | 583 void OfflinePageModelImpl::OnCreateArchiveDone(const GURL& requested_url, |
| 585 int64_t offline_id, | 584 int64_t offline_id, |
| 586 const ClientId& client_id, | 585 const ClientId& client_id, |
| 587 const base::Time& start_time, | 586 const base::Time& start_time, |
| 588 const SavePageCallback& callback, | 587 const SavePageCallback& callback, |
| 589 OfflinePageArchiver* archiver, | 588 OfflinePageArchiver* archiver, |
| 590 ArchiverResult archiver_result, | 589 ArchiverResult archiver_result, |
| 591 const GURL& url, | 590 const GURL& url, |
| 592 const base::FilePath& file_path, | 591 const base::FilePath& file_path, |
| 593 int64_t file_size) { | 592 int64_t file_size) { |
| 594 if (requested_url != url) { | 593 if (requested_url != url) { |
| 595 DVLOG(1) << "Saved URL does not match requested URL."; | 594 DVLOG(1) << "Saved URL does not match requested URL."; |
| 596 // TODO(fgorski): We have created an archive for a wrong URL. It should be | 595 // TODO(fgorski): We have created an archive for a wrong URL. It should be |
| 597 // deleted from here, once archiver has the right functionality. | 596 // deleted from here, once archiver has the right functionality. |
| 598 InformSavePageDone(callback, SavePageResult::ARCHIVE_CREATION_FAILED, | 597 InformSavePageDone(callback, SavePageResult::ARCHIVE_CREATION_FAILED, |
| 599 client_id, offline_id); | 598 client_id, offline_id); |
| 600 DeletePendingArchiver(archiver); | 599 DeletePendingArchiver(archiver); |
| 601 return; | 600 return; |
| 602 } | 601 } |
| 603 | 602 |
| 604 if (archiver_result != ArchiverResult::SUCCESSFULLY_CREATED) { | 603 if (archiver_result != ArchiverResult::SUCCESSFULLY_CREATED) { |
| 605 SavePageResult result = ToSavePageResult(archiver_result); | 604 SavePageResult result = ToSavePageResult(archiver_result); |
| 606 InformSavePageDone(callback, result, client_id, offline_id); | 605 InformSavePageDone(callback, result, client_id, offline_id); |
| 607 DeletePendingArchiver(archiver); | 606 DeletePendingArchiver(archiver); |
| 608 return; | 607 return; |
| 609 } | 608 } |
| 610 OfflinePageItem offline_page_item(url, offline_id, client_id, file_path, | 609 OfflinePageItem offline_page_item(url, offline_id, client_id, file_path, |
| 611 file_size, start_time); | 610 file_size, start_time); |
| 612 store_->AddOrUpdateOfflinePage( | 611 store_->AddOrUpdateOfflinePage( |
| 613 offline_page_item, | 612 offline_page_item, base::Bind(&OfflinePageModelImpl::OnAddOfflinePageDone, |
| 614 base::Bind(&OfflinePageModel::OnAddOfflinePageDone, | 613 weak_ptr_factory_.GetWeakPtr(), archiver, |
| 615 weak_ptr_factory_.GetWeakPtr(), archiver, callback, | 614 callback, offline_page_item)); |
| 616 offline_page_item)); | |
| 617 } | 615 } |
| 618 | 616 |
| 619 void OfflinePageModel::OnAddOfflinePageDone(OfflinePageArchiver* archiver, | 617 void OfflinePageModelImpl::OnAddOfflinePageDone( |
| 620 const SavePageCallback& callback, | 618 OfflinePageArchiver* archiver, |
| 621 const OfflinePageItem& offline_page, | 619 const SavePageCallback& callback, |
| 622 bool success) { | 620 const OfflinePageItem& offline_page, |
| 621 bool success) { |
| 623 SavePageResult result; | 622 SavePageResult result; |
| 624 if (success) { | 623 if (success) { |
| 625 offline_pages_[offline_page.offline_id] = offline_page; | 624 offline_pages_[offline_page.offline_id] = offline_page; |
| 626 result = SavePageResult::SUCCESS; | 625 result = SavePageResult::SUCCESS; |
| 627 UMA_HISTOGRAM_TIMES( | 626 UMA_HISTOGRAM_TIMES( |
| 628 AddHistogramSuffix( | 627 AddHistogramSuffix(offline_page.client_id, "OfflinePages.SavePageTime") |
| 629 offline_page.client_id, "OfflinePages.SavePageTime").c_str(), | 628 .c_str(), |
| 630 base::Time::Now() - offline_page.creation_time); | 629 base::Time::Now() - offline_page.creation_time); |
| 631 // 50 buckets capped between 1Kb and 10Mb. | 630 // 50 buckets capped between 1Kb and 10Mb. |
| 632 UMA_HISTOGRAM_COUNTS_10000(AddHistogramSuffix( | 631 UMA_HISTOGRAM_COUNTS_10000( |
| 633 offline_page.client_id, "OfflinePages.PageSize").c_str(), | 632 AddHistogramSuffix(offline_page.client_id, "OfflinePages.PageSize") |
| 633 .c_str(), |
| 634 offline_page.file_size / 1024); | 634 offline_page.file_size / 1024); |
| 635 } else { | 635 } else { |
| 636 result = SavePageResult::STORE_FAILURE; | 636 result = SavePageResult::STORE_FAILURE; |
| 637 } | 637 } |
| 638 InformSavePageDone(callback, result, offline_page.client_id, | 638 InformSavePageDone(callback, result, offline_page.client_id, |
| 639 offline_page.offline_id); | 639 offline_page.offline_id); |
| 640 DeletePendingArchiver(archiver); | 640 DeletePendingArchiver(archiver); |
| 641 | 641 |
| 642 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this)); | 642 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this)); |
| 643 } | 643 } |
| 644 | 644 |
| 645 void OfflinePageModel::OnMarkPageAccesseDone( | 645 void OfflinePageModelImpl::OnMarkPageAccesseDone( |
| 646 const OfflinePageItem& offline_page_item, bool success) { | 646 const OfflinePageItem& offline_page_item, |
| 647 bool success) { |
| 647 // Update the item in the cache only upon success. | 648 // Update the item in the cache only upon success. |
| 648 if (success) | 649 if (success) |
| 649 offline_pages_[offline_page_item.offline_id] = offline_page_item; | 650 offline_pages_[offline_page_item.offline_id] = offline_page_item; |
| 650 | 651 |
| 651 // No need to fire OfflinePageModelChanged event since updating access info | 652 // No need to fire OfflinePageModelChanged event since updating access info |
| 652 // should not have any impact to the UI. | 653 // should not have any impact to the UI. |
| 653 } | 654 } |
| 654 | 655 |
| 655 void OfflinePageModel::OnEnsureArchivesDirCreatedDone( | 656 void OfflinePageModelImpl::OnEnsureArchivesDirCreatedDone( |
| 656 const base::TimeTicks& start_time) { | 657 const base::TimeTicks& start_time) { |
| 657 UMA_HISTOGRAM_TIMES("OfflinePages.Model.ArchiveDirCreationTime", | 658 UMA_HISTOGRAM_TIMES("OfflinePages.Model.ArchiveDirCreationTime", |
| 658 base::TimeTicks::Now() - start_time); | 659 base::TimeTicks::Now() - start_time); |
| 659 | 660 |
| 660 store_->Load(base::Bind(&OfflinePageModel::OnLoadDone, | 661 store_->Load(base::Bind(&OfflinePageModelImpl::OnLoadDone, |
| 661 weak_ptr_factory_.GetWeakPtr(), start_time)); | 662 weak_ptr_factory_.GetWeakPtr(), start_time)); |
| 662 } | 663 } |
| 663 | 664 |
| 664 void OfflinePageModel::OnLoadDone( | 665 void OfflinePageModelImpl::OnLoadDone( |
| 665 const base::TimeTicks& start_time, | 666 const base::TimeTicks& start_time, |
| 666 OfflinePageMetadataStore::LoadStatus load_status, | 667 OfflinePageMetadataStore::LoadStatus load_status, |
| 667 const std::vector<OfflinePageItem>& offline_pages) { | 668 const std::vector<OfflinePageItem>& offline_pages) { |
| 668 DCHECK(!is_loaded_); | 669 DCHECK(!is_loaded_); |
| 669 is_loaded_ = true; | 670 is_loaded_ = true; |
| 670 | 671 |
| 671 // TODO(jianli): rebuild the store upon failure. | 672 // TODO(jianli): rebuild the store upon failure. |
| 672 | 673 |
| 673 if (load_status == OfflinePageMetadataStore::LOAD_SUCCEEDED) | 674 if (load_status == OfflinePageMetadataStore::LOAD_SUCCEEDED) |
| 674 CacheLoadedData(offline_pages); | 675 CacheLoadedData(offline_pages); |
| 675 | 676 |
| 676 UMA_HISTOGRAM_TIMES("OfflinePages.Model.ConstructionToLoadedEventTime", | 677 UMA_HISTOGRAM_TIMES("OfflinePages.Model.ConstructionToLoadedEventTime", |
| 677 base::TimeTicks::Now() - start_time); | 678 base::TimeTicks::Now() - start_time); |
| 678 | 679 |
| 679 // Create Storage Manager. | 680 // Create Storage Manager. |
| 680 storage_manager_.reset(new OfflinePageStorageManager( | 681 storage_manager_.reset(new OfflinePageStorageManager( |
| 681 this, GetPolicyController(), archive_manager_.get())); | 682 this, GetPolicyController(), archive_manager_.get())); |
| 682 | 683 |
| 683 // Run all the delayed tasks. | 684 // Run all the delayed tasks. |
| 684 for (const auto& delayed_task : delayed_tasks_) | 685 for (const auto& delayed_task : delayed_tasks_) |
| 685 delayed_task.Run(); | 686 delayed_task.Run(); |
| 686 delayed_tasks_.clear(); | 687 delayed_tasks_.clear(); |
| 687 | 688 |
| 688 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelLoaded(this)); | 689 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelLoaded(this)); |
| 689 | 690 |
| 690 CheckForExternalFileDeletion(); | 691 CheckForExternalFileDeletion(); |
| 691 | 692 |
| 692 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 693 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 693 FROM_HERE, base::Bind(&OfflinePageModel::ClearStorageIfNeeded, | 694 FROM_HERE, base::Bind(&OfflinePageModelImpl::ClearStorageIfNeeded, |
| 694 weak_ptr_factory_.GetWeakPtr(), | 695 weak_ptr_factory_.GetWeakPtr(), |
| 695 base::Bind(&OfflinePageModel::OnStorageCleared, | 696 base::Bind(&OfflinePageModelImpl::OnStorageCleared, |
| 696 weak_ptr_factory_.GetWeakPtr())), | 697 weak_ptr_factory_.GetWeakPtr())), |
| 697 kStorageManagerStartingDelay); | 698 kStorageManagerStartingDelay); |
| 698 } | 699 } |
| 699 | 700 |
| 700 void OfflinePageModel::InformSavePageDone(const SavePageCallback& callback, | 701 void OfflinePageModelImpl::InformSavePageDone(const SavePageCallback& callback, |
| 701 SavePageResult result, | 702 SavePageResult result, |
| 702 const ClientId& client_id, | 703 const ClientId& client_id, |
| 703 int64_t offline_id) { | 704 int64_t offline_id) { |
| 704 UMA_HISTOGRAM_ENUMERATION( | 705 UMA_HISTOGRAM_ENUMERATION( |
| 705 AddHistogramSuffix(client_id, "OfflinePages.SavePageResult").c_str(), | 706 AddHistogramSuffix(client_id, "OfflinePages.SavePageResult").c_str(), |
| 706 static_cast<int>(result), | 707 static_cast<int>(result), static_cast<int>(SavePageResult::RESULT_COUNT)); |
| 707 static_cast<int>(SavePageResult::RESULT_COUNT)); | |
| 708 archive_manager_->GetStorageStats( | 708 archive_manager_->GetStorageStats( |
| 709 base::Bind(&ReportStorageHistogramsAfterSave)); | 709 base::Bind(&ReportStorageHistogramsAfterSave)); |
| 710 base::ThreadTaskRunnerHandle::Get()->PostTask( | 710 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 711 FROM_HERE, base::Bind(&OfflinePageModel::ClearStorageIfNeeded, | 711 FROM_HERE, base::Bind(&OfflinePageModelImpl::ClearStorageIfNeeded, |
| 712 weak_ptr_factory_.GetWeakPtr(), | 712 weak_ptr_factory_.GetWeakPtr(), |
| 713 base::Bind(&OfflinePageModel::OnStorageCleared, | 713 base::Bind(&OfflinePageModelImpl::OnStorageCleared, |
| 714 weak_ptr_factory_.GetWeakPtr()))); | 714 weak_ptr_factory_.GetWeakPtr()))); |
| 715 callback.Run(result, offline_id); | 715 callback.Run(result, offline_id); |
| 716 } | 716 } |
| 717 | 717 |
| 718 void OfflinePageModel::DeletePendingArchiver(OfflinePageArchiver* archiver) { | 718 void OfflinePageModelImpl::DeletePendingArchiver( |
| 719 pending_archivers_.erase(std::find( | 719 OfflinePageArchiver* archiver) { |
| 720 pending_archivers_.begin(), pending_archivers_.end(), archiver)); | 720 pending_archivers_.erase(std::find(pending_archivers_.begin(), |
| 721 pending_archivers_.end(), archiver)); |
| 721 } | 722 } |
| 722 | 723 |
| 723 void OfflinePageModel::OnDeleteArchiveFilesDone( | 724 void OfflinePageModelImpl::OnDeleteArchiveFilesDone( |
| 724 const std::vector<int64_t>& offline_ids, | 725 const std::vector<int64_t>& offline_ids, |
| 725 const DeletePageCallback& callback, | 726 const DeletePageCallback& callback, |
| 726 bool success) { | 727 bool success) { |
| 727 if (!success) { | 728 if (!success) { |
| 728 InformDeletePageDone(callback, DeletePageResult::DEVICE_FAILURE); | 729 InformDeletePageDone(callback, DeletePageResult::DEVICE_FAILURE); |
| 729 return; | 730 return; |
| 730 } | 731 } |
| 731 | 732 |
| 732 store_->RemoveOfflinePages( | 733 store_->RemoveOfflinePages( |
| 733 offline_ids, | 734 offline_ids, |
| 734 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesDone, | 735 base::Bind(&OfflinePageModelImpl::OnRemoveOfflinePagesDone, |
| 735 weak_ptr_factory_.GetWeakPtr(), offline_ids, callback)); | 736 weak_ptr_factory_.GetWeakPtr(), offline_ids, callback)); |
| 736 } | 737 } |
| 737 | 738 |
| 738 void OfflinePageModel::OnRemoveOfflinePagesDone( | 739 void OfflinePageModelImpl::OnRemoveOfflinePagesDone( |
| 739 const std::vector<int64_t>& offline_ids, | 740 const std::vector<int64_t>& offline_ids, |
| 740 const DeletePageCallback& callback, | 741 const DeletePageCallback& callback, |
| 741 bool success) { | 742 bool success) { |
| 742 // Delete the offline page from the in memory cache regardless of success in | 743 // Delete the offline page from the in memory cache regardless of success in |
| 743 // store. | 744 // store. |
| 744 base::Time now = base::Time::Now(); | 745 base::Time now = base::Time::Now(); |
| 745 int64_t total_size = 0; | 746 int64_t total_size = 0; |
| 746 for (int64_t offline_id : offline_ids) { | 747 for (int64_t offline_id : offline_ids) { |
| 747 auto iter = offline_pages_.find(offline_id); | 748 auto iter = offline_pages_.find(offline_id); |
| 748 if (iter == offline_pages_.end()) | 749 if (iter == offline_pages_.end()) |
| 749 continue; | 750 continue; |
| 750 total_size += iter->second.file_size; | 751 total_size += iter->second.file_size; |
| 751 ClientId client_id = iter->second.client_id; | 752 ClientId client_id = iter->second.client_id; |
| 752 UMA_HISTOGRAM_CUSTOM_COUNTS( | 753 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 753 AddHistogramSuffix(client_id, "OfflinePages.PageLifetime").c_str(), | 754 AddHistogramSuffix(client_id, "OfflinePages.PageLifetime").c_str(), |
| 754 (now - iter->second.creation_time).InMinutes(), | 755 (now - iter->second.creation_time).InMinutes(), 1, |
| 755 1, | 756 base::TimeDelta::FromDays(365).InMinutes(), 100); |
| 756 base::TimeDelta::FromDays(365).InMinutes(), | |
| 757 100); | |
| 758 UMA_HISTOGRAM_CUSTOM_COUNTS( | 757 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 759 AddHistogramSuffix( | 758 AddHistogramSuffix(client_id, |
| 760 client_id, "OfflinePages.DeletePage.TimeSinceLastOpen").c_str(), | 759 "OfflinePages.DeletePage.TimeSinceLastOpen") |
| 761 (now - iter->second.last_access_time).InMinutes(), | 760 .c_str(), |
| 762 1, | 761 (now - iter->second.last_access_time).InMinutes(), 1, |
| 763 base::TimeDelta::FromDays(365).InMinutes(), | 762 base::TimeDelta::FromDays(365).InMinutes(), 100); |
| 764 100); | |
| 765 UMA_HISTOGRAM_CUSTOM_COUNTS( | 763 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 766 AddHistogramSuffix( | 764 AddHistogramSuffix(client_id, |
| 767 client_id, "OfflinePages.DeletePage.LastOpenToCreated").c_str(), | 765 "OfflinePages.DeletePage.LastOpenToCreated") |
| 768 (iter->second.last_access_time - iter->second.creation_time). | 766 .c_str(), |
| 769 InMinutes(), | 767 (iter->second.last_access_time - iter->second.creation_time) |
| 770 1, | 768 .InMinutes(), |
| 771 base::TimeDelta::FromDays(365).InMinutes(), | 769 1, base::TimeDelta::FromDays(365).InMinutes(), 100); |
| 772 100); | |
| 773 UMA_HISTOGRAM_MEMORY_KB( | 770 UMA_HISTOGRAM_MEMORY_KB( |
| 774 AddHistogramSuffix( | 771 AddHistogramSuffix(client_id, "OfflinePages.DeletePage.PageSize") |
| 775 client_id, "OfflinePages.DeletePage.PageSize").c_str(), | 772 .c_str(), |
| 776 iter->second.file_size / 1024); | 773 iter->second.file_size / 1024); |
| 777 UMA_HISTOGRAM_COUNTS( | 774 UMA_HISTOGRAM_COUNTS( |
| 778 AddHistogramSuffix( | 775 AddHistogramSuffix(client_id, "OfflinePages.DeletePage.AccessCount") |
| 779 client_id, "OfflinePages.DeletePage.AccessCount").c_str(), | 776 .c_str(), |
| 780 iter->second.access_count); | 777 iter->second.access_count); |
| 781 FOR_EACH_OBSERVER( | 778 FOR_EACH_OBSERVER( |
| 782 Observer, observers_, | 779 Observer, observers_, |
| 783 OfflinePageDeleted(iter->second.offline_id, iter->second.client_id)); | 780 OfflinePageDeleted(iter->second.offline_id, iter->second.client_id)); |
| 784 offline_pages_.erase(iter); | 781 offline_pages_.erase(iter); |
| 785 } | 782 } |
| 786 if (offline_ids.size() > 1) { | 783 if (offline_ids.size() > 1) { |
| 787 UMA_HISTOGRAM_COUNTS("OfflinePages.BatchDelete.Count", | 784 UMA_HISTOGRAM_COUNTS("OfflinePages.BatchDelete.Count", |
| 788 static_cast<int32_t>(offline_ids.size())); | 785 static_cast<int32_t>(offline_ids.size())); |
| 789 UMA_HISTOGRAM_MEMORY_KB( | 786 UMA_HISTOGRAM_MEMORY_KB("OfflinePages.BatchDelete.TotalPageSize", |
| 790 "OfflinePages.BatchDelete.TotalPageSize", total_size / 1024); | 787 total_size / 1024); |
| 791 } | 788 } |
| 792 // Deleting multiple pages always succeeds when it gets to this point. | 789 // Deleting multiple pages always succeeds when it gets to this point. |
| 793 InformDeletePageDone(callback, (success || offline_ids.size() > 1) | 790 InformDeletePageDone(callback, (success || offline_ids.size() > 1) |
| 794 ? DeletePageResult::SUCCESS | 791 ? DeletePageResult::SUCCESS |
| 795 : DeletePageResult::STORE_FAILURE); | 792 : DeletePageResult::STORE_FAILURE); |
| 796 } | 793 } |
| 797 | 794 |
| 798 void OfflinePageModel::InformDeletePageDone(const DeletePageCallback& callback, | 795 void OfflinePageModelImpl::InformDeletePageDone( |
| 799 DeletePageResult result) { | 796 const DeletePageCallback& callback, |
| 800 UMA_HISTOGRAM_ENUMERATION( | 797 DeletePageResult result) { |
| 801 "OfflinePages.DeletePageResult", | 798 UMA_HISTOGRAM_ENUMERATION("OfflinePages.DeletePageResult", |
| 802 static_cast<int>(result), | 799 static_cast<int>(result), |
| 803 static_cast<int>(DeletePageResult::RESULT_COUNT)); | 800 static_cast<int>(DeletePageResult::RESULT_COUNT)); |
| 804 archive_manager_->GetStorageStats( | 801 archive_manager_->GetStorageStats( |
| 805 base::Bind(&ReportStorageHistogramsAfterDelete)); | 802 base::Bind(&ReportStorageHistogramsAfterDelete)); |
| 806 if (!callback.is_null()) | 803 if (!callback.is_null()) |
| 807 callback.Run(result); | 804 callback.Run(result); |
| 808 } | 805 } |
| 809 | 806 |
| 810 void OfflinePageModel::ScanForMissingArchiveFiles( | 807 void OfflinePageModelImpl::ScanForMissingArchiveFiles( |
| 811 const std::set<base::FilePath>& archive_paths) { | 808 const std::set<base::FilePath>& archive_paths) { |
| 812 std::vector<int64_t> ids_of_pages_missing_archive_file; | 809 std::vector<int64_t> ids_of_pages_missing_archive_file; |
| 813 std::vector<std::pair<int64_t, ClientId>> offline_client_id_pairs; | 810 std::vector<std::pair<int64_t, ClientId>> offline_client_id_pairs; |
| 814 for (const auto& id_page_pair : offline_pages_) { | 811 for (const auto& id_page_pair : offline_pages_) { |
| 815 if (archive_paths.count(id_page_pair.second.file_path) == 0UL) { | 812 if (archive_paths.count(id_page_pair.second.file_path) == 0UL) { |
| 816 ids_of_pages_missing_archive_file.push_back(id_page_pair.first); | 813 ids_of_pages_missing_archive_file.push_back(id_page_pair.first); |
| 817 offline_client_id_pairs.push_back( | 814 offline_client_id_pairs.push_back( |
| 818 std::make_pair(id_page_pair.first, id_page_pair.second.client_id)); | 815 std::make_pair(id_page_pair.first, id_page_pair.second.client_id)); |
| 819 } | 816 } |
| 820 } | 817 } |
| 821 | 818 |
| 822 // No offline pages missing archive files, we can bail out. | 819 // No offline pages missing archive files, we can bail out. |
| 823 if (ids_of_pages_missing_archive_file.empty()) | 820 if (ids_of_pages_missing_archive_file.empty()) |
| 824 return; | 821 return; |
| 825 | 822 |
| 826 DeletePageCallback remove_pages_done_callback( | 823 DeletePageCallback remove_pages_done_callback(base::Bind( |
| 827 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesMissingArchiveFileDone, | 824 &OfflinePageModelImpl::OnRemoveOfflinePagesMissingArchiveFileDone, |
| 828 weak_ptr_factory_.GetWeakPtr(), offline_client_id_pairs)); | 825 weak_ptr_factory_.GetWeakPtr(), offline_client_id_pairs)); |
| 829 | 826 |
| 830 store_->RemoveOfflinePages( | 827 store_->RemoveOfflinePages( |
| 831 ids_of_pages_missing_archive_file, | 828 ids_of_pages_missing_archive_file, |
| 832 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesDone, | 829 base::Bind(&OfflinePageModelImpl::OnRemoveOfflinePagesDone, |
| 833 weak_ptr_factory_.GetWeakPtr(), | 830 weak_ptr_factory_.GetWeakPtr(), |
| 834 ids_of_pages_missing_archive_file, | 831 ids_of_pages_missing_archive_file, |
| 835 remove_pages_done_callback)); | 832 remove_pages_done_callback)); |
| 836 } | 833 } |
| 837 | 834 |
| 838 void OfflinePageModel::OnRemoveOfflinePagesMissingArchiveFileDone( | 835 void OfflinePageModelImpl::OnRemoveOfflinePagesMissingArchiveFileDone( |
| 839 const std::vector<std::pair<int64_t, ClientId>>& offline_client_id_pairs, | 836 const std::vector<std::pair<int64_t, ClientId>>& offline_client_id_pairs, |
| 840 DeletePageResult /* result */) { | 837 DeletePageResult /* result */) { |
| 841 for (const auto& id_pair : offline_client_id_pairs) { | 838 for (const auto& id_pair : offline_client_id_pairs) { |
| 842 FOR_EACH_OBSERVER(Observer, observers_, | 839 FOR_EACH_OBSERVER(Observer, observers_, |
| 843 OfflinePageDeleted(id_pair.first, id_pair.second)); | 840 OfflinePageDeleted(id_pair.first, id_pair.second)); |
| 844 } | 841 } |
| 845 } | 842 } |
| 846 | 843 |
| 847 void OfflinePageModel::OnRemoveAllFilesDoneForClearAll( | 844 void OfflinePageModelImpl::OnRemoveAllFilesDoneForClearAll( |
| 848 const base::Closure& callback, | 845 const base::Closure& callback, |
| 849 DeletePageResult result) { | 846 DeletePageResult result) { |
| 850 store_->Reset(base::Bind(&OfflinePageModel::OnResetStoreDoneForClearAll, | 847 store_->Reset(base::Bind(&OfflinePageModelImpl::OnResetStoreDoneForClearAll, |
| 851 weak_ptr_factory_.GetWeakPtr(), | 848 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 852 callback)); | |
| 853 } | 849 } |
| 854 | 850 |
| 855 void OfflinePageModel::OnResetStoreDoneForClearAll( | 851 void OfflinePageModelImpl::OnResetStoreDoneForClearAll( |
| 856 const base::Closure& callback, bool success) { | 852 const base::Closure& callback, |
| 853 bool success) { |
| 857 DCHECK(success); | 854 DCHECK(success); |
| 858 if (!success) { | 855 if (!success) { |
| 859 UMA_HISTOGRAM_ENUMERATION("OfflinePages.ClearAllStatus2", | 856 UMA_HISTOGRAM_ENUMERATION("OfflinePages.ClearAllStatus2", |
| 860 STORE_RESET_FAILED, | 857 STORE_RESET_FAILED, CLEAR_ALL_STATUS_COUNT); |
| 861 CLEAR_ALL_STATUS_COUNT); | |
| 862 } | 858 } |
| 863 | 859 |
| 864 offline_pages_.clear(); | 860 offline_pages_.clear(); |
| 865 store_->Load(base::Bind(&OfflinePageModel::OnReloadStoreDoneForClearAll, | 861 store_->Load(base::Bind(&OfflinePageModelImpl::OnReloadStoreDoneForClearAll, |
| 866 weak_ptr_factory_.GetWeakPtr(), | 862 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 867 callback)); | |
| 868 } | 863 } |
| 869 | 864 |
| 870 void OfflinePageModel::OnReloadStoreDoneForClearAll( | 865 void OfflinePageModelImpl::OnReloadStoreDoneForClearAll( |
| 871 const base::Closure& callback, | 866 const base::Closure& callback, |
| 872 OfflinePageMetadataStore::LoadStatus load_status, | 867 OfflinePageMetadataStore::LoadStatus load_status, |
| 873 const std::vector<OfflinePageItem>& offline_pages) { | 868 const std::vector<OfflinePageItem>& offline_pages) { |
| 874 DCHECK_EQ(OfflinePageMetadataStore::LOAD_SUCCEEDED, load_status); | 869 DCHECK_EQ(OfflinePageMetadataStore::LOAD_SUCCEEDED, load_status); |
| 875 UMA_HISTOGRAM_ENUMERATION( | 870 UMA_HISTOGRAM_ENUMERATION( |
| 876 "OfflinePages.ClearAllStatus2", | 871 "OfflinePages.ClearAllStatus2", |
| 877 load_status == OfflinePageMetadataStore::LOAD_SUCCEEDED ? | 872 load_status == OfflinePageMetadataStore::LOAD_SUCCEEDED |
| 878 CLEAR_ALL_SUCCEEDED : STORE_RELOAD_FAILED, | 873 ? CLEAR_ALL_SUCCEEDED |
| 874 : STORE_RELOAD_FAILED, |
| 879 CLEAR_ALL_STATUS_COUNT); | 875 CLEAR_ALL_STATUS_COUNT); |
| 880 | 876 |
| 881 CacheLoadedData(offline_pages); | 877 CacheLoadedData(offline_pages); |
| 882 callback.Run(); | 878 callback.Run(); |
| 883 } | 879 } |
| 884 | 880 |
| 885 void OfflinePageModel::CacheLoadedData( | 881 void OfflinePageModelImpl::CacheLoadedData( |
| 886 const std::vector<OfflinePageItem>& offline_pages) { | 882 const std::vector<OfflinePageItem>& offline_pages) { |
| 887 offline_pages_.clear(); | 883 offline_pages_.clear(); |
| 888 for (const auto& offline_page : offline_pages) | 884 for (const auto& offline_page : offline_pages) |
| 889 offline_pages_[offline_page.offline_id] = offline_page; | 885 offline_pages_[offline_page.offline_id] = offline_page; |
| 890 } | 886 } |
| 891 | 887 |
| 892 void OfflinePageModel::ClearStorageIfNeeded( | 888 void OfflinePageModelImpl::ClearStorageIfNeeded( |
| 893 const ClearStorageCallback& callback) { | 889 const ClearStorageCallback& callback) { |
| 894 storage_manager_->ClearPagesIfNeeded(callback); | 890 storage_manager_->ClearPagesIfNeeded(callback); |
| 895 } | 891 } |
| 896 | 892 |
| 897 void OfflinePageModel::OnStorageCleared(size_t expired_page_count, | 893 void OfflinePageModelImpl::OnStorageCleared(size_t expired_page_count, |
| 898 ClearStorageResult result) { | 894 ClearStorageResult result) { |
| 899 UMA_HISTOGRAM_ENUMERATION("OfflinePages.ClearStorageResult", | 895 UMA_HISTOGRAM_ENUMERATION("OfflinePages.ClearStorageResult", |
| 900 static_cast<int>(result), | 896 static_cast<int>(result), |
| 901 static_cast<int>(ClearStorageResult::RESULT_COUNT)); | 897 static_cast<int>(ClearStorageResult::RESULT_COUNT)); |
| 902 if (expired_page_count > 0) { | 898 if (expired_page_count > 0) { |
| 903 UMA_HISTOGRAM_COUNTS("OfflinePages.ExpirePage.BatchSize", | 899 UMA_HISTOGRAM_COUNTS("OfflinePages.ExpirePage.BatchSize", |
| 904 static_cast<int32_t>(expired_page_count)); | 900 static_cast<int32_t>(expired_page_count)); |
| 905 } | 901 } |
| 906 } | 902 } |
| 907 | 903 |
| 908 // static | 904 void OfflinePageModelImpl::RunWhenLoaded(const base::Closure& task) { |
| 909 int64_t OfflinePageModel::GenerateOfflineId() { | |
| 910 return base::RandGenerator(std::numeric_limits<int64_t>::max()) + 1; | |
| 911 } | |
| 912 | |
| 913 void OfflinePageModel::RunWhenLoaded(const base::Closure& task) { | |
| 914 if (!is_loaded_) { | 905 if (!is_loaded_) { |
| 915 delayed_tasks_.push_back(task); | 906 delayed_tasks_.push_back(task); |
| 916 return; | 907 return; |
| 917 } | 908 } |
| 918 | 909 |
| 919 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); | 910 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); |
| 920 } | 911 } |
| 921 | 912 |
| 922 } // namespace offline_pages | 913 } // namespace offline_pages |
| OLD | NEW |