electron/shell/browser/web_contents_zoom_controller.cc
electron-roller[bot] b0f315a637
chore: bump chromium to 99.0.4767.0 (main) (#31986)
* chore: bump chromium in DEPS to 98.0.4726.0

* 3292117: Remove unneeded base/compiler_specific.h includes in //chrome.

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

* 3289198: Enables calculating line, word and sentence boundaries on the browser

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

* 3276176: Remove expired gdi-text-printing flag and associated code.

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

* 3240963: content: allow embedder to prevent locking scheme registry

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

* 3269899: Rename WebContentsImpl::GetFrameTree to GetPrimaryFrameTree

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

* chore: fixup patch indices

* 3276279: Enable -Wshadow by default for the "chromium code" config.

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

* 3279737: appcache: Remove WebPreference/WebSetting

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

* 3275564: [api] Advance API deprecation for APIs last marked in v9.6

https://chromium-review.googlesource.com/c/v8/v8/+/3275564

* 3261873: Clean up WebScriptSource constructors

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

* 3279346: appcache: Remove ConsoleMessage appcache field

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

* 3264212: Move legacy file loading to legacy_test_runner

https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/3264212

Both Persistence and UI have been removed from globals, but the issues they
seemed to be patching are no longer reproducible from what I can tell, and
so we can just delete these and re-evaluate if something surfaces.

* 3290415: x11: remove the USE_X11 define.

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

* chore: bump Chromium to 98.0.4728.0

* 3179530: Defer system calls in PrintingContext for OOP printing

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

* 3299445: Consolidate is_win conditionals in chrome/test/BUILD.gn.

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

* chore: update patch indices

* 3223975: Break PrintJobWorker OOP logic into separate class

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

* chore: bump chromium in DEPS to 98.0.4730.0

* 3279001: Remove support for font-family: -webkit-pictograph

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

* chore: fixup patch indices

* chore: bump chromium in DEPS to 98.0.4732.0

* chore: update patches

* chore: bump chromium in DEPS to 98.0.4734.0

* chore: bump chromium in DEPS to 98.0.4736.0

* chore: update patches

* chore: update printing patch for miracle ptr

* chore: add noexcept to fix clang error

* chore: bump chromium in DEPS to 98.0.4738.0

* chore: update patches

* chore: bump chromium in DEPS to 98.0.4740.0

* chore: bump chromium in DEPS to 98.0.4742.0

* chore: bump chromium in DEPS to 98.0.4744.0

* chore: bump chromium in DEPS to 98.0.4746.0

* chore: bump chromium in DEPS to 98.0.4748.0

* chore: bump chromium in DEPS to 98.0.4750.0

* chore: update patches

* 3293841: Remove File Handling permissions code

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

* chore: update patches

3311700: Move the PpapiPluginSandboxedProcessLauncherDelegate | https://chromium-review.googlesource.com/c/chromium/src/+/3311700

* 3289260: [CodeHealth]: Remove uses of Notification Service

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

* 3301600: Disable scripted print in fenced frames

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

* chore: add missing thread_restrictions headers

* 3305132: Rewrite most `Foo* field_` pointer fields to `raw_ptr<Foo> field_`.

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

* fix: add ppapi_sandbox header for linux

3311700: Move the PpapiPluginSandboxedProcessLauncherDelegate | https://chromium-review.googlesource.com/c/chromium/src/+/3311700

* chore: manually bump chromium in DEPS to 98.0.4757.0

* chore: update patches

* 3321044: Remove DictionaryValue::Clear()
 Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3321044

* chore: update printing.patch
Refs:
- 3304556: [code health] Remove notification observation from PrintJob. | https://chromium-review.googlesource.com/c/chromium/src/+/3304556
- 3305095: [code health] Remove NotificationService from PrintViewManagerBase. | https://chromium-review.googlesource.com/c/chromium/src/+/3305095

* build: add v8-embedder-state headers to GN patch

* chore: bump chromium in DEPS to 99.0.4767.0

* chore: update patches

* chore: rename CookiePartitionKeychain

...to CookiePartitionKeyCollection

* chore: update video consumers

* refactor: use newer base::Value API

* 3232598: Convert net::DnsOverHttpsServerConfig into a class | https://chromium-review.googlesource.com/c/chromium/src/+/3232598

* 3327865: Remove the default WebContentsUserData ctor. | https://chromium-review.googlesource.com/c/chromium/src/+/3327865

* 3302814: DevTools: Add getPreference binding | https://chromium-review.googlesource.com/c/chromium/src/+/3302814

* 3301474: [tq][runtime] Use build flags for JS context promise hooks | https://chromium-review.googlesource.com/c/v8/v8/+/3301474

* oops 😵‍💫

* 3272411: Reland "base/allocator: Enable PartitionAlloc-Everywhere on macOS" | https://chromium-review.googlesource.com/c/chromium/src/+/3272411

build: turn PartitionAlloc back off on mac for now

* fix: WCO method got renamed

* 3344749: Revert "Stop using NSRunLoop in renderer process"

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

* 3288746: [serial] Fix BluetoothSerialDeviceEnumerator threading issues.

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

* Revert "3288746: [serial] Fix BluetoothSerialDeviceEnumerator threading issues."

This reverts commit 5cc69f102e43ca72ac9ef45063711bcc7d849740.

* chore: disable serial device enumerator sequence dcheck

* fix: comment out line in DeviceService dtor

* fixup! 3279001: Remove support for font-family: -webkit-pictograph

* fixup! 3279346: appcache: Remove ConsoleMessage appcache field

* chore: update patches after rebase

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com>
Co-authored-by: Samuel Attard <samuel.r.attard@gmail.com>
Co-authored-by: VerteDinde <khammond@slack-corp.com>
Co-authored-by: clavin <clavin@electronjs.org>
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
Co-authored-by: Jeremy Rose <jeremya@chromium.org>
2022-01-10 17:31:39 -05:00

287 lines
11 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()->GetRenderViewHost()->IsRenderViewLive() ||
blink::PageZoomValuesEqual(GetZoomLevel(), level) ||
zoom_mode_ == ZoomMode::kDisabled)
return;
int render_process_id =
web_contents()->GetRenderViewHost()->GetProcess()->GetID();
int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID();
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(render_process_id, render_view_id)) {
zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_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) {
old_process_id_ = web_contents()->GetRenderViewHost()->GetProcess()->GetID();
old_view_id_ = web_contents()->GetRenderViewHost()->GetRoutingID();
host_zoom_map_->SetTemporaryZoomLevel(old_process_id_, old_view_id_, level);
// Notify observers of zoom level changes.
for (Observer& observer : observers_)
observer.OnZoomLevelChanged(web_contents(), level, true);
}
bool WebContentsZoomController::UsesTemporaryZoomLevel() {
int render_process_id =
web_contents()->GetRenderViewHost()->GetProcess()->GetID();
int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID();
return host_zoom_map_->UsesTemporaryZoomLevel(render_process_id,
render_view_id);
}
void WebContentsZoomController::SetZoomMode(ZoomMode new_mode) {
if (new_mode == zoom_mode_)
return;
content::HostZoomMap* zoom_map =
content::HostZoomMap::GetForWebContents(web_contents());
int render_process_id =
web_contents()->GetRenderViewHost()->GetProcess()->GetID();
int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID();
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(render_process_id, render_view_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(render_process_id, render_view_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(render_process_id, render_view_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(render_process_id, render_view_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(render_process_id, render_view_id,
GetDefaultZoomLevel());
break;
}
}
zoom_mode_ = new_mode;
}
void WebContentsZoomController::ResetZoomModeOnNavigationIfNeeded(
const GURL& url) {
if (zoom_mode_ != ZoomMode::kIsolated && zoom_mode_ != ZoomMode::kManual)
return;
int render_process_id =
web_contents()->GetRenderViewHost()->GetProcess()->GetID();
int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID();
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(render_process_id, render_view_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;
if (host_zoom_map_->UsesTemporaryZoomLevel(old_process_id_, old_view_id_)) {
host_zoom_map_->ClearTemporaryZoomLevel(old_process_id_, old_view_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