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 { |
31 // The ideal primary icon size to fetch. Used only if | 32 // The ideal primary icon size to fetch. Used only if |
32 // |fetch_valid_primary_icon| is true. | 33 // |fetch_valid_primary_icon| is true. |
33 int ideal_primary_icon_size_in_px = -1; | 34 int ideal_primary_icon_size_in_px = -1; |
34 | 35 |
35 // The minimum primary icon size to fetch. Used only if | 36 // The minimum primary icon size to fetch. Used only if |
36 // |fetch_valid_primary_icon| is true. | 37 // |fetch_valid_primary_icon| is true. |
37 int minimum_primary_icon_size_in_px = -1; | 38 int minimum_primary_icon_size_in_px = -1; |
38 | 39 |
39 // The ideal badge icon size to fetch. Used only if | 40 // The ideal badge icon size to fetch. Used only if |
40 // |fetch_valid_badge_icon| is true. | 41 // |fetch_valid_badge_icon| is true. |
41 int ideal_badge_icon_size_in_px = -1; | 42 int ideal_badge_icon_size_in_px = -1; |
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 // a web app and a service worker controlling the manifest start URL and the | |
49 // current URL. | |
50 bool check_installable = false; | |
51 | |
52 // Check whether there is a fetchable, non-empty icon in the manifest | 48 // Check whether there is a fetchable, non-empty icon in the manifest |
53 // conforming to the primary icon size parameters. | 49 // conforming to the primary icon size parameters. |
54 bool fetch_valid_primary_icon = false; | 50 bool fetch_valid_primary_icon = false; |
55 | 51 |
56 // Check whether there is a fetchable, non-empty icon in the manifest | 52 // Check whether there is a fetchable, non-empty icon in the manifest |
57 // conforming to the badge icon size parameters. | 53 // conforming to the badge icon size parameters. |
58 bool fetch_valid_badge_icon = false; | 54 bool fetch_valid_badge_icon = false; |
| 55 |
| 56 // Check whether the site is installable. That is, it has a manifest valid for |
| 57 // a web app and a service worker controlling the manifest start URL and the |
| 58 // current URL. |
| 59 bool check_installable = false; |
| 60 |
| 61 // Whether or not to wait indefinitely for a service worker. If this is set to |
| 62 // false, the worker status will not be cached and will be re-checked if |
| 63 // GetData() is called again for the current page. |
| 64 bool wait_for_worker = true; |
59 }; | 65 }; |
60 | 66 |
61 // This struct is passed to an InstallableCallback when the InstallableManager | 67 // This struct is passed to an InstallableCallback when the InstallableManager |
62 // has finished working. Each reference is owned by InstallableManager, and | 68 // has finished working. Each reference is owned by InstallableManager, and |
63 // callers should copy any objects which they wish to use later. Non-requested | 69 // callers should copy any objects which they wish to use later. Non-requested |
64 // fields will be set to null, empty, or false. | 70 // fields will be set to null, empty, or false. |
65 struct InstallableData { | 71 struct InstallableData { |
66 // NO_ERROR_DETECTED if there were no issues. | 72 // NO_ERROR_DETECTED if there were no issues. |
67 const InstallableStatusCode error_code; | 73 const InstallableStatusCode error_code; |
68 | 74 |
(...skipping 28 matching lines...) Expand all Loading... |
97 // app manifest. If check_installable was true and the site isn't installable, | 103 // app manifest. If check_installable was true and the site isn't installable, |
98 // the reason will be in error_code. | 104 // the reason will be in error_code. |
99 const bool is_installable; | 105 const bool is_installable; |
100 }; | 106 }; |
101 | 107 |
102 using InstallableCallback = base::Callback<void(const InstallableData&)>; | 108 using InstallableCallback = base::Callback<void(const InstallableData&)>; |
103 | 109 |
104 // This class is responsible for fetching the resources required to check and | 110 // This class is responsible for fetching the resources required to check and |
105 // install a site. | 111 // install a site. |
106 class InstallableManager | 112 class InstallableManager |
107 : public content::WebContentsObserver, | 113 : public content::ServiceWorkerContextObserver, |
| 114 public content::WebContentsObserver, |
108 public content::WebContentsUserData<InstallableManager> { | 115 public content::WebContentsUserData<InstallableManager> { |
109 public: | 116 public: |
110 explicit InstallableManager(content::WebContents* web_contents); | 117 explicit InstallableManager(content::WebContents* web_contents); |
111 ~InstallableManager() override; | 118 ~InstallableManager() override; |
112 | 119 |
113 // Returns true if the overall security state of |web_contents| is sufficient | 120 // Returns true if the overall security state of |web_contents| is sufficient |
114 // to be considered installable. | 121 // to be considered installable. |
115 static bool IsContentSecure(content::WebContents* web_contents); | 122 static bool IsContentSecure(content::WebContents* web_contents); |
116 | 123 |
117 // Returns the minimum icon size in pixels for a site to be installable. | 124 // 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 | 125 // TODO(dominickn): consolidate this concept with minimum_icon_size_in_px |
119 // across all platforms. | 126 // across all platforms. |
120 static int GetMinimumIconSizeInPx(); | 127 static int GetMinimumIconSizeInPx(); |
121 | 128 |
122 // Get the installable data, fetching the resources specified in |params|. | 129 // Get the installable data, fetching the resources specified in |params|. |
123 // |callback| is invoked synchronously (i.e. no via PostTask on the UI thread | 130 // |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 | 131 // when the data is ready; the synchronous execution ensures that the |
125 // references |callback| receives in its InstallableData argument are valid. | 132 // references |callback| receives in its InstallableData argument are valid. |
126 // | 133 // |
| 134 // Clients must be prepared for |callback| to not ever be invoked. For |
| 135 // instance, if installability checking is requested, this method will wait |
| 136 // until the site registers a service worker (and hence not invoke |callback| |
| 137 // at all if a service worker is never registered). |
| 138 // |
127 // Calls requesting data that is already fetched will return the cached data. | 139 // 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. | 140 void GetData(const InstallableParams& params, |
129 virtual void GetData(const InstallableParams& params, | 141 const InstallableCallback& callback); |
130 const InstallableCallback& callback); | |
131 | 142 |
132 // Called via AppBannerManagerAndroid to record metrics on how often the | 143 // Called via AppBannerManagerAndroid to record metrics on how often the |
133 // installable check is completed when the menu or add to homescreen menu item | 144 // installable check is completed when the menu or add to homescreen menu item |
134 // is opened on Android. | 145 // is opened on Android. |
135 void RecordMenuOpenHistogram(); | 146 void RecordMenuOpenHistogram(); |
136 void RecordMenuItemAddToHomescreenHistogram(); | 147 void RecordMenuItemAddToHomescreenHistogram(); |
137 void RecordQueuedMetricsOnTaskCompletion(const InstallableParams& params, | 148 void RecordQueuedMetricsOnTaskCompletion(const InstallableParams& params, |
138 bool check_passed); | 149 bool check_passed); |
139 | 150 |
| 151 protected: |
| 152 // For mocking in tests. |
| 153 virtual void OnWaitingForServiceWorker() {} |
| 154 |
140 private: | 155 private: |
141 friend class InstallableManagerBrowserTest; | 156 friend class InstallableManagerBrowserTest; |
142 friend class InstallableManagerUnitTest; | 157 friend class InstallableManagerUnitTest; |
143 FRIEND_TEST_ALL_PREFIXES(InstallableManagerBrowserTest, | 158 FRIEND_TEST_ALL_PREFIXES(InstallableManagerBrowserTest, |
144 ManagerBeginsInEmptyState); | 159 ManagerBeginsInEmptyState); |
145 FRIEND_TEST_ALL_PREFIXES(InstallableManagerBrowserTest, CheckWebapp); | 160 FRIEND_TEST_ALL_PREFIXES(InstallableManagerBrowserTest, CheckWebapp); |
| 161 FRIEND_TEST_ALL_PREFIXES(InstallableManagerBrowserTest, |
| 162 CheckLazyServiceWorkerPassesWhenWaiting); |
| 163 FRIEND_TEST_ALL_PREFIXES(InstallableManagerBrowserTest, |
| 164 CheckLazyServiceWorkerNoFetchHandlerFails); |
146 | 165 |
147 using Task = std::pair<InstallableParams, InstallableCallback>; | 166 using Task = std::pair<InstallableParams, InstallableCallback>; |
148 using IconParams = std::tuple<int, int, content::Manifest::Icon::IconPurpose>; | 167 using IconParams = std::tuple<int, int, content::Manifest::Icon::IconPurpose>; |
149 | 168 |
150 struct ManifestProperty; | 169 struct ManifestProperty; |
151 struct InstallableProperty; | 170 struct ValidManifestProperty; |
| 171 struct ServiceWorkerProperty; |
152 struct IconProperty; | 172 struct IconProperty; |
153 | 173 |
154 // Returns an IconParams object that queries for a primary icon conforming to | 174 // Returns an IconParams object that queries for a primary icon conforming to |
155 // the primary icon size parameters in |params|. | 175 // the primary icon size parameters in |params|. |
156 IconParams ParamsForPrimaryIcon(const InstallableParams& params) const; | 176 IconParams ParamsForPrimaryIcon(const InstallableParams& params) const; |
157 // Returns an IconParams object that queries for a badge icon conforming to | 177 // Returns an IconParams object that queries for a badge icon conforming to |
158 // the badge icon size parameters in |params|. | 178 // the badge icon size parameters in |params|. |
159 IconParams ParamsForBadgeIcon(const InstallableParams& params) const; | 179 IconParams ParamsForBadgeIcon(const InstallableParams& params) const; |
160 | 180 |
161 // Returns true if |params| matches any fetched icon, or false if no icon has | 181 // Returns true if |params| matches any fetched icon, or false if no icon has |
162 // been requested yet or there is no match. | 182 // been requested yet or there is no match. |
163 bool IsIconFetched(const IconParams& params) const; | 183 bool IsIconFetched(const IconParams& params) const; |
164 | 184 |
165 // Sets the icon matching |params| as fetched. | 185 // Sets the icon matching |params| as fetched. |
166 void SetIconFetched(const IconParams& params); | 186 void SetIconFetched(const IconParams& params); |
167 | 187 |
168 // Returns the error code associated with the resources requested in |params|, | 188 // Returns the error code associated with the resources requested in |params|, |
169 // or NO_ERROR_DETECTED if there is no error. | 189 // or NO_ERROR_DETECTED if there is no error. |
170 InstallableStatusCode GetErrorCode(const InstallableParams& params); | 190 InstallableStatusCode GetErrorCode(const InstallableParams& params); |
171 | 191 |
172 // Gets/sets parts of particular properties. Exposed for testing. | 192 // Gets/sets parts of particular properties. Exposed for testing. |
173 InstallableStatusCode manifest_error() const; | 193 InstallableStatusCode manifest_error() const; |
174 InstallableStatusCode installable_error() const; | 194 InstallableStatusCode valid_manifest_error() const; |
175 void set_installable_error(InstallableStatusCode error_code); | 195 void set_valid_manifest_error(InstallableStatusCode error_code); |
| 196 InstallableStatusCode worker_error() const; |
| 197 bool worker_waiting() const; |
176 InstallableStatusCode icon_error(const IconParams& icon_params); | 198 InstallableStatusCode icon_error(const IconParams& icon_params); |
177 GURL& icon_url(const IconParams& icon_params); | 199 GURL& icon_url(const IconParams& icon_params); |
178 const SkBitmap* icon(const IconParams& icon); | 200 const SkBitmap* icon(const IconParams& icon); |
179 | 201 |
180 // Returns the WebContents to which this object is attached, or nullptr if the | 202 // Returns the WebContents to which this object is attached, or nullptr if the |
181 // WebContents doesn't exist or is currently being destroyed. | 203 // WebContents doesn't exist or is currently being destroyed. |
182 content::WebContents* GetWebContents(); | 204 content::WebContents* GetWebContents(); |
183 | 205 |
184 // Returns true if |params| requires no more work to be done. | 206 // Returns true if |params| requires no more work to be done. |
185 bool IsComplete(const InstallableParams& params) const; | 207 bool IsComplete(const InstallableParams& params) const; |
(...skipping 16 matching lines...) Expand all Loading... |
202 void FetchManifest(); | 224 void FetchManifest(); |
203 void OnDidGetManifest(const GURL& manifest_url, | 225 void OnDidGetManifest(const GURL& manifest_url, |
204 const content::Manifest& manifest); | 226 const content::Manifest& manifest); |
205 | 227 |
206 void CheckInstallable(); | 228 void CheckInstallable(); |
207 bool IsManifestValidForWebApp(const content::Manifest& manifest); | 229 bool IsManifestValidForWebApp(const content::Manifest& manifest); |
208 void CheckServiceWorker(); | 230 void CheckServiceWorker(); |
209 void OnDidCheckHasServiceWorker(content::ServiceWorkerCapability capability); | 231 void OnDidCheckHasServiceWorker(content::ServiceWorkerCapability capability); |
210 | 232 |
211 void CheckAndFetchBestIcon(const IconParams& params); | 233 void CheckAndFetchBestIcon(const IconParams& params); |
212 void OnIconFetched( | 234 void OnIconFetched(const GURL icon_url, |
213 const GURL icon_url, const IconParams& params, const SkBitmap& bitmap); | 235 const IconParams& params, |
| 236 const SkBitmap& bitmap); |
| 237 |
| 238 // content::ServiceWorkerContextObserver overrides |
| 239 void OnRegistrationStored(const GURL& pattern) override; |
214 | 240 |
215 // content::WebContentsObserver overrides | 241 // content::WebContentsObserver overrides |
216 void DidFinishNavigation(content::NavigationHandle* handle) override; | 242 void DidFinishNavigation(content::NavigationHandle* handle) override; |
217 void WebContentsDestroyed() override; | 243 void WebContentsDestroyed() override; |
218 | 244 |
219 const GURL& manifest_url() const; | 245 const GURL& manifest_url() const; |
220 const content::Manifest& manifest() const; | 246 const content::Manifest& manifest() const; |
221 bool is_installable() const; | 247 bool is_installable() const; |
222 | 248 |
223 // The list of <params, callback> pairs that have come from a call to GetData. | 249 // The list of <params, callback> pairs that have come from a call to GetData. |
224 std::vector<Task> tasks_; | 250 std::vector<Task> tasks_; |
225 | 251 |
226 // Installable properties cached on this object. | 252 // Installable properties cached on this object. |
227 std::unique_ptr<ManifestProperty> manifest_; | 253 std::unique_ptr<ManifestProperty> manifest_; |
228 std::unique_ptr<InstallableProperty> installable_; | 254 std::unique_ptr<ValidManifestProperty> valid_manifest_; |
| 255 std::unique_ptr<ServiceWorkerProperty> worker_; |
229 std::map<IconParams, IconProperty> icons_; | 256 std::map<IconParams, IconProperty> icons_; |
230 | 257 |
| 258 // Owned by the storage partition attached to the content::WebContents which |
| 259 // this object is scoped to. |
| 260 content::ServiceWorkerContext* service_worker_context_; |
| 261 |
231 // Whether or not the current page is a PWA. This is reset per navigation and | 262 // 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 | 263 // 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. | 264 // multiple times for one page, this will be set on the first check. |
234 InstallabilityCheckStatus page_status_; | 265 InstallabilityCheckStatus page_status_; |
235 | 266 |
236 // Counts for the number of queued requests of the menu and add to homescreen | 267 // 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 | 268 // menu item there have been whilst the installable check is awaiting |
238 // completion. Used for metrics recording. | 269 // completion. Used for metrics recording. |
239 int menu_open_count_; | 270 int menu_open_count_; |
240 int menu_item_add_to_homescreen_count_; | 271 int menu_item_add_to_homescreen_count_; |
241 | 272 |
242 bool is_active_; | 273 bool is_active_; |
243 bool is_pwa_check_complete_; | 274 bool is_pwa_check_complete_; |
244 | 275 |
245 base::WeakPtrFactory<InstallableManager> weak_factory_; | 276 base::WeakPtrFactory<InstallableManager> weak_factory_; |
246 | 277 |
247 DISALLOW_COPY_AND_ASSIGN(InstallableManager); | 278 DISALLOW_COPY_AND_ASSIGN(InstallableManager); |
248 }; | 279 }; |
249 | 280 |
250 #endif // CHROME_BROWSER_INSTALLABLE_INSTALLABLE_MANAGER_H_ | 281 #endif // CHROME_BROWSER_INSTALLABLE_INSTALLABLE_MANAGER_H_ |
OLD | NEW |