| 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/offline_page_feature.h" | 18 #include "components/offline_pages/offline_page_feature.h" |
| 19 #include "components/offline_pages/offline_page_item.h" | 19 #include "components/offline_pages/offline_page_item.h" |
| 20 #include "components/offline_pages/offline_page_model.h" | 20 #include "components/offline_pages/offline_page_model.h" |
| 21 #include "components/offline_pages/request_header/offline_page_header.h" | 21 #include "components/offline_pages/request_header/offline_page_header.h" |
| 22 #include "content/public/browser/browser_context.h" | 22 #include "content/public/browser/browser_context.h" |
| 23 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
| 24 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 25 | 25 |
| 26 namespace offline_pages { | 26 namespace offline_pages { |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 // Returns an offline page that is stored as the |offline_url|. | |
| 30 const OfflinePageItem* GetOfflinePageForOfflineURL( | |
| 31 content::BrowserContext* browser_context, | |
| 32 const GURL& offline_url) { | |
| 33 DCHECK(browser_context); | |
| 34 | |
| 35 // Note that we first check if the url likely points to an offline page | |
| 36 // before calling GetPageByOfflineURL in order to avoid unnecessary lookup | |
| 37 // cost. | |
| 38 if (!OfflinePageUtils::MightBeOfflineURL(offline_url)) | |
| 39 return nullptr; | |
| 40 | |
| 41 OfflinePageModel* offline_page_model = | |
| 42 OfflinePageModelFactory::GetForBrowserContext(browser_context); | |
| 43 if (!offline_page_model) | |
| 44 return nullptr; | |
| 45 | |
| 46 return offline_page_model->MaybeGetPageByOfflineURL(offline_url); | |
| 47 } | |
| 48 | |
| 49 void OnGetPageByOfflineURLDone( | |
| 50 const base::Callback<void(const GURL&)>& callback, | |
| 51 const OfflinePageItem* item) { | |
| 52 GURL result_url; | |
| 53 if (item) | |
| 54 result_url = item->url; | |
| 55 callback.Run(result_url); | |
| 56 } | |
| 57 | |
| 58 void OnGetPagesByOnlineURLDone( | 29 void OnGetPagesByOnlineURLDone( |
| 59 int tab_id, | 30 int tab_id, |
| 60 const base::Callback<void(const OfflinePageItem*)>& callback, | 31 const base::Callback<void(const OfflinePageItem*)>& callback, |
| 61 const MultipleOfflinePageItemResult& pages) { | 32 const MultipleOfflinePageItemResult& pages) { |
| 62 const OfflinePageItem* selected_page = nullptr; | 33 const OfflinePageItem* selected_page = nullptr; |
| 63 std::string tab_id_str = base::IntToString(tab_id); | 34 std::string tab_id_str = base::IntToString(tab_id); |
| 64 for (const auto& offline_page : pages) { | 35 for (const auto& offline_page : pages) { |
| 65 if (offline_page.client_id.name_space != kLastNNamespace || | 36 if (offline_page.client_id.name_space != kLastNNamespace || |
| 66 offline_page.client_id.id == tab_id_str) { | 37 offline_page.client_id.id == tab_id_str) { |
| 67 if (!selected_page || | 38 if (!selected_page || |
| 68 offline_page.creation_time > selected_page->creation_time) { | 39 offline_page.creation_time > selected_page->creation_time) { |
| 69 selected_page = &offline_page; | 40 selected_page = &offline_page; |
| 70 } | 41 } |
| 71 } | 42 } |
| 72 } | 43 } |
| 73 callback.Run(selected_page); | 44 callback.Run(selected_page); |
| 74 } | 45 } |
| 75 | 46 |
| 76 } // namespace | 47 } // namespace |
| 77 | 48 |
| 78 // static | 49 // static |
| 79 bool OfflinePageUtils::MightBeOfflineURL(const GURL& url) { | |
| 80 // It has to be a file URL ending with .mhtml extension. | |
| 81 return url.is_valid() && url.SchemeIsFile() && | |
| 82 base::EndsWith(url.spec(), | |
| 83 OfflinePageMHTMLArchiver::GetFileNameExtension(), | |
| 84 base::CompareCase::INSENSITIVE_ASCII); | |
| 85 } | |
| 86 | |
| 87 // static | |
| 88 GURL OfflinePageUtils::MaybeGetOnlineURLForOfflineURL( | |
| 89 content::BrowserContext* browser_context, | |
| 90 const GURL& offline_url) { | |
| 91 const OfflinePageItem* offline_page = | |
| 92 GetOfflinePageForOfflineURL(browser_context, offline_url); | |
| 93 if (!offline_page) | |
| 94 return GURL(); | |
| 95 | |
| 96 return offline_page->url; | |
| 97 } | |
| 98 | |
| 99 // static | |
| 100 void OfflinePageUtils::SelectPageForOnlineURL( | 50 void OfflinePageUtils::SelectPageForOnlineURL( |
| 101 content::BrowserContext* browser_context, | 51 content::BrowserContext* browser_context, |
| 102 const GURL& online_url, | 52 const GURL& online_url, |
| 103 int tab_id, | 53 int tab_id, |
| 104 const base::Callback<void(const OfflinePageItem*)>& callback) { | 54 const base::Callback<void(const OfflinePageItem*)>& callback) { |
| 105 OfflinePageModel* offline_page_model = | 55 OfflinePageModel* offline_page_model = |
| 106 OfflinePageModelFactory::GetForBrowserContext(browser_context); | 56 OfflinePageModelFactory::GetForBrowserContext(browser_context); |
| 107 if (!offline_page_model) { | 57 if (!offline_page_model) { |
| 108 base::ThreadTaskRunnerHandle::Get()->PostTask( | 58 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 109 FROM_HERE, base::Bind(callback, nullptr)); | 59 FROM_HERE, base::Bind(callback, nullptr)); |
| 110 return; | 60 return; |
| 111 } | 61 } |
| 112 | 62 |
| 113 offline_page_model->GetPagesByOnlineURL( | 63 offline_page_model->GetPagesByOnlineURL( |
| 114 online_url, base::Bind(&OnGetPagesByOnlineURLDone, tab_id, callback)); | 64 online_url, base::Bind(&OnGetPagesByOnlineURLDone, tab_id, callback)); |
| 115 } | 65 } |
| 116 | 66 |
| 117 // static | |
| 118 void OfflinePageUtils::GetOnlineURLForOfflineURL( | |
| 119 content::BrowserContext* browser_context, | |
| 120 const GURL& offline_url, | |
| 121 const base::Callback<void(const GURL&)>& callback) { | |
| 122 OfflinePageModel* offline_page_model = | |
| 123 OfflinePageModelFactory::GetForBrowserContext(browser_context); | |
| 124 if (!offline_page_model) { | |
| 125 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 126 FROM_HERE, base::Bind(&OnGetPageByOfflineURLDone, callback, nullptr)); | |
| 127 return; | |
| 128 } | |
| 129 | |
| 130 offline_page_model->GetPageByOfflineURL( | |
| 131 offline_url, base::Bind(&OnGetPageByOfflineURLDone, callback)); | |
| 132 } | |
| 133 | |
| 134 // static | |
| 135 bool OfflinePageUtils::IsOfflinePage(content::BrowserContext* browser_context, | |
| 136 const GURL& offline_url) { | |
| 137 return GetOfflinePageForOfflineURL(browser_context, offline_url) != nullptr; | |
| 138 } | |
| 139 | |
| 140 // static | |
| 141 void OfflinePageUtils::MarkPageAccessed( | |
| 142 content::BrowserContext* browser_context, const GURL& offline_url) { | |
| 143 DCHECK(browser_context); | |
| 144 | |
| 145 const OfflinePageItem* offline_page = | |
| 146 GetOfflinePageForOfflineURL(browser_context, offline_url); | |
| 147 if (!offline_page) | |
| 148 return; | |
| 149 | |
| 150 OfflinePageModel* offline_page_model = | |
| 151 OfflinePageModelFactory::GetForBrowserContext(browser_context); | |
| 152 DCHECK(offline_page_model); | |
| 153 offline_page_model->MarkPageAccessed(offline_page->offline_id); | |
| 154 } | |
| 155 | |
| 156 // static | |
| 157 const OfflinePageItem* OfflinePageUtils::GetOfflinePageFromWebContents( | 67 const OfflinePageItem* OfflinePageUtils::GetOfflinePageFromWebContents( |
| 158 content::WebContents* web_contents) { | 68 content::WebContents* web_contents) { |
| 159 OfflinePageTabHelper* tab_helper = | 69 OfflinePageTabHelper* tab_helper = |
| 160 OfflinePageTabHelper::FromWebContents(web_contents); | 70 OfflinePageTabHelper::FromWebContents(web_contents); |
| 161 if (!tab_helper) | 71 if (!tab_helper) |
| 162 return nullptr; | 72 return nullptr; |
| 163 const OfflinePageItem* offline_page = tab_helper->offline_page(); | 73 const OfflinePageItem* offline_page = tab_helper->offline_page(); |
| 164 if (!offline_page) | 74 if (!offline_page) |
| 165 return nullptr; | 75 return nullptr; |
| 166 | 76 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 188 bool OfflinePageUtils::GetTabId(content::WebContents* web_contents, | 98 bool OfflinePageUtils::GetTabId(content::WebContents* web_contents, |
| 189 int* tab_id) { | 99 int* tab_id) { |
| 190 TabAndroid* tab_android = TabAndroid::FromWebContents(web_contents); | 100 TabAndroid* tab_android = TabAndroid::FromWebContents(web_contents); |
| 191 if (!tab_android) | 101 if (!tab_android) |
| 192 return false; | 102 return false; |
| 193 *tab_id = tab_android->GetAndroidId(); | 103 *tab_id = tab_android->GetAndroidId(); |
| 194 return true; | 104 return true; |
| 195 } | 105 } |
| 196 | 106 |
| 197 } // namespace offline_pages | 107 } // namespace offline_pages |
| OLD | NEW |