web_contents_zoom_controller.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright (c) 2017 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef ELECTRON_SHELL_BROWSER_WEB_CONTENTS_ZOOM_CONTROLLER_H_
  5. #define ELECTRON_SHELL_BROWSER_WEB_CONTENTS_ZOOM_CONTROLLER_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/observer_list.h"
  8. #include "content/public/browser/host_zoom_map.h"
  9. #include "content/public/browser/web_contents_observer.h"
  10. #include "content/public/browser/web_contents_user_data.h"
  11. namespace electron {
  12. class WebContentsZoomObserver;
  13. // Manages the zoom changes of WebContents.
  14. class WebContentsZoomController
  15. : private content::WebContentsObserver,
  16. public content::WebContentsUserData<WebContentsZoomController> {
  17. public:
  18. // Defines how zoom changes are handled.
  19. enum ZoomMode {
  20. // Results in default zoom behavior, i.e. zoom changes are handled
  21. // automatically and on a per-origin basis, meaning that other tabs
  22. // navigated to the same origin will also zoom.
  23. ZOOM_MODE_DEFAULT,
  24. // Results in zoom changes being handled automatically, but on a per-tab
  25. // basis. Tabs in this zoom mode will not be affected by zoom changes in
  26. // other tabs, and vice versa.
  27. ZOOM_MODE_ISOLATED,
  28. // Overrides the automatic handling of zoom changes. The |onZoomChange|
  29. // event will still be dispatched, but the page will not actually be zoomed.
  30. // These zoom changes can be handled manually by listening for the
  31. // |onZoomChange| event. Zooming in this mode is also on a per-tab basis.
  32. ZOOM_MODE_MANUAL,
  33. // Disables all zooming in this tab. The tab will revert to the default
  34. // zoom level, and all attempted zoom changes will be ignored.
  35. ZOOM_MODE_DISABLED,
  36. };
  37. struct ZoomChangedEventData {
  38. ZoomChangedEventData(content::WebContents* web_contents,
  39. double old_zoom_level,
  40. double new_zoom_level,
  41. bool temporary,
  42. WebContentsZoomController::ZoomMode zoom_mode)
  43. : web_contents(web_contents),
  44. old_zoom_level(old_zoom_level),
  45. new_zoom_level(new_zoom_level),
  46. temporary(temporary),
  47. zoom_mode(zoom_mode) {}
  48. raw_ptr<content::WebContents> web_contents;
  49. double old_zoom_level;
  50. double new_zoom_level;
  51. bool temporary;
  52. WebContentsZoomController::ZoomMode zoom_mode;
  53. };
  54. explicit WebContentsZoomController(content::WebContents* web_contents);
  55. ~WebContentsZoomController() override;
  56. // disable copy
  57. WebContentsZoomController(const WebContentsZoomController&) = delete;
  58. WebContentsZoomController& operator=(const WebContentsZoomController&) =
  59. delete;
  60. void AddObserver(WebContentsZoomObserver* observer);
  61. void RemoveObserver(WebContentsZoomObserver* observer);
  62. void SetEmbedderZoomController(WebContentsZoomController* controller);
  63. // Gets the current zoom level by querying HostZoomMap (if not in manual zoom
  64. // mode) or from the ZoomController local value otherwise.
  65. double GetZoomLevel() const;
  66. // Sets the zoom level through HostZoomMap.
  67. // Returns true on success.
  68. bool SetZoomLevel(double zoom_level);
  69. void SetDefaultZoomFactor(double factor);
  70. double default_zoom_factor() { return default_zoom_factor_; }
  71. // Sets the temporary zoom level through HostZoomMap.
  72. void SetTemporaryZoomLevel(double level);
  73. bool UsesTemporaryZoomLevel();
  74. // Sets the zoom mode, which defines zoom behavior (see enum ZoomMode).
  75. void SetZoomMode(ZoomMode zoom_mode);
  76. ZoomMode zoom_mode() const { return zoom_mode_; }
  77. // Convenience method to get default zoom level. Implemented here for
  78. // inlining.
  79. double GetDefaultZoomLevel() const {
  80. return content::HostZoomMap::GetForWebContents(web_contents())
  81. ->GetDefaultZoomLevel();
  82. }
  83. protected:
  84. // content::WebContentsObserver overrides:
  85. void DidFinishNavigation(
  86. content::NavigationHandle* navigation_handle) override;
  87. void WebContentsDestroyed() override;
  88. void RenderFrameHostChanged(content::RenderFrameHost* old_host,
  89. content::RenderFrameHost* new_host) override;
  90. private:
  91. friend class content::WebContentsUserData<WebContentsZoomController>;
  92. void ResetZoomModeOnNavigationIfNeeded(const GURL& url);
  93. void SetZoomFactorOnNavigationIfNeeded(const GURL& url);
  94. void OnZoomLevelChanged(const content::HostZoomMap::ZoomLevelChange& change);
  95. // Updates the zoom icon and zoom percentage based on current values and
  96. // notifies the observer if changes have occurred. |host| may be empty,
  97. // meaning the change should apply to ~all sites. If it is not empty, the
  98. // change only affects sites with the given host.
  99. void UpdateState(const std::string& host);
  100. // The current zoom mode.
  101. ZoomMode zoom_mode_ = ZOOM_MODE_DEFAULT;
  102. // The current zoom level.
  103. double zoom_level_;
  104. // The current default zoom factor.
  105. double default_zoom_factor_;
  106. int old_process_id_ = -1;
  107. int old_view_id_ = -1;
  108. std::unique_ptr<ZoomChangedEventData> event_data_;
  109. raw_ptr<WebContentsZoomController> embedder_zoom_controller_ = nullptr;
  110. // Observer receiving notifications on state changes.
  111. base::ObserverList<WebContentsZoomObserver> observers_;
  112. // Keep track of the HostZoomMap we're currently subscribed to.
  113. raw_ptr<content::HostZoomMap> host_zoom_map_;
  114. base::CallbackListSubscription zoom_subscription_;
  115. WEB_CONTENTS_USER_DATA_KEY_DECL();
  116. };
  117. } // namespace electron
  118. #endif // ELECTRON_SHELL_BROWSER_WEB_CONTENTS_ZOOM_CONTROLLER_H_