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 "chrome/browser/favicon/favicon_handler.h" | 5 #include "chrome/browser/favicon/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 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 151 if (!b1.icon_sizes.empty()) | 151 if (!b1.icon_sizes.empty()) |
| 152 area1 = b1.icon_sizes.front().GetArea(); | 152 area1 = b1.icon_sizes.front().GetArea(); |
| 153 | 153 |
| 154 int area2 = 0; | 154 int area2 = 0; |
| 155 if (!b2.icon_sizes.empty()) | 155 if (!b2.icon_sizes.empty()) |
| 156 area2 = b2.icon_sizes.front().GetArea(); | 156 area2 = b2.icon_sizes.front().GetArea(); |
| 157 | 157 |
| 158 return area1 > area2; | 158 return area1 > area2; |
| 159 } | 159 } |
| 160 | 160 |
| 161 // Return the first |favicon_bitmap_results|'s icon_url if the result isn't | |
| 162 // empty, otherwise empty GURL is returned. | |
| 163 inline GURL GetIconURL(const std::vector< | |
|
sky
2014/10/30 03:34:30
GetFirstIconURL
sky
2014/10/30 03:34:31
Is it really necessary to inline here?
michaelbai
2014/10/30 19:22:50
Function was removed
michaelbai
2014/10/30 19:22:50
Function was removed
| |
| 164 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) { | |
| 165 // The history service sends back results for a single icon URL, so it does | |
| 166 // not matter which result we get the |icon_url| from. | |
| 167 return favicon_bitmap_results.empty() ? | |
| 168 GURL() : favicon_bitmap_results[0].icon_url; | |
| 169 } | |
| 170 | |
| 161 } // namespace | 171 } // namespace |
| 162 | 172 |
| 163 //////////////////////////////////////////////////////////////////////////////// | 173 //////////////////////////////////////////////////////////////////////////////// |
| 164 | 174 |
| 165 FaviconHandler::DownloadRequest::DownloadRequest() | 175 FaviconHandler::DownloadRequest::DownloadRequest() |
| 166 : icon_type(favicon_base::INVALID_ICON) {} | 176 : icon_type(favicon_base::INVALID_ICON) {} |
| 167 | 177 |
| 168 FaviconHandler::DownloadRequest::~DownloadRequest() { | 178 FaviconHandler::DownloadRequest::~DownloadRequest() { |
| 169 } | 179 } |
| 170 | 180 |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 281 return exact_match; | 291 return exact_match; |
| 282 } | 292 } |
| 283 | 293 |
| 284 void FaviconHandler::SetFavicon(const GURL& url, | 294 void FaviconHandler::SetFavicon(const GURL& url, |
| 285 const GURL& icon_url, | 295 const GURL& icon_url, |
| 286 const gfx::Image& image, | 296 const gfx::Image& image, |
| 287 favicon_base::IconType icon_type) { | 297 favicon_base::IconType icon_type) { |
| 288 if (client_->GetFaviconService() && ShouldSaveFavicon(url)) | 298 if (client_->GetFaviconService() && ShouldSaveFavicon(url)) |
| 289 SetHistoryFavicons(url, icon_url, icon_type, image); | 299 SetHistoryFavicons(url, icon_url, icon_type, image); |
| 290 | 300 |
| 291 if (UrlMatches(url, url_) && icon_type == favicon_base::FAVICON) { | 301 if (!UrlMatches(url, url_) || PageChangedSinceFaviconWasRequested()) |
| 292 if (!PageChangedSinceFaviconWasRequested()) | 302 return; |
| 303 | |
| 304 if (icon_type == favicon_base::FAVICON && !download_largest_icon_) | |
| 293 SetFaviconOnActivePage(icon_url, image); | 305 SetFaviconOnActivePage(icon_url, image); |
|
sky
2014/10/30 03:34:30
nit: spacing is off now (use git cl format).
michaelbai
2014/10/30 19:22:50
Acknowledged.
| |
| 294 } | 306 driver_->OnFaviconAvailable(image); |
| 295 } | 307 } |
| 296 | 308 |
| 297 void FaviconHandler::SetFaviconOnActivePage(const std::vector< | 309 gfx::Image FaviconHandler::SelectFaviconFrames(const std::vector< |
| 298 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) { | 310 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) { |
| 299 gfx::Image resized_image = favicon_base::SelectFaviconFramesFromPNGs( | 311 return favicon_base::SelectFaviconFramesFromPNGs( |
| 300 favicon_bitmap_results, | 312 favicon_bitmap_results, |
| 301 favicon_base::GetFaviconScales(), | 313 favicon_base::GetFaviconScales(), |
| 302 preferred_icon_size()); | 314 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); | |
| 308 } | 315 } |
| 309 | 316 |
| 310 void FaviconHandler::SetFaviconOnActivePage(const GURL& icon_url, | 317 void FaviconHandler::SetFaviconOnActivePage(const GURL& icon_url, |
| 311 const gfx::Image& image) { | 318 const gfx::Image& image) { |
| 312 // No matter what happens, we need to mark the favicon as being set. | 319 // No matter what happens, we need to mark the favicon as being set. |
| 313 driver_->SetActiveFaviconValidity(true); | 320 driver_->SetActiveFaviconValidity(true); |
| 314 | 321 |
| 315 bool icon_url_changed = driver_->GetActiveFaviconURL() != icon_url; | 322 bool icon_url_changed = driver_->GetActiveFaviconURL() != icon_url; |
| 316 driver_->SetActiveFaviconURL(icon_url); | 323 driver_->SetActiveFaviconURL(icon_url); |
| 317 | 324 |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 535 void FaviconHandler::OnFaviconDataForInitialURLFromFaviconService( | 542 void FaviconHandler::OnFaviconDataForInitialURLFromFaviconService( |
| 536 const std::vector<favicon_base::FaviconRawBitmapResult>& | 543 const std::vector<favicon_base::FaviconRawBitmapResult>& |
| 537 favicon_bitmap_results) { | 544 favicon_bitmap_results) { |
| 538 if (PageChangedSinceFaviconWasRequested()) | 545 if (PageChangedSinceFaviconWasRequested()) |
| 539 return; | 546 return; |
| 540 got_favicon_from_history_ = true; | 547 got_favicon_from_history_ = true; |
| 541 history_results_ = favicon_bitmap_results; | 548 history_results_ = favicon_bitmap_results; |
| 542 bool has_results = !favicon_bitmap_results.empty(); | 549 bool has_results = !favicon_bitmap_results.empty(); |
| 543 favicon_expired_or_incomplete_ = has_results && HasExpiredOrIncompleteResult( | 550 favicon_expired_or_incomplete_ = has_results && HasExpiredOrIncompleteResult( |
| 544 preferred_icon_size(), favicon_bitmap_results); | 551 preferred_icon_size(), favicon_bitmap_results); |
| 552 bool has_valid_result = HasValidResult(favicon_bitmap_results); | |
| 553 gfx::Image image; | |
| 554 if (has_valid_result) | |
| 555 image = SelectFaviconFrames(favicon_bitmap_results); | |
| 545 if (has_results && icon_types_ == favicon_base::FAVICON && | 556 if (has_results && icon_types_ == favicon_base::FAVICON && |
| 546 !driver_->GetActiveFaviconValidity() && | 557 !driver_->GetActiveFaviconValidity() && |
| 547 (!current_candidate() || | 558 (!current_candidate() || |
| 548 DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) { | 559 DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) { |
| 549 if (HasValidResult(favicon_bitmap_results)) { | 560 if (has_valid_result) { |
| 550 // The db knows the favicon (although it may be out of date) and the entry | 561 // 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 | 562 // 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 | 563 // 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. | 564 // user doesn't see a flash of the default favicon. |
| 554 SetFaviconOnActivePage(favicon_bitmap_results); | 565 if (!download_largest_icon_) |
| 566 SetFaviconOnActivePage(GetIconURL(favicon_bitmap_results), image); | |
| 555 } else { | 567 } else { |
| 556 // If |favicon_bitmap_results| does not have any valid results, treat the | 568 // If |favicon_bitmap_results| does not have any valid results, treat the |
| 557 // favicon as if it's expired. | 569 // favicon as if it's expired. |
| 558 // TODO(pkotwicz): Do something better. | 570 // TODO(pkotwicz): Do something better. |
| 559 favicon_expired_or_incomplete_ = true; | 571 favicon_expired_or_incomplete_ = true; |
| 560 } | 572 } |
| 561 } | 573 } |
| 562 if (has_results && !favicon_expired_or_incomplete_) { | 574 if (has_results && !favicon_expired_or_incomplete_) { |
| 563 if (current_candidate() && | 575 if (current_candidate() && |
| 564 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)) { | 576 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)) { |
| 565 // Mapping in the database is wrong. DownloadFavIconOrAskHistory will | 577 // Mapping in the database is wrong. DownloadFavIconOrAskHistory will |
| 566 // update the mapping for this url and download the favicon if we don't | 578 // update the mapping for this url and download the favicon if we don't |
| 567 // already have it. | 579 // already have it. |
| 568 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(), | 580 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(), |
| 569 current_candidate()->icon_url, | 581 current_candidate()->icon_url, |
| 570 current_candidate()->icon_type); | 582 current_candidate()->icon_type); |
| 571 } | 583 } |
| 572 } else if (current_candidate()) { | 584 } else if (current_candidate()) { |
| 573 // We know the official url for the favicon, but either don't have the | 585 // 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 | 586 // favicon or it's expired. Continue on to DownloadFaviconOrAskHistory to |
| 575 // either download or check history again. | 587 // either download or check history again. |
| 576 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(), | 588 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(), |
| 577 current_candidate()->icon_url, | 589 current_candidate()->icon_url, |
| 578 current_candidate()->icon_type); | 590 current_candidate()->icon_type); |
| 579 } | 591 } |
| 580 // else we haven't got the icon url. When we get it we'll ask the | 592 // else we haven't got the icon url. When we get it we'll ask the |
| 581 // renderer to download the icon. | 593 // renderer to download the icon. |
| 594 | |
| 595 if (has_valid_result) { | |
| 596 driver_->OnFaviconAvailable(image); | |
| 597 } | |
| 582 } | 598 } |
| 583 | 599 |
| 584 void FaviconHandler::DownloadFaviconOrAskFaviconService( | 600 void FaviconHandler::DownloadFaviconOrAskFaviconService( |
| 585 const GURL& page_url, | 601 const GURL& page_url, |
| 586 const GURL& icon_url, | 602 const GURL& icon_url, |
| 587 favicon_base::IconType icon_type) { | 603 favicon_base::IconType icon_type) { |
| 588 if (favicon_expired_or_incomplete_) { | 604 if (favicon_expired_or_incomplete_) { |
| 589 // We have the mapping, but the favicon is out of date. Download it now. | 605 // We have the mapping, but the favicon is out of date. Download it now. |
| 590 ScheduleDownload(page_url, icon_url, icon_type); | 606 ScheduleDownload(page_url, icon_url, icon_type); |
| 591 } else if (client_->GetFaviconService()) { | 607 } else if (client_->GetFaviconService()) { |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 612 } | 628 } |
| 613 | 629 |
| 614 void FaviconHandler::OnFaviconData(const std::vector< | 630 void FaviconHandler::OnFaviconData(const std::vector< |
| 615 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) { | 631 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) { |
| 616 if (PageChangedSinceFaviconWasRequested()) | 632 if (PageChangedSinceFaviconWasRequested()) |
| 617 return; | 633 return; |
| 618 | 634 |
| 619 bool has_results = !favicon_bitmap_results.empty(); | 635 bool has_results = !favicon_bitmap_results.empty(); |
| 620 bool has_expired_or_incomplete_result = HasExpiredOrIncompleteResult( | 636 bool has_expired_or_incomplete_result = HasExpiredOrIncompleteResult( |
| 621 preferred_icon_size(), favicon_bitmap_results); | 637 preferred_icon_size(), favicon_bitmap_results); |
| 622 | 638 bool has_valid_result = HasValidResult(favicon_bitmap_results); |
| 639 gfx::Image image; | |
| 640 if (has_valid_result) | |
| 641 image = SelectFaviconFrames(favicon_bitmap_results); | |
| 623 if (has_results && icon_types_ == favicon_base::FAVICON) { | 642 if (has_results && icon_types_ == favicon_base::FAVICON) { |
| 624 if (HasValidResult(favicon_bitmap_results)) { | 643 if (has_valid_result) { |
| 625 // There is a favicon, set it now. If expired we'll download the current | 644 // 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 | 645 // 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. | 646 // default and most likely the current one is fine anyway. |
| 628 SetFaviconOnActivePage(favicon_bitmap_results); | 647 if (!download_largest_icon_) |
| 648 SetFaviconOnActivePage(GetIconURL(favicon_bitmap_results), image); | |
| 629 } | 649 } |
| 630 if (has_expired_or_incomplete_result) { | 650 if (has_expired_or_incomplete_result) { |
| 631 // The favicon is out of date. Request the current one. | 651 // The favicon is out of date. Request the current one. |
| 632 ScheduleDownload(driver_->GetActiveURL(), | 652 ScheduleDownload(driver_->GetActiveURL(), |
| 633 driver_->GetActiveFaviconURL(), | 653 driver_->GetActiveFaviconURL(), |
| 634 favicon_base::FAVICON); | 654 favicon_base::FAVICON); |
| 635 } | 655 } |
| 636 } else if (current_candidate() && | 656 } else if (current_candidate() && |
| 637 (!has_results || has_expired_or_incomplete_result || | 657 (!has_results || has_expired_or_incomplete_result || |
| 638 !(DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)))) { | 658 !(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 | 659 // 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. | 660 // one got from page. Request the current one. |
| 641 ScheduleDownload(driver_->GetActiveURL(), | 661 ScheduleDownload(driver_->GetActiveURL(), |
| 642 current_candidate()->icon_url, | 662 current_candidate()->icon_url, |
| 643 current_candidate()->icon_type); | 663 current_candidate()->icon_type); |
| 644 } | 664 } |
| 645 history_results_ = favicon_bitmap_results; | 665 history_results_ = favicon_bitmap_results; |
| 666 | |
| 667 if (has_valid_result) | |
| 668 driver_->OnFaviconAvailable(image); | |
| 646 } | 669 } |
| 647 | 670 |
| 648 int FaviconHandler::ScheduleDownload(const GURL& url, | 671 int FaviconHandler::ScheduleDownload(const GURL& url, |
| 649 const GURL& image_url, | 672 const GURL& image_url, |
| 650 favicon_base::IconType icon_type) { | 673 favicon_base::IconType icon_type) { |
| 651 // A max bitmap size is specified to avoid receiving huge bitmaps in | 674 // A max bitmap size is specified to avoid receiving huge bitmaps in |
| 652 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() | 675 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() |
| 653 // for more details about the max bitmap size. | 676 // for more details about the max bitmap size. |
| 654 const int download_id = DownloadFavicon(image_url, | 677 const int download_id = DownloadFavicon(image_url, |
| 655 GetMaximalIconSize(icon_type)); | 678 GetMaximalIconSize(icon_type)); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 669 if (i->icon_sizes.empty()) | 692 if (i->icon_sizes.empty()) |
| 670 continue; | 693 continue; |
| 671 | 694 |
| 672 gfx::Size largest = i->icon_sizes[GetLargestSizeIndex(i->icon_sizes)]; | 695 gfx::Size largest = i->icon_sizes[GetLargestSizeIndex(i->icon_sizes)]; |
| 673 i->icon_sizes.clear(); | 696 i->icon_sizes.clear(); |
| 674 i->icon_sizes.push_back(largest); | 697 i->icon_sizes.push_back(largest); |
| 675 } | 698 } |
| 676 std::stable_sort(image_urls_.begin(), image_urls_.end(), | 699 std::stable_sort(image_urls_.begin(), image_urls_.end(), |
| 677 CompareIconSize); | 700 CompareIconSize); |
| 678 } | 701 } |
| OLD | NEW |