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 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 return; | 370 return; |
371 } | 371 } |
372 | 372 |
373 download_requests_.clear(); | 373 download_requests_.clear(); |
374 image_urls_ = pruned_candidates; | 374 image_urls_ = pruned_candidates; |
375 current_candidate_index_ = 0u; | 375 current_candidate_index_ = 0u; |
376 best_favicon_candidate_ = FaviconCandidate(); | 376 best_favicon_candidate_ = FaviconCandidate(); |
377 | 377 |
378 // TODO(davemoore) Should clear on empty url. Currently we ignore it. | 378 // TODO(davemoore) Should clear on empty url. Currently we ignore it. |
379 // This appears to be what FF does as well. | 379 // This appears to be what FF does as well. |
380 if (current_candidate()) | 380 if (current_candidate() && got_favicon_from_history_) |
381 ProcessCurrentUrl(); | 381 OnGotInitialHistoryDataAndIconURLCandidates(); |
382 } | 382 } |
383 | 383 |
384 void FaviconHandler::ProcessCurrentUrl() { | 384 void FaviconHandler::OnGotInitialHistoryDataAndIconURLCandidates() { |
385 DCHECK(!image_urls_.empty()); | 385 if (!favicon_expired_or_incomplete_ && HasValidResult(history_results_) && |
386 | |
387 if (!favicon_expired_or_incomplete_ && got_favicon_from_history_ && | |
388 HasValidResult(history_results_) && | |
389 DoUrlsAndIconsMatch(*current_candidate(), history_results_)) { | 386 DoUrlsAndIconsMatch(*current_candidate(), history_results_)) { |
| 387 // The data from history is valid and not expired. The icon URL of the |
| 388 // history data matches one of the page's icon URLs. We are done. No |
| 389 // additional downloads or history requests are needed. |
| 390 // TODO: Store all of the icon URLs associated with a page in history so |
| 391 // that we can check whether the page's icon URLs match the page's icon URLs |
| 392 // at the time that the favicon data was stored to the history database. |
390 return; | 393 return; |
391 } | 394 } |
392 | 395 |
393 if (got_favicon_from_history_) | 396 DownloadCurrentCandidateOrAskFaviconService(); |
394 DownloadFaviconOrAskFaviconService(current_candidate()->icon_url, | |
395 current_candidate()->icon_type); | |
396 } | 397 } |
397 | 398 |
398 void FaviconHandler::OnDidDownloadFavicon( | 399 void FaviconHandler::OnDidDownloadFavicon( |
399 int id, | 400 int id, |
400 const GURL& image_url, | 401 const GURL& image_url, |
401 const std::vector<SkBitmap>& bitmaps, | 402 const std::vector<SkBitmap>& bitmaps, |
402 const std::vector<gfx::Size>& original_bitmap_sizes) { | 403 const std::vector<gfx::Size>& original_bitmap_sizes) { |
403 DownloadRequests::iterator i = download_requests_.find(id); | 404 DownloadRequests::iterator i = download_requests_.find(id); |
404 if (i == download_requests_.end()) { | 405 if (i == download_requests_.end()) { |
405 // Currently WebContents notifies us of ANY downloads so that it is | 406 // Currently WebContents notifies us of ANY downloads so that it is |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 // The downloaded icon is still valid when there is no FaviconURL update | 447 // The downloaded icon is still valid when there is no FaviconURL update |
447 // during the downloading. | 448 // during the downloading. |
448 request_next_icon = !UpdateFaviconCandidate(image_url, image, score, | 449 request_next_icon = !UpdateFaviconCandidate(image_url, image, score, |
449 download_request.icon_type); | 450 download_request.icon_type); |
450 } | 451 } |
451 } | 452 } |
452 | 453 |
453 if (request_next_icon && current_candidate_index_ + 1 < image_urls_.size()) { | 454 if (request_next_icon && current_candidate_index_ + 1 < image_urls_.size()) { |
454 // Process the next candidate. | 455 // Process the next candidate. |
455 ++current_candidate_index_; | 456 ++current_candidate_index_; |
456 ProcessCurrentUrl(); | 457 DownloadCurrentCandidateOrAskFaviconService(); |
457 } else { | 458 } else { |
458 // We have either found the ideal candidate or run out of candidates. | 459 // We have either found the ideal candidate or run out of candidates. |
459 if (best_favicon_candidate_.icon_type != favicon_base::INVALID_ICON) { | 460 if (best_favicon_candidate_.icon_type != favicon_base::INVALID_ICON) { |
460 // No more icons to request, set the favicon from the candidate. | 461 // No more icons to request, set the favicon from the candidate. |
461 SetFavicon(best_favicon_candidate_.image_url, | 462 SetFavicon(best_favicon_candidate_.image_url, |
462 best_favicon_candidate_.image, | 463 best_favicon_candidate_.image, |
463 best_favicon_candidate_.icon_type); | 464 best_favicon_candidate_.icon_type); |
464 } | 465 } |
465 // Clear download related state. | 466 // Clear download related state. |
466 download_requests_.clear(); | 467 download_requests_.clear(); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
577 // to be expired (or the wrong url) we'll fetch later on. This way the | 578 // to be expired (or the wrong url) we'll fetch later on. This way the |
578 // user doesn't see a flash of the default favicon. | 579 // user doesn't see a flash of the default favicon. |
579 NotifyFaviconAvailable(favicon_bitmap_results); | 580 NotifyFaviconAvailable(favicon_bitmap_results); |
580 } else { | 581 } else { |
581 // If |favicon_bitmap_results| does not have any valid results, treat the | 582 // If |favicon_bitmap_results| does not have any valid results, treat the |
582 // favicon as if it's expired. | 583 // favicon as if it's expired. |
583 // TODO(pkotwicz): Do something better. | 584 // TODO(pkotwicz): Do something better. |
584 favicon_expired_or_incomplete_ = true; | 585 favicon_expired_or_incomplete_ = true; |
585 } | 586 } |
586 } | 587 } |
587 if (has_results && !favicon_expired_or_incomplete_) { | |
588 if (current_candidate() && | |
589 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)) { | |
590 // Mapping in the database is wrong. DownloadFavIconOrAskHistory will | |
591 // update the mapping for this url and download the favicon if we don't | |
592 // already have it. | |
593 DownloadFaviconOrAskFaviconService(current_candidate()->icon_url, | |
594 current_candidate()->icon_type); | |
595 } | |
596 } else if (current_candidate()) { | |
597 // We know the official url for the favicon, but either don't have the | |
598 // favicon or it's expired. Continue on to DownloadFaviconOrAskHistory to | |
599 // either download or check history again. | |
600 DownloadFaviconOrAskFaviconService(current_candidate()->icon_url, | |
601 current_candidate()->icon_type); | |
602 } | |
603 // else we haven't got the icon url. When we get it we'll ask the | |
604 // renderer to download the icon. | |
605 | 588 |
606 if (has_valid_result && (handler_type_ != FAVICON || download_largest_icon_)) | 589 if (has_valid_result && (handler_type_ != FAVICON || download_largest_icon_)) |
607 NotifyFaviconAvailable(favicon_bitmap_results); | 590 NotifyFaviconAvailable(favicon_bitmap_results); |
| 591 |
| 592 if (current_candidate()) |
| 593 OnGotInitialHistoryDataAndIconURLCandidates(); |
608 } | 594 } |
609 | 595 |
610 void FaviconHandler::DownloadFaviconOrAskFaviconService( | 596 void FaviconHandler::DownloadCurrentCandidateOrAskFaviconService() { |
611 const GURL& icon_url, | 597 GURL icon_url = current_candidate()->icon_url; |
612 favicon_base::IconType icon_type) { | 598 favicon_base::IconType icon_type = current_candidate()->icon_type; |
| 599 |
613 if (favicon_expired_or_incomplete_) { | 600 if (favicon_expired_or_incomplete_) { |
614 // We have the mapping, but the favicon is out of date. Download it now. | 601 // We have the mapping, but the favicon is out of date. Download it now. |
615 ScheduleDownload(icon_url, icon_type); | 602 ScheduleDownload(icon_url, icon_type); |
616 } else { | 603 } else { |
617 // We don't know the favicon, but we may have previously downloaded the | 604 // We don't know the favicon, but we may have previously downloaded the |
618 // favicon for another page that shares the same favicon. Ask for the | 605 // favicon for another page that shares the same favicon. Ask for the |
619 // favicon given the favicon URL. | 606 // favicon given the favicon URL. |
620 if (driver_->IsOffTheRecord()) { | 607 if (driver_->IsOffTheRecord()) { |
621 GetFaviconFromFaviconService( | 608 GetFaviconFromFaviconService( |
622 icon_url, icon_type, | 609 icon_url, icon_type, |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
681 // If DownloadFavicon() did not start a download, it returns a download id | 668 // If DownloadFavicon() did not start a download, it returns a download id |
682 // of 0. We still need to call OnDidDownloadFavicon() because the method is | 669 // of 0. We still need to call OnDidDownloadFavicon() because the method is |
683 // responsible for initiating the data request for the next candidate. | 670 // responsible for initiating the data request for the next candidate. |
684 OnDidDownloadFavicon(download_id, image_url, std::vector<SkBitmap>(), | 671 OnDidDownloadFavicon(download_id, image_url, std::vector<SkBitmap>(), |
685 std::vector<gfx::Size>()); | 672 std::vector<gfx::Size>()); |
686 | 673 |
687 } | 674 } |
688 } | 675 } |
689 | 676 |
690 } // namespace favicon | 677 } // namespace favicon |
OLD | NEW |