| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_BANNERS_APP_BANNER_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_BANNERS_APP_BANNER_MANAGER_H_ |
| 6 #define CHROME_BROWSER_BANNERS_APP_BANNER_MANAGER_H_ | 6 #define CHROME_BROWSER_BANNERS_APP_BANNER_MANAGER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/callback_forward.h" |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" | |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "chrome/browser/banners/app_banner_data_fetcher.h" | |
| 15 #include "chrome/browser/engagement/site_engagement_observer.h" | 14 #include "chrome/browser/engagement/site_engagement_observer.h" |
| 15 #include "chrome/browser/installable/installable_logging.h" |
| 16 #include "chrome/browser/installable/installable_manager.h" |
| 16 #include "content/public/browser/web_contents_observer.h" | 17 #include "content/public/browser/web_contents_observer.h" |
| 17 #include "third_party/WebKit/public/platform/modules/app_banner/WebAppBannerProm
ptReply.h" | 18 #include "third_party/WebKit/public/platform/modules/app_banner/WebAppBannerProm
ptReply.h" |
| 18 | 19 |
| 20 class SkBitmap; |
| 21 struct WebApplicationInfo; |
| 22 |
| 23 namespace content { |
| 24 class RenderFrameHost; |
| 25 class WebContents; |
| 26 } |
| 27 |
| 28 // This forward declaration exists solely for the DidFinishCreatingBookmarkApp |
| 29 // callback, implemented and called on desktop platforms only. |
| 30 namespace extensions { |
| 31 class Extension; |
| 32 } |
| 33 |
| 19 namespace banners { | 34 namespace banners { |
| 20 class AppBannerDataFetcher; | 35 |
| 21 | 36 // Coordinates the creation of an app banner, from detecting eligibility to |
| 22 /** | 37 // fetching data and creating the infobar. Sites declare that they want an app |
| 23 * Creates an app banner. | 38 // banner using the web app manifest. One web/native app may occupy the pipeline |
| 24 * | 39 // at a time; navigation resets the manager and discards any work in progress. |
| 25 * Hooks the wiring together for getting the data for a particular app. | 40 // |
| 26 * Monitors at most one app at a time, tracking the info for the most recently | 41 // This class contains the generic functionality shared between all platforms, |
| 27 * requested app. Any work in progress for other apps is discarded. | 42 // as well as no-op callbacks that the platform-specific implementations pass to |
| 28 */ | 43 // base::Bind. This allows a WeakPtrFactory to be housed in this class. |
| 44 // |
| 45 // The InstallableManager fetches and validate a site's eligibility for banners. |
| 46 // The manager is first called to fetch the manifest, so we can verify whether |
| 47 // the site is already installed (and on Android, divert the flow to a |
| 48 // native app banner if requested). The second call completes the checking for a |
| 49 // web app banner (checking manifest validity, service worker, and icon). |
| 29 class AppBannerManager : public content::WebContentsObserver, | 50 class AppBannerManager : public content::WebContentsObserver, |
| 30 public AppBannerDataFetcher::Delegate, | |
| 31 public SiteEngagementObserver { | 51 public SiteEngagementObserver { |
| 32 public: | 52 public: |
| 33 static void DisableSecureSchemeCheckForTesting(); | 53 static void DisableSecureSchemeCheckForTesting(); |
| 34 | 54 |
| 55 // Returns the current time. |
| 56 static base::Time GetCurrentTime(); |
| 57 |
| 58 // Fast-forwards the current time for testing. |
| 59 static void SetTimeDeltaForTesting(int days); |
| 60 |
| 61 // Sets the weights applied to direct and indirect navigations for triggering |
| 62 // the banner. Deprecated and will be removed when app banners fully migrates |
| 63 // to using site engagement as a trigger. |
| 35 static void SetEngagementWeights(double direct_engagement, | 64 static void SetEngagementWeights(double direct_engagement, |
| 36 double indirect_engagement); | 65 double indirect_engagement); |
| 37 | 66 |
| 38 // Returns whether or not the URLs match for everything except for the ref. | 67 // Returns whether or not the URLs match for everything except for the ref. |
| 39 static bool URLsAreForTheSamePage(const GURL& first, const GURL& second); | 68 static bool URLsAreForTheSamePage(const GURL& first, const GURL& second); |
| 40 | 69 |
| 41 // Requests an app banner. Set |is_debug_mode| when it is triggered by the | 70 // Requests an app banner. If |is_debug_mode| is true, any failure in the |
| 42 // developer's action in DevTools. | 71 // pipeline will be reported to the devtools console. |
| 43 virtual void RequestAppBanner(const GURL& validated_url, bool is_debug_mode); | 72 virtual void RequestAppBanner(const GURL& validated_url, bool is_debug_mode); |
| 44 | 73 |
| 45 ~AppBannerManager() override; | 74 // Overridden and passed through base::Bind on desktop platforms. Called when |
| 75 // the bookmark app install initiated by a banner has completed. Not used on |
| 76 // Android. |
| 77 virtual void DidFinishCreatingBookmarkApp( |
| 78 const extensions::Extension* extension, |
| 79 const WebApplicationInfo& web_app_info) { } |
| 80 |
| 81 // Overridden and passed through base::Bind on Android. Called when the |
| 82 // download of a native app's icon is complete, as native banners use an icon |
| 83 // provided from the Play Store rather than the web manifest. Not used on |
| 84 // desktop platforms. |
| 85 virtual void OnAppIconFetched(const SkBitmap& bitmap) { } |
| 86 |
| 87 // Overridden and passed through base::Bind on Android. Called after a web app |
| 88 // banner was successfully used to add a web app to homescreen to kick off an |
| 89 // asynchronous fetch of a splash screen icon. Not used on desktop platforms. |
| 90 virtual base::Closure FetchWebappSplashScreenImageCallback( |
| 91 const std::string& webapp_id); |
| 46 | 92 |
| 47 protected: | 93 protected: |
| 48 explicit AppBannerManager(content::WebContents* web_contents); | 94 explicit AppBannerManager(content::WebContents* web_contents); |
| 49 | 95 ~AppBannerManager() override; |
| 50 void ReplaceWebContents(content::WebContents* web_contents); | 96 |
| 51 | 97 // Return a string identifying this app for metrics. |
| 52 // Creates an AppBannerDataFetcher, which constructs an app banner. | 98 virtual std::string GetAppIdentifier(); |
| 53 virtual AppBannerDataFetcher* CreateAppBannerDataFetcher( | 99 |
| 54 base::WeakPtr<AppBannerDataFetcher::Delegate> weak_delegate, | 100 // Return a string describing what type of banner is being created. Used when |
| 55 bool is_debug_mode) = 0; | 101 // alerting websites that a banner is about to be created. |
| 56 | 102 virtual std::string GetBannerType(); |
| 57 // Return whether the AppBannerDataFetcher is active. | 103 |
| 58 bool IsFetcherActive(); | 104 // Returns a string parameter for a devtools console message corresponding to |
| 59 | 105 // |code|. Returns the empty string if |code| requires no parameter. |
| 60 scoped_refptr<AppBannerDataFetcher> data_fetcher() { return data_fetcher_; } | 106 std::string GetErrorParam(InstallableErrorCode code); |
| 61 | 107 |
| 62 private: | 108 // Returns the ideal and minimum icon sizes required for being installable. |
| 63 // WebContentsObserver overrides. | 109 virtual int GetIdealIconSizeInDp(); |
| 64 void DidStartNavigation( | 110 virtual int GetMinimumIconSizeInDp(); |
| 65 content::NavigationHandle* navigation_handle) override; | 111 |
| 66 void DidFinishNavigation( | 112 // Returns a WeakPtr to this object. Exposed so subclasses/infobars may |
| 67 content::NavigationHandle* navigation_handle) override; | 113 // may bind callbacks without needing their own WeakPtrFactory. |
| 114 base::WeakPtr<AppBannerManager> GetWeakPtr(); |
| 115 |
| 116 // Returns true if |is_debug_mode_| is true or the |
| 117 // kBypassAppBannerEngagementChecks flag is set. |
| 118 bool IsDebugMode() const; |
| 119 |
| 120 // Returns true if the webapp at |start_url| has already been installed. |
| 121 virtual bool IsWebAppInstalled(content::BrowserContext* browser_context, |
| 122 const GURL& start_url); |
| 123 |
| 124 // Callback invoked by the InstallableManager once it has fetched the page's |
| 125 // manifest. |
| 126 void OnDidGetManifest(const InstallableData& result); |
| 127 |
| 128 // Run at the conclusion of OnDidGetManifest. For web app banners, this calls |
| 129 // back to the InstallableManager to continue checking criteria. For native |
| 130 // app banners, this checks whether native apps are preferred in the manifest, |
| 131 // and calls to Java to verify native app details. If a native banner isn't or |
| 132 // can't be requested, it continues with the web app banner checks. |
| 133 virtual void PerformInstallableCheck(); |
| 134 |
| 135 // Callback invoked by the InstallableManager once it has finished checking |
| 136 // all other installable properties. |
| 137 void OnDidPerformInstallableCheck(const InstallableData& result); |
| 138 |
| 139 // Records that a banner was shown. The |event_name| corresponds to the RAPPOR |
| 140 // metric being recorded. |
| 141 void RecordDidShowBanner(const std::string& event_name); |
| 142 |
| 143 // Logs an error message corresponding to |code| to the devtools console |
| 144 // attached to |web_contents|. Does nothing if IsDebugMode() returns false. |
| 145 void ReportError(content::WebContents* web_contents, |
| 146 InstallableErrorCode code); |
| 147 |
| 148 // Stops the banner pipeline. Any callback currently running will terminate |
| 149 // the next time they check |is_active_|. |
| 150 virtual void Stop(); |
| 151 |
| 152 // Sends a message to the renderer that the page has met the requirements to |
| 153 // show a banner. The page can respond to cancel the banner (and possibly |
| 154 // display it later), or otherwise allow it to be shown. This is virtual to |
| 155 // allow tests to mock out the renderer IPC. |
| 156 virtual void SendBannerPromptRequest(); |
| 157 |
| 158 // content::WebContentsObserver overrides. |
| 159 void DidStartNavigation(content::NavigationHandle* handle) override; |
| 160 void DidFinishNavigation(content::NavigationHandle* handle) override; |
| 68 void DidFinishLoad(content::RenderFrameHost* render_frame_host, | 161 void DidFinishLoad(content::RenderFrameHost* render_frame_host, |
| 69 const GURL& validated_url) override; | 162 const GURL& validated_url) override; |
| 70 void MediaStartedPlaying(const MediaPlayerId& id) override; | 163 void MediaStartedPlaying(const MediaPlayerId& id) override; |
| 71 void MediaStoppedPlaying(const MediaPlayerId& id) override; | 164 void MediaStoppedPlaying(const MediaPlayerId& id) override; |
| 72 | 165 void WebContentsDestroyed() override; |
| 73 // AppBannerDataFetcher::Delegate overrides. | |
| 74 bool HandleNonWebApp(const std::string& platform, | |
| 75 const GURL& url, | |
| 76 const std::string& id, | |
| 77 bool is_debug_mode) override; | |
| 78 | 166 |
| 79 // SiteEngagementObserver overrides. | 167 // SiteEngagementObserver overrides. |
| 80 void OnEngagementIncreased(content::WebContents* web_contents, | 168 void OnEngagementIncreased(content::WebContents* web_contents, |
| 81 const GURL& url, | 169 const GURL& url, |
| 82 double score) override; | 170 double score) override; |
| 83 | 171 |
| 84 // Cancels an active DataFetcher, stopping its banners from appearing. | 172 // Subclass accessors for private fields which should not be changed outside |
| 85 void CancelActiveFetcher(); | 173 // this class. |
| 174 InstallableManager* manager() const { return manager_; } |
| 175 int event_request_id() const { return event_request_id_; } |
| 176 bool is_active() const { return is_active_; } |
| 177 |
| 178 // The title to display in the banner. |
| 179 base::string16 app_title_; |
| 180 |
| 181 // The URL for which the banner check is being conducted. |
| 182 GURL validated_url_; |
| 183 |
| 184 // The URL of the manifest. |
| 185 GURL manifest_url_; |
| 186 |
| 187 // The manifest object. |
| 188 content::Manifest manifest_; |
| 189 |
| 190 // The URL of the icon. |
| 191 GURL icon_url_; |
| 192 |
| 193 // The icon object. |
| 194 std::unique_ptr<SkBitmap> icon_; |
| 195 |
| 196 // The referrer string (if any) specified in the app URL. Used only for native |
| 197 // app banners. |
| 198 std::string referrer_; |
| 199 |
| 200 private: |
| 201 friend class AppBannerManagerTest; |
| 202 |
| 203 // Record that the banner could be shown at this point, if the triggering |
| 204 // heuristic allowed. |
| 205 void RecordCouldShowBanner(); |
| 206 |
| 207 // Creates a banner for the app. Overridden by subclasses as the infobar is |
| 208 // platform-specific. |
| 209 virtual void ShowBanner() = 0; |
| 210 |
| 211 // Returns true if the banner should be shown. |
| 212 bool CheckIfShouldShowBanner(); |
| 213 |
| 214 bool OnMessageReceived(const IPC::Message& message, |
| 215 content::RenderFrameHost* render_frame_host) override; |
| 216 |
| 217 // Called after the manager sends a message to the renderer regarding its |
| 218 // intention to show a prompt. The renderer will send a message back with the |
| 219 // opportunity to cancel. |
| 220 void OnBannerPromptReply(content::RenderFrameHost* render_frame_host, |
| 221 int request_id, |
| 222 blink::WebAppBannerPromptReply reply, |
| 223 std::string referrer); |
| 224 |
| 225 // Called when the client has prevented a banner from being shown, and is |
| 226 // now requesting that it be shown later. |
| 227 void OnRequestShowAppBanner(content::RenderFrameHost* render_frame_host, |
| 228 int request_id); |
| 86 | 229 |
| 87 // The type of navigation made to the page | 230 // The type of navigation made to the page |
| 88 ui::PageTransition last_transition_type_; | 231 ui::PageTransition last_transition_type_; |
| 89 | 232 |
| 90 // Fetches the data required to display a banner for the current page. | 233 // Fetches the data required to display a banner for the current page. |
| 91 scoped_refptr<AppBannerDataFetcher> data_fetcher_; | 234 InstallableManager* manager_; |
| 235 |
| 236 // A monotonically increasing id to verify the response to the |
| 237 // beforeinstallprompt event from the renderer. |
| 238 int event_request_id_; |
| 92 | 239 |
| 93 // We do not want to trigger a banner when the manager is attached to | 240 // We do not want to trigger a banner when the manager is attached to |
| 94 // a WebContents that is playing video. Banners triggering on a site in the | 241 // a WebContents that is playing video. Banners triggering on a site in the |
| 95 // background will appear when the tab is reactivated. | 242 // background will appear when the tab is reactivated. |
| 96 std::vector<MediaPlayerId> active_media_players_; | 243 std::vector<MediaPlayerId> active_media_players_; |
| 97 | 244 |
| 245 // Whether we are currently working on whether to show a banner. |
| 246 bool is_active_; |
| 247 |
| 98 // If a banner is requested before the page has finished loading, defer | 248 // If a banner is requested before the page has finished loading, defer |
| 99 // triggering the pipeline until the load is complete. | 249 // triggering the pipeline until the load is complete. |
| 100 bool banner_request_queued_; | 250 bool banner_request_queued_; |
| 101 bool load_finished_; | 251 bool load_finished_; |
| 102 | 252 |
| 103 // A weak pointer is used as the lifetime of the ServiceWorkerContext is | 253 // Record whether the page decides to defer showing the banner, and if it |
| 104 // longer than the lifetime of this banner manager. The banner manager | 254 // requests for it to be shown later on. |
| 105 // might be gone when calls sent to the ServiceWorkerContext are completed. | 255 bool was_canceled_by_page_; |
| 256 bool page_requested_prompt_; |
| 257 |
| 258 // Whether we should be logging errors to the console for this request. |
| 259 bool is_debug_mode_; |
| 260 |
| 261 // The concrete subclasses of this class are expected to have their lifetimes |
| 262 // scoped to the WebContents which they are observing. This allows us to use |
| 263 // weak pointers for callbacks. |
| 106 base::WeakPtrFactory<AppBannerManager> weak_factory_; | 264 base::WeakPtrFactory<AppBannerManager> weak_factory_; |
| 107 | 265 |
| 108 DISALLOW_COPY_AND_ASSIGN(AppBannerManager); | 266 DISALLOW_COPY_AND_ASSIGN(AppBannerManager); |
| 109 }; // class AppBannerManager | 267 }; |
| 110 | 268 |
| 111 } // namespace banners | 269 } // namespace banners |
| 112 | 270 |
| 113 #endif // CHROME_BROWSER_BANNERS_APP_BANNER_MANAGER_H_ | 271 #endif // CHROME_BROWSER_BANNERS_APP_BANNER_MANAGER_H_ |
| OLD | NEW |