Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(386)

Side by Side Diff: chrome/browser/android/offline_pages/offline_page_utils.cc

Issue 1521193002: [Offline pages] Refactor URL conversions from TabAndroid (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressing final CR feedback Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/android/offline_pages/offline_page_utils.h"
6
7 #include "base/strings/string_piece.h"
8 #include "base/strings/string_util.h"
9 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h"
10 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h"
11 #include "components/offline_pages/offline_page_feature.h"
12 #include "components/offline_pages/offline_page_item.h"
13 #include "components/offline_pages/offline_page_model.h"
14 #include "content/public/browser/browser_context.h"
15 #include "url/gurl.h"
16
17 namespace offline_pages {
18 namespace {
19
20 // Returns an offline page originated from the |online_url|.
21 const offline_pages::OfflinePageItem* GetOfflinePageForOnlineURL(
22 content::BrowserContext* browser_context,
23 const GURL& online_url) {
24 DCHECK(browser_context);
25
26 if (!offline_pages::IsOfflinePagesEnabled())
27 return nullptr;
28
29 offline_pages::OfflinePageModel* offline_page_model =
30 offline_pages::OfflinePageModelFactory::GetForBrowserContext(
31 browser_context);
32 if (!offline_page_model)
33 return nullptr;
34
35 return offline_page_model->GetPageByOnlineURL(online_url);
36 }
37
38 // Returns an offline page that is stored as the |offline_url|.
39 const offline_pages::OfflinePageItem* GetOfflinePageForOfflineURL(
40 content::BrowserContext* browser_context,
41 const GURL& offline_url) {
42 DCHECK(browser_context);
43
44 if (!offline_pages::IsOfflinePagesEnabled())
45 return nullptr;
46
47 // Note that we first check if the url likely points to an offline page
48 // before calling GetPageByOfflineURL in order to avoid unnecessary lookup
49 // cost.
50 if (!OfflinePageUtils::MightBeOfflineURL(offline_url))
51 return nullptr;
52
53 offline_pages::OfflinePageModel* offline_page_model =
54 offline_pages::OfflinePageModelFactory::GetForBrowserContext(
55 browser_context);
56 if (!offline_page_model)
57 return nullptr;
58
59 return offline_page_model->GetPageByOfflineURL(offline_url);
60 }
61
62 } // namespace
63
64 // static
65 bool OfflinePageUtils::MightBeOfflineURL(const GURL& url) {
66 // It has to be a file URL ending with .mhtml extension.
67 return url.is_valid() && url.SchemeIsFile() &&
68 base::EndsWith(url.spec(),
69 OfflinePageMHTMLArchiver::GetFileNameExtension(),
70 base::CompareCase::INSENSITIVE_ASCII);
71 }
72
73 // static
74 GURL OfflinePageUtils::GetOfflineURLForOnlineURL(
75 content::BrowserContext* browser_context,
76 const GURL& online_url) {
77 const offline_pages::OfflinePageItem* offline_page =
78 GetOfflinePageForOnlineURL(browser_context, online_url);
79 if (!offline_page)
80 return GURL();
81
82 return offline_page->GetOfflineURL();
83 }
84
85 // static
86 GURL OfflinePageUtils::GetOnlineURLForOfflineURL(
87 content::BrowserContext* browser_context,
88 const GURL& offline_url) {
89 const offline_pages::OfflinePageItem* offline_page =
90 GetOfflinePageForOfflineURL(browser_context, offline_url);
91 if (!offline_page)
92 return GURL();
93
94 return offline_page->url;
95 }
96
97 // static
98 int64 OfflinePageUtils::GetBookmarkIdForOfflineURL(
99 content::BrowserContext* browser_context,
100 const GURL& offline_url) {
101 const offline_pages::OfflinePageItem* offline_page =
102 GetOfflinePageForOfflineURL(browser_context, offline_url);
103 if (!offline_page)
104 return -1;
105
106 return offline_page->bookmark_id;
107 }
108
109 // static
110 bool OfflinePageUtils::IsOfflinePage(content::BrowserContext* browser_context,
111 const GURL& offline_url) {
112 return GetOfflinePageForOfflineURL(browser_context, offline_url) != nullptr;
113 }
114
115 // static
116 bool OfflinePageUtils::HasOfflinePageForOnlineURL(
117 content::BrowserContext* browser_context,
118 const GURL& online_url) {
119 const offline_pages::OfflinePageItem* offline_page =
120 GetOfflinePageForOnlineURL(browser_context, online_url);
121 return offline_page && !offline_page->file_path.empty();
122 }
123
124 // static
125 const bool OfflinePageUtils::HasOfflinePages(
126 content::BrowserContext* browser_context) {
127 DCHECK(browser_context);
128
129 if (!offline_pages::IsOfflinePagesEnabled())
130 return false;
131
132 offline_pages::OfflinePageModel* offline_page_model =
133 offline_pages::OfflinePageModelFactory::GetForBrowserContext(
134 browser_context);
135 return offline_page_model->HasOfflinePages();
136 }
137
138 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698