From af02739c4e1aea0a115e1bb0c3610d71c0ddf5ab Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 13 Jan 2016 12:46:13 +0800 Subject: [PATCH] Change webview's zoom level on JavaScript side --- atom/browser/api/atom_api_web_contents.cc | 20 -------------------- atom/browser/api/atom_api_web_contents.h | 4 ---- atom/browser/api/lib/web-contents.js | 3 +++ atom/common/api/api_messages.h | 6 ------ atom/renderer/api/atom_api_web_frame.cc | 10 ++++------ atom/renderer/api/lib/web-frame.js | 15 +++++++++------ atom/renderer/atom_renderer_client.cc | 16 ---------------- atom/renderer/lib/web-view/web-view.js | 10 ++++++++++ 8 files changed, 26 insertions(+), 58 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 614148f5067e..c4de932d37cc 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -33,7 +33,6 @@ #include "chrome/browser/printing/print_view_manager_basic.h" #include "chrome/browser/printing/print_preview_message_handler.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/native_web_keyboard_event.h" #include "content/public/browser/navigation_details.h" @@ -75,15 +74,6 @@ void SetUserAgentInIO(scoped_refptr getter, 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 mate { @@ -625,7 +615,6 @@ bool WebContents::OnMessageReceived(const IPC::Message& message) { IPC_MESSAGE_HANDLER(AtomViewHostMsg_Message, OnRendererMessage) IPC_MESSAGE_HANDLER_DELAY_REPLY(AtomViewHostMsg_Message_Sync, OnRendererMessageSync) - IPC_MESSAGE_HANDLER(AtomViewHostMsg_ZoomLevelChanged, OnZoomLevelChanged) IPC_MESSAGE_UNHANDLED(handled = false) IPC_END_MESSAGE_MAP() @@ -1154,15 +1143,6 @@ void WebContents::OnRendererMessageSync(const base::string16& channel, 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 mate::Handle WebContents::CreateFrom( v8::Isolate* isolate, content::WebContents* web_contents) { diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index 59e16dcab85f..bcef57b9a4aa 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -263,10 +263,6 @@ class WebContents : public mate::TrackableObject, const base::ListValue& args, IPC::Message* message); - // Called when guests need to be notified of - // embedders' zoom level change. - void OnZoomLevelChanged(double level); - v8::Global session_; v8::Global devtools_web_contents_; diff --git a/atom/browser/api/lib/web-contents.js b/atom/browser/api/lib/web-contents.js index a670850f5372..f502076755f0 100644 --- a/atom/browser/api/lib/web-contents.js +++ b/atom/browser/api/lib/web-contents.js @@ -59,6 +59,9 @@ PDFPageSize = { const webFrameMethods = [ 'executeJavaScript', 'insertText', + 'setZoomFactor', + 'setZoomLevel', + 'setZoomLevelLimits', ]; wrapWebContents = function(webContents) { diff --git a/atom/common/api/api_messages.h b/atom/common/api/api_messages.h index 2c9bb3b2f3e4..eeb26614847b 100644 --- a/atom/common/api/api_messages.h +++ b/atom/common/api/api_messages.h @@ -30,12 +30,6 @@ IPC_SYNC_MESSAGE_ROUTED2_1(AtomViewHostMsg_Message_Sync, base::ListValue /* arguments */, base::string16 /* result (in JSON) */) -IPC_MESSAGE_ROUTED1(AtomViewHostMsg_ZoomLevelChanged, - double /* level */) - -IPC_MESSAGE_ROUTED1(AtomViewMsg_SetZoomLevel, - double /* level */) - IPC_MESSAGE_ROUTED2(AtomViewMsg_Message, base::string16 /* channel */, base::ListValue /* arguments */) diff --git a/atom/renderer/api/atom_api_web_frame.cc b/atom/renderer/api/atom_api_web_frame.cc index ca4732ff3651..c72882886b91 100644 --- a/atom/renderer/api/atom_api_web_frame.cc +++ b/atom/renderer/api/atom_api_web_frame.cc @@ -4,7 +4,7 @@ #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/gfx_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) { - auto render_view = content::RenderView::FromWebView(web_frame_->view()); - // Notify guests if any for zoom level change. - render_view->Send( - new AtomViewHostMsg_ZoomLevelChanged(MSG_ROUTING_NONE, level)); - return web_frame_->view()->setZoomLevel(level); + double ret = web_frame_->view()->setZoomLevel(level); + mate::EmitEvent(isolate(), GetWrapper(isolate()), "zoom-level-changed", ret); + return ret; } double WebFrame::GetZoomLevel() const { diff --git a/atom/renderer/api/lib/web-frame.js b/atom/renderer/api/lib/web-frame.js index 90d1c5a87bd2..8ae35d7b8ac6 100644 --- a/atom/renderer/api/lib/web-frame.js +++ b/atom/renderer/api/lib/web-frame.js @@ -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, 'registerUrlSchemeAsBypassingCSP', 'registerURLSchemeAsBypassingCSP'); - deprecate.rename(webFrame, 'registerUrlSchemeAsPrivileged', 'registerURLSchemeAsPrivileged'); module.exports = webFrame; diff --git a/atom/renderer/atom_renderer_client.cc b/atom/renderer/atom_renderer_client.cc index d9c364c3733f..15165efa330a 100644 --- a/atom/renderer/atom_renderer_client.cc +++ b/atom/renderer/atom_renderer_client.cc @@ -57,22 +57,6 @@ class AtomRenderFrameObserver : public content::RenderFrameObserver { 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: AtomRendererClient* renderer_client_; diff --git a/atom/renderer/lib/web-view/web-view.js b/atom/renderer/lib/web-view/web-view.js index 7f3c000dd7f0..458e79a84224 100644 --- a/atom/renderer/lib/web-view/web-view.js +++ b/atom/renderer/lib/web-view/web-view.js @@ -1,3 +1,5 @@ +'user strict'; + var WebViewImpl, deprecate, getNextId, guestViewInternal, ipcRenderer, listener, nextId, ref, registerBrowserPluginElement, registerWebViewElement, remote, useCapture, v8Util, webFrame, webViewConstants, hasProp = {}.hasOwnProperty, slice = [].slice; @@ -39,6 +41,11 @@ WebViewImpl = (function() { this.setupFocusPropagation(); this.viewInstanceId = getNextId(); 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() { @@ -382,6 +389,9 @@ registerWebViewElement = function() { 'insertText', 'send', 'sendInputEvent', + 'setZoomFactor', + 'setZoomLevel', + 'setZoomLevelLimits', ]; /* Forward proto.foo* method calls to WebViewImpl.foo*. */