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