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.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/web_contents_zoom_controller.h"
|
2017-01-30 11:18:40 +00:00
|
|
|
|
2021-06-01 01:34:44 +00:00
|
|
|
#include <string>
|
|
|
|
|
2023-08-15 01:33:30 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
2017-01-30 11:18:40 +00:00
|
|
|
#include "content/public/browser/navigation_details.h"
|
|
|
|
#include "content/public/browser/navigation_entry.h"
|
|
|
|
#include "content/public/browser/navigation_handle.h"
|
2017-12-18 02:46:23 +00:00
|
|
|
#include "content/public/browser/render_frame_host.h"
|
2017-01-30 11:18:40 +00:00
|
|
|
#include "content/public/browser/render_process_host.h"
|
|
|
|
#include "content/public/browser/render_view_host.h"
|
|
|
|
#include "content/public/browser/web_contents.h"
|
2022-01-10 22:31:39 +00:00
|
|
|
#include "content/public/browser/web_contents_user_data.h"
|
2017-01-30 11:18:40 +00:00
|
|
|
#include "content/public/common/page_type.h"
|
|
|
|
#include "net/base/url_util.h"
|
2023-08-15 01:33:30 +00:00
|
|
|
#include "shell/browser/web_contents_zoom_observer.h"
|
2019-10-18 19:57:34 +00:00
|
|
|
#include "third_party/blink/public/common/page/page_zoom.h"
|
2017-01-30 11:18:40 +00:00
|
|
|
|
2023-08-15 01:33:30 +00:00
|
|
|
using content::BrowserThread;
|
|
|
|
|
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
|
|
|
namespace {
|
|
|
|
|
|
|
|
const double kPageZoomEpsilon = 0.001;
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2017-01-30 11:18:40 +00:00
|
|
|
WebContentsZoomController::WebContentsZoomController(
|
|
|
|
content::WebContents* web_contents)
|
2022-01-10 22:31:39 +00:00
|
|
|
: content::WebContentsObserver(web_contents),
|
|
|
|
content::WebContentsUserData<WebContentsZoomController>(*web_contents) {
|
2023-08-15 01:33:30 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
2017-01-30 11:18:40 +00:00
|
|
|
host_zoom_map_ = content::HostZoomMap::GetForWebContents(web_contents);
|
2023-08-15 01:33:30 +00:00
|
|
|
zoom_level_ = host_zoom_map_->GetDefaultZoomLevel();
|
|
|
|
default_zoom_factor_ = kPageZoomEpsilon;
|
|
|
|
|
|
|
|
zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback(
|
|
|
|
base::BindRepeating(&WebContentsZoomController::OnZoomLevelChanged,
|
|
|
|
base::Unretained(this)));
|
|
|
|
|
|
|
|
UpdateState(std::string());
|
2017-01-30 11:18:40 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 01:33:30 +00:00
|
|
|
WebContentsZoomController::~WebContentsZoomController() {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
|
|
|
for (auto& observer : observers_) {
|
|
|
|
observer.OnZoomControllerDestroyed(this);
|
|
|
|
}
|
|
|
|
}
|
2017-01-30 11:18:40 +00:00
|
|
|
|
2023-08-15 01:33:30 +00:00
|
|
|
void WebContentsZoomController::AddObserver(WebContentsZoomObserver* observer) {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
2017-01-30 11:18:40 +00:00
|
|
|
observers_.AddObserver(observer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebContentsZoomController::RemoveObserver(
|
2023-08-15 01:33:30 +00:00
|
|
|
WebContentsZoomObserver* observer) {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
2017-01-30 11:18:40 +00:00
|
|
|
observers_.RemoveObserver(observer);
|
|
|
|
}
|
|
|
|
|
2017-01-31 09:15:45 +00:00
|
|
|
void WebContentsZoomController::SetEmbedderZoomController(
|
|
|
|
WebContentsZoomController* controller) {
|
2023-08-15 01:33:30 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
2017-01-31 09:15:45 +00:00
|
|
|
embedder_zoom_controller_ = controller;
|
|
|
|
}
|
|
|
|
|
2023-08-15 01:33:30 +00:00
|
|
|
bool WebContentsZoomController::SetZoomLevel(double level) {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
|
|
|
content::NavigationEntry* entry =
|
|
|
|
web_contents()->GetController().GetLastCommittedEntry();
|
2023-12-04 15:39:20 +00:00
|
|
|
|
2023-08-15 01:33:30 +00:00
|
|
|
// Cannot zoom in disabled mode. Also, don't allow changing zoom level on
|
|
|
|
// a crashed tab, an error page or an interstitial page.
|
|
|
|
if (zoom_mode_ == ZOOM_MODE_DISABLED ||
|
|
|
|
!web_contents()->GetPrimaryMainFrame()->IsRenderFrameLive())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Do not actually rescale the page in manual mode.
|
|
|
|
if (zoom_mode_ == ZOOM_MODE_MANUAL) {
|
|
|
|
// If the zoom level hasn't changed, early out to avoid sending an event.
|
|
|
|
if (blink::PageZoomValuesEqual(zoom_level_, level))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
double old_zoom_level = zoom_level_;
|
2017-06-08 00:13:49 +00:00
|
|
|
zoom_level_ = level;
|
|
|
|
|
2023-08-15 01:33:30 +00:00
|
|
|
ZoomChangedEventData zoom_change_data(web_contents(), old_zoom_level,
|
2023-12-04 15:39:20 +00:00
|
|
|
zoom_level_, true /* temporary */,
|
2023-08-15 01:33:30 +00:00
|
|
|
zoom_mode_);
|
|
|
|
for (auto& observer : observers_)
|
|
|
|
observer.OnZoomChanged(zoom_change_data);
|
2017-06-08 00:13:49 +00:00
|
|
|
|
2023-08-15 01:33:30 +00:00
|
|
|
return true;
|
2017-01-30 17:06:50 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 00:13:49 +00:00
|
|
|
content::HostZoomMap* zoom_map =
|
|
|
|
content::HostZoomMap::GetForWebContents(web_contents());
|
2023-08-15 01:33:30 +00:00
|
|
|
DCHECK(zoom_map);
|
|
|
|
DCHECK(!event_data_);
|
|
|
|
event_data_ = std::make_unique<ZoomChangedEventData>(
|
|
|
|
web_contents(), GetZoomLevel(), level, false /* temporary */, zoom_mode_);
|
|
|
|
|
|
|
|
content::GlobalRenderFrameHostId rfh_id =
|
|
|
|
web_contents()->GetPrimaryMainFrame()->GetGlobalId();
|
|
|
|
if (zoom_mode_ == ZOOM_MODE_ISOLATED ||
|
2022-11-17 19:59:23 +00:00
|
|
|
zoom_map->UsesTemporaryZoomLevel(rfh_id)) {
|
|
|
|
zoom_map->SetTemporaryZoomLevel(rfh_id, level);
|
2023-08-15 01:33:30 +00:00
|
|
|
ZoomChangedEventData zoom_change_data(web_contents(), zoom_level_, level,
|
|
|
|
true /* temporary */, zoom_mode_);
|
|
|
|
for (auto& observer : observers_)
|
|
|
|
observer.OnZoomChanged(zoom_change_data);
|
2017-06-08 00:13:49 +00:00
|
|
|
} else {
|
2023-08-15 01:33:30 +00:00
|
|
|
if (!entry) {
|
|
|
|
// If we exit without triggering an update, we should clear event_data_,
|
|
|
|
// else we may later trigger a DCHECK(event_data_).
|
|
|
|
event_data_.reset();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::string host =
|
|
|
|
net::GetHostOrSpecFromURL(content::HostZoomMap::GetURLFromEntry(entry));
|
|
|
|
zoom_map->SetZoomLevelForHost(host, level);
|
2017-06-08 00:13:49 +00:00
|
|
|
}
|
2023-08-15 01:33:30 +00:00
|
|
|
|
|
|
|
DCHECK(!event_data_);
|
|
|
|
return true;
|
2017-01-30 11:18:40 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 01:33:30 +00:00
|
|
|
double WebContentsZoomController::GetZoomLevel() const {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
|
|
|
return zoom_mode_ == ZOOM_MODE_MANUAL
|
2017-06-08 00:13:49 +00:00
|
|
|
? zoom_level_
|
|
|
|
: content::HostZoomMap::GetZoomLevel(web_contents());
|
2017-01-30 11:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebContentsZoomController::SetDefaultZoomFactor(double factor) {
|
|
|
|
default_zoom_factor_ = factor;
|
|
|
|
}
|
|
|
|
|
|
|
|
double WebContentsZoomController::GetDefaultZoomFactor() {
|
|
|
|
return default_zoom_factor_;
|
|
|
|
}
|
|
|
|
|
2017-01-30 17:06:50 +00:00
|
|
|
void WebContentsZoomController::SetTemporaryZoomLevel(double level) {
|
2023-08-15 01:33:30 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
2022-11-17 19:59:23 +00:00
|
|
|
content::GlobalRenderFrameHostId old_rfh_id_ =
|
|
|
|
web_contents()->GetPrimaryMainFrame()->GetGlobalId();
|
|
|
|
host_zoom_map_->SetTemporaryZoomLevel(old_rfh_id_, level);
|
2023-08-15 01:33:30 +00:00
|
|
|
|
2017-01-30 17:06:50 +00:00
|
|
|
// Notify observers of zoom level changes.
|
2023-08-15 01:33:30 +00:00
|
|
|
ZoomChangedEventData zoom_change_data(web_contents(), zoom_level_, level,
|
|
|
|
true /* temporary */, zoom_mode_);
|
2023-12-04 15:39:20 +00:00
|
|
|
for (auto& observer : observers_)
|
2023-08-15 01:33:30 +00:00
|
|
|
observer.OnZoomChanged(zoom_change_data);
|
2017-01-30 17:06:50 +00:00
|
|
|
}
|
|
|
|
|
2017-01-31 09:15:45 +00:00
|
|
|
bool WebContentsZoomController::UsesTemporaryZoomLevel() {
|
2023-08-15 01:33:30 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
2022-11-17 19:59:23 +00:00
|
|
|
content::GlobalRenderFrameHostId rfh_id =
|
|
|
|
web_contents()->GetPrimaryMainFrame()->GetGlobalId();
|
|
|
|
return host_zoom_map_->UsesTemporaryZoomLevel(rfh_id);
|
2017-01-30 17:06:50 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 00:13:49 +00:00
|
|
|
void WebContentsZoomController::SetZoomMode(ZoomMode new_mode) {
|
2023-08-15 01:33:30 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
2017-06-08 00:13:49 +00:00
|
|
|
if (new_mode == zoom_mode_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
content::HostZoomMap* zoom_map =
|
|
|
|
content::HostZoomMap::GetForWebContents(web_contents());
|
2023-08-15 01:33:30 +00:00
|
|
|
DCHECK(zoom_map);
|
2022-11-17 19:59:23 +00:00
|
|
|
content::GlobalRenderFrameHostId rfh_id =
|
|
|
|
web_contents()->GetPrimaryMainFrame()->GetGlobalId();
|
2017-06-08 00:13:49 +00:00
|
|
|
double original_zoom_level = GetZoomLevel();
|
|
|
|
|
2023-08-15 01:33:30 +00:00
|
|
|
DCHECK(!event_data_);
|
|
|
|
event_data_ = std::make_unique<ZoomChangedEventData>(
|
|
|
|
web_contents(), original_zoom_level, original_zoom_level,
|
|
|
|
false /* temporary */, new_mode);
|
|
|
|
|
2017-06-08 00:13:49 +00:00
|
|
|
switch (new_mode) {
|
2023-08-15 01:33:30 +00:00
|
|
|
case ZOOM_MODE_DEFAULT: {
|
2017-06-08 00:13:49 +00:00
|
|
|
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);
|
2023-08-15 01:33:30 +00:00
|
|
|
event_data_->new_zoom_level = origin_zoom_level;
|
2022-11-17 19:59:23 +00:00
|
|
|
zoom_map->SetTemporaryZoomLevel(rfh_id, origin_zoom_level);
|
2017-06-08 00:13:49 +00:00
|
|
|
} 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.
|
2022-11-17 19:59:23 +00:00
|
|
|
zoom_map->ClearTemporaryZoomLevel(rfh_id);
|
2017-06-08 00:13:49 +00:00
|
|
|
break;
|
|
|
|
}
|
2023-08-15 01:33:30 +00:00
|
|
|
case ZOOM_MODE_ISOLATED: {
|
|
|
|
// Unless the zoom mode was |ZOOM_MODE_DISABLED| before this call, the
|
2017-06-08 00:13:49 +00:00
|
|
|
// page needs an initial isolated zoom back to the same level it was at
|
|
|
|
// in the other mode.
|
2023-08-15 01:33:30 +00:00
|
|
|
if (zoom_mode_ != ZOOM_MODE_DISABLED) {
|
2022-11-17 19:59:23 +00:00
|
|
|
zoom_map->SetTemporaryZoomLevel(rfh_id, original_zoom_level);
|
2017-06-08 00:13:49 +00:00
|
|
|
} else {
|
|
|
|
// When we don't call any HostZoomMap set functions, we send the event
|
|
|
|
// manually.
|
2023-08-15 01:33:30 +00:00
|
|
|
for (auto& observer : observers_)
|
|
|
|
observer.OnZoomChanged(*event_data_);
|
|
|
|
event_data_.reset();
|
2017-06-08 00:13:49 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2023-08-15 01:33:30 +00:00
|
|
|
case ZOOM_MODE_MANUAL: {
|
|
|
|
// Unless the zoom mode was |ZOOM_MODE_DISABLED| before this call, the
|
2017-06-08 00:13:49 +00:00
|
|
|
// page needs to be resized to the default zoom. While in manual mode,
|
|
|
|
// the zoom level is handled independently.
|
2023-08-15 01:33:30 +00:00
|
|
|
if (zoom_mode_ != ZOOM_MODE_DISABLED) {
|
2022-11-17 19:59:23 +00:00
|
|
|
zoom_map->SetTemporaryZoomLevel(rfh_id, GetDefaultZoomLevel());
|
2017-06-08 00:13:49 +00:00
|
|
|
zoom_level_ = original_zoom_level;
|
|
|
|
} else {
|
|
|
|
// When we don't call any HostZoomMap set functions, we send the event
|
|
|
|
// manually.
|
2023-08-15 01:33:30 +00:00
|
|
|
for (auto& observer : observers_)
|
|
|
|
observer.OnZoomChanged(*event_data_);
|
|
|
|
event_data_.reset();
|
2017-06-08 00:13:49 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2023-08-15 01:33:30 +00:00
|
|
|
case ZOOM_MODE_DISABLED: {
|
2017-06-08 00:13:49 +00:00
|
|
|
// The page needs to be zoomed back to default before disabling the zoom
|
2023-08-15 01:33:30 +00:00
|
|
|
double new_zoom_level = GetDefaultZoomLevel();
|
|
|
|
event_data_->new_zoom_level = new_zoom_level;
|
|
|
|
zoom_map->SetTemporaryZoomLevel(rfh_id, new_zoom_level);
|
2017-06-08 00:13:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-08-15 01:33:30 +00:00
|
|
|
// Any event data we've stored should have been consumed by this point.
|
|
|
|
DCHECK(!event_data_);
|
2017-06-08 00:13:49 +00:00
|
|
|
|
|
|
|
zoom_mode_ = new_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebContentsZoomController::ResetZoomModeOnNavigationIfNeeded(
|
|
|
|
const GURL& url) {
|
2023-08-15 01:33:30 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
|
|
|
if (zoom_mode_ != ZOOM_MODE_ISOLATED && zoom_mode_ != ZOOM_MODE_MANUAL)
|
2017-06-08 00:13:49 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
content::HostZoomMap* zoom_map =
|
|
|
|
content::HostZoomMap::GetForWebContents(web_contents());
|
|
|
|
zoom_level_ = zoom_map->GetDefaultZoomLevel();
|
2023-08-15 01:33:30 +00:00
|
|
|
double old_zoom_level = zoom_map->GetZoomLevel(web_contents());
|
2017-06-08 00:13:49 +00:00
|
|
|
double new_zoom_level = zoom_map->GetZoomLevelForHostAndScheme(
|
|
|
|
url.scheme(), net::GetHostOrSpecFromURL(url));
|
2023-12-04 15:39:20 +00:00
|
|
|
|
2023-08-15 01:33:30 +00:00
|
|
|
event_data_ = std::make_unique<ZoomChangedEventData>(
|
|
|
|
web_contents(), old_zoom_level, new_zoom_level, false, ZOOM_MODE_DEFAULT);
|
2023-12-04 15:39:20 +00:00
|
|
|
|
2023-08-15 01:33:30 +00:00
|
|
|
// The call to ClearTemporaryZoomLevel() doesn't generate any events from
|
|
|
|
// HostZoomMap, but the call to UpdateState() at the end of
|
|
|
|
// DidFinishNavigation will notify our observers.
|
|
|
|
// Note: it's possible the render_process/frame ids have disappeared (e.g.
|
|
|
|
// if we navigated to a new origin), but this won't cause a problem in the
|
|
|
|
// call below.
|
|
|
|
zoom_map->ClearTemporaryZoomLevel(
|
|
|
|
web_contents()->GetPrimaryMainFrame()->GetGlobalId());
|
|
|
|
zoom_mode_ = ZOOM_MODE_DEFAULT;
|
2017-06-08 00:13:49 +00:00
|
|
|
}
|
|
|
|
|
2017-01-30 11:18:40 +00:00
|
|
|
void WebContentsZoomController::DidFinishNavigation(
|
|
|
|
content::NavigationHandle* navigation_handle) {
|
2023-08-15 01:33:30 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
|
|
|
if (!navigation_handle->IsInPrimaryMainFrame() ||
|
|
|
|
!navigation_handle->HasCommitted()) {
|
2017-01-30 11:18:40 +00:00
|
|
|
return;
|
2023-08-15 01:33:30 +00:00
|
|
|
}
|
2017-01-30 11:18:40 +00:00
|
|
|
|
2023-08-15 01:33:30 +00:00
|
|
|
if (navigation_handle->IsErrorPage())
|
2017-01-30 11:18:40 +00:00
|
|
|
content::HostZoomMap::SendErrorPageZoomLevelRefresh(web_contents());
|
2023-08-15 01:33:30 +00:00
|
|
|
|
|
|
|
if (!navigation_handle->IsSameDocument()) {
|
|
|
|
ResetZoomModeOnNavigationIfNeeded(navigation_handle->GetURL());
|
|
|
|
SetZoomFactorOnNavigationIfNeeded(navigation_handle->GetURL());
|
2023-12-04 15:39:20 +00:00
|
|
|
|
|
|
|
// If the main frame's content has changed, the new page may have a
|
|
|
|
// different zoom level from the old one.
|
|
|
|
UpdateState(std::string());
|
2017-01-30 11:18:40 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 01:33:30 +00:00
|
|
|
DCHECK(!event_data_);
|
2017-01-30 11:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebContentsZoomController::WebContentsDestroyed() {
|
2023-08-15 01:33:30 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
|
|
|
// At this point we should no longer be sending any zoom events with this
|
|
|
|
// WebContents.
|
|
|
|
for (auto& observer : observers_) {
|
|
|
|
observer.OnZoomControllerDestroyed(this);
|
|
|
|
}
|
2018-08-16 22:57:40 +00:00
|
|
|
|
2017-01-31 09:25:48 +00:00
|
|
|
embedder_zoom_controller_ = nullptr;
|
2017-01-30 11:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebContentsZoomController::RenderFrameHostChanged(
|
|
|
|
content::RenderFrameHost* old_host,
|
|
|
|
content::RenderFrameHost* new_host) {
|
2023-08-15 01:33:30 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
|
|
|
// If our associated HostZoomMap changes, update our subscription.
|
2017-01-30 11:18:40 +00:00
|
|
|
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;
|
2023-08-15 01:33:30 +00:00
|
|
|
zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback(
|
|
|
|
base::BindRepeating(&WebContentsZoomController::OnZoomLevelChanged,
|
|
|
|
base::Unretained(this)));
|
2017-01-30 11:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebContentsZoomController::SetZoomFactorOnNavigationIfNeeded(
|
|
|
|
const GURL& url) {
|
2023-08-15 01:33:30 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
2019-10-18 19:57:34 +00:00
|
|
|
if (blink::PageZoomValuesEqual(GetDefaultZoomFactor(), kPageZoomEpsilon))
|
2017-01-30 11:18:40 +00:00
|
|
|
return;
|
|
|
|
|
2022-11-17 19:59:23 +00:00
|
|
|
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_);
|
2017-01-31 09:15:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (embedder_zoom_controller_ &&
|
|
|
|
embedder_zoom_controller_->UsesTemporaryZoomLevel()) {
|
|
|
|
double level = embedder_zoom_controller_->GetZoomLevel();
|
|
|
|
SetTemporaryZoomLevel(level);
|
2017-01-30 17:06:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-30 11:18:40 +00:00
|
|
|
// When kZoomFactor is available, it takes precedence over
|
|
|
|
// pref store values but if the host has zoom factor set explicitly
|
2020-10-13 17:25:21 +00:00
|
|
|
// then it takes precedence.
|
2017-01-30 11:18:40 +00:00
|
|
|
// pref store < kZoomFactor < setZoomLevel
|
|
|
|
std::string host = net::GetHostOrSpecFromURL(url);
|
2017-03-08 13:33:43 +00:00
|
|
|
std::string scheme = url.scheme();
|
2017-01-30 11:18:40 +00:00
|
|
|
double zoom_factor = GetDefaultZoomFactor();
|
2019-10-18 19:57:34 +00:00
|
|
|
double zoom_level = blink::PageZoomFactorToZoomLevel(zoom_factor);
|
2017-03-08 13:33:43 +00:00
|
|
|
if (host_zoom_map_->HasZoomLevel(scheme, host)) {
|
|
|
|
zoom_level = host_zoom_map_->GetZoomLevelForHostAndScheme(scheme, host);
|
|
|
|
}
|
2019-10-18 19:57:34 +00:00
|
|
|
if (blink::PageZoomValuesEqual(zoom_level, GetZoomLevel()))
|
2017-01-30 11:18:40 +00:00
|
|
|
return;
|
|
|
|
|
2017-03-08 13:33:43 +00:00
|
|
|
SetZoomLevel(zoom_level);
|
2017-01-30 11:18:40 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 01:33:30 +00:00
|
|
|
void WebContentsZoomController::OnZoomLevelChanged(
|
|
|
|
const content::HostZoomMap::ZoomLevelChange& change) {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
|
|
|
UpdateState(change.host);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebContentsZoomController::UpdateState(const std::string& host) {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
|
|
|
// If |host| is empty, all observers should be updated.
|
|
|
|
if (!host.empty()) {
|
|
|
|
// Use the navigation entry's URL instead of the WebContents' so virtual
|
|
|
|
// URLs work (e.g. chrome://settings). http://crbug.com/153950
|
|
|
|
content::NavigationEntry* entry =
|
|
|
|
web_contents()->GetController().GetLastCommittedEntry();
|
|
|
|
if (!entry || host != net::GetHostOrSpecFromURL(
|
|
|
|
content::HostZoomMap::GetURLFromEntry(entry))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event_data_) {
|
|
|
|
// For state changes initiated within the ZoomController, information about
|
|
|
|
// the change should be sent.
|
|
|
|
ZoomChangedEventData zoom_change_data = *event_data_;
|
|
|
|
event_data_.reset();
|
|
|
|
for (auto& observer : observers_)
|
|
|
|
observer.OnZoomChanged(zoom_change_data);
|
|
|
|
} else {
|
|
|
|
double zoom_level = GetZoomLevel();
|
|
|
|
ZoomChangedEventData zoom_change_data(web_contents(), zoom_level,
|
|
|
|
zoom_level, false, zoom_mode_);
|
|
|
|
for (auto& observer : observers_)
|
|
|
|
observer.OnZoomChanged(zoom_change_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 18:51:36 +00:00
|
|
|
WEB_CONTENTS_USER_DATA_KEY_IMPL(WebContentsZoomController);
|
2019-01-21 16:56:54 +00:00
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|