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 webContents = require('electron').webContents
|
2016-10-25 01:21:42 +00:00
|
|
|
const parseFeaturesString = require('../common/parse-features-string')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Doesn't exist in early initialization.
|
2016-06-07 17:29:24 +00:00
|
|
|
let webViewManager = null
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-06-07 17:29:24 +00:00
|
|
|
const supportedWebViewEvents = [
|
2016-01-25 16:37:15 +00:00
|
|
|
'load-commit',
|
2016-09-08 17:01:01 +00:00
|
|
|
'did-attach',
|
2016-01-25 16:37:15 +00:00
|
|
|
'did-finish-load',
|
|
|
|
'did-fail-load',
|
|
|
|
'did-frame-finish-load',
|
|
|
|
'did-start-loading',
|
|
|
|
'did-stop-loading',
|
|
|
|
'did-get-response-details',
|
|
|
|
'did-get-redirect-request',
|
|
|
|
'dom-ready',
|
|
|
|
'console-message',
|
|
|
|
'devtools-opened',
|
|
|
|
'devtools-closed',
|
|
|
|
'devtools-focused',
|
|
|
|
'new-window',
|
|
|
|
'will-navigate',
|
|
|
|
'did-navigate',
|
|
|
|
'did-navigate-in-page',
|
|
|
|
'close',
|
|
|
|
'crashed',
|
|
|
|
'gpu-crashed',
|
|
|
|
'plugin-crashed',
|
|
|
|
'destroyed',
|
|
|
|
'page-title-updated',
|
|
|
|
'page-favicon-updated',
|
|
|
|
'enter-html-full-screen',
|
|
|
|
'leave-html-full-screen',
|
|
|
|
'media-started-playing',
|
|
|
|
'media-paused',
|
|
|
|
'found-in-page',
|
2016-06-07 06:56:19 +00:00
|
|
|
'did-change-theme-color',
|
|
|
|
'update-target-url'
|
2016-03-24 20:15:04 +00:00
|
|
|
]
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-09-08 17:01:01 +00:00
|
|
|
let nextGuestInstanceId = 0
|
2016-06-07 17:29:24 +00:00
|
|
|
const guestInstances = {}
|
|
|
|
const embedderElementsMap = {}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Moves the last element of array to the first one.
|
2016-06-07 17:29:24 +00:00
|
|
|
const moveLastToFirst = function (list) {
|
2016-03-24 20:15:04 +00:00
|
|
|
return list.unshift(list.pop())
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Generate guestInstanceId.
|
2016-09-08 17:01:01 +00:00
|
|
|
const getNextGuestInstanceId = function () {
|
|
|
|
return ++nextGuestInstanceId
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Create a new guest instance.
|
2016-06-07 17:29:24 +00:00
|
|
|
const createGuest = function (embedder, params) {
|
2016-01-12 02:40:23 +00:00
|
|
|
if (webViewManager == null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
webViewManager = process.atomBinding('web_view_manager')
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-06-07 17:24:48 +00:00
|
|
|
|
2016-09-08 17:01:01 +00:00
|
|
|
const guestInstanceId = getNextGuestInstanceId(embedder)
|
2016-06-07 17:24:48 +00:00
|
|
|
const guest = webContents.create({
|
2016-06-16 01:54:28 +00:00
|
|
|
isGuest: true,
|
2016-01-12 02:40:23 +00:00
|
|
|
partition: params.partition,
|
|
|
|
embedder: embedder
|
2016-03-24 20:15:04 +00:00
|
|
|
})
|
2016-09-08 17:01:01 +00:00
|
|
|
guestInstances[guestInstanceId] = {
|
2016-01-12 02:40:23 +00:00
|
|
|
guest: guest,
|
|
|
|
embedder: embedder
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-09-08 17:01:01 +00:00
|
|
|
watchEmbedder(embedder)
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Init guest web view after attached.
|
2016-09-08 17:01:01 +00:00
|
|
|
guest.on('did-attach', function () {
|
2016-06-07 17:29:24 +00:00
|
|
|
let opts
|
2016-03-24 20:15:04 +00:00
|
|
|
params = this.attachParams
|
|
|
|
delete this.attachParams
|
|
|
|
this.viewInstanceId = params.instanceId
|
2016-01-12 02:40:23 +00:00
|
|
|
this.setSize({
|
|
|
|
normal: {
|
|
|
|
width: params.elementWidth,
|
|
|
|
height: params.elementHeight
|
|
|
|
},
|
|
|
|
enableAutoSize: params.autosize,
|
|
|
|
min: {
|
|
|
|
width: params.minwidth,
|
|
|
|
height: params.minheight
|
|
|
|
},
|
|
|
|
max: {
|
|
|
|
width: params.maxwidth,
|
|
|
|
height: params.maxheight
|
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
if (params.src) {
|
2016-03-24 20:15:04 +00:00
|
|
|
opts = {}
|
2016-01-12 02:40:23 +00:00
|
|
|
if (params.httpreferrer) {
|
2016-03-24 20:15:04 +00:00
|
|
|
opts.httpReferrer = params.httpreferrer
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (params.useragent) {
|
2016-03-24 20:15:04 +00:00
|
|
|
opts.userAgent = params.useragent
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
this.loadURL(params.src, opts)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-29 00:40:40 +00:00
|
|
|
guest.allowPopups = params.allowpopups
|
2016-03-24 20:15:04 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Dispatch events to embedder.
|
2016-06-07 17:24:48 +00:00
|
|
|
const fn = function (event) {
|
|
|
|
guest.on(event, function (_, ...args) {
|
2016-09-08 17:01:01 +00:00
|
|
|
const embedder = getEmbedder(guestInstanceId)
|
|
|
|
if (!embedder) {
|
|
|
|
return
|
|
|
|
}
|
2016-05-19 22:28:08 +00:00
|
|
|
embedder.send.apply(embedder, ['ELECTRON_GUEST_VIEW_INTERNAL_DISPATCH_EVENT-' + guest.viewInstanceId, event].concat(args))
|
2016-03-24 20:15:04 +00:00
|
|
|
})
|
|
|
|
}
|
2016-06-07 17:24:48 +00:00
|
|
|
for (const event of supportedWebViewEvents) {
|
2016-03-24 20:15:04 +00:00
|
|
|
fn(event)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Dispatch guest's IPC messages to embedder.
|
2016-03-24 20:15:04 +00:00
|
|
|
guest.on('ipc-message-host', function (_, [channel, ...args]) {
|
2016-09-08 17:01:01 +00:00
|
|
|
const embedder = getEmbedder(guestInstanceId)
|
|
|
|
if (!embedder) {
|
|
|
|
return
|
|
|
|
}
|
2016-05-19 22:28:08 +00:00
|
|
|
embedder.send.apply(embedder, ['ELECTRON_GUEST_VIEW_INTERNAL_IPC_MESSAGE-' + guest.viewInstanceId, channel].concat(args))
|
2016-03-24 20:15:04 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Autosize.
|
2016-03-24 20:15:04 +00:00
|
|
|
guest.on('size-changed', function (_, ...args) {
|
2016-09-08 17:01:01 +00:00
|
|
|
const embedder = getEmbedder(guestInstanceId)
|
|
|
|
if (!embedder) {
|
|
|
|
return
|
|
|
|
}
|
2016-05-19 22:28:08 +00:00
|
|
|
embedder.send.apply(embedder, ['ELECTRON_GUEST_VIEW_INTERNAL_SIZE_CHANGED-' + guest.viewInstanceId].concat(args))
|
2016-03-24 20:15:04 +00:00
|
|
|
})
|
2016-06-07 17:24:48 +00:00
|
|
|
|
2016-09-08 17:01:01 +00:00
|
|
|
return guestInstanceId
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Attach the guest to an element of embedder.
|
2016-06-07 17:29:24 +00:00
|
|
|
const attachGuest = function (embedder, elementInstanceId, guestInstanceId, params) {
|
2016-09-08 17:01:01 +00:00
|
|
|
let guest, guestInstance, key, oldKey, oldGuestInstanceId, ref1, webPreferences
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Destroy the old guest when attaching.
|
2016-03-24 20:15:04 +00:00
|
|
|
key = (embedder.getId()) + '-' + elementInstanceId
|
|
|
|
oldGuestInstanceId = embedderElementsMap[key]
|
2016-01-12 02:40:23 +00:00
|
|
|
if (oldGuestInstanceId != null) {
|
2016-09-08 17:01:01 +00:00
|
|
|
// Reattachment to the same guest is just a no-op.
|
2016-01-12 02:40:23 +00:00
|
|
|
if (oldGuestInstanceId === guestInstanceId) {
|
2016-03-24 20:15:04 +00:00
|
|
|
return
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-09-08 17:01:01 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
destroyGuest(embedder, oldGuestInstanceId)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-09-08 17:01:01 +00:00
|
|
|
|
|
|
|
guestInstance = guestInstances[guestInstanceId]
|
|
|
|
// If this isn't a valid guest instance then do nothing.
|
|
|
|
if (!guestInstance) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guest = guestInstance.guest
|
|
|
|
|
|
|
|
// If this guest is already attached to an element then remove it
|
|
|
|
if (guestInstance.elementInstanceId) {
|
|
|
|
oldKey = (guestInstance.embedder.getId()) + '-' + guestInstance.elementInstanceId
|
|
|
|
delete embedderElementsMap[oldKey]
|
|
|
|
webViewManager.removeGuest(guestInstance.embedder, guestInstanceId)
|
|
|
|
guestInstance.embedder.send('ELECTRON_GUEST_VIEW_INTERNAL_DESTROY_GUEST-' + guest.viewInstanceId)
|
|
|
|
}
|
|
|
|
|
2016-01-12 02:40:23 +00:00
|
|
|
webPreferences = {
|
|
|
|
guestInstanceId: guestInstanceId,
|
|
|
|
nodeIntegration: (ref1 = params.nodeintegration) != null ? ref1 : false,
|
|
|
|
plugins: params.plugins,
|
2016-05-19 21:32:16 +00:00
|
|
|
zoomFactor: params.zoomFactor,
|
2016-01-21 10:13:41 +00:00
|
|
|
webSecurity: !params.disablewebsecurity,
|
2016-06-07 20:49:31 +00:00
|
|
|
blinkFeatures: params.blinkfeatures,
|
2016-06-07 23:35:23 +00:00
|
|
|
disableBlinkFeatures: params.disableblinkfeatures
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-04-21 18:52:10 +00:00
|
|
|
|
2016-10-14 22:16:39 +00:00
|
|
|
// parse the 'webpreferences' attribute string, if set
|
|
|
|
// this uses the same parsing rules as window.open uses for its features
|
|
|
|
if (typeof params.webpreferences === 'string') {
|
2016-10-25 01:21:42 +00:00
|
|
|
parseFeaturesString(params.webpreferences, function (key, value) {
|
2016-10-14 22:16:39 +00:00
|
|
|
if (value === undefined) {
|
|
|
|
// no value was specified, default it to true
|
|
|
|
value = true
|
|
|
|
}
|
|
|
|
webPreferences[name] = value
|
2016-10-25 01:21:42 +00:00
|
|
|
})
|
2016-10-14 22:16:39 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 02:40:23 +00:00
|
|
|
if (params.preload) {
|
2016-03-24 20:15:04 +00:00
|
|
|
webPreferences.preloadURL = params.preload
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
webViewManager.addGuest(guestInstanceId, elementInstanceId, embedder, guest, webPreferences)
|
|
|
|
guest.attachParams = params
|
|
|
|
embedderElementsMap[key] = guestInstanceId
|
2016-09-08 17:01:01 +00:00
|
|
|
|
|
|
|
guest.setEmbedder(embedder)
|
|
|
|
guestInstance.embedder = embedder
|
|
|
|
guestInstance.elementInstanceId = elementInstanceId
|
|
|
|
|
|
|
|
watchEmbedder(embedder)
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Destroy an existing guest instance.
|
2016-09-08 17:01:01 +00:00
|
|
|
const destroyGuest = function (embedder, guestInstanceId) {
|
|
|
|
if (!(guestInstanceId in guestInstances)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let guestInstance = guestInstances[guestInstanceId]
|
|
|
|
if (embedder !== guestInstance.embedder) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
webViewManager.removeGuest(embedder, guestInstanceId)
|
|
|
|
guestInstance.guest.destroy()
|
|
|
|
delete guestInstances[guestInstanceId]
|
|
|
|
|
|
|
|
const key = embedder.getId() + '-' + guestInstance.elementInstanceId
|
|
|
|
return delete embedderElementsMap[key]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Once an embedder has had a guest attached we watch it for destruction to
|
|
|
|
// destroy any remaining guests.
|
|
|
|
const watchedEmbedders = new Set()
|
|
|
|
const watchEmbedder = function (embedder) {
|
|
|
|
if (watchedEmbedders.has(embedder)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
watchedEmbedders.add(embedder)
|
|
|
|
|
|
|
|
const destroyEvents = ['will-destroy', 'crashed', 'did-navigate']
|
|
|
|
const destroy = function () {
|
|
|
|
for (const guestInstanceId of Object.keys(guestInstances)) {
|
|
|
|
if (guestInstances[guestInstanceId].embedder === embedder) {
|
|
|
|
destroyGuest(embedder, parseInt(guestInstanceId))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const event of destroyEvents) {
|
|
|
|
embedder.removeListener(event, destroy)
|
|
|
|
}
|
|
|
|
|
|
|
|
watchedEmbedders.delete(embedder)
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const event of destroyEvents) {
|
|
|
|
embedder.once(event, destroy)
|
|
|
|
|
|
|
|
// Users might also listen to the crashed event, so we must ensure the guest
|
|
|
|
// is destroyed before users' listener gets called. It is done by moving our
|
|
|
|
// listener to the first one in queue.
|
|
|
|
const listeners = embedder._events[event]
|
|
|
|
if (Array.isArray(listeners)) {
|
|
|
|
moveLastToFirst(listeners)
|
|
|
|
}
|
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-04-06 23:21:26 +00:00
|
|
|
ipcMain.on('ELECTRON_GUEST_VIEW_MANAGER_CREATE_GUEST', function (event, params, requestId) {
|
2016-05-19 22:28:08 +00:00
|
|
|
event.sender.send('ELECTRON_RESPONSE_' + requestId, createGuest(event.sender, params))
|
2016-03-24 20:15:04 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-04-06 23:21:26 +00:00
|
|
|
ipcMain.on('ELECTRON_GUEST_VIEW_MANAGER_ATTACH_GUEST', function (event, elementInstanceId, guestInstanceId, params) {
|
2016-05-19 22:28:08 +00:00
|
|
|
attachGuest(event.sender, elementInstanceId, guestInstanceId, params)
|
2016-03-24 20:15:04 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-09-08 17:01:01 +00:00
|
|
|
ipcMain.on('ELECTRON_GUEST_VIEW_MANAGER_DESTROY_GUEST', function (event, guestInstanceId) {
|
|
|
|
destroyGuest(event.sender, guestInstanceId)
|
2016-03-24 20:15:04 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-09-08 17:01:01 +00:00
|
|
|
ipcMain.on('ELECTRON_GUEST_VIEW_MANAGER_SET_SIZE', function (event, guestInstanceId, params) {
|
|
|
|
const guestInstance = guestInstances[guestInstanceId]
|
2016-06-07 17:29:24 +00:00
|
|
|
return guestInstance != null ? guestInstance.guest.setSize(params) : void 0
|
2016-03-24 20:15:04 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Returns WebContents from its guest id.
|
2016-09-08 17:01:01 +00:00
|
|
|
exports.getGuest = function (guestInstanceId) {
|
|
|
|
const guestInstance = guestInstances[guestInstanceId]
|
2016-06-07 17:29:24 +00:00
|
|
|
return guestInstance != null ? guestInstance.guest : void 0
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Returns the embedder of the guest.
|
2016-09-08 17:01:01 +00:00
|
|
|
const getEmbedder = function (guestInstanceId) {
|
|
|
|
const guestInstance = guestInstances[guestInstanceId]
|
2016-06-07 17:29:24 +00:00
|
|
|
return guestInstance != null ? guestInstance.embedder : void 0
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-09-08 17:01:01 +00:00
|
|
|
exports.getEmbedder = getEmbedder
|