| 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 722 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 733 result = SavePageResult::SUCCESS; | 733 result = SavePageResult::SUCCESS; |
| 734 ReportPageHistogramAfterSave(offline_page); | 734 ReportPageHistogramAfterSave(offline_page); |
| 735 offline_event_logger_.RecordPageSaved( | 735 offline_event_logger_.RecordPageSaved( |
| 736 offline_page.client_id.name_space, offline_page.url.spec(), | 736 offline_page.client_id.name_space, offline_page.url.spec(), |
| 737 std::to_string(offline_page.offline_id)); | 737 std::to_string(offline_page.offline_id)); |
| 738 } else { | 738 } else { |
| 739 result = SavePageResult::STORE_FAILURE; | 739 result = SavePageResult::STORE_FAILURE; |
| 740 } | 740 } |
| 741 InformSavePageDone(callback, result, offline_page.client_id, | 741 InformSavePageDone(callback, result, offline_page.client_id, |
| 742 offline_page.offline_id); | 742 offline_page.offline_id); |
| 743 if (result == SavePageResult::SUCCESS) { |
| 744 DeleteExistingPagesWithSameURL(offline_page); |
| 745 } else { |
| 746 PostClearStorageIfNeededTask(); |
| 747 } |
| 748 |
| 743 DeletePendingArchiver(archiver); | 749 DeletePendingArchiver(archiver); |
| 744 | |
| 745 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this)); | 750 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this)); |
| 746 } | 751 } |
| 747 | 752 |
| 748 void OfflinePageModelImpl::OnMarkPageAccesseDone( | 753 void OfflinePageModelImpl::OnMarkPageAccesseDone( |
| 749 const OfflinePageItem& offline_page_item, | 754 const OfflinePageItem& offline_page_item, |
| 750 bool success) { | 755 bool success) { |
| 751 // Update the item in the cache only upon success. | 756 // Update the item in the cache only upon success. |
| 752 if (success) | 757 if (success) |
| 753 offline_pages_[offline_page_item.offline_id] = offline_page_item; | 758 offline_pages_[offline_page_item.offline_id] = offline_page_item; |
| 754 | 759 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 801 kStorageManagerStartingDelay); | 806 kStorageManagerStartingDelay); |
| 802 } | 807 } |
| 803 | 808 |
| 804 void OfflinePageModelImpl::InformSavePageDone(const SavePageCallback& callback, | 809 void OfflinePageModelImpl::InformSavePageDone(const SavePageCallback& callback, |
| 805 SavePageResult result, | 810 SavePageResult result, |
| 806 const ClientId& client_id, | 811 const ClientId& client_id, |
| 807 int64_t offline_id) { | 812 int64_t offline_id) { |
| 808 ReportSavePageResultHistogramAfterSave(client_id, result); | 813 ReportSavePageResultHistogramAfterSave(client_id, result); |
| 809 archive_manager_->GetStorageStats( | 814 archive_manager_->GetStorageStats( |
| 810 base::Bind(&ReportStorageHistogramsAfterSave)); | 815 base::Bind(&ReportStorageHistogramsAfterSave)); |
| 811 // Remove existing pages generated by the same policy and with same url. | |
| 812 size_t pages_allowed = | |
| 813 policy_controller_->GetPolicy(client_id.name_space).pages_allowed_per_url; | |
| 814 if (result == SavePageResult::SUCCESS && pages_allowed != kUnlimitedPages) { | |
| 815 GetPagesByOnlineURL( | |
| 816 offline_pages_[offline_id].url, | |
| 817 base::Bind(&OfflinePageModelImpl::OnPagesFoundWithSameURL, | |
| 818 weak_ptr_factory_.GetWeakPtr(), client_id, offline_id, | |
| 819 pages_allowed)); | |
| 820 } else { | |
| 821 PostClearStorageIfNeededTask(); | |
| 822 } | |
| 823 callback.Run(result, offline_id); | 816 callback.Run(result, offline_id); |
| 824 } | 817 } |
| 825 | 818 |
| 819 void OfflinePageModelImpl::DeleteExistingPagesWithSameURL( |
| 820 const OfflinePageItem& offline_page) { |
| 821 // Remove existing pages generated by the same policy and with same url. |
| 822 size_t pages_allowed = |
| 823 policy_controller_->GetPolicy(offline_page.client_id.name_space) |
| 824 .pages_allowed_per_url; |
| 825 if (pages_allowed == kUnlimitedPages) |
| 826 return; |
| 827 GetPagesByOnlineURL( |
| 828 offline_page.url, |
| 829 base::Bind(&OfflinePageModelImpl::OnPagesFoundWithSameURL, |
| 830 weak_ptr_factory_.GetWeakPtr(), offline_page, pages_allowed)); |
| 831 } |
| 832 |
| 826 void OfflinePageModelImpl::OnPagesFoundWithSameURL( | 833 void OfflinePageModelImpl::OnPagesFoundWithSameURL( |
| 827 const ClientId& client_id, | 834 const OfflinePageItem& offline_page, |
| 828 int64_t offline_id, | |
| 829 size_t pages_allowed, | 835 size_t pages_allowed, |
| 830 const MultipleOfflinePageItemResult& items) { | 836 const MultipleOfflinePageItemResult& items) { |
| 831 std::vector<OfflinePageItem> pages_to_delete; | 837 std::vector<OfflinePageItem> pages_to_delete; |
| 832 for (const auto& item : items) { | 838 for (const auto& item : items) { |
| 833 if (item.offline_id != offline_id && | 839 if (item.offline_id != offline_page.offline_id && |
| 834 item.client_id.name_space == client_id.name_space) { | 840 item.client_id.name_space == offline_page.client_id.name_space) { |
| 835 pages_to_delete.push_back(item); | 841 pages_to_delete.push_back(item); |
| 836 } | 842 } |
| 837 } | 843 } |
| 838 // Only keep |pages_allowed|-1 of most fresh pages and delete others, by | 844 // Only keep |pages_allowed|-1 of most fresh pages and delete others, by |
| 839 // sorting pages with the oldest ones first and resize the vector. | 845 // sorting pages with the oldest ones first and resize the vector. |
| 840 if (pages_to_delete.size() >= pages_allowed) { | 846 if (pages_to_delete.size() >= pages_allowed) { |
| 841 sort(pages_to_delete.begin(), pages_to_delete.end(), | 847 sort(pages_to_delete.begin(), pages_to_delete.end(), |
| 842 [](const OfflinePageItem& a, const OfflinePageItem& b) -> bool { | 848 [](const OfflinePageItem& a, const OfflinePageItem& b) -> bool { |
| 843 return a.last_access_time < b.last_access_time; | 849 return a.last_access_time < b.last_access_time; |
| 844 }); | 850 }); |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1053 void OfflinePageModelImpl::RunWhenLoaded(const base::Closure& task) { | 1059 void OfflinePageModelImpl::RunWhenLoaded(const base::Closure& task) { |
| 1054 if (!is_loaded_) { | 1060 if (!is_loaded_) { |
| 1055 delayed_tasks_.push_back(task); | 1061 delayed_tasks_.push_back(task); |
| 1056 return; | 1062 return; |
| 1057 } | 1063 } |
| 1058 | 1064 |
| 1059 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); | 1065 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); |
| 1060 } | 1066 } |
| 1061 | 1067 |
| 1062 } // namespace offline_pages | 1068 } // namespace offline_pages |
| OLD | NEW |