Add ZoomController to manager zoom changes for webcontents
This commit is contained in:
parent
07794a58aa
commit
63c0e4cbb1
10 changed files with 269 additions and 53 deletions
|
@ -22,6 +22,7 @@
|
||||||
#include "atom/browser/ui/drag_util.h"
|
#include "atom/browser/ui/drag_util.h"
|
||||||
#include "atom/browser/web_contents_permission_helper.h"
|
#include "atom/browser/web_contents_permission_helper.h"
|
||||||
#include "atom/browser/web_contents_preferences.h"
|
#include "atom/browser/web_contents_preferences.h"
|
||||||
|
#include "atom/browser/web_contents_zoom_controller.h"
|
||||||
#include "atom/browser/web_view_guest_delegate.h"
|
#include "atom/browser/web_view_guest_delegate.h"
|
||||||
#include "atom/common/api/api_messages.h"
|
#include "atom/common/api/api_messages.h"
|
||||||
#include "atom/common/api/event_emitter_caller.h"
|
#include "atom/common/api/event_emitter_caller.h"
|
||||||
|
@ -49,7 +50,6 @@
|
||||||
#include "content/browser/web_contents/web_contents_impl.h"
|
#include "content/browser/web_contents/web_contents_impl.h"
|
||||||
#include "content/common/view_messages.h"
|
#include "content/common/view_messages.h"
|
||||||
#include "content/public/browser/favicon_status.h"
|
#include "content/public/browser/favicon_status.h"
|
||||||
#include "content/public/browser/host_zoom_map.h"
|
|
||||||
#include "content/public/browser/native_web_keyboard_event.h"
|
#include "content/public/browser/native_web_keyboard_event.h"
|
||||||
#include "content/public/browser/navigation_details.h"
|
#include "content/public/browser/navigation_details.h"
|
||||||
#include "content/public/browser/navigation_entry.h"
|
#include "content/public/browser/navigation_entry.h"
|
||||||
|
@ -68,7 +68,6 @@
|
||||||
#include "content/public/common/context_menu_params.h"
|
#include "content/public/common/context_menu_params.h"
|
||||||
#include "native_mate/dictionary.h"
|
#include "native_mate/dictionary.h"
|
||||||
#include "native_mate/object_template_builder.h"
|
#include "native_mate/object_template_builder.h"
|
||||||
#include "net/base/url_util.h"
|
|
||||||
#include "net/url_request/url_request_context.h"
|
#include "net/url_request/url_request_context.h"
|
||||||
#include "third_party/WebKit/public/platform/WebInputEvent.h"
|
#include "third_party/WebKit/public/platform/WebInputEvent.h"
|
||||||
#include "third_party/WebKit/public/web/WebFindOptions.h"
|
#include "third_party/WebKit/public/web/WebFindOptions.h"
|
||||||
|
@ -249,13 +248,12 @@ WebContents::WebContents(v8::Isolate* isolate,
|
||||||
content::WebContents* web_contents,
|
content::WebContents* web_contents,
|
||||||
Type type)
|
Type type)
|
||||||
: content::WebContentsObserver(web_contents),
|
: content::WebContentsObserver(web_contents),
|
||||||
|
zoom_controller_(nullptr),
|
||||||
embedder_(nullptr),
|
embedder_(nullptr),
|
||||||
type_(type),
|
type_(type),
|
||||||
request_id_(0),
|
request_id_(0),
|
||||||
background_throttling_(true),
|
background_throttling_(true),
|
||||||
enable_devtools_(true),
|
enable_devtools_(true) {
|
||||||
zoom_factor_(content::kMinimumZoomFactor) {
|
|
||||||
|
|
||||||
if (type == REMOTE) {
|
if (type == REMOTE) {
|
||||||
web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent());
|
web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent());
|
||||||
Init(isolate);
|
Init(isolate);
|
||||||
|
@ -268,17 +266,15 @@ WebContents::WebContents(v8::Isolate* isolate,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
WebContents::WebContents(v8::Isolate* isolate,
|
WebContents::WebContents(v8::Isolate* isolate, const mate::Dictionary& options)
|
||||||
const mate::Dictionary& options)
|
: zoom_controller_(nullptr),
|
||||||
: embedder_(nullptr),
|
embedder_(nullptr),
|
||||||
type_(BROWSER_WINDOW),
|
type_(BROWSER_WINDOW),
|
||||||
request_id_(0),
|
request_id_(0),
|
||||||
background_throttling_(true),
|
background_throttling_(true),
|
||||||
enable_devtools_(true),
|
enable_devtools_(true) {
|
||||||
zoom_factor_(content::kMinimumZoomFactor) {
|
|
||||||
// Read options.
|
// Read options.
|
||||||
options.Get("backgroundThrottling", &background_throttling_);
|
options.Get("backgroundThrottling", &background_throttling_);
|
||||||
options.Get("zoomFactor", &zoom_factor_);
|
|
||||||
|
|
||||||
// FIXME(zcbenz): We should read "type" parameter for better design, but
|
// FIXME(zcbenz): We should read "type" parameter for better design, but
|
||||||
// on Windows we have encountered a compiler bug that if we read "type"
|
// on Windows we have encountered a compiler bug that if we read "type"
|
||||||
|
@ -350,10 +346,16 @@ void WebContents::InitWithSessionAndOptions(v8::Isolate* isolate,
|
||||||
// Save the preferences in C++.
|
// Save the preferences in C++.
|
||||||
new WebContentsPreferences(web_contents, options);
|
new WebContentsPreferences(web_contents, options);
|
||||||
|
|
||||||
// Intialize permission helper.
|
// Initialize permission helper.
|
||||||
WebContentsPermissionHelper::CreateForWebContents(web_contents);
|
WebContentsPermissionHelper::CreateForWebContents(web_contents);
|
||||||
// Intialize security state client.
|
// Initialize security state client.
|
||||||
SecurityStateTabHelper::CreateForWebContents(web_contents);
|
SecurityStateTabHelper::CreateForWebContents(web_contents);
|
||||||
|
// Initialize zoom controller.
|
||||||
|
WebContentsZoomController::CreateForWebContents(web_contents);
|
||||||
|
zoom_controller_ = WebContentsZoomController::FromWebContents(web_contents);
|
||||||
|
double zoom_factor;
|
||||||
|
if (options.Get("zoomFactor", &zoom_factor))
|
||||||
|
zoom_controller_->SetDefaultZoomFactor(zoom_factor);
|
||||||
|
|
||||||
web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent());
|
web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent());
|
||||||
|
|
||||||
|
@ -738,8 +740,6 @@ void WebContents::DidFinishNavigation(
|
||||||
auto url = navigation_handle->GetURL();
|
auto url = navigation_handle->GetURL();
|
||||||
bool is_in_page = navigation_handle->IsSamePage();
|
bool is_in_page = navigation_handle->IsSamePage();
|
||||||
if (is_main_frame && !is_in_page) {
|
if (is_main_frame && !is_in_page) {
|
||||||
// Set initial zoom factor if needed.
|
|
||||||
SetZoomFactorIfNeeded(url);
|
|
||||||
Emit("did-navigate", url);
|
Emit("did-navigate", url);
|
||||||
} else if (is_in_page) {
|
} else if (is_in_page) {
|
||||||
Emit("did-navigate-in-page", url, is_main_frame);
|
Emit("did-navigate-in-page", url, is_main_frame);
|
||||||
|
@ -1506,20 +1506,11 @@ void WebContents::Invalidate() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebContents::SetZoomLevel(double level) {
|
void WebContents::SetZoomLevel(double level) {
|
||||||
auto factor = content::ZoomLevelToZoomFactor(level);
|
zoom_controller_->SetZoomLevel(level);
|
||||||
if (!content::ZoomValuesEqual(zoom_factor_, factor)) {
|
|
||||||
content::NavigationEntry* entry =
|
|
||||||
web_contents()->GetController().GetLastCommittedEntry();
|
|
||||||
if (entry) {
|
|
||||||
std::string host = net::GetHostOrSpecFromURL(entry->GetURL());
|
|
||||||
host_zoom_factor_[host] = factor;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
content::HostZoomMap::SetZoomLevel(web_contents(), level);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
double WebContents::GetZoomLevel() {
|
double WebContents::GetZoomLevel() {
|
||||||
return content::HostZoomMap::GetZoomLevel(web_contents());
|
return zoom_controller_->GetZoomLevel();
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebContents::SetZoomFactor(double factor) {
|
void WebContents::SetZoomFactor(double factor) {
|
||||||
|
@ -1532,22 +1523,6 @@ double WebContents::GetZoomFactor() {
|
||||||
return content::ZoomLevelToZoomFactor(level);
|
return content::ZoomLevelToZoomFactor(level);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebContents::SetZoomFactorIfNeeded(const GURL& url) {
|
|
||||||
if (zoom_factor_ == content::kMinimumZoomFactor)
|
|
||||||
return;
|
|
||||||
|
|
||||||
std::string host = net::GetHostOrSpecFromURL(url);
|
|
||||||
double zoom_factor = zoom_factor_;
|
|
||||||
auto it = host_zoom_factor_.find(host);
|
|
||||||
if (it != host_zoom_factor_.end())
|
|
||||||
zoom_factor = it->second;
|
|
||||||
auto level = content::ZoomFactorToZoomLevel(zoom_factor);
|
|
||||||
if (content::ZoomValuesEqual(level, GetZoomLevel()))
|
|
||||||
return;
|
|
||||||
|
|
||||||
SetZoomLevel(level);
|
|
||||||
}
|
|
||||||
|
|
||||||
v8::Local<v8::Value> WebContents::GetWebPreferences(v8::Isolate* isolate) {
|
v8::Local<v8::Value> WebContents::GetWebPreferences(v8::Isolate* isolate) {
|
||||||
WebContentsPreferences* web_preferences =
|
WebContentsPreferences* web_preferences =
|
||||||
WebContentsPreferences::FromWebContents(web_contents());
|
WebContentsPreferences::FromWebContents(web_contents());
|
||||||
|
|
|
@ -39,6 +39,7 @@ namespace atom {
|
||||||
|
|
||||||
struct SetSizeParams;
|
struct SetSizeParams;
|
||||||
class AtomBrowserContext;
|
class AtomBrowserContext;
|
||||||
|
class WebContentsZoomController;
|
||||||
class WebViewGuestDelegate;
|
class WebViewGuestDelegate;
|
||||||
|
|
||||||
namespace api {
|
namespace api {
|
||||||
|
@ -206,6 +207,8 @@ class WebContents : public mate::TrackableObject<WebContents>,
|
||||||
v8::Local<v8::Value> DevToolsWebContents(v8::Isolate* isolate);
|
v8::Local<v8::Value> DevToolsWebContents(v8::Isolate* isolate);
|
||||||
v8::Local<v8::Value> Debugger(v8::Isolate* isolate);
|
v8::Local<v8::Value> Debugger(v8::Isolate* isolate);
|
||||||
|
|
||||||
|
WebContentsZoomController* GetZoomController() { return zoom_controller_; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
WebContents(v8::Isolate* isolate,
|
WebContents(v8::Isolate* isolate,
|
||||||
content::WebContents* web_contents,
|
content::WebContents* web_contents,
|
||||||
|
@ -349,20 +352,18 @@ class WebContents : public mate::TrackableObject<WebContents>,
|
||||||
const base::ListValue& args,
|
const base::ListValue& args,
|
||||||
IPC::Message* message);
|
IPC::Message* message);
|
||||||
|
|
||||||
// Called after committing a navigation, to set the zoom
|
|
||||||
// factor.
|
|
||||||
void SetZoomFactorIfNeeded(const GURL& url);
|
|
||||||
|
|
||||||
v8::Global<v8::Value> session_;
|
v8::Global<v8::Value> session_;
|
||||||
v8::Global<v8::Value> devtools_web_contents_;
|
v8::Global<v8::Value> devtools_web_contents_;
|
||||||
v8::Global<v8::Value> debugger_;
|
v8::Global<v8::Value> debugger_;
|
||||||
|
|
||||||
std::unique_ptr<WebViewGuestDelegate> guest_delegate_;
|
std::unique_ptr<WebViewGuestDelegate> guest_delegate_;
|
||||||
std::map<std::string, double> host_zoom_factor_;
|
|
||||||
|
|
||||||
// The host webcontents that may contain this webcontents.
|
// The host webcontents that may contain this webcontents.
|
||||||
WebContents* embedder_;
|
WebContents* embedder_;
|
||||||
|
|
||||||
|
// The zoom controller for this webContents.
|
||||||
|
WebContentsZoomController* zoom_controller_;
|
||||||
|
|
||||||
// The type of current WebContents.
|
// The type of current WebContents.
|
||||||
Type type_;
|
Type type_;
|
||||||
|
|
||||||
|
@ -375,9 +376,6 @@ class WebContents : public mate::TrackableObject<WebContents>,
|
||||||
// Whether to enable devtools.
|
// Whether to enable devtools.
|
||||||
bool enable_devtools_;
|
bool enable_devtools_;
|
||||||
|
|
||||||
// Initial zoom factor.
|
|
||||||
double zoom_factor_;
|
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(WebContents);
|
DISALLOW_COPY_AND_ASSIGN(WebContents);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
// found in the LICENSE file.
|
// found in the LICENSE file.
|
||||||
|
|
||||||
#include "atom/browser/web_contents_preferences.h"
|
#include "atom/browser/web_contents_preferences.h"
|
||||||
|
#include "atom/browser/web_contents_zoom_controller.h"
|
||||||
#include "atom/browser/web_view_manager.h"
|
#include "atom/browser/web_view_manager.h"
|
||||||
#include "atom/common/native_mate_converters/content_converter.h"
|
#include "atom/common/native_mate_converters/content_converter.h"
|
||||||
#include "atom/common/native_mate_converters/value_converter.h"
|
#include "atom/common/native_mate_converters/value_converter.h"
|
||||||
|
@ -24,6 +25,12 @@ void AddGuest(int guest_instance_id,
|
||||||
manager->AddGuest(guest_instance_id, element_instance_id, embedder,
|
manager->AddGuest(guest_instance_id, element_instance_id, embedder,
|
||||||
guest_web_contents);
|
guest_web_contents);
|
||||||
|
|
||||||
|
double zoom_factor;
|
||||||
|
if (options.GetDouble("zoomFactor", &zoom_factor)) {
|
||||||
|
atom::WebContentsZoomController::FromWebContents(guest_web_contents)
|
||||||
|
->SetDefaultZoomFactor(zoom_factor);
|
||||||
|
}
|
||||||
|
|
||||||
WebContentsPreferences::FromWebContents(guest_web_contents)->Merge(options);
|
WebContentsPreferences::FromWebContents(guest_web_contents)->Merge(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
139
atom/browser/web_contents_zoom_controller.cc
Normal file
139
atom/browser/web_contents_zoom_controller.cc
Normal file
|
@ -0,0 +1,139 @@
|
||||||
|
// 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 "atom/browser/web_contents_zoom_controller.h"
|
||||||
|
|
||||||
|
#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_process_host.h"
|
||||||
|
#include "content/public/browser/render_view_host.h"
|
||||||
|
#include "content/public/browser/web_contents.h"
|
||||||
|
#include "content/public/common/page_type.h"
|
||||||
|
#include "content/public/common/page_zoom.h"
|
||||||
|
#include "net/base/url_util.h"
|
||||||
|
|
||||||
|
DEFINE_WEB_CONTENTS_USER_DATA_KEY(atom::WebContentsZoomController);
|
||||||
|
|
||||||
|
namespace atom {
|
||||||
|
|
||||||
|
WebContentsZoomController::WebContentsZoomController(
|
||||||
|
content::WebContents* web_contents)
|
||||||
|
: content::WebContentsObserver(web_contents) {
|
||||||
|
default_zoom_factor_ = content::kEpsilon;
|
||||||
|
host_zoom_map_ = content::HostZoomMap::GetForWebContents(web_contents);
|
||||||
|
zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback(base::Bind(
|
||||||
|
&WebContentsZoomController::OnZoomLevelChanged, base::Unretained(this)));
|
||||||
|
}
|
||||||
|
|
||||||
|
WebContentsZoomController::~WebContentsZoomController() {}
|
||||||
|
|
||||||
|
void WebContentsZoomController::AddObserver(
|
||||||
|
WebContentsZoomController::Observer* observer) {
|
||||||
|
observers_.AddObserver(observer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebContentsZoomController::RemoveObserver(
|
||||||
|
WebContentsZoomController::Observer* observer) {
|
||||||
|
observers_.RemoveObserver(observer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebContentsZoomController::SetZoomLevel(double level) {
|
||||||
|
if (!web_contents()->GetRenderViewHost()->IsRenderViewLive() ||
|
||||||
|
content::ZoomValuesEqual(GetZoomLevel(), level))
|
||||||
|
return;
|
||||||
|
auto new_zoom_factor = content::ZoomLevelToZoomFactor(level);
|
||||||
|
content::NavigationEntry* entry =
|
||||||
|
web_contents()->GetController().GetLastCommittedEntry();
|
||||||
|
if (entry) {
|
||||||
|
std::string host = net::GetHostOrSpecFromURL(entry->GetURL());
|
||||||
|
// When new zoom level varies from kZoomFactor, it takes preference.
|
||||||
|
if (!content::ZoomValuesEqual(GetDefaultZoomFactor(), new_zoom_factor))
|
||||||
|
host_zoom_factor_[host] = new_zoom_factor;
|
||||||
|
content::HostZoomMap::SetZoomLevel(web_contents(), level);
|
||||||
|
// Notify observers of zoom level changes.
|
||||||
|
FOR_EACH_OBSERVER(WebContentsZoomController::Observer, observers_,
|
||||||
|
OnZoomLevelChanged(web_contents(), level));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
double WebContentsZoomController::GetZoomLevel() {
|
||||||
|
return content::HostZoomMap::GetZoomLevel(web_contents());
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebContentsZoomController::SetDefaultZoomFactor(double factor) {
|
||||||
|
default_zoom_factor_ = factor;
|
||||||
|
}
|
||||||
|
|
||||||
|
double WebContentsZoomController::GetDefaultZoomFactor() {
|
||||||
|
return default_zoom_factor_;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!navigation_handle->IsSamePage())
|
||||||
|
SetZoomFactorOnNavigationIfNeeded(navigation_handle->GetURL());
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebContentsZoomController::WebContentsDestroyed() {
|
||||||
|
observers_.Clear();
|
||||||
|
host_zoom_factor_.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback(base::Bind(
|
||||||
|
&WebContentsZoomController::OnZoomLevelChanged, base::Unretained(this)));
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebContentsZoomController::SetZoomFactorOnNavigationIfNeeded(
|
||||||
|
const GURL& url) {
|
||||||
|
if (content::ZoomValuesEqual(GetDefaultZoomFactor(), content::kEpsilon))
|
||||||
|
return;
|
||||||
|
|
||||||
|
// When kZoomFactor is available, it takes precedence over
|
||||||
|
// pref store values but if the host has zoom factor set explicitly
|
||||||
|
// then it takes precendence.
|
||||||
|
// pref store < kZoomFactor < setZoomLevel
|
||||||
|
std::string host = net::GetHostOrSpecFromURL(url);
|
||||||
|
double zoom_factor = GetDefaultZoomFactor();
|
||||||
|
auto it = host_zoom_factor_.find(host);
|
||||||
|
if (it != host_zoom_factor_.end())
|
||||||
|
zoom_factor = it->second;
|
||||||
|
auto level = content::ZoomFactorToZoomLevel(zoom_factor);
|
||||||
|
if (content::ZoomValuesEqual(level, GetZoomLevel()))
|
||||||
|
return;
|
||||||
|
|
||||||
|
SetZoomLevel(level);
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebContentsZoomController::OnZoomLevelChanged(
|
||||||
|
const content::HostZoomMap::ZoomLevelChange& change) {
|
||||||
|
if (change.mode == content::HostZoomMap::ZOOM_CHANGED_FOR_HOST) {
|
||||||
|
auto it = host_zoom_factor_.find(change.host);
|
||||||
|
if (it == host_zoom_factor_.end())
|
||||||
|
return;
|
||||||
|
host_zoom_factor_.insert(
|
||||||
|
it, std::make_pair(change.host,
|
||||||
|
content::ZoomLevelToZoomFactor(change.zoom_level)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace atom
|
73
atom/browser/web_contents_zoom_controller.h
Normal file
73
atom/browser/web_contents_zoom_controller.h
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
// Copyright (c) 2017 GitHub, Inc.
|
||||||
|
// Use of this source code is governed by the MIT license that can be
|
||||||
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
#ifndef ATOM_BROWSER_WEB_CONTENTS_ZOOM_CONTROLLER_H_
|
||||||
|
#define ATOM_BROWSER_WEB_CONTENTS_ZOOM_CONTROLLER_H_
|
||||||
|
|
||||||
|
#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"
|
||||||
|
|
||||||
|
namespace atom {
|
||||||
|
|
||||||
|
// Manages the zoom changes of WebContents.
|
||||||
|
class WebContentsZoomController
|
||||||
|
: public content::WebContentsObserver,
|
||||||
|
public content::WebContentsUserData<WebContentsZoomController> {
|
||||||
|
public:
|
||||||
|
class Observer {
|
||||||
|
public:
|
||||||
|
virtual void OnZoomLevelChanged(content::WebContents* web_contents,
|
||||||
|
double level) {}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual ~Observer() {}
|
||||||
|
};
|
||||||
|
|
||||||
|
explicit WebContentsZoomController(content::WebContents* web_contents);
|
||||||
|
~WebContentsZoomController() override;
|
||||||
|
|
||||||
|
void AddObserver(Observer* observer);
|
||||||
|
void RemoveObserver(Observer* observer);
|
||||||
|
|
||||||
|
// Methods for managing zoom levels.
|
||||||
|
void SetZoomLevel(double level);
|
||||||
|
double GetZoomLevel();
|
||||||
|
void SetDefaultZoomFactor(double factor);
|
||||||
|
double GetDefaultZoomFactor();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
// content::WebContentsObserver:
|
||||||
|
void DidFinishNavigation(content::NavigationHandle* handle) override;
|
||||||
|
void WebContentsDestroyed() override;
|
||||||
|
void RenderFrameHostChanged(content::RenderFrameHost* old_host,
|
||||||
|
content::RenderFrameHost* new_host) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
friend class content::WebContentsUserData<WebContentsZoomController>;
|
||||||
|
|
||||||
|
// Called after a navigation has committed to set default zoom factor.
|
||||||
|
void SetZoomFactorOnNavigationIfNeeded(const GURL& url);
|
||||||
|
|
||||||
|
// Track zoom changes of a host in other instances of a partition.
|
||||||
|
void OnZoomLevelChanged(const content::HostZoomMap::ZoomLevelChange& change);
|
||||||
|
|
||||||
|
// kZoomFactor.
|
||||||
|
double default_zoom_factor_;
|
||||||
|
|
||||||
|
// Map between zoom factor and hosts in this webContent.
|
||||||
|
std::map<std::string, double> host_zoom_factor_;
|
||||||
|
|
||||||
|
base::ObserverList<Observer> observers_;
|
||||||
|
|
||||||
|
content::HostZoomMap* host_zoom_map_;
|
||||||
|
|
||||||
|
std::unique_ptr<content::HostZoomMap::Subscription> zoom_subscription_;
|
||||||
|
|
||||||
|
DISALLOW_COPY_AND_ASSIGN(WebContentsZoomController);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace atom
|
||||||
|
|
||||||
|
#endif // ATOM_BROWSER_WEB_CONTENTS_ZOOM_CONTROLLER_H_
|
|
@ -39,7 +39,9 @@ void WebViewGuestDelegate::Initialize(api::WebContents* api_web_contents) {
|
||||||
|
|
||||||
void WebViewGuestDelegate::Destroy() {
|
void WebViewGuestDelegate::Destroy() {
|
||||||
// Give the content module an opportunity to perform some cleanup.
|
// Give the content module an opportunity to perform some cleanup.
|
||||||
|
embedder_zoom_controller_->RemoveObserver(this);
|
||||||
guest_host_->WillDestroy();
|
guest_host_->WillDestroy();
|
||||||
|
embedder_zoom_controller_ = nullptr;
|
||||||
guest_host_ = nullptr;
|
guest_host_ = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,6 +109,9 @@ void WebViewGuestDelegate::DidFinishNavigation(
|
||||||
|
|
||||||
void WebViewGuestDelegate::DidAttach(int guest_proxy_routing_id) {
|
void WebViewGuestDelegate::DidAttach(int guest_proxy_routing_id) {
|
||||||
api_web_contents_->Emit("did-attach");
|
api_web_contents_->Emit("did-attach");
|
||||||
|
embedder_zoom_controller_ =
|
||||||
|
WebContentsZoomController::FromWebContents(embedder_web_contents_);
|
||||||
|
embedder_zoom_controller_->AddObserver(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
content::WebContents* WebViewGuestDelegate::GetOwnerWebContents() const {
|
content::WebContents* WebViewGuestDelegate::GetOwnerWebContents() const {
|
||||||
|
@ -134,6 +139,14 @@ void WebViewGuestDelegate::WillAttach(
|
||||||
completion_callback.Run();
|
completion_callback.Run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WebViewGuestDelegate::OnZoomLevelChanged(
|
||||||
|
content::WebContents* web_contents,
|
||||||
|
double level) {
|
||||||
|
if (web_contents == GetOwnerWebContents()) {
|
||||||
|
api_web_contents_->GetZoomController()->SetZoomLevel(level);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void WebViewGuestDelegate::GuestSizeChangedDueToAutoSize(
|
void WebViewGuestDelegate::GuestSizeChangedDueToAutoSize(
|
||||||
const gfx::Size& old_size, const gfx::Size& new_size) {
|
const gfx::Size& old_size, const gfx::Size& new_size) {
|
||||||
api_web_contents_->Emit("size-changed",
|
api_web_contents_->Emit("size-changed",
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#ifndef ATOM_BROWSER_WEB_VIEW_GUEST_DELEGATE_H_
|
#ifndef ATOM_BROWSER_WEB_VIEW_GUEST_DELEGATE_H_
|
||||||
#define ATOM_BROWSER_WEB_VIEW_GUEST_DELEGATE_H_
|
#define ATOM_BROWSER_WEB_VIEW_GUEST_DELEGATE_H_
|
||||||
|
|
||||||
|
#include "atom/browser/web_contents_zoom_controller.h"
|
||||||
#include "content/public/browser/browser_plugin_guest_delegate.h"
|
#include "content/public/browser/browser_plugin_guest_delegate.h"
|
||||||
#include "content/public/browser/web_contents_observer.h"
|
#include "content/public/browser/web_contents_observer.h"
|
||||||
|
|
||||||
|
@ -31,7 +32,8 @@ struct SetSizeParams {
|
||||||
};
|
};
|
||||||
|
|
||||||
class WebViewGuestDelegate : public content::BrowserPluginGuestDelegate,
|
class WebViewGuestDelegate : public content::BrowserPluginGuestDelegate,
|
||||||
public content::WebContentsObserver {
|
public content::WebContentsObserver,
|
||||||
|
public WebContentsZoomController::Observer {
|
||||||
public:
|
public:
|
||||||
WebViewGuestDelegate();
|
WebViewGuestDelegate();
|
||||||
~WebViewGuestDelegate() override;
|
~WebViewGuestDelegate() override;
|
||||||
|
@ -63,6 +65,10 @@ class WebViewGuestDelegate : public content::BrowserPluginGuestDelegate,
|
||||||
content::RenderWidgetHost* GetOwnerRenderWidgetHost() override;
|
content::RenderWidgetHost* GetOwnerRenderWidgetHost() override;
|
||||||
content::SiteInstance* GetOwnerSiteInstance() override;
|
content::SiteInstance* GetOwnerSiteInstance() override;
|
||||||
|
|
||||||
|
// WebContentsZoomController::Observer:
|
||||||
|
void OnZoomLevelChanged(content::WebContents* web_contents,
|
||||||
|
double level) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// This method is invoked when the contents auto-resized to give the container
|
// This method is invoked when the contents auto-resized to give the container
|
||||||
// an opportunity to match it if it wishes.
|
// an opportunity to match it if it wishes.
|
||||||
|
@ -78,6 +84,10 @@ class WebViewGuestDelegate : public content::BrowserPluginGuestDelegate,
|
||||||
// The WebContents that attaches this guest view.
|
// The WebContents that attaches this guest view.
|
||||||
content::WebContents* embedder_web_contents_;
|
content::WebContents* embedder_web_contents_;
|
||||||
|
|
||||||
|
// The zoom controller of the embedder that is used
|
||||||
|
// to subscribe for zoom changes.
|
||||||
|
WebContentsZoomController* embedder_zoom_controller_;
|
||||||
|
|
||||||
// The size of the container element.
|
// The size of the container element.
|
||||||
gfx::Size element_size_;
|
gfx::Size element_size_;
|
||||||
|
|
||||||
|
|
|
@ -341,6 +341,8 @@
|
||||||
'atom/browser/web_contents_permission_helper.h',
|
'atom/browser/web_contents_permission_helper.h',
|
||||||
'atom/browser/web_contents_preferences.cc',
|
'atom/browser/web_contents_preferences.cc',
|
||||||
'atom/browser/web_contents_preferences.h',
|
'atom/browser/web_contents_preferences.h',
|
||||||
|
'atom/browser/web_contents_zoom_controller.cc',
|
||||||
|
'atom/browser/web_contents_zoom_controller.h',
|
||||||
'atom/browser/web_dialog_helper.cc',
|
'atom/browser/web_dialog_helper.cc',
|
||||||
'atom/browser/web_dialog_helper.h',
|
'atom/browser/web_dialog_helper.h',
|
||||||
'atom/browser/web_view_guest_delegate.cc',
|
'atom/browser/web_view_guest_delegate.cc',
|
||||||
|
|
|
@ -184,7 +184,7 @@ const attachGuest = function (event, elementInstanceId, guestInstanceId, params)
|
||||||
guestInstanceId: guestInstanceId,
|
guestInstanceId: guestInstanceId,
|
||||||
nodeIntegration: params.nodeintegration != null ? params.nodeintegration : false,
|
nodeIntegration: params.nodeintegration != null ? params.nodeintegration : false,
|
||||||
plugins: params.plugins,
|
plugins: params.plugins,
|
||||||
zoomFactor: params.zoomFactor,
|
zoomFactor: embedder.getZoomFactor(),
|
||||||
webSecurity: !params.disablewebsecurity,
|
webSecurity: !params.disablewebsecurity,
|
||||||
blinkFeatures: params.blinkfeatures,
|
blinkFeatures: params.blinkfeatures,
|
||||||
disableBlinkFeatures: params.disableblinkfeatures
|
disableBlinkFeatures: params.disableblinkfeatures
|
||||||
|
|
|
@ -231,7 +231,6 @@ class WebViewImpl {
|
||||||
const params = {
|
const params = {
|
||||||
instanceId: this.viewInstanceId,
|
instanceId: this.viewInstanceId,
|
||||||
userAgentOverride: this.userAgentOverride,
|
userAgentOverride: this.userAgentOverride,
|
||||||
zoomFactor: webFrame.getZoomFactor()
|
|
||||||
}
|
}
|
||||||
for (const attributeName in this.attributes) {
|
for (const attributeName in this.attributes) {
|
||||||
if (hasProp.call(this.attributes, attributeName)) {
|
if (hasProp.call(this.attributes, attributeName)) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue