2016-03-24 20:15:04 +00:00
|
|
|
'use strict'
|
2016-03-18 18:51:02 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
const ipcMain = require('electron').ipcMain
|
|
|
|
const BrowserWindow = require('electron').BrowserWindow
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
var hasProp = {}.hasOwnProperty
|
|
|
|
var 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|.
|
2016-03-24 20:15:04 +00:00
|
|
|
var mergeOptions = function (child, parent) {
|
|
|
|
var key, value
|
2016-01-12 02:40:23 +00:00
|
|
|
for (key in parent) {
|
2016-03-24 20:15:04 +00:00
|
|
|
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') {
|
2016-03-24 20:15:04 +00:00
|
|
|
child[key] = mergeOptions({}, value)
|
2016-01-12 02:40:23 +00:00
|
|
|
} else {
|
2016-03-24 20:15:04 +00:00
|
|
|
child[key] = value
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-24 20:15:04 +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.
|
2016-03-24 20:15:04 +00:00
|
|
|
var 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.
|
2016-03-24 20:15:04 +00:00
|
|
|
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) {
|
2016-03-24 20:15:04 +00:00
|
|
|
options.webPreferences = {}
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
mergeOptions(options.webPreferences, embedder.getWebPreferences())
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-30 17:51:56 +00:00
|
|
|
|
|
|
|
// Disable node integration on child window if disabled on parent window
|
|
|
|
if (embedder.getWebPreferences().nodeIntegration === false) {
|
|
|
|
options.webPreferences.nodeIntegration = false
|
|
|
|
}
|
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
return options
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Create a new guest created by |embedder| with |options|.
|
2016-03-24 20:15:04 +00:00
|
|
|
var createGuest = function (embedder, url, frameName, options) {
|
|
|
|
var closedByEmbedder, closedByUser, guest, guestId, ref1
|
|
|
|
guest = frameToGuest[frameName]
|
2016-01-12 02:40:23 +00:00
|
|
|
if (frameName && (guest != null)) {
|
2016-03-24 20:15:04 +00:00
|
|
|
guest.loadURL(url)
|
|
|
|
return guest.id
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Remember the embedder window's id.
|
2016-01-12 02:40:23 +00:00
|
|
|
if (options.webPreferences == null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
options.webPreferences = {}
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
options.webPreferences.openerId = (ref1 = BrowserWindow.fromWebContents(embedder)) != null ? ref1.id : void 0
|
|
|
|
guest = new BrowserWindow(options)
|
|
|
|
guest.loadURL(url)
|
2016-01-12 02:40:23 +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.
|
2016-03-24 20:15:04 +00:00
|
|
|
guestId = guest.id
|
|
|
|
closedByEmbedder = function () {
|
|
|
|
guest.removeListener('closed', closedByUser)
|
|
|
|
return guest.destroy()
|
|
|
|
}
|
|
|
|
closedByUser = function () {
|
|
|
|
embedder.send('ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_CLOSED_' + guestId)
|
|
|
|
return embedder.removeListener('render-view-deleted', closedByEmbedder)
|
|
|
|
}
|
|
|
|
embedder.once('render-view-deleted', closedByEmbedder)
|
|
|
|
guest.once('closed', closedByUser)
|
2016-01-12 02:40:23 +00:00
|
|
|
if (frameName) {
|
2016-03-24 20:15:04 +00:00
|
|
|
frameToGuest[frameName] = guest
|
|
|
|
guest.frameName = frameName
|
|
|
|
guest.once('closed', function () {
|
|
|
|
return delete frameToGuest[frameName]
|
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
return guest.id
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Routed window.open messages.
|
2016-03-25 03:17:33 +00:00
|
|
|
ipcMain.on('ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPEN', function (event, url, frameName, disposition, options) {
|
2016-03-24 20:15:04 +00:00
|
|
|
options = mergeBrowserWindowOptions(event.sender, options)
|
2016-03-25 03:17:33 +00:00
|
|
|
event.sender.emit('new-window', event, url, frameName, disposition, options)
|
2016-01-12 02:40:23 +00:00
|
|
|
if ((event.sender.isGuest() && !event.sender.allowPopups) || event.defaultPrevented) {
|
2016-03-29 00:40:40 +00:00
|
|
|
event.returnValue = null
|
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-03-24 20:15:04 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
ipcMain.on('ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_CLOSE', function (event, guestId) {
|
|
|
|
var ref1
|
|
|
|
return (ref1 = BrowserWindow.fromId(guestId)) != null ? ref1.destroy() : void 0
|
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
ipcMain.on('ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_METHOD', function (event, guestId, method, ...args) {
|
|
|
|
var ref1
|
2016-03-29 00:40:40 +00:00
|
|
|
event.returnValue = (ref1 = BrowserWindow.fromId(guestId)) != null ? ref1[method].apply(ref1, args) : void 0
|
2016-03-24 20:15:04 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
ipcMain.on('ATOM_SHELL_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
|
2016-01-12 02:40:23 +00:00
|
|
|
if (sourceId == null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
return
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
guestContents = (ref2 = BrowserWindow.fromId(guestId)) != null ? ref2.webContents : void 0
|
2016-01-12 02:40:23 +00:00
|
|
|
if ((guestContents != null ? guestContents.getURL().indexOf(targetOrigin) : void 0) === 0 || targetOrigin === '*') {
|
2016-03-24 20:15:04 +00:00
|
|
|
return guestContents != null ? guestContents.send('ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', sourceId, message, sourceOrigin) : void 0
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
ipcMain.on('ATOM_SHELL_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', function (event, guestId, method, ...args) {
|
|
|
|
var ref1, ref2
|
|
|
|
return (ref1 = BrowserWindow.fromId(guestId)) != null ? (ref2 = ref1.webContents) != null ? ref2[method].apply(ref2, args) : void 0 : void 0
|
|
|
|
})
|