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-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Doesn't exist in early initialization.
|
2016-03-24 20:15:04 +00:00
|
|
|
var webViewManager = null
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-25 16:37:15 +00:00
|
|
|
var supportedWebViewEvents = [
|
|
|
|
'load-commit',
|
|
|
|
'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-01-30 11:19:18 +00:00
|
|
|
'did-change-theme-color'
|
2016-03-24 20:15:04 +00:00
|
|
|
]
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
var nextInstanceId = 0
|
|
|
|
var guestInstances = {}
|
|
|
|
var embedderElementsMap = {}
|
|
|
|
var reverseEmbedderElementsMap = {}
|
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-03-24 20:15:04 +00:00
|
|
|
var moveLastToFirst = function (list) {
|
|
|
|
return list.unshift(list.pop())
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Generate guestInstanceId.
|
2016-03-24 20:15:04 +00:00
|
|
|
var getNextInstanceId = function () {
|
|
|
|
return ++nextInstanceId
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Create a new guest instance.
|
2016-03-24 20:15:04 +00:00
|
|
|
var createGuest = function (embedder, params) {
|
|
|
|
var destroy, destroyEvents, event, fn, guest, i, id, j, len, len1, listeners
|
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-03-24 20:15:04 +00:00
|
|
|
id = getNextInstanceId(embedder)
|
2016-01-12 02:40:23 +00:00
|
|
|
guest = webContents.create({
|
|
|
|
isGuest: true,
|
|
|
|
partition: params.partition,
|
|
|
|
embedder: embedder
|
2016-03-24 20:15:04 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
guestInstances[id] = {
|
|
|
|
guest: guest,
|
|
|
|
embedder: 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 guest when the embedder is gone or navigated.
|
2016-03-24 20:15:04 +00:00
|
|
|
destroyEvents = ['will-destroy', 'crashed', 'did-navigate']
|
|
|
|
destroy = function () {
|
2016-01-12 02:40:23 +00:00
|
|
|
if (guestInstances[id] != null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
return destroyGuest(embedder, id)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
for (i = 0, len = destroyEvents.length; i < len; i++) {
|
2016-03-24 20:15:04 +00:00
|
|
|
event = destroyEvents[i]
|
|
|
|
embedder.once(event, destroy)
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:44:21 +00:00
|
|
|
// 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.
|
2016-03-24 20:15:04 +00:00
|
|
|
listeners = embedder._events[event]
|
2016-01-12 02:40:23 +00:00
|
|
|
if (Array.isArray(listeners)) {
|
2016-03-24 20:15:04 +00:00
|
|
|
moveLastToFirst(listeners)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
guest.once('destroyed', function () {
|
|
|
|
var j, len1, results
|
|
|
|
results = []
|
2016-01-12 02:40:23 +00:00
|
|
|
for (j = 0, len1 = destroyEvents.length; j < len1; j++) {
|
2016-03-24 20:15:04 +00:00
|
|
|
event = destroyEvents[j]
|
|
|
|
results.push(embedder.removeListener(event, destroy))
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
return results
|
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Init guest web view after attached.
|
2016-03-24 20:15:04 +00:00
|
|
|
guest.once('did-attach', function () {
|
|
|
|
var opts
|
|
|
|
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-24 20:15:04 +00:00
|
|
|
return guest.allowPopups = params.allowpopups
|
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Dispatch events to embedder.
|
2016-03-24 20:15:04 +00:00
|
|
|
fn = function (event) {
|
|
|
|
return guest.on(event, function (_, ...args) {
|
|
|
|
return embedder.send.apply(embedder, ['ATOM_SHELL_GUEST_VIEW_INTERNAL_DISPATCH_EVENT-' + guest.viewInstanceId, event].concat(args))
|
|
|
|
})
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
for (j = 0, len1 = supportedWebViewEvents.length; j < len1; j++) {
|
2016-03-24 20:15:04 +00:00
|
|
|
event = supportedWebViewEvents[j]
|
|
|
|
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]) {
|
|
|
|
return embedder.send.apply(embedder, ['ATOM_SHELL_GUEST_VIEW_INTERNAL_IPC_MESSAGE-' + guest.viewInstanceId, channel].concat(args))
|
|
|
|
})
|
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) {
|
|
|
|
return embedder.send.apply(embedder, ['ATOM_SHELL_GUEST_VIEW_INTERNAL_SIZE_CHANGED-' + guest.viewInstanceId].concat(args))
|
|
|
|
})
|
|
|
|
return id
|
|
|
|
}
|
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-03-24 20:15:04 +00:00
|
|
|
var attachGuest = function (embedder, elementInstanceId, guestInstanceId, params) {
|
|
|
|
var guest, key, oldGuestInstanceId, ref1, webPreferences
|
|
|
|
guest = guestInstances[guestInstanceId].guest
|
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-01-14 18:35:29 +00:00
|
|
|
// Reattachment to the same guest is not currently supported.
|
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
|
|
|
}
|
|
|
|
if (guestInstances[oldGuestInstanceId] == 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
|
|
|
destroyGuest(embedder, oldGuestInstanceId)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
webPreferences = {
|
|
|
|
guestInstanceId: guestInstanceId,
|
|
|
|
nodeIntegration: (ref1 = params.nodeintegration) != null ? ref1 : false,
|
|
|
|
plugins: params.plugins,
|
2016-01-21 10:13:41 +00:00
|
|
|
webSecurity: !params.disablewebsecurity,
|
2016-01-31 21:35:34 +00:00
|
|
|
blinkFeatures: params.blinkfeatures
|
2016-03-24 20:15:04 +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
|
|
|
|
return reverseEmbedderElementsMap[guestInstanceId] = key
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Destroy an existing guest instance.
|
2016-03-24 20:15:04 +00:00
|
|
|
var destroyGuest = function (embedder, id) {
|
|
|
|
var key
|
|
|
|
webViewManager.removeGuest(embedder, id)
|
|
|
|
guestInstances[id].guest.destroy()
|
|
|
|
delete guestInstances[id]
|
|
|
|
key = reverseEmbedderElementsMap[id]
|
2016-01-12 02:40:23 +00:00
|
|
|
if (key != null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
delete reverseEmbedderElementsMap[id]
|
|
|
|
return delete embedderElementsMap[key]
|
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_VIEW_MANAGER_CREATE_GUEST', function (event, params, requestId) {
|
|
|
|
return event.sender.send('ATOM_SHELL_RESPONSE_' + requestId, createGuest(event.sender, params))
|
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
ipcMain.on('ATOM_SHELL_GUEST_VIEW_MANAGER_ATTACH_GUEST', function (event, elementInstanceId, guestInstanceId, params) {
|
|
|
|
return attachGuest(event.sender, elementInstanceId, guestInstanceId, params)
|
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
ipcMain.on('ATOM_SHELL_GUEST_VIEW_MANAGER_DESTROY_GUEST', function (event, id) {
|
|
|
|
return destroyGuest(event.sender, id)
|
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
ipcMain.on('ATOM_SHELL_GUEST_VIEW_MANAGER_SET_SIZE', function (event, id, params) {
|
|
|
|
var ref1
|
|
|
|
return (ref1 = guestInstances[id]) != null ? ref1.guest.setSize(params) : void 0
|
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Returns WebContents from its guest id.
|
2016-03-24 20:15:04 +00:00
|
|
|
exports.getGuest = function (id) {
|
|
|
|
var ref1
|
|
|
|
return (ref1 = guestInstances[id]) != null ? ref1.guest : void 0
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Returns the embedder of the guest.
|
2016-03-24 20:15:04 +00:00
|
|
|
exports.getEmbedder = function (id) {
|
|
|
|
var ref1
|
|
|
|
return (ref1 = guestInstances[id]) != null ? ref1.embedder : void 0
|
|
|
|
}
|