'new-window' event handlers can return their window when default behavior is prevented

This commit is contained in:
Michael Vasseur 2016-09-16 13:33:02 +02:00
Родитель ebaeec1677
Коммит 5a4cdcfde1
1 изменённых файлов: 28 добавлений и 20 удалений

Просмотреть файл

@ -43,20 +43,8 @@ const mergeBrowserWindowOptions = function (embedder, options) {
return options
}
// Create a new guest created by |embedder| with |options|.
const createGuest = function (embedder, url, frameName, options) {
let 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)
// Setup a new guest with |embedder|
const setupGuest = function (embedder, frameName, guest) {
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
@ -81,12 +69,10 @@ const createGuest = function (embedder, url, frameName, options) {
// here, since the window would be cleared of all changes in the next tick.
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.webContents.id
const guestId = guest.id
const closedByEmbedder = function () {
guest.removeListener('closed', closedByUser)
guest.destroy()
@ -108,7 +94,6 @@ const createGuest = function (embedder, url, frameName, options) {
embedder.once('render-view-deleted', closedByEmbedder)
guest.once('closed', closedByUser)
}
if (frameName) {
frameToGuest[frameName] = guest
guest.frameName = frameName
@ -116,8 +101,26 @@ const createGuest = function (embedder, url, frameName, options) {
delete frameToGuest[frameName]
})
}
return guest.id
}
return guestId
// 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)
guest.loadURL(url)
return setupGuest(embedder, frameName, guest)
}
const getGuestWindow = function (guestId) {
@ -138,8 +141,13 @@ const getGuestWindow = function (guestId) {
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)
var newGuest = event.newGuest
if ((event.sender.isGuest() && !event.sender.allowPopups) || event.defaultPrevented) {
event.returnValue = null
if (newGuest != undefined && newGuest != null) {
event.returnValue = setupGuest(event.sender, frameName, newGuest)
} else {
event.returnValue = null
}
} else {
event.returnValue = createGuest(event.sender, url, frameName, options)
}