Index: chrome/browser/android/offline_pages/offline_page_utils.cc |
diff --git a/chrome/browser/android/offline_pages/offline_page_utils.cc b/chrome/browser/android/offline_pages/offline_page_utils.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..06d4f013f694b343751f249ed2ccb106ef54401e |
--- /dev/null |
+++ b/chrome/browser/android/offline_pages/offline_page_utils.cc |
@@ -0,0 +1,136 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/android/offline_pages/offline_page_utils.h" |
+ |
+#include "base/strings/string_piece.h" |
+#include "base/strings/string_util.h" |
+#include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" |
+#include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
+#include "components/offline_pages/offline_page_feature.h" |
+#include "components/offline_pages/offline_page_item.h" |
+#include "components/offline_pages/offline_page_model.h" |
+#include "content/public/browser/browser_context.h" |
+#include "url/gurl.h" |
+ |
+namespace offline_pages { |
+namespace android { |
+ |
+namespace { |
+ |
+// Returns an offline page related to the |online_url\, if one exists in the |
+// |browser_context|. |
+const offline_pages::OfflinePageItem* GetOfflinePageByOnlineURL( |
+ content::BrowserContext* browser_context, |
+ const GURL& online_url) { |
+ DCHECK(browser_context); |
+ |
+ if (!offline_pages::IsOfflinePagesEnabled()) |
+ return nullptr; |
+ |
+ offline_pages::OfflinePageModel* offline_page_model = |
+ offline_pages::OfflinePageModelFactory::GetForBrowserContext( |
+ browser_context); |
+ if (!offline_page_model) |
+ return nullptr; |
+ |
+ return offline_page_model->GetPageByOnlineURL(online_url); |
+} |
+ |
+// Returns an offline page related to the |offline_url\, if one exists in the |
+// |browser_context|. |
+const offline_pages::OfflinePageItem* GetOfflinePageByOfflineURL( |
+ content::BrowserContext* browser_context, |
+ const GURL& offline_url) { |
+ DCHECK(browser_context); |
+ |
+ if (!offline_pages::IsOfflinePagesEnabled()) |
+ return nullptr; |
+ |
+ // Note that we first check if the url likely points to an offline page |
+ // before calling GetPageByOfflineURL in order to avoid unnecessary lookup |
+ // cost. |
+ if (!MightBeOfflineURL(offline_url)) |
+ return nullptr; |
+ |
+ offline_pages::OfflinePageModel* offline_page_model = |
+ offline_pages::OfflinePageModelFactory::GetForBrowserContext( |
+ browser_context); |
+ |
+ // TODO: bail out if model is null |
+ return offline_page_model->GetPageByOfflineURL(offline_url); |
+} |
+ |
+} // namespace |
+ |
+bool MightBeOfflineURL(const GURL& url) { |
+ // It has to be a file URL ending with .mhtml extension. |
+ return url.is_valid() && url.SchemeIsFile() && |
+ base::EndsWith(url.spec(), |
+ OfflinePageMHTMLArchiver::GetFileNameExtension(), |
+ base::CompareCase::INSENSITIVE_ASCII); |
+} |
+ |
+GURL GetOfflineURLByOnlineURL(content::BrowserContext* browser_context, |
+ const GURL& online_url) { |
+ const offline_pages::OfflinePageItem* offline_page = |
+ GetOfflinePageByOnlineURL(browser_context, online_url); |
+ if (!offline_page) |
+ return GURL::EmptyGURL(); |
+ |
+ return offline_page->GetOfflineURL(); |
+} |
+ |
+GURL GetOnlineURLByOfflineURL(content::BrowserContext* browser_context, |
+ const GURL& offline_url) { |
+ DCHECK(browser_context); |
+ |
+ const offline_pages::OfflinePageItem* offline_page = |
+ GetOfflinePageByOfflineURL(browser_context, offline_url); |
+ if (!offline_page) |
+ return GURL::EmptyGURL(); |
+ |
+ return offline_page->url; |
+} |
+ |
+int64 GetBookmarkIdByOfflineURL(content::BrowserContext* browser_context, |
+ const GURL& offline_url) { |
+ DCHECK(browser_context); |
+ |
+ const offline_pages::OfflinePageItem* offline_page = |
+ GetOfflinePageByOfflineURL(browser_context, offline_url); |
+ if (!offline_page) |
+ return -1; |
+ |
+ return offline_page->bookmark_id; |
+} |
+ |
+bool IsOfflinePage(content::BrowserContext* browser_context, |
+ const GURL& offline_url) { |
+ DCHECK(browser_context); |
+ |
+ return GetOfflinePageByOfflineURL(browser_context, offline_url) != nullptr; |
+} |
+ |
+bool HasOfflinePageForOnlineURL(content::BrowserContext* browser_context, |
+ const GURL& online_url) { |
+ const offline_pages::OfflinePageItem* offline_page = |
+ GetOfflinePageByOnlineURL(browser_context, online_url); |
+ return offline_page && !offline_page->file_path.empty(); |
+} |
+ |
+const bool HasOfflinePages(content::BrowserContext* browser_context) { |
+ DCHECK(browser_context); |
+ |
+ if (!offline_pages::IsOfflinePagesEnabled()) |
+ return false; |
+ |
+ offline_pages::OfflinePageModel* offline_page_model = |
+ offline_pages::OfflinePageModelFactory::GetForBrowserContext( |
+ browser_context); |
+ return !offline_page_model->GetAllPages().empty(); |
+} |
+ |
+} // namespace android |
+} // namespace offline_pages |