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/browser/bitmap_fetcher/bitmap_fetcher_delegate.h"
14 #include "chrome/common/web_application_info.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/browser/web_contents_observer.h"
17 #include "content/public/common/manifest.h"
18 #include "third_party/WebKit/public/platform/modules/app_banner/WebAppBannerPromptReply.h"
26 } // namespace infobars
29 class AppBannerDataFetcher
;
31 // Fetches data required to show a web app banner for the URL currently shown by
33 class AppBannerDataFetcher
34 : public base::RefCounted
<AppBannerDataFetcher
>,
35 public chrome::BitmapFetcherDelegate
,
36 public content::WebContentsObserver
{
40 virtual void OnDecidedWhetherToShow(AppBannerDataFetcher
* fetcher
,
42 virtual void OnFetcherDestroyed(AppBannerDataFetcher
* fetcher
) = 0;
47 // Called to handle a non-web app. Returns |true| if the non-web app can be
48 // handled, and the fetcher needs to remain active and wait for a callback.
49 virtual bool HandleNonWebApp(const std::string
& platform
,
51 const std::string
& id
) = 0;
54 // Returns the current time.
55 static base::Time
GetCurrentTime();
57 // Fast-forwards the current time for testing.
58 static void SetTimeDeltaForTesting(int days
);
60 AppBannerDataFetcher(content::WebContents
* web_contents
,
61 base::WeakPtr
<Delegate
> weak_delegate
,
64 // Begins creating a banner for the URL being displayed by the Delegate's
66 void Start(const GURL
& validated_url
, ui::PageTransition transition_type
);
68 // Stops the pipeline when any asynchronous calls return.
71 // Replaces the WebContents that is being observed.
72 void ReplaceWebContents(content::WebContents
* web_contents
);
74 // Adds an observer to the list.
75 void AddObserverForTesting(Observer
* observer
);
77 // Removes an observer from the list.
78 void RemoveObserverForTesting(Observer
* observer
);
80 // Returns whether or not the pipeline has been stopped.
81 bool is_active() { return is_active_
; }
83 // Returns whether the beforeinstallprompt Javascript event was canceled.
84 bool was_canceled_by_page() { return was_canceled_by_page_
; }
86 // Returns whether the page has validly requested that the banner be shown
87 // by calling prompt() on the beforeinstallprompt Javascript event.
88 bool page_requested_prompt() { return page_requested_prompt_
; }
90 // Returns the type of transition which triggered this fetch.
91 ui::PageTransition
transition_type() { return transition_type_
; }
93 // Returns the URL that kicked off the banner data retrieval.
94 const GURL
& validated_url() { return validated_url_
; }
96 // WebContentsObserver overrides.
97 void WebContentsDestroyed() override
;
98 void DidNavigateMainFrame(
99 const content::LoadCommittedDetails
& details
,
100 const content::FrameNavigateParams
& params
) override
;
101 bool OnMessageReceived(const IPC::Message
& message
,
102 content::RenderFrameHost
* render_frame_host
) override
;
105 ~AppBannerDataFetcher() override
;
107 // Return a string describing what type of banner is being created. Used when
108 // alerting websites that a banner is about to be created.
109 virtual std::string
GetBannerType();
111 // Called after the manager sends a message to the renderer regarding its
112 // intention to show a prompt. The renderer will send a message back with the
113 // opportunity to cancel.
114 void OnBannerPromptReply(content::RenderFrameHost
* render_frame_host
,
116 blink::WebAppBannerPromptReply reply
);
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
,
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 FetchIcon(const GURL
& image_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
);
138 // Callbacks for data retrieval.
139 void OnDidGetManifest(const content::Manifest
& manifest
);
140 void OnDidCheckHasServiceWorker(bool has_service_worker
);
141 void OnFetchComplete(const GURL
& url
, const SkBitmap
* icon
) override
;
143 // Returns whether the given web app has already been installed.
144 // Implemented on desktop platforms only.
145 virtual bool IsWebAppInstalled(content::BrowserContext
* browser_context
,
146 const GURL
& start_url
);
148 // Shows a banner for the app, if the given |icon| is valid.
149 virtual void RequestShowBanner(const SkBitmap
* icon
);
151 // Record that the banner could be shown at this point, if the triggering
152 // heuristic allowed.
153 void RecordCouldShowBanner();
155 // Creates a banner for the app using the given |icon|.
156 virtual void ShowBanner(const SkBitmap
* icon
,
157 const base::string16
& title
) = 0;
159 // Returns whether the banner should be shown.
160 bool CheckIfShouldShowBanner();
162 // Returns whether the fetcher is active and web contents have not been
164 bool CheckFetcherIsStillAlive(content::WebContents
* web_contents
);
166 // Returns whether the given Manifest is following the requirements to show
168 static bool IsManifestValidForWebApp(const content::Manifest
& manifest
,
169 content::WebContents
* web_contents
);
171 const int ideal_icon_size_
;
172 const base::WeakPtr
<Delegate
> weak_delegate_
;
173 base::ObserverList
<Observer
> observer_list_
;
175 bool was_canceled_by_page_
;
176 bool page_requested_prompt_
;
177 ui::PageTransition transition_type_
;
178 int event_request_id_
;
179 scoped_ptr
<chrome::BitmapFetcher
> bitmap_fetcher_
;
180 scoped_ptr
<SkBitmap
> app_icon_
;
183 base::string16 app_title_
;
184 content::Manifest web_app_data_
;
186 friend class AppBannerDataFetcherUnitTest
;
187 friend class base::RefCounted
<AppBannerDataFetcher
>;
188 DISALLOW_COPY_AND_ASSIGN(AppBannerDataFetcher
);
191 } // namespace banners
193 #endif // CHROME_BROWSER_BANNERS_APP_BANNER_DATA_FETCHER_H_