Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / banners / app_banner_data_fetcher.h
blob0fa8d53ccf6ede58dbfd079d5bd150622a93a712
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.
5 #ifndef CHROME_BROWSER_BANNERS_APP_BANNER_DATA_FETCHER_H_
6 #define CHROME_BROWSER_BANNERS_APP_BANNER_DATA_FETCHER_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/observer_list.h"
12 #include "base/time/time.h"
13 #include "chrome/common/web_application_info.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/browser/web_contents_observer.h"
16 #include "content/public/common/manifest.h"
17 #include "third_party/WebKit/public/platform/modules/app_banner/WebAppBannerPromptReply.h"
19 namespace chrome {
20 class BitmapFetcher;
21 } // namespace chrome
23 namespace infobars {
24 class InfoBar;
25 } // namespace infobars
27 namespace banners {
28 class AppBannerDataFetcher;
30 // Fetches data required to show a web app banner for the URL currently shown by
31 // the WebContents.
32 class AppBannerDataFetcher
33 : public base::RefCounted<AppBannerDataFetcher>,
34 public content::WebContentsObserver {
35 public:
36 class Observer {
37 public:
38 virtual void OnDecidedWhetherToShow(AppBannerDataFetcher* fetcher,
39 bool will_show) = 0;
40 virtual void OnFetcherDestroyed(AppBannerDataFetcher* fetcher) = 0;
43 class Delegate {
44 public:
45 // Called to handle a non-web app. Returns |true| if the non-web app can be
46 // handled, and the fetcher needs to remain active and wait for a callback.
47 virtual bool HandleNonWebApp(const std::string& platform,
48 const GURL& url,
49 const std::string& id) = 0;
52 // Returns the current time.
53 static base::Time GetCurrentTime();
55 // Fast-forwards the current time for testing.
56 static void SetTimeDeltaForTesting(int days);
58 AppBannerDataFetcher(content::WebContents* web_contents,
59 base::WeakPtr<Delegate> weak_delegate,
60 int ideal_icon_size_in_dp,
61 int minimum_icon_size_in_dp);
63 // Begins creating a banner for the URL being displayed by the Delegate's
64 // WebContents.
65 void Start(const GURL& validated_url, ui::PageTransition transition_type);
67 // Stops the pipeline when any asynchronous calls return.
68 void Cancel();
70 // Replaces the WebContents that is being observed.
71 void ReplaceWebContents(content::WebContents* web_contents);
73 // Adds an observer to the list.
74 void AddObserverForTesting(Observer* observer);
76 // Removes an observer from the list.
77 void RemoveObserverForTesting(Observer* observer);
79 // Returns whether or not the pipeline has been stopped.
80 bool is_active() { return is_active_; }
82 // Returns whether the beforeinstallprompt Javascript event was canceled.
83 bool was_canceled_by_page() { return was_canceled_by_page_; }
85 // Returns whether the page has validly requested that the banner be shown
86 // by calling prompt() on the beforeinstallprompt Javascript event.
87 bool page_requested_prompt() { return page_requested_prompt_; }
89 // Returns the type of transition which triggered this fetch.
90 ui::PageTransition transition_type() { return transition_type_; }
92 // Returns the URL that kicked off the banner data retrieval.
93 const GURL& validated_url() { return validated_url_; }
95 // WebContentsObserver overrides.
96 void WebContentsDestroyed() override;
97 void DidNavigateMainFrame(
98 const content::LoadCommittedDetails& details,
99 const content::FrameNavigateParams& params) override;
100 bool OnMessageReceived(const IPC::Message& message,
101 content::RenderFrameHost* render_frame_host) override;
103 protected:
104 ~AppBannerDataFetcher() override;
106 // Return a string describing what type of banner is being created. Used when
107 // alerting websites that a banner is about to be created.
108 virtual std::string GetBannerType();
110 // Called after the manager sends a message to the renderer regarding its
111 // intention to show a prompt. The renderer will send a message back with the
112 // opportunity to cancel.
113 void OnBannerPromptReply(content::RenderFrameHost* render_frame_host,
114 int request_id,
115 blink::WebAppBannerPromptReply reply,
116 std::string referrer);
118 // Called when the client has prevented a banner from being shown, and is
119 // now requesting that it be shown later.
120 void OnRequestShowAppBanner(content::RenderFrameHost* render_frame_host,
121 int request_id);
123 content::WebContents* GetWebContents();
124 virtual std::string GetAppIdentifier();
125 const content::Manifest& web_app_data() { return web_app_data_; }
126 void set_app_title(const base::string16& title) { app_title_ = title; }
127 int event_request_id() { return event_request_id_; }
129 // Fetches the icon at the given URL asynchronously, returning |false| if a
130 // load could not be started.
131 bool FetchAppIcon(content::WebContents* web_contents, const GURL& url);
133 // Records that a banner was shown. The |event_name| corresponds to the RAPPOR
134 // metric being recorded.
135 void RecordDidShowBanner(const std::string& event_name);
137 private:
138 // Callbacks for data retrieval.
139 void OnDidGetManifest(const content::Manifest& manifest);
140 void OnDidCheckHasServiceWorker(bool has_service_worker);
141 void OnAppIconFetched(const SkBitmap& bitmap);
143 // Called when it is determined that the webapp has fulfilled the initial
144 // criteria of having a manifest and a service worker.
145 void OnHasServiceWorker(content::WebContents* web_contents);
147 // Returns whether the given web app has already been installed.
148 // Implemented on desktop platforms only.
149 virtual bool IsWebAppInstalled(content::BrowserContext* browser_context,
150 const GURL& start_url);
152 // Record that the banner could be shown at this point, if the triggering
153 // heuristic allowed.
154 void RecordCouldShowBanner();
156 // Creates a banner for the app using the given |icon|.
157 virtual void ShowBanner(const SkBitmap* icon,
158 const base::string16& title,
159 const std::string& referrer) = 0;
161 // Returns whether the banner should be shown.
162 bool CheckIfShouldShowBanner();
164 // Returns whether the fetcher is active and web contents have not been
165 // closed.
166 bool CheckFetcherIsStillAlive(content::WebContents* web_contents);
168 // Returns whether the given Manifest is following the requirements to show
169 // a web app banner.
170 static bool IsManifestValidForWebApp(const content::Manifest& manifest,
171 content::WebContents* web_contents);
173 const base::WeakPtr<Delegate> weak_delegate_;
174 const int ideal_icon_size_in_dp_;
175 const int minimum_icon_size_in_dp_;
176 base::ObserverList<Observer> observer_list_;
177 bool is_active_;
178 bool was_canceled_by_page_;
179 bool page_requested_prompt_;
180 ui::PageTransition transition_type_;
181 int event_request_id_;
182 scoped_ptr<SkBitmap> app_icon_;
183 std::string referrer_;
185 GURL validated_url_;
186 base::string16 app_title_;
187 content::Manifest web_app_data_;
189 friend class AppBannerDataFetcherUnitTest;
190 friend class base::RefCounted<AppBannerDataFetcher>;
191 DISALLOW_COPY_AND_ASSIGN(AppBannerDataFetcher);
194 } // namespace banners
196 #endif // CHROME_BROWSER_BANNERS_APP_BANNER_DATA_FETCHER_H_