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 "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"
25 } // namespace infobars
28 class AppBannerDataFetcher
;
30 // Fetches data required to show a web app banner for the URL currently shown by
32 class AppBannerDataFetcher
33 : public base::RefCounted
<AppBannerDataFetcher
>,
34 public chrome::BitmapFetcherDelegate
,
35 public content::WebContentsObserver
{
39 virtual void OnDecidedWhetherToShow(AppBannerDataFetcher
* fetcher
,
41 virtual void OnFetcherDestroyed(AppBannerDataFetcher
* fetcher
) = 0;
46 // Called when no valid manifest was found. Returns |true| if the fetcher
47 // needs to remain active and wait for a callback.
48 virtual bool OnInvalidManifest(AppBannerDataFetcher
* fetcher
) = 0;
51 // Returns the current time.
52 static base::Time
GetCurrentTime();
54 // Fast-forwards the current time for testing.
55 static void SetTimeDeltaForTesting(int days
);
57 AppBannerDataFetcher(content::WebContents
* web_contents
,
58 base::WeakPtr
<Delegate
> weak_delegate
,
61 // Begins creating a banner for the URL being displayed by the Delegate's
63 void Start(const GURL
& validated_url
);
65 // Stops the pipeline when any asynchronous calls return.
68 // Replaces the WebContents that is being observed.
69 void ReplaceWebContents(content::WebContents
* web_contents
);
71 // Adds an observer to the list.
72 void AddObserverForTesting(Observer
* observer
);
74 // Removes an observer from the list.
75 void RemoveObserverForTesting(Observer
* observer
);
77 // Returns whether or not the pipeline has been stopped.
78 bool is_active() { return is_active_
; }
80 // Returns the URL that kicked off the banner data retrieval.
81 const GURL
& validated_url() { return validated_url_
; }
83 // WebContentsObserver overrides.
84 void WebContentsDestroyed() override
;
85 void DidNavigateMainFrame(
86 const content::LoadCommittedDetails
& details
,
87 const content::FrameNavigateParams
& params
) override
;
88 bool OnMessageReceived(const IPC::Message
& message
,
89 content::RenderFrameHost
* render_frame_host
) override
;
92 ~AppBannerDataFetcher() override
;
94 // Return a string describing what type of banner is being created. Used when
95 // alerting websites that a banner is about to be created.
96 virtual std::string
GetBannerType();
98 // Called after the manager sends a message to the renderer regarding its
99 // intention to show a prompt. The renderer will send a message back with the
100 // opportunity to cancel.
101 void OnBannerPromptReply(content::RenderFrameHost
* render_frame_host
,
103 blink::WebAppBannerPromptReply reply
);
105 content::WebContents
* GetWebContents();
106 virtual std::string
GetAppIdentifier();
107 const content::Manifest
& web_app_data() { return web_app_data_
; }
108 void set_app_title(const base::string16
& title
) { app_title_
= title
; }
110 // Fetches the icon at the given URL asynchronously, returning |false| if a
111 // load could not be started.
112 bool FetchIcon(const GURL
& image_url
);
114 // Creates a banner for the app using the given |icon|.
115 virtual infobars::InfoBar
* CreateBanner(const SkBitmap
* icon
,
116 const base::string16
& title
);
118 // Records that a banner was shown. The |event_name| corresponds to the RAPPOR
119 // metric being recorded.
120 void RecordDidShowBanner(const std::string
& event_name
);
123 // Callbacks for data retrieval.
124 void OnDidGetManifest(const content::Manifest
& manifest
);
125 void OnDidCheckHasServiceWorker(bool has_service_worker
);
126 void OnFetchComplete(const GURL
& url
, const SkBitmap
* icon
) override
;
128 // Shows a banner for the app, if the given |icon| is valid.
129 virtual void ShowBanner(const SkBitmap
* icon
);
131 // Record that the banner could be shown at this point, if the triggering
132 // heuristic allowed.
133 void RecordCouldShowBanner();
135 // Returns whether the banner should be shown.
136 bool CheckIfShouldShowBanner();
138 // Returns whether the given Manifest is following the requirements to show
140 static bool IsManifestValid(const content::Manifest
& manifest
);
142 const int ideal_icon_size_
;
143 const base::WeakPtr
<Delegate
> weak_delegate_
;
144 ObserverList
<Observer
> observer_list_
;
146 scoped_ptr
<chrome::BitmapFetcher
> bitmap_fetcher_
;
147 scoped_ptr
<SkBitmap
> app_icon_
;
150 base::string16 app_title_
;
151 content::Manifest web_app_data_
;
153 friend class AppBannerDataFetcherUnitTest
;
154 friend class base::RefCounted
<AppBannerDataFetcher
>;
155 DISALLOW_COPY_AND_ASSIGN(AppBannerDataFetcher
);
158 } // namespace banners
160 #endif // CHROME_BROWSER_BANNERS_APP_BANNER_DATA_FETCHER_H_