| 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/offline_page_feature.h" | 18 #include "components/offline_pages/offline_page_feature.h" |
| 18 #include "components/offline_pages/offline_page_item.h" | 19 #include "components/offline_pages/offline_page_item.h" |
| 19 #include "components/offline_pages/offline_page_model.h" | 20 #include "components/offline_pages/offline_page_model.h" |
| 20 #include "content/public/browser/browser_context.h" | 21 #include "content/public/browser/browser_context.h" |
| 21 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
| 22 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 23 | 24 |
| 24 namespace offline_pages { | 25 namespace offline_pages { |
| 25 namespace { | 26 namespace { |
| 26 | 27 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 | 67 |
| 67 void OnGetPageByOfflineURLDone( | 68 void OnGetPageByOfflineURLDone( |
| 68 const base::Callback<void(const GURL&)>& callback, | 69 const base::Callback<void(const GURL&)>& callback, |
| 69 const OfflinePageItem* item) { | 70 const OfflinePageItem* item) { |
| 70 GURL result_url; | 71 GURL result_url; |
| 71 if (item) | 72 if (item) |
| 72 result_url = item->url; | 73 result_url = item->url; |
| 73 callback.Run(result_url); | 74 callback.Run(result_url); |
| 74 } | 75 } |
| 75 | 76 |
| 77 void OnGetPagesByOnlineURLDone( |
| 78 int tab_id, |
| 79 const base::Callback<void(const OfflinePageItem*)>& callback, |
| 80 const MultipleOfflinePageItemResult& pages) { |
| 81 const OfflinePageItem* selected_page = nullptr; |
| 82 std::string tab_id_str = base::IntToString(tab_id); |
| 83 for (const auto& offline_page : pages) { |
| 84 if ((offline_page.client_id.name_space == kBookmarkNamespace) || |
| 85 (offline_page.client_id.name_space == kAsyncNamespace) || |
| 86 (offline_page.client_id.name_space == kLastNNamespace && |
| 87 offline_page.client_id.id == tab_id_str)) { |
| 88 if (!selected_page || |
| 89 offline_page.creation_time > selected_page->creation_time) { |
| 90 selected_page = &offline_page; |
| 91 } |
| 92 } |
| 93 } |
| 94 callback.Run(selected_page); |
| 95 } |
| 96 |
| 76 } // namespace | 97 } // namespace |
| 77 | 98 |
| 78 // static | 99 // static |
| 79 bool OfflinePageUtils::MightBeOfflineURL(const GURL& url) { | 100 bool OfflinePageUtils::MightBeOfflineURL(const GURL& url) { |
| 80 // It has to be a file URL ending with .mhtml extension. | 101 // It has to be a file URL ending with .mhtml extension. |
| 81 return url.is_valid() && url.SchemeIsFile() && | 102 return url.is_valid() && url.SchemeIsFile() && |
| 82 base::EndsWith(url.spec(), | 103 base::EndsWith(url.spec(), |
| 83 OfflinePageMHTMLArchiver::GetFileNameExtension(), | 104 OfflinePageMHTMLArchiver::GetFileNameExtension(), |
| 84 base::CompareCase::INSENSITIVE_ASCII); | 105 base::CompareCase::INSENSITIVE_ASCII); |
| 85 } | 106 } |
| 86 | 107 |
| 87 // static | 108 // static |
| 88 GURL OfflinePageUtils::MaybeGetOnlineURLForOfflineURL( | 109 GURL OfflinePageUtils::MaybeGetOnlineURLForOfflineURL( |
| 89 content::BrowserContext* browser_context, | 110 content::BrowserContext* browser_context, |
| 90 const GURL& offline_url) { | 111 const GURL& offline_url) { |
| 91 const OfflinePageItem* offline_page = | 112 const OfflinePageItem* offline_page = |
| 92 GetOfflinePageForOfflineURL(browser_context, offline_url); | 113 GetOfflinePageForOfflineURL(browser_context, offline_url); |
| 93 if (!offline_page) | 114 if (!offline_page) |
| 94 return GURL(); | 115 return GURL(); |
| 95 | 116 |
| 96 return offline_page->url; | 117 return offline_page->url; |
| 97 } | 118 } |
| 98 | 119 |
| 99 // static | 120 // static |
| 121 void OfflinePageUtils::SelectPageForOnlineURL( |
| 122 content::BrowserContext* browser_context, |
| 123 const GURL& online_url, |
| 124 int tab_id, |
| 125 const base::Callback<void(const OfflinePageItem*)>& callback) { |
| 126 OfflinePageModel* offline_page_model = |
| 127 OfflinePageModelFactory::GetForBrowserContext(browser_context); |
| 128 if (!offline_page_model) { |
| 129 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 130 FROM_HERE, base::Bind(callback, nullptr)); |
| 131 return; |
| 132 } |
| 133 |
| 134 offline_page_model->GetPagesByOnlineURL( |
| 135 online_url, base::Bind(&OnGetPagesByOnlineURLDone, tab_id, callback)); |
| 136 } |
| 137 |
| 138 // static |
| 100 void OfflinePageUtils::GetOnlineURLForOfflineURL( | 139 void OfflinePageUtils::GetOnlineURLForOfflineURL( |
| 101 content::BrowserContext* browser_context, | 140 content::BrowserContext* browser_context, |
| 102 const GURL& offline_url, | 141 const GURL& offline_url, |
| 103 const base::Callback<void(const GURL&)>& callback) { | 142 const base::Callback<void(const GURL&)>& callback) { |
| 104 OfflinePageModel* offline_page_model = | 143 OfflinePageModel* offline_page_model = |
| 105 OfflinePageModelFactory::GetForBrowserContext(browser_context); | 144 OfflinePageModelFactory::GetForBrowserContext(browser_context); |
| 106 if (!offline_page_model) { | 145 if (!offline_page_model) { |
| 107 base::ThreadTaskRunnerHandle::Get()->PostTask( | 146 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 108 FROM_HERE, base::Bind(&OnGetPageByOfflineURLDone, callback, nullptr)); | 147 FROM_HERE, base::Bind(&OnGetPageByOfflineURLDone, callback, nullptr)); |
| 109 return; | 148 return; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 bool OfflinePageUtils::GetTabId(content::WebContents* web_contents, | 194 bool OfflinePageUtils::GetTabId(content::WebContents* web_contents, |
| 156 int* tab_id) { | 195 int* tab_id) { |
| 157 TabAndroid* tab_android = TabAndroid::FromWebContents(web_contents); | 196 TabAndroid* tab_android = TabAndroid::FromWebContents(web_contents); |
| 158 if (!tab_android) | 197 if (!tab_android) |
| 159 return false; | 198 return false; |
| 160 *tab_id = tab_android->GetAndroidId(); | 199 *tab_id = tab_android->GetAndroidId(); |
| 161 return true; | 200 return true; |
| 162 } | 201 } |
| 163 | 202 |
| 164 } // namespace offline_pages | 203 } // namespace offline_pages |
| OLD | NEW |