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