Change webview's zoom level on JavaScript side

This commit is contained in:
Cheng Zhao 2016-01-13 12:46:13 +08:00
parent 118924ba7a
commit af02739c4e
8 changed files with 26 additions and 58 deletions

View file

@ -33,7 +33,6 @@
#include "chrome/browser/printing/print_view_manager_basic.h" #include "chrome/browser/printing/print_view_manager_basic.h"
#include "chrome/browser/printing/print_preview_message_handler.h" #include "chrome/browser/printing/print_preview_message_handler.h"
#include "content/common/view_messages.h" #include "content/common/view_messages.h"
#include "content/public/browser/browser_plugin_guest_manager.h"
#include "content/public/browser/favicon_status.h" #include "content/public/browser/favicon_status.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"
@ -75,15 +74,6 @@ void SetUserAgentInIO(scoped_refptr<net::URLRequestContextGetter> getter,
user_agent)); user_agent));
} }
bool NotifyZoomLevelChanged(
double level, content::WebContents* guest_web_contents) {
guest_web_contents->SendToAllFrames(
new AtomViewMsg_SetZoomLevel(MSG_ROUTING_NONE, level));
// Return false to iterate over all guests.
return false;
}
} // namespace } // namespace
namespace mate { namespace mate {
@ -625,7 +615,6 @@ bool WebContents::OnMessageReceived(const IPC::Message& message) {
IPC_MESSAGE_HANDLER(AtomViewHostMsg_Message, OnRendererMessage) IPC_MESSAGE_HANDLER(AtomViewHostMsg_Message, OnRendererMessage)
IPC_MESSAGE_HANDLER_DELAY_REPLY(AtomViewHostMsg_Message_Sync, IPC_MESSAGE_HANDLER_DELAY_REPLY(AtomViewHostMsg_Message_Sync,
OnRendererMessageSync) OnRendererMessageSync)
IPC_MESSAGE_HANDLER(AtomViewHostMsg_ZoomLevelChanged, OnZoomLevelChanged)
IPC_MESSAGE_UNHANDLED(handled = false) IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP() IPC_END_MESSAGE_MAP()
@ -1154,15 +1143,6 @@ void WebContents::OnRendererMessageSync(const base::string16& channel,
EmitWithSender(base::UTF16ToUTF8(channel), web_contents(), message, args); EmitWithSender(base::UTF16ToUTF8(channel), web_contents(), message, args);
} }
void WebContents::OnZoomLevelChanged(double level) {
auto manager = web_contents()->GetBrowserContext()->GetGuestManager();
if (!manager)
return;
manager->ForEachGuest(web_contents(),
base::Bind(&NotifyZoomLevelChanged,
level));
}
// static // static
mate::Handle<WebContents> WebContents::CreateFrom( mate::Handle<WebContents> WebContents::CreateFrom(
v8::Isolate* isolate, content::WebContents* web_contents) { v8::Isolate* isolate, content::WebContents* web_contents) {

View file

@ -263,10 +263,6 @@ class WebContents : public mate::TrackableObject<WebContents>,
const base::ListValue& args, const base::ListValue& args,
IPC::Message* message); IPC::Message* message);
// Called when guests need to be notified of
// embedders' zoom level change.
void OnZoomLevelChanged(double level);
v8::Global<v8::Value> session_; v8::Global<v8::Value> session_;
v8::Global<v8::Value> devtools_web_contents_; v8::Global<v8::Value> devtools_web_contents_;

View file

@ -59,6 +59,9 @@ PDFPageSize = {
const webFrameMethods = [ const webFrameMethods = [
'executeJavaScript', 'executeJavaScript',
'insertText', 'insertText',
'setZoomFactor',
'setZoomLevel',
'setZoomLevelLimits',
]; ];
wrapWebContents = function(webContents) { wrapWebContents = function(webContents) {

View file

@ -30,12 +30,6 @@ IPC_SYNC_MESSAGE_ROUTED2_1(AtomViewHostMsg_Message_Sync,
base::ListValue /* arguments */, base::ListValue /* arguments */,
base::string16 /* result (in JSON) */) base::string16 /* result (in JSON) */)
IPC_MESSAGE_ROUTED1(AtomViewHostMsg_ZoomLevelChanged,
double /* level */)
IPC_MESSAGE_ROUTED1(AtomViewMsg_SetZoomLevel,
double /* level */)
IPC_MESSAGE_ROUTED2(AtomViewMsg_Message, IPC_MESSAGE_ROUTED2(AtomViewMsg_Message,
base::string16 /* channel */, base::string16 /* channel */,
base::ListValue /* arguments */) base::ListValue /* arguments */)

View file

@ -4,7 +4,7 @@
#include "atom/renderer/api/atom_api_web_frame.h" #include "atom/renderer/api/atom_api_web_frame.h"
#include "atom/common/api/api_messages.h" #include "atom/common/api/event_emitter_caller.h"
#include "atom/common/native_mate_converters/callback.h" #include "atom/common/native_mate_converters/callback.h"
#include "atom/common/native_mate_converters/gfx_converter.h" #include "atom/common/native_mate_converters/gfx_converter.h"
#include "atom/common/native_mate_converters/string16_converter.h" #include "atom/common/native_mate_converters/string16_converter.h"
@ -38,11 +38,9 @@ void WebFrame::SetName(const std::string& name) {
} }
double WebFrame::SetZoomLevel(double level) { double WebFrame::SetZoomLevel(double level) {
auto render_view = content::RenderView::FromWebView(web_frame_->view()); double ret = web_frame_->view()->setZoomLevel(level);
// Notify guests if any for zoom level change. mate::EmitEvent(isolate(), GetWrapper(isolate()), "zoom-level-changed", ret);
render_view->Send( return ret;
new AtomViewHostMsg_ZoomLevelChanged(MSG_ROUTING_NONE, level));
return web_frame_->view()->setZoomLevel(level);
} }
double WebFrame::GetZoomLevel() const { double WebFrame::GetZoomLevel() const {

View file

@ -1,16 +1,19 @@
var deprecate, webFrame; 'use strict';
deprecate = require('electron').deprecate; const deprecate = require('electron').deprecate;
const EventEmitter = require('events').EventEmitter;
webFrame = process.atomBinding('web_frame').webFrame; const webFrame = process.atomBinding('web_frame').webFrame;
// webFrame is an EventEmitter.
webFrame.__proto__ = EventEmitter.prototype;
/* Deprecated. */ // Lots of webview would subscribe to webFrame's events.
webFrame.setMaxListeners(0);
// Deprecated.
deprecate.rename(webFrame, 'registerUrlSchemeAsSecure', 'registerURLSchemeAsSecure'); deprecate.rename(webFrame, 'registerUrlSchemeAsSecure', 'registerURLSchemeAsSecure');
deprecate.rename(webFrame, 'registerUrlSchemeAsBypassingCSP', 'registerURLSchemeAsBypassingCSP'); deprecate.rename(webFrame, 'registerUrlSchemeAsBypassingCSP', 'registerURLSchemeAsBypassingCSP');
deprecate.rename(webFrame, 'registerUrlSchemeAsPrivileged', 'registerURLSchemeAsPrivileged'); deprecate.rename(webFrame, 'registerUrlSchemeAsPrivileged', 'registerURLSchemeAsPrivileged');
module.exports = webFrame; module.exports = webFrame;

View file

@ -57,22 +57,6 @@ class AtomRenderFrameObserver : public content::RenderFrameObserver {
render_frame()->GetWebFrame(), context); render_frame()->GetWebFrame(), context);
} }
bool OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(AtomRenderFrameObserver, message)
IPC_MESSAGE_HANDLER(AtomViewMsg_SetZoomLevel, OnSetZoomLevel)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void OnSetZoomLevel(double level) {
auto view = render_frame()->GetWebFrame()->view();
if (view)
view->setZoomLevel(level);
}
private: private:
AtomRendererClient* renderer_client_; AtomRendererClient* renderer_client_;

View file

@ -1,3 +1,5 @@
'user strict';
var WebViewImpl, deprecate, getNextId, guestViewInternal, ipcRenderer, listener, nextId, ref, registerBrowserPluginElement, registerWebViewElement, remote, useCapture, v8Util, webFrame, webViewConstants, var WebViewImpl, deprecate, getNextId, guestViewInternal, ipcRenderer, listener, nextId, ref, registerBrowserPluginElement, registerWebViewElement, remote, useCapture, v8Util, webFrame, webViewConstants,
hasProp = {}.hasOwnProperty, hasProp = {}.hasOwnProperty,
slice = [].slice; slice = [].slice;
@ -39,6 +41,11 @@ WebViewImpl = (function() {
this.setupFocusPropagation(); this.setupFocusPropagation();
this.viewInstanceId = getNextId(); this.viewInstanceId = getNextId();
shadowRoot.appendChild(this.browserPluginNode); shadowRoot.appendChild(this.browserPluginNode);
// Subscribe to host's zoom level changes.
webFrame.on('zoom-level-changed', (zoomLevel) => {
this.webviewNode.setZoomLevel(zoomLevel);
});
} }
WebViewImpl.prototype.createBrowserPluginNode = function() { WebViewImpl.prototype.createBrowserPluginNode = function() {
@ -382,6 +389,9 @@ registerWebViewElement = function() {
'insertText', 'insertText',
'send', 'send',
'sendInputEvent', 'sendInputEvent',
'setZoomFactor',
'setZoomLevel',
'setZoomLevelLimits',
]; ];
/* Forward proto.foo* method calls to WebViewImpl.foo*. */ /* Forward proto.foo* method calls to WebViewImpl.foo*. */