'use strict' const {BrowserWindow, ipcMain, webContents} = require('electron') const hasProp = {}.hasOwnProperty const frameToGuest = {} // Copy attribute of |parent| to |child| if it is not defined in |child|. const mergeOptions = function (child, parent) { let key, value for (key in parent) { if (!hasProp.call(parent, key)) continue value = parent[key] if (!(key in child)) { if (typeof value === 'object') { child[key] = mergeOptions({}, value) } else { child[key] = value } } } return child } // Merge |options| with the |embedder|'s window's options. const mergeBrowserWindowOptions = function (embedder, options) { if (options.webPreferences == null) { options.webPreferences = {} } if (embedder.browserWindowOptions != null) { // Inherit the original options if it is a BrowserWindow. mergeOptions(options, embedder.browserWindowOptions) } else { // Or only inherit web-preferences if it is a webview. mergeOptions(options.webPreferences, embedder.getWebPreferences()) } // Disable node integration on child window if disabled on parent window if (embedder.getWebPreferences().nodeIntegration === false) { options.webPreferences.nodeIntegration = false } // Sets correct openerId here to give correct options to 'new-window' event handler options.webPreferences.openerId = embedder.id return options } // Setup a new guest with |embedder| const setupGuest = function (embedder, frameName, guest, options) { // 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.webContents.id const closedByEmbedder = function () { guest.removeListener('closed', closedByUser) guest.destroy() } const closedByUser = function () { embedder.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSED_' + guestId) embedder.removeListener('render-view-deleted', closedByEmbedder) } if (!options.webPreferences.sandbox) { // These events should only be handled when the guest window is opened by a // non-sandboxed renderer for two reasons: // // - `render-view-deleted` is emitted when the popup is closed by the user, // and that will eventually result in NativeWindow::NotifyWindowClosed // using a dangling pointer since `destroy()` would have been called by // `closeByEmbedded` // - No need to emit `ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSED_` since // there's no renderer code listening to it., embedder.once('render-view-deleted', closedByEmbedder) guest.once('closed', closedByUser) } if (frameName) { frameToGuest[frameName] = guest guest.frameName = frameName guest.once('closed', function () { delete frameToGuest[frameName] }) } return guestId } // Create a new guest created by |embedder| with |options|. const createGuest = function (embedder, url, frameName, options, postData) { let guest = frameToGuest[frameName] if (frameName && (guest != null)) { guest.loadURL(url) return guest.webContents.id } // Remember the embedder window's id. if (options.webPreferences == null) { options.webPreferences = {} } guest = new BrowserWindow(options) if (!options.webContents || url !== 'about:blank') { // We should not call `loadURL` if the window was constructed from an // existing webContents(window.open in a sandboxed renderer) and if the url // is not 'about:blank'. // // Navigating to the url when creating the window from an existing // webContents would not be necessary(it will navigate there anyway), but // apparently there's a bug that allows the child window to be scripted by // the opener, even when the child window is from another origin. // // That's why the second condition(url !== "about:blank") is required: to // force `OverrideSiteInstanceForNavigation` to be called and consequently // spawn a new renderer if the new window is targeting a different origin. // // If the URL is "about:blank", then it is very likely that the opener just // wants to synchronously script the popup, for example: // // let popup = window.open() // popup.document.body.write('