favicon_tab_helper.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2020 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 WEBLAYER_BROWSER_FAVICON_FAVICON_TAB_HELPER_H_
  5. #define WEBLAYER_BROWSER_FAVICON_FAVICON_TAB_HELPER_H_
  6. #include <memory>
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/observer_list.h"
  9. #include "components/favicon/core/favicon_driver_observer.h"
  10. #include "content/public/browser/web_contents_observer.h"
  11. #include "content/public/browser/web_contents_user_data.h"
  12. #include "ui/gfx/image/image.h"
  13. namespace weblayer {
  14. class FaviconFetcherDelegate;
  15. // FaviconTabHelper is responsible for creating favicon::ContentFaviconDriver
  16. // when necessary. FaviconTabHelper is used by FaviconFetcherImpl and notifies
  17. // FaviconFetcherDelegate when the favicon changes.
  18. class FaviconTabHelper : public content::WebContentsUserData<FaviconTabHelper>,
  19. public content::WebContentsObserver,
  20. public favicon::FaviconDriverObserver {
  21. public:
  22. // Used to track calls to RegisterFaviconFetcherDelegate(). When destroyed
  23. // the FaviconFetcherDelegate is removed.
  24. class ObserverSubscription {
  25. public:
  26. ObserverSubscription(const ObserverSubscription&) = delete;
  27. ObserverSubscription& operator=(const ObserverSubscription&) = delete;
  28. ~ObserverSubscription();
  29. private:
  30. friend class FaviconTabHelper;
  31. ObserverSubscription(FaviconTabHelper* helper,
  32. FaviconFetcherDelegate* delegate);
  33. raw_ptr<FaviconTabHelper> helper_;
  34. raw_ptr<FaviconFetcherDelegate> delegate_;
  35. };
  36. FaviconTabHelper(const FaviconTabHelper&) = delete;
  37. FaviconTabHelper& operator=(const FaviconTabHelper&) = delete;
  38. ~FaviconTabHelper() override;
  39. // Called when FaviconFetcherImpl is created. This ensures the necessary
  40. // wiring is in place and notifies |delegate| when the favicon changes.
  41. std::unique_ptr<ObserverSubscription> RegisterFaviconFetcherDelegate(
  42. FaviconFetcherDelegate* delegate);
  43. // Returns the favicon for the current navigation.
  44. const gfx::Image& favicon() const { return favicon_; }
  45. private:
  46. friend class content::WebContentsUserData<FaviconTabHelper>;
  47. explicit FaviconTabHelper(content::WebContents* contents);
  48. void AddDelegate(FaviconFetcherDelegate* delegate);
  49. void RemoveDelegate(FaviconFetcherDelegate* delegate);
  50. // favicon::FaviconDriverObserver:
  51. void OnFaviconUpdated(favicon::FaviconDriver* favicon_driver,
  52. NotificationIconType notification_icon_type,
  53. const GURL& icon_url,
  54. bool icon_url_changed,
  55. const gfx::Image& image) override;
  56. // content::WebContentsObserver:
  57. void PrimaryPageChanged(content::Page& page) override;
  58. raw_ptr<content::WebContents> web_contents_;
  59. // Number of observers attached.
  60. int observer_count_ = 0;
  61. base::ObserverList<FaviconFetcherDelegate> delegates_;
  62. gfx::Image favicon_;
  63. WEB_CONTENTS_USER_DATA_KEY_DECL();
  64. };
  65. } // namespace weblayer
  66. #endif // WEBLAYER_BROWSER_FAVICON_FAVICON_TAB_HELPER_H_