OLD | NEW |
---|---|
(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 android { | |
19 | |
20 namespace { | |
21 | |
22 // Returns an offline page related to the |online_url\, if one exists in the | |
jianli
2015/12/15 01:45:24
\ => |
also, suggest removing if part since it cau
fgorski
2015/12/15 16:33:01
Done.
| |
23 // |browser_context|. | |
24 const offline_pages::OfflinePageItem* GetOfflinePageByOnlineURL( | |
25 content::BrowserContext* browser_context, | |
26 const GURL& online_url) { | |
27 DCHECK(browser_context); | |
28 | |
29 if (!offline_pages::IsOfflinePagesEnabled()) | |
30 return nullptr; | |
31 | |
32 offline_pages::OfflinePageModel* offline_page_model = | |
33 offline_pages::OfflinePageModelFactory::GetForBrowserContext( | |
34 browser_context); | |
35 if (!offline_page_model) | |
36 return nullptr; | |
37 | |
38 return offline_page_model->GetPageByOnlineURL(online_url); | |
39 } | |
40 | |
41 // Returns an offline page related to the |offline_url\, if one exists in the | |
jianli
2015/12/15 01:45:24
\ => |
also, suggest removing if part since it cau
fgorski
2015/12/15 16:33:01
Done.
| |
42 // |browser_context|. | |
43 const offline_pages::OfflinePageItem* GetOfflinePageByOfflineURL( | |
44 content::BrowserContext* browser_context, | |
45 const GURL& offline_url) { | |
46 DCHECK(browser_context); | |
47 | |
48 if (!offline_pages::IsOfflinePagesEnabled()) | |
49 return nullptr; | |
50 | |
51 // Note that we first check if the url likely points to an offline page | |
52 // before calling GetPageByOfflineURL in order to avoid unnecessary lookup | |
53 // cost. | |
54 if (!MightBeOfflineURL(offline_url)) | |
55 return nullptr; | |
56 | |
57 offline_pages::OfflinePageModel* offline_page_model = | |
58 offline_pages::OfflinePageModelFactory::GetForBrowserContext( | |
59 browser_context); | |
60 | |
61 // TODO: bail out if model is null | |
jianli
2015/12/15 01:45:24
nit: either add DCHECK or do the bailout now.
fgorski
2015/12/15 16:33:01
Done.
| |
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::EmptyGURL(); | |
jianli
2015/12/15 01:45:24
We should return GURL() since here we don't return
fgorski
2015/12/15 16:33:02
Done.
| |
81 | |
82 return offline_page->GetOfflineURL(); | |
83 } | |
84 | |
85 GURL GetOnlineURLByOfflineURL(content::BrowserContext* browser_context, | |
86 const GURL& offline_url) { | |
87 DCHECK(browser_context); | |
88 | |
89 const offline_pages::OfflinePageItem* offline_page = | |
90 GetOfflinePageByOfflineURL(browser_context, offline_url); | |
91 if (!offline_page) | |
92 return GURL::EmptyGURL(); | |
jianli
2015/12/15 01:45:24
ditto
fgorski
2015/12/15 16:33:01
Done.
| |
93 | |
94 return offline_page->url; | |
95 } | |
96 | |
97 int64 GetBookmarkIdByOfflineURL(content::BrowserContext* browser_context, | |
98 const GURL& offline_url) { | |
99 DCHECK(browser_context); | |
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); | |
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 |