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 synchronously (i.e. no via PostTask on the UI thread |
| 91 // when the data is ready; the synchronous execution ensures that the |
| 92 // references |callback| receives in its InstallableData argument are valid. |
| 93 // |
| 94 // Calls requesting data that is already fetched will return the cached data. |
| 95 // This method is marked virtual so clients may mock this object in tests. |
| 96 virtual void GetData(const InstallableParams& params, |
| 97 const InstallableCallback& callback); |
| 98 |
| 99 private: |
| 100 friend class InstallableManagerUnitTest; |
| 101 FRIEND_TEST_ALL_PREFIXES(InstallableManagerBrowserTest, |
| 102 ManagerBeginsInEmptyState); |
| 103 FRIEND_TEST_ALL_PREFIXES(InstallableManagerBrowserTest, CheckWebapp); |
| 104 |
| 105 using Task = std::pair<InstallableParams, InstallableCallback>; |
| 106 using IconParams = std::pair<int, int>; |
| 107 |
| 108 struct ManifestProperty; |
| 109 struct InstallableProperty; |
| 110 struct IconProperty; |
| 111 |
| 112 // Returns the IconProperty matching |params|. Creates if it doesn't exist. |
| 113 IconProperty& GetIcon(const InstallableParams& params); |
| 114 |
| 115 // Returns true if the icon sizes in |params| matches any fetched icon. false |
| 116 // if no icon has been requested yet or there is no match. |
| 117 bool IsIconFetched(const InstallableParams& params) const; |
| 118 |
| 119 // Sets the icon parameters in |params| as being fetched. |
| 120 void SetIconFetched(const InstallableParams& params); |
| 121 |
| 122 // Returns the error code associated with the resources requested in |params|, |
| 123 // or NO_ERROR_DETECTED if there is no error. |
| 124 InstallableErrorCode GetErrorCode(const InstallableParams& params); |
| 125 |
| 126 // Gets/sets parts of particular properties. Exposed for testing. |
| 127 InstallableErrorCode manifest_error() const; |
| 128 InstallableErrorCode installable_error() const; |
| 129 void set_installable_error(InstallableErrorCode error_code); |
| 130 InstallableErrorCode icon_error(const IconParams& icon_params); |
| 131 GURL& icon_url(const IconParams& icon_params); |
| 132 const SkBitmap* icon(const IconParams& icon); |
| 133 |
| 134 // Returns the WebContents to which this object is attached, or nullptr if the |
| 135 // WebContents doesn't exist or is currently being destroyed. |
| 136 content::WebContents* GetWebContents(); |
| 137 |
| 138 // Returns true if |params| requires no more work to be done. |
| 139 bool IsComplete(const InstallableParams& params) const; |
| 140 |
| 141 // Resets members to empty and removes all queued tasks. |
| 142 // Called when navigating to a new page or if the WebContents is destroyed |
| 143 // whilst waiting for a callback. |
| 144 void Reset(); |
| 145 |
| 146 // Sets the fetched bit on the installable and icon subtasks. |
| 147 // Called if no manifest (or an empty manifest) was fetched from the site. |
| 148 void SetManifestDependentTasksComplete(); |
| 149 |
| 150 // Methods coordinating and dispatching work for the current task. |
| 151 void RunCallback(const Task& task, InstallableErrorCode error); |
| 152 void StartNextTask(); |
| 153 void WorkOnTask(); |
| 154 |
| 155 // Data retrieval methods. |
| 156 void FetchManifest(); |
| 157 void OnDidGetManifest(const GURL& manifest_url, |
| 158 const content::Manifest& manifest); |
| 159 |
| 160 void CheckInstallable(); |
| 161 bool IsManifestValidForWebApp(const content::Manifest& manifest); |
| 162 void CheckServiceWorker(); |
| 163 void OnDidCheckHasServiceWorker(bool has_service_worker); |
| 164 |
| 165 void CheckAndFetchBestIcon(); |
| 166 void OnAppIconFetched(const GURL icon_url, const SkBitmap& bitmap); |
| 167 |
| 168 // content::WebContentsObserver overrides |
| 169 void DidFinishNavigation(content::NavigationHandle* handle) override; |
| 170 void WebContentsDestroyed() override; |
| 171 |
| 172 const GURL& manifest_url() const; |
| 173 const content::Manifest& manifest() const; |
| 174 bool is_installable() const; |
| 175 |
| 176 // The list of <params, callback> pairs that have come from a call to GetData. |
| 177 std::vector<Task> tasks_; |
| 178 |
| 179 // Installable properties cached on this object. |
| 180 std::unique_ptr<ManifestProperty> manifest_; |
| 181 std::unique_ptr<InstallableProperty> installable_; |
| 182 std::map<IconParams, IconProperty> icons_; |
| 183 |
| 184 bool is_active_; |
| 185 |
| 186 base::WeakPtrFactory<InstallableManager> weak_factory_; |
| 187 |
| 188 DISALLOW_COPY_AND_ASSIGN(InstallableManager); |
| 189 }; |
| 190 |
| 191 #endif // CHROME_BROWSER_INSTALLABLE_INSTALLABLE_MANAGER_H_ |
OLD | NEW |