| 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" |
| 8 #include "base/location.h" |
| 9 #include "base/optional.h" |
| 7 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 8 #include "base/strings/string_piece.h" | 11 #include "base/strings/string_piece.h" |
| 9 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" |
| 10 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" | 14 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" |
| 11 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | 15 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| 12 #include "components/offline_pages/offline_page_feature.h" | 16 #include "components/offline_pages/offline_page_feature.h" |
| 13 #include "components/offline_pages/offline_page_item.h" | 17 #include "components/offline_pages/offline_page_item.h" |
| 14 #include "components/offline_pages/offline_page_model.h" | 18 #include "components/offline_pages/offline_page_model.h" |
| 15 #include "content/public/browser/browser_context.h" | 19 #include "content/public/browser/browser_context.h" |
| 16 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 17 | 21 |
| 18 namespace offline_pages { | 22 namespace offline_pages { |
| 19 namespace { | 23 namespace { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 return nullptr; | 55 return nullptr; |
| 52 | 56 |
| 53 OfflinePageModel* offline_page_model = | 57 OfflinePageModel* offline_page_model = |
| 54 OfflinePageModelFactory::GetForBrowserContext(browser_context); | 58 OfflinePageModelFactory::GetForBrowserContext(browser_context); |
| 55 if (!offline_page_model) | 59 if (!offline_page_model) |
| 56 return nullptr; | 60 return nullptr; |
| 57 | 61 |
| 58 return offline_page_model->MaybeGetPageByOfflineURL(offline_url); | 62 return offline_page_model->MaybeGetPageByOfflineURL(offline_url); |
| 59 } | 63 } |
| 60 | 64 |
| 65 void OnGetPageByOfflineURLDone( |
| 66 const base::Callback<void(const GURL&)>& callback, |
| 67 const base::Optional<OfflinePageItem>& item) { |
| 68 GURL result_url; |
| 69 if (item) |
| 70 result_url = item->url; |
| 71 callback.Run(result_url); |
| 72 } |
| 73 |
| 74 void OnGetBestPageForOnlineURLDone( |
| 75 const base::Callback<void(const GURL&)>& callback, |
| 76 const base::Optional<OfflinePageItem>& item) { |
| 77 GURL result_url; |
| 78 if (item) |
| 79 result_url = item->GetOfflineURL(); |
| 80 callback.Run(result_url); |
| 81 } |
| 82 |
| 61 } // namespace | 83 } // namespace |
| 62 | 84 |
| 63 // static | 85 // static |
| 64 bool OfflinePageUtils::MightBeOfflineURL(const GURL& url) { | 86 bool OfflinePageUtils::MightBeOfflineURL(const GURL& url) { |
| 65 // It has to be a file URL ending with .mhtml extension. | 87 // It has to be a file URL ending with .mhtml extension. |
| 66 return url.is_valid() && url.SchemeIsFile() && | 88 return url.is_valid() && url.SchemeIsFile() && |
| 67 base::EndsWith(url.spec(), | 89 base::EndsWith(url.spec(), |
| 68 OfflinePageMHTMLArchiver::GetFileNameExtension(), | 90 OfflinePageMHTMLArchiver::GetFileNameExtension(), |
| 69 base::CompareCase::INSENSITIVE_ASCII); | 91 base::CompareCase::INSENSITIVE_ASCII); |
| 70 } | 92 } |
| 71 | 93 |
| 72 // static | 94 // static |
| 73 GURL OfflinePageUtils::GetOfflineURLForOnlineURL( | 95 GURL OfflinePageUtils::MaybeGetOfflineURLForOnlineURL( |
| 74 content::BrowserContext* browser_context, | 96 content::BrowserContext* browser_context, |
| 75 const GURL& online_url) { | 97 const GURL& online_url) { |
| 76 const OfflinePageItem* offline_page = | 98 const OfflinePageItem* offline_page = |
| 77 MaybeGetBestOfflinePageForOnlineURL(browser_context, online_url); | 99 MaybeGetBestOfflinePageForOnlineURL(browser_context, online_url); |
| 78 if (!offline_page) | 100 if (!offline_page) |
| 79 return GURL(); | 101 return GURL(); |
| 80 | 102 |
| 81 return offline_page->GetOfflineURL(); | 103 return offline_page->GetOfflineURL(); |
| 82 } | 104 } |
| 83 | 105 |
| 84 // static | 106 // static |
| 85 GURL OfflinePageUtils::GetOnlineURLForOfflineURL( | 107 void OfflinePageUtils::GetOfflineURLForOnlineURL( |
| 108 content::BrowserContext* browser_context, |
| 109 const GURL& online_url, |
| 110 const base::Callback<void(const GURL&)>& callback) { |
| 111 OfflinePageModel* offline_page_model = |
| 112 OfflinePageModelFactory::GetForBrowserContext(browser_context); |
| 113 if (!offline_page_model) { |
| 114 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 115 FROM_HERE, |
| 116 base::Bind(&OnGetPageByOfflineURLDone, callback, base::nullopt)); |
| 117 return; |
| 118 } |
| 119 |
| 120 offline_page_model->GetBestPageForOnlineURL( |
| 121 online_url, base::Bind(&OnGetBestPageForOnlineURLDone, callback)); |
| 122 } |
| 123 |
| 124 // static |
| 125 GURL OfflinePageUtils::MaybeGetOnlineURLForOfflineURL( |
| 86 content::BrowserContext* browser_context, | 126 content::BrowserContext* browser_context, |
| 87 const GURL& offline_url) { | 127 const GURL& offline_url) { |
| 88 const OfflinePageItem* offline_page = | 128 const OfflinePageItem* offline_page = |
| 89 GetOfflinePageForOfflineURL(browser_context, offline_url); | 129 GetOfflinePageForOfflineURL(browser_context, offline_url); |
| 90 if (!offline_page) | 130 if (!offline_page) |
| 91 return GURL(); | 131 return GURL(); |
| 92 | 132 |
| 93 return offline_page->url; | 133 return offline_page->url; |
| 94 } | 134 } |
| 95 | 135 |
| 96 // static | 136 // static |
| 137 void OfflinePageUtils::GetOnlineURLForOfflineURL( |
| 138 content::BrowserContext* browser_context, |
| 139 const GURL& offline_url, |
| 140 const base::Callback<void(const GURL&)>& callback) { |
| 141 OfflinePageModel* offline_page_model = |
| 142 OfflinePageModelFactory::GetForBrowserContext(browser_context); |
| 143 if (!offline_page_model) { |
| 144 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 145 FROM_HERE, |
| 146 base::Bind(&OnGetPageByOfflineURLDone, callback, base::nullopt)); |
| 147 return; |
| 148 } |
| 149 |
| 150 offline_page_model->GetPageByOfflineURL( |
| 151 offline_url, base::Bind(&OnGetPageByOfflineURLDone, callback)); |
| 152 } |
| 153 |
| 154 // static |
| 97 bool OfflinePageUtils::IsOfflinePage(content::BrowserContext* browser_context, | 155 bool OfflinePageUtils::IsOfflinePage(content::BrowserContext* browser_context, |
| 98 const GURL& offline_url) { | 156 const GURL& offline_url) { |
| 99 return GetOfflinePageForOfflineURL(browser_context, offline_url) != nullptr; | 157 return GetOfflinePageForOfflineURL(browser_context, offline_url) != nullptr; |
| 100 } | 158 } |
| 101 | 159 |
| 102 // static | 160 // static |
| 103 bool OfflinePageUtils::HasOfflinePageForOnlineURL( | 161 bool OfflinePageUtils::HasOfflinePageForOnlineURL( |
| 104 content::BrowserContext* browser_context, | 162 content::BrowserContext* browser_context, |
| 105 const GURL& online_url) { | 163 const GURL& online_url) { |
| 106 const OfflinePageItem* offline_page = | 164 const OfflinePageItem* offline_page = |
| (...skipping 11 matching lines...) Expand all Loading... |
| 118 if (!offline_page) | 176 if (!offline_page) |
| 119 return; | 177 return; |
| 120 | 178 |
| 121 OfflinePageModel* offline_page_model = | 179 OfflinePageModel* offline_page_model = |
| 122 OfflinePageModelFactory::GetForBrowserContext(browser_context); | 180 OfflinePageModelFactory::GetForBrowserContext(browser_context); |
| 123 DCHECK(offline_page_model); | 181 DCHECK(offline_page_model); |
| 124 offline_page_model->MarkPageAccessed(offline_page->offline_id); | 182 offline_page_model->MarkPageAccessed(offline_page->offline_id); |
| 125 } | 183 } |
| 126 | 184 |
| 127 } // namespace offline_pages | 185 } // namespace offline_pages |
| OLD | NEW |