Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(664)

Side by Side Diff: components/favicon/core/favicon_handler.cc

Issue 2799273002: Add support to process favicons from Web Manifests (Closed)
Patch Set: Rebased. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/feature_list.h"
14 #include "base/memory/ref_counted_memory.h" 15 #include "base/memory/ref_counted_memory.h"
15 #include "base/metrics/histogram_macros.h" 16 #include "base/metrics/histogram_macros.h"
16 #include "build/build_config.h" 17 #include "build/build_config.h"
17 #include "components/favicon/core/favicon_service.h" 18 #include "components/favicon/core/favicon_service.h"
18 #include "components/favicon_base/favicon_util.h" 19 #include "components/favicon_base/favicon_util.h"
19 #include "components/favicon_base/select_favicon_frames.h" 20 #include "components/favicon_base/select_favicon_frames.h"
20 #include "skia/ext/image_operations.h" 21 #include "skia/ext/image_operations.h"
21 #include "ui/gfx/codec/png_codec.h" 22 #include "ui/gfx/codec/png_codec.h"
22 #include "ui/gfx/image/image_skia.h" 23 #include "ui/gfx/image/image_skia.h"
23 #include "ui/gfx/image/image_util.h" 24 #include "ui/gfx/image/image_util.h"
24 25
25 namespace favicon { 26 namespace favicon {
27
28 const base::Feature kFaviconsFromWebManifest{"FaviconsFromWebManifest",
29 base::FEATURE_DISABLED_BY_DEFAULT};
30
26 namespace { 31 namespace {
27 32
28 const int kNonTouchLargestIconSize = 192; 33 const int kNonTouchLargestIconSize = 192;
29 34
30 // Size (along each axis) of a touch icon. This currently corresponds to 35 // Size (along each axis) of a touch icon. This currently corresponds to
31 // the apple touch icon for iPad. 36 // the apple touch icon for iPad.
32 const int kTouchIconSize = 144; 37 const int kTouchIconSize = 144;
33 38
34 // Return true if |bitmap_result| is expired. 39 // Return true if |bitmap_result| is expired.
35 bool IsExpired(const favicon_base::FaviconRawBitmapResult& bitmap_result) { 40 bool IsExpired(const favicon_base::FaviconRawBitmapResult& bitmap_result) {
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 } 139 }
135 case FaviconDriverObserver::NON_TOUCH_LARGEST: 140 case FaviconDriverObserver::NON_TOUCH_LARGEST:
136 return std::vector<int>(1U, kNonTouchLargestIconSize); 141 return std::vector<int>(1U, kNonTouchLargestIconSize);
137 case FaviconDriverObserver::TOUCH_LARGEST: 142 case FaviconDriverObserver::TOUCH_LARGEST:
138 return std::vector<int>(1U, kTouchIconSize); 143 return std::vector<int>(1U, kTouchIconSize);
139 } 144 }
140 NOTREACHED(); 145 NOTREACHED();
141 return std::vector<int>(); 146 return std::vector<int>();
142 } 147 }
143 148
149 bool FaviconURLEquals(const FaviconURL& lhs, const FaviconURL& rhs) {
150 return lhs.icon_url == rhs.icon_url && lhs.icon_type == rhs.icon_type &&
151 lhs.icon_sizes == rhs.icon_sizes;
152 }
153
144 } // namespace 154 } // namespace
145 155
146 //////////////////////////////////////////////////////////////////////////////// 156 ////////////////////////////////////////////////////////////////////////////////
147 157
148 // static 158 // static
149 FaviconHandler::FaviconCandidate 159 FaviconHandler::FaviconCandidate
150 FaviconHandler::FaviconCandidate::FromFaviconURL( 160 FaviconHandler::FaviconCandidate::FromFaviconURL(
151 const favicon::FaviconURL& favicon_url, 161 const favicon::FaviconURL& favicon_url,
152 const std::vector<int>& desired_pixel_sizes) { 162 const std::vector<int>& desired_pixel_sizes) {
153 FaviconCandidate candidate; 163 FaviconCandidate candidate;
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 212
203 void FaviconHandler::FetchFavicon(const GURL& url) { 213 void FaviconHandler::FetchFavicon(const GURL& url) {
204 cancelable_task_tracker_for_page_url_.TryCancelAll(); 214 cancelable_task_tracker_for_page_url_.TryCancelAll();
205 cancelable_task_tracker_for_candidates_.TryCancelAll(); 215 cancelable_task_tracker_for_candidates_.TryCancelAll();
206 216
207 url_ = url; 217 url_ = url;
208 218
209 initial_history_result_expired_or_incomplete_ = false; 219 initial_history_result_expired_or_incomplete_ = false;
210 redownload_icons_ = false; 220 redownload_icons_ = false;
211 got_favicon_from_history_ = false; 221 got_favicon_from_history_ = false;
222 manifest_download_request_.Cancel();
212 image_download_request_.Cancel(); 223 image_download_request_.Cancel();
224 manifest_url_ = GURL();
213 candidates_.clear(); 225 candidates_.clear();
214 notification_icon_url_ = GURL(); 226 notification_icon_url_ = GURL();
215 notification_icon_type_ = favicon_base::INVALID_ICON; 227 notification_icon_type_ = favicon_base::INVALID_ICON;
216 num_image_download_requests_ = 0; 228 num_image_download_requests_ = 0;
217 current_candidate_index_ = 0u; 229 current_candidate_index_ = 0u;
218 best_favicon_ = DownloadedFavicon(); 230 best_favicon_ = DownloadedFavicon();
219 231
220 // 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
221 // 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
222 // available. 234 // available.
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 delegate_->OnFaviconUpdated(url_, handler_type_, icon_url, 302 delegate_->OnFaviconUpdated(url_, handler_type_, icon_url,
291 icon_url != notification_icon_url_, 303 icon_url != notification_icon_url_,
292 image_with_adjusted_colorspace); 304 image_with_adjusted_colorspace);
293 305
294 notification_icon_url_ = icon_url; 306 notification_icon_url_ = icon_url;
295 notification_icon_type_ = icon_type; 307 notification_icon_type_ = icon_type;
296 } 308 }
297 309
298 void FaviconHandler::OnUpdateCandidates( 310 void FaviconHandler::OnUpdateCandidates(
299 const GURL& page_url, 311 const GURL& page_url,
300 const std::vector<FaviconURL>& candidates) { 312 const std::vector<FaviconURL>& candidates,
313 const GURL& manifest_url) {
301 if (page_url != url_) 314 if (page_url != url_)
302 return; 315 return;
303 316
317 // |candidates| or |manifest_url| could have been modified via Javascript. If
318 // neither changed, ignore the call.
319 if (manifest_url_ == manifest_url &&
pkotwicz 2017/05/16 06:36:35 If the kFaviconsFromWebManifest is not enabled, wo
mastiz 2017/05/16 11:05:22 Not always but you're right, it deserves something
320 non_manifest_original_candidates_.size() == candidates.size() &&
321 std::equal(candidates.begin(), candidates.end(),
322 non_manifest_original_candidates_.begin(),
323 &FaviconURLEquals)) {
324 return;
325 }
326
327 non_manifest_original_candidates_ = candidates;
328 cancelable_task_tracker_for_candidates_.TryCancelAll();
329 manifest_download_request_.Cancel();
330 image_download_request_.Cancel();
331 num_image_download_requests_ = 0;
332 current_candidate_index_ = 0u;
333 best_favicon_ = DownloadedFavicon();
334
335 if (base::FeatureList::IsEnabled(kFaviconsFromWebManifest))
336 manifest_url_ = manifest_url;
337
338 // Check if the manifest was previously blacklisted (e.g. returned a 404) and
339 // ignore the manifest URL if that's the case.
340 if (!manifest_url_.is_empty() &&
341 service_->WasUnableToDownloadFavicon(manifest_url_)) {
342 DVLOG(1) << "Skip failed Manifest: " << manifest_url;
343 manifest_url_ = GURL();
344 }
345
346 // If no manifest available, proceed with the regular candidates only.
347 if (manifest_url_.is_empty()) {
348 OnGotFinalIconURLCandidates(candidates);
349 return;
350 }
351
352 // See if there is a cached favicon for the manifest. This will update the DB
353 // mappings which is an optimistic approach, since the download of the
354 // manifest might fail or it might list no icons (although
355 // WasUnableToDownloadFavicon() above attempts to capture those cases).
356 GetFaviconAndUpdateMappingsUnlessIncognito(
357 /*icon_url=*/manifest_url_, favicon_base::FAVICON,
358 base::Bind(&FaviconHandler::OnFaviconDataForManifestFromFaviconService,
359 base::Unretained(this)));
360 }
361
362 void FaviconHandler::OnFaviconDataForManifestFromFaviconService(
363 const std::vector<favicon_base::FaviconRawBitmapResult>&
364 favicon_bitmap_results) {
365 bool has_valid_result = HasValidResult(favicon_bitmap_results);
366 bool has_expired_or_incomplete_result =
367 !has_valid_result || HasExpiredOrIncompleteResult(preferred_icon_size(),
368 favicon_bitmap_results);
369
370 if (has_valid_result) {
371 // There is a valid favicon. Notify any observers. It is useful to notify
372 // the observers even if the favicon is expired or incomplete (incorrect
373 // size) because temporarily showing the user an expired favicon or
374 // streched favicon is preferable to showing the user the default favicon.
375 NotifyFaviconUpdated(favicon_bitmap_results);
376 }
377
378 if (has_expired_or_incomplete_result) {
379 manifest_download_request_.Reset(base::Bind(
380 &FaviconHandler::OnDidDownloadManifest, base::Unretained(this)));
381 delegate_->DownloadManifest(manifest_url_,
382 manifest_download_request_.callback());
383 }
384 }
385
386 void FaviconHandler::OnDidDownloadManifest(
387 const std::vector<FaviconURL>& candidates) {
388 // Mark manifest download as finished.
389 manifest_download_request_.Cancel();
390
391 if (!candidates.empty()) {
392 OnGotFinalIconURLCandidates(candidates);
393 return;
394 }
395
396 // If either the downloading of the manifest failed, OR the manifest contains
397 // no icons, proceed with the list of icons listed in the HTML.
398 DVLOG(1) << "Could not fetch Manifest icons from " << manifest_url_
399 << ", falling back to inlined ones, which are "
400 << non_manifest_original_candidates_.size();
401
402 service_->UnableToDownloadFavicon(manifest_url_);
403 manifest_url_ = GURL();
404
405 // Because UpdateFaviconMappingsAndFetch() was optimistically called for the
406 // manifest URL, we might need to undo that.
407 notification_icon_type_ = favicon_base::INVALID_ICON;
408
409 OnGotFinalIconURLCandidates(non_manifest_original_candidates_);
410 }
411
412 void FaviconHandler::OnGotFinalIconURLCandidates(
413 const std::vector<FaviconURL>& candidates) {
304 std::vector<FaviconCandidate> sorted_candidates; 414 std::vector<FaviconCandidate> sorted_candidates;
305 const std::vector<int> desired_pixel_sizes = 415 const std::vector<int> desired_pixel_sizes =
306 GetDesiredPixelSizes(handler_type_); 416 GetDesiredPixelSizes(handler_type_);
307 for (const FaviconURL& candidate : candidates) { 417 for (const FaviconURL& candidate : candidates) {
308 if (!candidate.icon_url.is_empty() && (candidate.icon_type & icon_types_)) { 418 if (!candidate.icon_url.is_empty() && (candidate.icon_type & icon_types_)) {
309 sorted_candidates.push_back( 419 sorted_candidates.push_back(
310 FaviconCandidate::FromFaviconURL(candidate, desired_pixel_sizes)); 420 FaviconCandidate::FromFaviconURL(candidate, desired_pixel_sizes));
311 } 421 }
312 } 422 }
313 423
314 std::stable_sort(sorted_candidates.begin(), sorted_candidates.end(), 424 std::stable_sort(sorted_candidates.begin(), sorted_candidates.end(),
315 &FaviconCandidate::CompareScore); 425 &FaviconCandidate::CompareScore);
316 426
317 if (candidates_.size() == sorted_candidates.size() &&
318 std::equal(sorted_candidates.begin(), sorted_candidates.end(),
319 candidates_.begin())) {
320 return;
321 }
322
323 cancelable_task_tracker_for_candidates_.TryCancelAll();
324 image_download_request_.Cancel();
325 candidates_ = std::move(sorted_candidates); 427 candidates_ = std::move(sorted_candidates);
326 num_image_download_requests_ = 0;
327 current_candidate_index_ = 0u;
328 best_favicon_ = DownloadedFavicon();
329 428
330 // TODO(davemoore) Should clear on empty url. Currently we ignore it. 429 // TODO(davemoore) Should clear on empty url. Currently we ignore it.
331 // This appears to be what FF does as well. 430 // This appears to be what FF does as well.
332 if (current_candidate() && got_favicon_from_history_) 431 if (current_candidate() && got_favicon_from_history_)
333 OnGotInitialHistoryDataAndIconURLCandidates(); 432 OnGotInitialHistoryDataAndIconURLCandidates();
334 } 433 }
335 434
336 // static 435 // static
337 int FaviconHandler::GetMaximalIconSize( 436 int FaviconHandler::GetMaximalIconSize(
338 FaviconDriverObserver::NotificationIconType handler_type) { 437 FaviconDriverObserver::NotificationIconType handler_type) {
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 // Process the next candidate. 511 // Process the next candidate.
413 ++current_candidate_index_; 512 ++current_candidate_index_;
414 DownloadCurrentCandidateOrAskFaviconService(); 513 DownloadCurrentCandidateOrAskFaviconService();
415 } else { 514 } else {
416 // OnDidDownloadFavicon() can only be called after requesting a download, so 515 // OnDidDownloadFavicon() can only be called after requesting a download, so
417 // |num_image_download_requests_| can never be 0. 516 // |num_image_download_requests_| can never be 0.
418 RecordDownloadAttemptsForHandlerType(handler_type_, 517 RecordDownloadAttemptsForHandlerType(handler_type_,
419 num_image_download_requests_); 518 num_image_download_requests_);
420 // We have either found the ideal candidate or run out of candidates. 519 // We have either found the ideal candidate or run out of candidates.
421 if (best_favicon_.candidate.icon_type != favicon_base::INVALID_ICON) { 520 if (best_favicon_.candidate.icon_type != favicon_base::INVALID_ICON) {
422 // No more icons to request, set the favicon from the candidate. 521 // No more icons to request, set the favicon from the candidate. The
423 SetFavicon(best_favicon_.candidate.icon_url, best_favicon_.image, 522 // manifest URL, if available, is used instead of the icon URL.
424 best_favicon_.candidate.icon_type); 523 SetFavicon(manifest_url_.is_empty() ? best_favicon_.candidate.icon_url
524 : manifest_url_,
525 best_favicon_.image, best_favicon_.candidate.icon_type);
425 } 526 }
426 // Clear download related state. 527 // Clear download related state.
427 current_candidate_index_ = candidates_.size(); 528 current_candidate_index_ = candidates_.size();
428 num_image_download_requests_ = 0; 529 num_image_download_requests_ = 0;
429 best_favicon_ = DownloadedFavicon(); 530 best_favicon_ = DownloadedFavicon();
430 } 531 }
431 } 532 }
432 533
433 const std::vector<GURL> FaviconHandler::GetIconURLs() const { 534 const std::vector<GURL> FaviconHandler::GetIconURLs() const {
434 std::vector<GURL> icon_urls; 535 std::vector<GURL> icon_urls;
435 for (const FaviconCandidate& candidate : candidates_) 536 for (const FaviconCandidate& candidate : candidates_)
436 icon_urls.push_back(candidate.icon_url); 537 icon_urls.push_back(candidate.icon_url);
437 return icon_urls; 538 return icon_urls;
438 } 539 }
439 540
440 bool FaviconHandler::HasPendingTasksForTest() { 541 bool FaviconHandler::HasPendingTasksForTest() {
441 return !image_download_request_.IsCancelled() || 542 return !image_download_request_.IsCancelled() ||
543 !manifest_download_request_.IsCancelled() ||
442 cancelable_task_tracker_for_page_url_.HasTrackedTasks() || 544 cancelable_task_tracker_for_page_url_.HasTrackedTasks() ||
443 cancelable_task_tracker_for_candidates_.HasTrackedTasks(); 545 cancelable_task_tracker_for_candidates_.HasTrackedTasks();
444 } 546 }
445 547
446 bool FaviconHandler::ShouldSaveFavicon() { 548 bool FaviconHandler::ShouldSaveFavicon() {
447 if (!delegate_->IsOffTheRecord()) 549 if (!delegate_->IsOffTheRecord())
448 return true; 550 return true;
449 551
450 // Always save favicon if the page is bookmarked. 552 // Always save favicon if the page is bookmarked.
451 return delegate_->IsBookmarked(url_); 553 return delegate_->IsBookmarked(url_);
(...skipping 17 matching lines...) Expand all
469 // url) we'll fetch later on. This way the user doesn't see a flash of the 571 // url) we'll fetch later on. This way the user doesn't see a flash of the
470 // default favicon. 572 // default favicon.
471 NotifyFaviconUpdated(favicon_bitmap_results); 573 NotifyFaviconUpdated(favicon_bitmap_results);
472 } 574 }
473 575
474 if (current_candidate()) 576 if (current_candidate())
475 OnGotInitialHistoryDataAndIconURLCandidates(); 577 OnGotInitialHistoryDataAndIconURLCandidates();
476 } 578 }
477 579
478 void FaviconHandler::DownloadCurrentCandidateOrAskFaviconService() { 580 void FaviconHandler::DownloadCurrentCandidateOrAskFaviconService() {
479 GURL icon_url = current_candidate()->icon_url; 581 const GURL icon_url = current_candidate()->icon_url;
480 favicon_base::IconType icon_type = current_candidate()->icon_type; 582 const favicon_base::IconType icon_type = current_candidate()->icon_type;
481 583 // If the icons listed in a manifest are being processed, skip the cache
482 if (redownload_icons_) { 584 // lookup for |icon_url| since the manifest's URL is used for caching, not the
585 // icon URL, and this lookup has happened earlier.
586 if (redownload_icons_ || !manifest_url_.is_empty()) {
483 // We have the mapping, but the favicon is out of date. Download it now. 587 // We have the mapping, but the favicon is out of date. Download it now.
484 ScheduleImageDownload(icon_url, icon_type); 588 ScheduleImageDownload(icon_url, icon_type);
485 } else { 589 } else {
486 // We don't know the favicon, but we may have previously downloaded the 590 GetFaviconAndUpdateMappingsUnlessIncognito(
487 // favicon for another page that shares the same favicon. Ask for the 591 icon_url, icon_type,
488 // favicon given the favicon URL. 592 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)));
489 if (delegate_->IsOffTheRecord()) {
490 service_->GetFavicon(
491 icon_url, icon_type, preferred_icon_size(),
492 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)),
493 &cancelable_task_tracker_for_candidates_);
494 } else {
495 // Ask the history service for the icon. This does two things:
496 // 1. Attempts to fetch the favicon data from the database.
497 // 2. If the favicon exists in the database, this updates the database to
498 // include the mapping between the page url and the favicon url.
499 // This is asynchronous. The history service will call back when done.
500 service_->UpdateFaviconMappingsAndFetch(
501 url_, icon_url, icon_type, preferred_icon_size(),
502 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)),
503 &cancelable_task_tracker_for_candidates_);
504 }
505 } 593 }
506 } 594 }
507 595
596 void FaviconHandler::GetFaviconAndUpdateMappingsUnlessIncognito(
597 const GURL& icon_url,
598 favicon_base::IconType icon_type,
599 const favicon_base::FaviconResultsCallback& callback) {
600 // We don't know the favicon, but we may have previously downloaded the
601 // favicon for another page that shares the same favicon. Ask for the
602 // favicon given the favicon URL.
603 if (delegate_->IsOffTheRecord()) {
604 service_->GetFavicon(icon_url, icon_type, preferred_icon_size(), callback,
605 &cancelable_task_tracker_for_candidates_);
606 } else {
607 // Ask the history service for the icon. This does two things:
608 // 1. Attempts to fetch the favicon data from the database.
609 // 2. If the favicon exists in the database, this updates the database to
610 // include the mapping between the page url and the favicon url.
611 // This is asynchronous. The history service will call back when done.
612 service_->UpdateFaviconMappingsAndFetch(
613 url_, icon_url, icon_type, preferred_icon_size(), callback,
614 &cancelable_task_tracker_for_candidates_);
615 }
616 }
617
508 void FaviconHandler::OnFaviconData(const std::vector< 618 void FaviconHandler::OnFaviconData(const std::vector<
509 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) { 619 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) {
510 bool has_valid_result = HasValidResult(favicon_bitmap_results); 620 bool has_valid_result = HasValidResult(favicon_bitmap_results);
511 bool has_expired_or_incomplete_result = 621 bool has_expired_or_incomplete_result =
512 !has_valid_result || HasExpiredOrIncompleteResult(preferred_icon_size(), 622 !has_valid_result || HasExpiredOrIncompleteResult(preferred_icon_size(),
513 favicon_bitmap_results); 623 favicon_bitmap_results);
514 624
515 if (has_valid_result) { 625 if (has_valid_result) {
516 // There is a valid favicon. Notify any observers. It is useful to notify 626 // There is a valid favicon. Notify any observers. It is useful to notify
517 // the observers even if the favicon is expired or incomplete (incorrect 627 // the observers even if the favicon is expired or incomplete (incorrect
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
549 // A max bitmap size is specified to avoid receiving huge bitmaps in 659 // A max bitmap size is specified to avoid receiving huge bitmaps in
550 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() 660 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload()
551 // for more details about the max bitmap size. 661 // for more details about the max bitmap size.
552 const int download_id = 662 const int download_id =
553 delegate_->DownloadImage(image_url, GetMaximalIconSize(handler_type_), 663 delegate_->DownloadImage(image_url, GetMaximalIconSize(handler_type_),
554 image_download_request_.callback()); 664 image_download_request_.callback());
555 DCHECK_NE(download_id, 0); 665 DCHECK_NE(download_id, 0);
556 } 666 }
557 667
558 } // namespace favicon 668 } // namespace favicon
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698