Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / components / favicon / core / favicon_handler.cc
blobadc0a5c4d721ecb7370b6b915af68a887d945c6e
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 #include "components/favicon/core/favicon_handler.h"
7 #include <algorithm>
8 #include <cmath>
9 #include <vector>
11 #include "base/bind.h"
12 #include "base/bind_helpers.h"
13 #include "base/memory/ref_counted_memory.h"
14 #include "build/build_config.h"
15 #include "components/favicon/core/favicon_driver.h"
16 #include "components/favicon/core/favicon_service.h"
17 #include "components/favicon_base/favicon_util.h"
18 #include "components/favicon_base/select_favicon_frames.h"
19 #include "skia/ext/image_operations.h"
20 #include "ui/gfx/codec/png_codec.h"
21 #include "ui/gfx/image/image_skia.h"
22 #include "ui/gfx/image/image_util.h"
24 namespace favicon {
25 namespace {
27 // Size (along each axis) of a touch icon. This currently corresponds to
28 // the apple touch icon for iPad.
29 const int kTouchIconSize = 144;
31 bool DoUrlAndIconMatch(const FaviconURL& favicon_url,
32 const GURL& url,
33 favicon_base::IconType icon_type) {
34 return favicon_url.icon_url == url && favicon_url.icon_type == icon_type;
37 // Returns true if all of the icon URLs and icon types in |bitmap_results| are
38 // identical and if they match the icon URL and icon type in |favicon_url|.
39 // Returns false if |bitmap_results| is empty.
40 bool DoUrlsAndIconsMatch(
41 const FaviconURL& favicon_url,
42 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) {
43 if (bitmap_results.empty())
44 return false;
46 const favicon_base::IconType icon_type = favicon_url.icon_type;
48 for (const auto& bitmap_result : bitmap_results) {
49 if (favicon_url.icon_url != bitmap_result.icon_url ||
50 icon_type != bitmap_result.icon_type) {
51 return false;
54 return true;
57 std::string UrlWithoutFragment(const GURL& gurl) {
58 GURL::Replacements replacements;
59 replacements.ClearRef();
60 return gurl.ReplaceComponents(replacements).spec();
63 bool UrlMatches(const GURL& gurl_a, const GURL& gurl_b) {
64 return UrlWithoutFragment(gurl_a) == UrlWithoutFragment(gurl_b);
67 // Return true if |bitmap_result| is expired.
68 bool IsExpired(const favicon_base::FaviconRawBitmapResult& bitmap_result) {
69 return bitmap_result.expired;
72 // Return true if |bitmap_result| is valid.
73 bool IsValid(const favicon_base::FaviconRawBitmapResult& bitmap_result) {
74 return bitmap_result.is_valid();
77 // Returns true if at least one of the bitmaps in |bitmap_results| is expired or
78 // if |bitmap_results| is missing favicons for |desired_size_in_dip| and one of
79 // the scale factors in favicon_base::GetFaviconScales().
80 bool HasExpiredOrIncompleteResult(
81 int desired_size_in_dip,
82 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) {
83 // Check if at least one of the bitmaps is expired.
84 std::vector<favicon_base::FaviconRawBitmapResult>::const_iterator it =
85 std::find_if(bitmap_results.begin(), bitmap_results.end(), IsExpired);
86 if (it != bitmap_results.end())
87 return true;
89 // Any favicon size is good if the desired size is 0.
90 if (desired_size_in_dip == 0)
91 return false;
93 // Check if the favicon for at least one of the scale factors is missing.
94 // |bitmap_results| should always be complete for data inserted by
95 // FaviconHandler as the FaviconHandler stores favicons resized to all
96 // of favicon_base::GetFaviconScales() into the history backend.
97 // Examples of when |bitmap_results| can be incomplete:
98 // - Favicons inserted into the history backend by sync.
99 // - Favicons for imported bookmarks.
100 std::vector<gfx::Size> favicon_sizes;
101 for (const auto& bitmap_result : bitmap_results)
102 favicon_sizes.push_back(bitmap_result.pixel_size);
104 std::vector<float> favicon_scales = favicon_base::GetFaviconScales();
105 for (float favicon_scale : favicon_scales) {
106 int edge_size_in_pixel = std::ceil(desired_size_in_dip * favicon_scale);
107 auto it = std::find(favicon_sizes.begin(), favicon_sizes.end(),
108 gfx::Size(edge_size_in_pixel, edge_size_in_pixel));
109 if (it == favicon_sizes.end())
110 return true;
112 return false;
115 // Returns true if at least one of |bitmap_results| is valid.
116 bool HasValidResult(
117 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) {
118 return std::find_if(bitmap_results.begin(), bitmap_results.end(), IsValid) !=
119 bitmap_results.end();
122 // Returns the index of the entry with the largest area.
123 int GetLargestSizeIndex(const std::vector<gfx::Size>& sizes) {
124 DCHECK(!sizes.empty());
125 size_t ret = 0;
126 for (size_t i = 1; i < sizes.size(); ++i) {
127 if (sizes[ret].GetArea() < sizes[i].GetArea())
128 ret = i;
130 return static_cast<int>(ret);
133 // Return the index of a size which is same as the given |size|, -1 returned if
134 // there is no such bitmap.
135 int GetIndexBySize(const std::vector<gfx::Size>& sizes,
136 const gfx::Size& size) {
137 DCHECK(!sizes.empty());
138 std::vector<gfx::Size>::const_iterator i =
139 std::find(sizes.begin(), sizes.end(), size);
140 if (i == sizes.end())
141 return -1;
143 return static_cast<int>(i - sizes.begin());
146 // Compare function used for std::stable_sort to sort as descend.
147 bool CompareIconSize(const FaviconURL& b1, const FaviconURL& b2) {
148 int area1 = 0;
149 if (!b1.icon_sizes.empty())
150 area1 = b1.icon_sizes.front().GetArea();
152 int area2 = 0;
153 if (!b2.icon_sizes.empty())
154 area2 = b2.icon_sizes.front().GetArea();
156 return area1 > area2;
159 } // namespace
161 ////////////////////////////////////////////////////////////////////////////////
163 FaviconHandler::DownloadRequest::DownloadRequest()
164 : icon_type(favicon_base::INVALID_ICON) {
167 FaviconHandler::DownloadRequest::~DownloadRequest() {
170 FaviconHandler::DownloadRequest::DownloadRequest(
171 const GURL& url,
172 const GURL& image_url,
173 favicon_base::IconType icon_type)
174 : url(url), image_url(image_url), icon_type(icon_type) {
177 ////////////////////////////////////////////////////////////////////////////////
179 FaviconHandler::FaviconCandidate::FaviconCandidate()
180 : score(0), icon_type(favicon_base::INVALID_ICON) {
183 FaviconHandler::FaviconCandidate::~FaviconCandidate() {
186 FaviconHandler::FaviconCandidate::FaviconCandidate(
187 const GURL& url,
188 const GURL& image_url,
189 const gfx::Image& image,
190 float score,
191 favicon_base::IconType icon_type)
192 : url(url),
193 image_url(image_url),
194 image(image),
195 score(score),
196 icon_type(icon_type) {}
198 ////////////////////////////////////////////////////////////////////////////////
200 FaviconHandler::FaviconHandler(FaviconService* service,
201 FaviconDriver* driver,
202 Type handler_type,
203 bool download_largest_icon)
204 : got_favicon_from_history_(false),
205 favicon_expired_or_incomplete_(false),
206 handler_type_(handler_type),
207 icon_types_(FaviconHandler::GetIconTypesFromHandlerType(handler_type)),
208 download_largest_icon_(download_largest_icon),
209 service_(service),
210 driver_(driver) {
211 DCHECK(driver_);
214 FaviconHandler::~FaviconHandler() {
217 // static
218 int FaviconHandler::GetIconTypesFromHandlerType(
219 FaviconHandler::Type handler_type) {
220 switch (handler_type) {
221 case FAVICON:
222 return favicon_base::FAVICON;
223 case TOUCH: // Falls through.
224 case LARGE:
225 return favicon_base::TOUCH_ICON | favicon_base::TOUCH_PRECOMPOSED_ICON;
226 default:
227 NOTREACHED();
229 return 0;
232 void FaviconHandler::FetchFavicon(const GURL& url) {
233 cancelable_task_tracker_.TryCancelAll();
235 url_ = url;
237 favicon_expired_or_incomplete_ = got_favicon_from_history_ = false;
238 image_urls_.clear();
240 // Request the favicon from the history service. In parallel to this the
241 // renderer is going to notify us (well WebContents) when the favicon url is
242 // available.
243 GetFaviconForURLFromFaviconService(
244 url_, icon_types_,
245 base::Bind(&FaviconHandler::OnFaviconDataForInitialURLFromFaviconService,
246 base::Unretained(this)),
247 &cancelable_task_tracker_);
250 bool FaviconHandler::UpdateFaviconCandidate(const GURL& url,
251 const GURL& image_url,
252 const gfx::Image& image,
253 float score,
254 favicon_base::IconType icon_type) {
255 bool replace_best_favicon_candidate = false;
256 bool exact_match = false;
257 if (download_largest_icon_) {
258 replace_best_favicon_candidate =
259 image.Size().GetArea() >
260 best_favicon_candidate_.image.Size().GetArea();
262 gfx::Size largest = best_favicon_candidate_.image.Size();
263 if (replace_best_favicon_candidate)
264 largest = image.Size();
266 // The size of the downloaded icon may not match the declared size. Stop
267 // downloading if:
268 // - current candidate is only candidate.
269 // - next candidate doesn't have sizes attributes, in this case, the rest
270 // candidates don't have sizes attribute either, stop downloading now,
271 // otherwise, all favicon without sizes attribute are downloaded.
272 // - next candidate has sizes attribute and it is not larger than largest,
273 // - current candidate is maximal one we want.
274 const int maximal_size = GetMaximalIconSize(icon_type);
275 exact_match = image_urls_.size() == 1 ||
276 image_urls_[1].icon_sizes.empty() ||
277 image_urls_[1].icon_sizes[0].GetArea() <= largest.GetArea() ||
278 (image.Size().width() == maximal_size &&
279 image.Size().height() == maximal_size);
280 } else {
281 exact_match = score == 1 || preferred_icon_size() == 0;
282 replace_best_favicon_candidate =
283 exact_match ||
284 best_favicon_candidate_.icon_type == favicon_base::INVALID_ICON ||
285 score > best_favicon_candidate_.score;
287 if (replace_best_favicon_candidate) {
288 best_favicon_candidate_ = FaviconCandidate(
289 url, image_url, image, score, icon_type);
291 return exact_match;
294 void FaviconHandler::SetFavicon(const GURL& url,
295 const GURL& icon_url,
296 const gfx::Image& image,
297 favicon_base::IconType icon_type) {
298 if (ShouldSaveFavicon(url))
299 SetHistoryFavicons(url, icon_url, icon_type, image);
301 if (!UrlMatches(url, url_) || PageChangedSinceFaviconWasRequested())
302 return;
304 NotifyFaviconAvailable(
305 icon_url,
306 image,
307 icon_type == favicon_base::FAVICON && !download_largest_icon_);
310 void FaviconHandler::NotifyFaviconAvailable(
311 const std::vector<favicon_base::FaviconRawBitmapResult>&
312 favicon_bitmap_results,
313 bool is_active_favicon) {
314 gfx::Image resized_image = favicon_base::SelectFaviconFramesFromPNGs(
315 favicon_bitmap_results,
316 favicon_base::GetFaviconScales(),
317 preferred_icon_size());
318 // The history service sends back results for a single icon URL, so it does
319 // not matter which result we get the |icon_url| from.
320 const GURL icon_url = favicon_bitmap_results.empty() ?
321 GURL() : favicon_bitmap_results[0].icon_url;
322 NotifyFaviconAvailable(icon_url, resized_image, is_active_favicon);
325 void FaviconHandler::NotifyFaviconAvailable(const GURL& icon_url,
326 const gfx::Image& image,
327 bool is_active_favicon) {
328 gfx::Image image_with_adjusted_colorspace = image;
329 favicon_base::SetFaviconColorSpace(&image_with_adjusted_colorspace);
331 driver_->OnFaviconAvailable(
332 image_with_adjusted_colorspace, icon_url, is_active_favicon);
335 void FaviconHandler::OnUpdateFaviconURL(
336 const std::vector<FaviconURL>& candidates) {
337 image_urls_.clear();
338 best_favicon_candidate_ = FaviconCandidate();
339 for (const FaviconURL& candidate : candidates) {
340 if (!candidate.icon_url.is_empty() && (candidate.icon_type & icon_types_))
341 image_urls_.push_back(candidate);
344 if (download_largest_icon_)
345 SortAndPruneImageUrls();
347 // TODO(davemoore) Should clear on empty url. Currently we ignore it.
348 // This appears to be what FF does as well.
349 if (!image_urls_.empty())
350 ProcessCurrentUrl();
353 void FaviconHandler::ProcessCurrentUrl() {
354 DCHECK(!image_urls_.empty());
356 // current_candidate() may return NULL if download_largest_icon_ is true and
357 // all the sizes are larger than the max.
358 if (PageChangedSinceFaviconWasRequested() || !current_candidate())
359 return;
361 if (current_candidate()->icon_type == favicon_base::FAVICON &&
362 !download_largest_icon_) {
363 if (!favicon_expired_or_incomplete_ &&
364 driver_->GetActiveFaviconValidity() &&
365 DoUrlAndIconMatch(*current_candidate(),
366 driver_->GetActiveFaviconURL(),
367 favicon_base::FAVICON))
368 return;
369 } else if (!favicon_expired_or_incomplete_ && got_favicon_from_history_ &&
370 HasValidResult(history_results_) &&
371 DoUrlsAndIconsMatch(*current_candidate(), history_results_)) {
372 return;
375 if (got_favicon_from_history_)
376 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(),
377 current_candidate()->icon_url,
378 current_candidate()->icon_type);
381 void FaviconHandler::OnDidDownloadFavicon(
382 int id,
383 const GURL& image_url,
384 const std::vector<SkBitmap>& bitmaps,
385 const std::vector<gfx::Size>& original_bitmap_sizes) {
386 DownloadRequests::iterator i = download_requests_.find(id);
387 if (i == download_requests_.end()) {
388 // Currently WebContents notifies us of ANY downloads so that it is
389 // possible to get here.
390 return;
393 DownloadRequest download_request = i->second;
394 download_requests_.erase(i);
396 if (current_candidate() &&
397 DoUrlAndIconMatch(*current_candidate(),
398 image_url,
399 download_request.icon_type)) {
400 bool request_next_icon = true;
401 float score = 0.0f;
402 gfx::ImageSkia image_skia;
403 if (download_largest_icon_ && !bitmaps.empty()) {
404 int index = -1;
405 // Use the largest bitmap if FaviconURL doesn't have sizes attribute.
406 if (current_candidate()->icon_sizes.empty()) {
407 index = GetLargestSizeIndex(original_bitmap_sizes);
408 } else {
409 index = GetIndexBySize(original_bitmap_sizes,
410 current_candidate()->icon_sizes[0]);
411 // Find largest bitmap if there is no one exactly matched.
412 if (index == -1)
413 index = GetLargestSizeIndex(original_bitmap_sizes);
415 image_skia = gfx::ImageSkia(gfx::ImageSkiaRep(bitmaps[index], 1));
416 } else {
417 image_skia = CreateFaviconImageSkia(bitmaps,
418 original_bitmap_sizes,
419 preferred_icon_size(),
420 &score);
423 if (!image_skia.isNull()) {
424 gfx::Image image(image_skia);
425 // The downloaded icon is still valid when there is no FaviconURL update
426 // during the downloading.
427 if (!bitmaps.empty()) {
428 request_next_icon = !UpdateFaviconCandidate(
429 download_request.url, image_url, image, score,
430 download_request.icon_type);
433 if (request_next_icon && !PageChangedSinceFaviconWasRequested() &&
434 image_urls_.size() > 1) {
435 // Remove the first member of image_urls_ and process the remaining.
436 image_urls_.erase(image_urls_.begin());
437 ProcessCurrentUrl();
438 } else if (best_favicon_candidate_.icon_type !=
439 favicon_base::INVALID_ICON) {
440 // No more icons to request, set the favicon from the candidate.
441 SetFavicon(best_favicon_candidate_.url,
442 best_favicon_candidate_.image_url,
443 best_favicon_candidate_.image,
444 best_favicon_candidate_.icon_type);
445 // Reset candidate.
446 image_urls_.clear();
447 download_requests_.clear();
448 best_favicon_candidate_ = FaviconCandidate();
453 bool FaviconHandler::PageChangedSinceFaviconWasRequested() {
454 if (UrlMatches(driver_->GetActiveURL(), url_) && url_.is_valid()) {
455 return false;
457 // If the URL has changed out from under us (as will happen with redirects)
458 // return true.
459 return true;
462 int FaviconHandler::DownloadFavicon(const GURL& image_url,
463 int max_bitmap_size) {
464 if (!image_url.is_valid()) {
465 NOTREACHED();
466 return 0;
468 return driver_->StartDownload(image_url, max_bitmap_size);
471 void FaviconHandler::UpdateFaviconMappingAndFetch(
472 const GURL& page_url,
473 const GURL& icon_url,
474 favicon_base::IconType icon_type,
475 const favicon_base::FaviconResultsCallback& callback,
476 base::CancelableTaskTracker* tracker) {
477 // TODO(pkotwicz): pass in all of |image_urls_| to
478 // UpdateFaviconMappingsAndFetch().
479 if (service_) {
480 std::vector<GURL> icon_urls;
481 icon_urls.push_back(icon_url);
482 service_->UpdateFaviconMappingsAndFetch(page_url, icon_urls, icon_type,
483 preferred_icon_size(), callback,
484 tracker);
488 void FaviconHandler::GetFaviconFromFaviconService(
489 const GURL& icon_url,
490 favicon_base::IconType icon_type,
491 const favicon_base::FaviconResultsCallback& callback,
492 base::CancelableTaskTracker* tracker) {
493 if (service_) {
494 service_->GetFavicon(icon_url, icon_type, preferred_icon_size(), callback,
495 tracker);
499 void FaviconHandler::GetFaviconForURLFromFaviconService(
500 const GURL& page_url,
501 int icon_types,
502 const favicon_base::FaviconResultsCallback& callback,
503 base::CancelableTaskTracker* tracker) {
504 if (service_) {
505 service_->GetFaviconForPageURL(page_url, icon_types, preferred_icon_size(),
506 callback, tracker);
510 void FaviconHandler::SetHistoryFavicons(const GURL& page_url,
511 const GURL& icon_url,
512 favicon_base::IconType icon_type,
513 const gfx::Image& image) {
514 // TODO(huangs): Get the following to garbage collect if handler_type_ == ALL.
515 if (service_) {
516 service_->SetFavicons(page_url, icon_url, icon_type, image);
520 bool FaviconHandler::ShouldSaveFavicon(const GURL& url) {
521 if (!driver_->IsOffTheRecord())
522 return true;
524 // Always save favicon if the page is bookmarked.
525 return driver_->IsBookmarked(url);
528 int FaviconHandler::GetMaximalIconSize(favicon_base::IconType icon_type) {
529 switch (icon_type) {
530 case favicon_base::FAVICON:
531 #if defined(OS_ANDROID)
532 return 192;
533 #else
534 return gfx::ImageSkia::GetMaxSupportedScale() * gfx::kFaviconSize;
535 #endif
536 case favicon_base::TOUCH_ICON:
537 case favicon_base::TOUCH_PRECOMPOSED_ICON:
538 return kTouchIconSize;
539 case favicon_base::INVALID_ICON:
540 return 0;
542 NOTREACHED();
543 return 0;
546 void FaviconHandler::OnFaviconDataForInitialURLFromFaviconService(
547 const std::vector<favicon_base::FaviconRawBitmapResult>&
548 favicon_bitmap_results) {
549 if (PageChangedSinceFaviconWasRequested())
550 return;
551 got_favicon_from_history_ = true;
552 history_results_ = favicon_bitmap_results;
553 bool has_results = !favicon_bitmap_results.empty();
554 favicon_expired_or_incomplete_ = has_results && HasExpiredOrIncompleteResult(
555 preferred_icon_size(), favicon_bitmap_results);
556 bool has_valid_result = HasValidResult(favicon_bitmap_results);
558 if (has_results && handler_type_ == FAVICON &&
559 !download_largest_icon_ && !driver_->GetActiveFaviconValidity() &&
560 (!current_candidate() ||
561 DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) {
562 if (has_valid_result) {
563 // The db knows the favicon (although it may be out of date) and the entry
564 // doesn't have an icon. Set the favicon now, and if the favicon turns out
565 // to be expired (or the wrong url) we'll fetch later on. This way the
566 // user doesn't see a flash of the default favicon.
567 NotifyFaviconAvailable(favicon_bitmap_results, true);
568 } else {
569 // If |favicon_bitmap_results| does not have any valid results, treat the
570 // favicon as if it's expired.
571 // TODO(pkotwicz): Do something better.
572 favicon_expired_or_incomplete_ = true;
575 if (has_results && !favicon_expired_or_incomplete_) {
576 if (current_candidate() &&
577 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)) {
578 // Mapping in the database is wrong. DownloadFavIconOrAskHistory will
579 // update the mapping for this url and download the favicon if we don't
580 // already have it.
581 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(),
582 current_candidate()->icon_url,
583 current_candidate()->icon_type);
585 } else if (current_candidate()) {
586 // We know the official url for the favicon, but either don't have the
587 // favicon or it's expired. Continue on to DownloadFaviconOrAskHistory to
588 // either download or check history again.
589 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(),
590 current_candidate()->icon_url,
591 current_candidate()->icon_type);
593 // else we haven't got the icon url. When we get it we'll ask the
594 // renderer to download the icon.
596 if (has_valid_result && (handler_type_ != FAVICON || download_largest_icon_))
597 NotifyFaviconAvailable(favicon_bitmap_results, false);
600 void FaviconHandler::DownloadFaviconOrAskFaviconService(
601 const GURL& page_url,
602 const GURL& icon_url,
603 favicon_base::IconType icon_type) {
604 if (favicon_expired_or_incomplete_) {
605 // We have the mapping, but the favicon is out of date. Download it now.
606 ScheduleDownload(page_url, icon_url, icon_type);
607 } else {
608 // We don't know the favicon, but we may have previously downloaded the
609 // favicon for another page that shares the same favicon. Ask for the
610 // favicon given the favicon URL.
611 if (driver_->IsOffTheRecord()) {
612 GetFaviconFromFaviconService(
613 icon_url, icon_type,
614 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)),
615 &cancelable_task_tracker_);
616 } else {
617 // Ask the history service for the icon. This does two things:
618 // 1. Attempts to fetch the favicon data from the database.
619 // 2. If the favicon exists in the database, this updates the database to
620 // include the mapping between the page url and the favicon url.
621 // This is asynchronous. The history service will call back when done.
622 UpdateFaviconMappingAndFetch(
623 page_url, icon_url, icon_type,
624 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)),
625 &cancelable_task_tracker_);
630 void FaviconHandler::OnFaviconData(const std::vector<
631 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) {
632 if (PageChangedSinceFaviconWasRequested())
633 return;
635 bool has_results = !favicon_bitmap_results.empty();
636 bool has_expired_or_incomplete_result = HasExpiredOrIncompleteResult(
637 preferred_icon_size(), favicon_bitmap_results);
638 bool has_valid_result = HasValidResult(favicon_bitmap_results);
640 if (has_results && handler_type_ == FAVICON && !download_largest_icon_) {
641 if (has_valid_result) {
642 // There is a favicon, set it now. If expired we'll download the current
643 // one again, but at least the user will get some icon instead of the
644 // default and most likely the current one is fine anyway.
645 NotifyFaviconAvailable(favicon_bitmap_results, true);
647 if (has_expired_or_incomplete_result) {
648 // The favicon is out of date. Request the current one.
649 ScheduleDownload(driver_->GetActiveURL(),
650 driver_->GetActiveFaviconURL(),
651 favicon_base::FAVICON);
653 } else if (current_candidate() &&
654 (!has_results || has_expired_or_incomplete_result ||
655 !(DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)))) {
656 // We don't know the favicon, it is out of date or its type is not same as
657 // one got from page. Request the current one.
658 ScheduleDownload(driver_->GetActiveURL(),
659 current_candidate()->icon_url,
660 current_candidate()->icon_type);
662 history_results_ = favicon_bitmap_results;
664 if (has_valid_result &&
665 (handler_type_ != FAVICON || download_largest_icon_)) {
666 NotifyFaviconAvailable(favicon_bitmap_results, false);
670 int FaviconHandler::ScheduleDownload(const GURL& url,
671 const GURL& image_url,
672 favicon_base::IconType icon_type) {
673 // A max bitmap size is specified to avoid receiving huge bitmaps in
674 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload()
675 // for more details about the max bitmap size.
676 const int download_id = DownloadFavicon(image_url,
677 GetMaximalIconSize(icon_type));
678 if (download_id) {
679 // Download ids should be unique.
680 DCHECK(download_requests_.find(download_id) == download_requests_.end());
681 download_requests_[download_id] =
682 DownloadRequest(url, image_url, icon_type);
685 return download_id;
688 void FaviconHandler::SortAndPruneImageUrls() {
689 // Not using const-reference since the loop mutates FaviconURL::icon_sizes.
690 for (FaviconURL& image_url : image_urls_) {
691 if (image_url.icon_sizes.empty())
692 continue;
694 gfx::Size largest =
695 image_url.icon_sizes[GetLargestSizeIndex(image_url.icon_sizes)];
696 image_url.icon_sizes.clear();
697 image_url.icon_sizes.push_back(largest);
699 std::stable_sort(image_urls_.begin(), image_urls_.end(),
700 CompareIconSize);
703 } // namespace favicon