Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / favicon / favicon_handler.cc
blobfcf89fe939638f967025ff89bcfbacbd64655fe9
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 "chrome/browser/favicon/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 "chrome/browser/favicon/favicon_service.h"
16 #include "components/favicon/core/browser/favicon_client.h"
17 #include "components/favicon/core/favicon_driver.h"
18 #include "components/favicon_base/favicon_util.h"
19 #include "components/favicon_base/select_favicon_frames.h"
20 #include "skia/ext/image_operations.h"
21 #include "ui/gfx/codec/png_codec.h"
22 #include "ui/gfx/image/image_skia.h"
23 #include "ui/gfx/image/image_util.h"
25 using favicon::FaviconURL;
27 namespace {
29 // Size (along each axis) of a touch icon. This currently corresponds to
30 // the apple touch icon for iPad.
31 const int kTouchIconSize = 144;
33 bool DoUrlAndIconMatch(const FaviconURL& favicon_url,
34 const GURL& url,
35 favicon_base::IconType icon_type) {
36 return favicon_url.icon_url == url && favicon_url.icon_type == icon_type;
39 // Returns true if all of the icon URLs and icon types in |bitmap_results| are
40 // identical and if they match the icon URL and icon type in |favicon_url|.
41 // Returns false if |bitmap_results| is empty.
42 bool DoUrlsAndIconsMatch(
43 const FaviconURL& favicon_url,
44 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) {
45 if (bitmap_results.empty())
46 return false;
48 const favicon_base::IconType icon_type = favicon_url.icon_type;
50 for (size_t i = 0; i < bitmap_results.size(); ++i) {
51 if (favicon_url.icon_url != bitmap_results[i].icon_url ||
52 icon_type != bitmap_results[i].icon_type) {
53 return false;
56 return true;
59 std::string UrlWithoutFragment(const GURL& gurl) {
60 GURL::Replacements replacements;
61 replacements.ClearRef();
62 return gurl.ReplaceComponents(replacements).spec();
65 bool UrlMatches(const GURL& gurl_a, const GURL& gurl_b) {
66 return UrlWithoutFragment(gurl_a) == UrlWithoutFragment(gurl_b);
69 // Return true if |bitmap_result| is expired.
70 bool IsExpired(const favicon_base::FaviconRawBitmapResult& bitmap_result) {
71 return bitmap_result.expired;
74 // Return true if |bitmap_result| is valid.
75 bool IsValid(const favicon_base::FaviconRawBitmapResult& bitmap_result) {
76 return bitmap_result.is_valid();
79 // Returns true if at least one of the bitmaps in |bitmap_results| is expired or
80 // if |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 // Check if at least one of the bitmaps is expired.
86 std::vector<favicon_base::FaviconRawBitmapResult>::const_iterator it =
87 std::find_if(bitmap_results.begin(), bitmap_results.end(), IsExpired);
88 if (it != bitmap_results.end())
89 return true;
91 // Any favicon size is good if the desired size is 0.
92 if (desired_size_in_dip == 0)
93 return false;
95 // Check if the favicon for at least one of the scale factors is missing.
96 // |bitmap_results| should always be complete for data inserted by
97 // FaviconHandler as the FaviconHandler stores favicons resized to all
98 // of favicon_base::GetFaviconScales() into the history backend.
99 // Examples of when |bitmap_results| can be incomplete:
100 // - Favicons inserted into the history backend by sync.
101 // - Favicons for imported bookmarks.
102 std::vector<gfx::Size> favicon_sizes;
103 for (size_t i = 0; i < bitmap_results.size(); ++i)
104 favicon_sizes.push_back(bitmap_results[i].pixel_size);
106 std::vector<float> favicon_scales = favicon_base::GetFaviconScales();
107 for (size_t i = 0; i < favicon_scales.size(); ++i) {
108 int edge_size_in_pixel = std::ceil(desired_size_in_dip * favicon_scales[i]);
109 std::vector<gfx::Size>::iterator it = std::find(favicon_sizes.begin(),
110 favicon_sizes.end(), gfx::Size(edge_size_in_pixel, edge_size_in_pixel));
111 if (it == favicon_sizes.end())
112 return true;
114 return false;
117 // Returns true if at least one of |bitmap_results| is valid.
118 bool HasValidResult(
119 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) {
120 return std::find_if(bitmap_results.begin(), bitmap_results.end(), IsValid) !=
121 bitmap_results.end();
124 // Returns the index of the entry with the largest area.
125 int GetLargestSizeIndex(const std::vector<gfx::Size>& sizes) {
126 DCHECK(!sizes.empty());
127 int ret = 0;
128 for (size_t i = 1; i < sizes.size(); ++i) {
129 if (sizes[ret].GetArea() < sizes[i].GetArea())
130 ret = i;
132 return ret;
135 // Return the index of a size which is same as the given |size|, -1 returned if
136 // there is no such bitmap.
137 int GetIndexBySize(const std::vector<gfx::Size>& sizes,
138 const gfx::Size& size) {
139 DCHECK(!sizes.empty());
140 std::vector<gfx::Size>::const_iterator i =
141 std::find(sizes.begin(), sizes.end(), size);
142 if (i == sizes.end())
143 return -1;
145 return static_cast<int>(i - sizes.begin());
148 // Compare function used for std::stable_sort to sort as descend.
149 bool CompareIconSize(const FaviconURL& b1, const FaviconURL& b2) {
150 int area1 = 0;
151 if (!b1.icon_sizes.empty())
152 area1 = b1.icon_sizes.front().GetArea();
154 int area2 = 0;
155 if (!b2.icon_sizes.empty())
156 area2 = b2.icon_sizes.front().GetArea();
158 return area1 > area2;
161 } // namespace
163 ////////////////////////////////////////////////////////////////////////////////
165 FaviconHandler::DownloadRequest::DownloadRequest()
166 : icon_type(favicon_base::INVALID_ICON) {}
168 FaviconHandler::DownloadRequest::~DownloadRequest() {
171 FaviconHandler::DownloadRequest::DownloadRequest(
172 const GURL& url,
173 const GURL& image_url,
174 favicon_base::IconType icon_type)
175 : url(url), image_url(image_url), icon_type(icon_type) {}
177 ////////////////////////////////////////////////////////////////////////////////
179 FaviconHandler::FaviconCandidate::FaviconCandidate()
180 : score(0), icon_type(favicon_base::INVALID_ICON) {}
182 FaviconHandler::FaviconCandidate::~FaviconCandidate() {
185 FaviconHandler::FaviconCandidate::FaviconCandidate(
186 const GURL& url,
187 const GURL& image_url,
188 const gfx::Image& image,
189 float score,
190 favicon_base::IconType icon_type)
191 : url(url),
192 image_url(image_url),
193 image(image),
194 score(score),
195 icon_type(icon_type) {}
197 ////////////////////////////////////////////////////////////////////////////////
199 FaviconHandler::FaviconHandler(FaviconClient* client,
200 FaviconDriver* driver,
201 Type icon_type,
202 bool download_largest_icon)
203 : got_favicon_from_history_(false),
204 favicon_expired_or_incomplete_(false),
205 icon_types_(icon_type == FAVICON
206 ? favicon_base::FAVICON
207 : favicon_base::TOUCH_ICON |
208 favicon_base::TOUCH_PRECOMPOSED_ICON),
209 download_largest_icon_(download_largest_icon),
210 client_(client),
211 driver_(driver) {
212 DCHECK(driver_);
215 FaviconHandler::~FaviconHandler() {
218 void FaviconHandler::FetchFavicon(const GURL& url) {
219 cancelable_task_tracker_.TryCancelAll();
221 url_ = url;
223 favicon_expired_or_incomplete_ = got_favicon_from_history_ = false;
224 image_urls_.clear();
226 // Request the favicon from the history service. In parallel to this the
227 // renderer is going to notify us (well WebContents) when the favicon url is
228 // available.
229 if (client_->GetFaviconService()) {
230 GetFaviconForURLFromFaviconService(
231 url_,
232 icon_types_,
233 base::Bind(
234 &FaviconHandler::OnFaviconDataForInitialURLFromFaviconService,
235 base::Unretained(this)),
236 &cancelable_task_tracker_);
240 bool FaviconHandler::UpdateFaviconCandidate(const GURL& url,
241 const GURL& image_url,
242 const gfx::Image& image,
243 float score,
244 favicon_base::IconType icon_type) {
245 bool replace_best_favicon_candidate = false;
246 bool exact_match = false;
247 if (download_largest_icon_) {
248 replace_best_favicon_candidate =
249 image.Size().GetArea() >
250 best_favicon_candidate_.image.Size().GetArea();
252 gfx::Size largest = best_favicon_candidate_.image.Size();
253 if (replace_best_favicon_candidate)
254 largest = image.Size();
256 // The size of the downloaded icon may not match the declared size. Stop
257 // downloading if:
258 // - current candidate is only candidate.
259 // - next candidate doesn't have sizes attributes, in this case, the rest
260 // candidates don't have sizes attribute either, stop downloading now,
261 // otherwise, all favicon without sizes attribute are downloaded.
262 // - next candidate has sizes attribute and it is not larger than largest,
263 // - current candidate is maximal one we want.
264 const int maximal_size = GetMaximalIconSize(icon_type);
265 exact_match = image_urls_.size() == 1 ||
266 image_urls_[1].icon_sizes.empty() ||
267 image_urls_[1].icon_sizes[0].GetArea() <= largest.GetArea() ||
268 (image.Size().width() == maximal_size &&
269 image.Size().height() == maximal_size);
270 } else {
271 exact_match = score == 1 || preferred_icon_size() == 0;
272 replace_best_favicon_candidate =
273 exact_match ||
274 best_favicon_candidate_.icon_type == favicon_base::INVALID_ICON ||
275 score > best_favicon_candidate_.score;
277 if (replace_best_favicon_candidate) {
278 best_favicon_candidate_ = FaviconCandidate(
279 url, image_url, image, score, icon_type);
281 return exact_match;
284 void FaviconHandler::SetFavicon(const GURL& url,
285 const GURL& icon_url,
286 const gfx::Image& image,
287 favicon_base::IconType icon_type) {
288 if (client_->GetFaviconService() && ShouldSaveFavicon(url))
289 SetHistoryFavicons(url, icon_url, icon_type, image);
291 if (UrlMatches(url, url_) && icon_type == favicon_base::FAVICON) {
292 if (!PageChangedSinceFaviconWasRequested())
293 SetFaviconOnActivePage(icon_url, image);
297 void FaviconHandler::SetFaviconOnActivePage(const std::vector<
298 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) {
299 gfx::Image resized_image = favicon_base::SelectFaviconFramesFromPNGs(
300 favicon_bitmap_results,
301 favicon_base::GetFaviconScales(),
302 preferred_icon_size());
303 // The history service sends back results for a single icon URL, so it does
304 // not matter which result we get the |icon_url| from.
305 const GURL icon_url = favicon_bitmap_results.empty() ?
306 GURL() : favicon_bitmap_results[0].icon_url;
307 SetFaviconOnActivePage(icon_url, resized_image);
310 void FaviconHandler::SetFaviconOnActivePage(const GURL& icon_url,
311 const gfx::Image& image) {
312 // No matter what happens, we need to mark the favicon as being set.
313 driver_->SetActiveFaviconValidity(true);
315 bool icon_url_changed = driver_->GetActiveFaviconURL() != icon_url;
316 driver_->SetActiveFaviconURL(icon_url);
318 if (image.IsEmpty())
319 return;
321 gfx::Image image_with_adjusted_colorspace = image;
322 favicon_base::SetFaviconColorSpace(&image_with_adjusted_colorspace);
324 driver_->SetActiveFaviconImage(image_with_adjusted_colorspace);
325 NotifyFaviconUpdated(icon_url_changed);
328 void FaviconHandler::OnUpdateFaviconURL(
329 const std::vector<FaviconURL>& candidates) {
330 image_urls_.clear();
331 best_favicon_candidate_ = FaviconCandidate();
332 for (std::vector<FaviconURL>::const_iterator i = candidates.begin();
333 i != candidates.end(); ++i) {
334 if (!i->icon_url.is_empty() && (i->icon_type & icon_types_))
335 image_urls_.push_back(*i);
338 if (!client_->GetFaviconService())
339 return;
341 if (download_largest_icon_)
342 SortAndPruneImageUrls();
344 // TODO(davemoore) Should clear on empty url. Currently we ignore it.
345 // This appears to be what FF does as well.
346 if (!image_urls_.empty())
347 ProcessCurrentUrl();
350 void FaviconHandler::ProcessCurrentUrl() {
351 DCHECK(!image_urls_.empty());
353 // current_candidate() may return NULL if download_largest_icon_ is true and
354 // all the sizes are larger than the max.
355 if (PageChangedSinceFaviconWasRequested() || !current_candidate())
356 return;
358 if (current_candidate()->icon_type == favicon_base::FAVICON) {
359 if (!favicon_expired_or_incomplete_ &&
360 driver_->GetActiveFaviconValidity() &&
361 DoUrlAndIconMatch(*current_candidate(),
362 driver_->GetActiveFaviconURL(),
363 favicon_base::FAVICON))
364 return;
365 } else if (!favicon_expired_or_incomplete_ && got_favicon_from_history_ &&
366 HasValidResult(history_results_) &&
367 DoUrlsAndIconsMatch(*current_candidate(), history_results_)) {
368 return;
371 if (got_favicon_from_history_)
372 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(),
373 current_candidate()->icon_url,
374 current_candidate()->icon_type);
377 void FaviconHandler::OnDidDownloadFavicon(
378 int id,
379 const GURL& image_url,
380 const std::vector<SkBitmap>& bitmaps,
381 const std::vector<gfx::Size>& original_bitmap_sizes) {
382 DownloadRequests::iterator i = download_requests_.find(id);
383 if (i == download_requests_.end()) {
384 // Currently WebContents notifies us of ANY downloads so that it is
385 // possible to get here.
386 return;
389 if (current_candidate() &&
390 DoUrlAndIconMatch(*current_candidate(), image_url, i->second.icon_type)) {
391 bool request_next_icon = true;
392 float score = 0.0f;
393 gfx::ImageSkia image_skia;
394 if (download_largest_icon_ && !bitmaps.empty()) {
395 int index = -1;
396 // Use the largest bitmap if FaviconURL doesn't have sizes attribute.
397 if (current_candidate()->icon_sizes.empty()) {
398 index = GetLargestSizeIndex(original_bitmap_sizes);
399 } else {
400 index = GetIndexBySize(original_bitmap_sizes,
401 current_candidate()->icon_sizes[0]);
402 // Find largest bitmap if there is no one exactly matched.
403 if (index == -1)
404 index = GetLargestSizeIndex(original_bitmap_sizes);
406 image_skia = gfx::ImageSkia(gfx::ImageSkiaRep(bitmaps[index], 1));
407 } else {
408 image_skia = CreateFaviconImageSkia(bitmaps,
409 original_bitmap_sizes,
410 preferred_icon_size(),
411 &score);
414 if (!image_skia.isNull()) {
415 gfx::Image image(image_skia);
416 // The downloaded icon is still valid when there is no FaviconURL update
417 // during the downloading.
418 if (!bitmaps.empty()) {
419 request_next_icon = !UpdateFaviconCandidate(
420 i->second.url, image_url, image, score, i->second.icon_type);
423 if (request_next_icon && !PageChangedSinceFaviconWasRequested() &&
424 image_urls_.size() > 1) {
425 // Remove the first member of image_urls_ and process the remaining.
426 image_urls_.erase(image_urls_.begin());
427 ProcessCurrentUrl();
428 } else if (best_favicon_candidate_.icon_type !=
429 favicon_base::INVALID_ICON) {
430 // No more icons to request, set the favicon from the candidate.
431 SetFavicon(best_favicon_candidate_.url,
432 best_favicon_candidate_.image_url,
433 best_favicon_candidate_.image,
434 best_favicon_candidate_.icon_type);
435 // Reset candidate.
436 image_urls_.clear();
437 best_favicon_candidate_ = FaviconCandidate();
440 download_requests_.erase(i);
443 bool FaviconHandler::PageChangedSinceFaviconWasRequested() {
444 if (UrlMatches(driver_->GetActiveURL(), url_) && url_.is_valid()) {
445 return false;
447 // If the URL has changed out from under us (as will happen with redirects)
448 // return true.
449 return true;
452 int FaviconHandler::DownloadFavicon(const GURL& image_url,
453 int max_bitmap_size) {
454 if (!image_url.is_valid()) {
455 NOTREACHED();
456 return 0;
458 return driver_->StartDownload(image_url, max_bitmap_size);
461 void FaviconHandler::UpdateFaviconMappingAndFetch(
462 const GURL& page_url,
463 const GURL& icon_url,
464 favicon_base::IconType icon_type,
465 const favicon_base::FaviconResultsCallback& callback,
466 base::CancelableTaskTracker* tracker) {
467 // TODO(pkotwicz): pass in all of |image_urls_| to
468 // UpdateFaviconMappingsAndFetch().
469 std::vector<GURL> icon_urls;
470 icon_urls.push_back(icon_url);
471 client_->GetFaviconService()->UpdateFaviconMappingsAndFetch(
472 page_url, icon_urls, icon_type, preferred_icon_size(), callback, tracker);
475 void FaviconHandler::GetFaviconFromFaviconService(
476 const GURL& icon_url,
477 favicon_base::IconType icon_type,
478 const favicon_base::FaviconResultsCallback& callback,
479 base::CancelableTaskTracker* tracker) {
480 client_->GetFaviconService()->GetFavicon(
481 icon_url, icon_type, preferred_icon_size(), callback, tracker);
484 void FaviconHandler::GetFaviconForURLFromFaviconService(
485 const GURL& page_url,
486 int icon_types,
487 const favicon_base::FaviconResultsCallback& callback,
488 base::CancelableTaskTracker* tracker) {
489 client_->GetFaviconService()->GetFaviconForPageURL(
490 page_url,
491 icon_types,
492 preferred_icon_size(),
493 callback,
494 tracker);
497 void FaviconHandler::SetHistoryFavicons(const GURL& page_url,
498 const GURL& icon_url,
499 favicon_base::IconType icon_type,
500 const gfx::Image& image) {
501 client_->GetFaviconService()->SetFavicons(
502 page_url, icon_url, icon_type, image);
505 bool FaviconHandler::ShouldSaveFavicon(const GURL& url) {
506 if (!driver_->IsOffTheRecord())
507 return true;
509 // Otherwise store the favicon if the page is bookmarked.
510 return client_->IsBookmarked(url);
513 void FaviconHandler::NotifyFaviconUpdated(bool icon_url_changed) {
514 driver_->NotifyFaviconUpdated(icon_url_changed);
517 int FaviconHandler::GetMaximalIconSize(favicon_base::IconType icon_type) {
518 switch (icon_type) {
519 case favicon_base::FAVICON:
520 #if defined(OS_ANDROID)
521 return 192;
522 #else
523 return gfx::ImageSkia::GetMaxSupportedScale() * gfx::kFaviconSize;
524 #endif
525 case favicon_base::TOUCH_ICON:
526 case favicon_base::TOUCH_PRECOMPOSED_ICON:
527 return kTouchIconSize;
528 case favicon_base::INVALID_ICON:
529 return 0;
531 NOTREACHED();
532 return 0;
535 void FaviconHandler::OnFaviconDataForInitialURLFromFaviconService(
536 const std::vector<favicon_base::FaviconRawBitmapResult>&
537 favicon_bitmap_results) {
538 if (PageChangedSinceFaviconWasRequested())
539 return;
540 got_favicon_from_history_ = true;
541 history_results_ = favicon_bitmap_results;
542 bool has_results = !favicon_bitmap_results.empty();
543 favicon_expired_or_incomplete_ = has_results && HasExpiredOrIncompleteResult(
544 preferred_icon_size(), favicon_bitmap_results);
545 if (has_results && icon_types_ == favicon_base::FAVICON &&
546 !driver_->GetActiveFaviconValidity() &&
547 (!current_candidate() ||
548 DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) {
549 if (HasValidResult(favicon_bitmap_results)) {
550 // The db knows the favicon (although it may be out of date) and the entry
551 // doesn't have an icon. Set the favicon now, and if the favicon turns out
552 // to be expired (or the wrong url) we'll fetch later on. This way the
553 // user doesn't see a flash of the default favicon.
554 SetFaviconOnActivePage(favicon_bitmap_results);
555 } else {
556 // If |favicon_bitmap_results| does not have any valid results, treat the
557 // favicon as if it's expired.
558 // TODO(pkotwicz): Do something better.
559 favicon_expired_or_incomplete_ = true;
562 if (has_results && !favicon_expired_or_incomplete_) {
563 if (current_candidate() &&
564 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)) {
565 // Mapping in the database is wrong. DownloadFavIconOrAskHistory will
566 // update the mapping for this url and download the favicon if we don't
567 // already have it.
568 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(),
569 current_candidate()->icon_url,
570 current_candidate()->icon_type);
572 } else if (current_candidate()) {
573 // We know the official url for the favicon, but either don't have the
574 // favicon or it's expired. Continue on to DownloadFaviconOrAskHistory to
575 // either download or check history again.
576 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(),
577 current_candidate()->icon_url,
578 current_candidate()->icon_type);
580 // else we haven't got the icon url. When we get it we'll ask the
581 // renderer to download the icon.
584 void FaviconHandler::DownloadFaviconOrAskFaviconService(
585 const GURL& page_url,
586 const GURL& icon_url,
587 favicon_base::IconType icon_type) {
588 if (favicon_expired_or_incomplete_) {
589 // We have the mapping, but the favicon is out of date. Download it now.
590 ScheduleDownload(page_url, icon_url, icon_type);
591 } else if (client_->GetFaviconService()) {
592 // We don't know the favicon, but we may have previously downloaded the
593 // favicon for another page that shares the same favicon. Ask for the
594 // favicon given the favicon URL.
595 if (driver_->IsOffTheRecord()) {
596 GetFaviconFromFaviconService(
597 icon_url, icon_type,
598 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)),
599 &cancelable_task_tracker_);
600 } else {
601 // Ask the history service for the icon. This does two things:
602 // 1. Attempts to fetch the favicon data from the database.
603 // 2. If the favicon exists in the database, this updates the database to
604 // include the mapping between the page url and the favicon url.
605 // This is asynchronous. The history service will call back when done.
606 UpdateFaviconMappingAndFetch(
607 page_url, icon_url, icon_type,
608 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)),
609 &cancelable_task_tracker_);
614 void FaviconHandler::OnFaviconData(const std::vector<
615 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) {
616 if (PageChangedSinceFaviconWasRequested())
617 return;
619 bool has_results = !favicon_bitmap_results.empty();
620 bool has_expired_or_incomplete_result = HasExpiredOrIncompleteResult(
621 preferred_icon_size(), favicon_bitmap_results);
623 if (has_results && icon_types_ == favicon_base::FAVICON) {
624 if (HasValidResult(favicon_bitmap_results)) {
625 // There is a favicon, set it now. If expired we'll download the current
626 // one again, but at least the user will get some icon instead of the
627 // default and most likely the current one is fine anyway.
628 SetFaviconOnActivePage(favicon_bitmap_results);
630 if (has_expired_or_incomplete_result) {
631 // The favicon is out of date. Request the current one.
632 ScheduleDownload(driver_->GetActiveURL(),
633 driver_->GetActiveFaviconURL(),
634 favicon_base::FAVICON);
636 } else if (current_candidate() &&
637 (!has_results || has_expired_or_incomplete_result ||
638 !(DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)))) {
639 // We don't know the favicon, it is out of date or its type is not same as
640 // one got from page. Request the current one.
641 ScheduleDownload(driver_->GetActiveURL(),
642 current_candidate()->icon_url,
643 current_candidate()->icon_type);
645 history_results_ = favicon_bitmap_results;
648 int FaviconHandler::ScheduleDownload(const GURL& url,
649 const GURL& image_url,
650 favicon_base::IconType icon_type) {
651 // A max bitmap size is specified to avoid receiving huge bitmaps in
652 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload()
653 // for more details about the max bitmap size.
654 const int download_id = DownloadFavicon(image_url,
655 GetMaximalIconSize(icon_type));
656 if (download_id) {
657 // Download ids should be unique.
658 DCHECK(download_requests_.find(download_id) == download_requests_.end());
659 download_requests_[download_id] =
660 DownloadRequest(url, image_url, icon_type);
663 return download_id;
666 void FaviconHandler::SortAndPruneImageUrls() {
667 for (std::vector<FaviconURL>::iterator i = image_urls_.begin();
668 i != image_urls_.end(); ++i) {
669 if (i->icon_sizes.empty())
670 continue;
672 gfx::Size largest = i->icon_sizes[GetLargestSizeIndex(i->icon_sizes)];
673 i->icon_sizes.clear();
674 i->icon_sizes.push_back(largest);
676 std::stable_sort(image_urls_.begin(), image_urls_.end(),
677 CompareIconSize);