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);
152 friend class TestFaviconHandler; // For testing
154 // Represents an in progress download of an image from the renderer.
155 struct DownloadRequest {
159 DownloadRequest(const GURL& url,
160 const GURL& image_url,
161 favicon_base::IconType icon_type);
165 favicon_base::IconType icon_type;
168 // Used to track a candidate for the favicon.
169 struct FaviconCandidate {
173 FaviconCandidate(const GURL& url,
174 const GURL& image_url,
175 const gfx::Image& image,
177 favicon_base::IconType icon_type);
183 favicon_base::IconType icon_type;
186 // Get the maximal icon size in pixels for a icon of type |icon_type| for the
188 static int GetMaximalIconSize(favicon_base::IconType icon_type);
190 // See description above class for details.
191 void OnFaviconDataForInitialURLFromFaviconService(const std::vector<
192 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results);
194 // If the favicon has expired, asks the renderer to download the favicon.
195 // Otherwise asks history to update the mapping between page url and icon
196 // url with a callback to OnFaviconData when done.
197 void DownloadFaviconOrAskFaviconService(const GURL& page_url,
198 const GURL& icon_url,
199 favicon_base::IconType icon_type);
201 // See description above class for details.
202 void OnFaviconData(const std::vector<favicon_base::FaviconRawBitmapResult>&
203 favicon_bitmap_results);
205 // Schedules a download for the specified entry. This adds the request to
206 // download_requests_.
207 int ScheduleDownload(const GURL& url,
208 const GURL& image_url,
209 favicon_base::IconType icon_type);
211 // Updates |favicon_candidate_| and returns true if it is an exact match.
212 bool UpdateFaviconCandidate(const GURL& url,
213 const GURL& image_url,
214 const gfx::Image& image,
216 favicon_base::IconType icon_type);
218 // Sets the image data for the favicon.
219 void SetFavicon(const GURL& url,
220 const GURL& icon_url,
221 const gfx::Image& image,
222 favicon_base::IconType icon_type);
224 // Notifies |driver_| favicon available. See
225 // FaviconDriver::NotifyFaviconAvailable() for |is_active_favicon| in detail.
226 void NotifyFaviconAvailable(
227 const std::vector<favicon_base::FaviconRawBitmapResult>&
228 favicon_bitmap_results,
229 bool is_active_favicon);
230 void NotifyFaviconAvailable(const GURL& icon_url,
231 const gfx::Image& image,
232 bool is_active_favicon);
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_