From 8386baf267545157584588f244bdfa8a75652555 Mon Sep 17 00:00:00 2001 From: Robo Date: Wed, 24 Feb 2016 15:41:09 +0530 Subject: [PATCH] add spec --- atom/browser/api/lib/ipc-main.js | 4 ---- atom/browser/api/lib/web-contents.js | 33 ++++++++++------------------ atom/renderer/lib/init.js | 5 ++--- spec/api-browser-window-spec.js | 19 ++++++++++++++++ spec/static/main.js | 11 ++++++++++ 5 files changed, 44 insertions(+), 28 deletions(-) diff --git a/atom/browser/api/lib/ipc-main.js b/atom/browser/api/lib/ipc-main.js index d6a043a92bfe..e253e03eaabe 100644 --- a/atom/browser/api/lib/ipc-main.js +++ b/atom/browser/api/lib/ipc-main.js @@ -1,7 +1,3 @@ const EventEmitter = require('events').EventEmitter; module.exports = new EventEmitter; - -// Every webContents would add a listenter to the -// WEB_FRAME_RESPONSE event, so ignore the listenters warning. -module.exports.setMaxListeners(0); diff --git a/atom/browser/api/lib/web-contents.js b/atom/browser/api/lib/web-contents.js index ed8afef7a526..7cd54bfddfb3 100644 --- a/atom/browser/api/lib/web-contents.js +++ b/atom/browser/api/lib/web-contents.js @@ -67,10 +67,6 @@ const webFrameMethods = [ 'setZoomLevelLimits' ]; -const asyncWebFrameMethods = [ - 'executeJavaScript', -]; - let wrapWebContents = function(webContents) { // webContents is an EventEmitter. var controller, method, name, ref1; @@ -112,37 +108,32 @@ let wrapWebContents = function(webContents) { }; } - for (let method of asyncWebFrameMethods) { - webContents[method] = function() { - let args = Array.prototype.slice.call(arguments); - this.send('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', method, args); - }; - } + const asyncWebFrameMethods = function(requestId, method, ...args) { + this.send('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', requestId, method, args); + ipcMain.once('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_RESPONSE_' + requestId, function(event, result) { + if (responseCallback[requestId]) { + responseCallback[requestId](result); + delete responseCallback[requestId]; + } + }); + }; // Make sure webContents.executeJavaScript would run the code only when the // webContents has been loaded. - const executeJavaScript = webContents.executeJavaScript; webContents.executeJavaScript = function(code, hasUserGesture, callback) { let requestId = getNextId(); if (typeof hasUserGesture === "function") { callback = hasUserGesture; hasUserGesture = false; } - if (callback !== null) + if (callback != null) responseCallback[requestId] = callback; if (this.getURL() && !this.isLoading()) - return executeJavaScript.call(this, requestId, code, hasUserGesture); + return asyncWebFrameMethods.call(this, requestId, "executeJavaScript", code, hasUserGesture); else - return this.once('did-finish-load', executeJavaScript.bind(this, requestId, code, hasUserGesture)); + return this.once('did-finish-load', asyncWebFrameMethods.bind(this, requestId, "executeJavaScript", code, hasUserGesture)); }; - ipcMain.on('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_RESPONSE', function(event, id, result) { - if (responseCallback[id]) { - responseCallback[id].apply(null, [result]); - delete responseCallback[id]; - } - }); - // Dispatch IPC messages to the ipc module. webContents.on('ipc-message', function(event, packed) { var args, channel; diff --git a/atom/renderer/lib/init.js b/atom/renderer/lib/init.js index f7f78e84a1be..cf42aa27229e 100644 --- a/atom/renderer/lib/init.js +++ b/atom/renderer/lib/init.js @@ -36,10 +36,9 @@ electron.ipcRenderer.on('ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', (event, m electron.webFrame[method].apply(electron.webFrame, args); }); -electron.ipcRenderer.on('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', (event, method, args) => { - let requestId = args.shift(); +electron.ipcRenderer.on('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', (event, requestId, method, args) => { const responseCallback = function(result) { - event.sender.send('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_RESPONSE', requestId, result); + event.sender.send('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_RESPONSE_' + requestId, result); }; args.push(responseCallback); electron.webFrame[method].apply(electron.webFrame, args); diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index 58cf57cc8d0c..25e92382ccaf 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -10,6 +10,7 @@ const screen = require('electron').screen; const app = remote.require('electron').app; const ipcMain = remote.require('electron').ipcMain; +const ipcRenderer = require('electron').ipcRenderer; const BrowserWindow = remote.require('electron').BrowserWindow; const isCI = remote.getGlobal('isCi'); @@ -690,4 +691,22 @@ describe('browser-window module', function() { assert.equal(fs.existsSync(serializedPath), false); }); }); + + describe('window.webContents.executeJavaScript', function() { + var expected = 'hello, world!'; + var code = '(() => \"' + expected + '\")()'; + + it('doesnt throw when no calback is provided', function() { + const result = ipcRenderer.sendSync('executeJavaScript', code, false); + assert.equal(result, 'success'); + }); + + it('returns result when calback is provided', function(done) { + ipcRenderer.send('executeJavaScript', code, true); + ipcRenderer.once('executeJavaScript-response', function(event, result) { + assert.equal(result, expected); + done(); + }); + }); + }); }); diff --git a/spec/static/main.js b/spec/static/main.js index 48fdf17c3dcf..9a049e3f1078 100644 --- a/spec/static/main.js +++ b/spec/static/main.js @@ -138,4 +138,15 @@ app.on('ready', function() { }); event.returnValue = "done"; }); + + ipcMain.on('executeJavaScript', function(event, code, hasCallback) { + if (hasCallback) { + window.webContents.executeJavaScript(code, (result) => { + window.webContents.send('executeJavaScript-response', result); + }); + } else { + window.webContents.executeJavaScript(code); + event.returnValue = "success"; + } + }); });