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 778 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 789 kStorageManagerStartingDelay); | 789 kStorageManagerStartingDelay); |
| 790 } | 790 } |
| 791 | 791 |
| 792 void OfflinePageModelImpl::InformSavePageDone(const SavePageCallback& callback, | 792 void OfflinePageModelImpl::InformSavePageDone(const SavePageCallback& callback, |
| 793 SavePageResult result, | 793 SavePageResult result, |
| 794 const ClientId& client_id, | 794 const ClientId& client_id, |
| 795 int64_t offline_id) { | 795 int64_t offline_id) { |
| 796 ReportSavePageResultHistogramAfterSave(client_id, result); | 796 ReportSavePageResultHistogramAfterSave(client_id, result); |
| 797 archive_manager_->GetStorageStats( | 797 archive_manager_->GetStorageStats( |
| 798 base::Bind(&ReportStorageHistogramsAfterSave)); | 798 base::Bind(&ReportStorageHistogramsAfterSave)); |
| 799 // Remove other user initiated pages with same url if the new save request was | |
|
Dmitry Titov
2016/06/23 04:23:15
this comment can be re-phrsed without "user initia
romax
2016/06/23 22:24:03
Done.
| |
| 800 // user initiated. | |
| 801 size_t pages_allowed = | |
| 802 policy_controller_->GetPolicy(client_id.name_space).pages_allowed_per_url; | |
| 803 if (result == SavePageResult::SUCCESS && pages_allowed > 0) { | |
|
Dmitry Titov
2016/06/23 04:23:15
pages_allowed > 0 looks strange here.
If it can n
romax
2016/06/23 22:24:04
Documented in the client policy class, and since t
| |
| 804 GetPagesByOnlineURL( | |
| 805 offline_pages_[offline_id].url, | |
| 806 base::Bind(&OfflinePageModelImpl::OnPagesFoundWithSameURL, | |
| 807 weak_ptr_factory_.GetWeakPtr(), client_id, offline_id, | |
| 808 pages_allowed)); | |
| 809 } else { | |
| 810 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
|
Dmitry Titov
2016/06/23 04:23:15
This PostTask call is repeated in OnDeleteOldPages
romax
2016/06/23 22:24:03
Done.
| |
| 811 FROM_HERE, | |
| 812 base::Bind(&OfflinePageModelImpl::ClearStorageIfNeeded, | |
| 813 weak_ptr_factory_.GetWeakPtr(), | |
| 814 base::Bind(&OfflinePageModelImpl::OnStorageCleared, | |
| 815 weak_ptr_factory_.GetWeakPtr()))); | |
| 816 } | |
| 817 callback.Run(result, offline_id); | |
| 818 } | |
| 819 | |
| 820 void OfflinePageModelImpl::OnPagesFoundWithSameURL( | |
| 821 const ClientId& client_id, | |
| 822 int64_t offline_id, | |
| 823 size_t pages_allowed, | |
| 824 const MultipleOfflinePageItemResult& items) { | |
| 825 std::vector<OfflinePageItem> pages_to_delete; | |
| 826 for (const auto& item : items) { | |
| 827 if (item.offline_id != offline_id && | |
| 828 item.client_id.name_space == client_id.name_space) { | |
| 829 pages_to_delete.push_back(item); | |
| 830 } | |
| 831 } | |
| 832 // Only keep |pages_allowed|-1 of most fresh pages and delete others, by | |
| 833 // sorting pages with the oldest ones first and resize the vector. | |
| 834 if (pages_to_delete.size() >= pages_allowed) { | |
| 835 sort(pages_to_delete.begin(), pages_to_delete.end(), | |
| 836 [](const OfflinePageItem& a, const OfflinePageItem& b) -> bool { | |
| 837 return a.last_access_time < b.last_access_time; | |
| 838 }); | |
| 839 pages_to_delete.resize(pages_to_delete.size() - pages_allowed + 1); | |
| 840 } | |
| 841 std::vector<int64_t> page_ids_to_delete; | |
| 842 for (const auto& item : pages_to_delete) | |
| 843 page_ids_to_delete.push_back(item.offline_id); | |
| 844 DeletePagesByOfflineId( | |
| 845 page_ids_to_delete, | |
| 846 base::Bind(&OfflinePageModelImpl::OnDeleteOldPagesWithSameURL, | |
| 847 weak_ptr_factory_.GetWeakPtr())); | |
| 848 } | |
| 849 | |
| 850 void OfflinePageModelImpl::OnDeleteOldPagesWithSameURL( | |
| 851 DeletePageResult result) { | |
| 852 // TODO(romax) Add UMAs for failure cases. | |
| 799 base::ThreadTaskRunnerHandle::Get()->PostTask( | 853 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 800 FROM_HERE, base::Bind(&OfflinePageModelImpl::ClearStorageIfNeeded, | 854 FROM_HERE, base::Bind(&OfflinePageModelImpl::ClearStorageIfNeeded, |
| 801 weak_ptr_factory_.GetWeakPtr(), | 855 weak_ptr_factory_.GetWeakPtr(), |
| 802 base::Bind(&OfflinePageModelImpl::OnStorageCleared, | 856 base::Bind(&OfflinePageModelImpl::OnStorageCleared, |
| 803 weak_ptr_factory_.GetWeakPtr()))); | 857 weak_ptr_factory_.GetWeakPtr()))); |
| 804 callback.Run(result, offline_id); | |
| 805 } | 858 } |
| 806 | 859 |
| 807 void OfflinePageModelImpl::DeletePendingArchiver( | 860 void OfflinePageModelImpl::DeletePendingArchiver( |
| 808 OfflinePageArchiver* archiver) { | 861 OfflinePageArchiver* archiver) { |
| 809 pending_archivers_.erase(std::find(pending_archivers_.begin(), | 862 pending_archivers_.erase(std::find(pending_archivers_.begin(), |
| 810 pending_archivers_.end(), archiver)); | 863 pending_archivers_.end(), archiver)); |
| 811 } | 864 } |
| 812 | 865 |
| 813 void OfflinePageModelImpl::OnDeleteArchiveFilesDone( | 866 void OfflinePageModelImpl::OnDeleteArchiveFilesDone( |
| 814 const std::vector<int64_t>& offline_ids, | 867 const std::vector<int64_t>& offline_ids, |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 983 void OfflinePageModelImpl::RunWhenLoaded(const base::Closure& task) { | 1036 void OfflinePageModelImpl::RunWhenLoaded(const base::Closure& task) { |
| 984 if (!is_loaded_) { | 1037 if (!is_loaded_) { |
| 985 delayed_tasks_.push_back(task); | 1038 delayed_tasks_.push_back(task); |
| 986 return; | 1039 return; |
| 987 } | 1040 } |
| 988 | 1041 |
| 989 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); | 1042 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); |
| 990 } | 1043 } |
| 991 | 1044 |
| 992 } // namespace offline_pages | 1045 } // namespace offline_pages |
| OLD | NEW |