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 switch (handler_type) { | |
67 case FaviconDriverObserver::NON_TOUCH_16_DIP: | |
68 UMA_HISTOGRAM_COUNTS_100("Favicons.DownloadAttempts.Favicons", attempts); | |
69 return; | |
70 case FaviconDriverObserver::NON_TOUCH_LARGEST: | |
71 UMA_HISTOGRAM_COUNTS_100("Favicons.DownloadAttempts.LargeIcons", | |
72 attempts); | |
73 return; | |
74 case FaviconDriverObserver::TOUCH_LARGEST: | |
75 UMA_HISTOGRAM_COUNTS_100("Favicons.DownloadAttempts.TouchIcons", | |
76 attempts); | |
77 return; | |
78 } | |
79 NOTREACHED(); | |
80 } | |
81 | |
82 void RecordDownloadOutcome(FaviconHandler::DownloadOutcome outcome) { | |
83 UMA_HISTOGRAM_ENUMERATION( | |
84 "Favicons.DownloadOutcome", static_cast<int>(outcome), | |
Ilya Sherman
2017/04/13 21:24:43
nit: The static_cast should no longer be necessary
fhorschig
2017/04/18 15:27:18
Removed. Nice to know that this works!
| |
85 FaviconHandler::DownloadOutcome::DOWNLOAD_OUTCOME_COUNT); | |
86 } | |
87 | |
62 // Returns true if |bitmap_results| is non-empty and: | 88 // Returns true if |bitmap_results| is non-empty and: |
63 // - At least one of the bitmaps in |bitmap_results| is expired | 89 // - At least one of the bitmaps in |bitmap_results| is expired |
64 // OR | 90 // OR |
65 // - |bitmap_results| is missing favicons for |desired_size_in_dip| and one of | 91 // - |bitmap_results| is missing favicons for |desired_size_in_dip| and one of |
66 // the scale factors in favicon_base::GetFaviconScales(). | 92 // the scale factors in favicon_base::GetFaviconScales(). |
67 bool HasExpiredOrIncompleteResult( | 93 bool HasExpiredOrIncompleteResult( |
68 int desired_size_in_dip, | 94 int desired_size_in_dip, |
69 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) { | 95 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) { |
70 if (bitmap_results.empty()) | 96 if (bitmap_results.empty()) |
71 return false; | 97 return false; |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
160 got_favicon_from_history_(false), | 186 got_favicon_from_history_(false), |
161 initial_history_result_expired_or_incomplete_(false), | 187 initial_history_result_expired_or_incomplete_(false), |
162 redownload_icons_(false), | 188 redownload_icons_(false), |
163 icon_types_(FaviconHandler::GetIconTypesFromHandlerType(handler_type)), | 189 icon_types_(FaviconHandler::GetIconTypesFromHandlerType(handler_type)), |
164 download_largest_icon_( | 190 download_largest_icon_( |
165 handler_type == FaviconDriverObserver::NON_TOUCH_LARGEST || | 191 handler_type == FaviconDriverObserver::NON_TOUCH_LARGEST || |
166 handler_type == FaviconDriverObserver::TOUCH_LARGEST), | 192 handler_type == FaviconDriverObserver::TOUCH_LARGEST), |
167 notification_icon_type_(favicon_base::INVALID_ICON), | 193 notification_icon_type_(favicon_base::INVALID_ICON), |
168 service_(service), | 194 service_(service), |
169 delegate_(delegate), | 195 delegate_(delegate), |
196 num_download_requests_(0), | |
170 current_candidate_index_(0u) { | 197 current_candidate_index_(0u) { |
171 DCHECK(delegate_); | 198 DCHECK(delegate_); |
172 } | 199 } |
173 | 200 |
174 FaviconHandler::~FaviconHandler() { | 201 FaviconHandler::~FaviconHandler() { |
175 } | 202 } |
176 | 203 |
177 // static | 204 // static |
178 int FaviconHandler::GetIconTypesFromHandlerType( | 205 int FaviconHandler::GetIconTypesFromHandlerType( |
179 FaviconDriverObserver::NotificationIconType handler_type) { | 206 FaviconDriverObserver::NotificationIconType handler_type) { |
(...skipping 12 matching lines...) Expand all Loading... | |
192 | 219 |
193 url_ = url; | 220 url_ = url; |
194 | 221 |
195 initial_history_result_expired_or_incomplete_ = false; | 222 initial_history_result_expired_or_incomplete_ = false; |
196 redownload_icons_ = false; | 223 redownload_icons_ = false; |
197 got_favicon_from_history_ = false; | 224 got_favicon_from_history_ = false; |
198 download_request_.Cancel(); | 225 download_request_.Cancel(); |
199 candidates_.clear(); | 226 candidates_.clear(); |
200 notification_icon_url_ = GURL(); | 227 notification_icon_url_ = GURL(); |
201 notification_icon_type_ = favicon_base::INVALID_ICON; | 228 notification_icon_type_ = favicon_base::INVALID_ICON; |
229 num_download_requests_ = 0; | |
202 current_candidate_index_ = 0u; | 230 current_candidate_index_ = 0u; |
203 best_favicon_ = DownloadedFavicon(); | 231 best_favicon_ = DownloadedFavicon(); |
204 | 232 |
205 // Request the favicon from the history service. In parallel to this the | 233 // 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 | 234 // renderer is going to notify us (well WebContents) when the favicon url is |
207 // available. | 235 // available. |
208 if (service_) { | 236 if (service_) { |
209 service_->GetFaviconForPageURL( | 237 service_->GetFaviconForPageURL( |
210 url_, icon_types_, preferred_icon_size(), | 238 url_, icon_types_, preferred_icon_size(), |
211 base::Bind( | 239 base::Bind( |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
304 &FaviconCandidate::CompareScore); | 332 &FaviconCandidate::CompareScore); |
305 | 333 |
306 if (candidates_.size() == sorted_candidates.size() && | 334 if (candidates_.size() == sorted_candidates.size() && |
307 std::equal(sorted_candidates.begin(), sorted_candidates.end(), | 335 std::equal(sorted_candidates.begin(), sorted_candidates.end(), |
308 candidates_.begin())) { | 336 candidates_.begin())) { |
309 return; | 337 return; |
310 } | 338 } |
311 | 339 |
312 download_request_.Cancel(); | 340 download_request_.Cancel(); |
313 candidates_ = std::move(sorted_candidates); | 341 candidates_ = std::move(sorted_candidates); |
342 num_download_requests_ = 0; | |
314 current_candidate_index_ = 0u; | 343 current_candidate_index_ = 0u; |
315 best_favicon_ = DownloadedFavicon(); | 344 best_favicon_ = DownloadedFavicon(); |
316 | 345 |
317 // TODO(davemoore) Should clear on empty url. Currently we ignore it. | 346 // TODO(davemoore) Should clear on empty url. Currently we ignore it. |
318 // This appears to be what FF does as well. | 347 // This appears to be what FF does as well. |
319 if (current_candidate() && got_favicon_from_history_) | 348 if (current_candidate() && got_favicon_from_history_) |
320 OnGotInitialHistoryDataAndIconURLCandidates(); | 349 OnGotInitialHistoryDataAndIconURLCandidates(); |
321 } | 350 } |
322 | 351 |
323 // static | 352 // static |
(...skipping 28 matching lines...) Expand all Loading... | |
352 int id, | 381 int id, |
353 int http_status_code, | 382 int http_status_code, |
354 const GURL& image_url, | 383 const GURL& image_url, |
355 const std::vector<SkBitmap>& bitmaps, | 384 const std::vector<SkBitmap>& bitmaps, |
356 const std::vector<gfx::Size>& original_bitmap_sizes) { | 385 const std::vector<gfx::Size>& original_bitmap_sizes) { |
357 // Mark download as finished. | 386 // Mark download as finished. |
358 download_request_.Cancel(); | 387 download_request_.Cancel(); |
359 | 388 |
360 if (bitmaps.empty() && http_status_code == 404) { | 389 if (bitmaps.empty() && http_status_code == 404) { |
361 DVLOG(1) << "Failed to Download Favicon:" << image_url; | 390 DVLOG(1) << "Failed to Download Favicon:" << image_url; |
391 RecordDownloadOutcome(DownloadOutcome::FAILED); | |
362 if (service_) | 392 if (service_) |
363 service_->UnableToDownloadFavicon(image_url); | 393 service_->UnableToDownloadFavicon(image_url); |
364 } | 394 } |
365 | 395 |
366 bool request_next_icon = true; | 396 bool request_next_icon = true; |
367 if (!bitmaps.empty()) { | 397 if (!bitmaps.empty()) { |
398 RecordDownloadOutcome(DownloadOutcome::SUCCEEDED); | |
368 float score = 0.0f; | 399 float score = 0.0f; |
369 gfx::ImageSkia image_skia; | 400 gfx::ImageSkia image_skia; |
370 if (download_largest_icon_) { | 401 if (download_largest_icon_) { |
371 std::vector<size_t> best_indices; | 402 std::vector<size_t> best_indices; |
372 SelectFaviconFrameIndices(original_bitmap_sizes, | 403 SelectFaviconFrameIndices(original_bitmap_sizes, |
373 GetDesiredPixelSizes(handler_type_), | 404 GetDesiredPixelSizes(handler_type_), |
374 &best_indices, &score); | 405 &best_indices, &score); |
375 DCHECK_EQ(1U, best_indices.size()); | 406 DCHECK_EQ(1U, best_indices.size()); |
376 image_skia = | 407 image_skia = |
377 gfx::ImageSkia::CreateFrom1xBitmap(bitmaps[best_indices.front()]); | 408 gfx::ImageSkia::CreateFrom1xBitmap(bitmaps[best_indices.front()]); |
(...skipping 14 matching lines...) Expand all Loading... | |
392 downloaded_favicon.candidate.score = score; | 423 downloaded_favicon.candidate.score = score; |
393 request_next_icon = !UpdateFaviconCandidate(downloaded_favicon); | 424 request_next_icon = !UpdateFaviconCandidate(downloaded_favicon); |
394 } | 425 } |
395 } | 426 } |
396 | 427 |
397 if (request_next_icon && current_candidate_index_ + 1 < candidates_.size()) { | 428 if (request_next_icon && current_candidate_index_ + 1 < candidates_.size()) { |
398 // Process the next candidate. | 429 // Process the next candidate. |
399 ++current_candidate_index_; | 430 ++current_candidate_index_; |
400 DownloadCurrentCandidateOrAskFaviconService(); | 431 DownloadCurrentCandidateOrAskFaviconService(); |
401 } else { | 432 } else { |
433 // OnDidDownloadFavicon() can only be called after requesting a download, so | |
434 // |num_download_requests_| can never be 0. | |
435 RecordDownloadAttemptsForHandlerType(handler_type_, num_download_requests_); | |
402 // We have either found the ideal candidate or run out of candidates. | 436 // We have either found the ideal candidate or run out of candidates. |
403 if (best_favicon_.candidate.icon_type != favicon_base::INVALID_ICON) { | 437 if (best_favicon_.candidate.icon_type != favicon_base::INVALID_ICON) { |
404 // No more icons to request, set the favicon from the candidate. | 438 // No more icons to request, set the favicon from the candidate. |
405 SetFavicon(best_favicon_.candidate.icon_url, best_favicon_.image, | 439 SetFavicon(best_favicon_.candidate.icon_url, best_favicon_.image, |
406 best_favicon_.candidate.icon_type); | 440 best_favicon_.candidate.icon_type); |
407 } | 441 } |
408 // Clear download related state. | 442 // Clear download related state. |
409 current_candidate_index_ = candidates_.size(); | 443 current_candidate_index_ = candidates_.size(); |
444 num_download_requests_ = 0; | |
410 best_favicon_ = DownloadedFavicon(); | 445 best_favicon_ = DownloadedFavicon(); |
411 } | 446 } |
412 } | 447 } |
413 | 448 |
414 const std::vector<GURL> FaviconHandler::GetIconURLs() const { | 449 const std::vector<GURL> FaviconHandler::GetIconURLs() const { |
415 std::vector<GURL> icon_urls; | 450 std::vector<GURL> icon_urls; |
416 for (const FaviconCandidate& candidate : candidates_) | 451 for (const FaviconCandidate& candidate : candidates_) |
417 icon_urls.push_back(candidate.icon_url); | 452 icon_urls.push_back(candidate.icon_url); |
418 return icon_urls; | 453 return icon_urls; |
419 } | 454 } |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
510 (has_results && !DoUrlsAndIconsMatch(current_candidate()->icon_url, | 545 (has_results && !DoUrlsAndIconsMatch(current_candidate()->icon_url, |
511 current_candidate()->icon_type, | 546 current_candidate()->icon_type, |
512 favicon_bitmap_results))) { | 547 favicon_bitmap_results))) { |
513 // The icon URLs have been updated since the favicon data was requested. | 548 // The icon URLs have been updated since the favicon data was requested. |
514 return; | 549 return; |
515 } | 550 } |
516 | 551 |
517 if (has_expired_or_incomplete_result) { | 552 if (has_expired_or_incomplete_result) { |
518 ScheduleDownload(current_candidate()->icon_url, | 553 ScheduleDownload(current_candidate()->icon_url, |
519 current_candidate()->icon_type); | 554 current_candidate()->icon_type); |
555 } else if (num_download_requests_ > 0) { | |
556 RecordDownloadAttemptsForHandlerType(handler_type_, num_download_requests_); | |
520 } | 557 } |
521 } | 558 } |
522 | 559 |
523 void FaviconHandler::ScheduleDownload(const GURL& image_url, | 560 void FaviconHandler::ScheduleDownload(const GURL& image_url, |
524 favicon_base::IconType icon_type) { | 561 favicon_base::IconType icon_type) { |
525 DCHECK(image_url.is_valid()); | 562 DCHECK(image_url.is_valid()); |
526 // Note that CancelableCallback starts cancelled. | 563 // Note that CancelableCallback starts cancelled. |
527 DCHECK(download_request_.IsCancelled()) << "More than one ongoing download"; | 564 DCHECK(download_request_.IsCancelled()) << "More than one ongoing download"; |
528 if (service_ && service_->WasUnableToDownloadFavicon(image_url)) { | 565 if (service_ && service_->WasUnableToDownloadFavicon(image_url)) { |
529 DVLOG(1) << "Skip Failed FavIcon: " << image_url; | 566 DVLOG(1) << "Skip Failed FavIcon: " << image_url; |
567 RecordDownloadOutcome(DownloadOutcome::SKIPPED); | |
530 OnDidDownloadFavicon(icon_type, 0, 0, image_url, std::vector<SkBitmap>(), | 568 OnDidDownloadFavicon(icon_type, 0, 0, image_url, std::vector<SkBitmap>(), |
531 std::vector<gfx::Size>()); | 569 std::vector<gfx::Size>()); |
532 return; | 570 return; |
533 } | 571 } |
572 ++num_download_requests_; | |
534 download_request_.Reset(base::Bind(&FaviconHandler::OnDidDownloadFavicon, | 573 download_request_.Reset(base::Bind(&FaviconHandler::OnDidDownloadFavicon, |
535 base::Unretained(this), icon_type)); | 574 base::Unretained(this), icon_type)); |
536 // A max bitmap size is specified to avoid receiving huge bitmaps in | 575 // A max bitmap size is specified to avoid receiving huge bitmaps in |
537 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() | 576 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() |
538 // for more details about the max bitmap size. | 577 // for more details about the max bitmap size. |
539 const int download_id = | 578 const int download_id = |
540 delegate_->DownloadImage(image_url, GetMaximalIconSize(handler_type_), | 579 delegate_->DownloadImage(image_url, GetMaximalIconSize(handler_type_), |
541 download_request_.callback()); | 580 download_request_.callback()); |
542 DCHECK_NE(download_id, 0); | 581 DCHECK_NE(download_id, 0); |
543 } | 582 } |
544 | 583 |
545 } // namespace favicon | 584 } // namespace favicon |
OLD | NEW |