Chromium Code Reviews| 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(service_); | |
| 230 DCHECK(delegate_); | |
| 230 } | 231 } |
| 231 | 232 |
| 232 FaviconHandler::~FaviconHandler() { | 233 FaviconHandler::~FaviconHandler() { |
| 233 } | 234 } |
| 234 | 235 |
| 235 // static | 236 // static |
| 236 int FaviconHandler::GetIconTypesFromHandlerType( | 237 int FaviconHandler::GetIconTypesFromHandlerType( |
| 237 FaviconDriverObserver::NotificationIconType handler_type) { | 238 FaviconDriverObserver::NotificationIconType handler_type) { |
| 238 switch (handler_type) { | 239 switch (handler_type) { |
| 239 case FaviconDriverObserver::NON_TOUCH_16_DIP: | 240 case FaviconDriverObserver::NON_TOUCH_16_DIP: |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 346 | 347 |
| 347 void FaviconHandler::NotifyFaviconUpdated(const GURL& icon_url, | 348 void FaviconHandler::NotifyFaviconUpdated(const GURL& icon_url, |
| 348 favicon_base::IconType icon_type, | 349 favicon_base::IconType icon_type, |
| 349 const gfx::Image& image) { | 350 const gfx::Image& image) { |
| 350 if (image.IsEmpty()) | 351 if (image.IsEmpty()) |
| 351 return; | 352 return; |
| 352 | 353 |
| 353 gfx::Image image_with_adjusted_colorspace = image; | 354 gfx::Image image_with_adjusted_colorspace = image; |
| 354 favicon_base::SetFaviconColorSpace(&image_with_adjusted_colorspace); | 355 favicon_base::SetFaviconColorSpace(&image_with_adjusted_colorspace); |
| 355 | 356 |
| 356 driver_->OnFaviconUpdated(url_, handler_type_, icon_url, | 357 delegate_->OnFaviconUpdated(url_, handler_type_, icon_url, |
| 357 icon_url != notification_icon_url_, | 358 icon_url != notification_icon_url_, |
| 358 image_with_adjusted_colorspace); | 359 image_with_adjusted_colorspace); |
| 359 | 360 |
| 360 notification_icon_url_ = icon_url; | 361 notification_icon_url_ = icon_url; |
| 361 notification_icon_type_ = icon_type; | 362 notification_icon_type_ = icon_type; |
| 362 } | 363 } |
| 363 | 364 |
| 364 void FaviconHandler::OnUpdateFaviconURL( | 365 void FaviconHandler::OnUpdateFaviconURL( |
| 365 const GURL& page_url, | 366 const GURL& page_url, |
| 366 const std::vector<FaviconURL>& candidates) { | 367 const std::vector<FaviconURL>& candidates) { |
| 367 if (page_url != url_) | 368 if (page_url != url_) |
| 368 return; | 369 return; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 406 // We are done. No additional downloads or history requests are needed. | 407 // 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 | 408 // TODO: Store all of the icon URLs associated with a page in history so |
| 408 // that we can check whether the page's icon URLs match the page's icon URLs | 409 // 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. | 410 // at the time that the favicon data was stored to the history database. |
| 410 return; | 411 return; |
| 411 } | 412 } |
| 412 | 413 |
| 413 DownloadCurrentCandidateOrAskFaviconService(); | 414 DownloadCurrentCandidateOrAskFaviconService(); |
| 414 } | 415 } |
| 415 | 416 |
| 416 void FaviconHandler::OnDidDownloadFavicon( | 417 void FaviconHandler::DidDownloadFavicon( |
| 417 int id, | 418 int id, |
| 419 int http_status_code, | |
| 418 const GURL& image_url, | 420 const GURL& image_url, |
| 419 const std::vector<SkBitmap>& bitmaps, | 421 const std::vector<SkBitmap>& bitmaps, |
| 420 const std::vector<gfx::Size>& original_bitmap_sizes) { | 422 const std::vector<gfx::Size>& original_bitmap_sizes) { |
| 423 if (bitmaps.empty() && http_status_code == 404) { | |
| 424 DVLOG(1) << "Failed to Download Favicon:" << image_url; | |
| 425 if (service_) | |
| 426 service_->UnableToDownloadFavicon(image_url); | |
| 427 } | |
| 428 | |
| 421 DownloadRequests::iterator i = download_requests_.find(id); | 429 DownloadRequests::iterator i = download_requests_.find(id); |
| 422 if (i == download_requests_.end()) { | 430 if (i == download_requests_.end()) { |
| 423 // Currently WebContents notifies us of ANY downloads so that it is | 431 // Currently WebContents notifies us of ANY downloads so that it is |
| 424 // possible to get here. | 432 // possible to get here. |
| 425 return; | 433 return; |
| 426 } | 434 } |
| 427 | 435 |
| 428 DownloadRequest download_request = i->second; | 436 DownloadRequest download_request = i->second; |
| 429 download_requests_.erase(i); | 437 download_requests_.erase(i); |
| 430 | 438 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 485 current_candidate_index_ = image_urls_.size(); | 493 current_candidate_index_ = image_urls_.size(); |
| 486 best_favicon_candidate_ = FaviconCandidate(); | 494 best_favicon_candidate_ = FaviconCandidate(); |
| 487 } | 495 } |
| 488 } | 496 } |
| 489 | 497 |
| 490 bool FaviconHandler::HasPendingTasksForTest() { | 498 bool FaviconHandler::HasPendingTasksForTest() { |
| 491 return !download_requests_.empty() || | 499 return !download_requests_.empty() || |
| 492 cancelable_task_tracker_.HasTrackedTasks(); | 500 cancelable_task_tracker_.HasTrackedTasks(); |
| 493 } | 501 } |
| 494 | 502 |
| 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( | 503 void FaviconHandler::UpdateFaviconMappingAndFetch( |
| 505 const GURL& page_url, | 504 const GURL& page_url, |
| 506 const GURL& icon_url, | 505 const GURL& icon_url, |
| 507 favicon_base::IconType icon_type, | 506 favicon_base::IconType icon_type, |
| 508 const favicon_base::FaviconResultsCallback& callback, | 507 const favicon_base::FaviconResultsCallback& callback, |
| 509 base::CancelableTaskTracker* tracker) { | 508 base::CancelableTaskTracker* tracker) { |
| 510 // TODO(pkotwicz): pass in all of |image_urls_| to | 509 // TODO(pkotwicz): pass in all of |image_urls_| to |
| 511 // UpdateFaviconMappingsAndFetch(). | 510 // UpdateFaviconMappingsAndFetch(). |
| 512 if (service_) { | 511 if (service_) { |
| 513 std::vector<GURL> icon_urls; | 512 std::vector<GURL> icon_urls; |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 543 void FaviconHandler::SetHistoryFavicons(const GURL& page_url, | 542 void FaviconHandler::SetHistoryFavicons(const GURL& page_url, |
| 544 const GURL& icon_url, | 543 const GURL& icon_url, |
| 545 favicon_base::IconType icon_type, | 544 favicon_base::IconType icon_type, |
| 546 const gfx::Image& image) { | 545 const gfx::Image& image) { |
| 547 if (service_) { | 546 if (service_) { |
| 548 service_->SetFavicons(page_url, icon_url, icon_type, image); | 547 service_->SetFavicons(page_url, icon_url, icon_type, image); |
| 549 } | 548 } |
| 550 } | 549 } |
| 551 | 550 |
| 552 bool FaviconHandler::ShouldSaveFavicon() { | 551 bool FaviconHandler::ShouldSaveFavicon() { |
| 553 if (!driver_->IsOffTheRecord()) | 552 if (!delegate_->IsOffTheRecord()) |
| 554 return true; | 553 return true; |
| 555 | 554 |
| 556 // Always save favicon if the page is bookmarked. | 555 // Always save favicon if the page is bookmarked. |
| 557 return driver_->IsBookmarked(url_); | 556 // TODO(mastiz): Fix. |
|
pkotwicz
2017/02/13 23:35:48
It is possible to bookmark a page while in incogni
mastiz
2017/02/23 21:55:53
I believe this is a bug that needs to be fixed but
| |
| 557 // return driver_->IsBookmarked(url_); | |
| 558 DCHECK(false); | |
| 559 return false; | |
| 558 } | 560 } |
| 559 | 561 |
| 560 int FaviconHandler::GetMaximalIconSize(favicon_base::IconType icon_type) { | 562 int FaviconHandler::GetMaximalIconSize(favicon_base::IconType icon_type) { |
| 561 switch (icon_type) { | 563 switch (icon_type) { |
| 562 case favicon_base::FAVICON: | 564 case favicon_base::FAVICON: |
| 563 #if defined(OS_ANDROID) | 565 #if defined(OS_ANDROID) |
| 564 return 192; | 566 return 192; |
| 565 #else | 567 #else |
| 566 return gfx::ImageSkia::GetMaxSupportedScale() * gfx::kFaviconSize; | 568 return gfx::ImageSkia::GetMaxSupportedScale() * gfx::kFaviconSize; |
| 567 #endif | 569 #endif |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 605 GURL icon_url = current_candidate()->icon_url; | 607 GURL icon_url = current_candidate()->icon_url; |
| 606 favicon_base::IconType icon_type = current_candidate()->icon_type; | 608 favicon_base::IconType icon_type = current_candidate()->icon_type; |
| 607 | 609 |
| 608 if (redownload_icons_) { | 610 if (redownload_icons_) { |
| 609 // We have the mapping, but the favicon is out of date. Download it now. | 611 // We have the mapping, but the favicon is out of date. Download it now. |
| 610 ScheduleDownload(icon_url, icon_type); | 612 ScheduleDownload(icon_url, icon_type); |
| 611 } else { | 613 } else { |
| 612 // We don't know the favicon, but we may have previously downloaded the | 614 // 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 | 615 // favicon for another page that shares the same favicon. Ask for the |
| 614 // favicon given the favicon URL. | 616 // favicon given the favicon URL. |
| 615 if (driver_->IsOffTheRecord()) { | 617 if (delegate_->IsOffTheRecord()) { |
| 616 GetFaviconFromFaviconService( | 618 GetFaviconFromFaviconService( |
| 617 icon_url, icon_type, | 619 icon_url, icon_type, |
| 618 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)), | 620 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)), |
| 619 &cancelable_task_tracker_); | 621 &cancelable_task_tracker_); |
| 620 } else { | 622 } else { |
| 621 // Ask the history service for the icon. This does two things: | 623 // Ask the history service for the icon. This does two things: |
| 622 // 1. Attempts to fetch the favicon data from the database. | 624 // 1. Attempts to fetch the favicon data from the database. |
| 623 // 2. If the favicon exists in the database, this updates the database to | 625 // 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. | 626 // include the mapping between the page url and the favicon url. |
| 625 // This is asynchronous. The history service will call back when done. | 627 // This is asynchronous. The history service will call back when done. |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 655 } | 657 } |
| 656 | 658 |
| 657 if (has_expired_or_incomplete_result) { | 659 if (has_expired_or_incomplete_result) { |
| 658 ScheduleDownload(current_candidate()->icon_url, | 660 ScheduleDownload(current_candidate()->icon_url, |
| 659 current_candidate()->icon_type); | 661 current_candidate()->icon_type); |
| 660 } | 662 } |
| 661 } | 663 } |
| 662 | 664 |
| 663 void FaviconHandler::ScheduleDownload(const GURL& image_url, | 665 void FaviconHandler::ScheduleDownload(const GURL& image_url, |
| 664 favicon_base::IconType icon_type) { | 666 favicon_base::IconType icon_type) { |
| 667 if (!image_url.is_valid()) { | |
| 668 NOTREACHED(); | |
| 669 return; | |
| 670 } | |
| 665 // A max bitmap size is specified to avoid receiving huge bitmaps in | 671 // A max bitmap size is specified to avoid receiving huge bitmaps in |
| 666 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() | 672 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() |
| 667 // for more details about the max bitmap size. | 673 // for more details about the max bitmap size. |
| 668 const int download_id = DownloadFavicon(image_url, | 674 const int download_id = |
| 669 GetMaximalIconSize(icon_type)); | 675 delegate_->DownloadImage(image_url, GetMaximalIconSize(icon_type), |
| 676 base::Bind(&FaviconHandler::DidDownloadFavicon, | |
| 677 weak_ptr_factory_.GetWeakPtr())); | |
| 670 | 678 |
| 671 // Download ids should be unique. | 679 // Download ids should be unique. |
| 672 DCHECK(download_requests_.find(download_id) == download_requests_.end()); | 680 DCHECK(download_requests_.find(download_id) == download_requests_.end()); |
| 673 download_requests_[download_id] = DownloadRequest(image_url, icon_type); | 681 download_requests_[download_id] = DownloadRequest(image_url, icon_type); |
| 674 | 682 |
| 675 if (download_id == 0) { | 683 if (download_id == 0) { |
| 676 // If DownloadFavicon() did not start a download, it returns a download id | 684 // 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 | 685 // of 0. We still need to call OnDidDownloadFavicon() because the method is |
| 678 // responsible for initiating the data request for the next candidate. | 686 // responsible for initiating the data request for the next candidate. |
| 679 OnDidDownloadFavicon(download_id, image_url, std::vector<SkBitmap>(), | 687 DidDownloadFavicon(download_id, 0, image_url, std::vector<SkBitmap>(), |
| 680 std::vector<gfx::Size>()); | 688 std::vector<gfx::Size>()); |
| 681 | |
| 682 } | 689 } |
| 683 } | 690 } |
| 684 | 691 |
| 685 } // namespace favicon | 692 } // namespace favicon |
| OLD | NEW |