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

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

Issue 285293005: Replace content's faviconURL with components/favicon's (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased. Created 6 years, 7 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 "chrome/browser/favicon/favicon_handler.h" 5 #include "chrome/browser/favicon/favicon_handler.h"
6 6
7 #include "build/build_config.h" 7 #include "build/build_config.h"
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/bind_helpers.h" 13 #include "base/bind_helpers.h"
14 #include "base/memory/ref_counted_memory.h" 14 #include "base/memory/ref_counted_memory.h"
15 #include "chrome/browser/favicon/favicon_service_factory.h" 15 #include "chrome/browser/favicon/favicon_service_factory.h"
16 #include "chrome/browser/favicon/favicon_util.h" 16 #include "chrome/browser/favicon/favicon_util.h"
17 #include "components/favicon_base/select_favicon_frames.h" 17 #include "components/favicon_base/select_favicon_frames.h"
18 #include "skia/ext/image_operations.h" 18 #include "skia/ext/image_operations.h"
19 #include "ui/gfx/codec/png_codec.h" 19 #include "ui/gfx/codec/png_codec.h"
20 #include "ui/gfx/image/image.h" 20 #include "ui/gfx/image/image.h"
21 #include "ui/gfx/image/image_skia.h" 21 #include "ui/gfx/image/image_skia.h"
22 #include "ui/gfx/image/image_util.h" 22 #include "ui/gfx/image/image_util.h"
23 23
24 using content::FaviconURL; 24 using favicon::FaviconURL;
25 25
26 namespace { 26 namespace {
27 27
28 // Size (along each axis) of a touch icon. This currently corresponds to 28 // Size (along each axis) of a touch icon. This currently corresponds to
29 // the apple touch icon for iPad. 29 // the apple touch icon for iPad.
30 const int kTouchIconSize = 144; 30 const int kTouchIconSize = 144;
31 31
32 // Returns favicon_base::IconType the given icon_type corresponds to.
33 favicon_base::IconType ToChromeIconType(FaviconURL::IconType icon_type) {
34 switch (icon_type) {
35 case FaviconURL::FAVICON:
36 return favicon_base::FAVICON;
37 case FaviconURL::TOUCH_ICON:
38 return favicon_base::TOUCH_ICON;
39 case FaviconURL::TOUCH_PRECOMPOSED_ICON:
40 return favicon_base::TOUCH_PRECOMPOSED_ICON;
41 case FaviconURL::INVALID_ICON:
42 return favicon_base::INVALID_ICON;
43 }
44 NOTREACHED();
45 return favicon_base::INVALID_ICON;
46 }
47
48 // Get the maximal icon size in pixels for a icon of type |icon_type| for the 32 // Get the maximal icon size in pixels for a icon of type |icon_type| for the
49 // current platform. 33 // current platform.
50 int GetMaximalIconSize(favicon_base::IconType icon_type) { 34 int GetMaximalIconSize(favicon_base::IconType icon_type) {
51 switch (icon_type) { 35 switch (icon_type) {
52 case favicon_base::FAVICON: 36 case favicon_base::FAVICON:
53 #if defined(OS_ANDROID) 37 #if defined(OS_ANDROID)
54 return 192; 38 return 192;
55 #else 39 #else
56 return gfx::ImageSkia::GetMaxSupportedScale() * gfx::kFaviconSize; 40 return gfx::ImageSkia::GetMaxSupportedScale() * gfx::kFaviconSize;
57 #endif 41 #endif
58 case favicon_base::TOUCH_ICON: 42 case favicon_base::TOUCH_ICON:
59 case favicon_base::TOUCH_PRECOMPOSED_ICON: 43 case favicon_base::TOUCH_PRECOMPOSED_ICON:
60 return kTouchIconSize; 44 return kTouchIconSize;
61 case favicon_base::INVALID_ICON: 45 case favicon_base::INVALID_ICON:
62 return 0; 46 return 0;
63 } 47 }
64 NOTREACHED(); 48 NOTREACHED();
65 return 0; 49 return 0;
66 } 50 }
67 51
68 bool DoUrlAndIconMatch(const FaviconURL& favicon_url, 52 bool DoUrlAndIconMatch(const FaviconURL& favicon_url,
69 const GURL& url, 53 const GURL& url,
70 favicon_base::IconType icon_type) { 54 favicon_base::IconType icon_type) {
71 return favicon_url.icon_url == url && 55 return favicon_url.icon_url == url && favicon_url.icon_type == icon_type;
72 ToChromeIconType(favicon_url.icon_type) == icon_type;
73 } 56 }
74 57
75 // Returns true if all of the icon URLs and icon types in |bitmap_results| are 58 // Returns true if all of the icon URLs and icon types in |bitmap_results| are
76 // identical and if they match the icon URL and icon type in |favicon_url|. 59 // identical and if they match the icon URL and icon type in |favicon_url|.
77 // Returns false if |bitmap_results| is empty. 60 // Returns false if |bitmap_results| is empty.
78 bool DoUrlsAndIconsMatch( 61 bool DoUrlsAndIconsMatch(
79 const FaviconURL& favicon_url, 62 const FaviconURL& favicon_url,
80 const std::vector<favicon_base::FaviconBitmapResult>& bitmap_results) { 63 const std::vector<favicon_base::FaviconBitmapResult>& bitmap_results) {
81 if (bitmap_results.empty()) 64 if (bitmap_results.empty())
82 return false; 65 return false;
83 66
84 const favicon_base::IconType icon_type = 67 const favicon_base::IconType icon_type = favicon_url.icon_type;
85 ToChromeIconType(favicon_url.icon_type);
86 68
87 for (size_t i = 0; i < bitmap_results.size(); ++i) { 69 for (size_t i = 0; i < bitmap_results.size(); ++i) {
88 if (favicon_url.icon_url != bitmap_results[i].icon_url || 70 if (favicon_url.icon_url != bitmap_results[i].icon_url ||
89 icon_type != bitmap_results[i].icon_type) { 71 icon_type != bitmap_results[i].icon_type) {
90 return false; 72 return false;
91 } 73 }
92 } 74 }
93 return true; 75 return true;
94 } 76 }
95 77
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 } 368 }
387 369
388 void FaviconHandler::ProcessCurrentUrl() { 370 void FaviconHandler::ProcessCurrentUrl() {
389 DCHECK(!image_urls_.empty()); 371 DCHECK(!image_urls_.empty());
390 372
391 // current_candidate() may return NULL if download_largest_icon_ is true and 373 // current_candidate() may return NULL if download_largest_icon_ is true and
392 // all the sizes are larger than the max. 374 // all the sizes are larger than the max.
393 if (PageChangedSinceFaviconWasRequested() || !current_candidate()) 375 if (PageChangedSinceFaviconWasRequested() || !current_candidate())
394 return; 376 return;
395 377
396 if (current_candidate()->icon_type == FaviconURL::FAVICON) { 378 if (current_candidate()->icon_type == favicon_base::FAVICON) {
397 if (!favicon_expired_or_incomplete_ && 379 if (!favicon_expired_or_incomplete_ &&
398 driver_->GetActiveFaviconValidity() && 380 driver_->GetActiveFaviconValidity() &&
399 DoUrlAndIconMatch(*current_candidate(), 381 DoUrlAndIconMatch(*current_candidate(),
400 driver_->GetActiveFaviconURL(), 382 driver_->GetActiveFaviconURL(),
401 favicon_base::FAVICON)) 383 favicon_base::FAVICON))
402 return; 384 return;
403 } else if (!favicon_expired_or_incomplete_ && got_favicon_from_history_ && 385 } else if (!favicon_expired_or_incomplete_ && got_favicon_from_history_ &&
404 HasValidResult(history_results_) && 386 HasValidResult(history_results_) &&
405 DoUrlsAndIconsMatch(*current_candidate(), history_results_)) { 387 DoUrlsAndIconsMatch(*current_candidate(), history_results_)) {
406 return; 388 return;
407 } 389 }
408 390
409 if (got_favicon_from_history_) 391 if (got_favicon_from_history_)
410 DownloadFaviconOrAskFaviconService( 392 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(),
411 driver_->GetActiveURL(), 393 current_candidate()->icon_url,
412 current_candidate()->icon_url, 394 current_candidate()->icon_type);
413 ToChromeIconType(current_candidate()->icon_type));
414 } 395 }
415 396
416 void FaviconHandler::OnDidDownloadFavicon( 397 void FaviconHandler::OnDidDownloadFavicon(
417 int id, 398 int id,
418 const GURL& image_url, 399 const GURL& image_url,
419 const std::vector<SkBitmap>& bitmaps, 400 const std::vector<SkBitmap>& bitmaps,
420 const std::vector<gfx::Size>& original_bitmap_sizes) { 401 const std::vector<gfx::Size>& original_bitmap_sizes) {
421 DownloadRequests::iterator i = download_requests_.find(id); 402 DownloadRequests::iterator i = download_requests_.find(id);
422 if (i == download_requests_.end()) { 403 if (i == download_requests_.end()) {
423 // Currently WebContents notifies us of ANY downloads so that it is 404 // Currently WebContents notifies us of ANY downloads so that it is
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
585 // TODO(pkotwicz): Do something better. 566 // TODO(pkotwicz): Do something better.
586 favicon_expired_or_incomplete_ = true; 567 favicon_expired_or_incomplete_ = true;
587 } 568 }
588 } 569 }
589 if (has_results && !favicon_expired_or_incomplete_) { 570 if (has_results && !favicon_expired_or_incomplete_) {
590 if (current_candidate() && 571 if (current_candidate() &&
591 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)) { 572 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)) {
592 // Mapping in the database is wrong. DownloadFavIconOrAskHistory will 573 // Mapping in the database is wrong. DownloadFavIconOrAskHistory will
593 // 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
594 // already have it. 575 // already have it.
595 DownloadFaviconOrAskFaviconService( 576 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(),
596 driver_->GetActiveURL(), 577 current_candidate()->icon_url,
597 current_candidate()->icon_url, 578 current_candidate()->icon_type);
598 ToChromeIconType(current_candidate()->icon_type));
599 } 579 }
600 } else if (current_candidate()) { 580 } else if (current_candidate()) {
601 // 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
602 // favicon or it's expired. Continue on to DownloadFaviconOrAskHistory to 582 // favicon or it's expired. Continue on to DownloadFaviconOrAskHistory to
603 // either download or check history again. 583 // either download or check history again.
604 DownloadFaviconOrAskFaviconService( 584 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(),
605 driver_->GetActiveURL(), 585 current_candidate()->icon_url,
606 current_candidate()->icon_url, 586 current_candidate()->icon_type);
607 ToChromeIconType(current_candidate()->icon_type));
608 } 587 }
609 // 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
610 // renderer to download the icon. 589 // renderer to download the icon.
611 } 590 }
612 591
613 void FaviconHandler::DownloadFaviconOrAskFaviconService( 592 void FaviconHandler::DownloadFaviconOrAskFaviconService(
614 const GURL& page_url, 593 const GURL& page_url,
615 const GURL& icon_url, 594 const GURL& icon_url,
616 favicon_base::IconType icon_type) { 595 favicon_base::IconType icon_type) {
617 if (favicon_expired_or_incomplete_) { 596 if (favicon_expired_or_incomplete_) {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
662 driver_->GetActiveFaviconURL(), 641 driver_->GetActiveFaviconURL(),
663 favicon_base::FAVICON); 642 favicon_base::FAVICON);
664 } 643 }
665 } else if (current_candidate() && 644 } else if (current_candidate() &&
666 (!has_results || has_expired_or_incomplete_result || 645 (!has_results || has_expired_or_incomplete_result ||
667 !(DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)))) { 646 !(DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)))) {
668 // We don't know the favicon, it is out of date or its type is not same as 647 // We don't know the favicon, it is out of date or its type is not same as
669 // one got from page. Request the current one. 648 // one got from page. Request the current one.
670 ScheduleDownload(driver_->GetActiveURL(), 649 ScheduleDownload(driver_->GetActiveURL(),
671 current_candidate()->icon_url, 650 current_candidate()->icon_url,
672 ToChromeIconType(current_candidate()->icon_type)); 651 current_candidate()->icon_type);
673 } 652 }
674 history_results_ = favicon_bitmap_results; 653 history_results_ = favicon_bitmap_results;
675 } 654 }
676 655
677 int FaviconHandler::ScheduleDownload(const GURL& url, 656 int FaviconHandler::ScheduleDownload(const GURL& url,
678 const GURL& image_url, 657 const GURL& image_url,
679 favicon_base::IconType icon_type) { 658 favicon_base::IconType icon_type) {
680 // A max bitmap size is specified to avoid receiving huge bitmaps in 659 // A max bitmap size is specified to avoid receiving huge bitmaps in
681 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() 660 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload()
682 // for more details about the max bitmap size. 661 // for more details about the max bitmap size.
683 const int download_id = DownloadFavicon(image_url, 662 const int download_id = DownloadFavicon(image_url,
684 GetMaximalIconSize(icon_type)); 663 GetMaximalIconSize(icon_type));
685 if (download_id) { 664 if (download_id) {
686 // Download ids should be unique. 665 // Download ids should be unique.
687 DCHECK(download_requests_.find(download_id) == download_requests_.end()); 666 DCHECK(download_requests_.find(download_id) == download_requests_.end());
688 download_requests_[download_id] = 667 download_requests_[download_id] =
689 DownloadRequest(url, image_url, icon_type); 668 DownloadRequest(url, image_url, icon_type);
690 } 669 }
691 670
692 return download_id; 671 return download_id;
693 } 672 }
694 673
695 void FaviconHandler::SortAndPruneImageUrls() { 674 void FaviconHandler::SortAndPruneImageUrls() {
696 for (std::vector<FaviconURL>::iterator i = image_urls_.begin(); 675 for (std::vector<FaviconURL>::iterator i = image_urls_.begin();
697 i != image_urls_.end();) { 676 i != image_urls_.end();) {
698 if (i->icon_sizes.empty()) { 677 if (i->icon_sizes.empty()) {
699 ++i; 678 ++i;
700 continue; 679 continue;
701 } 680 }
702 int max_size = GetMaximalIconSize(ToChromeIconType(i->icon_type)); 681 int max_size = GetMaximalIconSize(i->icon_type);
703 int index = GetLargestSizeIndex(i->icon_sizes, max_size * max_size); 682 int index = GetLargestSizeIndex(i->icon_sizes, max_size * max_size);
704 if (index == -1) { 683 if (index == -1) {
705 i = image_urls_.erase(i); 684 i = image_urls_.erase(i);
706 } else { 685 } else {
707 gfx::Size largest = i->icon_sizes[index]; 686 gfx::Size largest = i->icon_sizes[index];
708 i->icon_sizes.clear(); 687 i->icon_sizes.clear();
709 i->icon_sizes.push_back(largest); 688 i->icon_sizes.push_back(largest);
710 ++i; 689 ++i;
711 } 690 }
712 } 691 }
713 std::stable_sort(image_urls_.begin(), image_urls_.end(), 692 std::stable_sort(image_urls_.begin(), image_urls_.end(),
714 CompareIconSize); 693 CompareIconSize);
715 } 694 }
OLDNEW
« no previous file with comments | « chrome/browser/favicon/favicon_handler.h ('k') | chrome/browser/favicon/favicon_handler_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698