Add new-webview event that can be prevented

This commit is contained in:
Kevin Sawicki 2017-02-03 12:46:22 -08:00
parent 706b9f6cbf
commit 7eda8fbdfb

View file

@ -147,7 +147,8 @@ const createGuest = function (embedder, params) {
}
// Attach the guest to an element of embedder.
const attachGuest = function (embedder, elementInstanceId, guestInstanceId, params) {
const attachGuest = function (event, elementInstanceId, guestInstanceId, params) {
const embedder = event.sender
// Destroy the old guest when attaching.
const key = `${embedder.getId()}-${elementInstanceId}`
const oldGuestInstanceId = embedderElementsMap[key]
@ -204,6 +205,14 @@ const attachGuest = function (embedder, elementInstanceId, guestInstanceId, para
if (params.preload) {
webPreferences.preloadURL = params.preload
}
event.sender.emit('new-webview', event, guest, webPreferences)
if (event.defaultPrevented) {
if (guest.viewInstanceId == null) guest.viewInstanceId = params.instanceId
destroyGuest(embedder, guestInstanceId)
return
}
webViewManager.addGuest(guestInstanceId, elementInstanceId, embedder, guest, webPreferences)
guest.attachParams = params
embedderElementsMap[key] = guestInstanceId
@ -276,7 +285,7 @@ ipcMain.on('ELECTRON_GUEST_VIEW_MANAGER_CREATE_GUEST', function (event, params,
})
ipcMain.on('ELECTRON_GUEST_VIEW_MANAGER_ATTACH_GUEST', function (event, elementInstanceId, guestInstanceId, params) {
attachGuest(event.sender, elementInstanceId, guestInstanceId, params)
attachGuest(event, elementInstanceId, guestInstanceId, params)
})
ipcMain.on('ELECTRON_GUEST_VIEW_MANAGER_DESTROY_GUEST', function (event, guestInstanceId) {