OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_INSTALLABLE_INSTALLABLE_CHECKER_H_ | |
6 #define CHROME_BROWSER_INSTALLABLE_INSTALLABLE_CHECKER_H_ | |
7 | |
8 #include <utility> | |
9 | |
10 #include "base/callback_forward.h" | |
11 #include "base/gtest_prod_util.h" | |
12 #include "base/macros.h" | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "chrome/browser/installable/installable_logging.h" | |
15 #include "content/public/browser/web_contents_observer.h" | |
16 #include "content/public/browser/web_contents_user_data.h" | |
17 #include "content/public/common/manifest.h" | |
18 #include "third_party/skia/include/core/SkBitmap.h" | |
19 #include "url/gurl.h" | |
20 | |
21 // This struct specifies the work to be done by the InstallableChecker. | |
22 // Results are cached and fetched in the order specified in this struct. A web | |
23 // app manifest will always be fetched first. | |
24 struct InstallableParams { | |
25 // The ideal icon size to fetch. Used only if |check_valid_icon| is true. | |
26 int ideal_icon_size_in_dp = -1; | |
27 | |
28 // The minimum icon size to fetch. Used only if |check_valid_icon| is true. | |
29 int minimum_icon_size_in_dp = -1; | |
30 | |
31 // Check whether the site's manifest specifies a web app. i.e. it: | |
32 // - is non-empty | |
33 // - has a valid start URL | |
34 // - has a non-null name OR a non-null short_name | |
35 // - has a display field set to "standalone" or "fullscreen" | |
36 // - contains a PNG icon of at least 144x144px | |
37 bool check_valid_webapp_manifest = false; | |
38 | |
39 // Check for a service worker controlling the site and the manifest start URL. | |
40 bool check_service_worker = false; | |
41 | |
42 // Check whether there is an icon in the manifest conforming to the icon size | |
43 // parameters, and that the icon can be fetched and isn't an empty bitmap. | |
44 bool check_valid_icon = false; | |
benwells
2016/07/26 03:28:46
Does this also fetch the icon? From the name alone
dominickn
2016/07/28 00:36:28
Renamed to fetch.
| |
45 }; | |
46 | |
47 // This struct is passed to an InstallableCallback when the InstallableChecker | |
48 // has finished with a task. Each reference is owned by InstallableChecker, and | |
49 // callers should copy any objects which they wish to use later. | |
50 // Non-requested fields will be set to null, empty, or false. | |
51 struct InstallableResult { | |
52 // NO_ERROR_DETECTED if there were no issues. | |
53 const InstallableErrorCode error_code; | |
54 | |
55 // Empty if the site has no <link rel="manifest"> tag. | |
56 const GURL& manifest_url; | |
57 | |
58 // Empty if the site has an unparseable manifest. | |
59 const content::Manifest& manifest; | |
60 | |
61 // Empty if no icon was requested. | |
62 const GURL& icon_url; | |
63 | |
64 // nullptr if the most appropriate icon couldn't be determined or downloaded. | |
65 const SkBitmap* icon; | |
66 | |
67 const bool has_valid_webapp_manifest; | |
68 | |
69 const bool has_service_worker; | |
70 }; | |
71 | |
72 using InstallableCallback = base::Callback<void(const InstallableResult&)>; | |
73 | |
74 // This class is responsible for fetching the resources required to check and | |
75 // install a site. A call to Start() will always fetch a site's web app manifest | |
76 // if it exists. Other resources may be requested in the InstallableParams | |
77 // passed to Start. Results are provided in a struct sent to an | |
78 // InstallableCallback; previously fetched resources are cached for efficiency. | |
79 class InstallableChecker | |
benwells
2016/07/26 03:28:46
This name still doesn't seem great, as it is doing
dominickn
2016/07/28 00:36:27
On the name of the checker... I settled on checker
benwells
2016/07/28 08:10:58
While I don't like calling things Manager, I think
| |
80 : public content::WebContentsObserver, | |
81 public content::WebContentsUserData<InstallableChecker> { | |
82 public: | |
83 explicit InstallableChecker(content::WebContents* web_contents); | |
84 ~InstallableChecker() override; | |
85 | |
86 // Returns true if the InstallableChecker is currently running. | |
87 bool IsActive() const { return HasFlag(STARTED); } | |
benwells
2016/07/26 07:27:14
I don't think this function is worth the indirecti
dominickn
2016/07/28 00:36:28
It's gone.
| |
88 | |
89 // Returns true if |manifest| meets the requirements for being installable. | |
90 // Sets |valid_webapp_manifest_error_| if any errors are encountered. | |
91 bool IsManifestValidForWebApp(const content::Manifest& manifest); | |
92 | |
93 // Returns the minimum icon size in pixels for a site to be installable. | |
94 // TODO(dominickn): consolidate this concept with minimum_icon_size_in_dp | |
95 // across all platforms. | |
96 static int GetMinimumIconSizeInPx(); | |
97 | |
98 // Start the installable check, fetching the resources specified in |params|. | |
99 // |callback| is invoked on the UI thread when the checks are complete, | |
100 // passing an InstallableResult struct containing the requested resources. | |
101 // | |
102 // Separate calls to Start are processed serially; each call is guaranteed to | |
103 // invoke |callback| before the next one is processed. Resources are cached on | |
104 // this object: a previous call for the same page and with the same icon sizes | |
105 // will not perform duplicate work and will run the callback immediately with | |
106 // the cached resources. |callback| should defer rexpensive tasks to later as | |
benwells
2016/07/26 03:28:46
s/rexpensive/expensive
dominickn
2016/07/28 00:36:27
Done.
| |
107 // it will be directly called. | |
benwells
2016/07/26 03:28:45
Why is important not to do expensive work?
dominickn
2016/07/28 00:36:27
It will delay any other work that the checker is d
| |
108 // This method is marked virtual so clients may mock this object in tests. | |
109 virtual void Start(const InstallableParams& params, | |
110 const InstallableCallback& callback); | |
111 | |
112 private: | |
113 friend class InstallableCheckerUnitTest; | |
114 FRIEND_TEST_ALL_PREFIXES(InstallableCheckerBrowserTest, | |
115 CheckerBeginsInEmptyState); | |
116 FRIEND_TEST_ALL_PREFIXES(InstallableCheckerBrowserTest, | |
117 CheckWebapp); | |
118 | |
119 using Task = std::pair<InstallableParams, InstallableCallback>; | |
120 | |
121 // Bit flags used to maintain internal state. | |
122 enum StatusFlag : uint32_t { | |
benwells
2016/07/26 03:28:45
I'm guessing DORMANT and STARTED are mutually excl
dominickn
2016/07/28 00:36:28
DORMANT is meant to be the empty state. Strictly s
| |
123 DORMANT = 0, | |
124 STARTED = 1 << 0, | |
125 RUNNING_CALLBACKS = 1 << 1, | |
126 MANIFEST_FETCHED = 1 << 2, | |
127 MANIFEST_VALIDATED = 1 << 3, | |
128 ICON_FETCHED = 1 << 4, | |
129 SERVICE_WORKER_CHECKED = 1 << 5, | |
130 }; | |
131 | |
132 // Stops the checking pipeline for the current task. Does not cancel any other | |
133 // tasks which may be queued. | |
134 void Cancel(); | |
135 | |
136 // Returns true if the icon size in |params| matches the current request. | |
benwells
2016/07/26 03:28:46
What is the current request in this context? Does
dominickn
2016/07/28 00:36:27
Clarified.
| |
137 bool DoesIconSizeMatch(const InstallableParams& params) const; | |
138 | |
139 // Returns the error code associated with the resources requested in |params|, | |
140 // or NO_ERROR_DETECTED if there is no error. | |
141 InstallableErrorCode GetErrorCode(const InstallableParams& params); | |
142 | |
143 // Returns the WebContents to which this object is attached, or nullptr if the | |
144 // WebContents doesn't exist or is currently being destroyed. | |
145 content::WebContents* GetWebContents(); | |
146 | |
147 // Returns true if |params| requires no more work to be done. | |
148 bool IsComplete(const InstallableParams& params) const; | |
149 | |
150 // Returns true if |web_contents| is non-null and this checker remains active. | |
151 // If false is returned, |processing_error_| will be set. | |
152 bool IsRunning(content::WebContents* web_contents); | |
benwells
2016/07/26 03:28:46
This seems weird. Why would looking if something i
dominickn
2016/07/28 00:36:27
processing_error is gone. I've renamed the method
| |
153 | |
154 // Resets members to empty and removes all queued tasks. | |
155 // Called when navigating to a new page. | |
156 void Reset(); | |
157 | |
158 // Runs the callback associated with any task that is now complete. | |
159 void RunCallbacks(); | |
160 | |
161 // Runs the callbacks for any completed tasks, and starts work on the next. | |
162 void StartTask(); | |
163 | |
164 // Sets |flag| bit in |status_| to 0 and 1 respectively. | |
165 void ClearFlag(StatusFlag flag) { status_ &= ~flag; } | |
166 void SetFlag(StatusFlag flag) { status_ |= flag; } | |
167 | |
168 // Returns true if the current status has the |flag| bit set to 1. | |
169 bool HasFlag(StatusFlag flag) const { return (status_ & flag) != 0; } | |
170 | |
171 // Fetches the next required resource. | |
172 void FetchResource(); | |
173 | |
174 // Data retrieval methods. | |
175 void FetchManifest(); | |
176 void OnDidGetManifest(const GURL& manifest_url, | |
177 const content::Manifest& manifest); | |
178 | |
179 void CheckValidWebappManifest(); | |
180 | |
181 void CheckServiceWorker(); | |
182 void OnDidCheckHasServiceWorker(bool has_service_worker); | |
183 | |
184 void ExtractAndFetchBestIcon(); | |
185 void OnAppIconFetched(const GURL icon_url, const SkBitmap& bitmap); | |
186 | |
187 // content::WebContentsObserver overrides | |
188 void DidFinishNavigation(content::NavigationHandle* handle) override; | |
189 void WebContentsDestroyed() override; | |
190 | |
191 // Internal status field to keep track of what resources have been retrieved. | |
192 uint32_t status_; | |
193 | |
194 InstallableErrorCode processing_error_; | |
195 InstallableErrorCode manifest_error_; | |
196 InstallableErrorCode valid_webapp_manifest_error_; | |
197 InstallableErrorCode service_worker_error_; | |
198 InstallableErrorCode icon_error_; | |
199 | |
200 // The list of params + callback pairs that have come from a call to Start. | |
201 std::vector<Task> tasks_; | |
202 | |
203 // Tasks received while we are working. These are held separately to avoid | |
204 // contaminating the tasks_ vector (e.g. if a new Task is received during | |
205 // callback invocation in RunCallbacks). | |
206 std::vector<Task> pending_tasks_; | |
207 | |
208 // The icon size parameters used if a valid icon was requested. | |
209 int ideal_icon_size_in_dp_; | |
210 int minimum_icon_size_in_dp_; | |
211 | |
212 // Installable properties cached on this object. | |
213 GURL manifest_url_; | |
214 content::Manifest manifest_; | |
215 GURL icon_url_; | |
216 std::unique_ptr<SkBitmap> icon_; | |
217 bool has_valid_webapp_manifest_; | |
218 bool has_service_worker_; | |
219 | |
220 // This object has a lifetime scoped to a WebContents, so WeakPtrs can be used | |
221 // for binding callbacks. We use a factory so we can invalidate pointers. | |
benwells
2016/07/26 03:28:46
I don't understand the first sentence of this comm
dominickn
2016/07/28 00:36:28
Removed.
| |
222 base::WeakPtrFactory<InstallableChecker> weak_factory_; | |
223 | |
224 DISALLOW_COPY_AND_ASSIGN(InstallableChecker); | |
225 }; | |
226 | |
227 #endif // CHROME_BROWSER_INSTALLABLE_INSTALLABLE_CHECKER_H_ | |
OLD | NEW |