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 | |
| 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) { | |
| 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 } | |
| 799 base::ThreadTaskRunnerHandle::Get()->PostTask( | 810 base::ThreadTaskRunnerHandle::Get()->PostTask( |
|
fgorski
2016/06/17 22:51:06
It would probably make sense to run this code afte
romax
2016/06/20 20:04:41
Done.
| |
| 800 FROM_HERE, base::Bind(&OfflinePageModelImpl::ClearStorageIfNeeded, | 811 FROM_HERE, base::Bind(&OfflinePageModelImpl::ClearStorageIfNeeded, |
| 801 weak_ptr_factory_.GetWeakPtr(), | 812 weak_ptr_factory_.GetWeakPtr(), |
| 802 base::Bind(&OfflinePageModelImpl::OnStorageCleared, | 813 base::Bind(&OfflinePageModelImpl::OnStorageCleared, |
| 803 weak_ptr_factory_.GetWeakPtr()))); | 814 weak_ptr_factory_.GetWeakPtr()))); |
| 804 callback.Run(result, offline_id); | 815 callback.Run(result, offline_id); |
| 805 } | 816 } |
| 806 | 817 |
| 818 void OfflinePageModelImpl::OnPagesFoundWithSameURL( | |
| 819 const ClientId& client_id, | |
| 820 int64_t offline_id, | |
| 821 size_t pages_allowed, | |
| 822 const MultipleOfflinePageItemResult& items) { | |
| 823 std::vector<int64_t> pages_to_delete; | |
| 824 for (const auto& item : items) { | |
| 825 if (item.offline_id != offline_id && | |
| 826 item.client_id.name_space == client_id.name_space) { | |
| 827 pages_to_delete.push_back(item.offline_id); | |
| 828 } | |
| 829 } | |
| 830 // Only keep |pages_allowed|-1 of most fresh pages, and delete others, by | |
| 831 // sorting pages with fresh ones first and resize the vector. | |
| 832 if (pages_to_delete.size() >= pages_allowed) { | |
| 833 sort(pages_to_delete.begin(), pages_to_delete.end(), | |
| 834 [this](int64_t a, int64_t b) -> bool { | |
| 835 return this->offline_pages_[a].last_access_time > | |
|
fgorski
2016/06/17 22:51:06
rewrite this to not use offline_pages_ so that it
romax
2016/06/20 20:04:41
Done.
| |
| 836 this->offline_pages_[b].last_access_time; | |
| 837 }); | |
| 838 pages_to_delete.resize(pages_to_delete.size() - pages_allowed + 1); | |
| 839 } | |
| 840 DeletePagesByOfflineId( | |
| 841 pages_to_delete, | |
| 842 base::Bind(&OfflinePageModelImpl::OnDeleteOldPagesWithSameURL, | |
| 843 weak_ptr_factory_.GetWeakPtr())); | |
| 844 } | |
| 845 | |
| 846 void OfflinePageModelImpl::OnDeleteOldPagesWithSameURL( | |
| 847 DeletePageResult result) { | |
| 848 // TODO(romax) Seems like nothing to do here. | |
| 849 } | |
| 850 | |
| 807 void OfflinePageModelImpl::DeletePendingArchiver( | 851 void OfflinePageModelImpl::DeletePendingArchiver( |
| 808 OfflinePageArchiver* archiver) { | 852 OfflinePageArchiver* archiver) { |
| 809 pending_archivers_.erase(std::find(pending_archivers_.begin(), | 853 pending_archivers_.erase(std::find(pending_archivers_.begin(), |
| 810 pending_archivers_.end(), archiver)); | 854 pending_archivers_.end(), archiver)); |
| 811 } | 855 } |
| 812 | 856 |
| 813 void OfflinePageModelImpl::OnDeleteArchiveFilesDone( | 857 void OfflinePageModelImpl::OnDeleteArchiveFilesDone( |
| 814 const std::vector<int64_t>& offline_ids, | 858 const std::vector<int64_t>& offline_ids, |
| 815 const DeletePageCallback& callback, | 859 const DeletePageCallback& callback, |
| 816 bool success) { | 860 bool success) { |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 983 void OfflinePageModelImpl::RunWhenLoaded(const base::Closure& task) { | 1027 void OfflinePageModelImpl::RunWhenLoaded(const base::Closure& task) { |
| 984 if (!is_loaded_) { | 1028 if (!is_loaded_) { |
| 985 delayed_tasks_.push_back(task); | 1029 delayed_tasks_.push_back(task); |
| 986 return; | 1030 return; |
| 987 } | 1031 } |
| 988 | 1032 |
| 989 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); | 1033 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); |
| 990 } | 1034 } |
| 991 | 1035 |
| 992 } // namespace offline_pages | 1036 } // namespace offline_pages |
| OLD | NEW |