OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ | 5 #ifndef COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ |
6 #define COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ | 6 #define COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 // . Otherwise we ask the history database to update the mapping from | 61 // . Otherwise we ask the history database to update the mapping from |
62 // page url to favicon url and call us back with the favicon. Remember, it is | 62 // page url to favicon url and call us back with the favicon. Remember, it is |
63 // possible for the db to already have the favicon, just not the mapping | 63 // possible for the db to already have the favicon, just not the mapping |
64 // between page to favicon url. The callback for this is OnFaviconData. | 64 // between page to favicon url. The callback for this is OnFaviconData. |
65 // | 65 // |
66 // OnFaviconData either updates the favicon of the current page (if the | 66 // OnFaviconData either updates the favicon of the current page (if the |
67 // db knew about the favicon), or requests the renderer to download the | 67 // db knew about the favicon), or requests the renderer to download the |
68 // favicon. | 68 // favicon. |
69 // | 69 // |
70 // When the renderer downloads favicons, it considers the entire list of | 70 // When the renderer downloads favicons, it considers the entire list of |
71 // favicon candidates, if |download_largest_favicon_| is true, the largest | 71 // favicon candidates. Among those, the one that matches |target_spec_| |
72 // favicon will be used, otherwise the one that best matches the preferred size | 72 // is chosen first. Once the matching favicon has been determined, SetFavicon |
73 // is chosen (or the first one if there is no preferred size). Once the | 73 // is called which updates the page's favicon and notifies the database to save |
74 // matching favicon has been determined, SetFavicon is called which updates | 74 // the favicon. |
75 // the page's favicon and notifies the database to save the favicon. | |
76 | 75 |
77 class FaviconHandler { | 76 class FaviconHandler { |
78 public: | 77 public: |
| 78 class TargetSpec { |
| 79 public: |
| 80 static TargetSpec ForLargest(); |
| 81 static TargetSpec For16x16Dips(); |
| 82 |
| 83 TargetSpec(const TargetSpec&); |
| 84 ~TargetSpec(); |
| 85 |
| 86 // Returns the target sizes to be fetched, sorted in ascending order. |
| 87 // FaviconHandler will ensure a bitmap for each pixel sizes |
| 88 // either by searching among candidates for an exact match, or by resampling |
| 89 // the closest match. |
| 90 std::vector<int> GetPixelSizes() const; |
| 91 |
| 92 // Returns the maximum value instead of all. |
| 93 int GetMaxPixelSize() const; |
| 94 |
| 95 // Returns whether the spec is requiring the best-matching bitmap only. |
| 96 bool WantsBestBitmapOnly() const; |
| 97 |
| 98 // Compares two sizes in pixels, i.e. whether |size1| is a better match than |
| 99 // |size2| for |pixel_size|. |
| 100 bool IsBetterMatch(const gfx::Size& size1, const gfx::Size& size2) const; |
| 101 |
| 102 // Checks if |bitmap_results| contains all bitmaps required by this |
| 103 // specification. |
| 104 bool HasCompleteResult( |
| 105 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) |
| 106 const; |
| 107 |
| 108 gfx::ImageSkia CreateImageSkia( |
| 109 const std::vector<SkBitmap>& bitmaps, |
| 110 const std::vector<gfx::Size>& original_bitmap_sizes) const; |
| 111 |
| 112 gfx::Image CreateImageFromCache( |
| 113 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) |
| 114 const; |
| 115 |
| 116 private: |
| 117 TargetSpec(); |
| 118 |
| 119 // Map representing the target sizes to be fetched and their corresponding |
| 120 // scale factors. |
| 121 std::map<int, float> pixel_sizes_; |
| 122 |
| 123 // If true, FaviconHandler will resample candidates whenever needed to |
| 124 // ensure that bitmaps for all |pixel_sizes| are produced. |
| 125 bool ensure_exact_size_; |
| 126 }; |
| 127 |
79 class Delegate { | 128 class Delegate { |
80 public: | 129 public: |
81 // Mimics WebContents::ImageDownloadCallback. | 130 // Mimics WebContents::ImageDownloadCallback. |
82 typedef base::Callback<void( | 131 typedef base::Callback<void( |
83 int id, | 132 int id, |
84 int status_code, | 133 int status_code, |
85 const GURL& image_url, | 134 const GURL& image_url, |
86 const std::vector<SkBitmap>& bitmaps, | 135 const std::vector<SkBitmap>& bitmaps, |
87 const std::vector<gfx::Size>& original_bitmap_sizes)> | 136 const std::vector<gfx::Size>& original_bitmap_sizes)> |
88 ImageDownloadCallback; | 137 ImageDownloadCallback; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 favicon_base::IconType icon_type; | 204 favicon_base::IconType icon_type; |
156 }; | 205 }; |
157 | 206 |
158 // Used to track a candidate for the favicon. | 207 // Used to track a candidate for the favicon. |
159 struct FaviconCandidate { | 208 struct FaviconCandidate { |
160 FaviconCandidate(); | 209 FaviconCandidate(); |
161 ~FaviconCandidate(); | 210 ~FaviconCandidate(); |
162 | 211 |
163 FaviconCandidate(const GURL& image_url, | 212 FaviconCandidate(const GURL& image_url, |
164 const gfx::Image& image, | 213 const gfx::Image& image, |
165 float score, | |
166 favicon_base::IconType icon_type); | 214 favicon_base::IconType icon_type); |
167 | 215 |
168 GURL image_url; | 216 GURL image_url; |
169 gfx::Image image; | 217 gfx::Image image; |
170 float score; | |
171 favicon_base::IconType icon_type; | 218 favicon_base::IconType icon_type; |
172 }; | 219 }; |
173 | 220 |
174 // Returns the bit mask of favicon_base::IconType based on the handler's type. | 221 // Returns the bit mask of favicon_base::IconType based on the handler's type. |
175 static int GetIconTypesFromHandlerType( | 222 static int GetIconTypesFromHandlerType( |
176 FaviconDriverObserver::NotificationIconType handler_type); | 223 FaviconDriverObserver::NotificationIconType handler_type); |
177 | 224 |
178 // Called when the history request for favicon data mapped to |url_| has | 225 // Called when the history request for favicon data mapped to |url_| has |
179 // completed and the renderer has told us the icon URLs used by |url_| | 226 // completed and the renderer has told us the icon URLs used by |url_| |
180 void OnGotInitialHistoryDataAndIconURLCandidates(); | 227 void OnGotInitialHistoryDataAndIconURLCandidates(); |
(...skipping 20 matching lines...) Expand all Loading... |
201 // Triggered when a download of an image has finished. | 248 // Triggered when a download of an image has finished. |
202 void OnDidDownloadFavicon( | 249 void OnDidDownloadFavicon( |
203 int id, | 250 int id, |
204 int http_status_code, | 251 int http_status_code, |
205 const GURL& image_url, | 252 const GURL& image_url, |
206 const std::vector<SkBitmap>& bitmaps, | 253 const std::vector<SkBitmap>& bitmaps, |
207 const std::vector<gfx::Size>& original_bitmap_sizes); | 254 const std::vector<gfx::Size>& original_bitmap_sizes); |
208 | 255 |
209 bool ShouldSaveFavicon(); | 256 bool ShouldSaveFavicon(); |
210 | 257 |
211 // Updates |favicon_candidate_| and returns true if it is an exact match. | 258 // Updates |favicon_candidate_| and returns true if no more candidates should |
| 259 // be processed (e.g. an exact match was found). |
212 bool UpdateFaviconCandidate(const GURL& image_url, | 260 bool UpdateFaviconCandidate(const GURL& image_url, |
213 const gfx::Image& image, | 261 const gfx::Image& image, |
214 float score, | |
215 favicon_base::IconType icon_type); | 262 favicon_base::IconType icon_type); |
216 | 263 |
217 // Sets the image data for the favicon. | 264 // Sets the image data for the favicon. |
218 void SetFavicon(const GURL& icon_url, | 265 void SetFavicon(const GURL& icon_url, |
219 const gfx::Image& image, | 266 const gfx::Image& image, |
220 favicon_base::IconType icon_type); | 267 favicon_base::IconType icon_type); |
221 | 268 |
222 // Notifies |driver_| that FaviconHandler found an icon which matches the | 269 // Notifies |driver_| that FaviconHandler found an icon which matches the |
223 // |handler_type_| criteria. NotifyFaviconUpdated() can be called multiple | 270 // |handler_type_| criteria. NotifyFaviconUpdated() can be called multiple |
224 // times for the same page if: | 271 // times for the same page if: |
225 // - a better match is found for |handler_type_| (e.g. newer bitmap data) | 272 // - a better match is found for |handler_type_| (e.g. newer bitmap data) |
226 // - Javascript changes the page's icon URLs. | 273 // - Javascript changes the page's icon URLs. |
227 void NotifyFaviconUpdated( | 274 void NotifyFaviconUpdated( |
228 const std::vector<favicon_base::FaviconRawBitmapResult>& | 275 const std::vector<favicon_base::FaviconRawBitmapResult>& |
229 favicon_bitmap_results); | 276 favicon_bitmap_results); |
230 void NotifyFaviconUpdated(const GURL& icon_url, | 277 void NotifyFaviconUpdated(const GURL& icon_url, |
231 favicon_base::IconType icon_type, | 278 favicon_base::IconType icon_type, |
232 const gfx::Image& image); | 279 const gfx::Image& image); |
233 | 280 |
234 // Return the current candidate if any. | 281 // Return the current candidate if any. |
235 favicon::FaviconURL* current_candidate() { | 282 favicon::FaviconURL* current_candidate() { |
236 return current_candidate_index_ < image_urls_.size() | 283 return current_candidate_index_ < image_urls_.size() |
237 ? &image_urls_[current_candidate_index_] | 284 ? &image_urls_[current_candidate_index_] |
238 : nullptr; | 285 : nullptr; |
239 } | 286 } |
240 | 287 |
241 // Returns the preferred size of the image. 0 means no preference (any size | |
242 // will do). | |
243 int preferred_icon_size() const { | |
244 return download_largest_icon_ ? 0 : gfx::kFaviconSize; | |
245 } | |
246 | |
247 // Used for FaviconService requests. | 288 // Used for FaviconService requests. |
248 base::CancelableTaskTracker cancelable_task_tracker_; | 289 base::CancelableTaskTracker cancelable_task_tracker_; |
249 | 290 |
250 FaviconDriverObserver::NotificationIconType handler_type_; | 291 FaviconDriverObserver::NotificationIconType handler_type_; |
251 | 292 |
252 // URL of the page we're requesting the favicon for. | 293 // URL of the page we're requesting the favicon for. |
253 GURL url_; | 294 GURL url_; |
254 | 295 |
255 // Whether we got data back for the initial request to the FaviconService. | 296 // Whether we got data back for the initial request to the FaviconService. |
256 bool got_favicon_from_history_; | 297 bool got_favicon_from_history_; |
(...skipping 10 matching lines...) Expand all Loading... |
267 // |image_urls_| one by one. | 308 // |image_urls_| one by one. |
268 bool redownload_icons_; | 309 bool redownload_icons_; |
269 | 310 |
270 // Requests to the renderer to download favicons. | 311 // Requests to the renderer to download favicons. |
271 typedef std::map<int, DownloadRequest> DownloadRequests; | 312 typedef std::map<int, DownloadRequest> DownloadRequests; |
272 DownloadRequests download_requests_; | 313 DownloadRequests download_requests_; |
273 | 314 |
274 // The combination of the supported icon types. | 315 // The combination of the supported icon types. |
275 const int icon_types_; | 316 const int icon_types_; |
276 | 317 |
277 // Whether the largest icon should be downloaded. | 318 // Desired icon size. |
278 const bool download_largest_icon_; | 319 const TargetSpec target_spec_; |
279 | 320 |
280 // The prioritized favicon candidates from the page back from the renderer. | 321 // The prioritized favicon candidates from the page back from the renderer. |
281 std::vector<favicon::FaviconURL> image_urls_; | 322 std::vector<favicon::FaviconURL> image_urls_; |
282 | 323 |
283 // The icon URL and the icon type of the favicon in the most recent | 324 // The icon URL and the icon type of the favicon in the most recent |
284 // FaviconDriver::OnFaviconAvailable() notification. | 325 // FaviconDriver::OnFaviconAvailable() notification. |
285 GURL notification_icon_url_; | 326 GURL notification_icon_url_; |
286 favicon_base::IconType notification_icon_type_; | 327 favicon_base::IconType notification_icon_type_; |
287 | 328 |
288 // The FaviconService which implements favicon operations. May be null during | 329 // The FaviconService which implements favicon operations. May be null during |
(...skipping 14 matching lines...) Expand all Loading... |
303 FaviconCandidate best_favicon_candidate_; | 344 FaviconCandidate best_favicon_candidate_; |
304 | 345 |
305 base::WeakPtrFactory<FaviconHandler> weak_ptr_factory_; | 346 base::WeakPtrFactory<FaviconHandler> weak_ptr_factory_; |
306 | 347 |
307 DISALLOW_COPY_AND_ASSIGN(FaviconHandler); | 348 DISALLOW_COPY_AND_ASSIGN(FaviconHandler); |
308 }; | 349 }; |
309 | 350 |
310 } // namespace favicon | 351 } // namespace favicon |
311 | 352 |
312 #endif // COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ | 353 #endif // COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ |
OLD | NEW |