Roll ANGLE cc54ab3..c5b2ba5
[chromium-blink-merge.git] / components / favicon / core / favicon_handler.h
blob3fee8be7cb933e757619127df7b7830695c3e6d1
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 COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_
6 #define COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_
8 #include <map>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/callback_forward.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/task/cancelable_task_tracker.h"
15 #include "components/favicon/core/favicon_url.h"
16 #include "components/favicon_base/favicon_callback.h"
17 #include "ui/gfx/favicon_size.h"
18 #include "ui/gfx/image/image.h"
19 #include "url/gurl.h"
21 class SkBitmap;
23 namespace base {
24 class RefCountedMemory;
27 namespace favicon {
29 class FaviconDriver;
30 class FaviconService;
31 class TestFaviconHandler;
33 // FaviconHandler works with FaviconDriver to fetch the specific type of
34 // favicon.
36 // FetchFavicon requests the favicon from the favicon service which in turn
37 // requests the favicon from the history database. At this point
38 // we only know the URL of the page, and not necessarily the url of the
39 // favicon. To ensure we handle reloading stale favicons as well as
40 // reloading a favicon on page reload we always request the favicon from
41 // history regardless of whether the active favicon is valid.
43 // After the navigation two types of events are delivered (which is
44 // first depends upon who is faster): notification from the history
45 // db on our request for the favicon
46 // (OnFaviconDataForInitialURLFromFaviconService), or a message from the
47 // renderer giving us the URL of the favicon for the page (SetFaviconURL).
48 // . If the history db has a valid up to date favicon for the page, we update
49 // the current page and use the favicon.
50 // . When we receive the favicon url if it matches that of the current page
51 // and the current page's favicon is set, we do nothing (everything is
52 // ok).
53 // . On the other hand if the database does not know the favicon for url, or
54 // the favicon is out date, or the URL from the renderer does not match that
55 // of the current page we proceed to DownloadFaviconOrAskHistory. Before we
56 // invoke DownloadFaviconOrAskHistory we wait until we've received both
57 // the favicon url and the callback from history. We wait to ensure we
58 // truly know both the favicon url and the state of the database.
60 // DownloadFaviconOrAskHistory does the following:
61 // . If we have a valid favicon, but it is expired we ask the renderer to
62 // download the favicon.
63 // . Otherwise we ask the history database to update the mapping from
64 // page url to favicon url and call us back with the favicon. Remember, it is
65 // possible for the db to already have the favicon, just not the mapping
66 // between page to favicon url. The callback for this is OnFaviconData.
68 // OnFaviconData either updates the favicon of the current page (if the
69 // db knew about the favicon), or requests the renderer to download the
70 // favicon.
72 // When the renderer downloads favicons, it considers the entire list of
73 // favicon candidates, if |download_largest_favicon_| is true, the largest
74 // favicon will be used, otherwise the one that best matches the preferred size
75 // is chosen (or the first one if there is no preferred size). Once the
76 // matching favicon has been determined, SetFavicon is called which updates
77 // the page's favicon and notifies the database to save the favicon.
79 class FaviconHandler {
80 public:
81 enum Type { FAVICON, TOUCH, LARGE };
83 FaviconHandler(FaviconService* service,
84 FaviconDriver* driver,
85 Type handler_type,
86 bool download_largest_icon);
87 virtual ~FaviconHandler();
89 // Returns the bit mask of favicon_base::IconType based on the handler's type.
90 static int GetIconTypesFromHandlerType(Type icon_type);
92 // Initiates loading the favicon for the specified url.
93 void FetchFavicon(const GURL& url);
95 // Message Handler. Must be public, because also called from
96 // PrerenderContents. Collects the |image_urls| list.
97 void OnUpdateFaviconURL(const std::vector<favicon::FaviconURL>& candidates);
99 // Processes the current image_urls_ entry, requesting the image from the
100 // history / download service.
101 void ProcessCurrentUrl();
103 // Message handler for ImageHostMsg_DidDownloadImage. Called when the image
104 // at |image_url| has been downloaded.
105 // |bitmaps| is a list of all the frames of the image at |image_url|.
106 // |original_bitmap_sizes| are the sizes of |bitmaps| before they were resized
107 // to the maximum bitmap size passed to DownloadFavicon().
108 void OnDidDownloadFavicon(
109 int id,
110 const GURL& image_url,
111 const std::vector<SkBitmap>& bitmaps,
112 const std::vector<gfx::Size>& original_bitmap_sizes);
114 // For testing.
115 const std::vector<favicon::FaviconURL>& image_urls() const {
116 return image_urls_;
119 // Returns whether the handler is waiting for a download to complete or for
120 // data from the FaviconService. Reserved for testing.
121 bool HasPendingTasksForTest();
123 protected:
124 // These virtual methods make FaviconHandler testable and are overridden by
125 // TestFaviconHandler.
127 // Asks the render to download favicon, returns the request id.
128 virtual int DownloadFavicon(const GURL& image_url, int max_bitmap_size);
130 // Ask the favicon from history
131 virtual void UpdateFaviconMappingAndFetch(
132 const GURL& page_url,
133 const GURL& icon_url,
134 favicon_base::IconType icon_type,
135 const favicon_base::FaviconResultsCallback& callback,
136 base::CancelableTaskTracker* tracker);
138 virtual void GetFaviconFromFaviconService(
139 const GURL& icon_url,
140 favicon_base::IconType icon_type,
141 const favicon_base::FaviconResultsCallback& callback,
142 base::CancelableTaskTracker* tracker);
144 virtual void GetFaviconForURLFromFaviconService(
145 const GURL& page_url,
146 int icon_types,
147 const favicon_base::FaviconResultsCallback& callback,
148 base::CancelableTaskTracker* tracker);
150 virtual void SetHistoryFavicons(const GURL& page_url,
151 const GURL& icon_url,
152 favicon_base::IconType icon_type,
153 const gfx::Image& image);
155 // Returns true if the favicon should be saved.
156 virtual bool ShouldSaveFavicon(const GURL& url);
158 private:
159 // For testing:
160 friend class TestFaviconHandler;
162 // Represents an in progress download of an image from the renderer.
163 struct DownloadRequest {
164 DownloadRequest();
165 ~DownloadRequest();
167 DownloadRequest(const GURL& url,
168 const GURL& image_url,
169 favicon_base::IconType icon_type);
171 GURL url;
172 GURL image_url;
173 favicon_base::IconType icon_type;
176 // Used to track a candidate for the favicon.
177 struct FaviconCandidate {
178 FaviconCandidate();
179 ~FaviconCandidate();
181 FaviconCandidate(const GURL& url,
182 const GURL& image_url,
183 const gfx::Image& image,
184 float score,
185 favicon_base::IconType icon_type);
187 GURL url;
188 GURL image_url;
189 gfx::Image image;
190 float score;
191 favicon_base::IconType icon_type;
194 // Get the maximal icon size in pixels for a icon of type |icon_type| for the
195 // current platform.
196 static int GetMaximalIconSize(favicon_base::IconType icon_type);
198 // See description above class for details.
199 void OnFaviconDataForInitialURLFromFaviconService(const std::vector<
200 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results);
202 // If the favicon has expired, asks the renderer to download the favicon.
203 // Otherwise asks history to update the mapping between page url and icon
204 // url with a callback to OnFaviconData when done.
205 void DownloadFaviconOrAskFaviconService(const GURL& page_url,
206 const GURL& icon_url,
207 favicon_base::IconType icon_type);
209 // See description above class for details.
210 void OnFaviconData(const std::vector<favicon_base::FaviconRawBitmapResult>&
211 favicon_bitmap_results);
213 // Schedules a download for the specified entry. This adds the request to
214 // download_requests_.
215 void ScheduleDownload(const GURL& url,
216 const GURL& image_url,
217 favicon_base::IconType icon_type);
219 // Updates |favicon_candidate_| and returns true if it is an exact match.
220 bool UpdateFaviconCandidate(const GURL& url,
221 const GURL& image_url,
222 const gfx::Image& image,
223 float score,
224 favicon_base::IconType icon_type);
226 // Sets the image data for the favicon.
227 void SetFavicon(const GURL& url,
228 const GURL& icon_url,
229 const gfx::Image& image,
230 favicon_base::IconType icon_type);
232 // Notifies |driver_| favicon available. See
233 // FaviconDriver::NotifyFaviconAvailable() for |is_active_favicon| in detail.
234 void NotifyFaviconAvailable(
235 const std::vector<favicon_base::FaviconRawBitmapResult>&
236 favicon_bitmap_results);
237 void NotifyFaviconAvailable(const GURL& icon_url,
238 const gfx::Image& image);
240 // Return the current candidate if any.
241 favicon::FaviconURL* current_candidate() {
242 return (!image_urls_.empty()) ? &image_urls_.front() : NULL;
245 // Returns whether the page's url changed since the favicon was requested.
246 bool PageChangedSinceFaviconWasRequested();
248 // Returns the preferred size of the image. 0 means no preference (any size
249 // will do).
250 int preferred_icon_size() const {
251 if (download_largest_icon_)
252 return 0;
253 return handler_type_ == FAVICON ? gfx::kFaviconSize : 0;
256 // Sorts the entries in |image_urls_| by icon size in descending order.
257 // Additionally removes any entries whose sizes are all greater than the max
258 // allowed size.
259 void SortAndPruneImageUrls();
261 // Used for FaviconService requests.
262 base::CancelableTaskTracker cancelable_task_tracker_;
264 // URL of the page we're requesting the favicon for.
265 GURL url_;
267 // Whether we got data back for the initial request to the FaviconService.
268 bool got_favicon_from_history_;
270 // Whether the favicon is out of date or the favicon data in
271 // |history_results_| is known to be incomplete. If true, it means history
272 // knows about the favicon, but we need to download the favicon because the
273 // icon has expired or the data in the database is incomplete.
274 bool favicon_expired_or_incomplete_;
276 // Requests to the renderer to download favicons.
277 typedef std::map<int, DownloadRequest> DownloadRequests;
278 DownloadRequests download_requests_;
280 // The type of the current handler.
281 const Type handler_type_;
283 // The combination of the supported icon types.
284 const int icon_types_;
286 // Whether the largest icon should be downloaded.
287 const bool download_largest_icon_;
289 // The prioritized favicon candidates from the page back from the renderer.
290 std::vector<favicon::FaviconURL> image_urls_;
292 // The FaviconRawBitmapResults from history.
293 std::vector<favicon_base::FaviconRawBitmapResult> history_results_;
295 // The FaviconService which implements favicon operations. May be null during
296 // testing.
297 FaviconService* service_;
299 // This handler's driver, owns this object.
300 FaviconDriver* driver_;
302 // Best image we've seen so far. As images are downloaded from the page they
303 // are stored here. When there is an exact match, or no more images are
304 // available the favicon service and the current page are updated (assuming
305 // the image is for a favicon).
306 FaviconCandidate best_favicon_candidate_;
308 DISALLOW_COPY_AND_ASSIGN(FaviconHandler);
311 } // namespace favicon
313 #endif // COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_