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
);
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 the URL that kicked off the banner data retrieval.
84 const GURL
& validated_url() { return validated_url_
; }
86 // WebContentsObserver overrides.
87 void WebContentsDestroyed() override
;
88 void DidNavigateMainFrame(
89 const content::LoadCommittedDetails
& details
,
90 const content::FrameNavigateParams
& params
) override
;
91 bool OnMessageReceived(const IPC::Message
& message
,
92 content::RenderFrameHost
* render_frame_host
) override
;
95 ~AppBannerDataFetcher() override
;
97 // Return a string describing what type of banner is being created. Used when
98 // alerting websites that a banner is about to be created.
99 virtual std::string
GetBannerType();
101 // Called after the manager sends a message to the renderer regarding its
102 // intention to show a prompt. The renderer will send a message back with the
103 // opportunity to cancel.
104 void OnBannerPromptReply(content::RenderFrameHost
* render_frame_host
,
106 blink::WebAppBannerPromptReply reply
);
108 content::WebContents
* GetWebContents();
109 virtual std::string
GetAppIdentifier();
110 const content::Manifest
& web_app_data() { return web_app_data_
; }
111 void set_app_title(const base::string16
& title
) { app_title_
= title
; }
112 int event_request_id() { return event_request_id_
; }
114 // Fetches the icon at the given URL asynchronously, returning |false| if a
115 // load could not be started.
116 bool FetchIcon(const GURL
& image_url
);
118 // Creates a banner for the app using the given |icon|.
119 virtual void ShowBanner(const SkBitmap
* icon
,
120 const base::string16
& title
) = 0;
122 // Records that a banner was shown. The |event_name| corresponds to the RAPPOR
123 // metric being recorded.
124 void RecordDidShowBanner(const std::string
& event_name
);
127 // Callbacks for data retrieval.
128 void OnDidGetManifest(const content::Manifest
& manifest
);
129 void OnDidCheckHasServiceWorker(bool has_service_worker
);
130 void OnFetchComplete(const GURL
& url
, const SkBitmap
* icon
) override
;
132 // Shows a banner for the app, if the given |icon| is valid.
133 virtual void RequestShowBanner(const SkBitmap
* icon
);
135 // Record that the banner could be shown at this point, if the triggering
136 // heuristic allowed.
137 void RecordCouldShowBanner();
139 // Returns whether the banner should be shown.
140 bool CheckIfShouldShowBanner();
142 // Returns whether the fetcher is active and web contents have not been
144 bool CheckFetcherIsStillAlive(content::WebContents
* web_contents
);
146 // Returns whether the given Manifest is following the requirements to show
148 static bool IsManifestValidForWebApp(const content::Manifest
& manifest
,
149 content::WebContents
* web_contents
);
151 const int ideal_icon_size_
;
152 const base::WeakPtr
<Delegate
> weak_delegate_
;
153 base::ObserverList
<Observer
> observer_list_
;
155 int event_request_id_
;
156 scoped_ptr
<chrome::BitmapFetcher
> bitmap_fetcher_
;
157 scoped_ptr
<SkBitmap
> app_icon_
;
160 base::string16 app_title_
;
161 content::Manifest web_app_data_
;
163 friend class AppBannerDataFetcherUnitTest
;
164 friend class base::RefCounted
<AppBannerDataFetcher
>;
165 DISALLOW_COPY_AND_ASSIGN(AppBannerDataFetcher
);
168 } // namespace banners
170 #endif // CHROME_BROWSER_BANNERS_APP_BANNER_DATA_FETCHER_H_