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/16 23:07:43
There aren't any callers which set this to false.
dominickn
2017/06/17 00:00:26
This is set to false in app_banner_manager.cc when
pkotwicz
2017/06/19 18:46:51
Sorry for the confusion. I had this test in partic
dominickn
2017/06/20 02:26:24
That makes sense now. Added the proposed test and
| |
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 |
149 protected: | |
150 // For mocking in tests. | |
151 virtual void OnWaitingForServiceWorker() {} | |
152 | |
140 private: | 153 private: |
141 friend class InstallableManagerBrowserTest; | 154 friend class InstallableManagerBrowserTest; |
142 friend class InstallableManagerUnitTest; | 155 friend class InstallableManagerUnitTest; |
143 FRIEND_TEST_ALL_PREFIXES(InstallableManagerBrowserTest, | 156 FRIEND_TEST_ALL_PREFIXES(InstallableManagerBrowserTest, |
144 ManagerBeginsInEmptyState); | 157 ManagerBeginsInEmptyState); |
145 FRIEND_TEST_ALL_PREFIXES(InstallableManagerBrowserTest, CheckWebapp); | 158 FRIEND_TEST_ALL_PREFIXES(InstallableManagerBrowserTest, CheckWebapp); |
159 FRIEND_TEST_ALL_PREFIXES(InstallableManagerBrowserTest, | |
160 CheckLazyServiceWorkerPassesWhenWaiting); | |
161 FRIEND_TEST_ALL_PREFIXES(InstallableManagerBrowserTest, | |
162 CheckLazyServiceWorkerNoFetchHandlerFails); | |
146 | 163 |
147 using Task = std::pair<InstallableParams, InstallableCallback>; | 164 using Task = std::pair<InstallableParams, InstallableCallback>; |
148 using IconParams = std::tuple<int, int, content::Manifest::Icon::IconPurpose>; | 165 using IconParams = std::tuple<int, int, content::Manifest::Icon::IconPurpose>; |
149 | 166 |
150 struct ManifestProperty; | 167 struct ManifestProperty; |
151 struct InstallableProperty; | 168 struct ValidManifestProperty; |
169 struct ServiceWorkerProperty; | |
152 struct IconProperty; | 170 struct IconProperty; |
153 | 171 |
154 // Returns an IconParams object that queries for a primary icon conforming to | 172 // Returns an IconParams object that queries for a primary icon conforming to |
155 // the primary icon size parameters in |params|. | 173 // the primary icon size parameters in |params|. |
156 IconParams ParamsForPrimaryIcon(const InstallableParams& params) const; | 174 IconParams ParamsForPrimaryIcon(const InstallableParams& params) const; |
157 // Returns an IconParams object that queries for a badge icon conforming to | 175 // Returns an IconParams object that queries for a badge icon conforming to |
158 // the badge icon size parameters in |params|. | 176 // the badge icon size parameters in |params|. |
159 IconParams ParamsForBadgeIcon(const InstallableParams& params) const; | 177 IconParams ParamsForBadgeIcon(const InstallableParams& params) const; |
160 | 178 |
161 // Returns true if |params| matches any fetched icon, or false if no icon has | 179 // Returns true if |params| matches any fetched icon, or false if no icon has |
162 // been requested yet or there is no match. | 180 // been requested yet or there is no match. |
163 bool IsIconFetched(const IconParams& params) const; | 181 bool IsIconFetched(const IconParams& params) const; |
164 | 182 |
165 // Sets the icon matching |params| as fetched. | 183 // Sets the icon matching |params| as fetched. |
166 void SetIconFetched(const IconParams& params); | 184 void SetIconFetched(const IconParams& params); |
167 | 185 |
168 // Returns the error code associated with the resources requested in |params|, | 186 // Returns the error code associated with the resources requested in |params|, |
169 // or NO_ERROR_DETECTED if there is no error. | 187 // or NO_ERROR_DETECTED if there is no error. |
170 InstallableStatusCode GetErrorCode(const InstallableParams& params); | 188 InstallableStatusCode GetErrorCode(const InstallableParams& params); |
171 | 189 |
172 // Gets/sets parts of particular properties. Exposed for testing. | 190 // Gets/sets parts of particular properties. Exposed for testing. |
173 InstallableStatusCode manifest_error() const; | 191 InstallableStatusCode manifest_error() const; |
174 InstallableStatusCode installable_error() const; | 192 InstallableStatusCode valid_manifest_error() const; |
175 void set_installable_error(InstallableStatusCode error_code); | 193 void set_valid_manifest_error(InstallableStatusCode error_code); |
194 InstallableStatusCode worker_error() const; | |
195 bool worker_waiting() const; | |
176 InstallableStatusCode icon_error(const IconParams& icon_params); | 196 InstallableStatusCode icon_error(const IconParams& icon_params); |
177 GURL& icon_url(const IconParams& icon_params); | 197 GURL& icon_url(const IconParams& icon_params); |
178 const SkBitmap* icon(const IconParams& icon); | 198 const SkBitmap* icon(const IconParams& icon); |
179 | 199 |
180 // Returns the WebContents to which this object is attached, or nullptr if the | 200 // Returns the WebContents to which this object is attached, or nullptr if the |
181 // WebContents doesn't exist or is currently being destroyed. | 201 // WebContents doesn't exist or is currently being destroyed. |
182 content::WebContents* GetWebContents(); | 202 content::WebContents* GetWebContents(); |
183 | 203 |
184 // Returns true if |params| requires no more work to be done. | 204 // Returns true if |params| requires no more work to be done. |
185 bool IsComplete(const InstallableParams& params) const; | 205 bool IsComplete(const InstallableParams& params) const; |
(...skipping 16 matching lines...) Expand all Loading... | |
202 void FetchManifest(); | 222 void FetchManifest(); |
203 void OnDidGetManifest(const GURL& manifest_url, | 223 void OnDidGetManifest(const GURL& manifest_url, |
204 const content::Manifest& manifest); | 224 const content::Manifest& manifest); |
205 | 225 |
206 void CheckInstallable(); | 226 void CheckInstallable(); |
207 bool IsManifestValidForWebApp(const content::Manifest& manifest); | 227 bool IsManifestValidForWebApp(const content::Manifest& manifest); |
208 void CheckServiceWorker(); | 228 void CheckServiceWorker(); |
209 void OnDidCheckHasServiceWorker(content::ServiceWorkerCapability capability); | 229 void OnDidCheckHasServiceWorker(content::ServiceWorkerCapability capability); |
210 | 230 |
211 void CheckAndFetchBestIcon(const IconParams& params); | 231 void CheckAndFetchBestIcon(const IconParams& params); |
212 void OnIconFetched( | 232 void OnIconFetched(const GURL icon_url, |
213 const GURL icon_url, const IconParams& params, const SkBitmap& bitmap); | 233 const IconParams& params, |
234 const SkBitmap& bitmap); | |
235 | |
236 // content::ServiceWorkerContextObserver overrides | |
237 void OnRegistrationStored(const GURL& pattern) override; | |
214 | 238 |
215 // content::WebContentsObserver overrides | 239 // content::WebContentsObserver overrides |
216 void DidFinishNavigation(content::NavigationHandle* handle) override; | 240 void DidFinishNavigation(content::NavigationHandle* handle) override; |
217 void WebContentsDestroyed() override; | 241 void WebContentsDestroyed() override; |
218 | 242 |
219 const GURL& manifest_url() const; | 243 const GURL& manifest_url() const; |
220 const content::Manifest& manifest() const; | 244 const content::Manifest& manifest() const; |
221 bool is_installable() const; | 245 bool is_installable() const; |
222 | 246 |
223 // The list of <params, callback> pairs that have come from a call to GetData. | 247 // The list of <params, callback> pairs that have come from a call to GetData. |
224 std::vector<Task> tasks_; | 248 std::vector<Task> tasks_; |
225 | 249 |
226 // Installable properties cached on this object. | 250 // Installable properties cached on this object. |
227 std::unique_ptr<ManifestProperty> manifest_; | 251 std::unique_ptr<ManifestProperty> manifest_; |
228 std::unique_ptr<InstallableProperty> installable_; | 252 std::unique_ptr<ValidManifestProperty> valid_manifest_; |
253 std::unique_ptr<ServiceWorkerProperty> worker_; | |
229 std::map<IconParams, IconProperty> icons_; | 254 std::map<IconParams, IconProperty> icons_; |
230 | 255 |
256 // Owned by the storage partition attached to the content::WebContents which | |
257 // this object is scoped to. | |
258 content::ServiceWorkerContext* service_worker_context_; | |
259 | |
231 // Whether or not the current page is a PWA. This is reset per navigation and | 260 // 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 | 261 // 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. | 262 // multiple times for one page, this will be set on the first check. |
234 InstallabilityCheckStatus page_status_; | 263 InstallabilityCheckStatus page_status_; |
235 | 264 |
236 // Counts for the number of queued requests of the menu and add to homescreen | 265 // 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 | 266 // menu item there have been whilst the installable check is awaiting |
238 // completion. Used for metrics recording. | 267 // completion. Used for metrics recording. |
239 int menu_open_count_; | 268 int menu_open_count_; |
240 int menu_item_add_to_homescreen_count_; | 269 int menu_item_add_to_homescreen_count_; |
241 | 270 |
242 bool is_active_; | 271 bool is_active_; |
243 bool is_pwa_check_complete_; | 272 bool is_pwa_check_complete_; |
244 | 273 |
245 base::WeakPtrFactory<InstallableManager> weak_factory_; | 274 base::WeakPtrFactory<InstallableManager> weak_factory_; |
246 | 275 |
247 DISALLOW_COPY_AND_ASSIGN(InstallableManager); | 276 DISALLOW_COPY_AND_ASSIGN(InstallableManager); |
248 }; | 277 }; |
249 | 278 |
250 #endif // CHROME_BROWSER_INSTALLABLE_INSTALLABLE_MANAGER_H_ | 279 #endif // CHROME_BROWSER_INSTALLABLE_INSTALLABLE_MANAGER_H_ |
OLD | NEW |