| 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" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" | 13 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" |
| 14 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | 14 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| 15 #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h" | 15 #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h" |
| 16 #include "chrome/browser/android/tab_android.h" | 16 #include "chrome/browser/android/tab_android.h" |
| 17 #include "components/offline_pages/client_namespace_constants.h" | 17 #include "components/offline_pages/client_namespace_constants.h" |
| 18 #include "components/offline_pages/client_policy_controller.h" |
| 18 #include "components/offline_pages/offline_page_feature.h" | 19 #include "components/offline_pages/offline_page_feature.h" |
| 19 #include "components/offline_pages/offline_page_item.h" | 20 #include "components/offline_pages/offline_page_item.h" |
| 20 #include "components/offline_pages/offline_page_model.h" | 21 #include "components/offline_pages/offline_page_model.h" |
| 21 #include "components/offline_pages/request_header/offline_page_header.h" | 22 #include "components/offline_pages/request_header/offline_page_header.h" |
| 22 #include "content/public/browser/browser_context.h" | 23 #include "content/public/browser/browser_context.h" |
| 23 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
| 24 #include "url/gurl.h" | 25 #include "url/gurl.h" |
| 25 | 26 |
| 26 namespace offline_pages { | 27 namespace offline_pages { |
| 27 namespace { | 28 namespace { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 50 const base::Callback<void(const GURL&)>& callback, | 51 const base::Callback<void(const GURL&)>& callback, |
| 51 const OfflinePageItem* item) { | 52 const OfflinePageItem* item) { |
| 52 GURL result_url; | 53 GURL result_url; |
| 53 if (item) | 54 if (item) |
| 54 result_url = item->url; | 55 result_url = item->url; |
| 55 callback.Run(result_url); | 56 callback.Run(result_url); |
| 56 } | 57 } |
| 57 | 58 |
| 58 void OnGetPagesByOnlineURLDone( | 59 void OnGetPagesByOnlineURLDone( |
| 59 int tab_id, | 60 int tab_id, |
| 61 const std::vector<std::string>& namespaces_to_show_in_original_tab, |
| 60 const base::Callback<void(const OfflinePageItem*)>& callback, | 62 const base::Callback<void(const OfflinePageItem*)>& callback, |
| 61 const MultipleOfflinePageItemResult& pages) { | 63 const MultipleOfflinePageItemResult& pages) { |
| 62 const OfflinePageItem* selected_page = nullptr; | 64 const OfflinePageItem* selected_page = nullptr; |
| 63 std::string tab_id_str = base::IntToString(tab_id); | 65 std::string tab_id_str = base::IntToString(tab_id); |
| 66 |
| 64 for (const auto& offline_page : pages) { | 67 for (const auto& offline_page : pages) { |
| 65 if (offline_page.client_id.name_space != kLastNNamespace || | 68 auto result = std::find(namespaces_to_show_in_original_tab.begin(), |
| 66 offline_page.client_id.id == tab_id_str) { | 69 namespaces_to_show_in_original_tab.end(), |
| 67 if (!selected_page || | 70 offline_page.client_id.name_space); |
| 68 offline_page.creation_time > selected_page->creation_time) { | 71 if (result != namespaces_to_show_in_original_tab.end() && |
| 69 selected_page = &offline_page; | 72 offline_page.client_id.id != tab_id_str) { |
| 70 } | 73 continue; |
| 74 } |
| 75 |
| 76 if (!selected_page || |
| 77 offline_page.creation_time > selected_page->creation_time) { |
| 78 selected_page = &offline_page; |
| 71 } | 79 } |
| 72 } | 80 } |
| 73 callback.Run(selected_page); | 81 callback.Run(selected_page); |
| 74 } | 82 } |
| 75 | 83 |
| 76 } // namespace | 84 } // namespace |
| 77 | 85 |
| 78 // static | 86 // static |
| 79 bool OfflinePageUtils::MightBeOfflineURL(const GURL& url) { | 87 bool OfflinePageUtils::MightBeOfflineURL(const GURL& url) { |
| 80 // It has to be a file URL ending with .mhtml extension. | 88 // It has to be a file URL ending with .mhtml extension. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 104 const base::Callback<void(const OfflinePageItem*)>& callback) { | 112 const base::Callback<void(const OfflinePageItem*)>& callback) { |
| 105 OfflinePageModel* offline_page_model = | 113 OfflinePageModel* offline_page_model = |
| 106 OfflinePageModelFactory::GetForBrowserContext(browser_context); | 114 OfflinePageModelFactory::GetForBrowserContext(browser_context); |
| 107 if (!offline_page_model) { | 115 if (!offline_page_model) { |
| 108 base::ThreadTaskRunnerHandle::Get()->PostTask( | 116 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 109 FROM_HERE, base::Bind(callback, nullptr)); | 117 FROM_HERE, base::Bind(callback, nullptr)); |
| 110 return; | 118 return; |
| 111 } | 119 } |
| 112 | 120 |
| 113 offline_page_model->GetPagesByOnlineURL( | 121 offline_page_model->GetPagesByOnlineURL( |
| 114 online_url, base::Bind(&OnGetPagesByOnlineURLDone, tab_id, callback)); | 122 online_url, base::Bind(&OnGetPagesByOnlineURLDone, tab_id, |
| 123 offline_page_model->GetPolicyController() |
| 124 ->GetNamespacesRestrictedToOriginalTab(), |
| 125 callback)); |
| 115 } | 126 } |
| 116 | 127 |
| 117 // static | 128 // static |
| 118 void OfflinePageUtils::GetOnlineURLForOfflineURL( | 129 void OfflinePageUtils::GetOnlineURLForOfflineURL( |
| 119 content::BrowserContext* browser_context, | 130 content::BrowserContext* browser_context, |
| 120 const GURL& offline_url, | 131 const GURL& offline_url, |
| 121 const base::Callback<void(const GURL&)>& callback) { | 132 const base::Callback<void(const GURL&)>& callback) { |
| 122 OfflinePageModel* offline_page_model = | 133 OfflinePageModel* offline_page_model = |
| 123 OfflinePageModelFactory::GetForBrowserContext(browser_context); | 134 OfflinePageModelFactory::GetForBrowserContext(browser_context); |
| 124 if (!offline_page_model) { | 135 if (!offline_page_model) { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 bool OfflinePageUtils::GetTabId(content::WebContents* web_contents, | 199 bool OfflinePageUtils::GetTabId(content::WebContents* web_contents, |
| 189 int* tab_id) { | 200 int* tab_id) { |
| 190 TabAndroid* tab_android = TabAndroid::FromWebContents(web_contents); | 201 TabAndroid* tab_android = TabAndroid::FromWebContents(web_contents); |
| 191 if (!tab_android) | 202 if (!tab_android) |
| 192 return false; | 203 return false; |
| 193 *tab_id = tab_android->GetAndroidId(); | 204 *tab_id = tab_android->GetAndroidId(); |
| 194 return true; | 205 return true; |
| 195 } | 206 } |
| 196 | 207 |
| 197 } // namespace offline_pages | 208 } // namespace offline_pages |
| OLD | NEW |