2017-01-30 11:18:40 +00:00
|
|
|
// Copyright (c) 2017 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#ifndef ELECTRON_SHELL_BROWSER_WEB_CONTENTS_ZOOM_CONTROLLER_H_
|
|
|
|
#define ELECTRON_SHELL_BROWSER_WEB_CONTENTS_ZOOM_CONTROLLER_H_
|
2017-01-30 11:18:40 +00:00
|
|
|
|
2023-05-11 20:07:39 +00:00
|
|
|
#include "base/memory/raw_ptr.h"
|
2022-02-25 18:17:35 +00:00
|
|
|
#include "base/observer_list.h"
|
2017-01-30 11:18:40 +00:00
|
|
|
#include "content/public/browser/host_zoom_map.h"
|
|
|
|
#include "content/public/browser/web_contents_observer.h"
|
|
|
|
#include "content/public/browser/web_contents_user_data.h"
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2017-01-30 11:18:40 +00:00
|
|
|
|
2023-08-15 01:33:30 +00:00
|
|
|
class WebContentsZoomObserver;
|
|
|
|
|
2017-01-30 11:18:40 +00:00
|
|
|
// Manages the zoom changes of WebContents.
|
|
|
|
class WebContentsZoomController
|
2024-05-21 19:21:31 +00:00
|
|
|
: private content::WebContentsObserver,
|
2017-01-30 11:18:40 +00:00
|
|
|
public content::WebContentsUserData<WebContentsZoomController> {
|
|
|
|
public:
|
2017-06-08 00:13:49 +00:00
|
|
|
// Defines how zoom changes are handled.
|
2023-08-15 01:33:30 +00:00
|
|
|
enum ZoomMode {
|
2017-06-08 00:13:49 +00:00
|
|
|
// Results in default zoom behavior, i.e. zoom changes are handled
|
|
|
|
// automatically and on a per-origin basis, meaning that other tabs
|
|
|
|
// navigated to the same origin will also zoom.
|
2023-08-15 01:33:30 +00:00
|
|
|
ZOOM_MODE_DEFAULT,
|
2017-06-08 00:13:49 +00:00
|
|
|
// Results in zoom changes being handled automatically, but on a per-tab
|
|
|
|
// basis. Tabs in this zoom mode will not be affected by zoom changes in
|
|
|
|
// other tabs, and vice versa.
|
2023-08-15 01:33:30 +00:00
|
|
|
ZOOM_MODE_ISOLATED,
|
2017-06-08 00:13:49 +00:00
|
|
|
// Overrides the automatic handling of zoom changes. The |onZoomChange|
|
|
|
|
// event will still be dispatched, but the page will not actually be zoomed.
|
|
|
|
// These zoom changes can be handled manually by listening for the
|
|
|
|
// |onZoomChange| event. Zooming in this mode is also on a per-tab basis.
|
2023-08-15 01:33:30 +00:00
|
|
|
ZOOM_MODE_MANUAL,
|
2017-06-08 00:13:49 +00:00
|
|
|
// Disables all zooming in this tab. The tab will revert to the default
|
|
|
|
// zoom level, and all attempted zoom changes will be ignored.
|
2023-08-15 01:33:30 +00:00
|
|
|
ZOOM_MODE_DISABLED,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ZoomChangedEventData {
|
|
|
|
ZoomChangedEventData(content::WebContents* web_contents,
|
|
|
|
double old_zoom_level,
|
|
|
|
double new_zoom_level,
|
|
|
|
bool temporary,
|
|
|
|
WebContentsZoomController::ZoomMode zoom_mode)
|
|
|
|
: web_contents(web_contents),
|
|
|
|
old_zoom_level(old_zoom_level),
|
|
|
|
new_zoom_level(new_zoom_level),
|
|
|
|
temporary(temporary),
|
|
|
|
zoom_mode(zoom_mode) {}
|
|
|
|
raw_ptr<content::WebContents> web_contents;
|
|
|
|
double old_zoom_level;
|
|
|
|
double new_zoom_level;
|
|
|
|
bool temporary;
|
|
|
|
WebContentsZoomController::ZoomMode zoom_mode;
|
2017-06-08 00:13:49 +00:00
|
|
|
};
|
2017-01-30 11:18:40 +00:00
|
|
|
|
|
|
|
explicit WebContentsZoomController(content::WebContents* web_contents);
|
|
|
|
~WebContentsZoomController() override;
|
|
|
|
|
2021-11-03 11:41:45 +00:00
|
|
|
// disable copy
|
|
|
|
WebContentsZoomController(const WebContentsZoomController&) = delete;
|
|
|
|
WebContentsZoomController& operator=(const WebContentsZoomController&) =
|
|
|
|
delete;
|
|
|
|
|
2023-08-15 01:33:30 +00:00
|
|
|
void AddObserver(WebContentsZoomObserver* observer);
|
|
|
|
void RemoveObserver(WebContentsZoomObserver* observer);
|
2017-01-30 11:18:40 +00:00
|
|
|
|
2017-01-31 09:15:45 +00:00
|
|
|
void SetEmbedderZoomController(WebContentsZoomController* controller);
|
|
|
|
|
2023-08-15 01:33:30 +00:00
|
|
|
// Gets the current zoom level by querying HostZoomMap (if not in manual zoom
|
|
|
|
// mode) or from the ZoomController local value otherwise.
|
|
|
|
double GetZoomLevel() const;
|
|
|
|
|
|
|
|
// Sets the zoom level through HostZoomMap.
|
|
|
|
// Returns true on success.
|
|
|
|
bool SetZoomLevel(double zoom_level);
|
|
|
|
|
2017-01-30 11:18:40 +00:00
|
|
|
void SetDefaultZoomFactor(double factor);
|
2024-01-30 02:43:28 +00:00
|
|
|
double default_zoom_factor() { return default_zoom_factor_; }
|
2023-08-15 01:33:30 +00:00
|
|
|
|
|
|
|
// Sets the temporary zoom level through HostZoomMap.
|
2017-01-30 17:06:50 +00:00
|
|
|
void SetTemporaryZoomLevel(double level);
|
|
|
|
bool UsesTemporaryZoomLevel();
|
2017-01-30 11:18:40 +00:00
|
|
|
|
2017-06-08 00:13:49 +00:00
|
|
|
// Sets the zoom mode, which defines zoom behavior (see enum ZoomMode).
|
|
|
|
void SetZoomMode(ZoomMode zoom_mode);
|
2017-06-08 07:57:24 +00:00
|
|
|
|
2017-06-08 00:13:49 +00:00
|
|
|
ZoomMode zoom_mode() const { return zoom_mode_; }
|
|
|
|
|
|
|
|
// Convenience method to get default zoom level. Implemented here for
|
|
|
|
// inlining.
|
|
|
|
double GetDefaultZoomLevel() const {
|
|
|
|
return content::HostZoomMap::GetForWebContents(web_contents())
|
|
|
|
->GetDefaultZoomLevel();
|
|
|
|
}
|
|
|
|
|
2017-01-30 11:18:40 +00:00
|
|
|
protected:
|
2023-08-15 01:33:30 +00:00
|
|
|
// content::WebContentsObserver overrides:
|
|
|
|
void DidFinishNavigation(
|
|
|
|
content::NavigationHandle* navigation_handle) override;
|
2017-01-30 11:18:40 +00:00
|
|
|
void WebContentsDestroyed() override;
|
|
|
|
void RenderFrameHostChanged(content::RenderFrameHost* old_host,
|
|
|
|
content::RenderFrameHost* new_host) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class content::WebContentsUserData<WebContentsZoomController>;
|
|
|
|
|
2023-08-15 01:33:30 +00:00
|
|
|
void ResetZoomModeOnNavigationIfNeeded(const GURL& url);
|
2017-01-30 11:18:40 +00:00
|
|
|
void SetZoomFactorOnNavigationIfNeeded(const GURL& url);
|
2023-08-15 01:33:30 +00:00
|
|
|
void OnZoomLevelChanged(const content::HostZoomMap::ZoomLevelChange& change);
|
2017-01-30 11:18:40 +00:00
|
|
|
|
2023-08-15 01:33:30 +00:00
|
|
|
// Updates the zoom icon and zoom percentage based on current values and
|
|
|
|
// notifies the observer if changes have occurred. |host| may be empty,
|
|
|
|
// meaning the change should apply to ~all sites. If it is not empty, the
|
|
|
|
// change only affects sites with the given host.
|
|
|
|
void UpdateState(const std::string& host);
|
2017-06-08 00:13:49 +00:00
|
|
|
|
2023-08-15 01:33:30 +00:00
|
|
|
// The current zoom mode.
|
|
|
|
ZoomMode zoom_mode_ = ZOOM_MODE_DEFAULT;
|
2017-06-08 00:13:49 +00:00
|
|
|
|
2023-08-15 01:33:30 +00:00
|
|
|
// The current zoom level.
|
|
|
|
double zoom_level_;
|
2017-01-30 11:18:40 +00:00
|
|
|
|
2023-08-15 01:33:30 +00:00
|
|
|
// The current default zoom factor.
|
|
|
|
double default_zoom_factor_;
|
2019-10-18 19:57:34 +00:00
|
|
|
|
2018-05-21 22:18:38 +00:00
|
|
|
int old_process_id_ = -1;
|
|
|
|
int old_view_id_ = -1;
|
2017-01-31 09:15:45 +00:00
|
|
|
|
2023-08-15 01:33:30 +00:00
|
|
|
std::unique_ptr<ZoomChangedEventData> event_data_;
|
|
|
|
|
2023-05-11 20:07:39 +00:00
|
|
|
raw_ptr<WebContentsZoomController> embedder_zoom_controller_ = nullptr;
|
2017-01-31 09:15:45 +00:00
|
|
|
|
2023-08-15 01:33:30 +00:00
|
|
|
// Observer receiving notifications on state changes.
|
|
|
|
base::ObserverList<WebContentsZoomObserver> observers_;
|
2017-01-30 11:18:40 +00:00
|
|
|
|
2023-08-15 01:33:30 +00:00
|
|
|
// Keep track of the HostZoomMap we're currently subscribed to.
|
2023-05-11 20:07:39 +00:00
|
|
|
raw_ptr<content::HostZoomMap> host_zoom_map_;
|
2017-01-30 11:18:40 +00:00
|
|
|
|
2023-08-15 01:33:30 +00:00
|
|
|
base::CallbackListSubscription zoom_subscription_;
|
|
|
|
|
2019-01-21 16:56:54 +00:00
|
|
|
WEB_CONTENTS_USER_DATA_KEY_DECL();
|
2017-01-30 11:18:40 +00:00
|
|
|
};
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|
2017-01-30 11:18:40 +00:00
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#endif // ELECTRON_SHELL_BROWSER_WEB_CONTENTS_ZOOM_CONTROLLER_H_
|