Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(335)

Side by Side Diff: chrome/browser/favicon/favicon_handler.cc

Issue 684983003: Add Observer in FaviconTabHelper (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 270 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 return exact_match; 281 return exact_match;
282 } 282 }
283 283
284 void FaviconHandler::SetFavicon(const GURL& url, 284 void FaviconHandler::SetFavicon(const GURL& url,
285 const GURL& icon_url, 285 const GURL& icon_url,
286 const gfx::Image& image, 286 const gfx::Image& image,
287 favicon_base::IconType icon_type) { 287 favicon_base::IconType icon_type) {
288 if (client_->GetFaviconService() && ShouldSaveFavicon(url)) 288 if (client_->GetFaviconService() && ShouldSaveFavicon(url))
289 SetHistoryFavicons(url, icon_url, icon_type, image); 289 SetHistoryFavicons(url, icon_url, icon_type, image);
290 290
291 if (UrlMatches(url, url_) && icon_type == favicon_base::FAVICON) { 291 if (UrlMatches(url, url_) && !PageChangedSinceFaviconWasRequested()) {
292 if (!PageChangedSinceFaviconWasRequested()) 292 if (icon_type == favicon_base::FAVICON && !download_largest_icon_)
293 SetFaviconOnActivePage(icon_url, image); 293 SetFaviconOnActivePage(icon_url, image);
294 if (!image.IsEmpty())
295 NotifyFaviconUpdated(icon_url);
sky 2014/10/29 15:50:55 Sorry, this is wrong. We shouldn't need the invali
294 } 296 }
295 } 297 }
296 298
297 void FaviconHandler::SetFaviconOnActivePage(const std::vector< 299 void FaviconHandler::SetFaviconOnActivePage(const std::vector<
298 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) { 300 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) {
299 gfx::Image resized_image = favicon_base::SelectFaviconFramesFromPNGs( 301 gfx::Image resized_image = favicon_base::SelectFaviconFramesFromPNGs(
300 favicon_bitmap_results, 302 favicon_bitmap_results,
301 favicon_base::GetFaviconScales(), 303 favicon_base::GetFaviconScales(),
302 preferred_icon_size()); 304 preferred_icon_size());
303 // The history service sends back results for a single icon URL, so it does 305 // 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. 306 // not matter which result we get the |icon_url| from.
305 const GURL icon_url = favicon_bitmap_results.empty() ? 307 const GURL icon_url = favicon_bitmap_results.empty() ?
306 GURL() : favicon_bitmap_results[0].icon_url; 308 GURL() : favicon_bitmap_results[0].icon_url;
307 SetFaviconOnActivePage(icon_url, resized_image); 309 SetFaviconOnActivePage(icon_url, resized_image);
308 } 310 }
309 311
310 void FaviconHandler::SetFaviconOnActivePage(const GURL& icon_url, 312 void FaviconHandler::SetFaviconOnActivePage(const GURL& icon_url,
311 const gfx::Image& image) { 313 const gfx::Image& image) {
312 // No matter what happens, we need to mark the favicon as being set. 314 // No matter what happens, we need to mark the favicon as being set.
313 driver_->SetActiveFaviconValidity(true); 315 driver_->SetActiveFaviconValidity(true);
314
315 bool icon_url_changed = driver_->GetActiveFaviconURL() != icon_url;
316 driver_->SetActiveFaviconURL(icon_url); 316 driver_->SetActiveFaviconURL(icon_url);
317 317
318 if (image.IsEmpty()) 318 if (image.IsEmpty())
319 return; 319 return;
320 320
321 gfx::Image image_with_adjusted_colorspace = image; 321 gfx::Image image_with_adjusted_colorspace = image;
322 favicon_base::SetFaviconColorSpace(&image_with_adjusted_colorspace); 322 favicon_base::SetFaviconColorSpace(&image_with_adjusted_colorspace);
323 323
324 driver_->SetActiveFaviconImage(image_with_adjusted_colorspace); 324 driver_->SetActiveFaviconImage(image_with_adjusted_colorspace);
325 NotifyFaviconUpdated(icon_url_changed);
326 } 325 }
327 326
328 void FaviconHandler::OnUpdateFaviconURL( 327 void FaviconHandler::OnUpdateFaviconURL(
329 const std::vector<FaviconURL>& candidates) { 328 const std::vector<FaviconURL>& candidates) {
330 image_urls_.clear(); 329 image_urls_.clear();
331 best_favicon_candidate_ = FaviconCandidate(); 330 best_favicon_candidate_ = FaviconCandidate();
332 for (std::vector<FaviconURL>::const_iterator i = candidates.begin(); 331 for (std::vector<FaviconURL>::const_iterator i = candidates.begin();
333 i != candidates.end(); ++i) { 332 i != candidates.end(); ++i) {
334 if (!i->icon_url.is_empty() && (i->icon_type & icon_types_)) 333 if (!i->icon_url.is_empty() && (i->icon_type & icon_types_))
335 image_urls_.push_back(*i); 334 image_urls_.push_back(*i);
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
503 } 502 }
504 503
505 bool FaviconHandler::ShouldSaveFavicon(const GURL& url) { 504 bool FaviconHandler::ShouldSaveFavicon(const GURL& url) {
506 if (!driver_->IsOffTheRecord()) 505 if (!driver_->IsOffTheRecord())
507 return true; 506 return true;
508 507
509 // Otherwise store the favicon if the page is bookmarked. 508 // Otherwise store the favicon if the page is bookmarked.
510 return client_->IsBookmarked(url); 509 return client_->IsBookmarked(url);
511 } 510 }
512 511
513 void FaviconHandler::NotifyFaviconUpdated(bool icon_url_changed) { 512 void FaviconHandler::NotifyFaviconUpdated(const GURL& icon_url) {
513 bool icon_url_changed = false;
514 if (icon_types_ == favicon_base::FAVICON) {
515 icon_url_changed = driver_->GetActiveFaviconURL() != icon_url;
516 } else {
517 icon_url_changed = icon_url != notified_touch_icon_url_;
518 notified_touch_icon_url_ = icon_url;
519 }
514 driver_->NotifyFaviconUpdated(icon_url_changed); 520 driver_->NotifyFaviconUpdated(icon_url_changed);
515 } 521 }
516 522
517 int FaviconHandler::GetMaximalIconSize(favicon_base::IconType icon_type) { 523 int FaviconHandler::GetMaximalIconSize(favicon_base::IconType icon_type) {
518 switch (icon_type) { 524 switch (icon_type) {
519 case favicon_base::FAVICON: 525 case favicon_base::FAVICON:
520 #if defined(OS_ANDROID) 526 #if defined(OS_ANDROID)
521 return 192; 527 return 192;
522 #else 528 #else
523 return gfx::ImageSkia::GetMaxSupportedScale() * gfx::kFaviconSize; 529 return gfx::ImageSkia::GetMaxSupportedScale() * gfx::kFaviconSize;
(...skipping 20 matching lines...) Expand all
544 preferred_icon_size(), favicon_bitmap_results); 550 preferred_icon_size(), favicon_bitmap_results);
545 if (has_results && icon_types_ == favicon_base::FAVICON && 551 if (has_results && icon_types_ == favicon_base::FAVICON &&
546 !driver_->GetActiveFaviconValidity() && 552 !driver_->GetActiveFaviconValidity() &&
547 (!current_candidate() || 553 (!current_candidate() ||
548 DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) { 554 DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) {
549 if (HasValidResult(favicon_bitmap_results)) { 555 if (HasValidResult(favicon_bitmap_results)) {
550 // The db knows the favicon (although it may be out of date) and the entry 556 // 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 557 // 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 558 // 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. 559 // user doesn't see a flash of the default favicon.
554 SetFaviconOnActivePage(favicon_bitmap_results); 560 if (!download_largest_icon_)
561 SetFaviconOnActivePage(favicon_bitmap_results);
562 NotifyFaviconUpdated(favicon_bitmap_results[0].icon_url);
555 } else { 563 } else {
556 // If |favicon_bitmap_results| does not have any valid results, treat the 564 // If |favicon_bitmap_results| does not have any valid results, treat the
557 // favicon as if it's expired. 565 // favicon as if it's expired.
558 // TODO(pkotwicz): Do something better. 566 // TODO(pkotwicz): Do something better.
559 favicon_expired_or_incomplete_ = true; 567 favicon_expired_or_incomplete_ = true;
560 } 568 }
561 } 569 }
562 if (has_results && !favicon_expired_or_incomplete_) { 570 if (has_results && !favicon_expired_or_incomplete_) {
563 if (current_candidate() && 571 if (current_candidate() &&
564 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)) { 572 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)) {
565 // Mapping in the database is wrong. DownloadFavIconOrAskHistory will 573 // Mapping in the database is wrong. DownloadFavIconOrAskHistory will
566 // update the mapping for this url and download the favicon if we don't 574 // update the mapping for this url and download the favicon if we don't
567 // already have it. 575 // already have it.
568 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(), 576 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(),
569 current_candidate()->icon_url, 577 current_candidate()->icon_url,
570 current_candidate()->icon_type); 578 current_candidate()->icon_type);
571 } 579 }
572 } else if (current_candidate()) { 580 } else if (current_candidate()) {
573 // We know the official url for the favicon, but either don't have the 581 // 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 582 // favicon or it's expired. Continue on to DownloadFaviconOrAskHistory to
575 // either download or check history again. 583 // either download or check history again.
576 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(), 584 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(),
577 current_candidate()->icon_url, 585 current_candidate()->icon_url,
578 current_candidate()->icon_type); 586 current_candidate()->icon_type);
579 } 587 }
580 // else we haven't got the icon url. When we get it we'll ask the 588 // else we haven't got the icon url. When we get it we'll ask the
581 // renderer to download the icon. 589 // renderer to download the icon.
590
591 // Notify the TOUCH_ICON or TOUCH_PRECOMPOSED_ICON available in
592 // HistoryBackend.
593 if (icon_types_ != favicon_base::FAVICON &&
594 HasValidResult(favicon_bitmap_results)) {
595 NotifyFaviconUpdated(favicon_bitmap_results[0].icon_url);
596 }
michaelbai 2014/10/29 04:30:32 I just simply added code here because this is touc
582 } 597 }
583 598
584 void FaviconHandler::DownloadFaviconOrAskFaviconService( 599 void FaviconHandler::DownloadFaviconOrAskFaviconService(
585 const GURL& page_url, 600 const GURL& page_url,
586 const GURL& icon_url, 601 const GURL& icon_url,
587 favicon_base::IconType icon_type) { 602 favicon_base::IconType icon_type) {
588 if (favicon_expired_or_incomplete_) { 603 if (favicon_expired_or_incomplete_) {
589 // We have the mapping, but the favicon is out of date. Download it now. 604 // We have the mapping, but the favicon is out of date. Download it now.
590 ScheduleDownload(page_url, icon_url, icon_type); 605 ScheduleDownload(page_url, icon_url, icon_type);
591 } else if (client_->GetFaviconService()) { 606 } else if (client_->GetFaviconService()) {
(...skipping 26 matching lines...) Expand all
618 633
619 bool has_results = !favicon_bitmap_results.empty(); 634 bool has_results = !favicon_bitmap_results.empty();
620 bool has_expired_or_incomplete_result = HasExpiredOrIncompleteResult( 635 bool has_expired_or_incomplete_result = HasExpiredOrIncompleteResult(
621 preferred_icon_size(), favicon_bitmap_results); 636 preferred_icon_size(), favicon_bitmap_results);
622 637
623 if (has_results && icon_types_ == favicon_base::FAVICON) { 638 if (has_results && icon_types_ == favicon_base::FAVICON) {
624 if (HasValidResult(favicon_bitmap_results)) { 639 if (HasValidResult(favicon_bitmap_results)) {
625 // There is a favicon, set it now. If expired we'll download the current 640 // 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 641 // 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. 642 // default and most likely the current one is fine anyway.
628 SetFaviconOnActivePage(favicon_bitmap_results); 643 if (!download_largest_icon_)
644 SetFaviconOnActivePage(favicon_bitmap_results);
645 NotifyFaviconUpdated(favicon_bitmap_results[0].icon_url);
629 } 646 }
630 if (has_expired_or_incomplete_result) { 647 if (has_expired_or_incomplete_result) {
631 // The favicon is out of date. Request the current one. 648 // The favicon is out of date. Request the current one.
632 ScheduleDownload(driver_->GetActiveURL(), 649 ScheduleDownload(driver_->GetActiveURL(),
633 driver_->GetActiveFaviconURL(), 650 driver_->GetActiveFaviconURL(),
634 favicon_base::FAVICON); 651 favicon_base::FAVICON);
635 } 652 }
636 } else if (current_candidate() && 653 } else if (current_candidate() &&
637 (!has_results || has_expired_or_incomplete_result || 654 (!has_results || has_expired_or_incomplete_result ||
638 !(DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)))) { 655 !(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 656 // 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. 657 // one got from page. Request the current one.
641 ScheduleDownload(driver_->GetActiveURL(), 658 ScheduleDownload(driver_->GetActiveURL(),
642 current_candidate()->icon_url, 659 current_candidate()->icon_url,
643 current_candidate()->icon_type); 660 current_candidate()->icon_type);
644 } 661 }
662 // Notify the TOUCH_ICON or TOUCH_PRECOMPOSED_ICON available in
663 // HistoryBackend.
664 if (icon_types_ != favicon_base::FAVICON &&
665 HasValidResult(favicon_bitmap_results)) {
666 NotifyFaviconUpdated(favicon_bitmap_results[0].icon_url);
667 }
645 history_results_ = favicon_bitmap_results; 668 history_results_ = favicon_bitmap_results;
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,
(...skipping 14 matching lines...) Expand all
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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698