OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
dewittj
2015/12/15 18:30:31
Could these all just be static functions in Offlin
jianli
2015/12/15 23:53:34
Since we have quite a few static methods, it would
fgorski
2015/12/16 21:43:16
Overall, yes we could have methods with similar fu
fgorski
2015/12/16 21:43:16
Done.
| |
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 android { | |
19 | |
20 namespace { | |
21 | |
22 // Returns an offline page originated from the |online_url|. | |
23 const offline_pages::OfflinePageItem* GetOfflinePageByOnlineURL( | |
24 content::BrowserContext* browser_context, | |
25 const GURL& online_url) { | |
26 DCHECK(browser_context); | |
27 | |
28 if (!offline_pages::IsOfflinePagesEnabled()) | |
29 return nullptr; | |
30 | |
31 offline_pages::OfflinePageModel* offline_page_model = | |
32 offline_pages::OfflinePageModelFactory::GetForBrowserContext( | |
33 browser_context); | |
34 if (!offline_page_model) | |
35 return nullptr; | |
36 | |
37 return offline_page_model->GetPageByOnlineURL(online_url); | |
38 } | |
39 | |
40 // Returns an offline page that is stored as the |offline_url|. | |
41 const offline_pages::OfflinePageItem* GetOfflinePageByOfflineURL( | |
42 content::BrowserContext* browser_context, | |
43 const GURL& offline_url) { | |
44 DCHECK(browser_context); | |
45 | |
46 if (!offline_pages::IsOfflinePagesEnabled()) | |
47 return nullptr; | |
48 | |
49 // Note that we first check if the url likely points to an offline page | |
50 // before calling GetPageByOfflineURL in order to avoid unnecessary lookup | |
51 // cost. | |
52 if (!MightBeOfflineURL(offline_url)) | |
53 return nullptr; | |
54 | |
55 offline_pages::OfflinePageModel* offline_page_model = | |
56 offline_pages::OfflinePageModelFactory::GetForBrowserContext( | |
57 browser_context); | |
58 | |
jianli
2015/12/15 23:53:34
nit: remove empty line
fgorski
2015/12/16 21:43:16
Done.
| |
59 if (!offline_page_model) | |
60 return nullptr; | |
61 | |
62 return offline_page_model->GetPageByOfflineURL(offline_url); | |
63 } | |
64 | |
65 } // namespace | |
66 | |
67 bool MightBeOfflineURL(const GURL& url) { | |
68 // It has to be a file URL ending with .mhtml extension. | |
69 return url.is_valid() && url.SchemeIsFile() && | |
70 base::EndsWith(url.spec(), | |
71 OfflinePageMHTMLArchiver::GetFileNameExtension(), | |
72 base::CompareCase::INSENSITIVE_ASCII); | |
73 } | |
74 | |
75 GURL GetOfflineURLByOnlineURL(content::BrowserContext* browser_context, | |
76 const GURL& online_url) { | |
77 const offline_pages::OfflinePageItem* offline_page = | |
78 GetOfflinePageByOnlineURL(browser_context, online_url); | |
79 if (!offline_page) | |
80 return GURL(); | |
81 | |
82 return offline_page->GetOfflineURL(); | |
83 } | |
84 | |
85 GURL GetOnlineURLByOfflineURL(content::BrowserContext* browser_context, | |
86 const GURL& offline_url) { | |
87 DCHECK(browser_context); | |
jianli
2015/12/15 23:53:34
nit: DCHECK is not needed since it is already in G
fgorski
2015/12/16 21:43:16
Done.
| |
88 | |
89 const offline_pages::OfflinePageItem* offline_page = | |
90 GetOfflinePageByOfflineURL(browser_context, offline_url); | |
91 if (!offline_page) | |
92 return GURL(); | |
93 | |
94 return offline_page->url; | |
95 } | |
96 | |
97 int64 GetBookmarkIdByOfflineURL(content::BrowserContext* browser_context, | |
98 const GURL& offline_url) { | |
99 DCHECK(browser_context); | |
jianli
2015/12/15 23:53:34
ditto
fgorski
2015/12/16 21:43:16
Done.
| |
100 | |
101 const offline_pages::OfflinePageItem* offline_page = | |
102 GetOfflinePageByOfflineURL(browser_context, offline_url); | |
103 if (!offline_page) | |
104 return -1; | |
105 | |
106 return offline_page->bookmark_id; | |
107 } | |
108 | |
109 bool IsOfflinePage(content::BrowserContext* browser_context, | |
110 const GURL& offline_url) { | |
111 DCHECK(browser_context); | |
jianli
2015/12/15 23:53:34
ditto
fgorski
2015/12/16 21:43:16
Done.
| |
112 | |
113 return GetOfflinePageByOfflineURL(browser_context, offline_url) != nullptr; | |
114 } | |
115 | |
116 bool HasOfflinePageForOnlineURL(content::BrowserContext* browser_context, | |
117 const GURL& online_url) { | |
118 const offline_pages::OfflinePageItem* offline_page = | |
119 GetOfflinePageByOnlineURL(browser_context, online_url); | |
120 return offline_page && !offline_page->file_path.empty(); | |
121 } | |
122 | |
123 const bool HasOfflinePages(content::BrowserContext* browser_context) { | |
124 DCHECK(browser_context); | |
125 | |
126 if (!offline_pages::IsOfflinePagesEnabled()) | |
127 return false; | |
128 | |
129 offline_pages::OfflinePageModel* offline_page_model = | |
130 offline_pages::OfflinePageModelFactory::GetForBrowserContext( | |
131 browser_context); | |
132 return !offline_page_model->GetAllPages().empty(); | |
133 } | |
134 | |
135 } // namespace android | |
136 } // namespace offline_pages | |
OLD | NEW |