electron/shell/browser/web_contents_zoom_controller.cc
electron-roller[bot] 87c183df6a
chore: bump chromium to 110.0.5415.0 (main) (#36186)
* chore: bump chromium in DEPS to 109.0.5386.0

* chore: bump chromium in DEPS to 109.0.5388.0

* chore: bump chromium in DEPS to 109.0.5390.0

* chore: bump chromium in DEPS to 109.0.5392.0

* chore: bump chromium in DEPS to 109.0.5394.0

* chore: bump chromium in DEPS to 109.0.5396.0

* chore: bump chromium in DEPS to 109.0.5398.0

* chore: bump chromium in DEPS to 109.0.5400.0

* chore: update galactus

* chore: bump chromium in DEPS to 109.0.5402.0

* chore: bump chromium in DEPS to 109.0.5403.0

* chore: bump chromium in DEPS to 109.0.5406.0

* chore: update patches

* 4004247: Delete unused DocumentWebContentsDelegate

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/4004247

* chore: bump chromium in DEPS to 109.0.5408.1

* chore: update patches

* 3949284: Support pkey debug mode without pkey 0 access

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3949284

* chore: bump chromium in DEPS to 109.0.5410.0

* chore: update patches

* 4000944: [Extensions] Create an API directory in //chrome/renderer/extensions

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/4000944

* 3988524: Remove DocumentOverlayWindowViews | https://chromium-review.googlesource.com/c/chromium/src/+/3988524

Co-authored-by: George Xu <33054982+georgexu99@users.noreply.github.com>

* chore: bump chromium in DEPS to 109.0.5412.0

* chore: update patches

* 3984022: Add AddChildWindowToBrowser to DisplayClient mojo interface

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3984022

* 3957079: Delete the CryptoToken component extension and internal API

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3957079

* 4004421: Migreate ScopedAllowIO to ScopedAllowBlocking

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/4004421

Co-authored-by: George Xu <georgexu99@users.noreply.github.com>

* chore: bump chromium in DEPS to 109.0.5414.0

* chore: update patches

* 4016180: Split PPAPI Mojo interfaces out of RenderFrameHostImpl.

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/4016180

* 3970838: [MPArch] Convert HostZoomMap and ZoomController off of RenderViewHost ids

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3970838

* 3997795: Don't add Chromium as a login item

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3997795

* 3993482: Remove RefCountedString::TakeString

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3993482

* 3990749: Allow forward-declared sources in base::ScopedObservation<>

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3990749

* fixup! 3957079: Delete the CryptoToken component extension and internal API

* chore: bump chromium in DEPS to 110.0.5415.0

* 3883790: Move devtools_frame_token to the RenderFrameHost, to preserve RFH identity across MPArch activations.

https://chromium-review.googlesource.com/c/chromium/src/+/3883790

* 4022205: Move license tooling into //tools/licenses

https://chromium-review.googlesource.com/c/chromium/src/+/4022205

* chore: fixup patch indices

* fixup! 3957079: Delete the CryptoToken component extension and internal API

* 4008687: Finish ScopedAllowIO migration

https://chromium-review.googlesource.com/c/chromium/src/+/4008687

* 3991548: Move WindowButtonOrderObserver and WindowFrameAction to LinuxUi

https://chromium-review.googlesource.com/c/chromium/src/+/3991548

* fixup! 3984022: Add AddChildWindowToBrowser to DisplayClient mojo interface

* 4016595: Migrate non-default ScopedObservation<> instantiations to ScopedObservationTraits<> in chrome/browser/

https://chromium-review.googlesource.com/c/chromium/src/+/4016595

* 4000481: Rename :chromedriver to :chromedriver_server

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/4000481

* 4008687: Finish ScopedAllowIO migration

https://chromium-review.googlesource.com/c/chromium/src/+/4008687

* 3988524: Remove DocumentOverlayWindowViews

https://chromium-review.googlesource.com/c/chromium/src/+/3988524

* fixup! 3997795: Don't add Chromium as a login item

* chore: fixup patches

* 3996872: Plumb input event task runner to EventFactoryEvdev

https://chromium-review.googlesource.com/c/chromium/src/+/3996872

* 4014994: Enable SiteIsolationForGuests by default.

https://chromium-review.googlesource.com/c/chromium/src/+/4014994

* chore: adopt new thread restrictions api for //electron (#36357)

chore: add thread blocking api

* fixup! 4014994: Enable SiteIsolationForGuests by default.

* pull parent HWND for dialogs on ui thread

* chore: set parent_window in MessageBoxSettings

* chore: remove redundant patch

* chore: revert accidental deletion

* chore: update patches

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
Co-authored-by: Samuel Attard <sattard@salesforce.com>
Co-authored-by: Keeley Hammond <khammond@slack-corp.com>
Co-authored-by: VerteDinde <vertedinde@electronjs.org>
Co-authored-by: George Xu <33054982+georgexu99@users.noreply.github.com>
Co-authored-by: George Xu <georgexu99@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
Co-authored-by: Robo <hop2deep@gmail.com>
Co-authored-by: Jeremy Rose <jeremya@chromium.org>
Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com>
2022-11-17 14:59:23 -05:00

281 lines
10 KiB
C++

// Copyright (c) 2017 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/web_contents_zoom_controller.h"
#include <string>
#include "content/public/browser/navigation_details.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_user_data.h"
#include "content/public/common/page_type.h"
#include "net/base/url_util.h"
#include "third_party/blink/public/common/page/page_zoom.h"
namespace electron {
WebContentsZoomController::WebContentsZoomController(
content::WebContents* web_contents)
: content::WebContentsObserver(web_contents),
content::WebContentsUserData<WebContentsZoomController>(*web_contents) {
default_zoom_factor_ = kPageZoomEpsilon;
host_zoom_map_ = content::HostZoomMap::GetForWebContents(web_contents);
}
WebContentsZoomController::~WebContentsZoomController() = default;
void WebContentsZoomController::AddObserver(
WebContentsZoomController::Observer* observer) {
observers_.AddObserver(observer);
}
void WebContentsZoomController::RemoveObserver(
WebContentsZoomController::Observer* observer) {
observers_.RemoveObserver(observer);
}
void WebContentsZoomController::SetEmbedderZoomController(
WebContentsZoomController* controller) {
embedder_zoom_controller_ = controller;
}
void WebContentsZoomController::SetZoomLevel(double level) {
if (!web_contents()->GetPrimaryMainFrame()->IsRenderFrameLive() ||
blink::PageZoomValuesEqual(GetZoomLevel(), level) ||
zoom_mode_ == ZoomMode::kDisabled)
return;
content::GlobalRenderFrameHostId rfh_id =
web_contents()->GetPrimaryMainFrame()->GetGlobalId();
if (zoom_mode_ == ZoomMode::kManual) {
zoom_level_ = level;
for (Observer& observer : observers_)
observer.OnZoomLevelChanged(web_contents(), level, true);
return;
}
content::HostZoomMap* zoom_map =
content::HostZoomMap::GetForWebContents(web_contents());
if (zoom_mode_ == ZoomMode::kIsolated ||
zoom_map->UsesTemporaryZoomLevel(rfh_id)) {
zoom_map->SetTemporaryZoomLevel(rfh_id, level);
// Notify observers of zoom level changes.
for (Observer& observer : observers_)
observer.OnZoomLevelChanged(web_contents(), level, true);
} else {
content::HostZoomMap::SetZoomLevel(web_contents(), level);
// Notify observers of zoom level changes.
for (Observer& observer : observers_)
observer.OnZoomLevelChanged(web_contents(), level, false);
}
}
double WebContentsZoomController::GetZoomLevel() {
return zoom_mode_ == ZoomMode::kManual
? zoom_level_
: content::HostZoomMap::GetZoomLevel(web_contents());
}
void WebContentsZoomController::SetDefaultZoomFactor(double factor) {
default_zoom_factor_ = factor;
}
double WebContentsZoomController::GetDefaultZoomFactor() {
return default_zoom_factor_;
}
void WebContentsZoomController::SetTemporaryZoomLevel(double level) {
content::GlobalRenderFrameHostId old_rfh_id_ =
web_contents()->GetPrimaryMainFrame()->GetGlobalId();
host_zoom_map_->SetTemporaryZoomLevel(old_rfh_id_, level);
// Notify observers of zoom level changes.
for (Observer& observer : observers_)
observer.OnZoomLevelChanged(web_contents(), level, true);
}
bool WebContentsZoomController::UsesTemporaryZoomLevel() {
content::GlobalRenderFrameHostId rfh_id =
web_contents()->GetPrimaryMainFrame()->GetGlobalId();
return host_zoom_map_->UsesTemporaryZoomLevel(rfh_id);
}
void WebContentsZoomController::SetZoomMode(ZoomMode new_mode) {
if (new_mode == zoom_mode_)
return;
content::HostZoomMap* zoom_map =
content::HostZoomMap::GetForWebContents(web_contents());
content::GlobalRenderFrameHostId rfh_id =
web_contents()->GetPrimaryMainFrame()->GetGlobalId();
double original_zoom_level = GetZoomLevel();
switch (new_mode) {
case ZoomMode::kDefault: {
content::NavigationEntry* entry =
web_contents()->GetController().GetLastCommittedEntry();
if (entry) {
GURL url = content::HostZoomMap::GetURLFromEntry(entry);
std::string host = net::GetHostOrSpecFromURL(url);
if (zoom_map->HasZoomLevel(url.scheme(), host)) {
// If there are other tabs with the same origin, then set this tab's
// zoom level to match theirs. The temporary zoom level will be
// cleared below, but this call will make sure this tab re-draws at
// the correct zoom level.
double origin_zoom_level =
zoom_map->GetZoomLevelForHostAndScheme(url.scheme(), host);
zoom_map->SetTemporaryZoomLevel(rfh_id, origin_zoom_level);
} else {
// The host will need a level prior to removing the temporary level.
// We don't want the zoom level to change just because we entered
// default mode.
zoom_map->SetZoomLevelForHost(host, original_zoom_level);
}
}
// Remove per-tab zoom data for this tab. No event callback expected.
zoom_map->ClearTemporaryZoomLevel(rfh_id);
break;
}
case ZoomMode::kIsolated: {
// Unless the zoom mode was |ZoomMode::kDisabled| before this call, the
// page needs an initial isolated zoom back to the same level it was at
// in the other mode.
if (zoom_mode_ != ZoomMode::kDisabled) {
zoom_map->SetTemporaryZoomLevel(rfh_id, original_zoom_level);
} else {
// When we don't call any HostZoomMap set functions, we send the event
// manually.
for (Observer& observer : observers_)
observer.OnZoomLevelChanged(web_contents(), original_zoom_level,
false);
}
break;
}
case ZoomMode::kManual: {
// Unless the zoom mode was |ZoomMode::kDisabled| before this call, the
// page needs to be resized to the default zoom. While in manual mode,
// the zoom level is handled independently.
if (zoom_mode_ != ZoomMode::kDisabled) {
zoom_map->SetTemporaryZoomLevel(rfh_id, GetDefaultZoomLevel());
zoom_level_ = original_zoom_level;
} else {
// When we don't call any HostZoomMap set functions, we send the event
// manually.
for (Observer& observer : observers_)
observer.OnZoomLevelChanged(web_contents(), original_zoom_level,
false);
}
break;
}
case ZoomMode::kDisabled: {
// The page needs to be zoomed back to default before disabling the zoom
zoom_map->SetTemporaryZoomLevel(rfh_id, GetDefaultZoomLevel());
break;
}
}
zoom_mode_ = new_mode;
}
void WebContentsZoomController::ResetZoomModeOnNavigationIfNeeded(
const GURL& url) {
if (zoom_mode_ != ZoomMode::kIsolated && zoom_mode_ != ZoomMode::kManual)
return;
content::GlobalRenderFrameHostId rfh_id =
web_contents()->GetPrimaryMainFrame()->GetGlobalId();
content::HostZoomMap* zoom_map =
content::HostZoomMap::GetForWebContents(web_contents());
zoom_level_ = zoom_map->GetDefaultZoomLevel();
double new_zoom_level = zoom_map->GetZoomLevelForHostAndScheme(
url.scheme(), net::GetHostOrSpecFromURL(url));
for (Observer& observer : observers_)
observer.OnZoomLevelChanged(web_contents(), new_zoom_level, false);
zoom_map->ClearTemporaryZoomLevel(rfh_id);
zoom_mode_ = ZoomMode::kDefault;
}
void WebContentsZoomController::DidFinishNavigation(
content::NavigationHandle* navigation_handle) {
if (!navigation_handle->IsInMainFrame() || !navigation_handle->HasCommitted())
return;
if (navigation_handle->IsErrorPage()) {
content::HostZoomMap::SendErrorPageZoomLevelRefresh(web_contents());
return;
}
ResetZoomModeOnNavigationIfNeeded(navigation_handle->GetURL());
SetZoomFactorOnNavigationIfNeeded(navigation_handle->GetURL());
}
void WebContentsZoomController::WebContentsDestroyed() {
for (Observer& observer : observers_)
observer.OnZoomControllerWebContentsDestroyed();
observers_.Clear();
embedder_zoom_controller_ = nullptr;
}
void WebContentsZoomController::RenderFrameHostChanged(
content::RenderFrameHost* old_host,
content::RenderFrameHost* new_host) {
// If our associated HostZoomMap changes, update our event subscription.
content::HostZoomMap* new_host_zoom_map =
content::HostZoomMap::GetForWebContents(web_contents());
if (new_host_zoom_map == host_zoom_map_)
return;
host_zoom_map_ = new_host_zoom_map;
}
void WebContentsZoomController::SetZoomFactorOnNavigationIfNeeded(
const GURL& url) {
if (blink::PageZoomValuesEqual(GetDefaultZoomFactor(), kPageZoomEpsilon))
return;
content::GlobalRenderFrameHostId old_rfh_id_ =
content::GlobalRenderFrameHostId(old_process_id_, old_view_id_);
if (host_zoom_map_->UsesTemporaryZoomLevel(old_rfh_id_)) {
host_zoom_map_->ClearTemporaryZoomLevel(old_rfh_id_);
}
if (embedder_zoom_controller_ &&
embedder_zoom_controller_->UsesTemporaryZoomLevel()) {
double level = embedder_zoom_controller_->GetZoomLevel();
SetTemporaryZoomLevel(level);
return;
}
// When kZoomFactor is available, it takes precedence over
// pref store values but if the host has zoom factor set explicitly
// then it takes precedence.
// pref store < kZoomFactor < setZoomLevel
std::string host = net::GetHostOrSpecFromURL(url);
std::string scheme = url.scheme();
double zoom_factor = GetDefaultZoomFactor();
double zoom_level = blink::PageZoomFactorToZoomLevel(zoom_factor);
if (host_zoom_map_->HasZoomLevel(scheme, host)) {
zoom_level = host_zoom_map_->GetZoomLevelForHostAndScheme(scheme, host);
}
if (blink::PageZoomValuesEqual(zoom_level, GetZoomLevel()))
return;
SetZoomLevel(zoom_level);
}
WEB_CONTENTS_USER_DATA_KEY_IMPL(WebContentsZoomController);
} // namespace electron