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_MANAGER_H_ | |
6 #define CHROME_BROWSER_INSTALLABLE_INSTALLABLE_MANAGER_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 "chrome/browser/installable/installable_property.h" | |
16 #include "content/public/browser/web_contents_observer.h" | |
17 #include "content/public/browser/web_contents_user_data.h" | |
18 #include "content/public/common/manifest.h" | |
19 #include "third_party/skia/include/core/SkBitmap.h" | |
20 #include "url/gurl.h" | |
21 | |
22 // This struct specifies the work to be done by the InstallableManager. | |
23 // Results are cached and fetched in the order specified in this struct. A web | |
24 // app manifest will always be fetched first. | |
25 struct InstallableParams { | |
26 // The ideal icon size to fetch. Used only if |fetch_valid_icon| is true. | |
27 int ideal_icon_size_in_dp = -1; | |
28 | |
29 // The minimum icon size to fetch. Used only if |fetch_valid_icon| is true. | |
30 int minimum_icon_size_in_dp = -1; | |
31 | |
32 // Check whether the site is installable. That is, it's manifest is valid for | |
33 // a web app, and it has a service worker controlling the manifest start URL | |
34 // and the current URL. | |
35 bool check_installable = false; | |
36 | |
37 // Check whether there is an icon in the manifest conforming to the icon size | |
38 // parameters, and that the icon can be fetched and isn't an empty bitmap. | |
39 bool fetch_valid_icon = false; | |
40 }; | |
41 | |
42 // This struct is passed to an InstallableCallback when the InstallableManager | |
43 // has finished with a task. Each reference is owned by InstallableManager, and | |
44 // callers should copy any objects which they wish to use later. | |
45 // Non-requested fields will be set to null, empty, or false. | |
46 struct InstallableData { | |
47 // NO_ERROR_DETECTED if there were no issues. | |
48 const InstallableErrorCode error_code; | |
49 | |
50 // Empty if the site has no <link rel="manifest"> tag. | |
51 const GURL& manifest_url; | |
52 | |
53 // Empty if the site has an unparseable manifest. | |
54 const content::Manifest& manifest; | |
55 | |
56 // Empty if no icon was requested. | |
57 const GURL& icon_url; | |
58 | |
59 // nullptr if the most appropriate icon couldn't be determined or downloaded. | |
benwells
2016/07/29 04:36:54
Nit: add comment about ownership. Also mention und
dominickn
2016/07/31 23:32:04
Done.
| |
60 const SkBitmap* icon; | |
61 | |
62 // true if the site has a service worker and a viable web app manifest. | |
benwells
2016/07/29 04:36:54
Nit: mention that if it is not installable the rea
dominickn
2016/07/31 23:32:04
Done.
| |
63 const bool is_installable; | |
64 }; | |
65 | |
66 using InstallableCallback = base::Callback<void(const InstallableData&)>; | |
67 | |
68 // This class is responsible for fetching the resources required to check and | |
69 // install a site. A call to GetData() will always fetch a site's web app | |
70 // manifest if it exists; optionally the full installability criteria can be | |
benwells
2016/07/29 04:36:54
Nit: you can probably remove the second sentence o
dominickn
2016/07/31 23:32:04
Done.
| |
71 // checked. | |
72 class InstallableManager | |
73 : public content::WebContentsObserver, | |
74 public content::WebContentsUserData<InstallableManager> { | |
75 public: | |
76 explicit InstallableManager(content::WebContents* web_contents); | |
77 ~InstallableManager() override; | |
78 | |
79 // Returns true if |manifest| meets the requirements for being installable. | |
80 bool IsManifestValidForWebApp(const content::Manifest& manifest); | |
81 | |
82 // Returns the minimum icon size in pixels for a site to be installable. | |
83 // TODO(dominickn): consolidate this concept with minimum_icon_size_in_dp | |
84 // across all platforms. | |
85 static int GetMinimumIconSizeInPx(); | |
86 | |
87 // Run the installable check, fetching the resources specified in |params|. | |
88 // |callback| is invoked on the UI thread when the checks are complete, | |
89 // passing an InstallableData struct containing the requested resources. | |
benwells
2016/07/29 04:36:54
s/Run the installable check/Get the installable da
dominickn
2016/07/31 23:32:04
Done.
| |
90 // | |
91 // Separate calls to GetData are processed serially; each call is guaranteed | |
92 // to invoke |callback| before the next one is processed. Resources are | |
93 // cached; a previous call for the same page and with the same icon sizes | |
94 // will not perform duplicate work and will run the callback immediately with | |
benwells
2016/07/29 04:36:54
Will the callback ever be run synchronously / befo
dominickn
2016/07/31 23:32:04
Done.
benwells
2016/08/03 01:17:28
Nit: I don't think it's clear that the callback ca
dominickn
2016/08/03 06:38:04
Done.
| |
95 // the cached resources. | |
96 // This method is marked virtual so clients may mock this object in tests. | |
97 virtual void GetData(const InstallableParams& params, | |
98 const InstallableCallback& callback); | |
99 | |
100 private: | |
101 friend class InstallableManagerUnitTest; | |
102 FRIEND_TEST_ALL_PREFIXES(InstallableManagerBrowserTest, | |
103 ManagerBeginsInEmptyState); | |
104 FRIEND_TEST_ALL_PREFIXES(InstallableManagerBrowserTest, CheckWebapp); | |
105 | |
106 using Task = std::pair<InstallableParams, InstallableCallback>; | |
107 using Tasks = std::vector<Task>; | |
108 using IconParams = std::pair<int, int>; | |
109 using IconMap = std::map<IconParams, IconProperty>; | |
benwells
2016/07/29 04:36:54
Nit: consider not having Tasks / IconMap but just
dominickn
2016/07/31 23:32:04
Done.
| |
110 | |
111 IconProperty& GetIcon(const InstallableParams& params); | |
112 | |
113 // Returns true if the icon size in |params| matches the icon size which has | |
114 // most recently been requested. false if no icon has been requested yet or | |
benwells
2016/07/29 04:36:54
s/most recently/already/
I thikn that is more cor
dominickn
2016/07/31 23:32:05
Done.
| |
115 // they don't match. | |
116 bool IsIconFetched(const InstallableParams& params) const; | |
117 | |
118 // Sets the icon parameters in |params| as being fetched. | |
119 void SetIconFetched(const InstallableParams& params); | |
120 | |
121 // Returns the error code associated with the resources requested in |params|, | |
122 // or NO_ERROR_DETECTED if there is no error. | |
123 InstallableErrorCode GetErrorCode(const InstallableParams& params); | |
124 | |
125 // Returns the WebContents to which this object is attached, or nullptr if the | |
126 // WebContents doesn't exist or is currently being destroyed. | |
127 content::WebContents* GetWebContents(); | |
128 | |
129 // Returns true if |params| requires no more work to be done. | |
130 bool IsComplete(const InstallableParams& params) const; | |
131 | |
132 // Returns true if |web_contents| is non-null and this manager remains active. | |
133 bool IsContentsValid(content::WebContents* web_contents) const; | |
134 | |
135 // Resets members to empty and removes all queued tasks. | |
136 // Called when navigating to a new page or if the WebContents has unexpectedly | |
137 // died whilst waiting for a callback. | |
138 void Reset(); | |
139 | |
140 // Sets manifest validity, service worker, and icon fetch tasks to complete. | |
141 // Called if no manifest (or an empty manifest) was fetched from the site. | |
142 void SetManifestDependentTasksComplete(); | |
143 | |
144 void StartNextTask(); | |
145 void RunCallback(const Task& task, InstallableErrorCode error); | |
146 | |
147 // Core work loop which coordinates and dispatches the work required for the | |
148 // current task. | |
149 void WorkOnTask(); | |
150 | |
151 // Data retrieval methods. | |
152 void FetchManifest(); | |
153 void OnDidGetManifest(const GURL& manifest_url, | |
154 const content::Manifest& manifest); | |
155 | |
156 void CheckInstallable(); | |
157 void CheckServiceWorker(); | |
158 void OnDidCheckHasServiceWorker(bool has_service_worker); | |
159 | |
160 void CheckAndFetchBestIcon(); | |
161 void OnAppIconFetched(const GURL icon_url, const SkBitmap& bitmap); | |
162 | |
163 // content::WebContentsObserver overrides | |
164 void DidFinishNavigation(content::NavigationHandle* handle) override; | |
165 void WebContentsDestroyed() override; | |
166 | |
167 const GURL& manifest_url() const; | |
168 const content::Manifest& manifest() const; | |
169 bool is_installable() const; | |
170 | |
171 // The list of <params, callback> pairs that have come from a call to GetData. | |
172 Tasks tasks_; | |
173 | |
174 // Installable properties cached on this object. | |
175 ManifestProperty manifest_prop_; | |
176 BooleanProperty installable_prop_; | |
177 IconMap icons_; | |
178 | |
179 bool is_active_; | |
180 | |
181 base::WeakPtrFactory<InstallableManager> weak_factory_; | |
182 | |
183 DISALLOW_COPY_AND_ASSIGN(InstallableManager); | |
184 }; | |
185 | |
186 #endif // CHROME_BROWSER_INSTALLABLE_INSTALLABLE_MANAGER_H_ | |
OLD | NEW |