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_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 629 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 640 if (online_url_without_fragment == | 640 if (online_url_without_fragment == |
| 641 id_page_pair.second.url.ReplaceComponents(remove_params)) { | 641 id_page_pair.second.url.ReplaceComponents(remove_params)) { |
| 642 result.push_back(id_page_pair.second); | 642 result.push_back(id_page_pair.second); |
| 643 } | 643 } |
| 644 } | 644 } |
| 645 | 645 |
| 646 callback.Run(result); | 646 callback.Run(result); |
| 647 } | 647 } |
| 648 | 648 |
| 649 void OfflinePageModelImpl::CheckMetadataConsistency() { | 649 void OfflinePageModelImpl::CheckMetadataConsistency() { |
| 650 DCHECK(is_loaded_); | |
| 651 archive_manager_->GetAllArchives( | 650 archive_manager_->GetAllArchives( |
| 652 base::Bind(&OfflinePageModelImpl::CheckMetadataConsistencyForArchivePaths, | 651 base::Bind(&OfflinePageModelImpl::CheckMetadataConsistencyForArchivePaths, |
| 653 weak_ptr_factory_.GetWeakPtr())); | 652 weak_ptr_factory_.GetWeakPtr())); |
| 654 } | 653 } |
| 655 | 654 |
| 656 void OfflinePageModelImpl::ExpirePages( | 655 void OfflinePageModelImpl::ExpirePages( |
| 657 const std::vector<int64_t>& offline_ids, | 656 const std::vector<int64_t>& offline_ids, |
| 658 const base::Time& expiration_time, | 657 const base::Time& expiration_time, |
| 659 const base::Callback<void(bool)>& callback) { | 658 const base::Callback<void(bool)>& callback) { |
| 660 std::vector<base::FilePath> paths_to_delete; | 659 std::vector<base::FilePath> paths_to_delete; |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 809 | 808 |
| 810 // No need to fire OfflinePageModelChanged event since updating access info | 809 // No need to fire OfflinePageModelChanged event since updating access info |
| 811 // should not have any impact to the UI. | 810 // should not have any impact to the UI. |
| 812 } | 811 } |
| 813 | 812 |
| 814 void OfflinePageModelImpl::OnEnsureArchivesDirCreatedDone( | 813 void OfflinePageModelImpl::OnEnsureArchivesDirCreatedDone( |
| 815 const base::TimeTicks& start_time) { | 814 const base::TimeTicks& start_time) { |
| 816 UMA_HISTOGRAM_TIMES("OfflinePages.Model.ArchiveDirCreationTime", | 815 UMA_HISTOGRAM_TIMES("OfflinePages.Model.ArchiveDirCreationTime", |
| 817 base::TimeTicks::Now() - start_time); | 816 base::TimeTicks::Now() - start_time); |
| 818 | 817 |
| 818 store_->Initialize(base::Bind(&OfflinePageModelImpl::OnStoreInitialized, | |
| 819 weak_ptr_factory_.GetWeakPtr(), start_time)); | |
| 820 } | |
| 821 | |
| 822 void OfflinePageModelImpl::OnStoreInitialized(const base::TimeTicks& start_time, | |
| 823 StoreState state) { | |
| 824 if (state == StoreState::FAILED_RESET) { | |
| 825 CompleteLoad(); | |
| 826 return; | |
| 827 } | |
| 828 | |
| 829 if (state == StoreState::FAILED_LOADING) { | |
| 830 store_->Reset(base::Bind(&OfflinePageModelImpl::OnStoreInitialized, | |
| 831 weak_ptr_factory_.GetWeakPtr(), start_time)); | |
| 832 return; | |
| 833 } | |
| 834 | |
| 835 DCHECK_EQ(state, StoreState::LOADED); | |
| 819 store_->GetOfflinePages(base::Bind(&OfflinePageModelImpl::OnLoadDone, | 836 store_->GetOfflinePages(base::Bind(&OfflinePageModelImpl::OnLoadDone, |
| 820 weak_ptr_factory_.GetWeakPtr(), | 837 weak_ptr_factory_.GetWeakPtr(), |
| 821 start_time)); | 838 start_time)); |
| 822 } | 839 } |
| 823 | 840 |
| 824 void OfflinePageModelImpl::OnLoadDone( | 841 void OfflinePageModelImpl::OnLoadDone( |
| 825 const base::TimeTicks& start_time, | 842 const base::TimeTicks& start_time, |
| 826 OfflinePageMetadataStore::LoadStatus load_status, | |
| 827 const std::vector<OfflinePageItem>& offline_pages) { | 843 const std::vector<OfflinePageItem>& offline_pages) { |
| 828 DCHECK(!is_loaded_); | 844 DCHECK(!is_loaded_); |
| 829 is_loaded_ = true; | |
| 830 | |
| 831 // TODO(jianli): rebuild the store upon failure. | |
| 832 | |
| 833 if (load_status == OfflinePageMetadataStore::LOAD_SUCCEEDED) | |
| 834 CacheLoadedData(offline_pages); | |
| 835 | 845 |
| 836 UMA_HISTOGRAM_TIMES("OfflinePages.Model.ConstructionToLoadedEventTime", | 846 UMA_HISTOGRAM_TIMES("OfflinePages.Model.ConstructionToLoadedEventTime", |
| 837 base::TimeTicks::Now() - start_time); | 847 base::TimeTicks::Now() - start_time); |
| 838 | 848 |
| 839 // Create Storage Manager. | 849 CacheLoadedData(offline_pages); |
| 840 storage_manager_.reset(new OfflinePageStorageManager( | 850 CompleteLoad(); |
| 841 this, GetPolicyController(), archive_manager_.get())); | |
| 842 | 851 |
| 843 // Run all the delayed tasks. | 852 // Ensure necessary cleanup operations are started. |
| 844 for (const auto& delayed_task : delayed_tasks_) | |
| 845 delayed_task.Run(); | |
| 846 delayed_tasks_.clear(); | |
| 847 | |
| 848 for (Observer& observer : observers_) | |
| 849 observer.OfflinePageModelLoaded(this); | |
| 850 | |
| 851 CheckMetadataConsistency(); | 853 CheckMetadataConsistency(); |
| 852 | 854 |
| 853 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 855 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
|
fgorski
2016/11/14 21:59:47
Move to CompleteLoad, as we want to clear storage
fgorski
2016/11/14 22:24:09
Done.
| |
| 854 FROM_HERE, base::Bind(&OfflinePageModelImpl::ClearStorageIfNeeded, | 856 FROM_HERE, base::Bind(&OfflinePageModelImpl::ClearStorageIfNeeded, |
| 855 weak_ptr_factory_.GetWeakPtr(), | 857 weak_ptr_factory_.GetWeakPtr(), |
| 856 base::Bind(&OfflinePageModelImpl::OnStorageCleared, | 858 base::Bind(&OfflinePageModelImpl::OnStorageCleared, |
| 857 weak_ptr_factory_.GetWeakPtr())), | 859 weak_ptr_factory_.GetWeakPtr())), |
| 858 kStorageManagerStartingDelay); | 860 kStorageManagerStartingDelay); |
| 859 } | 861 } |
| 860 | 862 |
| 863 void OfflinePageModelImpl::CompleteLoad() { | |
| 864 is_loaded_ = true; | |
| 865 | |
| 866 // Inform observers the load is done. | |
| 867 for (Observer& observer : observers_) | |
| 868 observer.OfflinePageModelLoaded(this); | |
| 869 | |
| 870 // Run all the delayed tasks. | |
| 871 for (const auto& delayed_task : delayed_tasks_) | |
| 872 delayed_task.Run(); | |
| 873 delayed_tasks_.clear(); | |
| 874 } | |
| 875 | |
| 861 void OfflinePageModelImpl::InformSavePageDone(const SavePageCallback& callback, | 876 void OfflinePageModelImpl::InformSavePageDone(const SavePageCallback& callback, |
| 862 SavePageResult result, | 877 SavePageResult result, |
| 863 const ClientId& client_id, | 878 const ClientId& client_id, |
| 864 int64_t offline_id) { | 879 int64_t offline_id) { |
| 865 ReportSavePageResultHistogramAfterSave(client_id, result); | 880 ReportSavePageResultHistogramAfterSave(client_id, result); |
| 866 archive_manager_->GetStorageStats( | 881 archive_manager_->GetStorageStats( |
| 867 base::Bind(&ReportStorageHistogramsAfterSave)); | 882 base::Bind(&ReportStorageHistogramsAfterSave)); |
| 868 callback.Run(result, offline_id); | 883 callback.Run(result, offline_id); |
| 869 } | 884 } |
| 870 | 885 |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1044 success); | 1059 success); |
| 1045 } | 1060 } |
| 1046 | 1061 |
| 1047 void OfflinePageModelImpl::CacheLoadedData( | 1062 void OfflinePageModelImpl::CacheLoadedData( |
| 1048 const std::vector<OfflinePageItem>& offline_pages) { | 1063 const std::vector<OfflinePageItem>& offline_pages) { |
| 1049 offline_pages_.clear(); | 1064 offline_pages_.clear(); |
| 1050 for (const auto& offline_page : offline_pages) | 1065 for (const auto& offline_page : offline_pages) |
| 1051 offline_pages_[offline_page.offline_id] = offline_page; | 1066 offline_pages_[offline_page.offline_id] = offline_page; |
| 1052 } | 1067 } |
| 1053 | 1068 |
| 1054 void OfflinePageModelImpl::ClearStorageIfNeeded( | 1069 void OfflinePageModelImpl::ClearStorageIfNeeded( |
|
fgorski
2016/11/14 21:59:47
make this method call itself.
fgorski
2016/11/14 22:24:10
Done through Post...Task method instead.
Intention
| |
| 1055 const ClearStorageCallback& callback) { | 1070 const ClearStorageCallback& callback) { |
| 1071 // Create Storage Manager if necessary. | |
| 1072 if (!storage_manager_) { | |
| 1073 storage_manager_.reset(new OfflinePageStorageManager( | |
| 1074 this, GetPolicyController(), archive_manager_.get())); | |
| 1075 } | |
| 1056 storage_manager_->ClearPagesIfNeeded(callback); | 1076 storage_manager_->ClearPagesIfNeeded(callback); |
| 1057 } | 1077 } |
| 1058 | 1078 |
| 1059 void OfflinePageModelImpl::OnStorageCleared(size_t expired_page_count, | 1079 void OfflinePageModelImpl::OnStorageCleared(size_t expired_page_count, |
| 1060 ClearStorageResult result) { | 1080 ClearStorageResult result) { |
| 1061 UMA_HISTOGRAM_ENUMERATION("OfflinePages.ClearStorageResult", | 1081 UMA_HISTOGRAM_ENUMERATION("OfflinePages.ClearStorageResult", |
| 1062 static_cast<int>(result), | 1082 static_cast<int>(result), |
| 1063 static_cast<int>(ClearStorageResult::RESULT_COUNT)); | 1083 static_cast<int>(ClearStorageResult::RESULT_COUNT)); |
| 1064 if (expired_page_count > 0) { | 1084 if (expired_page_count > 0) { |
| 1065 UMA_HISTOGRAM_COUNTS("OfflinePages.ExpirePage.BatchSize", | 1085 UMA_HISTOGRAM_COUNTS("OfflinePages.ExpirePage.BatchSize", |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 1088 } | 1108 } |
| 1089 | 1109 |
| 1090 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); | 1110 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); |
| 1091 } | 1111 } |
| 1092 | 1112 |
| 1093 base::Time OfflinePageModelImpl::GetCurrentTime() const { | 1113 base::Time OfflinePageModelImpl::GetCurrentTime() const { |
| 1094 return testing_clock_ ? testing_clock_->Now() : base::Time::Now(); | 1114 return testing_clock_ ? testing_clock_->Now() : base::Time::Now(); |
| 1095 } | 1115 } |
| 1096 | 1116 |
| 1097 } // namespace offline_pages | 1117 } // namespace offline_pages |
| OLD | NEW |