From ebf977326992eafad08f1af7ddb18d44aaba7237 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 9 Jun 2016 10:35:48 -0700 Subject: [PATCH 1/6] Use const/let and removes CoffeeScript ref vars --- lib/browser/guest-window-manager.js | 56 +++++++++++++++++------------ 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/lib/browser/guest-window-manager.js b/lib/browser/guest-window-manager.js index a3d60bee8ad..c3767ad8c6f 100644 --- a/lib/browser/guest-window-manager.js +++ b/lib/browser/guest-window-manager.js @@ -3,12 +3,12 @@ const ipcMain = require('electron').ipcMain const BrowserWindow = require('electron').BrowserWindow -var hasProp = {}.hasOwnProperty -var frameToGuest = {} +const hasProp = {}.hasOwnProperty +const frameToGuest = {} // Copy attribute of |parent| to |child| if it is not defined in |child|. -var mergeOptions = function (child, parent) { - var key, value +const mergeOptions = function (child, parent) { + let key, value for (key in parent) { if (!hasProp.call(parent, key)) continue value = parent[key] @@ -24,7 +24,7 @@ var mergeOptions = function (child, parent) { } // Merge |options| with the |embedder|'s window's options. -var mergeBrowserWindowOptions = function (embedder, options) { +const mergeBrowserWindowOptions = function (embedder, options) { if (embedder.browserWindowOptions != null) { // Inherit the original options if it is a BrowserWindow. mergeOptions(options, embedder.browserWindowOptions) @@ -45,9 +45,8 @@ var mergeBrowserWindowOptions = function (embedder, options) { } // Create a new guest created by |embedder| with |options|. -var createGuest = function (embedder, url, frameName, options) { - var closedByEmbedder, closedByUser, guest, guestId, ref1 - guest = frameToGuest[frameName] +const createGuest = function (embedder, url, frameName, options) { + let guest = frameToGuest[frameName] if (frameName && (guest != null)) { guest.loadURL(url) return guest.id @@ -57,24 +56,27 @@ var createGuest = function (embedder, url, frameName, options) { if (options.webPreferences == null) { options.webPreferences = {} } - options.webPreferences.openerId = (ref1 = BrowserWindow.fromWebContents(embedder)) != null ? ref1.id : void 0 + const embedderWindow = BrowserWindow.fromWebContents(embedder) + options.webPreferences.openerId = embedderWindow != null ? embedderWindow.id : void 0 guest = new BrowserWindow(options) guest.loadURL(url) // When |embedder| is destroyed we should also destroy attached guest, and if // guest is closed by user then we should prevent |embedder| from double // closing guest. - guestId = guest.id - closedByEmbedder = function () { + const guestId = guest.id + + const closedByEmbedder = function () { guest.removeListener('closed', closedByUser) - return guest.destroy() + guest.destroy() } - closedByUser = function () { + const closedByUser = function () { embedder.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSED_' + guestId) - return embedder.removeListener('render-view-deleted', closedByEmbedder) + embedder.removeListener('render-view-deleted', closedByEmbedder) } embedder.once('render-view-deleted', closedByEmbedder) guest.once('closed', closedByUser) + if (frameName) { frameToGuest[frameName] = guest guest.frameName = frameName @@ -82,6 +84,7 @@ var createGuest = function (embedder, url, frameName, options) { delete frameToGuest[frameName] }) } + return guest.id } @@ -97,28 +100,35 @@ ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_OPEN', function (event, url, fr }) ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSE', function (event, guestId) { - var ref1 - (ref1 = BrowserWindow.fromId(guestId)) != null ? ref1.destroy() : void 0 + const guestWindow = BrowserWindow.fromId(guestId) + if (guestWindow != null) guestWindow.destroy() }) ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', function (event, guestId, method, ...args) { - var ref1 - event.returnValue = (ref1 = BrowserWindow.fromId(guestId)) != null ? ref1[method].apply(ref1, args) : void 0 + const guestWindow = BrowserWindow.fromId(guestId) + event.returnValue = guestWindow != null ? guestWindow[method].apply(guestWindow, args) : void 0 }) ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', function (event, guestId, message, targetOrigin, sourceOrigin) { - var guestContents, ref1, ref2, sourceId - sourceId = (ref1 = BrowserWindow.fromWebContents(event.sender)) != null ? ref1.id : void 0 + const sourceContents = BrowserWindow.fromWebContents(event.sender) + const sourceId = sourceContents != null ? sourceContents.id : void 0 if (sourceId == null) { return } - guestContents = (ref2 = BrowserWindow.fromId(guestId)) != null ? ref2.webContents : void 0 + + const guestWindow = BrowserWindow.fromId(guestId) + const guestContents = guestWindow != null ? guestWindow.webContents : void 0 if ((guestContents != null ? guestContents.getURL().indexOf(targetOrigin) : void 0) === 0 || targetOrigin === '*') { guestContents != null ? guestContents.send('ELECTRON_GUEST_WINDOW_POSTMESSAGE', sourceId, message, sourceOrigin) : void 0 } }) ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', function (event, guestId, method, ...args) { - var ref1, ref2 - (ref1 = BrowserWindow.fromId(guestId)) != null ? (ref2 = ref1.webContents) != null ? ref2[method].apply(ref2, args) : void 0 : void 0 + const guestWindow = BrowserWindow.fromId(guestId) + if (guestWindow != null) { + const guestContents = guestWindow.webContents + if (guestContents != null) { + guestContents[method].apply(guestContents, args) + } + } }) From bbc3a71c9ff59a4fc36c10e3abdaa0722ef63500 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 9 Jun 2016 11:29:38 -0700 Subject: [PATCH 2/6] Use webContents id as guest id in opened windows --- lib/browser/guest-window-manager.js | 50 ++++++++++++++--------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/lib/browser/guest-window-manager.js b/lib/browser/guest-window-manager.js index c3767ad8c6f..78c9158f8ec 100644 --- a/lib/browser/guest-window-manager.js +++ b/lib/browser/guest-window-manager.js @@ -1,7 +1,6 @@ 'use strict' -const ipcMain = require('electron').ipcMain -const BrowserWindow = require('electron').BrowserWindow +const {BrowserWindow, ipcMain, webContents} = require('electron') const hasProp = {}.hasOwnProperty const frameToGuest = {} @@ -56,15 +55,14 @@ const createGuest = function (embedder, url, frameName, options) { if (options.webPreferences == null) { options.webPreferences = {} } - const embedderWindow = BrowserWindow.fromWebContents(embedder) - options.webPreferences.openerId = embedderWindow != null ? embedderWindow.id : void 0 + options.webPreferences.openerId = embedder.id guest = new BrowserWindow(options) guest.loadURL(url) // When |embedder| is destroyed we should also destroy attached guest, and if // guest is closed by user then we should prevent |embedder| from double // closing guest. - const guestId = guest.id + const guestId = guest.webContents.id const closedByEmbedder = function () { guest.removeListener('closed', closedByUser) @@ -85,7 +83,18 @@ const createGuest = function (embedder, url, frameName, options) { }) } - return guest.id + return guestId +} + +const getGuestWindow = function (guestId) { + const guestContents = webContents.fromId(guestId) + if (guestContents == null) return + + let guestWindow = BrowserWindow.fromWebContents(guestContents) + if (guestWindow == null && guestContents.hostWebContents != null) { + guestWindow = BrowserWindow.fromWebContents(guestContents.hostWebContents) + } + return guestWindow } // Routed window.open messages. @@ -100,35 +109,26 @@ ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_OPEN', function (event, url, fr }) ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSE', function (event, guestId) { - const guestWindow = BrowserWindow.fromId(guestId) + const guestWindow = getGuestWindow(guestId) if (guestWindow != null) guestWindow.destroy() }) ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', function (event, guestId, method, ...args) { - const guestWindow = BrowserWindow.fromId(guestId) - event.returnValue = guestWindow != null ? guestWindow[method].apply(guestWindow, args) : void 0 + const guestWindow = getGuestWindow(guestId) + event.returnValue = guestWindow != null ? guestWindow[method].apply(guestWindow, args) : null }) ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', function (event, guestId, message, targetOrigin, sourceOrigin) { - const sourceContents = BrowserWindow.fromWebContents(event.sender) - const sourceId = sourceContents != null ? sourceContents.id : void 0 - if (sourceId == null) { - return - } + const guestContents = webContents.fromId(guestId) + if (guestContents == null) return - const guestWindow = BrowserWindow.fromId(guestId) - const guestContents = guestWindow != null ? guestWindow.webContents : void 0 - if ((guestContents != null ? guestContents.getURL().indexOf(targetOrigin) : void 0) === 0 || targetOrigin === '*') { - guestContents != null ? guestContents.send('ELECTRON_GUEST_WINDOW_POSTMESSAGE', sourceId, message, sourceOrigin) : void 0 + if (guestContents.getURL().indexOf(targetOrigin) === 0 || targetOrigin === '*') { + const sourceId = event.sender.id + guestContents.send('ELECTRON_GUEST_WINDOW_POSTMESSAGE', sourceId, message, sourceOrigin) } }) ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', function (event, guestId, method, ...args) { - const guestWindow = BrowserWindow.fromId(guestId) - if (guestWindow != null) { - const guestContents = guestWindow.webContents - if (guestContents != null) { - guestContents[method].apply(guestContents, args) - } - } + const guestContents = webContents.fromId(guestId) + if (guestContents != null) guestContents[method].apply(guestContents, args) }) From a184b6afb3443a0cded259ffe17ba3704f4ba962 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 9 Jun 2016 12:02:01 -0700 Subject: [PATCH 3/6] Add spec for window.open from --- spec/chromium-spec.js | 21 ++++++++++++++++++- .../pages/webview-opener-postMessage.html | 19 +++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/pages/webview-opener-postMessage.html diff --git a/spec/chromium-spec.js b/spec/chromium-spec.js index cd046ab29ab..83e8bdf3fc6 100644 --- a/spec/chromium-spec.js +++ b/spec/chromium-spec.js @@ -1,8 +1,8 @@ - const assert = require('assert') const http = require('http') const path = require('path') const ws = require('ws') +const url = require('url') const remote = require('electron').remote const BrowserWindow = remote.require('electron').BrowserWindow @@ -327,6 +327,25 @@ describe('chromium feature', function () { window.addEventListener('message', listener) b = window.open('file://' + fixtures + '/pages/window-opener-postMessage.html', '', 'show=no') }) + + it('works for windows opened from a ', function (done) { + const webview = new WebView() + webview.addEventListener('console-message', function (e) { + webview.remove() + assert.equal(e.message, 'message') + done() + }) + webview.allowpopups = true + webview.src = url.format({ + pathname: `${fixtures}/pages/webview-opener-postMessage.html`, + protocol: 'file', + query: { + p: `${fixtures}/pages/window-opener-postMessage.html` + }, + slashes: true + }) + document.body.appendChild(webview) + }) }) describe('creating a Uint8Array under browser side', function () { diff --git a/spec/fixtures/pages/webview-opener-postMessage.html b/spec/fixtures/pages/webview-opener-postMessage.html new file mode 100644 index 00000000000..e926e32bc07 --- /dev/null +++ b/spec/fixtures/pages/webview-opener-postMessage.html @@ -0,0 +1,19 @@ + + + + + + + + + From c8180ab301882f7459bcc7d01d792e1b79733cf5 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 9 Jun 2016 12:06:49 -0700 Subject: [PATCH 4/6] Update spec description --- spec/chromium-spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/chromium-spec.js b/spec/chromium-spec.js index 83e8bdf3fc6..be40f127ca2 100644 --- a/spec/chromium-spec.js +++ b/spec/chromium-spec.js @@ -328,7 +328,7 @@ describe('chromium feature', function () { b = window.open('file://' + fixtures + '/pages/window-opener-postMessage.html', '', 'show=no') }) - it('works for windows opened from a ', function (done) { + it('supports windows opened from a ', function (done) { const webview = new WebView() webview.addEventListener('console-message', function (e) { webview.remove() From 39180e65397f9861699352477b8bdd66b9c8779f Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 9 Jun 2016 13:37:50 -0700 Subject: [PATCH 5/6] Get webContents from guestId --- spec/chromium-spec.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/spec/chromium-spec.js b/spec/chromium-spec.js index be40f127ca2..939570d6abc 100644 --- a/spec/chromium-spec.js +++ b/spec/chromium-spec.js @@ -5,8 +5,7 @@ const ws = require('ws') const url = require('url') const remote = require('electron').remote -const BrowserWindow = remote.require('electron').BrowserWindow -const session = remote.require('electron').session +const {BrowserWindow, session, webContents} = remote const isCI = remote.getGlobal('isCi') @@ -235,7 +234,7 @@ describe('chromium feature', function () { else targetURL = 'file://' + fixtures + '/pages/base-page.html' b = window.open(targetURL) - BrowserWindow.fromId(b.guestId).webContents.once('did-finish-load', function () { + webContents.fromId(b.guestId).once('did-finish-load', function () { assert.equal(b.location, targetURL) b.close() done() @@ -246,10 +245,10 @@ describe('chromium feature', function () { // Load a page that definitely won't redirect var b b = window.open('about:blank') - BrowserWindow.fromId(b.guestId).webContents.once('did-finish-load', function () { + webContents.fromId(b.guestId).once('did-finish-load', function () { // When it loads, redirect b.location = 'file://' + fixtures + '/pages/base-page.html' - BrowserWindow.fromId(b.guestId).webContents.once('did-finish-load', function () { + webContents.fromId(b.guestId).once('did-finish-load', function () { // After our second redirect, cleanup and callback b.close() done() @@ -308,7 +307,7 @@ describe('chromium feature', function () { } window.addEventListener('message', listener) b = window.open('file://' + fixtures + '/pages/window-open-postMessage.html', '', 'show=no') - BrowserWindow.fromId(b.guestId).webContents.once('did-finish-load', function () { + webContents.fromId(b.guestId).once('did-finish-load', function () { b.postMessage('testing', '*') }) }) From 939ae567acb1cc7a35b0e614dfa6d1f813f74df3 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 9 Jun 2016 13:53:36 -0700 Subject: [PATCH 6/6] :art: --- lib/browser/guest-window-manager.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/browser/guest-window-manager.js b/lib/browser/guest-window-manager.js index 78c9158f8ec..776ab91fec9 100644 --- a/lib/browser/guest-window-manager.js +++ b/lib/browser/guest-window-manager.js @@ -91,8 +91,11 @@ const getGuestWindow = function (guestId) { if (guestContents == null) return let guestWindow = BrowserWindow.fromWebContents(guestContents) - if (guestWindow == null && guestContents.hostWebContents != null) { - guestWindow = BrowserWindow.fromWebContents(guestContents.hostWebContents) + if (guestWindow == null) { + const hostContents = guestContents.hostWebContents + if (hostContents != null) { + guestWindow = BrowserWindow.fromWebContents(hostContents) + } } return guestWindow }