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 RecordAttemptsForHandlerType( | |
pkotwicz
2017/04/12 02:48:20
Maybe rename this method to: RecordDownloadAttempt
fhorschig
2017/04/12 08:32:39
Done.
| |
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 | |
62 // Returns true if |bitmap_results| is non-empty and: | 82 // Returns true if |bitmap_results| is non-empty and: |
63 // - At least one of the bitmaps in |bitmap_results| is expired | 83 // - At least one of the bitmaps in |bitmap_results| is expired |
64 // OR | 84 // OR |
65 // - |bitmap_results| is missing favicons for |desired_size_in_dip| and one of | 85 // - |bitmap_results| is missing favicons for |desired_size_in_dip| and one of |
66 // the scale factors in favicon_base::GetFaviconScales(). | 86 // the scale factors in favicon_base::GetFaviconScales(). |
67 bool HasExpiredOrIncompleteResult( | 87 bool HasExpiredOrIncompleteResult( |
68 int desired_size_in_dip, | 88 int desired_size_in_dip, |
69 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) { | 89 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) { |
70 if (bitmap_results.empty()) | 90 if (bitmap_results.empty()) |
71 return false; | 91 return false; |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
160 got_favicon_from_history_(false), | 180 got_favicon_from_history_(false), |
161 initial_history_result_expired_or_incomplete_(false), | 181 initial_history_result_expired_or_incomplete_(false), |
162 redownload_icons_(false), | 182 redownload_icons_(false), |
163 icon_types_(FaviconHandler::GetIconTypesFromHandlerType(handler_type)), | 183 icon_types_(FaviconHandler::GetIconTypesFromHandlerType(handler_type)), |
164 download_largest_icon_( | 184 download_largest_icon_( |
165 handler_type == FaviconDriverObserver::NON_TOUCH_LARGEST || | 185 handler_type == FaviconDriverObserver::NON_TOUCH_LARGEST || |
166 handler_type == FaviconDriverObserver::TOUCH_LARGEST), | 186 handler_type == FaviconDriverObserver::TOUCH_LARGEST), |
167 notification_icon_type_(favicon_base::INVALID_ICON), | 187 notification_icon_type_(favicon_base::INVALID_ICON), |
168 service_(service), | 188 service_(service), |
169 delegate_(delegate), | 189 delegate_(delegate), |
190 num_download_requests_(0), | |
170 current_candidate_index_(0u) { | 191 current_candidate_index_(0u) { |
171 DCHECK(delegate_); | 192 DCHECK(delegate_); |
172 } | 193 } |
173 | 194 |
174 FaviconHandler::~FaviconHandler() { | 195 FaviconHandler::~FaviconHandler() { |
175 } | 196 } |
176 | 197 |
177 // static | 198 // static |
178 int FaviconHandler::GetIconTypesFromHandlerType( | 199 int FaviconHandler::GetIconTypesFromHandlerType( |
179 FaviconDriverObserver::NotificationIconType handler_type) { | 200 FaviconDriverObserver::NotificationIconType handler_type) { |
(...skipping 12 matching lines...) Expand all Loading... | |
192 | 213 |
193 url_ = url; | 214 url_ = url; |
194 | 215 |
195 initial_history_result_expired_or_incomplete_ = false; | 216 initial_history_result_expired_or_incomplete_ = false; |
196 redownload_icons_ = false; | 217 redownload_icons_ = false; |
197 got_favicon_from_history_ = false; | 218 got_favicon_from_history_ = false; |
198 download_request_.Cancel(); | 219 download_request_.Cancel(); |
199 candidates_.clear(); | 220 candidates_.clear(); |
200 notification_icon_url_ = GURL(); | 221 notification_icon_url_ = GURL(); |
201 notification_icon_type_ = favicon_base::INVALID_ICON; | 222 notification_icon_type_ = favicon_base::INVALID_ICON; |
223 num_download_requests_ = 0; | |
202 current_candidate_index_ = 0u; | 224 current_candidate_index_ = 0u; |
203 best_favicon_ = DownloadedFavicon(); | 225 best_favicon_ = DownloadedFavicon(); |
204 | 226 |
205 // Request the favicon from the history service. In parallel to this the | 227 // 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 | 228 // renderer is going to notify us (well WebContents) when the favicon url is |
207 // available. | 229 // available. |
208 if (service_) { | 230 if (service_) { |
209 service_->GetFaviconForPageURL( | 231 service_->GetFaviconForPageURL( |
210 url_, icon_types_, preferred_icon_size(), | 232 url_, icon_types_, preferred_icon_size(), |
211 base::Bind( | 233 base::Bind( |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
304 &FaviconCandidate::CompareScore); | 326 &FaviconCandidate::CompareScore); |
305 | 327 |
306 if (candidates_.size() == sorted_candidates.size() && | 328 if (candidates_.size() == sorted_candidates.size() && |
307 std::equal(sorted_candidates.begin(), sorted_candidates.end(), | 329 std::equal(sorted_candidates.begin(), sorted_candidates.end(), |
308 candidates_.begin())) { | 330 candidates_.begin())) { |
309 return; | 331 return; |
310 } | 332 } |
311 | 333 |
312 download_request_.Cancel(); | 334 download_request_.Cancel(); |
313 candidates_ = std::move(sorted_candidates); | 335 candidates_ = std::move(sorted_candidates); |
336 num_download_requests_ = 0; | |
314 current_candidate_index_ = 0u; | 337 current_candidate_index_ = 0u; |
315 best_favicon_ = DownloadedFavicon(); | 338 best_favicon_ = DownloadedFavicon(); |
316 | 339 |
317 // TODO(davemoore) Should clear on empty url. Currently we ignore it. | 340 // TODO(davemoore) Should clear on empty url. Currently we ignore it. |
318 // This appears to be what FF does as well. | 341 // This appears to be what FF does as well. |
319 if (current_candidate() && got_favicon_from_history_) | 342 if (current_candidate() && got_favicon_from_history_) |
320 OnGotInitialHistoryDataAndIconURLCandidates(); | 343 OnGotInitialHistoryDataAndIconURLCandidates(); |
321 } | 344 } |
322 | 345 |
323 // static | 346 // static |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
392 downloaded_favicon.candidate.score = score; | 415 downloaded_favicon.candidate.score = score; |
393 request_next_icon = !UpdateFaviconCandidate(downloaded_favicon); | 416 request_next_icon = !UpdateFaviconCandidate(downloaded_favicon); |
394 } | 417 } |
395 } | 418 } |
396 | 419 |
397 if (request_next_icon && current_candidate_index_ + 1 < candidates_.size()) { | 420 if (request_next_icon && current_candidate_index_ + 1 < candidates_.size()) { |
398 // Process the next candidate. | 421 // Process the next candidate. |
399 ++current_candidate_index_; | 422 ++current_candidate_index_; |
400 DownloadCurrentCandidateOrAskFaviconService(); | 423 DownloadCurrentCandidateOrAskFaviconService(); |
401 } else { | 424 } else { |
425 // OnDidDownloadFavicon can only be called after invoking a request, so | |
426 // |num_download_requests_| can never be 0. | |
pkotwicz
2017/04/12 02:48:21
Nit: OnDidDownloadFavicon -> OnDidDownloadFavicon(
fhorschig
2017/04/12 08:32:39
Done.
| |
427 RecordAttemptsForHandlerType(handler_type_, num_download_requests_); | |
402 // We have either found the ideal candidate or run out of candidates. | 428 // We have either found the ideal candidate or run out of candidates. |
403 if (best_favicon_.candidate.icon_type != favicon_base::INVALID_ICON) { | 429 if (best_favicon_.candidate.icon_type != favicon_base::INVALID_ICON) { |
404 // No more icons to request, set the favicon from the candidate. | 430 // No more icons to request, set the favicon from the candidate. |
405 SetFavicon(best_favicon_.candidate.icon_url, best_favicon_.image, | 431 SetFavicon(best_favicon_.candidate.icon_url, best_favicon_.image, |
406 best_favicon_.candidate.icon_type); | 432 best_favicon_.candidate.icon_type); |
407 } | 433 } |
408 // Clear download related state. | 434 // Clear download related state. |
409 current_candidate_index_ = candidates_.size(); | 435 current_candidate_index_ = candidates_.size(); |
436 num_download_requests_ = 0; | |
410 best_favicon_ = DownloadedFavicon(); | 437 best_favicon_ = DownloadedFavicon(); |
411 } | 438 } |
412 } | 439 } |
413 | 440 |
414 const std::vector<GURL> FaviconHandler::GetIconURLs() const { | 441 const std::vector<GURL> FaviconHandler::GetIconURLs() const { |
415 std::vector<GURL> icon_urls; | 442 std::vector<GURL> icon_urls; |
416 for (const FaviconCandidate& candidate : candidates_) | 443 for (const FaviconCandidate& candidate : candidates_) |
417 icon_urls.push_back(candidate.icon_url); | 444 icon_urls.push_back(candidate.icon_url); |
418 return icon_urls; | 445 return icon_urls; |
419 } | 446 } |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
492 | 519 |
493 void FaviconHandler::OnFaviconData(const std::vector< | 520 void FaviconHandler::OnFaviconData(const std::vector< |
494 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) { | 521 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) { |
495 bool has_results = !favicon_bitmap_results.empty(); | 522 bool has_results = !favicon_bitmap_results.empty(); |
496 bool has_valid_result = HasValidResult(favicon_bitmap_results); | 523 bool has_valid_result = HasValidResult(favicon_bitmap_results); |
497 bool has_expired_or_incomplete_result = | 524 bool has_expired_or_incomplete_result = |
498 !has_valid_result || HasExpiredOrIncompleteResult(preferred_icon_size(), | 525 !has_valid_result || HasExpiredOrIncompleteResult(preferred_icon_size(), |
499 favicon_bitmap_results); | 526 favicon_bitmap_results); |
500 | 527 |
501 if (has_valid_result) { | 528 if (has_valid_result) { |
529 if (num_download_requests_ > 0) { | |
530 RecordAttemptsForHandlerType(handler_type_, current_candidate_index_); | |
pkotwicz
2017/04/12 02:48:21
Don't you want to pass |num_download_requests_| he
fhorschig
2017/04/12 08:32:39
Yes, done.
| |
531 } | |
502 // There is a valid favicon. Notify any observers. It is useful to notify | 532 // There is a valid favicon. Notify any observers. It is useful to notify |
503 // the observers even if the favicon is expired or incomplete (incorrect | 533 // the observers even if the favicon is expired or incomplete (incorrect |
504 // size) because temporarily showing the user an expired favicon or | 534 // size) because temporarily showing the user an expired favicon or |
505 // streched favicon is preferable to showing the user the default favicon. | 535 // streched favicon is preferable to showing the user the default favicon. |
506 NotifyFaviconUpdated(favicon_bitmap_results); | 536 NotifyFaviconUpdated(favicon_bitmap_results); |
507 } | 537 } |
508 | 538 |
509 if (!current_candidate() || | 539 if (!current_candidate() || |
510 (has_results && !DoUrlsAndIconsMatch(current_candidate()->icon_url, | 540 (has_results && !DoUrlsAndIconsMatch(current_candidate()->icon_url, |
511 current_candidate()->icon_type, | 541 current_candidate()->icon_type, |
(...skipping 12 matching lines...) Expand all Loading... | |
524 favicon_base::IconType icon_type) { | 554 favicon_base::IconType icon_type) { |
525 DCHECK(image_url.is_valid()); | 555 DCHECK(image_url.is_valid()); |
526 // Note that CancelableCallback starts cancelled. | 556 // Note that CancelableCallback starts cancelled. |
527 DCHECK(download_request_.IsCancelled()) << "More than one ongoing download"; | 557 DCHECK(download_request_.IsCancelled()) << "More than one ongoing download"; |
528 if (service_ && service_->WasUnableToDownloadFavicon(image_url)) { | 558 if (service_ && service_->WasUnableToDownloadFavicon(image_url)) { |
529 DVLOG(1) << "Skip Failed FavIcon: " << image_url; | 559 DVLOG(1) << "Skip Failed FavIcon: " << image_url; |
530 OnDidDownloadFavicon(icon_type, 0, 0, image_url, std::vector<SkBitmap>(), | 560 OnDidDownloadFavicon(icon_type, 0, 0, image_url, std::vector<SkBitmap>(), |
531 std::vector<gfx::Size>()); | 561 std::vector<gfx::Size>()); |
532 return; | 562 return; |
533 } | 563 } |
564 ++num_download_requests_; | |
534 download_request_.Reset(base::Bind(&FaviconHandler::OnDidDownloadFavicon, | 565 download_request_.Reset(base::Bind(&FaviconHandler::OnDidDownloadFavicon, |
535 base::Unretained(this), icon_type)); | 566 base::Unretained(this), icon_type)); |
536 // 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 |
537 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() | 568 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() |
538 // for more details about the max bitmap size. | 569 // for more details about the max bitmap size. |
539 const int download_id = | 570 const int download_id = |
540 delegate_->DownloadImage(image_url, GetMaximalIconSize(handler_type_), | 571 delegate_->DownloadImage(image_url, GetMaximalIconSize(handler_type_), |
541 download_request_.callback()); | 572 download_request_.callback()); |
542 DCHECK_NE(download_id, 0); | 573 DCHECK_NE(download_id, 0); |
543 } | 574 } |
544 | 575 |
545 } // namespace favicon | 576 } // namespace favicon |
OLD | NEW |