| 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/browser/favicon_handler.h" | 5 #include "components/favicon/core/browser/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 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 image_url(image_url), | 195 image_url(image_url), |
| 196 image(image), | 196 image(image), |
| 197 score(score), | 197 score(score), |
| 198 icon_type(icon_type) {} | 198 icon_type(icon_type) {} |
| 199 | 199 |
| 200 //////////////////////////////////////////////////////////////////////////////// | 200 //////////////////////////////////////////////////////////////////////////////// |
| 201 | 201 |
| 202 FaviconHandler::FaviconHandler(FaviconService* service, | 202 FaviconHandler::FaviconHandler(FaviconService* service, |
| 203 FaviconClient* client, | 203 FaviconClient* client, |
| 204 FaviconDriver* driver, | 204 FaviconDriver* driver, |
| 205 Type icon_type, | 205 Type handler_type, |
| 206 bool download_largest_icon) | 206 bool download_largest_icon) |
| 207 : got_favicon_from_history_(false), | 207 : got_favicon_from_history_(false), |
| 208 favicon_expired_or_incomplete_(false), | 208 favicon_expired_or_incomplete_(false), |
| 209 icon_types_(icon_type == FAVICON | 209 handler_type_(handler_type), |
| 210 ? favicon_base::FAVICON | 210 icon_types_(FaviconHandler::GetIconTypesFromHandlerType(handler_type)), |
| 211 : favicon_base::TOUCH_ICON | | |
| 212 favicon_base::TOUCH_PRECOMPOSED_ICON), | |
| 213 download_largest_icon_(download_largest_icon), | 211 download_largest_icon_(download_largest_icon), |
| 214 service_(service), | 212 service_(service), |
| 215 client_(client), | 213 client_(client), |
| 216 driver_(driver) { | 214 driver_(driver) { |
| 217 DCHECK(driver_); | 215 DCHECK(driver_); |
| 218 } | 216 } |
| 219 | 217 |
| 220 FaviconHandler::~FaviconHandler() { | 218 FaviconHandler::~FaviconHandler() { |
| 221 } | 219 } |
| 222 | 220 |
| 221 // static |
| 222 int FaviconHandler::GetIconTypesFromHandlerType( |
| 223 FaviconHandler::Type handler_type) { |
| 224 switch (handler_type) { |
| 225 case FAVICON: |
| 226 return favicon_base::FAVICON; |
| 227 case TOUCH: // Falls through. |
| 228 case LARGE: |
| 229 return favicon_base::TOUCH_ICON | favicon_base::TOUCH_PRECOMPOSED_ICON; |
| 230 default: |
| 231 NOTREACHED(); |
| 232 } |
| 233 return 0; |
| 234 } |
| 235 |
| 223 void FaviconHandler::FetchFavicon(const GURL& url) { | 236 void FaviconHandler::FetchFavicon(const GURL& url) { |
| 224 cancelable_task_tracker_.TryCancelAll(); | 237 cancelable_task_tracker_.TryCancelAll(); |
| 225 | 238 |
| 226 url_ = url; | 239 url_ = url; |
| 227 | 240 |
| 228 favicon_expired_or_incomplete_ = got_favicon_from_history_ = false; | 241 favicon_expired_or_incomplete_ = got_favicon_from_history_ = false; |
| 229 image_urls_.clear(); | 242 image_urls_.clear(); |
| 230 | 243 |
| 231 // Request the favicon from the history service. In parallel to this the | 244 // Request the favicon from the history service. In parallel to this the |
| 232 // renderer is going to notify us (well WebContents) when the favicon url is | 245 // renderer is going to notify us (well WebContents) when the favicon url is |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 495 if (service_) { | 508 if (service_) { |
| 496 service_->GetFaviconForPageURL(page_url, icon_types, preferred_icon_size(), | 509 service_->GetFaviconForPageURL(page_url, icon_types, preferred_icon_size(), |
| 497 callback, tracker); | 510 callback, tracker); |
| 498 } | 511 } |
| 499 } | 512 } |
| 500 | 513 |
| 501 void FaviconHandler::SetHistoryFavicons(const GURL& page_url, | 514 void FaviconHandler::SetHistoryFavicons(const GURL& page_url, |
| 502 const GURL& icon_url, | 515 const GURL& icon_url, |
| 503 favicon_base::IconType icon_type, | 516 favicon_base::IconType icon_type, |
| 504 const gfx::Image& image) { | 517 const gfx::Image& image) { |
| 518 // TODO(huangs): Get the following to garbage collect if handler_type_ == ALL. |
| 505 if (service_) { | 519 if (service_) { |
| 506 service_->SetFavicons(page_url, icon_url, icon_type, image); | 520 service_->SetFavicons(page_url, icon_url, icon_type, image); |
| 507 } | 521 } |
| 508 } | 522 } |
| 509 | 523 |
| 510 bool FaviconHandler::ShouldSaveFavicon(const GURL& url) { | 524 bool FaviconHandler::ShouldSaveFavicon(const GURL& url) { |
| 511 if (!driver_->IsOffTheRecord()) | 525 if (!driver_->IsOffTheRecord()) |
| 512 return true; | 526 return true; |
| 513 | 527 |
| 514 // Otherwise store the favicon if the page is bookmarked. | 528 // Otherwise store the favicon if the page is bookmarked. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 538 favicon_bitmap_results) { | 552 favicon_bitmap_results) { |
| 539 if (PageChangedSinceFaviconWasRequested()) | 553 if (PageChangedSinceFaviconWasRequested()) |
| 540 return; | 554 return; |
| 541 got_favicon_from_history_ = true; | 555 got_favicon_from_history_ = true; |
| 542 history_results_ = favicon_bitmap_results; | 556 history_results_ = favicon_bitmap_results; |
| 543 bool has_results = !favicon_bitmap_results.empty(); | 557 bool has_results = !favicon_bitmap_results.empty(); |
| 544 favicon_expired_or_incomplete_ = has_results && HasExpiredOrIncompleteResult( | 558 favicon_expired_or_incomplete_ = has_results && HasExpiredOrIncompleteResult( |
| 545 preferred_icon_size(), favicon_bitmap_results); | 559 preferred_icon_size(), favicon_bitmap_results); |
| 546 bool has_valid_result = HasValidResult(favicon_bitmap_results); | 560 bool has_valid_result = HasValidResult(favicon_bitmap_results); |
| 547 | 561 |
| 548 if (has_results && icon_types_ == favicon_base::FAVICON && | 562 if (has_results && handler_type_ == FAVICON && |
| 549 !download_largest_icon_ && !driver_->GetActiveFaviconValidity() && | 563 !download_largest_icon_ && !driver_->GetActiveFaviconValidity() && |
| 550 (!current_candidate() || | 564 (!current_candidate() || |
| 551 DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) { | 565 DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) { |
| 552 if (has_valid_result) { | 566 if (has_valid_result) { |
| 553 // The db knows the favicon (although it may be out of date) and the entry | 567 // The db knows the favicon (although it may be out of date) and the entry |
| 554 // doesn't have an icon. Set the favicon now, and if the favicon turns out | 568 // doesn't have an icon. Set the favicon now, and if the favicon turns out |
| 555 // to be expired (or the wrong url) we'll fetch later on. This way the | 569 // to be expired (or the wrong url) we'll fetch later on. This way the |
| 556 // user doesn't see a flash of the default favicon. | 570 // user doesn't see a flash of the default favicon. |
| 557 NotifyFaviconAvailable(favicon_bitmap_results, true); | 571 NotifyFaviconAvailable(favicon_bitmap_results, true); |
| 558 } else { | 572 } else { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 576 // We know the official url for the favicon, but either don't have the | 590 // We know the official url for the favicon, but either don't have the |
| 577 // favicon or it's expired. Continue on to DownloadFaviconOrAskHistory to | 591 // favicon or it's expired. Continue on to DownloadFaviconOrAskHistory to |
| 578 // either download or check history again. | 592 // either download or check history again. |
| 579 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(), | 593 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(), |
| 580 current_candidate()->icon_url, | 594 current_candidate()->icon_url, |
| 581 current_candidate()->icon_type); | 595 current_candidate()->icon_type); |
| 582 } | 596 } |
| 583 // else we haven't got the icon url. When we get it we'll ask the | 597 // else we haven't got the icon url. When we get it we'll ask the |
| 584 // renderer to download the icon. | 598 // renderer to download the icon. |
| 585 | 599 |
| 586 if (has_valid_result && | 600 if (has_valid_result && (handler_type_ != FAVICON || download_largest_icon_)) |
| 587 (icon_types_ != favicon_base::FAVICON || download_largest_icon_)) | |
| 588 NotifyFaviconAvailable(favicon_bitmap_results, false); | 601 NotifyFaviconAvailable(favicon_bitmap_results, false); |
| 589 } | 602 } |
| 590 | 603 |
| 591 void FaviconHandler::DownloadFaviconOrAskFaviconService( | 604 void FaviconHandler::DownloadFaviconOrAskFaviconService( |
| 592 const GURL& page_url, | 605 const GURL& page_url, |
| 593 const GURL& icon_url, | 606 const GURL& icon_url, |
| 594 favicon_base::IconType icon_type) { | 607 favicon_base::IconType icon_type) { |
| 595 if (favicon_expired_or_incomplete_) { | 608 if (favicon_expired_or_incomplete_) { |
| 596 // We have the mapping, but the favicon is out of date. Download it now. | 609 // We have the mapping, but the favicon is out of date. Download it now. |
| 597 ScheduleDownload(page_url, icon_url, icon_type); | 610 ScheduleDownload(page_url, icon_url, icon_type); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 621 void FaviconHandler::OnFaviconData(const std::vector< | 634 void FaviconHandler::OnFaviconData(const std::vector< |
| 622 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) { | 635 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) { |
| 623 if (PageChangedSinceFaviconWasRequested()) | 636 if (PageChangedSinceFaviconWasRequested()) |
| 624 return; | 637 return; |
| 625 | 638 |
| 626 bool has_results = !favicon_bitmap_results.empty(); | 639 bool has_results = !favicon_bitmap_results.empty(); |
| 627 bool has_expired_or_incomplete_result = HasExpiredOrIncompleteResult( | 640 bool has_expired_or_incomplete_result = HasExpiredOrIncompleteResult( |
| 628 preferred_icon_size(), favicon_bitmap_results); | 641 preferred_icon_size(), favicon_bitmap_results); |
| 629 bool has_valid_result = HasValidResult(favicon_bitmap_results); | 642 bool has_valid_result = HasValidResult(favicon_bitmap_results); |
| 630 | 643 |
| 631 if (has_results && icon_types_ == favicon_base::FAVICON && | 644 if (has_results && handler_type_ == FAVICON && !download_largest_icon_) { |
| 632 !download_largest_icon_) { | |
| 633 if (has_valid_result) { | 645 if (has_valid_result) { |
| 634 // There is a favicon, set it now. If expired we'll download the current | 646 // There is a favicon, set it now. If expired we'll download the current |
| 635 // one again, but at least the user will get some icon instead of the | 647 // one again, but at least the user will get some icon instead of the |
| 636 // default and most likely the current one is fine anyway. | 648 // default and most likely the current one is fine anyway. |
| 637 NotifyFaviconAvailable(favicon_bitmap_results, true); | 649 NotifyFaviconAvailable(favicon_bitmap_results, true); |
| 638 } | 650 } |
| 639 if (has_expired_or_incomplete_result) { | 651 if (has_expired_or_incomplete_result) { |
| 640 // The favicon is out of date. Request the current one. | 652 // The favicon is out of date. Request the current one. |
| 641 ScheduleDownload(driver_->GetActiveURL(), | 653 ScheduleDownload(driver_->GetActiveURL(), |
| 642 driver_->GetActiveFaviconURL(), | 654 driver_->GetActiveFaviconURL(), |
| 643 favicon_base::FAVICON); | 655 favicon_base::FAVICON); |
| 644 } | 656 } |
| 645 } else if (current_candidate() && | 657 } else if (current_candidate() && |
| 646 (!has_results || has_expired_or_incomplete_result || | 658 (!has_results || has_expired_or_incomplete_result || |
| 647 !(DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)))) { | 659 !(DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)))) { |
| 648 // We don't know the favicon, it is out of date or its type is not same as | 660 // We don't know the favicon, it is out of date or its type is not same as |
| 649 // one got from page. Request the current one. | 661 // one got from page. Request the current one. |
| 650 ScheduleDownload(driver_->GetActiveURL(), | 662 ScheduleDownload(driver_->GetActiveURL(), |
| 651 current_candidate()->icon_url, | 663 current_candidate()->icon_url, |
| 652 current_candidate()->icon_type); | 664 current_candidate()->icon_type); |
| 653 } | 665 } |
| 654 history_results_ = favicon_bitmap_results; | 666 history_results_ = favicon_bitmap_results; |
| 655 | 667 |
| 656 if (has_valid_result && | 668 if (has_valid_result && |
| 657 (icon_types_ != favicon_base::FAVICON || download_largest_icon_)) { | 669 (handler_type_ != FAVICON || download_largest_icon_)) { |
| 658 NotifyFaviconAvailable(favicon_bitmap_results, false); | 670 NotifyFaviconAvailable(favicon_bitmap_results, false); |
| 659 } | 671 } |
| 660 } | 672 } |
| 661 | 673 |
| 662 int FaviconHandler::ScheduleDownload(const GURL& url, | 674 int FaviconHandler::ScheduleDownload(const GURL& url, |
| 663 const GURL& image_url, | 675 const GURL& image_url, |
| 664 favicon_base::IconType icon_type) { | 676 favicon_base::IconType icon_type) { |
| 665 // A max bitmap size is specified to avoid receiving huge bitmaps in | 677 // A max bitmap size is specified to avoid receiving huge bitmaps in |
| 666 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() | 678 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() |
| 667 // for more details about the max bitmap size. | 679 // for more details about the max bitmap size. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 684 continue; | 696 continue; |
| 685 | 697 |
| 686 gfx::Size largest = | 698 gfx::Size largest = |
| 687 image_url.icon_sizes[GetLargestSizeIndex(image_url.icon_sizes)]; | 699 image_url.icon_sizes[GetLargestSizeIndex(image_url.icon_sizes)]; |
| 688 image_url.icon_sizes.clear(); | 700 image_url.icon_sizes.clear(); |
| 689 image_url.icon_sizes.push_back(largest); | 701 image_url.icon_sizes.push_back(largest); |
| 690 } | 702 } |
| 691 std::stable_sort(image_urls_.begin(), image_urls_.end(), | 703 std::stable_sort(image_urls_.begin(), image_urls_.end(), |
| 692 CompareIconSize); | 704 CompareIconSize); |
| 693 } | 705 } |
| OLD | NEW |