icon_helper.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #ifndef ANDROID_WEBVIEW_BROWSER_ICON_HELPER_H_
  5. #define ANDROID_WEBVIEW_BROWSER_ICON_HELPER_H_
  6. #include <stdint.h>
  7. #include <string>
  8. #include <unordered_set>
  9. #include "base/memory/raw_ptr.h"
  10. #include "content/public/browser/reload_type.h"
  11. #include "content/public/browser/web_contents_observer.h"
  12. #include "third_party/blink/public/mojom/favicon/favicon_url.mojom-forward.h"
  13. #include "url/gurl.h"
  14. class SkBitmap;
  15. namespace gfx {
  16. class Size;
  17. }
  18. namespace android_webview {
  19. // A helper that observes favicon changes for Webview.
  20. class IconHelper : public content::WebContentsObserver {
  21. public:
  22. class Listener {
  23. public:
  24. virtual bool ShouldDownloadFavicon(const GURL& icon_url) = 0;
  25. virtual void OnReceivedIcon(const GURL& icon_url,
  26. const SkBitmap& bitmap) = 0;
  27. virtual void OnReceivedTouchIconUrl(const std::string& url,
  28. const bool precomposed) = 0;
  29. protected:
  30. virtual ~Listener() {}
  31. };
  32. explicit IconHelper(content::WebContents* web_contents);
  33. IconHelper(const IconHelper&) = delete;
  34. IconHelper& operator=(const IconHelper&) = delete;
  35. ~IconHelper() override;
  36. void SetListener(Listener* listener);
  37. // From WebContentsObserver
  38. void DidUpdateFaviconURL(
  39. content::RenderFrameHost* render_frame_host,
  40. const std::vector<blink::mojom::FaviconURLPtr>& candidates) override;
  41. void DidStartNavigation(content::NavigationHandle* navigation) override;
  42. void DownloadFaviconCallback(
  43. int id,
  44. int http_status_code,
  45. const GURL& image_url,
  46. const std::vector<SkBitmap>& bitmaps,
  47. const std::vector<gfx::Size>& original_bitmap_sizes);
  48. private:
  49. void MarkUnableToDownloadFavicon(const GURL& icon_url);
  50. bool WasUnableToDownloadFavicon(const GURL& icon_url) const;
  51. void ClearUnableToDownloadFavicons();
  52. raw_ptr<Listener> listener_;
  53. using MissingFaviconURLHash = size_t;
  54. std::unordered_set<MissingFaviconURLHash> missing_favicon_urls_;
  55. };
  56. } // namespace android_webview
  57. #endif // ANDROID_WEBVIEW_BROWSER_ICON_HELPER_H_