From 218d69d288abf3d177794ffc60d7eddd902461c1 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sat, 25 Oct 2014 09:05:50 +0800 Subject: [PATCH] Dispatch WebContents events to --- atom/browser/lib/guest-view-manager.coffee | 18 +++++++++++++++++ atom/renderer/lib/guest-view-internal.coffee | 21 ++++++++++++++++++++ atom/renderer/lib/web-view.coffee | 5 +---- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/atom/browser/lib/guest-view-manager.coffee b/atom/browser/lib/guest-view-manager.coffee index 51419f63b212..279873254958 100644 --- a/atom/browser/lib/guest-view-manager.coffee +++ b/atom/browser/lib/guest-view-manager.coffee @@ -2,6 +2,17 @@ ipc = require 'ipc' webContents = require 'web-contents' webViewManager = null # Doesn't exist in early initialization. +supportedWebViewEvents = [ + 'did-finish-load' + 'did-fail-load' + 'did-frame-finish-load' + 'did-start-loading' + 'did-stop-loading' + 'did-get-redirect-request' + 'crashed' + 'destroyed' +] + nextInstanceId = 0 guestInstances = {} @@ -27,6 +38,7 @@ createGuest = (embedder, params) -> # Init guest web view after attached. guest.once 'did-attach', (event, params) -> + @viewInstanceId = params.instanceId min = width: params.minwidth, height: params.minheight max = width: params.maxwidth, height: params.maxheight @setAutoSize params.autosize, min, max @@ -35,6 +47,12 @@ createGuest = (embedder, params) -> if params.allowtransparency? @setAllowTransparency params.allowtransparency + # Dispatch events to embedder. + for event in supportedWebViewEvents + do (event) -> + guest.on event, (_, args...) -> + embedder.send "ATOM_SHELL_GUEST_VIEW_INTERNAL_DISPATCH_EVENT-#{guest.viewInstanceId}", event, args... + id # Destroy an existing guest instance. diff --git a/atom/renderer/lib/guest-view-internal.coffee b/atom/renderer/lib/guest-view-internal.coffee index 7fc2de3f2dde..69a67ae9b755 100644 --- a/atom/renderer/lib/guest-view-internal.coffee +++ b/atom/renderer/lib/guest-view-internal.coffee @@ -2,7 +2,28 @@ ipc = require 'ipc' requestId = 0 +WEB_VIEW_EVENTS = + 'did-finish-load': [] + 'did-fail-load': ['errorCode', 'errorDescription'] + 'did-frame-finish-load': ['isMainFrame'] + 'did-start-loading': [] + 'did-stop-loading': [] + 'did-get-redirect-request': ['oldUrl', 'newUrl', 'isMainFrame'] + 'crashed': [] + 'destroyed': [] + +dispatchEvent = (webView, event, args...) -> + throw new Error("Unkown event #{event}") unless WEB_VIEW_EVENTS[event]? + domEvent = new Event(event) + for f, i in WEB_VIEW_EVENTS[event] + domEvent[f] = args[i] + webView.webviewNode.dispatchEvent domEvent + module.exports = + registerEvents: (webView, viewInstanceId) -> + ipc.on "ATOM_SHELL_GUEST_VIEW_INTERNAL_DISPATCH_EVENT-#{viewInstanceId}", (event, args...) -> + dispatchEvent webView, event, args... + createGuest: (type, params, callback) -> requestId++ ipc.send 'ATOM_SHELL_GUEST_VIEW_MANAGER_CREATE_GUEST', type, params, requestId diff --git a/atom/renderer/lib/web-view.coffee b/atom/renderer/lib/web-view.coffee index 0c16485d1d41..1ebce7a4bd59 100644 --- a/atom/renderer/lib/web-view.coffee +++ b/atom/renderer/lib/web-view.coffee @@ -98,6 +98,7 @@ class WebView @setupWebviewNodeProperties() @viewInstanceId = getNextId() + guestViewInternal.registerEvents this, @viewInstanceId shadowRoot.appendChild @browserPluginNode @@ -515,10 +516,6 @@ registerWebViewElement = -> "executeJavaScript" "insertCSS" "send" - # "getZoom" - # "print" - # "setZoom" - # "terminate" ] # Forward proto.foo* method calls to WebView.foo*.