icon_helper.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright (c) 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "android_webview/browser/icon_helper.h"
  5. #include "base/bind.h"
  6. #include "base/callback.h"
  7. #include "base/check_op.h"
  8. #include "base/hash/hash.h"
  9. #include "base/notreached.h"
  10. #include "components/favicon_base/select_favicon_frames.h"
  11. #include "content/public/browser/browser_thread.h"
  12. #include "content/public/browser/navigation_handle.h"
  13. #include "content/public/browser/web_contents.h"
  14. #include "third_party/blink/public/mojom/favicon/favicon_url.mojom.h"
  15. #include "third_party/skia/include/core/SkBitmap.h"
  16. #include "ui/gfx/geometry/size.h"
  17. using content::BrowserThread;
  18. using content::WebContents;
  19. namespace android_webview {
  20. namespace {
  21. const int kLargestIconSize = 192;
  22. } // namespace
  23. IconHelper::IconHelper(WebContents* web_contents)
  24. : WebContentsObserver(web_contents),
  25. listener_(nullptr),
  26. missing_favicon_urls_() {}
  27. IconHelper::~IconHelper() {
  28. }
  29. void IconHelper::SetListener(Listener* listener) {
  30. listener_ = listener;
  31. }
  32. void IconHelper::DownloadFaviconCallback(
  33. int id,
  34. int http_status_code,
  35. const GURL& image_url,
  36. const std::vector<SkBitmap>& bitmaps,
  37. const std::vector<gfx::Size>& original_bitmap_sizes) {
  38. DCHECK_CURRENTLY_ON(BrowserThread::UI);
  39. if (http_status_code == 404) {
  40. MarkUnableToDownloadFavicon(image_url);
  41. return;
  42. }
  43. if (bitmaps.size() == 0) {
  44. return;
  45. }
  46. // We can protentially have multiple frames of the icon
  47. // in different sizes. We need more fine grain API spec
  48. // to let clients pick out the frame they want.
  49. if (listener_) {
  50. std::vector<size_t> best_indices;
  51. SelectFaviconFrameIndices(original_bitmap_sizes,
  52. std::vector<int>(1U, kLargestIconSize),
  53. &best_indices, nullptr);
  54. listener_->OnReceivedIcon(
  55. image_url,
  56. bitmaps[best_indices.size() == 0 ? 0 : best_indices.front()]);
  57. }
  58. }
  59. void IconHelper::DidUpdateFaviconURL(
  60. content::RenderFrameHost* render_frame_host,
  61. const std::vector<blink::mojom::FaviconURLPtr>& candidates) {
  62. DCHECK_CURRENTLY_ON(BrowserThread::UI);
  63. for (const auto& candidate : candidates) {
  64. if (!candidate->icon_url.is_valid())
  65. continue;
  66. switch (candidate->icon_type) {
  67. case blink::mojom::FaviconIconType::kFavicon:
  68. if ((listener_ &&
  69. !listener_->ShouldDownloadFavicon(candidate->icon_url)) ||
  70. WasUnableToDownloadFavicon(candidate->icon_url)) {
  71. break;
  72. }
  73. web_contents()->DownloadImage(
  74. candidate->icon_url,
  75. true, // Is a favicon
  76. gfx::Size(), // No preferred size
  77. kLargestIconSize, // Max bitmap size
  78. false, // Normal cache policy
  79. base::BindOnce(&IconHelper::DownloadFaviconCallback,
  80. base::Unretained(this)));
  81. break;
  82. case blink::mojom::FaviconIconType::kTouchIcon:
  83. if (listener_)
  84. listener_->OnReceivedTouchIconUrl(candidate->icon_url.spec(), false);
  85. break;
  86. case blink::mojom::FaviconIconType::kTouchPrecomposedIcon:
  87. if (listener_)
  88. listener_->OnReceivedTouchIconUrl(candidate->icon_url.spec(), true);
  89. break;
  90. case blink::mojom::FaviconIconType::kInvalid:
  91. // Silently ignore it. Only trigger a callback on valid icons.
  92. break;
  93. default:
  94. NOTREACHED();
  95. break;
  96. }
  97. }
  98. }
  99. void IconHelper::DidStartNavigation(content::NavigationHandle* navigation) {
  100. if (navigation->IsInPrimaryMainFrame() &&
  101. navigation->GetReloadType() == content::ReloadType::BYPASSING_CACHE) {
  102. ClearUnableToDownloadFavicons();
  103. }
  104. }
  105. void IconHelper::MarkUnableToDownloadFavicon(const GURL& icon_url) {
  106. MissingFaviconURLHash url_hash = base::FastHash(icon_url.spec());
  107. missing_favicon_urls_.insert(url_hash);
  108. }
  109. bool IconHelper::WasUnableToDownloadFavicon(const GURL& icon_url) const {
  110. MissingFaviconURLHash url_hash = base::FastHash(icon_url.spec());
  111. return missing_favicon_urls_.find(url_hash) != missing_favicon_urls_.end();
  112. }
  113. void IconHelper::ClearUnableToDownloadFavicons() {
  114. missing_favicon_urls_.clear();
  115. }
  116. } // namespace android_webview