| 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 <utility> | 11 #include <utility> |
| 11 #include <vector> | 12 #include <vector> |
| 12 | 13 |
| 13 #include "base/callback_forward.h" | 14 #include "base/callback_forward.h" |
| 14 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
| 15 #include "base/macros.h" | 16 #include "base/macros.h" |
| 16 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 17 #include "chrome/browser/installable/installable_logging.h" | 18 #include "chrome/browser/installable/installable_logging.h" |
| 18 #include "content/public/browser/web_contents_observer.h" | 19 #include "content/public/browser/web_contents_observer.h" |
| 19 #include "content/public/browser/web_contents_user_data.h" | 20 #include "content/public/browser/web_contents_user_data.h" |
| 20 #include "content/public/common/manifest.h" | 21 #include "content/public/common/manifest.h" |
| 21 #include "third_party/skia/include/core/SkBitmap.h" | 22 #include "third_party/skia/include/core/SkBitmap.h" |
| 22 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 23 | 24 |
| 24 // This struct specifies the work to be done by the InstallableManager. | 25 // 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 // Data is cached and fetched in the order specified in this struct. A web app |
| 26 // manifest will always be fetched first. | 27 // manifest will always be fetched first. |
| 27 struct InstallableParams { | 28 struct InstallableParams { |
| 28 // The ideal primary icon size to fetch. Used only if | 29 // The ideal primary icon size to fetch. Used only if |
| 29 // |fetch_valid_primary_icon| is true. | 30 // |fetch_valid_primary_icon| is true. |
| 30 int ideal_primary_icon_size_in_px = -1; | 31 int ideal_primary_icon_size_in_px = -1; |
| 31 | 32 |
| 32 // The minimum primary icon size to fetch. Used only if | 33 // The minimum primary icon size to fetch. Used only if |
| 33 // |fetch_valid_primary_icon| is true. | 34 // |fetch_valid_primary_icon| is true. |
| 34 int minimum_primary_icon_size_in_px = -1; | 35 int minimum_primary_icon_size_in_px = -1; |
| 35 | 36 |
| 37 // The ideal badge icon size to fetch. Used only if |
| 38 // |fetch_valid_badge_icon| is true. |
| 39 int ideal_badge_icon_size_in_px = -1; |
| 40 |
| 41 // The minimum badge icon size to fetch. Used only if |
| 42 // |fetch_valid_badge_icon| is true. |
| 43 int minimum_badge_icon_size_in_px = -1; |
| 44 |
| 36 // Check whether the site is installable. That is, it has a manifest valid for | 45 // Check whether the site is installable. That is, it has a manifest valid for |
| 37 // a web app and a service worker controlling the manifest start URL and the | 46 // a web app and a service worker controlling the manifest start URL and the |
| 38 // current URL. | 47 // current URL. |
| 39 bool check_installable = false; | 48 bool check_installable = false; |
| 40 | 49 |
| 41 // Check whether there is a fetchable, non-empty icon in the manifest | 50 // Check whether there is a fetchable, non-empty icon in the manifest |
| 42 // conforming to the primary icon size parameters. | 51 // conforming to the primary icon size parameters. |
| 43 bool fetch_valid_primary_icon = false; | 52 bool fetch_valid_primary_icon = false; |
| 53 |
| 54 // Check whether there is a fetchable, non-empty icon in the manifest |
| 55 // conforming to the badge icon size parameters. |
| 56 bool fetch_valid_badge_icon = false; |
| 44 }; | 57 }; |
| 45 | 58 |
| 46 // This struct is passed to an InstallableCallback when the InstallableManager | 59 // This struct is passed to an InstallableCallback when the InstallableManager |
| 47 // has finished working. Each reference is owned by InstallableManager, and | 60 // has finished working. Each reference is owned by InstallableManager, and |
| 48 // callers should copy any objects which they wish to use later. Non-requested | 61 // callers should copy any objects which they wish to use later. Non-requested |
| 49 // fields will be set to null, empty, or false. | 62 // fields will be set to null, empty, or false. |
| 50 struct InstallableData { | 63 struct InstallableData { |
| 51 // NO_ERROR_DETECTED if there were no issues. | 64 // NO_ERROR_DETECTED if there were no issues. |
| 52 const InstallableStatusCode error_code; | 65 const InstallableStatusCode error_code; |
| 53 | 66 |
| 54 // Empty if the site has no <link rel="manifest"> tag. | 67 // Empty if the site has no <link rel="manifest"> tag. |
| 55 const GURL& manifest_url; | 68 const GURL& manifest_url; |
| 56 | 69 |
| 57 // Empty if the site has an unparseable manifest. | 70 // Empty if the site has an unparseable manifest. |
| 58 const content::Manifest& manifest; | 71 const content::Manifest& manifest; |
| 59 | 72 |
| 60 // Empty if no primary_icon was requested. | 73 // Empty if no primary_icon was requested. |
| 61 const GURL& primary_icon_url; | 74 const GURL& primary_icon_url; |
| 62 | 75 |
| 63 // nullptr if the most appropriate primary icon couldn't be determined or | 76 // nullptr if the most appropriate primary icon couldn't be determined or |
| 64 // downloaded. The underlying primary icon is owned by the InstallableManager; | 77 // downloaded. The underlying primary icon is owned by the InstallableManager; |
| 65 // clients must copy the bitmap if they want to to use it. If | 78 // clients must copy the bitmap if they want to to use it. If |
| 66 // fetch_valid_primary_icon was true and a primary icon could not be | 79 // fetch_valid_primary_icon was true and a primary icon could not be |
| 67 // retrieved, the reason will be in error_code. | 80 // retrieved, the reason will be in error_code. |
| 68 const SkBitmap* primary_icon; | 81 const SkBitmap* primary_icon; |
| 69 | 82 |
| 83 // Empty if no badge_icon was requested. |
| 84 const GURL& badge_icon_url; |
| 85 |
| 86 // nullptr if the most appropriate badge icon couldn't be determined or |
| 87 // downloaded. The underlying badge icon is owned by the InstallableManager; |
| 88 // clients must copy the bitmap if they want to to use it. Since the badge |
| 89 // icon is optional, no error code is set if it cannot be fetched, and clients |
| 90 // specifying fetch_valid_badge_icon must check that the bitmap exists before |
| 91 // using it. |
| 92 const SkBitmap* badge_icon; |
| 93 |
| 70 // true if the site has a service worker and a viable web app manifest. If | 94 // true if the site has a service worker and a viable web app manifest. If |
| 71 // check_installable was true and the site isn't installable, the reason will | 95 // check_installable was true and the site isn't installable, the reason will |
| 72 // be in error_code. | 96 // be in error_code. |
| 73 const bool is_installable; | 97 const bool is_installable; |
| 74 }; | 98 }; |
| 75 | 99 |
| 76 using InstallableCallback = base::Callback<void(const InstallableData&)>; | 100 using InstallableCallback = base::Callback<void(const InstallableData&)>; |
| 77 | 101 |
| 78 // This class is responsible for fetching the resources required to check and | 102 // This class is responsible for fetching the resources required to check and |
| 79 // install a site. | 103 // install a site. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 103 virtual void GetData(const InstallableParams& params, | 127 virtual void GetData(const InstallableParams& params, |
| 104 const InstallableCallback& callback); | 128 const InstallableCallback& callback); |
| 105 | 129 |
| 106 private: | 130 private: |
| 107 friend class InstallableManagerUnitTest; | 131 friend class InstallableManagerUnitTest; |
| 108 FRIEND_TEST_ALL_PREFIXES(InstallableManagerBrowserTest, | 132 FRIEND_TEST_ALL_PREFIXES(InstallableManagerBrowserTest, |
| 109 ManagerBeginsInEmptyState); | 133 ManagerBeginsInEmptyState); |
| 110 FRIEND_TEST_ALL_PREFIXES(InstallableManagerBrowserTest, CheckWebapp); | 134 FRIEND_TEST_ALL_PREFIXES(InstallableManagerBrowserTest, CheckWebapp); |
| 111 | 135 |
| 112 using Task = std::pair<InstallableParams, InstallableCallback>; | 136 using Task = std::pair<InstallableParams, InstallableCallback>; |
| 113 using IconParams = std::pair<int, int>; | 137 using IconParams = std::tuple<int, int, content::Manifest::Icon::IconPurpose>; |
| 114 | 138 |
| 115 struct ManifestProperty; | 139 struct ManifestProperty; |
| 116 struct InstallableProperty; | 140 struct InstallableProperty; |
| 117 struct IconProperty; | 141 struct IconProperty; |
| 118 | 142 |
| 119 // Returns the IconProperty matching |params|. Creates if it doesn't exist. | 143 // Returns an IconParams object that queries for a primary icon conforming to |
| 120 IconProperty& GetIcon(const InstallableParams& params); | 144 // the primary icon size parameters in |params|. |
| 145 IconParams ParamsForPrimaryIcon(const InstallableParams& params) const; |
| 146 // Returns an IconParams object that queries for a badge icon conforming to |
| 147 // the badge icon size parameters in |params|. |
| 148 IconParams ParamsForBadgeIcon(const InstallableParams& params) const; |
| 121 | 149 |
| 122 // Returns true if the icon sizes in |params| matches any fetched icon. false | 150 // Returns true if |params| matches any fetched icon, or false if no icon has |
| 123 // if no icon has been requested yet or there is no match. | 151 // been requested yet or there is no match. |
| 124 bool IsIconFetched(const InstallableParams& params) const; | 152 bool IsIconFetched(const IconParams& params) const; |
| 125 | 153 |
| 126 // Sets the icon parameters in |params| as being fetched. | 154 // Sets the icon matching |params| as fetched. |
| 127 void SetIconFetched(const InstallableParams& params); | 155 void SetIconFetched(const IconParams& params); |
| 128 | 156 |
| 129 // Returns the error code associated with the resources requested in |params|, | 157 // Returns the error code associated with the resources requested in |params|, |
| 130 // or NO_ERROR_DETECTED if there is no error. | 158 // or NO_ERROR_DETECTED if there is no error. |
| 131 InstallableStatusCode GetErrorCode(const InstallableParams& params); | 159 InstallableStatusCode GetErrorCode(const InstallableParams& params); |
| 132 | 160 |
| 133 // Gets/sets parts of particular properties. Exposed for testing. | 161 // Gets/sets parts of particular properties. Exposed for testing. |
| 134 InstallableStatusCode manifest_error() const; | 162 InstallableStatusCode manifest_error() const; |
| 135 InstallableStatusCode installable_error() const; | 163 InstallableStatusCode installable_error() const; |
| 136 void set_installable_error(InstallableStatusCode error_code); | 164 void set_installable_error(InstallableStatusCode error_code); |
| 137 InstallableStatusCode icon_error(const IconParams& icon_params); | 165 InstallableStatusCode icon_error(const IconParams& icon_params); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 162 // Data retrieval methods. | 190 // Data retrieval methods. |
| 163 void FetchManifest(); | 191 void FetchManifest(); |
| 164 void OnDidGetManifest(const GURL& manifest_url, | 192 void OnDidGetManifest(const GURL& manifest_url, |
| 165 const content::Manifest& manifest); | 193 const content::Manifest& manifest); |
| 166 | 194 |
| 167 void CheckInstallable(); | 195 void CheckInstallable(); |
| 168 bool IsManifestValidForWebApp(const content::Manifest& manifest); | 196 bool IsManifestValidForWebApp(const content::Manifest& manifest); |
| 169 void CheckServiceWorker(); | 197 void CheckServiceWorker(); |
| 170 void OnDidCheckHasServiceWorker(bool has_service_worker); | 198 void OnDidCheckHasServiceWorker(bool has_service_worker); |
| 171 | 199 |
| 172 void CheckAndFetchBestIcon(); | 200 void CheckAndFetchBestIcon(const IconParams& params); |
| 173 void OnAppIconFetched(const GURL icon_url, const SkBitmap& bitmap); | 201 void OnIconFetched( |
| 202 const GURL icon_url, const IconParams& params, const SkBitmap& bitmap); |
| 174 | 203 |
| 175 // content::WebContentsObserver overrides | 204 // content::WebContentsObserver overrides |
| 176 void DidFinishNavigation(content::NavigationHandle* handle) override; | 205 void DidFinishNavigation(content::NavigationHandle* handle) override; |
| 177 void WebContentsDestroyed() override; | 206 void WebContentsDestroyed() override; |
| 178 | 207 |
| 179 const GURL& manifest_url() const; | 208 const GURL& manifest_url() const; |
| 180 const content::Manifest& manifest() const; | 209 const content::Manifest& manifest() const; |
| 181 bool is_installable() const; | 210 bool is_installable() const; |
| 182 | 211 |
| 183 // The list of <params, callback> pairs that have come from a call to GetData. | 212 // The list of <params, callback> pairs that have come from a call to GetData. |
| 184 std::vector<Task> tasks_; | 213 std::vector<Task> tasks_; |
| 185 | 214 |
| 186 // Installable properties cached on this object. | 215 // Installable properties cached on this object. |
| 187 std::unique_ptr<ManifestProperty> manifest_; | 216 std::unique_ptr<ManifestProperty> manifest_; |
| 188 std::unique_ptr<InstallableProperty> installable_; | 217 std::unique_ptr<InstallableProperty> installable_; |
| 189 std::map<IconParams, IconProperty> icons_; | 218 std::map<IconParams, IconProperty> icons_; |
| 190 | 219 |
| 191 bool is_active_; | 220 bool is_active_; |
| 192 | 221 |
| 193 base::WeakPtrFactory<InstallableManager> weak_factory_; | 222 base::WeakPtrFactory<InstallableManager> weak_factory_; |
| 194 | 223 |
| 195 DISALLOW_COPY_AND_ASSIGN(InstallableManager); | 224 DISALLOW_COPY_AND_ASSIGN(InstallableManager); |
| 196 }; | 225 }; |
| 197 | 226 |
| 198 #endif // CHROME_BROWSER_INSTALLABLE_INSTALLABLE_MANAGER_H_ | 227 #endif // CHROME_BROWSER_INSTALLABLE_INSTALLABLE_MANAGER_H_ |
| OLD | NEW |