| 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_model_impl.h" | 5 #include "components/offline_pages/offline_page_model_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 | 34 |
| 35 namespace offline_pages { | 35 namespace offline_pages { |
| 36 | 36 |
| 37 namespace { | 37 namespace { |
| 38 | 38 |
| 39 // The delay used to schedule the first clear storage request for storage | 39 // The delay used to schedule the first clear storage request for storage |
| 40 // manager after the model is loaded. | 40 // manager after the model is loaded. |
| 41 const base::TimeDelta kStorageManagerStartingDelay = | 41 const base::TimeDelta kStorageManagerStartingDelay = |
| 42 base::TimeDelta::FromSeconds(20); | 42 base::TimeDelta::FromSeconds(20); |
| 43 | 43 |
| 44 // This enum is used in an UMA histogram. Hence the entries here shouldn't | |
| 45 // be deleted or re-ordered and new ones should be added to the end. | |
| 46 enum ClearAllStatus { | |
| 47 CLEAR_ALL_SUCCEEDED, | |
| 48 STORE_RESET_FAILED, | |
| 49 STORE_RELOAD_FAILED, | |
| 50 | |
| 51 // NOTE: always keep this entry at the end. | |
| 52 CLEAR_ALL_STATUS_COUNT | |
| 53 }; | |
| 54 | |
| 55 int64_t GenerateOfflineId() { | 44 int64_t GenerateOfflineId() { |
| 56 return base::RandGenerator(std::numeric_limits<int64_t>::max()) + 1; | 45 return base::RandGenerator(std::numeric_limits<int64_t>::max()) + 1; |
| 57 } | 46 } |
| 58 | 47 |
| 59 // The maximum histogram size for the metrics that measure time between views of | 48 // The maximum histogram size for the metrics that measure time between views of |
| 60 // a given page. | 49 // a given page. |
| 61 const base::TimeDelta kMaxOpenedPageHistogramBucket = | 50 const base::TimeDelta kMaxOpenedPageHistogramBucket = |
| 62 base::TimeDelta::FromDays(90); | 51 base::TimeDelta::FromDays(90); |
| 63 | 52 |
| 64 SavePageResult ToSavePageResult(ArchiverResult archiver_result) { | 53 SavePageResult ToSavePageResult(ArchiverResult archiver_result) { |
| (...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 InformDeletePageDone(callback, DeletePageResult::SUCCESS); | 421 InformDeletePageDone(callback, DeletePageResult::SUCCESS); |
| 433 return; | 422 return; |
| 434 } | 423 } |
| 435 | 424 |
| 436 archive_manager_->DeleteMultipleArchives( | 425 archive_manager_->DeleteMultipleArchives( |
| 437 paths_to_delete, | 426 paths_to_delete, |
| 438 base::Bind(&OfflinePageModelImpl::OnDeleteArchiveFilesDone, | 427 base::Bind(&OfflinePageModelImpl::OnDeleteArchiveFilesDone, |
| 439 weak_ptr_factory_.GetWeakPtr(), offline_ids, callback)); | 428 weak_ptr_factory_.GetWeakPtr(), offline_ids, callback)); |
| 440 } | 429 } |
| 441 | 430 |
| 442 void OfflinePageModelImpl::ClearAll(const base::Closure& callback) { | |
| 443 DCHECK(is_loaded_); | |
| 444 | |
| 445 std::vector<int64_t> offline_ids; | |
| 446 for (const auto& id_page_pair : offline_pages_) | |
| 447 offline_ids.push_back(id_page_pair.first); | |
| 448 DeletePagesByOfflineId( | |
| 449 offline_ids, | |
| 450 base::Bind(&OfflinePageModelImpl::OnRemoveAllFilesDoneForClearAll, | |
| 451 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 452 } | |
| 453 | |
| 454 void OfflinePageModelImpl::DeleteCachedPagesByURLPredicate( | 431 void OfflinePageModelImpl::DeleteCachedPagesByURLPredicate( |
| 455 const UrlPredicate& predicate, | 432 const UrlPredicate& predicate, |
| 456 const DeletePageCallback& callback) { | 433 const DeletePageCallback& callback) { |
| 457 RunWhenLoaded( | 434 RunWhenLoaded( |
| 458 base::Bind(&OfflinePageModelImpl::DoDeleteCachedPagesByURLPredicate, | 435 base::Bind(&OfflinePageModelImpl::DoDeleteCachedPagesByURLPredicate, |
| 459 weak_ptr_factory_.GetWeakPtr(), predicate, callback)); | 436 weak_ptr_factory_.GetWeakPtr(), predicate, callback)); |
| 460 } | 437 } |
| 461 | 438 |
| 462 void OfflinePageModelImpl::DoDeleteCachedPagesByURLPredicate( | 439 void OfflinePageModelImpl::DoDeleteCachedPagesByURLPredicate( |
| 463 const UrlPredicate& predicate, | 440 const UrlPredicate& predicate, |
| (...skipping 584 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1048 | 1025 |
| 1049 void OfflinePageModelImpl::OnDeleteOrphanedArchivesDone( | 1026 void OfflinePageModelImpl::OnDeleteOrphanedArchivesDone( |
| 1050 const std::vector<base::FilePath>& archives, | 1027 const std::vector<base::FilePath>& archives, |
| 1051 bool success) { | 1028 bool success) { |
| 1052 UMA_HISTOGRAM_COUNTS("OfflinePages.Consistency.OrphanedArchivesCount", | 1029 UMA_HISTOGRAM_COUNTS("OfflinePages.Consistency.OrphanedArchivesCount", |
| 1053 static_cast<int32_t>(archives.size())); | 1030 static_cast<int32_t>(archives.size())); |
| 1054 UMA_HISTOGRAM_BOOLEAN("OfflinePages.Consistency.DeleteOrphanedArchivesResult", | 1031 UMA_HISTOGRAM_BOOLEAN("OfflinePages.Consistency.DeleteOrphanedArchivesResult", |
| 1055 success); | 1032 success); |
| 1056 } | 1033 } |
| 1057 | 1034 |
| 1058 void OfflinePageModelImpl::OnRemoveAllFilesDoneForClearAll( | |
| 1059 const base::Closure& callback, | |
| 1060 DeletePageResult result) { | |
| 1061 store_->Reset(base::Bind(&OfflinePageModelImpl::OnResetStoreDoneForClearAll, | |
| 1062 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 1063 } | |
| 1064 | |
| 1065 void OfflinePageModelImpl::OnResetStoreDoneForClearAll( | |
| 1066 const base::Closure& callback, | |
| 1067 bool success) { | |
| 1068 DCHECK(success); | |
| 1069 if (!success) { | |
| 1070 offline_event_logger_.RecordStoreClearError(); | |
| 1071 UMA_HISTOGRAM_ENUMERATION("OfflinePages.ClearAllStatus2", | |
| 1072 STORE_RESET_FAILED, CLEAR_ALL_STATUS_COUNT); | |
| 1073 } | |
| 1074 | |
| 1075 offline_pages_.clear(); | |
| 1076 store_->GetOfflinePages( | |
| 1077 base::Bind(&OfflinePageModelImpl::OnReloadStoreDoneForClearAll, | |
| 1078 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 1079 } | |
| 1080 | |
| 1081 void OfflinePageModelImpl::OnReloadStoreDoneForClearAll( | |
| 1082 const base::Closure& callback, | |
| 1083 OfflinePageMetadataStore::LoadStatus load_status, | |
| 1084 const std::vector<OfflinePageItem>& offline_pages) { | |
| 1085 DCHECK_EQ(OfflinePageMetadataStore::LOAD_SUCCEEDED, load_status); | |
| 1086 UMA_HISTOGRAM_ENUMERATION( | |
| 1087 "OfflinePages.ClearAllStatus2", | |
| 1088 load_status == OfflinePageMetadataStore::LOAD_SUCCEEDED | |
| 1089 ? CLEAR_ALL_SUCCEEDED | |
| 1090 : STORE_RELOAD_FAILED, | |
| 1091 CLEAR_ALL_STATUS_COUNT); | |
| 1092 | |
| 1093 if (load_status == OfflinePageMetadataStore::LOAD_SUCCEEDED) { | |
| 1094 offline_event_logger_.RecordStoreCleared(); | |
| 1095 } else { | |
| 1096 offline_event_logger_.RecordStoreReloadError(); | |
| 1097 } | |
| 1098 | |
| 1099 CacheLoadedData(offline_pages); | |
| 1100 callback.Run(); | |
| 1101 } | |
| 1102 | |
| 1103 void OfflinePageModelImpl::CacheLoadedData( | 1035 void OfflinePageModelImpl::CacheLoadedData( |
| 1104 const std::vector<OfflinePageItem>& offline_pages) { | 1036 const std::vector<OfflinePageItem>& offline_pages) { |
| 1105 offline_pages_.clear(); | 1037 offline_pages_.clear(); |
| 1106 for (const auto& offline_page : offline_pages) | 1038 for (const auto& offline_page : offline_pages) |
| 1107 offline_pages_[offline_page.offline_id] = offline_page; | 1039 offline_pages_[offline_page.offline_id] = offline_page; |
| 1108 } | 1040 } |
| 1109 | 1041 |
| 1110 void OfflinePageModelImpl::ClearStorageIfNeeded( | 1042 void OfflinePageModelImpl::ClearStorageIfNeeded( |
| 1111 const ClearStorageCallback& callback) { | 1043 const ClearStorageCallback& callback) { |
| 1112 storage_manager_->ClearPagesIfNeeded(callback); | 1044 storage_manager_->ClearPagesIfNeeded(callback); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1144 } | 1076 } |
| 1145 | 1077 |
| 1146 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); | 1078 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); |
| 1147 } | 1079 } |
| 1148 | 1080 |
| 1149 base::Time OfflinePageModelImpl::GetCurrentTime() const { | 1081 base::Time OfflinePageModelImpl::GetCurrentTime() const { |
| 1150 return testing_clock_ ? testing_clock_->Now() : base::Time::Now(); | 1082 return testing_clock_ ? testing_clock_->Now() : base::Time::Now(); |
| 1151 } | 1083 } |
| 1152 | 1084 |
| 1153 } // namespace offline_pages | 1085 } // namespace offline_pages |
| OLD | NEW |