| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_BANNERS_APP_BANNER_DATA_FETCHER_H_ | |
| 6 #define CHROME_BROWSER_BANNERS_APP_BANNER_DATA_FETCHER_H_ | |
| 7 | |
| 8 #include <memory> | |
| 9 | |
| 10 #include "base/macros.h" | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "base/memory/weak_ptr.h" | |
| 13 #include "base/observer_list.h" | |
| 14 #include "base/time/time.h" | |
| 15 #include "chrome/common/web_application_info.h" | |
| 16 #include "content/public/browser/browser_thread.h" | |
| 17 #include "content/public/browser/web_contents.h" | |
| 18 #include "content/public/browser/web_contents_observer.h" | |
| 19 #include "content/public/common/manifest.h" | |
| 20 #include "third_party/WebKit/public/platform/modules/app_banner/WebAppBannerProm
ptReply.h" | |
| 21 | |
| 22 namespace chrome { | |
| 23 class BitmapFetcher; | |
| 24 } // namespace chrome | |
| 25 | |
| 26 namespace infobars { | |
| 27 class InfoBar; | |
| 28 } // namespace infobars | |
| 29 | |
| 30 namespace banners { | |
| 31 class AppBannerDataFetcher; | |
| 32 | |
| 33 // Fetches data required to show a web app banner for the URL currently shown by | |
| 34 // the WebContents. | |
| 35 class AppBannerDataFetcher : public base::RefCountedThreadSafe< | |
| 36 AppBannerDataFetcher, | |
| 37 content::BrowserThread::DeleteOnUIThread>, | |
| 38 public content::WebContentsObserver { | |
| 39 public: | |
| 40 class Observer { | |
| 41 public: | |
| 42 virtual void OnDecidedWhetherToShow(AppBannerDataFetcher* fetcher, | |
| 43 bool will_show) = 0; | |
| 44 virtual void OnFetcherDestroyed(AppBannerDataFetcher* fetcher) = 0; | |
| 45 }; | |
| 46 | |
| 47 class Delegate { | |
| 48 public: | |
| 49 // Called to handle a non-web app. Returns |true| if the non-web app can be | |
| 50 // handled, and the fetcher needs to remain active and wait for a callback. | |
| 51 virtual bool HandleNonWebApp(const std::string& platform, | |
| 52 const GURL& url, | |
| 53 const std::string& id, | |
| 54 bool is_debug_mode) = 0; | |
| 55 }; | |
| 56 | |
| 57 // Returns the current time. | |
| 58 static base::Time GetCurrentTime(); | |
| 59 | |
| 60 // Fast-forwards the current time for testing. | |
| 61 static void SetTimeDeltaForTesting(int days); | |
| 62 | |
| 63 AppBannerDataFetcher(content::WebContents* web_contents, | |
| 64 base::WeakPtr<Delegate> weak_delegate, | |
| 65 int ideal_icon_size_in_dp, | |
| 66 int minimum_icon_size_in_dp, | |
| 67 bool is_debug_mode); | |
| 68 | |
| 69 // Begins creating a banner for the URL being displayed by the Delegate's | |
| 70 // WebContents. | |
| 71 void Start(const GURL& validated_url, ui::PageTransition transition_type); | |
| 72 | |
| 73 // Stops the pipeline when any asynchronous calls return. | |
| 74 void Cancel(); | |
| 75 | |
| 76 // Replaces the WebContents that is being observed. | |
| 77 void ReplaceWebContents(content::WebContents* web_contents); | |
| 78 | |
| 79 // Adds an observer to the list. | |
| 80 void AddObserverForTesting(Observer* observer); | |
| 81 | |
| 82 // Removes an observer from the list. | |
| 83 void RemoveObserverForTesting(Observer* observer); | |
| 84 | |
| 85 // Returns whether or not the pipeline has been stopped. | |
| 86 bool is_active() { return is_active_; } | |
| 87 | |
| 88 // Returns whether the beforeinstallprompt Javascript event was canceled. | |
| 89 bool was_canceled_by_page() { return was_canceled_by_page_; } | |
| 90 | |
| 91 // Returns whether the page has validly requested that the banner be shown | |
| 92 // by calling prompt() on the beforeinstallprompt Javascript event. | |
| 93 bool page_requested_prompt() { return page_requested_prompt_; } | |
| 94 | |
| 95 // Returns true when it was created by the user action in DevTools or | |
| 96 // "bypass-app-banner-engagement-checks" flag is set. | |
| 97 bool is_debug_mode() const { return is_debug_mode_; } | |
| 98 | |
| 99 // Returns the type of transition which triggered this fetch. | |
| 100 ui::PageTransition transition_type() { return transition_type_; } | |
| 101 | |
| 102 // Returns the URL that kicked off the banner data retrieval. | |
| 103 const GURL& validated_url() { return validated_url_; } | |
| 104 | |
| 105 // WebContentsObserver overrides. | |
| 106 void WebContentsDestroyed() override; | |
| 107 void DidNavigateMainFrame( | |
| 108 const content::LoadCommittedDetails& details, | |
| 109 const content::FrameNavigateParams& params) override; | |
| 110 bool OnMessageReceived(const IPC::Message& message, | |
| 111 content::RenderFrameHost* render_frame_host) override; | |
| 112 | |
| 113 protected: | |
| 114 ~AppBannerDataFetcher() override; | |
| 115 | |
| 116 // Return a string describing what type of banner is being created. Used when | |
| 117 // alerting websites that a banner is about to be created. | |
| 118 virtual std::string GetBannerType(); | |
| 119 | |
| 120 // Called after the manager sends a message to the renderer regarding its | |
| 121 // intention to show a prompt. The renderer will send a message back with the | |
| 122 // opportunity to cancel. | |
| 123 void OnBannerPromptReply(content::RenderFrameHost* render_frame_host, | |
| 124 int request_id, | |
| 125 blink::WebAppBannerPromptReply reply, | |
| 126 std::string referrer); | |
| 127 | |
| 128 // Called when the client has prevented a banner from being shown, and is | |
| 129 // now requesting that it be shown later. | |
| 130 void OnRequestShowAppBanner(content::RenderFrameHost* render_frame_host, | |
| 131 int request_id); | |
| 132 | |
| 133 content::WebContents* GetWebContents(); | |
| 134 virtual std::string GetAppIdentifier(); | |
| 135 const GURL& manifest_url() { return manifest_url_; } | |
| 136 const content::Manifest& manifest() { return manifest_; } | |
| 137 void set_app_title(const base::string16& title) { app_title_ = title; } | |
| 138 int event_request_id() { return event_request_id_; } | |
| 139 | |
| 140 // Fetches the icon at the given URL asynchronously, returning |false| if a | |
| 141 // load could not be started. | |
| 142 bool FetchAppIcon(content::WebContents* web_contents, const GURL& url); | |
| 143 | |
| 144 // Records that a banner was shown. The |event_name| corresponds to the RAPPOR | |
| 145 // metric being recorded. | |
| 146 void RecordDidShowBanner(const std::string& event_name); | |
| 147 | |
| 148 private: | |
| 149 // Callbacks for data retrieval. | |
| 150 void OnDidGetManifest(const GURL& manifest_url, | |
| 151 const content::Manifest& manifest); | |
| 152 void OnDidCheckHasServiceWorker(bool has_service_worker); | |
| 153 void OnAppIconFetched(const SkBitmap& bitmap); | |
| 154 | |
| 155 // Called when it is determined that the webapp has fulfilled the initial | |
| 156 // criteria of having a manifest and a service worker. | |
| 157 void OnHasServiceWorker(content::WebContents* web_contents); | |
| 158 | |
| 159 // Returns whether the given web app has already been installed. | |
| 160 virtual bool IsWebAppInstalled(content::BrowserContext* browser_context, | |
| 161 const GURL& start_url); | |
| 162 | |
| 163 // Record that the banner could be shown at this point, if the triggering | |
| 164 // heuristic allowed. | |
| 165 void RecordCouldShowBanner(); | |
| 166 | |
| 167 // Creates a banner for the app using the given icon. | |
| 168 virtual void ShowBanner(const GURL& icon_url, | |
| 169 const SkBitmap* icon, | |
| 170 const base::string16& title, | |
| 171 const std::string& referrer) = 0; | |
| 172 | |
| 173 // Returns whether the banner should be shown. | |
| 174 bool CheckIfShouldShowBanner(); | |
| 175 | |
| 176 // Returns whether the fetcher is active and web contents have not been | |
| 177 // closed. | |
| 178 bool CheckFetcherIsStillAlive(content::WebContents* web_contents); | |
| 179 | |
| 180 // Returns whether the given Manifest is following the requirements to show | |
| 181 // a web app banner. | |
| 182 static bool IsManifestValidForWebApp(const content::Manifest& manifest, | |
| 183 content::WebContents* web_contents, | |
| 184 bool is_debug_mode); | |
| 185 | |
| 186 const base::WeakPtr<Delegate> weak_delegate_; | |
| 187 const int ideal_icon_size_in_dp_; | |
| 188 const int minimum_icon_size_in_dp_; | |
| 189 base::ObserverList<Observer> observer_list_; | |
| 190 bool is_active_; | |
| 191 bool was_canceled_by_page_; | |
| 192 bool page_requested_prompt_; | |
| 193 const bool is_debug_mode_; | |
| 194 ui::PageTransition transition_type_; | |
| 195 int event_request_id_; | |
| 196 GURL app_icon_url_; | |
| 197 std::unique_ptr<SkBitmap> app_icon_; | |
| 198 std::string referrer_; | |
| 199 | |
| 200 GURL validated_url_; | |
| 201 base::string16 app_title_; | |
| 202 | |
| 203 // The URL of the Web app manifest. | |
| 204 GURL manifest_url_; | |
| 205 | |
| 206 content::Manifest manifest_; | |
| 207 | |
| 208 friend struct content::BrowserThread::DeleteOnThread< | |
| 209 content::BrowserThread::UI>; | |
| 210 friend class base::DeleteHelper<AppBannerDataFetcher>; | |
| 211 friend class AppBannerDataFetcherUnitTest; | |
| 212 friend class base::RefCounted<AppBannerDataFetcher>; | |
| 213 DISALLOW_COPY_AND_ASSIGN(AppBannerDataFetcher); | |
| 214 }; | |
| 215 | |
| 216 } // namespace banners | |
| 217 | |
| 218 #endif // CHROME_BROWSER_BANNERS_APP_BANNER_DATA_FETCHER_H_ | |
| OLD | NEW |