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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 // Return true if |bitmap_result| is expired. | 67 // Return true if |bitmap_result| is expired. |
68 bool IsExpired(const favicon_base::FaviconRawBitmapResult& bitmap_result) { | 68 bool IsExpired(const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
69 return bitmap_result.expired; | 69 return bitmap_result.expired; |
70 } | 70 } |
71 | 71 |
72 // Return true if |bitmap_result| is valid. | 72 // Return true if |bitmap_result| is valid. |
73 bool IsValid(const favicon_base::FaviconRawBitmapResult& bitmap_result) { | 73 bool IsValid(const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
74 return bitmap_result.is_valid(); | 74 return bitmap_result.is_valid(); |
75 } | 75 } |
76 | 76 |
77 // Returns true if at least one of the bitmaps in |bitmap_results| is expired or | 77 // Returns true if |bitmap_results| is non-empty and: |
78 // if |bitmap_results| is missing favicons for |desired_size_in_dip| and one of | 78 // - At least one of the bitmaps in |bitmap_results| is expired |
79 // the scale factors in favicon_base::GetFaviconScales(). | 79 // OR |
| 80 // - |bitmap_results| is missing favicons for |desired_size_in_dip| and one of |
| 81 // the scale factors in favicon_base::GetFaviconScales(). |
80 bool HasExpiredOrIncompleteResult( | 82 bool HasExpiredOrIncompleteResult( |
81 int desired_size_in_dip, | 83 int desired_size_in_dip, |
82 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) { | 84 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) { |
| 85 if (bitmap_results.empty()) |
| 86 return false; |
| 87 |
83 // Check if at least one of the bitmaps is expired. | 88 // Check if at least one of the bitmaps is expired. |
84 std::vector<favicon_base::FaviconRawBitmapResult>::const_iterator it = | 89 std::vector<favicon_base::FaviconRawBitmapResult>::const_iterator it = |
85 std::find_if(bitmap_results.begin(), bitmap_results.end(), IsExpired); | 90 std::find_if(bitmap_results.begin(), bitmap_results.end(), IsExpired); |
86 if (it != bitmap_results.end()) | 91 if (it != bitmap_results.end()) |
87 return true; | 92 return true; |
88 | 93 |
89 // Any favicon size is good if the desired size is 0. | 94 // Any favicon size is good if the desired size is 0. |
90 if (desired_size_in_dip == 0) | 95 if (desired_size_in_dip == 0) |
91 return false; | 96 return false; |
92 | 97 |
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
551 } | 556 } |
552 | 557 |
553 void FaviconHandler::OnFaviconDataForInitialURLFromFaviconService( | 558 void FaviconHandler::OnFaviconDataForInitialURLFromFaviconService( |
554 const std::vector<favicon_base::FaviconRawBitmapResult>& | 559 const std::vector<favicon_base::FaviconRawBitmapResult>& |
555 favicon_bitmap_results) { | 560 favicon_bitmap_results) { |
556 if (PageChangedSinceFaviconWasRequested()) | 561 if (PageChangedSinceFaviconWasRequested()) |
557 return; | 562 return; |
558 got_favicon_from_history_ = true; | 563 got_favicon_from_history_ = true; |
559 history_results_ = favicon_bitmap_results; | 564 history_results_ = favicon_bitmap_results; |
560 bool has_results = !favicon_bitmap_results.empty(); | 565 bool has_results = !favicon_bitmap_results.empty(); |
561 favicon_expired_or_incomplete_ = has_results && HasExpiredOrIncompleteResult( | 566 favicon_expired_or_incomplete_ = HasExpiredOrIncompleteResult( |
562 preferred_icon_size(), favicon_bitmap_results); | 567 preferred_icon_size(), favicon_bitmap_results); |
563 bool has_valid_result = HasValidResult(favicon_bitmap_results); | 568 bool has_valid_result = HasValidResult(favicon_bitmap_results); |
564 | 569 |
565 if (has_results && handler_type_ == FAVICON && | 570 if (has_results && handler_type_ == FAVICON && |
566 !download_largest_icon_ && !driver_->GetActiveFaviconValidity() && | 571 !download_largest_icon_ && !driver_->GetActiveFaviconValidity() && |
567 (!current_candidate() || | 572 (!current_candidate() || |
568 DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) { | 573 DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) { |
569 if (has_valid_result) { | 574 if (has_valid_result) { |
570 // The db knows the favicon (although it may be out of date) and the entry | 575 // The db knows the favicon (although it may be out of date) and the entry |
571 // doesn't have an icon. Set the favicon now, and if the favicon turns out | 576 // doesn't have an icon. Set the favicon now, and if the favicon turns out |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
636 | 641 |
637 void FaviconHandler::OnFaviconData(const std::vector< | 642 void FaviconHandler::OnFaviconData(const std::vector< |
638 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) { | 643 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) { |
639 if (PageChangedSinceFaviconWasRequested()) | 644 if (PageChangedSinceFaviconWasRequested()) |
640 return; | 645 return; |
641 | 646 |
642 bool has_results = !favicon_bitmap_results.empty(); | 647 bool has_results = !favicon_bitmap_results.empty(); |
643 bool has_expired_or_incomplete_result = HasExpiredOrIncompleteResult( | 648 bool has_expired_or_incomplete_result = HasExpiredOrIncompleteResult( |
644 preferred_icon_size(), favicon_bitmap_results); | 649 preferred_icon_size(), favicon_bitmap_results); |
645 bool has_valid_result = HasValidResult(favicon_bitmap_results); | 650 bool has_valid_result = HasValidResult(favicon_bitmap_results); |
| 651 history_results_ = favicon_bitmap_results; |
646 | 652 |
647 if (has_results && handler_type_ == FAVICON && !download_largest_icon_) { | 653 if (has_valid_result) { |
648 if (has_valid_result) { | 654 // There is a valid favicon. Notify any observers. It is useful to notify |
649 // There is a favicon, set it now. If expired we'll download the current | 655 // the observers even if the favicon is expired or incomplete (incorrect |
650 // one again, but at least the user will get some icon instead of the | 656 // size) because temporarily showing the user an expired favicon or |
651 // default and most likely the current one is fine anyway. | 657 // streched favicon is preferable to showing the user the default favicon. |
652 NotifyFaviconAvailable(favicon_bitmap_results); | 658 NotifyFaviconAvailable(favicon_bitmap_results); |
653 } | 659 } |
654 if (has_expired_or_incomplete_result) { | 660 |
655 // The favicon is out of date. Request the current one. | 661 if (!current_candidate() || |
656 ScheduleDownload(driver_->GetActiveURL(), | 662 (has_results && |
657 driver_->GetActiveFaviconURL(), | 663 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) { |
658 favicon_base::FAVICON); | 664 // The icon URLs have been updated since the favicon data was requested. |
659 } | 665 return; |
660 } else if (current_candidate() && | 666 } |
661 (!has_results || has_expired_or_incomplete_result || | 667 |
662 !(DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)))) { | 668 if (!has_results || has_expired_or_incomplete_result) { |
663 // We don't know the favicon, it is out of date or its type is not same as | |
664 // one got from page. Request the current one. | |
665 ScheduleDownload(driver_->GetActiveURL(), | 669 ScheduleDownload(driver_->GetActiveURL(), |
666 current_candidate()->icon_url, | 670 current_candidate()->icon_url, |
667 current_candidate()->icon_type); | 671 current_candidate()->icon_type); |
668 } | 672 } |
669 history_results_ = favicon_bitmap_results; | |
670 | |
671 if (has_valid_result && | |
672 (handler_type_ != FAVICON || download_largest_icon_)) { | |
673 NotifyFaviconAvailable(favicon_bitmap_results); | |
674 } | |
675 } | 673 } |
676 | 674 |
677 void FaviconHandler::ScheduleDownload(const GURL& url, | 675 void FaviconHandler::ScheduleDownload(const GURL& url, |
678 const GURL& image_url, | 676 const GURL& image_url, |
679 favicon_base::IconType icon_type) { | 677 favicon_base::IconType icon_type) { |
680 // A max bitmap size is specified to avoid receiving huge bitmaps in | 678 // A max bitmap size is specified to avoid receiving huge bitmaps in |
681 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() | 679 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() |
682 // for more details about the max bitmap size. | 680 // for more details about the max bitmap size. |
683 const int download_id = DownloadFavicon(image_url, | 681 const int download_id = DownloadFavicon(image_url, |
684 GetMaximalIconSize(icon_type)); | 682 GetMaximalIconSize(icon_type)); |
(...skipping 14 matching lines...) Expand all Loading... |
699 gfx::Size largest = | 697 gfx::Size largest = |
700 image_url.icon_sizes[GetLargestSizeIndex(image_url.icon_sizes)]; | 698 image_url.icon_sizes[GetLargestSizeIndex(image_url.icon_sizes)]; |
701 image_url.icon_sizes.clear(); | 699 image_url.icon_sizes.clear(); |
702 image_url.icon_sizes.push_back(largest); | 700 image_url.icon_sizes.push_back(largest); |
703 } | 701 } |
704 std::stable_sort(image_urls_.begin(), image_urls_.end(), | 702 std::stable_sort(image_urls_.begin(), image_urls_.end(), |
705 CompareIconSize); | 703 CompareIconSize); |
706 } | 704 } |
707 | 705 |
708 } // namespace favicon | 706 } // namespace favicon |
OLD | NEW |