Chromium Code Reviews| 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 #include "components/favicon/core/favicon_handler.h" | 5 #include "components/favicon/core/favicon_handler.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 319 | 319 |
| 320 void FaviconHandler::SetFavicon(const GURL& icon_url, | 320 void FaviconHandler::SetFavicon(const GURL& icon_url, |
| 321 const gfx::Image& image, | 321 const gfx::Image& image, |
| 322 favicon_base::IconType icon_type) { | 322 favicon_base::IconType icon_type) { |
| 323 if (ShouldSaveFavicon()) | 323 if (ShouldSaveFavicon()) |
| 324 SetHistoryFavicons(url_, icon_url, icon_type, image); | 324 SetHistoryFavicons(url_, icon_url, icon_type, image); |
| 325 | 325 |
| 326 NotifyFaviconUpdated(icon_url, icon_type, image); | 326 NotifyFaviconUpdated(icon_url, icon_type, image); |
| 327 } | 327 } |
| 328 | 328 |
| 329 void FaviconHandler::CurrentCandidateFinished(bool exact_match_found) { | |
| 330 if (!exact_match_found && current_candidate_index_ + 1 < image_urls_.size()) { | |
| 331 // Process the next candidate. | |
| 332 ++current_candidate_index_; | |
| 333 DownloadCurrentCandidateOrAskFaviconService(); | |
| 334 } else { | |
| 335 // We have either found the ideal candidate or run out of candidates. | |
| 336 if (best_favicon_candidate_.icon_type != favicon_base::INVALID_ICON) { | |
| 337 // No more icons to request, set the favicon from the candidate. | |
| 338 SetFavicon(best_favicon_candidate_.image_url, | |
| 339 best_favicon_candidate_.image, | |
| 340 best_favicon_candidate_.icon_type); | |
| 341 } | |
| 342 // Clear download related state. | |
| 343 download_requests_.clear(); | |
| 344 current_candidate_index_ = image_urls_.size(); | |
| 345 best_favicon_candidate_ = FaviconCandidate(); | |
| 346 } | |
| 347 } | |
| 348 | |
| 329 void FaviconHandler::NotifyFaviconUpdated( | 349 void FaviconHandler::NotifyFaviconUpdated( |
| 330 const std::vector<favicon_base::FaviconRawBitmapResult>& | 350 const std::vector<favicon_base::FaviconRawBitmapResult>& |
| 331 favicon_bitmap_results) { | 351 favicon_bitmap_results) { |
| 332 if (favicon_bitmap_results.empty()) | 352 if (favicon_bitmap_results.empty()) |
| 333 return; | 353 return; |
| 334 | 354 |
| 335 gfx::Image resized_image = favicon_base::SelectFaviconFramesFromPNGs( | 355 gfx::Image resized_image = favicon_base::SelectFaviconFramesFromPNGs( |
| 336 favicon_bitmap_results, | 356 favicon_bitmap_results, |
| 337 favicon_base::GetFaviconScales(), | 357 favicon_base::GetFaviconScales(), |
| 338 preferred_icon_size()); | 358 preferred_icon_size()); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 435 DownloadRequest download_request = i->second; | 455 DownloadRequest download_request = i->second; |
| 436 download_requests_.erase(i); | 456 download_requests_.erase(i); |
| 437 | 457 |
| 438 if (!current_candidate() || | 458 if (!current_candidate() || |
| 439 !DoUrlAndIconMatch(*current_candidate(), | 459 !DoUrlAndIconMatch(*current_candidate(), |
| 440 image_url, | 460 image_url, |
| 441 download_request.icon_type)) { | 461 download_request.icon_type)) { |
| 442 return; | 462 return; |
| 443 } | 463 } |
| 444 | 464 |
| 445 bool request_next_icon = true; | 465 bool exact_match_found = false; |
| 446 if (!bitmaps.empty()) { | 466 if (!bitmaps.empty()) { |
| 447 float score = 0.0f; | 467 float score = 0.0f; |
| 448 gfx::ImageSkia image_skia; | 468 gfx::ImageSkia image_skia; |
| 449 if (download_largest_icon_) { | 469 if (download_largest_icon_) { |
| 450 int index = -1; | 470 int index = -1; |
| 451 // Use the largest bitmap if FaviconURL doesn't have sizes attribute. | 471 // Use the largest bitmap if FaviconURL doesn't have sizes attribute. |
| 452 if (current_candidate()->icon_sizes.empty()) { | 472 if (current_candidate()->icon_sizes.empty()) { |
| 453 index = GetLargestSizeIndex(original_bitmap_sizes); | 473 index = GetLargestSizeIndex(original_bitmap_sizes); |
| 454 } else { | 474 } else { |
| 455 index = GetIndexBySize(original_bitmap_sizes, | 475 index = GetIndexBySize(original_bitmap_sizes, |
| 456 current_candidate()->icon_sizes[0]); | 476 current_candidate()->icon_sizes[0]); |
| 457 // Find largest bitmap if there is no one exactly matched. | 477 // Find largest bitmap if there is no one exactly matched. |
| 458 if (index == -1) | 478 if (index == -1) |
| 459 index = GetLargestSizeIndex(original_bitmap_sizes); | 479 index = GetLargestSizeIndex(original_bitmap_sizes); |
| 460 } | 480 } |
| 461 image_skia = gfx::ImageSkia(gfx::ImageSkiaRep(bitmaps[index], 1)); | 481 image_skia = gfx::ImageSkia(gfx::ImageSkiaRep(bitmaps[index], 1)); |
| 462 } else { | 482 } else { |
| 463 image_skia = CreateFaviconImageSkia(bitmaps, | 483 image_skia = CreateFaviconImageSkia(bitmaps, |
| 464 original_bitmap_sizes, | 484 original_bitmap_sizes, |
| 465 preferred_icon_size(), | 485 preferred_icon_size(), |
| 466 &score); | 486 &score); |
| 467 } | 487 } |
| 468 | 488 |
| 469 if (!image_skia.isNull()) { | 489 if (!image_skia.isNull()) { |
| 470 gfx::Image image(image_skia); | 490 gfx::Image image(image_skia); |
| 471 // The downloaded icon is still valid when there is no FaviconURL update | 491 // The downloaded icon is still valid when there is no FaviconURL update |
| 472 // during the downloading. | 492 // during the downloading. |
| 473 request_next_icon = !UpdateFaviconCandidate(image_url, image, score, | 493 exact_match_found = UpdateFaviconCandidate(image_url, image, score, |
| 474 download_request.icon_type); | 494 download_request.icon_type); |
| 475 } | 495 } |
| 476 } | 496 } |
| 477 | 497 |
| 478 if (request_next_icon && current_candidate_index_ + 1 < image_urls_.size()) { | 498 CurrentCandidateFinished(exact_match_found); |
| 479 // Process the next candidate. | |
| 480 ++current_candidate_index_; | |
| 481 DownloadCurrentCandidateOrAskFaviconService(); | |
| 482 } else { | |
| 483 // We have either found the ideal candidate or run out of candidates. | |
| 484 if (best_favicon_candidate_.icon_type != favicon_base::INVALID_ICON) { | |
| 485 // No more icons to request, set the favicon from the candidate. | |
| 486 SetFavicon(best_favicon_candidate_.image_url, | |
| 487 best_favicon_candidate_.image, | |
| 488 best_favicon_candidate_.icon_type); | |
| 489 } | |
| 490 // Clear download related state. | |
| 491 download_requests_.clear(); | |
| 492 current_candidate_index_ = image_urls_.size(); | |
| 493 best_favicon_candidate_ = FaviconCandidate(); | |
| 494 } | |
| 495 } | 499 } |
| 496 | 500 |
| 497 bool FaviconHandler::HasPendingTasksForTest() { | 501 bool FaviconHandler::HasPendingTasksForTest() { |
| 498 return !download_requests_.empty() || | 502 return !download_requests_.empty() || |
| 499 cancelable_task_tracker_.HasTrackedTasks(); | 503 cancelable_task_tracker_.HasTrackedTasks(); |
| 500 } | 504 } |
| 501 | 505 |
| 502 void FaviconHandler::UpdateFaviconMappingAndFetch( | 506 void FaviconHandler::UpdateFaviconMappingAndFetch( |
| 503 const GURL& page_url, | 507 const GURL& page_url, |
| 504 const GURL& icon_url, | 508 const GURL& icon_url, |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 654 | 658 |
| 655 if (has_expired_or_incomplete_result) { | 659 if (has_expired_or_incomplete_result) { |
| 656 ScheduleDownload(current_candidate()->icon_url, | 660 ScheduleDownload(current_candidate()->icon_url, |
| 657 current_candidate()->icon_type); | 661 current_candidate()->icon_type); |
| 658 } | 662 } |
| 659 } | 663 } |
| 660 | 664 |
| 661 void FaviconHandler::ScheduleDownload(const GURL& image_url, | 665 void FaviconHandler::ScheduleDownload(const GURL& image_url, |
| 662 favicon_base::IconType icon_type) { | 666 favicon_base::IconType icon_type) { |
| 663 DCHECK(image_url.is_valid()); | 667 DCHECK(image_url.is_valid()); |
| 668 if (service_ && service_->WasUnableToDownloadFavicon(image_url)) { | |
|
pkotwicz
2017/03/03 18:31:16
Can you call OnDidDownloadFavicon() instead?
This
mastiz
2017/03/03 20:37:01
Done.
| |
| 669 DVLOG(1) << "Skip Failed FavIcon: " << image_url; | |
| 670 CurrentCandidateFinished(/*found_exact_match=*/false); | |
| 671 return; | |
| 672 } | |
| 664 // A max bitmap size is specified to avoid receiving huge bitmaps in | 673 // A max bitmap size is specified to avoid receiving huge bitmaps in |
| 665 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() | 674 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() |
| 666 // for more details about the max bitmap size. | 675 // for more details about the max bitmap size. |
| 667 const int download_id = | 676 const int download_id = |
| 668 delegate_->DownloadImage(image_url, GetMaximalIconSize(icon_type), | 677 delegate_->DownloadImage(image_url, GetMaximalIconSize(icon_type), |
| 669 base::Bind(&FaviconHandler::OnDidDownloadFavicon, | 678 base::Bind(&FaviconHandler::OnDidDownloadFavicon, |
| 670 weak_ptr_factory_.GetWeakPtr())); | 679 weak_ptr_factory_.GetWeakPtr())); |
| 671 | 680 |
| 672 // Download ids should be unique. | 681 // Download ids should be unique. |
| 673 DCHECK(download_requests_.find(download_id) == download_requests_.end()); | 682 DCHECK(download_requests_.find(download_id) == download_requests_.end()); |
| 674 download_requests_[download_id] = DownloadRequest(image_url, icon_type); | 683 download_requests_[download_id] = DownloadRequest(image_url, icon_type); |
| 675 | 684 |
| 676 if (download_id == 0) { | 685 if (download_id == 0) { |
| 677 // If DownloadFavicon() did not start a download, it returns a download id | 686 // If DownloadFavicon() did not start a download, it returns a download id |
| 678 // of 0. We still need to call OnDidDownloadFavicon() because the method is | 687 // of 0. We still need to call OnDidDownloadFavicon() because the method is |
| 679 // responsible for initiating the data request for the next candidate. | 688 // responsible for initiating the data request for the next candidate. |
| 680 OnDidDownloadFavicon(download_id, 0, image_url, std::vector<SkBitmap>(), | 689 OnDidDownloadFavicon(download_id, 0, image_url, std::vector<SkBitmap>(), |
| 681 std::vector<gfx::Size>()); | 690 std::vector<gfx::Size>()); |
| 682 } | 691 } |
| 683 } | 692 } |
| 684 | 693 |
| 685 } // namespace favicon | 694 } // namespace favicon |
| OLD | NEW |