| 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 590 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 601 } | 601 } |
| 602 }, | 602 }, |
| 603 callback); | 603 callback); |
| 604 | 604 |
| 605 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::GetPagesMatchingQuery, | 605 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::GetPagesMatchingQuery, |
| 606 weak_ptr_factory_.GetWeakPtr(), | 606 weak_ptr_factory_.GetWeakPtr(), |
| 607 base::Passed(builder.Build(GetPolicyController())), | 607 base::Passed(builder.Build(GetPolicyController())), |
| 608 multiple_callback)); | 608 multiple_callback)); |
| 609 } | 609 } |
| 610 | 610 |
| 611 void OfflinePageModelImpl::GetPagesByOnlineURL( | 611 void OfflinePageModelImpl::GetPagesByURL( |
| 612 const GURL& online_url, | 612 const GURL& url, |
| 613 URLSearchMode url_search_mode, |
| 613 const MultipleOfflinePageItemCallback& callback) { | 614 const MultipleOfflinePageItemCallback& callback) { |
| 614 RunWhenLoaded( | 615 RunWhenLoaded( |
| 615 base::Bind(&OfflinePageModelImpl::GetPagesByOnlineURLWhenLoadDone, | 616 base::Bind(&OfflinePageModelImpl::GetPagesByURLWhenLoadDone, |
| 616 weak_ptr_factory_.GetWeakPtr(), online_url, callback)); | 617 weak_ptr_factory_.GetWeakPtr(), url, |
| 618 url_search_mode, callback)); |
| 617 } | 619 } |
| 618 | 620 |
| 619 void OfflinePageModelImpl::GetPagesByOnlineURLWhenLoadDone( | 621 void OfflinePageModelImpl::GetPagesByURLWhenLoadDone( |
| 620 const GURL& online_url, | 622 const GURL& url, |
| 623 URLSearchMode url_search_mode, |
| 621 const MultipleOfflinePageItemCallback& callback) const { | 624 const MultipleOfflinePageItemCallback& callback) const { |
| 622 std::vector<OfflinePageItem> result; | 625 std::vector<OfflinePageItem> result; |
| 623 | 626 |
| 624 GURL::Replacements remove_params; | 627 GURL::Replacements remove_params; |
| 625 remove_params.ClearRef(); | 628 remove_params.ClearRef(); |
| 626 | 629 |
| 627 GURL online_url_without_fragment = | 630 GURL url_without_fragment = |
| 628 online_url.ReplaceComponents(remove_params); | 631 url.ReplaceComponents(remove_params); |
| 629 | 632 |
| 630 for (const auto& id_page_pair : offline_pages_) { | 633 for (const auto& id_page_pair : offline_pages_) { |
| 631 if (id_page_pair.second.IsExpired()) | 634 if (id_page_pair.second.IsExpired()) |
| 632 continue; | 635 continue; |
| 633 if (online_url == id_page_pair.second.url) { | 636 // First, search by last committed URL with fragment stripped. |
| 637 if (url_without_fragment == |
| 638 id_page_pair.second.url.ReplaceComponents(remove_params)) { |
| 634 result.push_back(id_page_pair.second); | 639 result.push_back(id_page_pair.second); |
| 635 continue; | 640 continue; |
| 636 } | 641 } |
| 637 // If the full URL does not match, try with the fragment identifier | 642 // Then, search by original request URL if |url_search_mode| wants it. |
| 638 // stripped. | 643 // Note that we want to do the exact match with fragment included. This is |
| 639 if (online_url_without_fragment == | 644 // because original URL is used for redirect purpose and it is always safer |
| 640 id_page_pair.second.url.ReplaceComponents(remove_params)) { | 645 // to support the exact redirect. |
| 646 if (url_search_mode == URLSearchMode::SEARCH_BY_ALL_URLS && |
| 647 url == id_page_pair.second.original_url) { |
| 641 result.push_back(id_page_pair.second); | 648 result.push_back(id_page_pair.second); |
| 642 } | 649 } |
| 643 } | 650 } |
| 644 | 651 |
| 645 callback.Run(result); | 652 callback.Run(result); |
| 646 } | 653 } |
| 647 | 654 |
| 648 void OfflinePageModelImpl::CheckMetadataConsistency() { | 655 void OfflinePageModelImpl::CheckMetadataConsistency() { |
| 649 DCHECK(is_loaded_); | 656 DCHECK(is_loaded_); |
| 650 archive_manager_->GetAllArchives( | 657 archive_manager_->GetAllArchives( |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 870 } | 877 } |
| 871 | 878 |
| 872 void OfflinePageModelImpl::DeleteExistingPagesWithSameURL( | 879 void OfflinePageModelImpl::DeleteExistingPagesWithSameURL( |
| 873 const OfflinePageItem& offline_page) { | 880 const OfflinePageItem& offline_page) { |
| 874 // Remove existing pages generated by the same policy and with same url. | 881 // Remove existing pages generated by the same policy and with same url. |
| 875 size_t pages_allowed = | 882 size_t pages_allowed = |
| 876 policy_controller_->GetPolicy(offline_page.client_id.name_space) | 883 policy_controller_->GetPolicy(offline_page.client_id.name_space) |
| 877 .pages_allowed_per_url; | 884 .pages_allowed_per_url; |
| 878 if (pages_allowed == kUnlimitedPages) | 885 if (pages_allowed == kUnlimitedPages) |
| 879 return; | 886 return; |
| 880 GetPagesByOnlineURL( | 887 GetPagesByURL( |
| 881 offline_page.url, | 888 offline_page.url, |
| 889 URLSearchMode::SEARCH_BY_FINAL_URL_ONLY, |
| 882 base::Bind(&OfflinePageModelImpl::OnPagesFoundWithSameURL, | 890 base::Bind(&OfflinePageModelImpl::OnPagesFoundWithSameURL, |
| 883 weak_ptr_factory_.GetWeakPtr(), offline_page, pages_allowed)); | 891 weak_ptr_factory_.GetWeakPtr(), offline_page, pages_allowed)); |
| 884 } | 892 } |
| 885 | 893 |
| 886 void OfflinePageModelImpl::OnPagesFoundWithSameURL( | 894 void OfflinePageModelImpl::OnPagesFoundWithSameURL( |
| 887 const OfflinePageItem& offline_page, | 895 const OfflinePageItem& offline_page, |
| 888 size_t pages_allowed, | 896 size_t pages_allowed, |
| 889 const MultipleOfflinePageItemResult& items) { | 897 const MultipleOfflinePageItemResult& items) { |
| 890 std::vector<OfflinePageItem> pages_to_delete; | 898 std::vector<OfflinePageItem> pages_to_delete; |
| 891 for (const auto& item : items) { | 899 for (const auto& item : items) { |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1089 } | 1097 } |
| 1090 | 1098 |
| 1091 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); | 1099 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); |
| 1092 } | 1100 } |
| 1093 | 1101 |
| 1094 base::Time OfflinePageModelImpl::GetCurrentTime() const { | 1102 base::Time OfflinePageModelImpl::GetCurrentTime() const { |
| 1095 return testing_clock_ ? testing_clock_->Now() : base::Time::Now(); | 1103 return testing_clock_ ? testing_clock_->Now() : base::Time::Now(); |
| 1096 } | 1104 } |
| 1097 | 1105 |
| 1098 } // namespace offline_pages | 1106 } // namespace offline_pages |
| OLD | NEW |