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 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 : url(url), | 191 : url(url), |
192 image_url(image_url), | 192 image_url(image_url), |
193 image(image), | 193 image(image), |
194 score(score), | 194 score(score), |
195 icon_type(icon_type) {} | 195 icon_type(icon_type) {} |
196 | 196 |
197 //////////////////////////////////////////////////////////////////////////////// | 197 //////////////////////////////////////////////////////////////////////////////// |
198 | 198 |
199 FaviconHandler::FaviconHandler(FaviconClient* client, | 199 FaviconHandler::FaviconHandler(FaviconClient* client, |
200 FaviconDriver* driver, | 200 FaviconDriver* driver, |
201 Type icon_type, | 201 Type handler_type, |
202 bool download_largest_icon) | 202 bool download_largest_icon) |
203 : got_favicon_from_history_(false), | 203 : got_favicon_from_history_(false), |
204 favicon_expired_or_incomplete_(false), | 204 favicon_expired_or_incomplete_(false), |
205 icon_types_(icon_type == FAVICON | 205 handler_type_(handler_type), |
206 ? favicon_base::FAVICON | 206 icon_types_(FaviconHandler::GetIconTypesFromHandlerType(handler_type)), |
207 : favicon_base::TOUCH_ICON | | |
208 favicon_base::TOUCH_PRECOMPOSED_ICON), | |
209 download_largest_icon_(download_largest_icon), | 207 download_largest_icon_(download_largest_icon), |
210 client_(client), | 208 client_(client), |
211 driver_(driver) { | 209 driver_(driver) { |
212 DCHECK(driver_); | 210 DCHECK(driver_); |
213 } | 211 } |
214 | 212 |
215 FaviconHandler::~FaviconHandler() { | 213 FaviconHandler::~FaviconHandler() { |
216 } | 214 } |
217 | 215 |
| 216 // static |
| 217 int FaviconHandler::GetIconTypesFromHandlerType( |
| 218 FaviconHandler::Type handler_type) { |
| 219 if (handler_type == FAVICON) |
| 220 return favicon_base::FAVICON; |
| 221 return favicon_base::TOUCH_ICON | favicon_base::TOUCH_PRECOMPOSED_ICON; |
| 222 } |
| 223 |
218 void FaviconHandler::FetchFavicon(const GURL& url) { | 224 void FaviconHandler::FetchFavicon(const GURL& url) { |
219 cancelable_task_tracker_.TryCancelAll(); | 225 cancelable_task_tracker_.TryCancelAll(); |
220 | 226 |
221 url_ = url; | 227 url_ = url; |
222 | 228 |
223 favicon_expired_or_incomplete_ = got_favicon_from_history_ = false; | 229 favicon_expired_or_incomplete_ = got_favicon_from_history_ = false; |
224 image_urls_.clear(); | 230 image_urls_.clear(); |
225 | 231 |
226 // Request the favicon from the history service. In parallel to this the | 232 // 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 | 233 // renderer is going to notify us (well WebContents) when the favicon url is |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
489 icon_types, | 495 icon_types, |
490 preferred_icon_size(), | 496 preferred_icon_size(), |
491 callback, | 497 callback, |
492 tracker); | 498 tracker); |
493 } | 499 } |
494 | 500 |
495 void FaviconHandler::SetHistoryFavicons(const GURL& page_url, | 501 void FaviconHandler::SetHistoryFavicons(const GURL& page_url, |
496 const GURL& icon_url, | 502 const GURL& icon_url, |
497 favicon_base::IconType icon_type, | 503 favicon_base::IconType icon_type, |
498 const gfx::Image& image) { | 504 const gfx::Image& image) { |
| 505 // TODO(huangs): Get the following to garbage collect if handler_type_ == ALL. |
499 client_->GetFaviconService()->SetFavicons( | 506 client_->GetFaviconService()->SetFavicons( |
500 page_url, icon_url, icon_type, image); | 507 page_url, icon_url, icon_type, image); |
501 } | 508 } |
502 | 509 |
503 bool FaviconHandler::ShouldSaveFavicon(const GURL& url) { | 510 bool FaviconHandler::ShouldSaveFavicon(const GURL& url) { |
504 if (!driver_->IsOffTheRecord()) | 511 if (!driver_->IsOffTheRecord()) |
505 return true; | 512 return true; |
506 | 513 |
507 // Otherwise store the favicon if the page is bookmarked. | 514 // Otherwise store the favicon if the page is bookmarked. |
508 return client_->IsBookmarked(url); | 515 return client_->IsBookmarked(url); |
(...skipping 22 matching lines...) Expand all Loading... |
531 favicon_bitmap_results) { | 538 favicon_bitmap_results) { |
532 if (PageChangedSinceFaviconWasRequested()) | 539 if (PageChangedSinceFaviconWasRequested()) |
533 return; | 540 return; |
534 got_favicon_from_history_ = true; | 541 got_favicon_from_history_ = true; |
535 history_results_ = favicon_bitmap_results; | 542 history_results_ = favicon_bitmap_results; |
536 bool has_results = !favicon_bitmap_results.empty(); | 543 bool has_results = !favicon_bitmap_results.empty(); |
537 favicon_expired_or_incomplete_ = has_results && HasExpiredOrIncompleteResult( | 544 favicon_expired_or_incomplete_ = has_results && HasExpiredOrIncompleteResult( |
538 preferred_icon_size(), favicon_bitmap_results); | 545 preferred_icon_size(), favicon_bitmap_results); |
539 bool has_valid_result = HasValidResult(favicon_bitmap_results); | 546 bool has_valid_result = HasValidResult(favicon_bitmap_results); |
540 | 547 |
541 if (has_results && icon_types_ == favicon_base::FAVICON && | 548 if (has_results && handler_type_ == FAVICON && |
542 !download_largest_icon_ && !driver_->GetActiveFaviconValidity() && | 549 !download_largest_icon_ && !driver_->GetActiveFaviconValidity() && |
543 (!current_candidate() || | 550 (!current_candidate() || |
544 DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) { | 551 DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) { |
545 if (has_valid_result) { | 552 if (has_valid_result) { |
546 // The db knows the favicon (although it may be out of date) and the entry | 553 // The db knows the favicon (although it may be out of date) and the entry |
547 // doesn't have an icon. Set the favicon now, and if the favicon turns out | 554 // doesn't have an icon. Set the favicon now, and if the favicon turns out |
548 // to be expired (or the wrong url) we'll fetch later on. This way the | 555 // to be expired (or the wrong url) we'll fetch later on. This way the |
549 // user doesn't see a flash of the default favicon. | 556 // user doesn't see a flash of the default favicon. |
550 NotifyFaviconAvailable(favicon_bitmap_results, true); | 557 NotifyFaviconAvailable(favicon_bitmap_results, true); |
551 } else { | 558 } else { |
(...skipping 17 matching lines...) Expand all Loading... |
569 // We know the official url for the favicon, but either don't have the | 576 // We know the official url for the favicon, but either don't have the |
570 // favicon or it's expired. Continue on to DownloadFaviconOrAskHistory to | 577 // favicon or it's expired. Continue on to DownloadFaviconOrAskHistory to |
571 // either download or check history again. | 578 // either download or check history again. |
572 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(), | 579 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(), |
573 current_candidate()->icon_url, | 580 current_candidate()->icon_url, |
574 current_candidate()->icon_type); | 581 current_candidate()->icon_type); |
575 } | 582 } |
576 // else we haven't got the icon url. When we get it we'll ask the | 583 // else we haven't got the icon url. When we get it we'll ask the |
577 // renderer to download the icon. | 584 // renderer to download the icon. |
578 | 585 |
579 if (has_valid_result && | 586 if (has_valid_result && (handler_type_ != FAVICON || download_largest_icon_)) |
580 (icon_types_ != favicon_base::FAVICON || download_largest_icon_)) | |
581 NotifyFaviconAvailable(favicon_bitmap_results, false); | 587 NotifyFaviconAvailable(favicon_bitmap_results, false); |
582 } | 588 } |
583 | 589 |
584 void FaviconHandler::DownloadFaviconOrAskFaviconService( | 590 void FaviconHandler::DownloadFaviconOrAskFaviconService( |
585 const GURL& page_url, | 591 const GURL& page_url, |
586 const GURL& icon_url, | 592 const GURL& icon_url, |
587 favicon_base::IconType icon_type) { | 593 favicon_base::IconType icon_type) { |
588 if (favicon_expired_or_incomplete_) { | 594 if (favicon_expired_or_incomplete_) { |
589 // We have the mapping, but the favicon is out of date. Download it now. | 595 // We have the mapping, but the favicon is out of date. Download it now. |
590 ScheduleDownload(page_url, icon_url, icon_type); | 596 ScheduleDownload(page_url, icon_url, icon_type); |
(...skipping 23 matching lines...) Expand all Loading... |
614 void FaviconHandler::OnFaviconData(const std::vector< | 620 void FaviconHandler::OnFaviconData(const std::vector< |
615 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) { | 621 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) { |
616 if (PageChangedSinceFaviconWasRequested()) | 622 if (PageChangedSinceFaviconWasRequested()) |
617 return; | 623 return; |
618 | 624 |
619 bool has_results = !favicon_bitmap_results.empty(); | 625 bool has_results = !favicon_bitmap_results.empty(); |
620 bool has_expired_or_incomplete_result = HasExpiredOrIncompleteResult( | 626 bool has_expired_or_incomplete_result = HasExpiredOrIncompleteResult( |
621 preferred_icon_size(), favicon_bitmap_results); | 627 preferred_icon_size(), favicon_bitmap_results); |
622 bool has_valid_result = HasValidResult(favicon_bitmap_results); | 628 bool has_valid_result = HasValidResult(favicon_bitmap_results); |
623 | 629 |
624 if (has_results && icon_types_ == favicon_base::FAVICON && | 630 if (has_results && handler_type_ == FAVICON && !download_largest_icon_) { |
625 !download_largest_icon_) { | |
626 if (has_valid_result) { | 631 if (has_valid_result) { |
627 // There is a favicon, set it now. If expired we'll download the current | 632 // There is a favicon, set it now. If expired we'll download the current |
628 // one again, but at least the user will get some icon instead of the | 633 // one again, but at least the user will get some icon instead of the |
629 // default and most likely the current one is fine anyway. | 634 // default and most likely the current one is fine anyway. |
630 NotifyFaviconAvailable(favicon_bitmap_results, true); | 635 NotifyFaviconAvailable(favicon_bitmap_results, true); |
631 } | 636 } |
632 if (has_expired_or_incomplete_result) { | 637 if (has_expired_or_incomplete_result) { |
633 // The favicon is out of date. Request the current one. | 638 // The favicon is out of date. Request the current one. |
634 ScheduleDownload(driver_->GetActiveURL(), | 639 ScheduleDownload(driver_->GetActiveURL(), |
635 driver_->GetActiveFaviconURL(), | 640 driver_->GetActiveFaviconURL(), |
636 favicon_base::FAVICON); | 641 favicon_base::FAVICON); |
637 } | 642 } |
638 } else if (current_candidate() && | 643 } else if (current_candidate() && |
639 (!has_results || has_expired_or_incomplete_result || | 644 (!has_results || has_expired_or_incomplete_result || |
640 !(DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)))) { | 645 !(DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)))) { |
641 // We don't know the favicon, it is out of date or its type is not same as | 646 // We don't know the favicon, it is out of date or its type is not same as |
642 // one got from page. Request the current one. | 647 // one got from page. Request the current one. |
643 ScheduleDownload(driver_->GetActiveURL(), | 648 ScheduleDownload(driver_->GetActiveURL(), |
644 current_candidate()->icon_url, | 649 current_candidate()->icon_url, |
645 current_candidate()->icon_type); | 650 current_candidate()->icon_type); |
646 } | 651 } |
647 history_results_ = favicon_bitmap_results; | 652 history_results_ = favicon_bitmap_results; |
648 | 653 |
649 if (has_valid_result && | 654 if (has_valid_result && |
650 (icon_types_ != favicon_base::FAVICON || download_largest_icon_)) { | 655 (handler_type_ != FAVICON || download_largest_icon_)) { |
651 NotifyFaviconAvailable(favicon_bitmap_results, false); | 656 NotifyFaviconAvailable(favicon_bitmap_results, false); |
652 } | 657 } |
653 } | 658 } |
654 | 659 |
655 int FaviconHandler::ScheduleDownload(const GURL& url, | 660 int FaviconHandler::ScheduleDownload(const GURL& url, |
656 const GURL& image_url, | 661 const GURL& image_url, |
657 favicon_base::IconType icon_type) { | 662 favicon_base::IconType icon_type) { |
658 // A max bitmap size is specified to avoid receiving huge bitmaps in | 663 // A max bitmap size is specified to avoid receiving huge bitmaps in |
659 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() | 664 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() |
660 // for more details about the max bitmap size. | 665 // for more details about the max bitmap size. |
(...skipping 15 matching lines...) Expand all Loading... |
676 if (i->icon_sizes.empty()) | 681 if (i->icon_sizes.empty()) |
677 continue; | 682 continue; |
678 | 683 |
679 gfx::Size largest = i->icon_sizes[GetLargestSizeIndex(i->icon_sizes)]; | 684 gfx::Size largest = i->icon_sizes[GetLargestSizeIndex(i->icon_sizes)]; |
680 i->icon_sizes.clear(); | 685 i->icon_sizes.clear(); |
681 i->icon_sizes.push_back(largest); | 686 i->icon_sizes.push_back(largest); |
682 } | 687 } |
683 std::stable_sort(image_urls_.begin(), image_urls_.end(), | 688 std::stable_sort(image_urls_.begin(), image_urls_.end(), |
684 CompareIconSize); | 689 CompareIconSize); |
685 } | 690 } |
OLD | NEW |