web_contents_zoom_controller.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 ATOM_BROWSER_WEB_CONTENTS_ZOOM_CONTROLLER_H_
  5. #define ATOM_BROWSER_WEB_CONTENTS_ZOOM_CONTROLLER_H_
  6. #include <map>
  7. #include <string>
  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 atom {
  12. // Manages the zoom changes of WebContents.
  13. class WebContentsZoomController
  14. : public content::WebContentsObserver,
  15. public content::WebContentsUserData<WebContentsZoomController> {
  16. public:
  17. class Observer {
  18. public:
  19. virtual void OnZoomLevelChanged(content::WebContents* web_contents,
  20. double level,
  21. bool is_temporary) {}
  22. protected:
  23. virtual ~Observer() {}
  24. };
  25. // Defines how zoom changes are handled.
  26. enum ZoomMode {
  27. // Results in default zoom behavior, i.e. zoom changes are handled
  28. // automatically and on a per-origin basis, meaning that other tabs
  29. // navigated to the same origin will also zoom.
  30. ZOOM_MODE_DEFAULT,
  31. // Results in zoom changes being handled automatically, but on a per-tab
  32. // basis. Tabs in this zoom mode will not be affected by zoom changes in
  33. // other tabs, and vice versa.
  34. ZOOM_MODE_ISOLATED,
  35. // Overrides the automatic handling of zoom changes. The |onZoomChange|
  36. // event will still be dispatched, but the page will not actually be zoomed.
  37. // These zoom changes can be handled manually by listening for the
  38. // |onZoomChange| event. Zooming in this mode is also on a per-tab basis.
  39. ZOOM_MODE_MANUAL,
  40. // Disables all zooming in this tab. The tab will revert to the default
  41. // zoom level, and all attempted zoom changes will be ignored.
  42. ZOOM_MODE_DISABLED,
  43. };
  44. explicit WebContentsZoomController(content::WebContents* web_contents);
  45. ~WebContentsZoomController() override;
  46. void AddObserver(Observer* observer);
  47. void RemoveObserver(Observer* observer);
  48. void SetEmbedderZoomController(WebContentsZoomController* controller);
  49. // Methods for managing zoom levels.
  50. void SetZoomLevel(double level);
  51. double GetZoomLevel();
  52. void SetDefaultZoomFactor(double factor);
  53. double GetDefaultZoomFactor();
  54. void SetTemporaryZoomLevel(double level);
  55. bool UsesTemporaryZoomLevel();
  56. // Sets the zoom mode, which defines zoom behavior (see enum ZoomMode).
  57. void SetZoomMode(ZoomMode zoom_mode);
  58. void ResetZoomModeOnNavigationIfNeeded(const GURL& url);
  59. ZoomMode zoom_mode() const { return zoom_mode_; }
  60. // Convenience method to get default zoom level. Implemented here for
  61. // inlining.
  62. double GetDefaultZoomLevel() const {
  63. return content::HostZoomMap::GetForWebContents(web_contents())
  64. ->GetDefaultZoomLevel();
  65. }
  66. protected:
  67. // content::WebContentsObserver:
  68. void DidFinishNavigation(content::NavigationHandle* handle) override;
  69. void WebContentsDestroyed() override;
  70. void RenderFrameHostChanged(content::RenderFrameHost* old_host,
  71. content::RenderFrameHost* new_host) override;
  72. private:
  73. friend class content::WebContentsUserData<WebContentsZoomController>;
  74. // Called after a navigation has committed to set default zoom factor.
  75. void SetZoomFactorOnNavigationIfNeeded(const GURL& url);
  76. // The current zoom mode.
  77. ZoomMode zoom_mode_ = ZOOM_MODE_DEFAULT;
  78. // Current zoom level.
  79. double zoom_level_ = 1.0;
  80. // kZoomFactor.
  81. double default_zoom_factor_ = 0;
  82. int old_process_id_ = -1;
  83. int old_view_id_ = -1;
  84. WebContentsZoomController* embedder_zoom_controller_ = nullptr;
  85. base::ObserverList<Observer> observers_;
  86. content::HostZoomMap* host_zoom_map_;
  87. DISALLOW_COPY_AND_ASSIGN(WebContentsZoomController);
  88. };
  89. } // namespace atom
  90. #endif // ATOM_BROWSER_WEB_CONTENTS_ZOOM_CONTROLLER_H_