Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "chrome/browser/android/offline_pages/offline_page_utils.h" | 5 #include "chrome/browser/android/offline_pages/offline_page_utils.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 #include "components/offline_pages/offline_page_item.h" | 23 #include "components/offline_pages/offline_page_item.h" |
| 24 #include "components/offline_pages/offline_page_model.h" | 24 #include "components/offline_pages/offline_page_model.h" |
| 25 #include "components/offline_pages/request_header/offline_page_header.h" | 25 #include "components/offline_pages/request_header/offline_page_header.h" |
| 26 #include "content/public/browser/browser_context.h" | 26 #include "content/public/browser/browser_context.h" |
| 27 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
| 28 #include "url/gurl.h" | 28 #include "url/gurl.h" |
| 29 | 29 |
| 30 namespace offline_pages { | 30 namespace offline_pages { |
| 31 namespace { | 31 namespace { |
| 32 | 32 |
| 33 void OnGetPagesByOnlineURLDone( | 33 void OnGetPagesByURLDone( |
| 34 const GURL& url, | |
| 35 bool search_by_final_url_only, | |
| 34 int tab_id, | 36 int tab_id, |
| 35 const std::vector<std::string>& namespaces_to_show_in_original_tab, | 37 const std::vector<std::string>& namespaces_to_show_in_original_tab, |
| 36 const base::Callback<void(const OfflinePageItem*)>& callback, | 38 const base::Callback<void(const OfflinePageItem*)>& callback, |
| 37 const MultipleOfflinePageItemResult& pages) { | 39 const MultipleOfflinePageItemResult& pages) { |
| 38 const OfflinePageItem* selected_page = nullptr; | 40 const OfflinePageItem* selected_page_for_final_url = nullptr; |
| 41 const OfflinePageItem* selected_page_for_original_url = nullptr; | |
| 39 std::string tab_id_str = base::IntToString(tab_id); | 42 std::string tab_id_str = base::IntToString(tab_id); |
| 40 | 43 |
| 41 for (const auto& offline_page : pages) { | 44 for (const auto& page : pages) { |
| 42 auto result = std::find(namespaces_to_show_in_original_tab.begin(), | 45 auto result = std::find(namespaces_to_show_in_original_tab.begin(), |
| 43 namespaces_to_show_in_original_tab.end(), | 46 namespaces_to_show_in_original_tab.end(), |
| 44 offline_page.client_id.name_space); | 47 page.client_id.name_space); |
| 45 if (result != namespaces_to_show_in_original_tab.end() && | 48 if (result != namespaces_to_show_in_original_tab.end() && |
| 46 offline_page.client_id.id != tab_id_str) { | 49 page.client_id.id != tab_id_str) { |
| 47 continue; | 50 continue; |
| 48 } | 51 } |
| 49 | 52 |
| 50 if (!selected_page || | 53 if (OfflinePageUtils::EqualsIgnoringFragment(url, page.url)) { |
| 51 offline_page.creation_time > selected_page->creation_time) { | 54 if (!selected_page_for_final_url || |
| 52 selected_page = &offline_page; | 55 page.creation_time > selected_page_for_final_url->creation_time) { |
| 56 selected_page_for_final_url = &page; | |
| 57 } | |
| 58 } else { | |
| 59 DCHECK(!search_by_final_url_only); | |
| 60 DCHECK(url == page.original_url); | |
|
fgorski
2016/11/16 23:43:46
what about the fragment here?
Why is there no symm
jianli
2016/11/17 01:12:14
With more thought, I decided to change to do match
| |
| 61 if (!selected_page_for_original_url || | |
| 62 page.creation_time > selected_page_for_original_url->creation_time) { | |
| 63 selected_page_for_original_url = &page; | |
| 64 } | |
| 53 } | 65 } |
| 54 } | 66 } |
| 55 callback.Run(selected_page); | 67 |
| 68 // Match for final URL should take high priority than matching for original | |
| 69 // URL. | |
| 70 callback.Run(selected_page_for_final_url ? selected_page_for_final_url | |
| 71 : selected_page_for_original_url); | |
| 56 } | 72 } |
| 57 | 73 |
| 58 } // namespace | 74 } // namespace |
| 59 | 75 |
| 60 // static | 76 // static |
| 61 void OfflinePageUtils::SelectPageForOnlineURL( | 77 void OfflinePageUtils::SelectPageForURL( |
| 62 content::BrowserContext* browser_context, | 78 content::BrowserContext* browser_context, |
| 63 const GURL& online_url, | 79 const GURL& url, |
| 80 bool search_by_final_url_only, | |
| 64 int tab_id, | 81 int tab_id, |
| 65 const base::Callback<void(const OfflinePageItem*)>& callback) { | 82 const base::Callback<void(const OfflinePageItem*)>& callback) { |
| 66 OfflinePageModel* offline_page_model = | 83 OfflinePageModel* offline_page_model = |
| 67 OfflinePageModelFactory::GetForBrowserContext(browser_context); | 84 OfflinePageModelFactory::GetForBrowserContext(browser_context); |
| 68 if (!offline_page_model) { | 85 if (!offline_page_model) { |
| 69 base::ThreadTaskRunnerHandle::Get()->PostTask( | 86 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 70 FROM_HERE, base::Bind(callback, nullptr)); | 87 FROM_HERE, base::Bind(callback, nullptr)); |
| 71 return; | 88 return; |
| 72 } | 89 } |
| 73 | 90 |
| 74 offline_page_model->GetPagesByOnlineURL( | 91 offline_page_model->GetPagesByURL( |
| 75 online_url, base::Bind(&OnGetPagesByOnlineURLDone, tab_id, | 92 url, |
| 76 offline_page_model->GetPolicyController() | 93 search_by_final_url_only, |
| 77 ->GetNamespacesRestrictedToOriginalTab(), | 94 base::Bind(&OnGetPagesByURLDone, url, search_by_final_url_only, tab_id, |
| 78 callback)); | 95 offline_page_model->GetPolicyController() |
| 96 ->GetNamespacesRestrictedToOriginalTab(), | |
| 97 callback)); | |
| 79 } | 98 } |
| 80 | 99 |
| 81 const OfflinePageItem* OfflinePageUtils::GetOfflinePageFromWebContents( | 100 const OfflinePageItem* OfflinePageUtils::GetOfflinePageFromWebContents( |
| 82 content::WebContents* web_contents) { | 101 content::WebContents* web_contents) { |
| 83 OfflinePageTabHelper* tab_helper = | 102 OfflinePageTabHelper* tab_helper = |
| 84 OfflinePageTabHelper::FromWebContents(web_contents); | 103 OfflinePageTabHelper::FromWebContents(web_contents); |
| 85 if (!tab_helper) | 104 if (!tab_helper) |
| 86 return nullptr; | 105 return nullptr; |
| 87 const OfflinePageItem* offline_page = tab_helper->offline_page(); | 106 const OfflinePageItem* offline_page = tab_helper->offline_page(); |
| 88 if (!offline_page) | 107 if (!offline_page) |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 140 const std::vector<OfflinePageItem>& pages) { | 159 const std::vector<OfflinePageItem>& pages) { |
| 141 for (auto& page : pages) { | 160 for (auto& page : pages) { |
| 142 if (page.client_id.name_space == name_space) { | 161 if (page.client_id.name_space == name_space) { |
| 143 callback.Run(true); | 162 callback.Run(true); |
| 144 return; | 163 return; |
| 145 } | 164 } |
| 146 } | 165 } |
| 147 callback.Run(false); | 166 callback.Run(false); |
| 148 }; | 167 }; |
| 149 | 168 |
| 150 offline_page_model->GetPagesByOnlineURL( | 169 offline_page_model->GetPagesByURL( |
| 151 offline_page_url, base::Bind(continuation, name_space, callback)); | 170 offline_page_url, |
| 171 true /* search_by_final_url_only */, | |
| 172 base::Bind(continuation, name_space, callback)); | |
| 152 } | 173 } |
| 153 | 174 |
| 154 // static | 175 // static |
| 155 void OfflinePageUtils::CheckExistenceOfRequestsWithURL( | 176 void OfflinePageUtils::CheckExistenceOfRequestsWithURL( |
| 156 content::BrowserContext* browser_context, | 177 content::BrowserContext* browser_context, |
| 157 const std::string name_space, | 178 const std::string name_space, |
| 158 const GURL& offline_page_url, | 179 const GURL& offline_page_url, |
| 159 const base::Callback<void(bool)>& callback) { | 180 const base::Callback<void(bool)>& callback) { |
| 160 RequestCoordinator* request_coordinator = | 181 RequestCoordinator* request_coordinator = |
| 161 RequestCoordinatorFactory::GetForBrowserContext(browser_context); | 182 RequestCoordinatorFactory::GetForBrowserContext(browser_context); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 185 GURL::Replacements remove_params; | 206 GURL::Replacements remove_params; |
| 186 remove_params.ClearRef(); | 207 remove_params.ClearRef(); |
| 187 | 208 |
| 188 GURL lhs_stripped = lhs.ReplaceComponents(remove_params); | 209 GURL lhs_stripped = lhs.ReplaceComponents(remove_params); |
| 189 GURL rhs_stripped = lhs.ReplaceComponents(remove_params); | 210 GURL rhs_stripped = lhs.ReplaceComponents(remove_params); |
| 190 | 211 |
| 191 return lhs_stripped == rhs_stripped; | 212 return lhs_stripped == rhs_stripped; |
| 192 } | 213 } |
| 193 | 214 |
| 194 } // namespace offline_pages | 215 } // namespace offline_pages |
| OLD | NEW |