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 <map> | |
9 #include <memory> | |
10 #include <utility> | |
11 #include <vector> | |
12 | |
13 #include "base/callback_forward.h" | |
14 #include "base/gtest_prod_util.h" | |
15 #include "base/macros.h" | |
16 #include "base/memory/weak_ptr.h" | |
17 #include "chrome/browser/installable/installable_logging.h" | |
18 #include "content/public/browser/web_contents_observer.h" | |
19 #include "content/public/browser/web_contents_user_data.h" | |
20 #include "content/public/common/manifest.h" | |
21 #include "third_party/skia/include/core/SkBitmap.h" | |
22 #include "url/gurl.h" | |
23 | |
24 // This struct specifies the work to be done by the InstallableManager. | |
25 // Data is cached and fetched in the order specified in this struct. A web app | |
26 // manifest will always be fetched first. | |
27 struct InstallableParams { | |
28 // The ideal icon size to fetch. Used only if |fetch_valid_icon| is true. | |
29 int ideal_icon_size_in_dp = -1; | |
30 | |
31 // The minimum icon size to fetch. Used only if |fetch_valid_icon| is true. | |
32 int minimum_icon_size_in_dp = -1; | |
33 | |
34 // Check whether the site is installable. That is, it has a manifest valid for | |
35 // a web app and a service worker controlling the manifest start URL and the | |
36 // current URL. | |
37 bool check_installable = false; | |
38 | |
39 // Check whether there is an icon in the manifest conforming to the icon size | |
40 // parameters, and that the icon can be fetched and isn't an empty bitmap. | |
41 bool fetch_valid_icon = false; | |
42 }; | |
43 | |
44 // This struct is passed to an InstallableCallback when the InstallableManager | |
45 // has finished working. Each reference is owned by InstallableManager, and | |
46 // callers should copy any objects which they wish to use later. Non-requested | |
47 // fields will be set to null, empty, or false. | |
48 struct InstallableData { | |
49 // NO_ERROR_DETECTED if there were no issues. | |
50 const InstallableErrorCode error_code; | |
51 | |
52 // Empty if the site has no <link rel="manifest"> tag. | |
53 const GURL& manifest_url; | |
54 | |
55 // Empty if the site has an unparseable manifest. | |
56 const content::Manifest& manifest; | |
57 | |
58 // Empty if no icon was requested. | |
59 const GURL& icon_url; | |
60 | |
61 // nullptr if the most appropriate icon couldn't be determined or downloaded. | |
62 // The underlying icon is owned by the InstallableManager; clients must copy | |
63 // the bitmap if they want to to use it. If fetch_valid_icon was true and an | |
64 // icon could not be retrieved, the reason will be in error_code. | |
65 const SkBitmap* icon; | |
66 | |
67 // true if the site has a service worker and a viable web app manifest. If | |
68 // check_installable was true and the site isn't installable, the reason will | |
69 // be in error_code. | |
70 const bool is_installable; | |
71 }; | |
72 | |
73 using InstallableCallback = base::Callback<void(const InstallableData&)>; | |
74 | |
75 // This class is responsible for fetching the resources required to check and | |
76 // install a site. | |
77 class InstallableManager | |
78 : public content::WebContentsObserver, | |
79 public content::WebContentsUserData<InstallableManager> { | |
80 public: | |
81 explicit InstallableManager(content::WebContents* web_contents); | |
82 ~InstallableManager() override; | |
83 | |
84 // Returns the minimum icon size in pixels for a site to be installable. | |
85 // TODO(dominickn): consolidate this concept with minimum_icon_size_in_dp | |
86 // across all platforms. | |
87 static int GetMinimumIconSizeInPx(); | |
88 | |
89 // Get the installable data, fetching the resources specified in |params|. | |
90 // |callback| is invoked on the UI thread when the data is ready | |
91 // | |
92 // Calls requesting data that is already fetched will return the cached data. | |
93 // This method is marked virtual so clients may mock this object in tests. | |
94 virtual void GetData(const InstallableParams& params, | |
95 const InstallableCallback& callback); | |
96 | |
97 private: | |
98 friend class InstallableManagerUnitTest; | |
99 FRIEND_TEST_ALL_PREFIXES(InstallableManagerBrowserTest, | |
100 ManagerBeginsInEmptyState); | |
101 FRIEND_TEST_ALL_PREFIXES(InstallableManagerBrowserTest, CheckWebapp); | |
102 | |
103 using Task = std::pair<InstallableParams, InstallableCallback>; | |
104 using IconParams = std::pair<int, int>; | |
105 | |
106 struct ManifestProperty { | |
benwells
2016/08/03 01:17:28
Nit: Can these just be fwd declared here?
Edit: a
dominickn
2016/08/03 06:38:04
Done.
| |
107 ~ManifestProperty(); | |
108 | |
109 InstallableErrorCode error = NO_ERROR_DETECTED; | |
110 GURL url; | |
111 content::Manifest manifest; | |
112 bool fetched = false; | |
113 }; | |
114 | |
115 struct InstallableProperty { | |
116 InstallableErrorCode error = NO_ERROR_DETECTED; | |
117 bool installable = false; | |
118 bool fetched = false; | |
119 }; | |
120 | |
121 struct IconProperty { | |
122 IconProperty(); | |
123 IconProperty(IconProperty&& other); | |
124 IconProperty& operator=(IconProperty&& other); | |
125 ~IconProperty(); | |
126 | |
127 InstallableErrorCode error; | |
128 GURL url; | |
129 std::unique_ptr<SkBitmap> icon; | |
130 bool fetched; | |
131 | |
132 private: | |
133 // This class contains a std::unique_ptr and therefore must be non-copyable. | |
134 DISALLOW_COPY_AND_ASSIGN(IconProperty); | |
135 }; | |
136 | |
137 // Returns the IconProperty matching |params|. Creates if it doesn't exist. | |
138 IconProperty& GetIcon(const InstallableParams& params); | |
139 | |
140 // Returns true if the icon sizes in |params| matches any fetched icon. false | |
141 // if no icon has been requested yet or there is no match. | |
142 bool IsIconFetched(const InstallableParams& params) const; | |
143 | |
144 // Sets the icon parameters in |params| as being fetched. | |
145 void SetIconFetched(const InstallableParams& params); | |
146 | |
147 // Returns the error code associated with the resources requested in |params|, | |
148 // or NO_ERROR_DETECTED if there is no error. | |
149 InstallableErrorCode GetErrorCode(const InstallableParams& params); | |
150 | |
151 // Returns the WebContents to which this object is attached, or nullptr if the | |
152 // WebContents doesn't exist or is currently being destroyed. | |
153 content::WebContents* GetWebContents(); | |
154 | |
155 // Returns true if |params| requires no more work to be done. | |
156 bool IsComplete(const InstallableParams& params) const; | |
157 | |
158 // Resets members to empty and removes all queued tasks. | |
159 // Called when navigating to a new page or if the WebContents is destroyed | |
160 // whilst waiting for a callback. | |
161 void Reset(); | |
162 | |
163 // Sets the fetched bit on the installable and icon subtasks. | |
164 // Called if no manifest (or an empty manifest) was fetched from the site. | |
165 void SetManifestDependentTasksComplete(); | |
166 | |
167 // Methods coordinating and dispatching work for the current task. | |
168 void RunCallback(const Task& task, InstallableErrorCode error); | |
169 void StartNextTask(); | |
170 void WorkOnTask(); | |
171 | |
172 // Data retrieval methods. | |
173 void FetchManifest(); | |
174 void OnDidGetManifest(const GURL& manifest_url, | |
175 const content::Manifest& manifest); | |
176 | |
177 void CheckInstallable(); | |
178 bool IsManifestValidForWebApp(const content::Manifest& manifest); | |
179 void CheckServiceWorker(); | |
180 void OnDidCheckHasServiceWorker(bool has_service_worker); | |
181 | |
182 void CheckAndFetchBestIcon(); | |
183 void OnAppIconFetched(const GURL icon_url, const SkBitmap& bitmap); | |
184 | |
185 // content::WebContentsObserver overrides | |
186 void DidFinishNavigation(content::NavigationHandle* handle) override; | |
187 void WebContentsDestroyed() override; | |
188 | |
189 const GURL& manifest_url() const; | |
190 const content::Manifest& manifest() const; | |
191 bool is_installable() const; | |
192 | |
193 // The list of <params, callback> pairs that have come from a call to GetData. | |
194 std::vector<Task> tasks_; | |
195 | |
196 // Installable properties cached on this object. | |
197 std::unique_ptr<ManifestProperty> manifest_; | |
198 std::unique_ptr<InstallableProperty> installable_; | |
199 std::map<IconParams, IconProperty> icons_; | |
200 | |
201 bool is_active_; | |
202 | |
203 base::WeakPtrFactory<InstallableManager> weak_factory_; | |
204 | |
205 DISALLOW_COPY_AND_ASSIGN(InstallableManager); | |
206 }; | |
207 | |
208 #endif // CHROME_BROWSER_INSTALLABLE_INSTALLABLE_MANAGER_H_ | |
OLD | NEW |