1 // Copyright (c) 2012 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_FAVICON_FAVICON_HANDLER_H_
6 #define CHROME_BROWSER_FAVICON_FAVICON_HANDLER_H_
12 #include "base/basictypes.h"
13 #include "base/callback_forward.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/task/cancelable_task_tracker.h"
16 #include "components/favicon/core/favicon_url.h"
17 #include "components/favicon_base/favicon_callback.h"
18 #include "ui/gfx/favicon_size.h"
19 #include "ui/gfx/image/image.h"
27 class RefCountedMemory
;
30 // FaviconHandler works with FaviconDriver to fetch the specific type of
33 // FetchFavicon requests the favicon from the favicon service which in turn
34 // requests the favicon from the history database. At this point
35 // we only know the URL of the page, and not necessarily the url of the
36 // favicon. To ensure we handle reloading stale favicons as well as
37 // reloading a favicon on page reload we always request the favicon from
38 // history regardless of whether the active favicon is valid.
40 // After the navigation two types of events are delivered (which is
41 // first depends upon who is faster): notification from the history
42 // db on our request for the favicon
43 // (OnFaviconDataForInitialURLFromFaviconService), or a message from the
44 // renderer giving us the URL of the favicon for the page (SetFaviconURL).
45 // . If the history db has a valid up to date favicon for the page, we update
46 // the current page and use the favicon.
47 // . When we receive the favicon url if it matches that of the current page
48 // and the current page's favicon is set, we do nothing (everything is
50 // . On the other hand if the database does not know the favicon for url, or
51 // the favicon is out date, or the URL from the renderer does not match that
52 // of the current page we proceed to DownloadFaviconOrAskHistory. Before we
53 // invoke DownloadFaviconOrAskHistory we wait until we've received both
54 // the favicon url and the callback from history. We wait to ensure we
55 // truly know both the favicon url and the state of the database.
57 // DownloadFaviconOrAskHistory does the following:
58 // . If we have a valid favicon, but it is expired we ask the renderer to
59 // download the favicon.
60 // . Otherwise we ask the history database to update the mapping from
61 // page url to favicon url and call us back with the favicon. Remember, it is
62 // possible for the db to already have the favicon, just not the mapping
63 // between page to favicon url. The callback for this is OnFaviconData.
65 // OnFaviconData either updates the favicon of the current page (if the
66 // db knew about the favicon), or requests the renderer to download the
69 // When the renderer downloads favicons, it considers the entire list of
70 // favicon candidates, if |download_largest_favicon_| is true, the largest
71 // favicon will be used, otherwise the one that best matches the preferred size
72 // is chosen (or the first one if there is no preferred size). Once the
73 // matching favicon has been determined, SetFavicon is called which updates
74 // the page's favicon and notifies the database to save the favicon.
76 class FaviconHandler
{
83 FaviconHandler(FaviconClient
* client
,
84 FaviconDriver
* driver
,
86 bool download_largest_icon
);
87 virtual ~FaviconHandler();
89 // Initiates loading the favicon for the specified url.
90 void FetchFavicon(const GURL
& url
);
92 // Message Handler. Must be public, because also called from
93 // PrerenderContents. Collects the |image_urls| list.
94 void OnUpdateFaviconURL(const std::vector
<favicon::FaviconURL
>& candidates
);
96 // Processes the current image_irls_ entry, requesting the image from the
97 // history / download service.
98 void ProcessCurrentUrl();
100 // Message handler for ImageHostMsg_DidDownloadImage. Called when the image
101 // at |image_url| has been downloaded.
102 // |bitmaps| is a list of all the frames of the image at |image_url|.
103 // |original_bitmap_sizes| are the sizes of |bitmaps| before they were resized
104 // to the maximum bitmap size passed to DownloadFavicon().
105 void OnDidDownloadFavicon(
107 const GURL
& image_url
,
108 const std::vector
<SkBitmap
>& bitmaps
,
109 const std::vector
<gfx::Size
>& original_bitmap_sizes
);
112 const std::vector
<favicon::FaviconURL
>& image_urls() const {
117 // These virtual methods make FaviconHandler testable and are overridden by
118 // TestFaviconHandler.
120 // Asks the render to download favicon, returns the request id.
121 virtual int DownloadFavicon(const GURL
& image_url
, int max_bitmap_size
);
123 // Ask the favicon from history
124 virtual void UpdateFaviconMappingAndFetch(
125 const GURL
& page_url
,
126 const GURL
& icon_url
,
127 favicon_base::IconType icon_type
,
128 const favicon_base::FaviconResultsCallback
& callback
,
129 base::CancelableTaskTracker
* tracker
);
131 virtual void GetFaviconFromFaviconService(
132 const GURL
& icon_url
,
133 favicon_base::IconType icon_type
,
134 const favicon_base::FaviconResultsCallback
& callback
,
135 base::CancelableTaskTracker
* tracker
);
137 virtual void GetFaviconForURLFromFaviconService(
138 const GURL
& page_url
,
140 const favicon_base::FaviconResultsCallback
& callback
,
141 base::CancelableTaskTracker
* tracker
);
143 virtual void SetHistoryFavicons(const GURL
& page_url
,
144 const GURL
& icon_url
,
145 favicon_base::IconType icon_type
,
146 const gfx::Image
& image
);
148 // Returns true if the favicon should be saved.
149 virtual bool ShouldSaveFavicon(const GURL
& url
);
151 // Notifies the driver that the favicon for the active entry was updated.
152 // |icon_url_changed| is true if a favicon with a different icon URL has been
153 // selected since the previous call to NotifyFaviconUpdated().
154 virtual void NotifyFaviconUpdated(bool icon_url_changed
);
157 friend class TestFaviconHandler
; // For testing
159 // Represents an in progress download of an image from the renderer.
160 struct DownloadRequest
{
164 DownloadRequest(const GURL
& url
,
165 const GURL
& image_url
,
166 favicon_base::IconType icon_type
);
170 favicon_base::IconType icon_type
;
173 // Used to track a candidate for the favicon.
174 struct FaviconCandidate
{
178 FaviconCandidate(const GURL
& url
,
179 const GURL
& image_url
,
180 const gfx::Image
& image
,
182 favicon_base::IconType icon_type
);
188 favicon_base::IconType icon_type
;
191 // Get the maximal icon size in pixels for a icon of type |icon_type| for the
193 static int GetMaximalIconSize(favicon_base::IconType icon_type
);
195 // See description above class for details.
196 void OnFaviconDataForInitialURLFromFaviconService(const std::vector
<
197 favicon_base::FaviconRawBitmapResult
>& favicon_bitmap_results
);
199 // If the favicon has expired, asks the renderer to download the favicon.
200 // Otherwise asks history to update the mapping between page url and icon
201 // url with a callback to OnFaviconData when done.
202 void DownloadFaviconOrAskFaviconService(const GURL
& page_url
,
203 const GURL
& icon_url
,
204 favicon_base::IconType icon_type
);
206 // See description above class for details.
207 void OnFaviconData(const std::vector
<favicon_base::FaviconRawBitmapResult
>&
208 favicon_bitmap_results
);
210 // Schedules a download for the specified entry. This adds the request to
211 // download_requests_.
212 int ScheduleDownload(const GURL
& url
,
213 const GURL
& image_url
,
214 favicon_base::IconType icon_type
);
216 // Updates |favicon_candidate_| and returns true if it is an exact match.
217 bool UpdateFaviconCandidate(const GURL
& url
,
218 const GURL
& image_url
,
219 const gfx::Image
& image
,
221 favicon_base::IconType icon_type
);
223 // Sets the image data for the favicon.
224 void SetFavicon(const GURL
& url
,
225 const GURL
& icon_url
,
226 const gfx::Image
& image
,
227 favicon_base::IconType icon_type
);
229 // Sets the favicon's data.
230 void SetFaviconOnActivePage(const std::vector
<
231 favicon_base::FaviconRawBitmapResult
>& favicon_bitmap_results
);
232 void SetFaviconOnActivePage(const GURL
& icon_url
, const gfx::Image
& image
);
234 // Return the current candidate if any.
235 favicon::FaviconURL
* current_candidate() {
236 return (!image_urls_
.empty()) ? &image_urls_
.front() : NULL
;
239 // Returns whether the page's url changed since the favicon was requested.
240 bool PageChangedSinceFaviconWasRequested();
242 // Returns the preferred size of the image. 0 means no preference (any size
244 int preferred_icon_size() const {
245 if (download_largest_icon_
)
247 return icon_types_
== favicon_base::FAVICON
? gfx::kFaviconSize
: 0;
250 // Sorts the entries in |image_urls_| by icon size in descending order.
251 // Additionally removes any entries whose sizes are all greater than the max
253 void SortAndPruneImageUrls();
255 // Used for FaviconService requests.
256 base::CancelableTaskTracker cancelable_task_tracker_
;
258 // URL of the page we're requesting the favicon for.
261 // Whether we got the initial response for the favicon back from the renderer.
262 bool got_favicon_from_history_
;
264 // Whether the favicon is out of date or the favicon data in
265 // |history_results_| is known to be incomplete. If true, it means history
266 // knows about the favicon, but we need to download the favicon because the
267 // icon has expired or the data in the database is incomplete.
268 bool favicon_expired_or_incomplete_
;
270 // Requests to the renderer to download favicons.
271 typedef std::map
<int, DownloadRequest
> DownloadRequests
;
272 DownloadRequests download_requests_
;
274 // The combination of the supported icon types.
275 const int icon_types_
;
277 // Whether the largest icon should be downloaded.
278 const bool download_largest_icon_
;
280 // The prioritized favicon candidates from the page back from the renderer.
281 std::vector
<favicon::FaviconURL
> image_urls_
;
283 // The FaviconRawBitmapResults from history.
284 std::vector
<favicon_base::FaviconRawBitmapResult
> history_results_
;
286 // The client which implements embedder-specific Favicon operations.
287 FaviconClient
* client_
; // weak
289 // This handler's driver.
290 FaviconDriver
* driver_
; // weak
292 // Best image we've seen so far. As images are downloaded from the page they
293 // are stored here. When there is an exact match, or no more images are
294 // available the favicon service and the current page are updated (assuming
295 // the image is for a favicon).
296 FaviconCandidate best_favicon_candidate_
;
298 DISALLOW_COPY_AND_ASSIGN(FaviconHandler
);
301 #endif // CHROME_BROWSER_FAVICON_FAVICON_HANDLER_H_