| 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/core/offline_page_model_impl.h" | 5 #include "components/offline_pages/core/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" |
| 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/rand_util.h" | 15 #include "base/rand_util.h" |
| 16 #include "base/sequenced_task_runner.h" | 16 #include "base/sequenced_task_runner.h" |
| 17 #include "base/strings/string16.h" | 17 #include "base/strings/string16.h" |
| 18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
| 20 #include "base/time/clock.h" | 20 #include "base/time/clock.h" |
| 21 #include "base/time/time.h" | 21 #include "base/time/time.h" |
| 22 #include "components/offline_pages/core/archive_manager.h" | 22 #include "components/offline_pages/core/archive_manager.h" |
| 23 #include "components/offline_pages/core/client_namespace_constants.h" | 23 #include "components/offline_pages/core/client_namespace_constants.h" |
| 24 #include "components/offline_pages/core/client_policy_controller.h" | 24 #include "components/offline_pages/core/client_policy_controller.h" |
| 25 #include "components/offline_pages/core/offline_page_item.h" | 25 #include "components/offline_pages/core/offline_page_item.h" |
| 26 #include "components/offline_pages/core/offline_page_model_query.h" | 26 #include "components/offline_pages/core/offline_page_model_query.h" |
| 27 #include "components/offline_pages/core/offline_page_storage_manager.h" | 27 #include "components/offline_pages/core/offline_page_storage_manager.h" |
| 28 #include "url/gurl.h" | 28 #include "url/gurl.h" |
| 29 | 29 |
| 30 using ArchiverResult = offline_pages::OfflinePageArchiver::ArchiverResult; | 30 namespace offline_pages { |
| 31 using ClearStorageCallback = | |
| 32 offline_pages::OfflinePageStorageManager::ClearStorageCallback; | |
| 33 using ClearStorageResult = | |
| 34 offline_pages::OfflinePageStorageManager::ClearStorageResult; | |
| 35 | 31 |
| 36 namespace offline_pages { | 32 using ArchiverResult = OfflinePageArchiver::ArchiverResult; |
| 33 using ClearStorageCallback = OfflinePageStorageManager::ClearStorageCallback; |
| 34 using ClearStorageResult = OfflinePageStorageManager::ClearStorageResult; |
| 35 using LifetimeType = LifetimePolicy::LifetimeType; |
| 37 | 36 |
| 38 namespace { | 37 namespace { |
| 39 | 38 |
| 40 // 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 |
| 41 // manager after the model is loaded. | 40 // manager after the model is loaded. |
| 42 const base::TimeDelta kStorageManagerStartingDelay = | 41 const base::TimeDelta kStorageManagerStartingDelay = |
| 43 base::TimeDelta::FromSeconds(20); | 42 base::TimeDelta::FromSeconds(20); |
| 44 | 43 |
| 45 int64_t GenerateOfflineId() { | |
| 46 return base::RandGenerator(std::numeric_limits<int64_t>::max()) + 1; | |
| 47 } | |
| 48 | |
| 49 // The maximum histogram size for the metrics that measure time between views of | 44 // The maximum histogram size for the metrics that measure time between views of |
| 50 // a given page. | 45 // a given page. |
| 51 const base::TimeDelta kMaxOpenedPageHistogramBucket = | 46 const base::TimeDelta kMaxOpenedPageHistogramBucket = |
| 52 base::TimeDelta::FromDays(90); | 47 base::TimeDelta::FromDays(90); |
| 53 | 48 |
| 49 // The const set representing all LifetimeTypes. |
| 50 const std::set<LifetimeType> kAllLifetimeTypes({LifetimeType::TEMPORARY, |
| 51 LifetimeType::PERSISTENT}); |
| 52 |
| 53 int64_t GenerateOfflineId() { |
| 54 return base::RandGenerator(std::numeric_limits<int64_t>::max()) + 1; |
| 55 } |
| 56 |
| 54 SavePageResult ToSavePageResult(ArchiverResult archiver_result) { | 57 SavePageResult ToSavePageResult(ArchiverResult archiver_result) { |
| 55 SavePageResult result; | 58 SavePageResult result; |
| 56 switch (archiver_result) { | 59 switch (archiver_result) { |
| 57 case ArchiverResult::SUCCESSFULLY_CREATED: | 60 case ArchiverResult::SUCCESSFULLY_CREATED: |
| 58 result = SavePageResult::SUCCESS; | 61 result = SavePageResult::SUCCESS; |
| 59 break; | 62 break; |
| 60 case ArchiverResult::ERROR_DEVICE_FULL: | 63 case ArchiverResult::ERROR_DEVICE_FULL: |
| 61 result = SavePageResult::DEVICE_FULL; | 64 result = SavePageResult::DEVICE_FULL; |
| 62 break; | 65 break; |
| 63 case ArchiverResult::ERROR_CONTENT_UNAVAILABLE: | 66 case ArchiverResult::ERROR_CONTENT_UNAVAILABLE: |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 } | 312 } |
| 310 | 313 |
| 311 } // namespace | 314 } // namespace |
| 312 | 315 |
| 313 // protected | 316 // protected |
| 314 OfflinePageModelImpl::OfflinePageModelImpl() | 317 OfflinePageModelImpl::OfflinePageModelImpl() |
| 315 : OfflinePageModel(), is_loaded_(false), weak_ptr_factory_(this) {} | 318 : OfflinePageModel(), is_loaded_(false), weak_ptr_factory_(this) {} |
| 316 | 319 |
| 317 OfflinePageModelImpl::OfflinePageModelImpl( | 320 OfflinePageModelImpl::OfflinePageModelImpl( |
| 318 std::unique_ptr<OfflinePageMetadataStore> store, | 321 std::unique_ptr<OfflinePageMetadataStore> store, |
| 319 const base::FilePath& archives_dir, | 322 const ArchiveDirectories& archive_dirs, |
| 320 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | 323 const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
| 321 : store_(std::move(store)), | 324 : store_(std::move(store)), |
| 322 archives_dir_(archives_dir), | 325 archive_dirs_(archive_dirs), |
| 323 is_loaded_(false), | 326 is_loaded_(false), |
| 324 policy_controller_(new ClientPolicyController()), | 327 policy_controller_(new ClientPolicyController()), |
| 325 archive_manager_(new ArchiveManager(archives_dir, task_runner)), | 328 archive_manager_(new ArchiveManager(archive_dirs, task_runner)), |
| 326 testing_clock_(nullptr), | 329 testing_clock_(nullptr), |
| 327 weak_ptr_factory_(this) { | 330 weak_ptr_factory_(this) { |
| 328 archive_manager_->EnsureArchivesDirCreated( | 331 archive_manager_->EnsureArchivesDirCreated( |
| 329 base::Bind(&OfflinePageModelImpl::OnEnsureArchivesDirCreatedDone, | 332 base::Bind(&OfflinePageModelImpl::OnEnsureArchivesDirCreatedDone, |
| 330 weak_ptr_factory_.GetWeakPtr(), base::TimeTicks::Now())); | 333 weak_ptr_factory_.GetWeakPtr(), base::TimeTicks::Now())); |
| 331 } | 334 } |
| 332 | 335 |
| 333 OfflinePageModelImpl::~OfflinePageModelImpl() {} | 336 OfflinePageModelImpl::~OfflinePageModelImpl() {} |
| 334 | 337 |
| 335 void OfflinePageModelImpl::AddObserver(Observer* observer) { | 338 void OfflinePageModelImpl::AddObserver(Observer* observer) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 364 // If we already have an offline id, use it. If not, generate one. | 367 // If we already have an offline id, use it. If not, generate one. |
| 365 int64_t offline_id = save_page_params.proposed_offline_id; | 368 int64_t offline_id = save_page_params.proposed_offline_id; |
| 366 if (offline_id == kInvalidOfflineId) | 369 if (offline_id == kInvalidOfflineId) |
| 367 offline_id = GenerateOfflineId(); | 370 offline_id = GenerateOfflineId(); |
| 368 | 371 |
| 369 OfflinePageArchiver::CreateArchiveParams create_archive_params; | 372 OfflinePageArchiver::CreateArchiveParams create_archive_params; |
| 370 // If the page is being saved in the background, we should try to remove the | 373 // If the page is being saved in the background, we should try to remove the |
| 371 // popup overlay that obstructs viewing the normal content. | 374 // popup overlay that obstructs viewing the normal content. |
| 372 create_archive_params.remove_popup_overlay = save_page_params.is_background; | 375 create_archive_params.remove_popup_overlay = save_page_params.is_background; |
| 373 archiver->CreateArchive( | 376 archiver->CreateArchive( |
| 374 archives_dir_, create_archive_params, | 377 GetArchiveDirectory(save_page_params.client_id.name_space), |
| 378 create_archive_params, |
| 375 base::Bind(&OfflinePageModelImpl::OnCreateArchiveDone, | 379 base::Bind(&OfflinePageModelImpl::OnCreateArchiveDone, |
| 376 weak_ptr_factory_.GetWeakPtr(), save_page_params, offline_id, | 380 weak_ptr_factory_.GetWeakPtr(), save_page_params, offline_id, |
| 377 GetCurrentTime(), callback)); | 381 GetCurrentTime(), callback)); |
| 378 pending_archivers_.push_back(std::move(archiver)); | 382 pending_archivers_.push_back(std::move(archiver)); |
| 379 } | 383 } |
| 380 | 384 |
| 381 void OfflinePageModelImpl::MarkPageAccessed(int64_t offline_id) { | 385 void OfflinePageModelImpl::MarkPageAccessed(int64_t offline_id) { |
| 382 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::MarkPageAccessedWhenLoadDone, | 386 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::MarkPageAccessedWhenLoadDone, |
| 383 weak_ptr_factory_.GetWeakPtr(), offline_id)); | 387 weak_ptr_factory_.GetWeakPtr(), offline_id)); |
| 384 } | 388 } |
| (...skipping 469 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 854 // Clear storage. | 858 // Clear storage. |
| 855 PostClearStorageIfNeededTask(true /* delayed */); | 859 PostClearStorageIfNeededTask(true /* delayed */); |
| 856 } | 860 } |
| 857 | 861 |
| 858 void OfflinePageModelImpl::InformSavePageDone(const SavePageCallback& callback, | 862 void OfflinePageModelImpl::InformSavePageDone(const SavePageCallback& callback, |
| 859 SavePageResult result, | 863 SavePageResult result, |
| 860 const ClientId& client_id, | 864 const ClientId& client_id, |
| 861 int64_t offline_id) { | 865 int64_t offline_id) { |
| 862 ReportSavePageResultHistogramAfterSave(client_id, result); | 866 ReportSavePageResultHistogramAfterSave(client_id, result); |
| 863 archive_manager_->GetStorageStats( | 867 archive_manager_->GetStorageStats( |
| 864 base::Bind(&ReportStorageHistogramsAfterSave)); | 868 kAllLifetimeTypes, base::Bind(&ReportStorageHistogramsAfterSave)); |
| 865 callback.Run(result, offline_id); | 869 callback.Run(result, offline_id); |
| 866 } | 870 } |
| 867 | 871 |
| 868 void OfflinePageModelImpl::DeleteExistingPagesWithSameURL( | 872 void OfflinePageModelImpl::DeleteExistingPagesWithSameURL( |
| 869 const OfflinePageItem& offline_page) { | 873 const OfflinePageItem& offline_page) { |
| 870 // Remove existing pages generated by the same policy and with same url. | 874 // Remove existing pages generated by the same policy and with same url. |
| 871 size_t pages_allowed = | 875 size_t pages_allowed = |
| 872 policy_controller_->GetPolicy(offline_page.client_id.name_space) | 876 policy_controller_->GetPolicy(offline_page.client_id.name_space) |
| 873 .pages_allowed_per_url; | 877 .pages_allowed_per_url; |
| 874 if (pages_allowed == kUnlimitedPages) | 878 if (pages_allowed == kUnlimitedPages) |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 968 InformDeletePageDone(callback, delete_result); | 972 InformDeletePageDone(callback, delete_result); |
| 969 } | 973 } |
| 970 | 974 |
| 971 void OfflinePageModelImpl::InformDeletePageDone( | 975 void OfflinePageModelImpl::InformDeletePageDone( |
| 972 const DeletePageCallback& callback, | 976 const DeletePageCallback& callback, |
| 973 DeletePageResult result) { | 977 DeletePageResult result) { |
| 974 UMA_HISTOGRAM_ENUMERATION("OfflinePages.DeletePageResult", | 978 UMA_HISTOGRAM_ENUMERATION("OfflinePages.DeletePageResult", |
| 975 static_cast<int>(result), | 979 static_cast<int>(result), |
| 976 static_cast<int>(DeletePageResult::RESULT_COUNT)); | 980 static_cast<int>(DeletePageResult::RESULT_COUNT)); |
| 977 archive_manager_->GetStorageStats( | 981 archive_manager_->GetStorageStats( |
| 978 base::Bind(&ReportStorageHistogramsAfterDelete)); | 982 kAllLifetimeTypes, base::Bind(&ReportStorageHistogramsAfterDelete)); |
| 979 if (!callback.is_null()) | 983 if (!callback.is_null()) |
| 980 callback.Run(result); | 984 callback.Run(result); |
| 981 } | 985 } |
| 982 | 986 |
| 983 void OfflinePageModelImpl::CheckMetadataConsistencyForArchivePaths( | 987 void OfflinePageModelImpl::CheckMetadataConsistencyForArchivePaths( |
| 984 const std::set<base::FilePath>& archive_paths) { | 988 const std::set<base::FilePath>& archive_paths) { |
| 985 DeletePagesMissingArchiveFile(archive_paths); | 989 DeletePagesMissingArchiveFile(archive_paths); |
| 986 DeleteOrphanedArchives(archive_paths); | 990 DeleteOrphanedArchives(archive_paths); |
| 987 } | 991 } |
| 988 | 992 |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1080 weak_ptr_factory_.GetWeakPtr())), | 1084 weak_ptr_factory_.GetWeakPtr())), |
| 1081 delay); | 1085 delay); |
| 1082 } | 1086 } |
| 1083 | 1087 |
| 1084 bool OfflinePageModelImpl::IsRemovedOnCacheReset( | 1088 bool OfflinePageModelImpl::IsRemovedOnCacheReset( |
| 1085 const OfflinePageItem& offline_page) const { | 1089 const OfflinePageItem& offline_page) const { |
| 1086 return policy_controller_->IsRemovedOnCacheReset( | 1090 return policy_controller_->IsRemovedOnCacheReset( |
| 1087 offline_page.client_id.name_space); | 1091 offline_page.client_id.name_space); |
| 1088 } | 1092 } |
| 1089 | 1093 |
| 1094 const base::FilePath& OfflinePageModelImpl::GetArchiveDirectory( |
| 1095 const std::string& name_space) const { |
| 1096 return archive_dirs_.at( |
| 1097 policy_controller_->GetPolicy(name_space).lifetime_policy.lifetime_type); |
| 1098 } |
| 1099 |
| 1090 void OfflinePageModelImpl::RunWhenLoaded(const base::Closure& task) { | 1100 void OfflinePageModelImpl::RunWhenLoaded(const base::Closure& task) { |
| 1091 if (!is_loaded_) { | 1101 if (!is_loaded_) { |
| 1092 delayed_tasks_.push_back(task); | 1102 delayed_tasks_.push_back(task); |
| 1093 return; | 1103 return; |
| 1094 } | 1104 } |
| 1095 | 1105 |
| 1096 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); | 1106 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); |
| 1097 } | 1107 } |
| 1098 | 1108 |
| 1099 base::Time OfflinePageModelImpl::GetCurrentTime() const { | 1109 base::Time OfflinePageModelImpl::GetCurrentTime() const { |
| 1100 return testing_clock_ ? testing_clock_->Now() : base::Time::Now(); | 1110 return testing_clock_ ? testing_clock_->Now() : base::Time::Now(); |
| 1101 } | 1111 } |
| 1102 | 1112 |
| 1103 } // namespace offline_pages | 1113 } // namespace offline_pages |
| OLD | NEW |