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

Side by Side Diff: components/favicon/core/favicon_handler.cc

Issue 2738663002: FaviconHandler test rewrite
Patch Set: Merge branch 'mastiz_cl' into favicon_handler_unittest2 Created 3 years, 9 months 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 "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 377 matching lines...) Expand 10 before | Expand all | Expand 10 after
388 image_urls_ = pruned_candidates; 388 image_urls_ = pruned_candidates;
389 current_candidate_index_ = 0u; 389 current_candidate_index_ = 0u;
390 best_favicon_candidate_ = FaviconCandidate(); 390 best_favicon_candidate_ = FaviconCandidate();
391 391
392 // TODO(davemoore) Should clear on empty url. Currently we ignore it. 392 // TODO(davemoore) Should clear on empty url. Currently we ignore it.
393 // This appears to be what FF does as well. 393 // This appears to be what FF does as well.
394 if (current_candidate() && got_favicon_from_history_) 394 if (current_candidate() && got_favicon_from_history_)
395 OnGotInitialHistoryDataAndIconURLCandidates(); 395 OnGotInitialHistoryDataAndIconURLCandidates();
396 } 396 }
397 397
398 // static
399 int FaviconHandler::GetMaximalIconSize(favicon_base::IconType icon_type) {
400 switch (icon_type) {
401 case favicon_base::FAVICON:
402 #if defined(OS_ANDROID)
403 return 192;
404 #else
405 return gfx::ImageSkia::GetMaxSupportedScale() * gfx::kFaviconSize;
406 #endif
407 case favicon_base::TOUCH_ICON:
408 case favicon_base::TOUCH_PRECOMPOSED_ICON:
409 return kTouchIconSize;
410 case favicon_base::INVALID_ICON:
411 return 0;
412 }
413 NOTREACHED();
414 return 0;
415 }
416
398 void FaviconHandler::OnGotInitialHistoryDataAndIconURLCandidates() { 417 void FaviconHandler::OnGotInitialHistoryDataAndIconURLCandidates() {
399 if (!initial_history_result_expired_or_incomplete_ && 418 if (!initial_history_result_expired_or_incomplete_ &&
400 DoUrlAndIconMatch(*current_candidate(), notification_icon_url_, 419 DoUrlAndIconMatch(*current_candidate(), notification_icon_url_,
401 notification_icon_type_)) { 420 notification_icon_type_)) {
402 // - The data from history is valid and not expired. 421 // - The data from history is valid and not expired.
403 // - The icon URL of the history data matches one of the page's icon URLs. 422 // - The icon URL of the history data matches one of the page's icon URLs.
404 // - The icon URL of the history data matches the icon URL of the last 423 // - The icon URL of the history data matches the icon URL of the last
405 // OnFaviconAvailable() notification. 424 // OnFaviconAvailable() notification.
406 // We are done. No additional downloads or history requests are needed. 425 // We are done. No additional downloads or history requests are needed.
407 // TODO: Store all of the icon URLs associated with a page in history so 426 // TODO: Store all of the icon URLs associated with a page in history so
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
548 } 567 }
549 568
550 bool FaviconHandler::ShouldSaveFavicon() { 569 bool FaviconHandler::ShouldSaveFavicon() {
551 if (!delegate_->IsOffTheRecord()) 570 if (!delegate_->IsOffTheRecord())
552 return true; 571 return true;
553 572
554 // Always save favicon if the page is bookmarked. 573 // Always save favicon if the page is bookmarked.
555 return delegate_->IsBookmarked(url_); 574 return delegate_->IsBookmarked(url_);
556 } 575 }
557 576
558 int FaviconHandler::GetMaximalIconSize(favicon_base::IconType icon_type) {
559 switch (icon_type) {
560 case favicon_base::FAVICON:
561 #if defined(OS_ANDROID)
562 return 192;
563 #else
564 return gfx::ImageSkia::GetMaxSupportedScale() * gfx::kFaviconSize;
565 #endif
566 case favicon_base::TOUCH_ICON:
567 case favicon_base::TOUCH_PRECOMPOSED_ICON:
568 return kTouchIconSize;
569 case favicon_base::INVALID_ICON:
570 return 0;
571 }
572 NOTREACHED();
573 return 0;
574 }
575
576 void FaviconHandler::OnFaviconDataForInitialURLFromFaviconService( 577 void FaviconHandler::OnFaviconDataForInitialURLFromFaviconService(
577 const std::vector<favicon_base::FaviconRawBitmapResult>& 578 const std::vector<favicon_base::FaviconRawBitmapResult>&
578 favicon_bitmap_results) { 579 favicon_bitmap_results) {
579 got_favicon_from_history_ = true; 580 got_favicon_from_history_ = true;
580 bool has_valid_result = HasValidResult(favicon_bitmap_results); 581 bool has_valid_result = HasValidResult(favicon_bitmap_results);
581 initial_history_result_expired_or_incomplete_ = 582 initial_history_result_expired_or_incomplete_ =
582 !has_valid_result || 583 !has_valid_result ||
583 HasExpiredOrIncompleteResult(preferred_icon_size(), 584 HasExpiredOrIncompleteResult(preferred_icon_size(),
584 favicon_bitmap_results); 585 favicon_bitmap_results);
585 redownload_icons_ = initial_history_result_expired_or_incomplete_ && 586 redownload_icons_ = initial_history_result_expired_or_incomplete_ &&
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
654 655
655 if (has_expired_or_incomplete_result) { 656 if (has_expired_or_incomplete_result) {
656 ScheduleDownload(current_candidate()->icon_url, 657 ScheduleDownload(current_candidate()->icon_url,
657 current_candidate()->icon_type); 658 current_candidate()->icon_type);
658 } 659 }
659 } 660 }
660 661
661 void FaviconHandler::ScheduleDownload(const GURL& image_url, 662 void FaviconHandler::ScheduleDownload(const GURL& image_url,
662 favicon_base::IconType icon_type) { 663 favicon_base::IconType icon_type) {
663 DCHECK(image_url.is_valid()); 664 DCHECK(image_url.is_valid());
664 if (service_ && service_->WasUnableToDownloadFavicon(image_url)) { 665 int download_id = 0;
665 DVLOG(1) << "Skip Failed FavIcon: " << image_url; 666 if (service_ && !service_->WasUnableToDownloadFavicon(image_url)) {
666 OnDidDownloadFavicon(0, 0, image_url, std::vector<SkBitmap>(), 667 // A max bitmap size is specified to avoid receiving huge bitmaps in
667 std::vector<gfx::Size>()); 668 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload()
668 return; 669 // for more details about the max bitmap size.
670 download_id = delegate_->DownloadImage(
671 image_url, GetMaximalIconSize(icon_type),
672 base::Bind(&FaviconHandler::OnDidDownloadFavicon,
673 weak_ptr_factory_.GetWeakPtr()));
669 } 674 }
670 // A max bitmap size is specified to avoid receiving huge bitmaps in
671 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload()
672 // for more details about the max bitmap size.
673 const int download_id =
674 delegate_->DownloadImage(image_url, GetMaximalIconSize(icon_type),
675 base::Bind(&FaviconHandler::OnDidDownloadFavicon,
676 weak_ptr_factory_.GetWeakPtr()));
677 675
678 // Download ids should be unique. 676 // Download ids should be unique.
679 DCHECK(download_requests_.find(download_id) == download_requests_.end()); 677 DCHECK(download_requests_.find(download_id) == download_requests_.end());
680 download_requests_[download_id] = DownloadRequest(image_url, icon_type); 678 download_requests_[download_id] = DownloadRequest(image_url, icon_type);
681 679
682 // TODO(mastiz): Remove the download_id == 0 handling because it's not used
683 // in production code, only tests.
684 if (download_id == 0) { 680 if (download_id == 0) {
685 // If DownloadFavicon() did not start a download, it returns a download id 681 DVLOG(1) << "Skip Failed FavIcon: " << image_url;
686 // of 0. We still need to call OnDidDownloadFavicon() because the method is 682 OnDidDownloadFavicon(0, 404, image_url, std::vector<SkBitmap>(),
687 // responsible for initiating the data request for the next candidate.
688 OnDidDownloadFavicon(download_id, 0, image_url, std::vector<SkBitmap>(),
689 std::vector<gfx::Size>()); 683 std::vector<gfx::Size>());
690 } 684 }
691 } 685 }
692 686
693 } // namespace favicon 687 } // namespace favicon
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698