diff --git a/lib/browser/guest-window-manager.js b/lib/browser/guest-window-manager.js index c3767ad8c6..78c9158f8e 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) })