| 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/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "base/strings/string_piece.h" | 8 #include "base/strings/string_piece.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" | 10 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" |
| 11 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | 11 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| 12 #include "components/offline_pages/offline_page_feature.h" | 12 #include "components/offline_pages/offline_page_feature.h" |
| 13 #include "components/offline_pages/offline_page_item.h" | 13 #include "components/offline_pages/offline_page_item.h" |
| 14 #include "components/offline_pages/offline_page_model.h" | 14 #include "components/offline_pages/offline_page_model.h" |
| 15 #include "content/public/browser/browser_context.h" | 15 #include "content/public/browser/browser_context.h" |
| 16 #include "url/gurl.h" | 16 #include "url/gurl.h" |
| 17 | 17 |
| 18 namespace offline_pages { | 18 namespace offline_pages { |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 // Returns an offline page originated from the |online_url|. | 21 // Returns an offline page originated from the |online_url|. |
| 22 const offline_pages::OfflinePageItem* GetOfflinePageForOnlineURL( | 22 const OfflinePageItem* GetOfflinePageForOnlineURL( |
| 23 content::BrowserContext* browser_context, | 23 content::BrowserContext* browser_context, |
| 24 const GURL& online_url) { | 24 const GURL& online_url) { |
| 25 DCHECK(browser_context); | 25 DCHECK(browser_context); |
| 26 | 26 |
| 27 if (!offline_pages::IsOfflinePagesEnabled()) | 27 if (!IsOfflinePagesEnabled()) |
| 28 return nullptr; | 28 return nullptr; |
| 29 | 29 |
| 30 offline_pages::OfflinePageModel* offline_page_model = | 30 OfflinePageModel* offline_page_model = |
| 31 offline_pages::OfflinePageModelFactory::GetForBrowserContext( | 31 OfflinePageModelFactory::GetForBrowserContext(browser_context); |
| 32 browser_context); | |
| 33 if (!offline_page_model) | 32 if (!offline_page_model) |
| 34 return nullptr; | 33 return nullptr; |
| 35 | 34 |
| 36 return offline_page_model->MaybeGetPageByOnlineURL(online_url); | 35 return offline_page_model->MaybeGetPageByOnlineURL(online_url); |
| 37 } | 36 } |
| 38 | 37 |
| 39 // Returns an offline page that is stored as the |offline_url|. | 38 // Returns an offline page that is stored as the |offline_url|. |
| 40 const offline_pages::OfflinePageItem* GetOfflinePageForOfflineURL( | 39 const OfflinePageItem* GetOfflinePageForOfflineURL( |
| 41 content::BrowserContext* browser_context, | 40 content::BrowserContext* browser_context, |
| 42 const GURL& offline_url) { | 41 const GURL& offline_url) { |
| 43 DCHECK(browser_context); | 42 DCHECK(browser_context); |
| 44 | 43 |
| 45 if (!offline_pages::IsOfflinePagesEnabled()) | 44 if (!IsOfflinePagesEnabled()) |
| 46 return nullptr; | 45 return nullptr; |
| 47 | 46 |
| 48 // Note that we first check if the url likely points to an offline page | 47 // Note that we first check if the url likely points to an offline page |
| 49 // before calling GetPageByOfflineURL in order to avoid unnecessary lookup | 48 // before calling GetPageByOfflineURL in order to avoid unnecessary lookup |
| 50 // cost. | 49 // cost. |
| 51 if (!OfflinePageUtils::MightBeOfflineURL(offline_url)) | 50 if (!OfflinePageUtils::MightBeOfflineURL(offline_url)) |
| 52 return nullptr; | 51 return nullptr; |
| 53 | 52 |
| 54 offline_pages::OfflinePageModel* offline_page_model = | 53 OfflinePageModel* offline_page_model = |
| 55 offline_pages::OfflinePageModelFactory::GetForBrowserContext( | 54 OfflinePageModelFactory::GetForBrowserContext(browser_context); |
| 56 browser_context); | |
| 57 if (!offline_page_model) | 55 if (!offline_page_model) |
| 58 return nullptr; | 56 return nullptr; |
| 59 | 57 |
| 60 return offline_page_model->MaybeGetPageByOfflineURL(offline_url); | 58 return offline_page_model->MaybeGetPageByOfflineURL(offline_url); |
| 61 } | 59 } |
| 62 | 60 |
| 63 } // namespace | 61 } // namespace |
| 64 | 62 |
| 65 // static | 63 // static |
| 66 bool OfflinePageUtils::MightBeOfflineURL(const GURL& url) { | 64 bool OfflinePageUtils::MightBeOfflineURL(const GURL& url) { |
| 67 // It has to be a file URL ending with .mhtml extension. | 65 // It has to be a file URL ending with .mhtml extension. |
| 68 return url.is_valid() && url.SchemeIsFile() && | 66 return url.is_valid() && url.SchemeIsFile() && |
| 69 base::EndsWith(url.spec(), | 67 base::EndsWith(url.spec(), |
| 70 OfflinePageMHTMLArchiver::GetFileNameExtension(), | 68 OfflinePageMHTMLArchiver::GetFileNameExtension(), |
| 71 base::CompareCase::INSENSITIVE_ASCII); | 69 base::CompareCase::INSENSITIVE_ASCII); |
| 72 } | 70 } |
| 73 | 71 |
| 74 // static | 72 // static |
| 75 GURL OfflinePageUtils::GetOfflineURLForOnlineURL( | 73 GURL OfflinePageUtils::GetOfflineURLForOnlineURL( |
| 76 content::BrowserContext* browser_context, | 74 content::BrowserContext* browser_context, |
| 77 const GURL& online_url) { | 75 const GURL& online_url) { |
| 78 const offline_pages::OfflinePageItem* offline_page = | 76 const OfflinePageItem* offline_page = |
| 79 GetOfflinePageForOnlineURL(browser_context, online_url); | 77 GetOfflinePageForOnlineURL(browser_context, online_url); |
| 80 if (!offline_page) | 78 if (!offline_page) |
| 81 return GURL(); | 79 return GURL(); |
| 82 | 80 |
| 83 return offline_page->GetOfflineURL(); | 81 return offline_page->GetOfflineURL(); |
| 84 } | 82 } |
| 85 | 83 |
| 86 // static | 84 // static |
| 87 GURL OfflinePageUtils::GetOnlineURLForOfflineURL( | 85 GURL OfflinePageUtils::GetOnlineURLForOfflineURL( |
| 88 content::BrowserContext* browser_context, | 86 content::BrowserContext* browser_context, |
| 89 const GURL& offline_url) { | 87 const GURL& offline_url) { |
| 90 const offline_pages::OfflinePageItem* offline_page = | 88 const OfflinePageItem* offline_page = |
| 91 GetOfflinePageForOfflineURL(browser_context, offline_url); | 89 GetOfflinePageForOfflineURL(browser_context, offline_url); |
| 92 if (!offline_page) | 90 if (!offline_page) |
| 93 return GURL(); | 91 return GURL(); |
| 94 | 92 |
| 95 return offline_page->url; | 93 return offline_page->url; |
| 96 } | 94 } |
| 97 | 95 |
| 98 // static | 96 // static |
| 99 int64_t OfflinePageUtils::GetBookmarkIdForOfflineURL( | 97 int64_t OfflinePageUtils::GetBookmarkIdForOfflineURL( |
| 100 content::BrowserContext* browser_context, | 98 content::BrowserContext* browser_context, |
| 101 const GURL& offline_url) { | 99 const GURL& offline_url) { |
| 102 const offline_pages::OfflinePageItem* offline_page = | 100 const OfflinePageItem* offline_page = |
| 103 GetOfflinePageForOfflineURL(browser_context, offline_url); | 101 GetOfflinePageForOfflineURL(browser_context, offline_url); |
| 104 if (!offline_page) | 102 if (!offline_page) |
| 105 return -1; | 103 return -1; |
| 106 | 104 |
| 107 if (offline_page->client_id.name_space != offline_pages::kBookmarkNamespace) { | 105 if (offline_page->client_id.name_space != offline_pages::kBookmarkNamespace) { |
| 108 return -1; | 106 return -1; |
| 109 } | 107 } |
| 110 | 108 |
| 111 int64_t result; | 109 int64_t result; |
| 112 if (base::StringToInt64(offline_page->client_id.id, &result)) { | 110 if (base::StringToInt64(offline_page->client_id.id, &result)) { |
| 113 return result; | 111 return result; |
| 114 } | 112 } |
| 115 return -1; | 113 return -1; |
| 116 } | 114 } |
| 117 | 115 |
| 118 // static | 116 // static |
| 119 bool OfflinePageUtils::IsOfflinePage(content::BrowserContext* browser_context, | 117 bool OfflinePageUtils::IsOfflinePage(content::BrowserContext* browser_context, |
| 120 const GURL& offline_url) { | 118 const GURL& offline_url) { |
| 121 return GetOfflinePageForOfflineURL(browser_context, offline_url) != nullptr; | 119 return GetOfflinePageForOfflineURL(browser_context, offline_url) != nullptr; |
| 122 } | 120 } |
| 123 | 121 |
| 124 // static | 122 // static |
| 125 bool OfflinePageUtils::HasOfflinePageForOnlineURL( | 123 bool OfflinePageUtils::HasOfflinePageForOnlineURL( |
| 126 content::BrowserContext* browser_context, | 124 content::BrowserContext* browser_context, |
| 127 const GURL& online_url) { | 125 const GURL& online_url) { |
| 128 const offline_pages::OfflinePageItem* offline_page = | 126 const OfflinePageItem* offline_page = |
| 129 GetOfflinePageForOnlineURL(browser_context, online_url); | 127 GetOfflinePageForOnlineURL(browser_context, online_url); |
| 130 return offline_page && !offline_page->file_path.empty(); | 128 return offline_page && !offline_page->file_path.empty(); |
| 131 } | 129 } |
| 132 | 130 |
| 131 // static |
| 132 void OfflinePageUtils::MarkPageAccessed( |
| 133 content::BrowserContext* browser_context, const GURL& offline_url) { |
| 134 DCHECK(browser_context); |
| 135 |
| 136 const OfflinePageItem* offline_page = |
| 137 GetOfflinePageForOfflineURL(browser_context, offline_url); |
| 138 if (!offline_page) |
| 139 return; |
| 140 |
| 141 OfflinePageModel* offline_page_model = |
| 142 OfflinePageModelFactory::GetForBrowserContext(browser_context); |
| 143 DCHECK(offline_page_model); |
| 144 offline_page_model->MarkPageAccessed(offline_page->offline_id); |
| 145 } |
| 146 |
| 133 } // namespace offline_pages | 147 } // namespace offline_pages |
| OLD | NEW |