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 "build/build_config.h" | 7 #include "build/build_config.h" |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <vector> | 10 #include <vector> |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 favicon_base::IconType icon_type) | 234 favicon_base::IconType icon_type) |
235 : url(url), | 235 : url(url), |
236 image_url(image_url), | 236 image_url(image_url), |
237 image(image), | 237 image(image), |
238 score(score), | 238 score(score), |
239 icon_type(icon_type) {} | 239 icon_type(icon_type) {} |
240 | 240 |
241 //////////////////////////////////////////////////////////////////////////////// | 241 //////////////////////////////////////////////////////////////////////////////// |
242 | 242 |
243 FaviconHandler::FaviconHandler(FaviconClient* client, | 243 FaviconHandler::FaviconHandler(FaviconClient* client, |
244 FaviconHandlerDelegate* delegate, | 244 FaviconDriver* driver, |
245 Type icon_type, | 245 Type icon_type, |
246 bool download_largest_icon) | 246 bool download_largest_icon) |
247 : got_favicon_from_history_(false), | 247 : got_favicon_from_history_(false), |
248 favicon_expired_or_incomplete_(false), | 248 favicon_expired_or_incomplete_(false), |
249 icon_types_(icon_type == FAVICON | 249 icon_types_(icon_type == FAVICON |
250 ? favicon_base::FAVICON | 250 ? favicon_base::FAVICON |
251 : favicon_base::TOUCH_ICON | | 251 : favicon_base::TOUCH_ICON | |
252 favicon_base::TOUCH_PRECOMPOSED_ICON), | 252 favicon_base::TOUCH_PRECOMPOSED_ICON), |
253 download_largest_icon_(download_largest_icon), | 253 download_largest_icon_(download_largest_icon), |
254 client_(client), | 254 client_(client), |
255 delegate_(delegate) { | 255 driver_(driver) { |
256 DCHECK(delegate_); | 256 DCHECK(driver_); |
257 } | 257 } |
258 | 258 |
259 FaviconHandler::~FaviconHandler() { | 259 FaviconHandler::~FaviconHandler() { |
260 } | 260 } |
261 | 261 |
262 void FaviconHandler::FetchFavicon(const GURL& url) { | 262 void FaviconHandler::FetchFavicon(const GURL& url) { |
263 cancelable_task_tracker_.TryCancelAll(); | 263 cancelable_task_tracker_.TryCancelAll(); |
264 | 264 |
265 url_ = url; | 265 url_ = url; |
266 | 266 |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
488 best_favicon_candidate_.icon_type); | 488 best_favicon_candidate_.icon_type); |
489 // Reset candidate. | 489 // Reset candidate. |
490 image_urls_.clear(); | 490 image_urls_.clear(); |
491 best_favicon_candidate_ = FaviconCandidate(); | 491 best_favicon_candidate_ = FaviconCandidate(); |
492 } | 492 } |
493 } | 493 } |
494 download_requests_.erase(i); | 494 download_requests_.erase(i); |
495 } | 495 } |
496 | 496 |
497 NavigationEntry* FaviconHandler::GetEntry() { | 497 NavigationEntry* FaviconHandler::GetEntry() { |
498 NavigationEntry* entry = delegate_->GetActiveEntry(); | 498 NavigationEntry* entry = driver_->GetActiveEntry(); |
499 if (entry && UrlMatches(entry->GetURL(), url_)) | 499 if (entry && UrlMatches(entry->GetURL(), url_)) |
500 return entry; | 500 return entry; |
501 | 501 |
502 // If the URL has changed out from under us (as will happen with redirects) | 502 // If the URL has changed out from under us (as will happen with redirects) |
503 // return NULL. | 503 // return NULL. |
504 return NULL; | 504 return NULL; |
505 } | 505 } |
506 | 506 |
507 int FaviconHandler::DownloadFavicon(const GURL& image_url, | 507 int FaviconHandler::DownloadFavicon(const GURL& image_url, |
508 int max_bitmap_size) { | 508 int max_bitmap_size) { |
509 if (!image_url.is_valid()) { | 509 if (!image_url.is_valid()) { |
510 NOTREACHED(); | 510 NOTREACHED(); |
511 return 0; | 511 return 0; |
512 } | 512 } |
513 return delegate_->StartDownload(image_url, max_bitmap_size); | 513 return driver_->StartDownload(image_url, max_bitmap_size); |
514 } | 514 } |
515 | 515 |
516 void FaviconHandler::UpdateFaviconMappingAndFetch( | 516 void FaviconHandler::UpdateFaviconMappingAndFetch( |
517 const GURL& page_url, | 517 const GURL& page_url, |
518 const GURL& icon_url, | 518 const GURL& icon_url, |
519 favicon_base::IconType icon_type, | 519 favicon_base::IconType icon_type, |
520 const FaviconService::FaviconResultsCallback& callback, | 520 const FaviconService::FaviconResultsCallback& callback, |
521 base::CancelableTaskTracker* tracker) { | 521 base::CancelableTaskTracker* tracker) { |
522 // TODO(pkotwicz): pass in all of |image_urls_| to | 522 // TODO(pkotwicz): pass in all of |image_urls_| to |
523 // UpdateFaviconMappingsAndFetch(). | 523 // UpdateFaviconMappingsAndFetch(). |
(...skipping 26 matching lines...) Expand all Loading... |
550 | 550 |
551 void FaviconHandler::SetHistoryFavicons(const GURL& page_url, | 551 void FaviconHandler::SetHistoryFavicons(const GURL& page_url, |
552 const GURL& icon_url, | 552 const GURL& icon_url, |
553 favicon_base::IconType icon_type, | 553 favicon_base::IconType icon_type, |
554 const gfx::Image& image) { | 554 const gfx::Image& image) { |
555 client_->GetFaviconService()->SetFavicons( | 555 client_->GetFaviconService()->SetFavicons( |
556 page_url, icon_url, icon_type, image); | 556 page_url, icon_url, icon_type, image); |
557 } | 557 } |
558 | 558 |
559 bool FaviconHandler::ShouldSaveFavicon(const GURL& url) { | 559 bool FaviconHandler::ShouldSaveFavicon(const GURL& url) { |
560 if (!delegate_->IsOffTheRecord()) | 560 if (!driver_->IsOffTheRecord()) |
561 return true; | 561 return true; |
562 | 562 |
563 // Otherwise store the favicon if the page is bookmarked. | 563 // Otherwise store the favicon if the page is bookmarked. |
564 return client_->IsBookmarked(url); | 564 return client_->IsBookmarked(url); |
565 } | 565 } |
566 | 566 |
567 void FaviconHandler::NotifyFaviconUpdated(bool icon_url_changed) { | 567 void FaviconHandler::NotifyFaviconUpdated(bool icon_url_changed) { |
568 delegate_->NotifyFaviconUpdated(icon_url_changed); | 568 driver_->NotifyFaviconUpdated(icon_url_changed); |
569 } | 569 } |
570 | 570 |
571 void FaviconHandler::OnFaviconDataForInitialURLFromFaviconService( | 571 void FaviconHandler::OnFaviconDataForInitialURLFromFaviconService( |
572 const std::vector<favicon_base::FaviconBitmapResult>& | 572 const std::vector<favicon_base::FaviconBitmapResult>& |
573 favicon_bitmap_results) { | 573 favicon_bitmap_results) { |
574 NavigationEntry* entry = GetEntry(); | 574 NavigationEntry* entry = GetEntry(); |
575 if (!entry) | 575 if (!entry) |
576 return; | 576 return; |
577 | 577 |
578 got_favicon_from_history_ = true; | 578 got_favicon_from_history_ = true; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
626 const GURL& page_url, | 626 const GURL& page_url, |
627 const GURL& icon_url, | 627 const GURL& icon_url, |
628 favicon_base::IconType icon_type) { | 628 favicon_base::IconType icon_type) { |
629 if (favicon_expired_or_incomplete_) { | 629 if (favicon_expired_or_incomplete_) { |
630 // We have the mapping, but the favicon is out of date. Download it now. | 630 // We have the mapping, but the favicon is out of date. Download it now. |
631 ScheduleDownload(page_url, icon_url, icon_type); | 631 ScheduleDownload(page_url, icon_url, icon_type); |
632 } else if (client_->GetFaviconService()) { | 632 } else if (client_->GetFaviconService()) { |
633 // We don't know the favicon, but we may have previously downloaded the | 633 // We don't know the favicon, but we may have previously downloaded the |
634 // favicon for another page that shares the same favicon. Ask for the | 634 // favicon for another page that shares the same favicon. Ask for the |
635 // favicon given the favicon URL. | 635 // favicon given the favicon URL. |
636 if (delegate_->IsOffTheRecord()) { | 636 if (driver_->IsOffTheRecord()) { |
637 GetFaviconFromFaviconService( | 637 GetFaviconFromFaviconService( |
638 icon_url, icon_type, | 638 icon_url, icon_type, |
639 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)), | 639 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)), |
640 &cancelable_task_tracker_); | 640 &cancelable_task_tracker_); |
641 } else { | 641 } else { |
642 // Ask the history service for the icon. This does two things: | 642 // Ask the history service for the icon. This does two things: |
643 // 1. Attempts to fetch the favicon data from the database. | 643 // 1. Attempts to fetch the favicon data from the database. |
644 // 2. If the favicon exists in the database, this updates the database to | 644 // 2. If the favicon exists in the database, this updates the database to |
645 // include the mapping between the page url and the favicon url. | 645 // include the mapping between the page url and the favicon url. |
646 // This is asynchronous. The history service will call back when done. | 646 // This is asynchronous. The history service will call back when done. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
682 ScheduleDownload(entry->GetURL(), current_candidate()->icon_url, | 682 ScheduleDownload(entry->GetURL(), current_candidate()->icon_url, |
683 ToChromeIconType(current_candidate()->icon_type)); | 683 ToChromeIconType(current_candidate()->icon_type)); |
684 } | 684 } |
685 history_results_ = favicon_bitmap_results; | 685 history_results_ = favicon_bitmap_results; |
686 } | 686 } |
687 | 687 |
688 int FaviconHandler::ScheduleDownload(const GURL& url, | 688 int FaviconHandler::ScheduleDownload(const GURL& url, |
689 const GURL& image_url, | 689 const GURL& image_url, |
690 favicon_base::IconType icon_type) { | 690 favicon_base::IconType icon_type) { |
691 // A max bitmap size is specified to avoid receiving huge bitmaps in | 691 // A max bitmap size is specified to avoid receiving huge bitmaps in |
692 // OnDidDownloadFavicon(). See FaviconHandlerDelegate::StartDownload() | 692 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() |
693 // for more details about the max bitmap size. | 693 // for more details about the max bitmap size. |
694 const int download_id = DownloadFavicon(image_url, | 694 const int download_id = DownloadFavicon(image_url, |
695 GetMaximalIconSize(icon_type)); | 695 GetMaximalIconSize(icon_type)); |
696 if (download_id) { | 696 if (download_id) { |
697 // Download ids should be unique. | 697 // Download ids should be unique. |
698 DCHECK(download_requests_.find(download_id) == download_requests_.end()); | 698 DCHECK(download_requests_.find(download_id) == download_requests_.end()); |
699 download_requests_[download_id] = | 699 download_requests_[download_id] = |
700 DownloadRequest(url, image_url, icon_type); | 700 DownloadRequest(url, image_url, icon_type); |
701 } | 701 } |
702 | 702 |
(...skipping 14 matching lines...) Expand all Loading... |
717 } else { | 717 } else { |
718 gfx::Size largest = i->icon_sizes[index]; | 718 gfx::Size largest = i->icon_sizes[index]; |
719 i->icon_sizes.clear(); | 719 i->icon_sizes.clear(); |
720 i->icon_sizes.push_back(largest); | 720 i->icon_sizes.push_back(largest); |
721 ++i; | 721 ++i; |
722 } | 722 } |
723 } | 723 } |
724 std::stable_sort(image_urls_.begin(), image_urls_.end(), | 724 std::stable_sort(image_urls_.begin(), image_urls_.end(), |
725 CompareIconSize); | 725 CompareIconSize); |
726 } | 726 } |
OLD | NEW |