| 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 "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 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 13 #include "base/memory/ref_counted_memory.h" | 13 #include "base/memory/ref_counted_memory.h" |
| 14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 15 #include "components/favicon/core/favicon_driver.h" | |
| 16 #include "components/favicon/core/favicon_service.h" | 15 #include "components/favicon/core/favicon_service.h" |
| 17 #include "components/favicon_base/favicon_util.h" | 16 #include "components/favicon_base/favicon_util.h" |
| 18 #include "components/favicon_base/select_favicon_frames.h" | 17 #include "components/favicon_base/select_favicon_frames.h" |
| 19 #include "skia/ext/image_operations.h" | 18 #include "skia/ext/image_operations.h" |
| 20 #include "ui/gfx/codec/png_codec.h" | 19 #include "ui/gfx/codec/png_codec.h" |
| 21 #include "ui/gfx/image/image_skia.h" | 20 #include "ui/gfx/image/image_skia.h" |
| 22 #include "ui/gfx/image/image_util.h" | 21 #include "ui/gfx/image/image_util.h" |
| 23 | 22 |
| 24 namespace favicon { | 23 namespace favicon { |
| 25 namespace { | 24 namespace { |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 favicon_base::IconType icon_type) | 204 favicon_base::IconType icon_type) |
| 206 : image_url(image_url), | 205 : image_url(image_url), |
| 207 image(image), | 206 image(image), |
| 208 score(score), | 207 score(score), |
| 209 icon_type(icon_type) {} | 208 icon_type(icon_type) {} |
| 210 | 209 |
| 211 //////////////////////////////////////////////////////////////////////////////// | 210 //////////////////////////////////////////////////////////////////////////////// |
| 212 | 211 |
| 213 FaviconHandler::FaviconHandler( | 212 FaviconHandler::FaviconHandler( |
| 214 FaviconService* service, | 213 FaviconService* service, |
| 215 FaviconDriver* driver, | 214 Delegate* delegate, |
| 216 FaviconDriverObserver::NotificationIconType handler_type) | 215 FaviconDriverObserver::NotificationIconType handler_type) |
| 217 : handler_type_(handler_type), | 216 : handler_type_(handler_type), |
| 218 got_favicon_from_history_(false), | 217 got_favicon_from_history_(false), |
| 219 initial_history_result_expired_or_incomplete_(false), | 218 initial_history_result_expired_or_incomplete_(false), |
| 220 redownload_icons_(false), | 219 redownload_icons_(false), |
| 221 icon_types_(FaviconHandler::GetIconTypesFromHandlerType(handler_type)), | 220 icon_types_(FaviconHandler::GetIconTypesFromHandlerType(handler_type)), |
| 222 download_largest_icon_( | 221 download_largest_icon_( |
| 223 handler_type == FaviconDriverObserver::NON_TOUCH_LARGEST || | 222 handler_type == FaviconDriverObserver::NON_TOUCH_LARGEST || |
| 224 handler_type == FaviconDriverObserver::TOUCH_LARGEST), | 223 handler_type == FaviconDriverObserver::TOUCH_LARGEST), |
| 225 notification_icon_type_(favicon_base::INVALID_ICON), | 224 notification_icon_type_(favicon_base::INVALID_ICON), |
| 226 service_(service), | 225 service_(service), |
| 227 driver_(driver), | 226 delegate_(delegate), |
| 228 current_candidate_index_(0u) { | 227 current_candidate_index_(0u), |
| 229 DCHECK(driver_); | 228 weak_ptr_factory_(this) { |
| 229 DCHECK(delegate_); |
| 230 } | 230 } |
| 231 | 231 |
| 232 FaviconHandler::~FaviconHandler() { | 232 FaviconHandler::~FaviconHandler() { |
| 233 } | 233 } |
| 234 | 234 |
| 235 // static | 235 // static |
| 236 int FaviconHandler::GetIconTypesFromHandlerType( | 236 int FaviconHandler::GetIconTypesFromHandlerType( |
| 237 FaviconDriverObserver::NotificationIconType handler_type) { | 237 FaviconDriverObserver::NotificationIconType handler_type) { |
| 238 switch (handler_type) { | 238 switch (handler_type) { |
| 239 case FaviconDriverObserver::NON_TOUCH_16_DIP: | 239 case FaviconDriverObserver::NON_TOUCH_16_DIP: |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 | 346 |
| 347 void FaviconHandler::NotifyFaviconUpdated(const GURL& icon_url, | 347 void FaviconHandler::NotifyFaviconUpdated(const GURL& icon_url, |
| 348 favicon_base::IconType icon_type, | 348 favicon_base::IconType icon_type, |
| 349 const gfx::Image& image) { | 349 const gfx::Image& image) { |
| 350 if (image.IsEmpty()) | 350 if (image.IsEmpty()) |
| 351 return; | 351 return; |
| 352 | 352 |
| 353 gfx::Image image_with_adjusted_colorspace = image; | 353 gfx::Image image_with_adjusted_colorspace = image; |
| 354 favicon_base::SetFaviconColorSpace(&image_with_adjusted_colorspace); | 354 favicon_base::SetFaviconColorSpace(&image_with_adjusted_colorspace); |
| 355 | 355 |
| 356 driver_->OnFaviconUpdated(url_, handler_type_, icon_url, | 356 delegate_->OnFaviconUpdated(url_, handler_type_, icon_url, |
| 357 icon_url != notification_icon_url_, | 357 icon_url != notification_icon_url_, |
| 358 image_with_adjusted_colorspace); | 358 image_with_adjusted_colorspace); |
| 359 | 359 |
| 360 notification_icon_url_ = icon_url; | 360 notification_icon_url_ = icon_url; |
| 361 notification_icon_type_ = icon_type; | 361 notification_icon_type_ = icon_type; |
| 362 } | 362 } |
| 363 | 363 |
| 364 void FaviconHandler::OnUpdateFaviconURL( | 364 void FaviconHandler::OnUpdateFaviconURL( |
| 365 const GURL& page_url, | 365 const GURL& page_url, |
| 366 const std::vector<FaviconURL>& candidates) { | 366 const std::vector<FaviconURL>& candidates) { |
| 367 if (page_url != url_) | 367 if (page_url != url_) |
| 368 return; | 368 return; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 // that we can check whether the page's icon URLs match the page's icon URLs | 408 // that we can check whether the page's icon URLs match the page's icon URLs |
| 409 // at the time that the favicon data was stored to the history database. | 409 // at the time that the favicon data was stored to the history database. |
| 410 return; | 410 return; |
| 411 } | 411 } |
| 412 | 412 |
| 413 DownloadCurrentCandidateOrAskFaviconService(); | 413 DownloadCurrentCandidateOrAskFaviconService(); |
| 414 } | 414 } |
| 415 | 415 |
| 416 void FaviconHandler::OnDidDownloadFavicon( | 416 void FaviconHandler::OnDidDownloadFavicon( |
| 417 int id, | 417 int id, |
| 418 int http_status_code, |
| 418 const GURL& image_url, | 419 const GURL& image_url, |
| 419 const std::vector<SkBitmap>& bitmaps, | 420 const std::vector<SkBitmap>& bitmaps, |
| 420 const std::vector<gfx::Size>& original_bitmap_sizes) { | 421 const std::vector<gfx::Size>& original_bitmap_sizes) { |
| 422 if (bitmaps.empty() && http_status_code == 404) { |
| 423 DVLOG(1) << "Failed to Download Favicon:" << image_url; |
| 424 if (service_) |
| 425 service_->UnableToDownloadFavicon(image_url); |
| 426 } |
| 427 |
| 421 DownloadRequests::iterator i = download_requests_.find(id); | 428 DownloadRequests::iterator i = download_requests_.find(id); |
| 422 if (i == download_requests_.end()) { | 429 if (i == download_requests_.end()) { |
| 423 // Currently WebContents notifies us of ANY downloads so that it is | 430 // Currently WebContents notifies us of ANY downloads so that it is |
| 424 // possible to get here. | 431 // possible to get here. |
| 425 return; | 432 return; |
| 426 } | 433 } |
| 427 | 434 |
| 428 DownloadRequest download_request = i->second; | 435 DownloadRequest download_request = i->second; |
| 429 download_requests_.erase(i); | 436 download_requests_.erase(i); |
| 430 | 437 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 485 current_candidate_index_ = image_urls_.size(); | 492 current_candidate_index_ = image_urls_.size(); |
| 486 best_favicon_candidate_ = FaviconCandidate(); | 493 best_favicon_candidate_ = FaviconCandidate(); |
| 487 } | 494 } |
| 488 } | 495 } |
| 489 | 496 |
| 490 bool FaviconHandler::HasPendingTasksForTest() { | 497 bool FaviconHandler::HasPendingTasksForTest() { |
| 491 return !download_requests_.empty() || | 498 return !download_requests_.empty() || |
| 492 cancelable_task_tracker_.HasTrackedTasks(); | 499 cancelable_task_tracker_.HasTrackedTasks(); |
| 493 } | 500 } |
| 494 | 501 |
| 495 int FaviconHandler::DownloadFavicon(const GURL& image_url, | |
| 496 int max_bitmap_size) { | |
| 497 if (!image_url.is_valid()) { | |
| 498 NOTREACHED(); | |
| 499 return 0; | |
| 500 } | |
| 501 return driver_->StartDownload(image_url, max_bitmap_size); | |
| 502 } | |
| 503 | |
| 504 void FaviconHandler::UpdateFaviconMappingAndFetch( | 502 void FaviconHandler::UpdateFaviconMappingAndFetch( |
| 505 const GURL& page_url, | 503 const GURL& page_url, |
| 506 const GURL& icon_url, | 504 const GURL& icon_url, |
| 507 favicon_base::IconType icon_type, | 505 favicon_base::IconType icon_type, |
| 508 const favicon_base::FaviconResultsCallback& callback, | 506 const favicon_base::FaviconResultsCallback& callback, |
| 509 base::CancelableTaskTracker* tracker) { | 507 base::CancelableTaskTracker* tracker) { |
| 510 // TODO(pkotwicz): pass in all of |image_urls_| to | 508 // TODO(pkotwicz): pass in all of |image_urls_| to |
| 511 // UpdateFaviconMappingsAndFetch(). | 509 // UpdateFaviconMappingsAndFetch(). |
| 512 if (service_) { | 510 if (service_) { |
| 513 std::vector<GURL> icon_urls; | 511 std::vector<GURL> icon_urls; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 543 void FaviconHandler::SetHistoryFavicons(const GURL& page_url, | 541 void FaviconHandler::SetHistoryFavicons(const GURL& page_url, |
| 544 const GURL& icon_url, | 542 const GURL& icon_url, |
| 545 favicon_base::IconType icon_type, | 543 favicon_base::IconType icon_type, |
| 546 const gfx::Image& image) { | 544 const gfx::Image& image) { |
| 547 if (service_) { | 545 if (service_) { |
| 548 service_->SetFavicons(page_url, icon_url, icon_type, image); | 546 service_->SetFavicons(page_url, icon_url, icon_type, image); |
| 549 } | 547 } |
| 550 } | 548 } |
| 551 | 549 |
| 552 bool FaviconHandler::ShouldSaveFavicon() { | 550 bool FaviconHandler::ShouldSaveFavicon() { |
| 553 if (!driver_->IsOffTheRecord()) | 551 if (!delegate_->IsOffTheRecord()) |
| 554 return true; | 552 return true; |
| 555 | 553 |
| 556 // Always save favicon if the page is bookmarked. | 554 // Always save favicon if the page is bookmarked. |
| 557 return driver_->IsBookmarked(url_); | 555 return delegate_->IsBookmarked(url_); |
| 558 } | 556 } |
| 559 | 557 |
| 560 int FaviconHandler::GetMaximalIconSize(favicon_base::IconType icon_type) { | 558 int FaviconHandler::GetMaximalIconSize(favicon_base::IconType icon_type) { |
| 561 switch (icon_type) { | 559 switch (icon_type) { |
| 562 case favicon_base::FAVICON: | 560 case favicon_base::FAVICON: |
| 563 #if defined(OS_ANDROID) | 561 #if defined(OS_ANDROID) |
| 564 return 192; | 562 return 192; |
| 565 #else | 563 #else |
| 566 return gfx::ImageSkia::GetMaxSupportedScale() * gfx::kFaviconSize; | 564 return gfx::ImageSkia::GetMaxSupportedScale() * gfx::kFaviconSize; |
| 567 #endif | 565 #endif |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 605 GURL icon_url = current_candidate()->icon_url; | 603 GURL icon_url = current_candidate()->icon_url; |
| 606 favicon_base::IconType icon_type = current_candidate()->icon_type; | 604 favicon_base::IconType icon_type = current_candidate()->icon_type; |
| 607 | 605 |
| 608 if (redownload_icons_) { | 606 if (redownload_icons_) { |
| 609 // We have the mapping, but the favicon is out of date. Download it now. | 607 // We have the mapping, but the favicon is out of date. Download it now. |
| 610 ScheduleDownload(icon_url, icon_type); | 608 ScheduleDownload(icon_url, icon_type); |
| 611 } else { | 609 } else { |
| 612 // We don't know the favicon, but we may have previously downloaded the | 610 // We don't know the favicon, but we may have previously downloaded the |
| 613 // favicon for another page that shares the same favicon. Ask for the | 611 // favicon for another page that shares the same favicon. Ask for the |
| 614 // favicon given the favicon URL. | 612 // favicon given the favicon URL. |
| 615 if (driver_->IsOffTheRecord()) { | 613 if (delegate_->IsOffTheRecord()) { |
| 616 GetFaviconFromFaviconService( | 614 GetFaviconFromFaviconService( |
| 617 icon_url, icon_type, | 615 icon_url, icon_type, |
| 618 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)), | 616 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)), |
| 619 &cancelable_task_tracker_); | 617 &cancelable_task_tracker_); |
| 620 } else { | 618 } else { |
| 621 // Ask the history service for the icon. This does two things: | 619 // Ask the history service for the icon. This does two things: |
| 622 // 1. Attempts to fetch the favicon data from the database. | 620 // 1. Attempts to fetch the favicon data from the database. |
| 623 // 2. If the favicon exists in the database, this updates the database to | 621 // 2. If the favicon exists in the database, this updates the database to |
| 624 // include the mapping between the page url and the favicon url. | 622 // include the mapping between the page url and the favicon url. |
| 625 // This is asynchronous. The history service will call back when done. | 623 // This is asynchronous. The history service will call back when done. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 655 } | 653 } |
| 656 | 654 |
| 657 if (has_expired_or_incomplete_result) { | 655 if (has_expired_or_incomplete_result) { |
| 658 ScheduleDownload(current_candidate()->icon_url, | 656 ScheduleDownload(current_candidate()->icon_url, |
| 659 current_candidate()->icon_type); | 657 current_candidate()->icon_type); |
| 660 } | 658 } |
| 661 } | 659 } |
| 662 | 660 |
| 663 void FaviconHandler::ScheduleDownload(const GURL& image_url, | 661 void FaviconHandler::ScheduleDownload(const GURL& image_url, |
| 664 favicon_base::IconType icon_type) { | 662 favicon_base::IconType icon_type) { |
| 663 if (!image_url.is_valid()) { |
| 664 NOTREACHED(); |
| 665 return; |
| 666 } |
| 665 // A max bitmap size is specified to avoid receiving huge bitmaps in | 667 // A max bitmap size is specified to avoid receiving huge bitmaps in |
| 666 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() | 668 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() |
| 667 // for more details about the max bitmap size. | 669 // for more details about the max bitmap size. |
| 668 const int download_id = DownloadFavicon(image_url, | 670 const int download_id = |
| 669 GetMaximalIconSize(icon_type)); | 671 delegate_->DownloadImage(image_url, GetMaximalIconSize(icon_type), |
| 672 base::Bind(&FaviconHandler::OnDidDownloadFavicon, |
| 673 weak_ptr_factory_.GetWeakPtr())); |
| 670 | 674 |
| 671 // Download ids should be unique. | 675 // Download ids should be unique. |
| 672 DCHECK(download_requests_.find(download_id) == download_requests_.end()); | 676 DCHECK(download_requests_.find(download_id) == download_requests_.end()); |
| 673 download_requests_[download_id] = DownloadRequest(image_url, icon_type); | 677 download_requests_[download_id] = DownloadRequest(image_url, icon_type); |
| 674 | 678 |
| 675 if (download_id == 0) { | 679 if (download_id == 0) { |
| 676 // If DownloadFavicon() did not start a download, it returns a download id | 680 // If DownloadFavicon() did not start a download, it returns a download id |
| 677 // of 0. We still need to call OnDidDownloadFavicon() because the method is | 681 // of 0. We still need to call OnDidDownloadFavicon() because the method is |
| 678 // responsible for initiating the data request for the next candidate. | 682 // responsible for initiating the data request for the next candidate. |
| 679 OnDidDownloadFavicon(download_id, image_url, std::vector<SkBitmap>(), | 683 OnDidDownloadFavicon(download_id, 0, image_url, std::vector<SkBitmap>(), |
| 680 std::vector<gfx::Size>()); | 684 std::vector<gfx::Size>()); |
| 681 | |
| 682 } | 685 } |
| 683 } | 686 } |
| 684 | 687 |
| 685 } // namespace favicon | 688 } // namespace favicon |
| OLD | NEW |