zoom_controller.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_ZOOM_ZOOM_CONTROLLER_H_
  5. #define COMPONENTS_ZOOM_ZOOM_CONTROLLER_H_
  6. #include <memory>
  7. #include "base/compiler_specific.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/observer_list.h"
  11. #include "components/prefs/pref_member.h"
  12. #include "content/public/browser/host_zoom_map.h"
  13. #include "content/public/browser/web_contents_observer.h"
  14. #include "content/public/browser/web_contents_user_data.h"
  15. #include "third_party/abseil-cpp/absl/types/optional.h"
  16. class ZoomControllerTest;
  17. namespace content {
  18. class WebContents;
  19. }
  20. namespace zoom {
  21. class ZoomObserver;
  22. class ZoomRequestClient : public base::RefCounted<ZoomRequestClient> {
  23. public:
  24. ZoomRequestClient() {}
  25. ZoomRequestClient(const ZoomRequestClient&) = delete;
  26. ZoomRequestClient& operator=(const ZoomRequestClient&) = delete;
  27. virtual bool ShouldSuppressBubble() const = 0;
  28. protected:
  29. virtual ~ZoomRequestClient() {}
  30. private:
  31. friend class base::RefCounted<ZoomRequestClient>;
  32. };
  33. // Per-tab class to manage zoom changes and the Omnibox zoom icon. Lives on the
  34. // UI thread.
  35. class ZoomController : public content::WebContentsObserver,
  36. public content::WebContentsUserData<ZoomController> {
  37. public:
  38. // Defines how zoom changes are handled.
  39. enum ZoomMode {
  40. // Results in default zoom behavior, i.e. zoom changes are handled
  41. // automatically and on a per-origin basis, meaning that other tabs
  42. // navigated to the same origin will also zoom.
  43. ZOOM_MODE_DEFAULT,
  44. // Results in zoom changes being handled automatically, but on a per-tab
  45. // basis. Tabs in this zoom mode will not be affected by zoom changes in
  46. // other tabs, and vice versa.
  47. ZOOM_MODE_ISOLATED,
  48. // Overrides the automatic handling of zoom changes. The |onZoomChange|
  49. // event will still be dispatched, but the page will not actually be zoomed.
  50. // These zoom changes can be handled manually by listening for the
  51. // |onZoomChange| event. Zooming in this mode is also on a per-tab basis.
  52. ZOOM_MODE_MANUAL,
  53. // Disables all zooming in this tab. The tab will revert to the default
  54. // zoom level, and all attempted zoom changes will be ignored.
  55. ZOOM_MODE_DISABLED,
  56. };
  57. enum RelativeZoom {
  58. ZOOM_BELOW_DEFAULT_ZOOM,
  59. ZOOM_AT_DEFAULT_ZOOM,
  60. ZOOM_ABOVE_DEFAULT_ZOOM
  61. };
  62. struct ZoomChangedEventData {
  63. ZoomChangedEventData(content::WebContents* web_contents,
  64. double old_zoom_level,
  65. double new_zoom_level,
  66. ZoomController::ZoomMode zoom_mode,
  67. bool can_show_bubble)
  68. : web_contents(web_contents),
  69. old_zoom_level(old_zoom_level),
  70. new_zoom_level(new_zoom_level),
  71. zoom_mode(zoom_mode),
  72. can_show_bubble(can_show_bubble) {}
  73. content::WebContents* web_contents;
  74. double old_zoom_level;
  75. double new_zoom_level;
  76. ZoomController::ZoomMode zoom_mode;
  77. bool can_show_bubble;
  78. };
  79. // Since it's possible for a WebContents to not have a ZoomController, provide
  80. // a simple, safe and reliable method to find the current zoom level for a
  81. // given WebContents*.
  82. static double GetZoomLevelForWebContents(content::WebContents* web_contents);
  83. ZoomController(const ZoomController&) = delete;
  84. ZoomController& operator=(const ZoomController&) = delete;
  85. ~ZoomController() override;
  86. ZoomMode zoom_mode() const { return zoom_mode_; }
  87. // Convenience method to get default zoom level. Implemented here for
  88. // inlining.
  89. double GetDefaultZoomLevel() const {
  90. return content::HostZoomMap::GetForWebContents(web_contents())
  91. ->GetDefaultZoomLevel();
  92. }
  93. // Convenience method to quickly check if the tab's at default zoom.
  94. // Virtual for testing.
  95. virtual bool IsAtDefaultZoom() const;
  96. // Returns which image should be loaded for the current zoom level.
  97. RelativeZoom GetZoomRelativeToDefault() const;
  98. const ZoomRequestClient* last_client() const { return last_client_.get(); }
  99. void AddObserver(ZoomObserver* observer);
  100. void RemoveObserver(ZoomObserver* observer);
  101. // Used to set whether the zoom notification bubble can be shown when the
  102. // zoom level is changed for this controller. Default behavior is to show
  103. // the bubble.
  104. void SetShowsNotificationBubble(bool can_show_bubble) {
  105. can_show_bubble_ = can_show_bubble;
  106. }
  107. // Gets the current zoom level by querying HostZoomMap (if not in manual zoom
  108. // mode) or from the ZoomController local value otherwise.
  109. double GetZoomLevel() const;
  110. // Calls GetZoomLevel() then converts the returned value to a percentage
  111. // zoom factor.
  112. // Virtual for testing.
  113. virtual int GetZoomPercent() const;
  114. // Sets the zoom level through HostZoomMap.
  115. // Returns true on success.
  116. bool SetZoomLevel(double zoom_level);
  117. // Sets the zoom level via HostZoomMap (or stores it locally if in manual zoom
  118. // mode), and attributes the zoom to |client|. Returns true on success.
  119. bool SetZoomLevelByClient(
  120. double zoom_level,
  121. const scoped_refptr<const ZoomRequestClient>& client);
  122. // Sets the zoom mode, which defines zoom behavior (see enum ZoomMode).
  123. void SetZoomMode(ZoomMode zoom_mode);
  124. // Set and query whether or not the page scale factor is one.
  125. void SetPageScaleFactorIsOneForTesting(bool is_one);
  126. bool PageScaleFactorIsOne() const;
  127. // content::WebContentsObserver overrides:
  128. void DidFinishNavigation(
  129. content::NavigationHandle* navigation_handle) override;
  130. void WebContentsDestroyed() override;
  131. void RenderFrameHostChanged(content::RenderFrameHost* old_host,
  132. content::RenderFrameHost* new_host) override;
  133. void OnPageScaleFactorChanged(float page_scale_factor) override;
  134. protected:
  135. // Protected for testing.
  136. explicit ZoomController(content::WebContents* web_contents);
  137. private:
  138. friend class content::WebContentsUserData<ZoomController>;
  139. friend class ::ZoomControllerTest;
  140. void ResetZoomModeOnNavigationIfNeeded(const GURL& url);
  141. void OnZoomLevelChanged(const content::HostZoomMap::ZoomLevelChange& change);
  142. // Updates the zoom icon and zoom percentage based on current values and
  143. // notifies the observer if changes have occurred. |host| may be empty,
  144. // meaning the change should apply to ~all sites. If it is not empty, the
  145. // change only affects sites with the given host.
  146. void UpdateState(const std::string& host);
  147. // True if changes to zoom level can trigger the zoom notification bubble.
  148. bool can_show_bubble_ = true;
  149. // The current zoom mode.
  150. ZoomMode zoom_mode_ = ZOOM_MODE_DEFAULT;
  151. // Current zoom level.
  152. double zoom_level_;
  153. std::unique_ptr<ZoomChangedEventData> event_data_;
  154. // Keeps track of the extension (if any) that initiated the last zoom change
  155. // that took effect.
  156. scoped_refptr<const ZoomRequestClient> last_client_;
  157. // Observer receiving notifications on state changes.
  158. base::ObserverList<ZoomObserver>::Unchecked observers_;
  159. raw_ptr<content::BrowserContext> browser_context_;
  160. // Keep track of the HostZoomMap we're currently subscribed to.
  161. raw_ptr<content::HostZoomMap> host_zoom_map_;
  162. base::CallbackListSubscription zoom_subscription_;
  163. // Whether the page scale factor was one the last time we notified our
  164. // observers of a change to PageScaleFactorIsOne.
  165. bool last_page_scale_factor_was_one_ = true;
  166. // If set, this value is returned in PageScaleFactorIsOne.
  167. absl::optional<bool> page_scale_factor_is_one_for_testing_;
  168. WEB_CONTENTS_USER_DATA_KEY_DECL();
  169. };
  170. } // namespace zoom
  171. #endif // COMPONENTS_ZOOM_ZOOM_CONTROLLER_H_