| 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 <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
| 14 #include "base/memory/ref_counted_memory.h" | 14 #include "base/memory/ref_counted_memory.h" |
| 15 #include "base/metrics/histogram_macros.h" |
| 15 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 16 #include "components/favicon/core/favicon_service.h" | 17 #include "components/favicon/core/favicon_service.h" |
| 17 #include "components/favicon_base/favicon_util.h" | 18 #include "components/favicon_base/favicon_util.h" |
| 18 #include "components/favicon_base/select_favicon_frames.h" | 19 #include "components/favicon_base/select_favicon_frames.h" |
| 19 #include "skia/ext/image_operations.h" | 20 #include "skia/ext/image_operations.h" |
| 20 #include "ui/gfx/codec/png_codec.h" | 21 #include "ui/gfx/codec/png_codec.h" |
| 21 #include "ui/gfx/image/image_skia.h" | 22 #include "ui/gfx/image/image_skia.h" |
| 22 #include "ui/gfx/image/image_util.h" | 23 #include "ui/gfx/image/image_util.h" |
| 23 | 24 |
| 24 namespace favicon { | 25 namespace favicon { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 52 // Return true if |bitmap_result| is expired. | 53 // Return true if |bitmap_result| is expired. |
| 53 bool IsExpired(const favicon_base::FaviconRawBitmapResult& bitmap_result) { | 54 bool IsExpired(const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
| 54 return bitmap_result.expired; | 55 return bitmap_result.expired; |
| 55 } | 56 } |
| 56 | 57 |
| 57 // Return true if |bitmap_result| is valid. | 58 // Return true if |bitmap_result| is valid. |
| 58 bool IsValid(const favicon_base::FaviconRawBitmapResult& bitmap_result) { | 59 bool IsValid(const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
| 59 return bitmap_result.is_valid(); | 60 return bitmap_result.is_valid(); |
| 60 } | 61 } |
| 61 | 62 |
| 63 void RecordDownloadAttemptsForHandlerType( |
| 64 FaviconDriverObserver::NotificationIconType handler_type, |
| 65 int attempts) { |
| 66 // If not at least one attempts was recorded or more than 15 attempts were |
| 67 // registered, something went wrong. Underflows are stored in bucket 0 and |
| 68 // overflows in bucket 16. |
| 69 attempts = std::max(0, std::min(attempts, 16)); |
| 70 switch (handler_type) { |
| 71 case FaviconDriverObserver::NON_TOUCH_16_DIP: |
| 72 UMA_HISTOGRAM_SPARSE_SLOWLY("Favicons.DownloadAttempts.Favicons", |
| 73 attempts); |
| 74 return; |
| 75 case FaviconDriverObserver::NON_TOUCH_LARGEST: |
| 76 UMA_HISTOGRAM_SPARSE_SLOWLY("Favicons.DownloadAttempts.LargeIcons", |
| 77 attempts); |
| 78 return; |
| 79 case FaviconDriverObserver::TOUCH_LARGEST: |
| 80 UMA_HISTOGRAM_SPARSE_SLOWLY("Favicons.DownloadAttempts.TouchIcons", |
| 81 attempts); |
| 82 return; |
| 83 } |
| 84 NOTREACHED(); |
| 85 } |
| 86 |
| 62 // Returns true if |bitmap_results| is non-empty and: | 87 // Returns true if |bitmap_results| is non-empty and: |
| 63 // - At least one of the bitmaps in |bitmap_results| is expired | 88 // - At least one of the bitmaps in |bitmap_results| is expired |
| 64 // OR | 89 // OR |
| 65 // - |bitmap_results| is missing favicons for |desired_size_in_dip| and one of | 90 // - |bitmap_results| is missing favicons for |desired_size_in_dip| and one of |
| 66 // the scale factors in favicon_base::GetFaviconScales(). | 91 // the scale factors in favicon_base::GetFaviconScales(). |
| 67 bool HasExpiredOrIncompleteResult( | 92 bool HasExpiredOrIncompleteResult( |
| 68 int desired_size_in_dip, | 93 int desired_size_in_dip, |
| 69 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) { | 94 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) { |
| 70 if (bitmap_results.empty()) | 95 if (bitmap_results.empty()) |
| 71 return false; | 96 return false; |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 got_favicon_from_history_(false), | 185 got_favicon_from_history_(false), |
| 161 initial_history_result_expired_or_incomplete_(false), | 186 initial_history_result_expired_or_incomplete_(false), |
| 162 redownload_icons_(false), | 187 redownload_icons_(false), |
| 163 icon_types_(FaviconHandler::GetIconTypesFromHandlerType(handler_type)), | 188 icon_types_(FaviconHandler::GetIconTypesFromHandlerType(handler_type)), |
| 164 download_largest_icon_( | 189 download_largest_icon_( |
| 165 handler_type == FaviconDriverObserver::NON_TOUCH_LARGEST || | 190 handler_type == FaviconDriverObserver::NON_TOUCH_LARGEST || |
| 166 handler_type == FaviconDriverObserver::TOUCH_LARGEST), | 191 handler_type == FaviconDriverObserver::TOUCH_LARGEST), |
| 167 notification_icon_type_(favicon_base::INVALID_ICON), | 192 notification_icon_type_(favicon_base::INVALID_ICON), |
| 168 service_(service), | 193 service_(service), |
| 169 delegate_(delegate), | 194 delegate_(delegate), |
| 195 num_download_requests_(0), |
| 170 current_candidate_index_(0u) { | 196 current_candidate_index_(0u) { |
| 171 DCHECK(delegate_); | 197 DCHECK(delegate_); |
| 172 } | 198 } |
| 173 | 199 |
| 174 FaviconHandler::~FaviconHandler() { | 200 FaviconHandler::~FaviconHandler() { |
| 175 } | 201 } |
| 176 | 202 |
| 177 // static | 203 // static |
| 178 int FaviconHandler::GetIconTypesFromHandlerType( | 204 int FaviconHandler::GetIconTypesFromHandlerType( |
| 179 FaviconDriverObserver::NotificationIconType handler_type) { | 205 FaviconDriverObserver::NotificationIconType handler_type) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 192 | 218 |
| 193 url_ = url; | 219 url_ = url; |
| 194 | 220 |
| 195 initial_history_result_expired_or_incomplete_ = false; | 221 initial_history_result_expired_or_incomplete_ = false; |
| 196 redownload_icons_ = false; | 222 redownload_icons_ = false; |
| 197 got_favicon_from_history_ = false; | 223 got_favicon_from_history_ = false; |
| 198 download_request_.Cancel(); | 224 download_request_.Cancel(); |
| 199 candidates_.clear(); | 225 candidates_.clear(); |
| 200 notification_icon_url_ = GURL(); | 226 notification_icon_url_ = GURL(); |
| 201 notification_icon_type_ = favicon_base::INVALID_ICON; | 227 notification_icon_type_ = favicon_base::INVALID_ICON; |
| 228 num_download_requests_ = 0; |
| 202 current_candidate_index_ = 0u; | 229 current_candidate_index_ = 0u; |
| 203 best_favicon_ = DownloadedFavicon(); | 230 best_favicon_ = DownloadedFavicon(); |
| 204 | 231 |
| 205 // Request the favicon from the history service. In parallel to this the | 232 // Request the favicon from the history service. In parallel to this the |
| 206 // renderer is going to notify us (well WebContents) when the favicon url is | 233 // renderer is going to notify us (well WebContents) when the favicon url is |
| 207 // available. | 234 // available. |
| 208 service_->GetFaviconForPageURL( | 235 service_->GetFaviconForPageURL( |
| 209 url_, icon_types_, preferred_icon_size(), | 236 url_, icon_types_, preferred_icon_size(), |
| 210 base::Bind(&FaviconHandler::OnFaviconDataForInitialURLFromFaviconService, | 237 base::Bind(&FaviconHandler::OnFaviconDataForInitialURLFromFaviconService, |
| 211 base::Unretained(this)), | 238 base::Unretained(this)), |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 &FaviconCandidate::CompareScore); | 328 &FaviconCandidate::CompareScore); |
| 302 | 329 |
| 303 if (candidates_.size() == sorted_candidates.size() && | 330 if (candidates_.size() == sorted_candidates.size() && |
| 304 std::equal(sorted_candidates.begin(), sorted_candidates.end(), | 331 std::equal(sorted_candidates.begin(), sorted_candidates.end(), |
| 305 candidates_.begin())) { | 332 candidates_.begin())) { |
| 306 return; | 333 return; |
| 307 } | 334 } |
| 308 | 335 |
| 309 download_request_.Cancel(); | 336 download_request_.Cancel(); |
| 310 candidates_ = std::move(sorted_candidates); | 337 candidates_ = std::move(sorted_candidates); |
| 338 num_download_requests_ = 0; |
| 311 current_candidate_index_ = 0u; | 339 current_candidate_index_ = 0u; |
| 312 best_favicon_ = DownloadedFavicon(); | 340 best_favicon_ = DownloadedFavicon(); |
| 313 | 341 |
| 314 // TODO(davemoore) Should clear on empty url. Currently we ignore it. | 342 // TODO(davemoore) Should clear on empty url. Currently we ignore it. |
| 315 // This appears to be what FF does as well. | 343 // This appears to be what FF does as well. |
| 316 if (current_candidate() && got_favicon_from_history_) | 344 if (current_candidate() && got_favicon_from_history_) |
| 317 OnGotInitialHistoryDataAndIconURLCandidates(); | 345 OnGotInitialHistoryDataAndIconURLCandidates(); |
| 318 } | 346 } |
| 319 | 347 |
| 320 // static | 348 // static |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 downloaded_favicon.candidate.score = score; | 416 downloaded_favicon.candidate.score = score; |
| 389 request_next_icon = !UpdateFaviconCandidate(downloaded_favicon); | 417 request_next_icon = !UpdateFaviconCandidate(downloaded_favicon); |
| 390 } | 418 } |
| 391 } | 419 } |
| 392 | 420 |
| 393 if (request_next_icon && current_candidate_index_ + 1 < candidates_.size()) { | 421 if (request_next_icon && current_candidate_index_ + 1 < candidates_.size()) { |
| 394 // Process the next candidate. | 422 // Process the next candidate. |
| 395 ++current_candidate_index_; | 423 ++current_candidate_index_; |
| 396 DownloadCurrentCandidateOrAskFaviconService(); | 424 DownloadCurrentCandidateOrAskFaviconService(); |
| 397 } else { | 425 } else { |
| 426 // OnDidDownloadFavicon() can only be called after requesting a download, so |
| 427 // |num_download_requests_| can never be 0. |
| 428 RecordDownloadAttemptsForHandlerType(handler_type_, num_download_requests_); |
| 398 // We have either found the ideal candidate or run out of candidates. | 429 // We have either found the ideal candidate or run out of candidates. |
| 399 if (best_favicon_.candidate.icon_type != favicon_base::INVALID_ICON) { | 430 if (best_favicon_.candidate.icon_type != favicon_base::INVALID_ICON) { |
| 400 // No more icons to request, set the favicon from the candidate. | 431 // No more icons to request, set the favicon from the candidate. |
| 401 SetFavicon(best_favicon_.candidate.icon_url, best_favicon_.image, | 432 SetFavicon(best_favicon_.candidate.icon_url, best_favicon_.image, |
| 402 best_favicon_.candidate.icon_type); | 433 best_favicon_.candidate.icon_type); |
| 403 } | 434 } |
| 404 // Clear download related state. | 435 // Clear download related state. |
| 405 current_candidate_index_ = candidates_.size(); | 436 current_candidate_index_ = candidates_.size(); |
| 437 num_download_requests_ = 0; |
| 406 best_favicon_ = DownloadedFavicon(); | 438 best_favicon_ = DownloadedFavicon(); |
| 407 } | 439 } |
| 408 } | 440 } |
| 409 | 441 |
| 410 const std::vector<GURL> FaviconHandler::GetIconURLs() const { | 442 const std::vector<GURL> FaviconHandler::GetIconURLs() const { |
| 411 std::vector<GURL> icon_urls; | 443 std::vector<GURL> icon_urls; |
| 412 for (const FaviconCandidate& candidate : candidates_) | 444 for (const FaviconCandidate& candidate : candidates_) |
| 413 icon_urls.push_back(candidate.icon_url); | 445 icon_urls.push_back(candidate.icon_url); |
| 414 return icon_urls; | 446 return icon_urls; |
| 415 } | 447 } |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 (has_results && !DoUrlsAndIconsMatch(current_candidate()->icon_url, | 538 (has_results && !DoUrlsAndIconsMatch(current_candidate()->icon_url, |
| 507 current_candidate()->icon_type, | 539 current_candidate()->icon_type, |
| 508 favicon_bitmap_results))) { | 540 favicon_bitmap_results))) { |
| 509 // The icon URLs have been updated since the favicon data was requested. | 541 // The icon URLs have been updated since the favicon data was requested. |
| 510 return; | 542 return; |
| 511 } | 543 } |
| 512 | 544 |
| 513 if (has_expired_or_incomplete_result) { | 545 if (has_expired_or_incomplete_result) { |
| 514 ScheduleDownload(current_candidate()->icon_url, | 546 ScheduleDownload(current_candidate()->icon_url, |
| 515 current_candidate()->icon_type); | 547 current_candidate()->icon_type); |
| 548 } else if (num_download_requests_ > 0) { |
| 549 RecordDownloadAttemptsForHandlerType(handler_type_, num_download_requests_); |
| 516 } | 550 } |
| 517 } | 551 } |
| 518 | 552 |
| 519 void FaviconHandler::ScheduleDownload(const GURL& image_url, | 553 void FaviconHandler::ScheduleDownload(const GURL& image_url, |
| 520 favicon_base::IconType icon_type) { | 554 favicon_base::IconType icon_type) { |
| 521 DCHECK(image_url.is_valid()); | 555 DCHECK(image_url.is_valid()); |
| 522 // Note that CancelableCallback starts cancelled. | 556 // Note that CancelableCallback starts cancelled. |
| 523 DCHECK(download_request_.IsCancelled()) << "More than one ongoing download"; | 557 DCHECK(download_request_.IsCancelled()) << "More than one ongoing download"; |
| 524 if (service_->WasUnableToDownloadFavicon(image_url)) { | 558 if (service_->WasUnableToDownloadFavicon(image_url)) { |
| 525 DVLOG(1) << "Skip Failed FavIcon: " << image_url; | 559 DVLOG(1) << "Skip Failed FavIcon: " << image_url; |
| 526 OnDidDownloadFavicon(icon_type, 0, 0, image_url, std::vector<SkBitmap>(), | 560 OnDidDownloadFavicon(icon_type, 0, 0, image_url, std::vector<SkBitmap>(), |
| 527 std::vector<gfx::Size>()); | 561 std::vector<gfx::Size>()); |
| 528 return; | 562 return; |
| 529 } | 563 } |
| 564 ++num_download_requests_; |
| 530 download_request_.Reset(base::Bind(&FaviconHandler::OnDidDownloadFavicon, | 565 download_request_.Reset(base::Bind(&FaviconHandler::OnDidDownloadFavicon, |
| 531 base::Unretained(this), icon_type)); | 566 base::Unretained(this), icon_type)); |
| 532 // A max bitmap size is specified to avoid receiving huge bitmaps in | 567 // A max bitmap size is specified to avoid receiving huge bitmaps in |
| 533 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() | 568 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() |
| 534 // for more details about the max bitmap size. | 569 // for more details about the max bitmap size. |
| 535 const int download_id = | 570 const int download_id = |
| 536 delegate_->DownloadImage(image_url, GetMaximalIconSize(handler_type_), | 571 delegate_->DownloadImage(image_url, GetMaximalIconSize(handler_type_), |
| 537 download_request_.callback()); | 572 download_request_.callback()); |
| 538 DCHECK_NE(download_id, 0); | 573 DCHECK_NE(download_id, 0); |
| 539 } | 574 } |
| 540 | 575 |
| 541 } // namespace favicon | 576 } // namespace favicon |
| OLD | NEW |