| 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 |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 download_requests_.clear(); | 255 download_requests_.clear(); |
| 256 image_urls_.clear(); | 256 image_urls_.clear(); |
| 257 notification_icon_url_ = GURL(); | 257 notification_icon_url_ = GURL(); |
| 258 notification_icon_type_ = favicon_base::INVALID_ICON; | 258 notification_icon_type_ = favicon_base::INVALID_ICON; |
| 259 current_candidate_index_ = 0u; | 259 current_candidate_index_ = 0u; |
| 260 best_favicon_candidate_ = FaviconCandidate(); | 260 best_favicon_candidate_ = FaviconCandidate(); |
| 261 | 261 |
| 262 // Request the favicon from the history service. In parallel to this the | 262 // Request the favicon from the history service. In parallel to this the |
| 263 // renderer is going to notify us (well WebContents) when the favicon url is | 263 // renderer is going to notify us (well WebContents) when the favicon url is |
| 264 // available. | 264 // available. |
| 265 GetFaviconForURLFromFaviconService( | 265 if (service_) { |
| 266 url_, icon_types_, | 266 service_->GetFaviconForPageURL( |
| 267 base::Bind(&FaviconHandler::OnFaviconDataForInitialURLFromFaviconService, | 267 url_, icon_types_, preferred_icon_size(), |
| 268 base::Unretained(this)), | 268 base::Bind( |
| 269 &cancelable_task_tracker_); | 269 &FaviconHandler::OnFaviconDataForInitialURLFromFaviconService, |
| 270 base::Unretained(this)), |
| 271 &cancelable_task_tracker_); |
| 272 } |
| 270 } | 273 } |
| 271 | 274 |
| 272 bool FaviconHandler::UpdateFaviconCandidate(const GURL& image_url, | 275 bool FaviconHandler::UpdateFaviconCandidate(const GURL& image_url, |
| 273 const gfx::Image& image, | 276 const gfx::Image& image, |
| 274 float score, | 277 float score, |
| 275 favicon_base::IconType icon_type) { | 278 favicon_base::IconType icon_type) { |
| 276 bool replace_best_favicon_candidate = false; | 279 bool replace_best_favicon_candidate = false; |
| 277 bool exact_match = false; | 280 bool exact_match = false; |
| 278 if (download_largest_icon_) { | 281 if (download_largest_icon_) { |
| 279 replace_best_favicon_candidate = | 282 replace_best_favicon_candidate = |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 if (replace_best_favicon_candidate) { | 315 if (replace_best_favicon_candidate) { |
| 313 best_favicon_candidate_ = | 316 best_favicon_candidate_ = |
| 314 FaviconCandidate(image_url, image, score, icon_type); | 317 FaviconCandidate(image_url, image, score, icon_type); |
| 315 } | 318 } |
| 316 return exact_match; | 319 return exact_match; |
| 317 } | 320 } |
| 318 | 321 |
| 319 void FaviconHandler::SetFavicon(const GURL& icon_url, | 322 void FaviconHandler::SetFavicon(const GURL& icon_url, |
| 320 const gfx::Image& image, | 323 const gfx::Image& image, |
| 321 favicon_base::IconType icon_type) { | 324 favicon_base::IconType icon_type) { |
| 322 if (ShouldSaveFavicon()) | 325 if (service_ && ShouldSaveFavicon()) |
| 323 SetHistoryFavicons(url_, icon_url, icon_type, image); | 326 service_->SetFavicons(url_, icon_url, icon_type, image); |
| 324 | 327 |
| 325 NotifyFaviconUpdated(icon_url, icon_type, image); | 328 NotifyFaviconUpdated(icon_url, icon_type, image); |
| 326 } | 329 } |
| 327 | 330 |
| 328 void FaviconHandler::NotifyFaviconUpdated( | 331 void FaviconHandler::NotifyFaviconUpdated( |
| 329 const std::vector<favicon_base::FaviconRawBitmapResult>& | 332 const std::vector<favicon_base::FaviconRawBitmapResult>& |
| 330 favicon_bitmap_results) { | 333 favicon_bitmap_results) { |
| 331 if (favicon_bitmap_results.empty()) | 334 if (favicon_bitmap_results.empty()) |
| 332 return; | 335 return; |
| 333 | 336 |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 current_candidate_index_ = image_urls_.size(); | 494 current_candidate_index_ = image_urls_.size(); |
| 492 best_favicon_candidate_ = FaviconCandidate(); | 495 best_favicon_candidate_ = FaviconCandidate(); |
| 493 } | 496 } |
| 494 } | 497 } |
| 495 | 498 |
| 496 bool FaviconHandler::HasPendingTasksForTest() { | 499 bool FaviconHandler::HasPendingTasksForTest() { |
| 497 return !download_requests_.empty() || | 500 return !download_requests_.empty() || |
| 498 cancelable_task_tracker_.HasTrackedTasks(); | 501 cancelable_task_tracker_.HasTrackedTasks(); |
| 499 } | 502 } |
| 500 | 503 |
| 501 void FaviconHandler::UpdateFaviconMappingAndFetch( | |
| 502 const GURL& page_url, | |
| 503 const GURL& icon_url, | |
| 504 favicon_base::IconType icon_type, | |
| 505 const favicon_base::FaviconResultsCallback& callback, | |
| 506 base::CancelableTaskTracker* tracker) { | |
| 507 // TODO(pkotwicz): pass in all of |image_urls_| to | |
| 508 // UpdateFaviconMappingsAndFetch(). | |
| 509 if (service_) { | |
| 510 std::vector<GURL> icon_urls; | |
| 511 icon_urls.push_back(icon_url); | |
| 512 service_->UpdateFaviconMappingsAndFetch(page_url, icon_urls, icon_type, | |
| 513 preferred_icon_size(), callback, | |
| 514 tracker); | |
| 515 } | |
| 516 } | |
| 517 | |
| 518 void FaviconHandler::GetFaviconFromFaviconService( | |
| 519 const GURL& icon_url, | |
| 520 favicon_base::IconType icon_type, | |
| 521 const favicon_base::FaviconResultsCallback& callback, | |
| 522 base::CancelableTaskTracker* tracker) { | |
| 523 if (service_) { | |
| 524 service_->GetFavicon(icon_url, icon_type, preferred_icon_size(), callback, | |
| 525 tracker); | |
| 526 } | |
| 527 } | |
| 528 | |
| 529 void FaviconHandler::GetFaviconForURLFromFaviconService( | |
| 530 const GURL& page_url, | |
| 531 int icon_types, | |
| 532 const favicon_base::FaviconResultsCallback& callback, | |
| 533 base::CancelableTaskTracker* tracker) { | |
| 534 if (service_) { | |
| 535 service_->GetFaviconForPageURL(page_url, icon_types, preferred_icon_size(), | |
| 536 callback, tracker); | |
| 537 } | |
| 538 } | |
| 539 | |
| 540 void FaviconHandler::SetHistoryFavicons(const GURL& page_url, | |
| 541 const GURL& icon_url, | |
| 542 favicon_base::IconType icon_type, | |
| 543 const gfx::Image& image) { | |
| 544 if (service_) { | |
| 545 service_->SetFavicons(page_url, icon_url, icon_type, image); | |
| 546 } | |
| 547 } | |
| 548 | |
| 549 bool FaviconHandler::ShouldSaveFavicon() { | 504 bool FaviconHandler::ShouldSaveFavicon() { |
| 550 if (!delegate_->IsOffTheRecord()) | 505 if (!delegate_->IsOffTheRecord()) |
| 551 return true; | 506 return true; |
| 552 | 507 |
| 553 // Always save favicon if the page is bookmarked. | 508 // Always save favicon if the page is bookmarked. |
| 554 return delegate_->IsBookmarked(url_); | 509 return delegate_->IsBookmarked(url_); |
| 555 } | 510 } |
| 556 | 511 |
| 557 int FaviconHandler::GetMaximalIconSize(favicon_base::IconType icon_type) { | 512 int FaviconHandler::GetMaximalIconSize(favicon_base::IconType icon_type) { |
| 558 switch (icon_type) { | 513 switch (icon_type) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 598 OnGotInitialHistoryDataAndIconURLCandidates(); | 553 OnGotInitialHistoryDataAndIconURLCandidates(); |
| 599 } | 554 } |
| 600 | 555 |
| 601 void FaviconHandler::DownloadCurrentCandidateOrAskFaviconService() { | 556 void FaviconHandler::DownloadCurrentCandidateOrAskFaviconService() { |
| 602 GURL icon_url = current_candidate()->icon_url; | 557 GURL icon_url = current_candidate()->icon_url; |
| 603 favicon_base::IconType icon_type = current_candidate()->icon_type; | 558 favicon_base::IconType icon_type = current_candidate()->icon_type; |
| 604 | 559 |
| 605 if (redownload_icons_) { | 560 if (redownload_icons_) { |
| 606 // We have the mapping, but the favicon is out of date. Download it now. | 561 // We have the mapping, but the favicon is out of date. Download it now. |
| 607 ScheduleDownload(icon_url, icon_type); | 562 ScheduleDownload(icon_url, icon_type); |
| 608 } else { | 563 } else if (service_) { |
| 609 // We don't know the favicon, but we may have previously downloaded the | 564 // We don't know the favicon, but we may have previously downloaded the |
| 610 // favicon for another page that shares the same favicon. Ask for the | 565 // favicon for another page that shares the same favicon. Ask for the |
| 611 // favicon given the favicon URL. | 566 // favicon given the favicon URL. |
| 612 if (delegate_->IsOffTheRecord()) { | 567 if (delegate_->IsOffTheRecord()) { |
| 613 GetFaviconFromFaviconService( | 568 service_->GetFavicon( |
| 614 icon_url, icon_type, | 569 icon_url, icon_type, preferred_icon_size(), |
| 615 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)), | 570 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)), |
| 616 &cancelable_task_tracker_); | 571 &cancelable_task_tracker_); |
| 617 } else { | 572 } else { |
| 618 // Ask the history service for the icon. This does two things: | 573 // Ask the history service for the icon. This does two things: |
| 619 // 1. Attempts to fetch the favicon data from the database. | 574 // 1. Attempts to fetch the favicon data from the database. |
| 620 // 2. If the favicon exists in the database, this updates the database to | 575 // 2. If the favicon exists in the database, this updates the database to |
| 621 // include the mapping between the page url and the favicon url. | 576 // include the mapping between the page url and the favicon url. |
| 622 // This is asynchronous. The history service will call back when done. | 577 // This is asynchronous. The history service will call back when done. |
| 623 UpdateFaviconMappingAndFetch( | 578 // TODO(pkotwicz): pass in all of |image_urls_| to |
| 624 url_, icon_url, icon_type, | 579 // UpdateFaviconMappingsAndFetch(). |
| 580 service_->UpdateFaviconMappingsAndFetch( |
| 581 url_, {icon_url}, icon_type, preferred_icon_size(), |
| 625 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)), | 582 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)), |
| 626 &cancelable_task_tracker_); | 583 &cancelable_task_tracker_); |
| 627 } | 584 } |
| 628 } | 585 } |
| 629 } | 586 } |
| 630 | 587 |
| 631 void FaviconHandler::OnFaviconData(const std::vector< | 588 void FaviconHandler::OnFaviconData(const std::vector< |
| 632 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) { | 589 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) { |
| 633 bool has_results = !favicon_bitmap_results.empty(); | 590 bool has_results = !favicon_bitmap_results.empty(); |
| 634 bool has_valid_result = HasValidResult(favicon_bitmap_results); | 591 bool has_valid_result = HasValidResult(favicon_bitmap_results); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 678 if (download_id == 0) { | 635 if (download_id == 0) { |
| 679 // If DownloadFavicon() did not start a download, it returns a download id | 636 // If DownloadFavicon() did not start a download, it returns a download id |
| 680 // of 0. We still need to call OnDidDownloadFavicon() because the method is | 637 // of 0. We still need to call OnDidDownloadFavicon() because the method is |
| 681 // responsible for initiating the data request for the next candidate. | 638 // responsible for initiating the data request for the next candidate. |
| 682 DidDownloadFavicon(download_id, 0, image_url, std::vector<SkBitmap>(), | 639 DidDownloadFavicon(download_id, 0, image_url, std::vector<SkBitmap>(), |
| 683 std::vector<gfx::Size>()); | 640 std::vector<gfx::Size>()); |
| 684 } | 641 } |
| 685 } | 642 } |
| 686 | 643 |
| 687 } // namespace favicon | 644 } // namespace favicon |
| OLD | NEW |