OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #ifndef CHROME_BROWSER_INSTALLABLE_INSTALLABLE_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_INSTALLABLE_INSTALLABLE_MANAGER_H_ |
6 #define CHROME_BROWSER_INSTALLABLE_INSTALLABLE_MANAGER_H_ | 6 #define CHROME_BROWSER_INSTALLABLE_INSTALLABLE_MANAGER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <memory> | 9 #include <memory> |
10 #include <tuple> | 10 #include <tuple> |
11 #include <utility> | 11 #include <utility> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/callback_forward.h" | 14 #include "base/callback_forward.h" |
15 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
18 #include "chrome/browser/installable/installable_logging.h" | 18 #include "chrome/browser/installable/installable_logging.h" |
19 #include "chrome/browser/installable/installable_metrics.h" | 19 #include "chrome/browser/installable/installable_metrics.h" |
20 #include "content/public/browser/service_worker_context.h" | 20 #include "content/public/browser/service_worker_context.h" |
21 #include "content/public/browser/service_worker_context_observer.h" | |
21 #include "content/public/browser/web_contents_observer.h" | 22 #include "content/public/browser/web_contents_observer.h" |
22 #include "content/public/browser/web_contents_user_data.h" | 23 #include "content/public/browser/web_contents_user_data.h" |
23 #include "content/public/common/manifest.h" | 24 #include "content/public/common/manifest.h" |
24 #include "third_party/skia/include/core/SkBitmap.h" | 25 #include "third_party/skia/include/core/SkBitmap.h" |
25 #include "url/gurl.h" | 26 #include "url/gurl.h" |
26 | 27 |
27 // This struct specifies the work to be done by the InstallableManager. | 28 // This struct specifies the work to be done by the InstallableManager. |
28 // Data is cached and fetched in the order specified in this struct. A web app | 29 // Data is cached and fetched in the order specified in this struct. A web app |
29 // manifest will always be fetched first. | 30 // manifest will always be fetched first. |
30 struct InstallableParams { | 31 struct InstallableParams { |
(...skipping 11 matching lines...) Expand all Loading... | |
42 | 43 |
43 // The minimum badge icon size to fetch. Used only if | 44 // The minimum badge icon size to fetch. Used only if |
44 // |fetch_valid_badge_icon| is true. | 45 // |fetch_valid_badge_icon| is true. |
45 int minimum_badge_icon_size_in_px = -1; | 46 int minimum_badge_icon_size_in_px = -1; |
46 | 47 |
47 // Check whether the site is installable. That is, it has a manifest valid for | 48 // Check whether the site is installable. That is, it has a manifest valid for |
48 // a web app and a service worker controlling the manifest start URL and the | 49 // a web app and a service worker controlling the manifest start URL and the |
49 // current URL. | 50 // current URL. |
50 bool check_installable = false; | 51 bool check_installable = false; |
51 | 52 |
53 // Whether or not to wait indefinitely for a service worker. | |
54 bool wait_for_worker = true; | |
pkotwicz
2017/06/14 15:34:36
Drive by: This should be false for add_to_homescre
dominickn
2017/06/15 04:16:07
Nope, it should not be false for add_to_homescreen
pkotwicz
2017/06/15 15:10:04
2. Doesn't your change cause the Web Manifest to b
dominickn
2017/06/15 23:45:38
The fix for this is to make add to homescreen do w
pkotwicz
2017/06/16 23:07:43
We will need to fetch the icons in the first call
dominickn
2017/06/17 00:00:26
No, we'll ignore icons if the web page has a manif
pkotwicz
2017/06/19 18:46:50
We need to decide whether to launch a page in full
dominickn
2017/06/20 02:26:24
As discussed offline, this shouldn't be an issue b
| |
55 | |
52 // Check whether there is a fetchable, non-empty icon in the manifest | 56 // Check whether there is a fetchable, non-empty icon in the manifest |
53 // conforming to the primary icon size parameters. | 57 // conforming to the primary icon size parameters. |
54 bool fetch_valid_primary_icon = false; | 58 bool fetch_valid_primary_icon = false; |
55 | 59 |
56 // Check whether there is a fetchable, non-empty icon in the manifest | 60 // Check whether there is a fetchable, non-empty icon in the manifest |
57 // conforming to the badge icon size parameters. | 61 // conforming to the badge icon size parameters. |
58 bool fetch_valid_badge_icon = false; | 62 bool fetch_valid_badge_icon = false; |
59 }; | 63 }; |
60 | 64 |
61 // This struct is passed to an InstallableCallback when the InstallableManager | 65 // This struct is passed to an InstallableCallback when the InstallableManager |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
97 // app manifest. If check_installable was true and the site isn't installable, | 101 // app manifest. If check_installable was true and the site isn't installable, |
98 // the reason will be in error_code. | 102 // the reason will be in error_code. |
99 const bool is_installable; | 103 const bool is_installable; |
100 }; | 104 }; |
101 | 105 |
102 using InstallableCallback = base::Callback<void(const InstallableData&)>; | 106 using InstallableCallback = base::Callback<void(const InstallableData&)>; |
103 | 107 |
104 // This class is responsible for fetching the resources required to check and | 108 // This class is responsible for fetching the resources required to check and |
105 // install a site. | 109 // install a site. |
106 class InstallableManager | 110 class InstallableManager |
107 : public content::WebContentsObserver, | 111 : public content::ServiceWorkerContextObserver, |
112 public content::WebContentsObserver, | |
108 public content::WebContentsUserData<InstallableManager> { | 113 public content::WebContentsUserData<InstallableManager> { |
109 public: | 114 public: |
110 explicit InstallableManager(content::WebContents* web_contents); | 115 explicit InstallableManager(content::WebContents* web_contents); |
111 ~InstallableManager() override; | 116 ~InstallableManager() override; |
112 | 117 |
113 // Returns true if the overall security state of |web_contents| is sufficient | 118 // Returns true if the overall security state of |web_contents| is sufficient |
114 // to be considered installable. | 119 // to be considered installable. |
115 static bool IsContentSecure(content::WebContents* web_contents); | 120 static bool IsContentSecure(content::WebContents* web_contents); |
116 | 121 |
117 // Returns the minimum icon size in pixels for a site to be installable. | 122 // Returns the minimum icon size in pixels for a site to be installable. |
118 // TODO(dominickn): consolidate this concept with minimum_icon_size_in_px | 123 // TODO(dominickn): consolidate this concept with minimum_icon_size_in_px |
119 // across all platforms. | 124 // across all platforms. |
120 static int GetMinimumIconSizeInPx(); | 125 static int GetMinimumIconSizeInPx(); |
121 | 126 |
122 // Get the installable data, fetching the resources specified in |params|. | 127 // Get the installable data, fetching the resources specified in |params|. |
123 // |callback| is invoked synchronously (i.e. no via PostTask on the UI thread | 128 // |callback| is invoked synchronously (i.e. no via PostTask on the UI thread |
124 // when the data is ready; the synchronous execution ensures that the | 129 // when the data is ready; the synchronous execution ensures that the |
125 // references |callback| receives in its InstallableData argument are valid. | 130 // references |callback| receives in its InstallableData argument are valid. |
126 // | 131 // |
132 // Clients must be prepared for |callback| to not ever be invoked. For | |
133 // instance, if installability checking is requested, this method will wait | |
134 // until the site registers a service worker (and hence not invoke |callback| | |
135 // at all if a service worker is never registered). | |
136 // | |
127 // Calls requesting data that is already fetched will return the cached data. | 137 // Calls requesting data that is already fetched will return the cached data. |
128 // This method is marked virtual so clients may mock this object in tests. | 138 void GetData(const InstallableParams& params, |
129 virtual void GetData(const InstallableParams& params, | 139 const InstallableCallback& callback); |
130 const InstallableCallback& callback); | |
131 | 140 |
132 // Called via AppBannerManagerAndroid to record metrics on how often the | 141 // Called via AppBannerManagerAndroid to record metrics on how often the |
133 // installable check is completed when the menu or add to homescreen menu item | 142 // installable check is completed when the menu or add to homescreen menu item |
134 // is opened on Android. | 143 // is opened on Android. |
135 void RecordMenuOpenHistogram(); | 144 void RecordMenuOpenHistogram(); |
136 void RecordMenuItemAddToHomescreenHistogram(); | 145 void RecordMenuItemAddToHomescreenHistogram(); |
137 void RecordQueuedMetricsOnTaskCompletion(const InstallableParams& params, | 146 void RecordQueuedMetricsOnTaskCompletion(const InstallableParams& params, |
138 bool check_passed); | 147 bool check_passed); |
139 | 148 |
140 private: | 149 private: |
141 friend class InstallableManagerBrowserTest; | 150 friend class InstallableManagerBrowserTest; |
142 friend class InstallableManagerUnitTest; | 151 friend class InstallableManagerUnitTest; |
143 FRIEND_TEST_ALL_PREFIXES(InstallableManagerBrowserTest, | 152 FRIEND_TEST_ALL_PREFIXES(InstallableManagerBrowserTest, |
144 ManagerBeginsInEmptyState); | 153 ManagerBeginsInEmptyState); |
145 FRIEND_TEST_ALL_PREFIXES(InstallableManagerBrowserTest, CheckWebapp); | 154 FRIEND_TEST_ALL_PREFIXES(InstallableManagerBrowserTest, CheckWebapp); |
155 FRIEND_TEST_ALL_PREFIXES(InstallableManagerBrowserTest, | |
156 CheckLazyServiceWorker); | |
146 | 157 |
147 using Task = std::pair<InstallableParams, InstallableCallback>; | 158 using Task = std::pair<InstallableParams, InstallableCallback>; |
148 using IconParams = std::tuple<int, int, content::Manifest::Icon::IconPurpose>; | 159 using IconParams = std::tuple<int, int, content::Manifest::Icon::IconPurpose>; |
149 | 160 |
150 struct ManifestProperty; | 161 struct ManifestProperty; |
151 struct InstallableProperty; | 162 struct ValidManifestProperty; |
163 struct ServiceWorkerProperty; | |
152 struct IconProperty; | 164 struct IconProperty; |
153 | 165 |
154 // Returns an IconParams object that queries for a primary icon conforming to | 166 // Returns an IconParams object that queries for a primary icon conforming to |
155 // the primary icon size parameters in |params|. | 167 // the primary icon size parameters in |params|. |
156 IconParams ParamsForPrimaryIcon(const InstallableParams& params) const; | 168 IconParams ParamsForPrimaryIcon(const InstallableParams& params) const; |
157 // Returns an IconParams object that queries for a badge icon conforming to | 169 // Returns an IconParams object that queries for a badge icon conforming to |
158 // the badge icon size parameters in |params|. | 170 // the badge icon size parameters in |params|. |
159 IconParams ParamsForBadgeIcon(const InstallableParams& params) const; | 171 IconParams ParamsForBadgeIcon(const InstallableParams& params) const; |
160 | 172 |
161 // Returns true if |params| matches any fetched icon, or false if no icon has | 173 // Returns true if |params| matches any fetched icon, or false if no icon has |
162 // been requested yet or there is no match. | 174 // been requested yet or there is no match. |
163 bool IsIconFetched(const IconParams& params) const; | 175 bool IsIconFetched(const IconParams& params) const; |
164 | 176 |
165 // Sets the icon matching |params| as fetched. | 177 // Sets the icon matching |params| as fetched. |
166 void SetIconFetched(const IconParams& params); | 178 void SetIconFetched(const IconParams& params); |
167 | 179 |
168 // Returns the error code associated with the resources requested in |params|, | 180 // Returns the error code associated with the resources requested in |params|, |
169 // or NO_ERROR_DETECTED if there is no error. | 181 // or NO_ERROR_DETECTED if there is no error. |
170 InstallableStatusCode GetErrorCode(const InstallableParams& params); | 182 InstallableStatusCode GetErrorCode(const InstallableParams& params); |
171 | 183 |
172 // Gets/sets parts of particular properties. Exposed for testing. | 184 // Gets/sets parts of particular properties. Exposed for testing. |
173 InstallableStatusCode manifest_error() const; | 185 InstallableStatusCode manifest_error() const; |
174 InstallableStatusCode installable_error() const; | 186 InstallableStatusCode valid_manifest_error() const; |
175 void set_installable_error(InstallableStatusCode error_code); | 187 void set_valid_manifest_error(InstallableStatusCode error_code); |
188 InstallableStatusCode worker_error() const; | |
176 InstallableStatusCode icon_error(const IconParams& icon_params); | 189 InstallableStatusCode icon_error(const IconParams& icon_params); |
177 GURL& icon_url(const IconParams& icon_params); | 190 GURL& icon_url(const IconParams& icon_params); |
178 const SkBitmap* icon(const IconParams& icon); | 191 const SkBitmap* icon(const IconParams& icon); |
179 | 192 |
180 // Returns the WebContents to which this object is attached, or nullptr if the | 193 // Returns the WebContents to which this object is attached, or nullptr if the |
181 // WebContents doesn't exist or is currently being destroyed. | 194 // WebContents doesn't exist or is currently being destroyed. |
182 content::WebContents* GetWebContents(); | 195 content::WebContents* GetWebContents(); |
183 | 196 |
184 // Returns true if |params| requires no more work to be done. | 197 // Returns true if |params| requires no more work to be done. |
185 bool IsComplete(const InstallableParams& params) const; | 198 bool IsComplete(const InstallableParams& params) const; |
(...skipping 16 matching lines...) Expand all Loading... | |
202 void FetchManifest(); | 215 void FetchManifest(); |
203 void OnDidGetManifest(const GURL& manifest_url, | 216 void OnDidGetManifest(const GURL& manifest_url, |
204 const content::Manifest& manifest); | 217 const content::Manifest& manifest); |
205 | 218 |
206 void CheckInstallable(); | 219 void CheckInstallable(); |
207 bool IsManifestValidForWebApp(const content::Manifest& manifest); | 220 bool IsManifestValidForWebApp(const content::Manifest& manifest); |
208 void CheckServiceWorker(); | 221 void CheckServiceWorker(); |
209 void OnDidCheckHasServiceWorker(content::ServiceWorkerCapability capability); | 222 void OnDidCheckHasServiceWorker(content::ServiceWorkerCapability capability); |
210 | 223 |
211 void CheckAndFetchBestIcon(const IconParams& params); | 224 void CheckAndFetchBestIcon(const IconParams& params); |
212 void OnIconFetched( | 225 void OnIconFetched(const GURL icon_url, |
213 const GURL icon_url, const IconParams& params, const SkBitmap& bitmap); | 226 const IconParams& params, |
227 const SkBitmap& bitmap); | |
228 | |
229 // content::ServiceWorkerContextObserver overrides | |
230 void OnRegistrationStored(const GURL& pattern) override; | |
214 | 231 |
215 // content::WebContentsObserver overrides | 232 // content::WebContentsObserver overrides |
216 void DidFinishNavigation(content::NavigationHandle* handle) override; | 233 void DidFinishNavigation(content::NavigationHandle* handle) override; |
217 void WebContentsDestroyed() override; | 234 void WebContentsDestroyed() override; |
218 | 235 |
219 const GURL& manifest_url() const; | 236 const GURL& manifest_url() const; |
220 const content::Manifest& manifest() const; | 237 const content::Manifest& manifest() const; |
221 bool is_installable() const; | 238 bool is_installable() const; |
222 | 239 |
223 // The list of <params, callback> pairs that have come from a call to GetData. | 240 // The list of <params, callback> pairs that have come from a call to GetData. |
224 std::vector<Task> tasks_; | 241 std::vector<Task> tasks_; |
225 | 242 |
226 // Installable properties cached on this object. | 243 // Installable properties cached on this object. |
227 std::unique_ptr<ManifestProperty> manifest_; | 244 std::unique_ptr<ManifestProperty> manifest_; |
228 std::unique_ptr<InstallableProperty> installable_; | 245 std::unique_ptr<ValidManifestProperty> valid_manifest_; |
246 std::unique_ptr<ServiceWorkerProperty> worker_; | |
229 std::map<IconParams, IconProperty> icons_; | 247 std::map<IconParams, IconProperty> icons_; |
230 | 248 |
249 // Owned by the storage partition attached to the content::WebContents which | |
250 // this object is scoped to. | |
251 content::ServiceWorkerContext* service_worker_context_; | |
252 | |
231 // Whether or not the current page is a PWA. This is reset per navigation and | 253 // Whether or not the current page is a PWA. This is reset per navigation and |
232 // is independent of the caching mechanism, i.e. if a PWA check is run | 254 // is independent of the caching mechanism, i.e. if a PWA check is run |
233 // multiple times for one page, this will be set on the first check. | 255 // multiple times for one page, this will be set on the first check. |
234 InstallabilityCheckStatus page_status_; | 256 InstallabilityCheckStatus page_status_; |
235 | 257 |
236 // Counts for the number of queued requests of the menu and add to homescreen | 258 // Counts for the number of queued requests of the menu and add to homescreen |
237 // menu item there have been whilst the installable check is awaiting | 259 // menu item there have been whilst the installable check is awaiting |
238 // completion. Used for metrics recording. | 260 // completion. Used for metrics recording. |
239 int menu_open_count_; | 261 int menu_open_count_; |
240 int menu_item_add_to_homescreen_count_; | 262 int menu_item_add_to_homescreen_count_; |
241 | 263 |
242 bool is_active_; | 264 bool is_active_; |
243 bool is_pwa_check_complete_; | 265 bool is_pwa_check_complete_; |
244 | 266 |
245 base::WeakPtrFactory<InstallableManager> weak_factory_; | 267 base::WeakPtrFactory<InstallableManager> weak_factory_; |
246 | 268 |
247 DISALLOW_COPY_AND_ASSIGN(InstallableManager); | 269 DISALLOW_COPY_AND_ASSIGN(InstallableManager); |
248 }; | 270 }; |
249 | 271 |
250 #endif // CHROME_BROWSER_INSTALLABLE_INSTALLABLE_MANAGER_H_ | 272 #endif // CHROME_BROWSER_INSTALLABLE_INSTALLABLE_MANAGER_H_ |
OLD | NEW |