Use webContents id as guest id in opened windows
This commit is contained in:
parent
ebf9773269
commit
bbc3a71c9f
1 changed files with 25 additions and 25 deletions
|
@ -1,7 +1,6 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const ipcMain = require('electron').ipcMain
|
const {BrowserWindow, ipcMain, webContents} = require('electron')
|
||||||
const BrowserWindow = require('electron').BrowserWindow
|
|
||||||
|
|
||||||
const hasProp = {}.hasOwnProperty
|
const hasProp = {}.hasOwnProperty
|
||||||
const frameToGuest = {}
|
const frameToGuest = {}
|
||||||
|
@ -56,15 +55,14 @@ const createGuest = function (embedder, url, frameName, options) {
|
||||||
if (options.webPreferences == null) {
|
if (options.webPreferences == null) {
|
||||||
options.webPreferences = {}
|
options.webPreferences = {}
|
||||||
}
|
}
|
||||||
const embedderWindow = BrowserWindow.fromWebContents(embedder)
|
options.webPreferences.openerId = embedder.id
|
||||||
options.webPreferences.openerId = embedderWindow != null ? embedderWindow.id : void 0
|
|
||||||
guest = new BrowserWindow(options)
|
guest = new BrowserWindow(options)
|
||||||
guest.loadURL(url)
|
guest.loadURL(url)
|
||||||
|
|
||||||
// When |embedder| is destroyed we should also destroy attached guest, and if
|
// When |embedder| is destroyed we should also destroy attached guest, and if
|
||||||
// guest is closed by user then we should prevent |embedder| from double
|
// guest is closed by user then we should prevent |embedder| from double
|
||||||
// closing guest.
|
// closing guest.
|
||||||
const guestId = guest.id
|
const guestId = guest.webContents.id
|
||||||
|
|
||||||
const closedByEmbedder = function () {
|
const closedByEmbedder = function () {
|
||||||
guest.removeListener('closed', closedByUser)
|
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.
|
// 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) {
|
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()
|
if (guestWindow != null) guestWindow.destroy()
|
||||||
})
|
})
|
||||||
|
|
||||||
ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', function (event, guestId, method, ...args) {
|
ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', function (event, guestId, method, ...args) {
|
||||||
const guestWindow = BrowserWindow.fromId(guestId)
|
const guestWindow = getGuestWindow(guestId)
|
||||||
event.returnValue = guestWindow != null ? guestWindow[method].apply(guestWindow, args) : void 0
|
event.returnValue = guestWindow != null ? guestWindow[method].apply(guestWindow, args) : null
|
||||||
})
|
})
|
||||||
|
|
||||||
ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', function (event, guestId, message, targetOrigin, sourceOrigin) {
|
ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', function (event, guestId, message, targetOrigin, sourceOrigin) {
|
||||||
const sourceContents = BrowserWindow.fromWebContents(event.sender)
|
const guestContents = webContents.fromId(guestId)
|
||||||
const sourceId = sourceContents != null ? sourceContents.id : void 0
|
if (guestContents == null) return
|
||||||
if (sourceId == null) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const guestWindow = BrowserWindow.fromId(guestId)
|
if (guestContents.getURL().indexOf(targetOrigin) === 0 || targetOrigin === '*') {
|
||||||
const guestContents = guestWindow != null ? guestWindow.webContents : void 0
|
const sourceId = event.sender.id
|
||||||
if ((guestContents != null ? guestContents.getURL().indexOf(targetOrigin) : void 0) === 0 || targetOrigin === '*') {
|
guestContents.send('ELECTRON_GUEST_WINDOW_POSTMESSAGE', sourceId, message, sourceOrigin)
|
||||||
guestContents != null ? guestContents.send('ELECTRON_GUEST_WINDOW_POSTMESSAGE', sourceId, message, sourceOrigin) : void 0
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', function (event, guestId, method, ...args) {
|
ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', function (event, guestId, method, ...args) {
|
||||||
const guestWindow = BrowserWindow.fromId(guestId)
|
const guestContents = webContents.fromId(guestId)
|
||||||
if (guestWindow != null) {
|
if (guestContents != null) guestContents[method].apply(guestContents, args)
|
||||||
const guestContents = guestWindow.webContents
|
|
||||||
if (guestContents != null) {
|
|
||||||
guestContents[method].apply(guestContents, args)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue