Fix FaviconHandler when it has multiple icon URLs and one of them is invalid
[chromium-blink-merge.git] / components / favicon / core / favicon_handler.cc
blob3befa3491828e4e127d74984f61048540b62d2a7
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 |bitmap_results| is non-empty and:
78 // - At least one of the bitmaps in |bitmap_results| is expired
79 // OR
80 // - |bitmap_results| is missing favicons for |desired_size_in_dip| and one of
81 // the scale factors in favicon_base::GetFaviconScales().
82 bool HasExpiredOrIncompleteResult(
83 int desired_size_in_dip,
84 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) {
85 if (bitmap_results.empty())
86 return false;
88 // Check if at least one of the bitmaps is expired.
89 std::vector<favicon_base::FaviconRawBitmapResult>::const_iterator it =
90 std::find_if(bitmap_results.begin(), bitmap_results.end(), IsExpired);
91 if (it != bitmap_results.end())
92 return true;
94 // Any favicon size is good if the desired size is 0.
95 if (desired_size_in_dip == 0)
96 return false;
98 // Check if the favicon for at least one of the scale factors is missing.
99 // |bitmap_results| should always be complete for data inserted by
100 // FaviconHandler as the FaviconHandler stores favicons resized to all
101 // of favicon_base::GetFaviconScales() into the history backend.
102 // Examples of when |bitmap_results| can be incomplete:
103 // - Favicons inserted into the history backend by sync.
104 // - Favicons for imported bookmarks.
105 std::vector<gfx::Size> favicon_sizes;
106 for (const auto& bitmap_result : bitmap_results)
107 favicon_sizes.push_back(bitmap_result.pixel_size);
109 std::vector<float> favicon_scales = favicon_base::GetFaviconScales();
110 for (float favicon_scale : favicon_scales) {
111 int edge_size_in_pixel = std::ceil(desired_size_in_dip * favicon_scale);
112 auto it = std::find(favicon_sizes.begin(), favicon_sizes.end(),
113 gfx::Size(edge_size_in_pixel, edge_size_in_pixel));
114 if (it == favicon_sizes.end())
115 return true;
117 return false;
120 // Returns true if at least one of |bitmap_results| is valid.
121 bool HasValidResult(
122 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) {
123 return std::find_if(bitmap_results.begin(), bitmap_results.end(), IsValid) !=
124 bitmap_results.end();
127 // Returns the index of the entry with the largest area.
128 int GetLargestSizeIndex(const std::vector<gfx::Size>& sizes) {
129 DCHECK(!sizes.empty());
130 size_t ret = 0;
131 for (size_t i = 1; i < sizes.size(); ++i) {
132 if (sizes[ret].GetArea() < sizes[i].GetArea())
133 ret = i;
135 return static_cast<int>(ret);
138 // Return the index of a size which is same as the given |size|, -1 returned if
139 // there is no such bitmap.
140 int GetIndexBySize(const std::vector<gfx::Size>& sizes,
141 const gfx::Size& size) {
142 DCHECK(!sizes.empty());
143 std::vector<gfx::Size>::const_iterator i =
144 std::find(sizes.begin(), sizes.end(), size);
145 if (i == sizes.end())
146 return -1;
148 return static_cast<int>(i - sizes.begin());
151 // Compare function used for std::stable_sort to sort as descend.
152 bool CompareIconSize(const FaviconURL& b1, const FaviconURL& b2) {
153 int area1 = 0;
154 if (!b1.icon_sizes.empty())
155 area1 = b1.icon_sizes.front().GetArea();
157 int area2 = 0;
158 if (!b2.icon_sizes.empty())
159 area2 = b2.icon_sizes.front().GetArea();
161 return area1 > area2;
164 } // namespace
166 ////////////////////////////////////////////////////////////////////////////////
168 FaviconHandler::DownloadRequest::DownloadRequest()
169 : icon_type(favicon_base::INVALID_ICON) {
172 FaviconHandler::DownloadRequest::~DownloadRequest() {
175 FaviconHandler::DownloadRequest::DownloadRequest(
176 const GURL& url,
177 const GURL& image_url,
178 favicon_base::IconType icon_type)
179 : url(url), image_url(image_url), icon_type(icon_type) {
182 ////////////////////////////////////////////////////////////////////////////////
184 FaviconHandler::FaviconCandidate::FaviconCandidate()
185 : score(0), icon_type(favicon_base::INVALID_ICON) {
188 FaviconHandler::FaviconCandidate::~FaviconCandidate() {
191 FaviconHandler::FaviconCandidate::FaviconCandidate(
192 const GURL& url,
193 const GURL& image_url,
194 const gfx::Image& image,
195 float score,
196 favicon_base::IconType icon_type)
197 : url(url),
198 image_url(image_url),
199 image(image),
200 score(score),
201 icon_type(icon_type) {}
203 ////////////////////////////////////////////////////////////////////////////////
205 FaviconHandler::FaviconHandler(FaviconService* service,
206 FaviconDriver* driver,
207 Type handler_type,
208 bool download_largest_icon)
209 : got_favicon_from_history_(false),
210 favicon_expired_or_incomplete_(false),
211 handler_type_(handler_type),
212 icon_types_(FaviconHandler::GetIconTypesFromHandlerType(handler_type)),
213 download_largest_icon_(download_largest_icon),
214 service_(service),
215 driver_(driver) {
216 DCHECK(driver_);
219 FaviconHandler::~FaviconHandler() {
222 // static
223 int FaviconHandler::GetIconTypesFromHandlerType(
224 FaviconHandler::Type handler_type) {
225 switch (handler_type) {
226 case FAVICON:
227 return favicon_base::FAVICON;
228 case TOUCH: // Falls through.
229 case LARGE:
230 return favicon_base::TOUCH_ICON | favicon_base::TOUCH_PRECOMPOSED_ICON;
231 default:
232 NOTREACHED();
234 return 0;
237 void FaviconHandler::FetchFavicon(const GURL& url) {
238 cancelable_task_tracker_.TryCancelAll();
240 url_ = url;
242 favicon_expired_or_incomplete_ = got_favicon_from_history_ = false;
243 download_requests_.clear();
244 image_urls_.clear();
245 history_results_.clear();
246 best_favicon_candidate_ = FaviconCandidate();
248 // Request the favicon from the history service. In parallel to this the
249 // renderer is going to notify us (well WebContents) when the favicon url is
250 // available.
251 GetFaviconForURLFromFaviconService(
252 url_, icon_types_,
253 base::Bind(&FaviconHandler::OnFaviconDataForInitialURLFromFaviconService,
254 base::Unretained(this)),
255 &cancelable_task_tracker_);
258 bool FaviconHandler::UpdateFaviconCandidate(const GURL& url,
259 const GURL& image_url,
260 const gfx::Image& image,
261 float score,
262 favicon_base::IconType icon_type) {
263 bool replace_best_favicon_candidate = false;
264 bool exact_match = false;
265 if (download_largest_icon_) {
266 replace_best_favicon_candidate =
267 image.Size().GetArea() >
268 best_favicon_candidate_.image.Size().GetArea();
270 gfx::Size largest = best_favicon_candidate_.image.Size();
271 if (replace_best_favicon_candidate)
272 largest = image.Size();
274 // The size of the downloaded icon may not match the declared size. Stop
275 // downloading if:
276 // - current candidate is only candidate.
277 // - next candidate doesn't have sizes attributes, in this case, the rest
278 // candidates don't have sizes attribute either, stop downloading now,
279 // otherwise, all favicon without sizes attribute are downloaded.
280 // - next candidate has sizes attribute and it is not larger than largest,
281 // - current candidate is maximal one we want.
282 const int maximal_size = GetMaximalIconSize(icon_type);
283 exact_match = image_urls_.size() == 1 ||
284 image_urls_[1].icon_sizes.empty() ||
285 image_urls_[1].icon_sizes[0].GetArea() <= largest.GetArea() ||
286 (image.Size().width() == maximal_size &&
287 image.Size().height() == maximal_size);
288 } else {
289 exact_match = score == 1 || preferred_icon_size() == 0;
290 replace_best_favicon_candidate =
291 exact_match ||
292 best_favicon_candidate_.icon_type == favicon_base::INVALID_ICON ||
293 score > best_favicon_candidate_.score;
295 if (replace_best_favicon_candidate) {
296 best_favicon_candidate_ = FaviconCandidate(
297 url, image_url, image, score, icon_type);
299 return exact_match;
302 void FaviconHandler::SetFavicon(const GURL& url,
303 const GURL& icon_url,
304 const gfx::Image& image,
305 favicon_base::IconType icon_type) {
306 if (ShouldSaveFavicon(url))
307 SetHistoryFavicons(url, icon_url, icon_type, image);
309 NotifyFaviconAvailable(icon_url, image);
312 void FaviconHandler::NotifyFaviconAvailable(
313 const std::vector<favicon_base::FaviconRawBitmapResult>&
314 favicon_bitmap_results) {
315 gfx::Image resized_image = favicon_base::SelectFaviconFramesFromPNGs(
316 favicon_bitmap_results,
317 favicon_base::GetFaviconScales(),
318 preferred_icon_size());
319 // The history service sends back results for a single icon URL, so it does
320 // not matter which result we get the |icon_url| from.
321 const GURL icon_url = favicon_bitmap_results.empty() ?
322 GURL() : favicon_bitmap_results[0].icon_url;
323 NotifyFaviconAvailable(icon_url, resized_image);
326 void FaviconHandler::NotifyFaviconAvailable(const GURL& icon_url,
327 const gfx::Image& image) {
328 gfx::Image image_with_adjusted_colorspace = image;
329 favicon_base::SetFaviconColorSpace(&image_with_adjusted_colorspace);
331 bool is_active_favicon =
332 (handler_type_ == FAVICON && !download_largest_icon_);
334 driver_->OnFaviconAvailable(
335 image_with_adjusted_colorspace, icon_url, is_active_favicon);
338 void FaviconHandler::OnUpdateFaviconURL(
339 const std::vector<FaviconURL>& candidates) {
340 download_requests_.clear();
341 image_urls_.clear();
342 best_favicon_candidate_ = FaviconCandidate();
344 for (const FaviconURL& candidate : candidates) {
345 if (!candidate.icon_url.is_empty() && (candidate.icon_type & icon_types_))
346 image_urls_.push_back(candidate);
349 if (download_largest_icon_)
350 SortAndPruneImageUrls();
352 // TODO(davemoore) Should clear on empty url. Currently we ignore it.
353 // This appears to be what FF does as well.
354 if (!image_urls_.empty())
355 ProcessCurrentUrl();
358 void FaviconHandler::ProcessCurrentUrl() {
359 DCHECK(!image_urls_.empty());
361 // current_candidate() may return NULL if download_largest_icon_ is true and
362 // all the sizes are larger than the max.
363 if (PageChangedSinceFaviconWasRequested() || !current_candidate())
364 return;
366 if (current_candidate()->icon_type == favicon_base::FAVICON &&
367 !download_largest_icon_) {
368 if (!favicon_expired_or_incomplete_ &&
369 driver_->GetActiveFaviconValidity() &&
370 DoUrlAndIconMatch(*current_candidate(),
371 driver_->GetActiveFaviconURL(),
372 favicon_base::FAVICON))
373 return;
374 } else if (!favicon_expired_or_incomplete_ && got_favicon_from_history_ &&
375 HasValidResult(history_results_) &&
376 DoUrlsAndIconsMatch(*current_candidate(), history_results_)) {
377 return;
380 if (got_favicon_from_history_)
381 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(),
382 current_candidate()->icon_url,
383 current_candidate()->icon_type);
386 void FaviconHandler::OnDidDownloadFavicon(
387 int id,
388 const GURL& image_url,
389 const std::vector<SkBitmap>& bitmaps,
390 const std::vector<gfx::Size>& original_bitmap_sizes) {
391 DownloadRequests::iterator i = download_requests_.find(id);
392 if (i == download_requests_.end()) {
393 // Currently WebContents notifies us of ANY downloads so that it is
394 // possible to get here.
395 return;
398 DownloadRequest download_request = i->second;
399 download_requests_.erase(i);
401 if (PageChangedSinceFaviconWasRequested() ||
402 !current_candidate() ||
403 !DoUrlAndIconMatch(*current_candidate(),
404 image_url,
405 download_request.icon_type)) {
406 return;
409 bool request_next_icon = true;
410 if (!bitmaps.empty()) {
411 float score = 0.0f;
412 gfx::ImageSkia image_skia;
413 if (download_largest_icon_) {
414 int index = -1;
415 // Use the largest bitmap if FaviconURL doesn't have sizes attribute.
416 if (current_candidate()->icon_sizes.empty()) {
417 index = GetLargestSizeIndex(original_bitmap_sizes);
418 } else {
419 index = GetIndexBySize(original_bitmap_sizes,
420 current_candidate()->icon_sizes[0]);
421 // Find largest bitmap if there is no one exactly matched.
422 if (index == -1)
423 index = GetLargestSizeIndex(original_bitmap_sizes);
425 image_skia = gfx::ImageSkia(gfx::ImageSkiaRep(bitmaps[index], 1));
426 } else {
427 image_skia = CreateFaviconImageSkia(bitmaps,
428 original_bitmap_sizes,
429 preferred_icon_size(),
430 &score);
433 if (!image_skia.isNull()) {
434 gfx::Image image(image_skia);
435 // The downloaded icon is still valid when there is no FaviconURL update
436 // during the downloading.
437 request_next_icon = !UpdateFaviconCandidate(
438 download_request.url, image_url, image, score,
439 download_request.icon_type);
443 if (request_next_icon && image_urls_.size() > 1) {
444 // Remove the first member of image_urls_ and process the remaining.
445 image_urls_.erase(image_urls_.begin());
446 ProcessCurrentUrl();
447 } else {
448 // We have either found the ideal candidate or run out of candidates.
449 if (best_favicon_candidate_.icon_type != favicon_base::INVALID_ICON) {
450 // No more icons to request, set the favicon from the candidate.
451 SetFavicon(best_favicon_candidate_.url, best_favicon_candidate_.image_url,
452 best_favicon_candidate_.image,
453 best_favicon_candidate_.icon_type);
455 // Clear download related state.
456 image_urls_.clear();
457 download_requests_.clear();
458 best_favicon_candidate_ = FaviconCandidate();
462 bool FaviconHandler::HasPendingTasksForTest() {
463 return !download_requests_.empty() ||
464 cancelable_task_tracker_.HasTrackedTasks();
467 bool FaviconHandler::PageChangedSinceFaviconWasRequested() {
468 if (UrlMatches(driver_->GetActiveURL(), url_) && url_.is_valid()) {
469 return false;
471 // If the URL has changed out from under us (as will happen with redirects)
472 // return true.
473 return true;
476 int FaviconHandler::DownloadFavicon(const GURL& image_url,
477 int max_bitmap_size) {
478 if (!image_url.is_valid()) {
479 NOTREACHED();
480 return 0;
482 return driver_->StartDownload(image_url, max_bitmap_size);
485 void FaviconHandler::UpdateFaviconMappingAndFetch(
486 const GURL& page_url,
487 const GURL& icon_url,
488 favicon_base::IconType icon_type,
489 const favicon_base::FaviconResultsCallback& callback,
490 base::CancelableTaskTracker* tracker) {
491 // TODO(pkotwicz): pass in all of |image_urls_| to
492 // UpdateFaviconMappingsAndFetch().
493 if (service_) {
494 std::vector<GURL> icon_urls;
495 icon_urls.push_back(icon_url);
496 service_->UpdateFaviconMappingsAndFetch(page_url, icon_urls, icon_type,
497 preferred_icon_size(), callback,
498 tracker);
502 void FaviconHandler::GetFaviconFromFaviconService(
503 const GURL& icon_url,
504 favicon_base::IconType icon_type,
505 const favicon_base::FaviconResultsCallback& callback,
506 base::CancelableTaskTracker* tracker) {
507 if (service_) {
508 service_->GetFavicon(icon_url, icon_type, preferred_icon_size(), callback,
509 tracker);
513 void FaviconHandler::GetFaviconForURLFromFaviconService(
514 const GURL& page_url,
515 int icon_types,
516 const favicon_base::FaviconResultsCallback& callback,
517 base::CancelableTaskTracker* tracker) {
518 if (service_) {
519 service_->GetFaviconForPageURL(page_url, icon_types, preferred_icon_size(),
520 callback, tracker);
524 void FaviconHandler::SetHistoryFavicons(const GURL& page_url,
525 const GURL& icon_url,
526 favicon_base::IconType icon_type,
527 const gfx::Image& image) {
528 // TODO(huangs): Get the following to garbage collect if handler_type_ == ALL.
529 if (service_) {
530 service_->SetFavicons(page_url, icon_url, icon_type, image);
534 bool FaviconHandler::ShouldSaveFavicon(const GURL& url) {
535 if (!driver_->IsOffTheRecord())
536 return true;
538 // Always save favicon if the page is bookmarked.
539 return driver_->IsBookmarked(url);
542 int FaviconHandler::GetMaximalIconSize(favicon_base::IconType icon_type) {
543 switch (icon_type) {
544 case favicon_base::FAVICON:
545 #if defined(OS_ANDROID)
546 return 192;
547 #else
548 return gfx::ImageSkia::GetMaxSupportedScale() * gfx::kFaviconSize;
549 #endif
550 case favicon_base::TOUCH_ICON:
551 case favicon_base::TOUCH_PRECOMPOSED_ICON:
552 return kTouchIconSize;
553 case favicon_base::INVALID_ICON:
554 return 0;
556 NOTREACHED();
557 return 0;
560 void FaviconHandler::OnFaviconDataForInitialURLFromFaviconService(
561 const std::vector<favicon_base::FaviconRawBitmapResult>&
562 favicon_bitmap_results) {
563 if (PageChangedSinceFaviconWasRequested())
564 return;
565 got_favicon_from_history_ = true;
566 history_results_ = favicon_bitmap_results;
567 bool has_results = !favicon_bitmap_results.empty();
568 favicon_expired_or_incomplete_ = HasExpiredOrIncompleteResult(
569 preferred_icon_size(), favicon_bitmap_results);
570 bool has_valid_result = HasValidResult(favicon_bitmap_results);
572 if (has_results && handler_type_ == FAVICON &&
573 !download_largest_icon_ && !driver_->GetActiveFaviconValidity() &&
574 (!current_candidate() ||
575 DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) {
576 if (has_valid_result) {
577 // The db knows the favicon (although it may be out of date) and the entry
578 // doesn't have an icon. Set the favicon now, and if the favicon turns out
579 // to be expired (or the wrong url) we'll fetch later on. This way the
580 // user doesn't see a flash of the default favicon.
581 NotifyFaviconAvailable(favicon_bitmap_results);
582 } else {
583 // If |favicon_bitmap_results| does not have any valid results, treat the
584 // favicon as if it's expired.
585 // TODO(pkotwicz): Do something better.
586 favicon_expired_or_incomplete_ = true;
589 if (has_results && !favicon_expired_or_incomplete_) {
590 if (current_candidate() &&
591 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)) {
592 // Mapping in the database is wrong. DownloadFavIconOrAskHistory will
593 // update the mapping for this url and download the favicon if we don't
594 // already have it.
595 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(),
596 current_candidate()->icon_url,
597 current_candidate()->icon_type);
599 } else if (current_candidate()) {
600 // We know the official url for the favicon, but either don't have the
601 // favicon or it's expired. Continue on to DownloadFaviconOrAskHistory to
602 // either download or check history again.
603 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(),
604 current_candidate()->icon_url,
605 current_candidate()->icon_type);
607 // else we haven't got the icon url. When we get it we'll ask the
608 // renderer to download the icon.
610 if (has_valid_result && (handler_type_ != FAVICON || download_largest_icon_))
611 NotifyFaviconAvailable(favicon_bitmap_results);
614 void FaviconHandler::DownloadFaviconOrAskFaviconService(
615 const GURL& page_url,
616 const GURL& icon_url,
617 favicon_base::IconType icon_type) {
618 if (favicon_expired_or_incomplete_) {
619 // We have the mapping, but the favicon is out of date. Download it now.
620 ScheduleDownload(page_url, icon_url, icon_type);
621 } else {
622 // We don't know the favicon, but we may have previously downloaded the
623 // favicon for another page that shares the same favicon. Ask for the
624 // favicon given the favicon URL.
625 if (driver_->IsOffTheRecord()) {
626 GetFaviconFromFaviconService(
627 icon_url, icon_type,
628 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)),
629 &cancelable_task_tracker_);
630 } else {
631 // Ask the history service for the icon. This does two things:
632 // 1. Attempts to fetch the favicon data from the database.
633 // 2. If the favicon exists in the database, this updates the database to
634 // include the mapping between the page url and the favicon url.
635 // This is asynchronous. The history service will call back when done.
636 UpdateFaviconMappingAndFetch(
637 page_url, icon_url, icon_type,
638 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)),
639 &cancelable_task_tracker_);
644 void FaviconHandler::OnFaviconData(const std::vector<
645 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) {
646 if (PageChangedSinceFaviconWasRequested())
647 return;
649 bool has_results = !favicon_bitmap_results.empty();
650 bool has_expired_or_incomplete_result = HasExpiredOrIncompleteResult(
651 preferred_icon_size(), favicon_bitmap_results);
652 bool has_valid_result = HasValidResult(favicon_bitmap_results);
653 history_results_ = favicon_bitmap_results;
655 if (has_valid_result) {
656 // There is a valid favicon. Notify any observers. It is useful to notify
657 // the observers even if the favicon is expired or incomplete (incorrect
658 // size) because temporarily showing the user an expired favicon or
659 // streched favicon is preferable to showing the user the default favicon.
660 NotifyFaviconAvailable(favicon_bitmap_results);
663 if (!current_candidate() ||
664 (has_results &&
665 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) {
666 // The icon URLs have been updated since the favicon data was requested.
667 return;
670 if (!has_results || has_expired_or_incomplete_result) {
671 ScheduleDownload(driver_->GetActiveURL(),
672 current_candidate()->icon_url,
673 current_candidate()->icon_type);
677 void FaviconHandler::ScheduleDownload(const GURL& url,
678 const GURL& image_url,
679 favicon_base::IconType icon_type) {
680 // A max bitmap size is specified to avoid receiving huge bitmaps in
681 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload()
682 // for more details about the max bitmap size.
683 const int download_id = DownloadFavicon(image_url,
684 GetMaximalIconSize(icon_type));
686 // Download ids should be unique.
687 DCHECK(download_requests_.find(download_id) == download_requests_.end());
688 download_requests_[download_id] = DownloadRequest(url, image_url, icon_type);
690 if (download_id == 0) {
691 // If DownloadFavicon() did not start a download, it returns a download id
692 // of 0. We still need to call OnDidDownloadFavicon() because the method is
693 // responsible for initiating the data request for the next candidate.
694 OnDidDownloadFavicon(download_id, image_url, std::vector<SkBitmap>(),
695 std::vector<gfx::Size>());
700 void FaviconHandler::SortAndPruneImageUrls() {
701 // Not using const-reference since the loop mutates FaviconURL::icon_sizes.
702 for (FaviconURL& image_url : image_urls_) {
703 if (image_url.icon_sizes.empty())
704 continue;
706 gfx::Size largest =
707 image_url.icon_sizes[GetLargestSizeIndex(image_url.icon_sizes)];
708 image_url.icon_sizes.clear();
709 image_url.icon_sizes.push_back(largest);
711 std::stable_sort(image_urls_.begin(), image_urls_.end(),
712 CompareIconSize);
715 } // namespace favicon