electron/lib/browser/guest-window-manager.js

180 lines
6.6 KiB
JavaScript
Raw Normal View History

'use strict'
2016-03-18 18:51:02 +00:00
const {BrowserWindow, ipcMain, webContents} = require('electron')
2016-01-12 02:40:23 +00:00
const hasProp = {}.hasOwnProperty
const frameToGuest = {}
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Copy attribute of |parent| to |child| if it is not defined in |child|.
const mergeOptions = function (child, parent) {
let key, value
2016-01-12 02:40:23 +00:00
for (key in parent) {
if (!hasProp.call(parent, key)) continue
value = parent[key]
2016-01-12 02:40:23 +00:00
if (!(key in child)) {
if (typeof value === 'object') {
child[key] = mergeOptions({}, value)
2016-01-12 02:40:23 +00:00
} else {
child[key] = value
2016-01-12 02:40:23 +00:00
}
}
}
return child
}
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Merge |options| with the |embedder|'s window's options.
const mergeBrowserWindowOptions = function (embedder, options) {
2016-01-12 02:40:23 +00:00
if (embedder.browserWindowOptions != null) {
2016-01-14 18:35:29 +00:00
// Inherit the original options if it is a BrowserWindow.
mergeOptions(options, embedder.browserWindowOptions)
2016-01-12 02:40:23 +00:00
} else {
2016-01-14 18:35:29 +00:00
// Or only inherit web-preferences if it is a webview.
2016-01-12 02:40:23 +00:00
if (options.webPreferences == null) {
options.webPreferences = {}
2016-01-12 02:40:23 +00:00
}
mergeOptions(options.webPreferences, embedder.getWebPreferences())
2016-01-12 02:40:23 +00:00
}
// Disable node integration on child window if disabled on parent window
if (embedder.getWebPreferences().nodeIntegration === false) {
options.webPreferences.nodeIntegration = false
}
return options
}
2016-01-12 02:40:23 +00:00
// Setup a new guest with |embedder|
const setupGuest = function (embedder, frameName, guest) {
2016-09-29 13:37:28 +00:00
2016-01-14 18:44:21 +00:00
// 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 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)
}
2016-01-12 02:40:23 +00:00
if (frameName) {
frameToGuest[frameName] = guest
guest.frameName = frameName
guest.once('closed', function () {
2016-05-19 22:28:08 +00:00
delete frameToGuest[frameName]
})
2016-01-12 02:40:23 +00:00
}
return guest.id
}
// Create a new guest created by |embedder| with |options|.
const createGuest = function (embedder, url, frameName, options) {
const guest = frameToGuest[frameName]
if (frameName && (guest != null)) {
guest.loadURL(url)
return guest.id
}
// Remember the embedder window's id.
if (options.webPreferences == null) {
options.webPreferences = {}
}
options.webPreferences.openerId = embedder.id
guest = new BrowserWindow(options)
2016-09-29 13:37:28 +00:00
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('<h1>hello</h1>')
//
// The above code would not work if a navigation to "about:blank" is done
// here, since the window would be cleared of all changes in the next tick.
guest.loadURL(url)
}
return setupGuest(embedder, frameName, guest)
}
const getGuestWindow = function (guestId) {
const guestContents = webContents.fromId(guestId)
if (guestContents == null) return
let guestWindow = BrowserWindow.fromWebContents(guestContents)
2016-06-09 20:53:36 +00:00
if (guestWindow == null) {
const hostContents = guestContents.hostWebContents
if (hostContents != null) {
guestWindow = BrowserWindow.fromWebContents(hostContents)
}
}
return guestWindow
}
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Routed window.open messages.
ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_OPEN', function (event, url, frameName, disposition, options, additionalFeatures) {
options = mergeBrowserWindowOptions(event.sender, options)
event.sender.emit('new-window', event, url, frameName, disposition, options, additionalFeatures)
2016-09-29 12:41:35 +00:00
const newGuest = event.newGuest
2016-01-12 02:40:23 +00:00
if ((event.sender.isGuest() && !event.sender.allowPopups) || event.defaultPrevented) {
2016-09-29 12:41:35 +00:00
if (newGuest != undefined && newGuest != null) {
event.returnValue = setupGuest(event.sender, frameName, newGuest)
2016-09-29 12:41:35 +00:00
} else {
event.returnValue = null
2016-09-29 12:41:35 +00:00
}
2016-01-12 02:40:23 +00:00
} else {
2016-03-29 00:40:40 +00:00
event.returnValue = createGuest(event.sender, url, frameName, options)
2016-01-12 02:40:23 +00:00
}
})
2016-01-12 02:40:23 +00:00
ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSE', function (event, guestId) {
const guestWindow = getGuestWindow(guestId)
if (guestWindow != null) guestWindow.destroy()
})
2016-01-12 02:40:23 +00:00
ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', function (event, guestId, method, ...args) {
const guestWindow = getGuestWindow(guestId)
event.returnValue = guestWindow != null ? guestWindow[method].apply(guestWindow, args) : null
})
2016-01-12 02:40:23 +00:00
ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', function (event, guestId, message, targetOrigin, sourceOrigin) {
const guestContents = webContents.fromId(guestId)
if (guestContents == null) return
if (guestContents.getURL().indexOf(targetOrigin) === 0 || targetOrigin === '*') {
const sourceId = event.sender.id
guestContents.send('ELECTRON_GUEST_WINDOW_POSTMESSAGE', sourceId, message, sourceOrigin)
2016-01-12 02:40:23 +00:00
}
})
2016-01-12 02:40:23 +00:00
ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', function (event, guestId, method, ...args) {
const guestContents = webContents.fromId(guestId)
if (guestContents != null) guestContents[method].apply(guestContents, args)
})