2016-03-24 20:34:36 +00:00
|
|
|
'use strict'
|
2016-01-13 09:07:18 +00:00
|
|
|
|
2016-03-24 20:34:36 +00:00
|
|
|
const electron = require('electron')
|
2018-05-18 02:09:25 +00:00
|
|
|
const {EventEmitter} = require('events')
|
2018-05-21 12:56:05 +00:00
|
|
|
const fs = require('fs')
|
2016-03-24 20:34:36 +00:00
|
|
|
const v8Util = process.atomBinding('v8_util')
|
2017-01-24 23:05:34 +00:00
|
|
|
|
2016-07-25 07:39:09 +00:00
|
|
|
const {ipcMain, isPromise, webContents} = electron
|
2016-05-11 07:15:32 +00:00
|
|
|
|
|
|
|
const objectsRegistry = require('./objects-registry')
|
2018-05-24 12:05:46 +00:00
|
|
|
const bufferUtils = require('../common/buffer-utils')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-05-27 17:46:02 +00:00
|
|
|
const hasProp = {}.hasOwnProperty
|
|
|
|
|
2016-02-22 02:52:21 +00:00
|
|
|
// The internal properties of Function.
|
|
|
|
const FUNCTION_PROPERTIES = [
|
2016-03-24 20:34:36 +00:00
|
|
|
'length', 'name', 'arguments', 'caller', 'prototype'
|
|
|
|
]
|
2016-02-22 02:52:21 +00:00
|
|
|
|
2016-02-22 06:36:50 +00:00
|
|
|
// The remote functions in renderer processes.
|
2016-04-26 07:10:27 +00:00
|
|
|
// id => Function
|
2016-05-11 11:40:48 +00:00
|
|
|
let rendererFunctions = v8Util.createDoubleIDWeakMap()
|
2016-02-22 06:36:50 +00:00
|
|
|
|
2016-02-22 02:52:21 +00:00
|
|
|
// Return the description of object's members:
|
2016-03-24 20:34:36 +00:00
|
|
|
let getObjectMembers = function (object) {
|
|
|
|
let names = Object.getOwnPropertyNames(object)
|
2016-02-22 02:52:21 +00:00
|
|
|
// For Function, we should not override following properties even though they
|
|
|
|
// are "own" properties.
|
|
|
|
if (typeof object === 'function') {
|
|
|
|
names = names.filter((name) => {
|
2016-03-24 20:34:36 +00:00
|
|
|
return !FUNCTION_PROPERTIES.includes(name)
|
|
|
|
})
|
2016-02-22 02:52:21 +00:00
|
|
|
}
|
|
|
|
// Map properties to descriptors.
|
|
|
|
return names.map((name) => {
|
2016-03-24 20:34:36 +00:00
|
|
|
let descriptor = Object.getOwnPropertyDescriptor(object, name)
|
|
|
|
let member = {name, enumerable: descriptor.enumerable, writable: false}
|
2016-02-22 02:52:21 +00:00
|
|
|
if (descriptor.get === undefined && typeof object[name] === 'function') {
|
2016-03-24 20:34:36 +00:00
|
|
|
member.type = 'method'
|
2016-02-22 02:52:21 +00:00
|
|
|
} else {
|
2016-03-24 20:34:36 +00:00
|
|
|
if (descriptor.set || descriptor.writable) member.writable = true
|
|
|
|
member.type = 'get'
|
2016-02-22 02:52:21 +00:00
|
|
|
}
|
2016-03-24 20:34:36 +00:00
|
|
|
return member
|
|
|
|
})
|
|
|
|
}
|
2016-02-22 02:52:21 +00:00
|
|
|
|
|
|
|
// Return the description of object's prototype.
|
2016-03-24 20:34:36 +00:00
|
|
|
let getObjectPrototype = function (object) {
|
|
|
|
let proto = Object.getPrototypeOf(object)
|
|
|
|
if (proto === null || proto === Object.prototype) return null
|
2016-02-22 02:52:21 +00:00
|
|
|
return {
|
2016-03-04 23:57:21 +00:00
|
|
|
members: getObjectMembers(proto),
|
2016-03-24 20:34:36 +00:00
|
|
|
proto: getObjectPrototype(proto)
|
|
|
|
}
|
|
|
|
}
|
2016-02-22 02:52:21 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Convert a real value into meta data.
|
2018-07-10 08:15:40 +00:00
|
|
|
let valueToMeta = function (sender, contextId, value, optimizeSimpleObject = false) {
|
2016-04-20 05:26:49 +00:00
|
|
|
// Determine the type of value.
|
2016-06-21 00:54:15 +00:00
|
|
|
const meta = { type: typeof value }
|
2016-04-20 05:32:32 +00:00
|
|
|
if (meta.type === 'object') {
|
2016-04-20 05:26:49 +00:00
|
|
|
// Recognize certain types of objects.
|
2016-04-20 05:32:32 +00:00
|
|
|
if (value === null) {
|
|
|
|
meta.type = 'value'
|
2018-05-24 12:05:46 +00:00
|
|
|
} else if (bufferUtils.isBuffer(value)) {
|
2016-04-20 05:32:32 +00:00
|
|
|
meta.type = 'buffer'
|
|
|
|
} else if (Array.isArray(value)) {
|
|
|
|
meta.type = 'array'
|
|
|
|
} else if (value instanceof Error) {
|
|
|
|
meta.type = 'error'
|
|
|
|
} else if (value instanceof Date) {
|
|
|
|
meta.type = 'date'
|
2016-05-25 05:38:35 +00:00
|
|
|
} else if (isPromise(value)) {
|
2016-04-20 05:26:49 +00:00
|
|
|
meta.type = 'promise'
|
2016-05-27 17:46:02 +00:00
|
|
|
} else if (hasProp.call(value, 'callee') && value.length != null) {
|
2016-04-20 05:26:49 +00:00
|
|
|
// Treat the arguments object as array.
|
|
|
|
meta.type = 'array'
|
|
|
|
} else if (optimizeSimpleObject && v8Util.getHiddenValue(value, 'simple')) {
|
|
|
|
// Treat simple objects as value.
|
|
|
|
meta.type = 'value'
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
|
2016-04-20 05:26:49 +00:00
|
|
|
// Fill the meta object according to value's type.
|
2016-01-12 02:40:23 +00:00
|
|
|
if (meta.type === 'array') {
|
2018-07-10 08:15:40 +00:00
|
|
|
meta.members = value.map((el) => valueToMeta(sender, contextId, el, optimizeSimpleObject))
|
2016-01-12 02:40:23 +00:00
|
|
|
} else if (meta.type === 'object' || meta.type === 'function') {
|
2016-04-20 05:26:49 +00:00
|
|
|
meta.name = value.constructor ? value.constructor.name : ''
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:44:21 +00:00
|
|
|
// Reference the original value if it's an object, because when it's
|
|
|
|
// passed to renderer we would assume the renderer keeps a reference of
|
|
|
|
// it.
|
2018-07-10 08:15:40 +00:00
|
|
|
meta.id = objectsRegistry.add(sender, contextId, value)
|
2016-03-24 20:34:36 +00:00
|
|
|
meta.members = getObjectMembers(value)
|
|
|
|
meta.proto = getObjectPrototype(value)
|
2016-01-12 02:40:23 +00:00
|
|
|
} else if (meta.type === 'buffer') {
|
2018-05-24 12:05:46 +00:00
|
|
|
meta.value = bufferUtils.bufferToMeta(value)
|
2016-01-12 02:40:23 +00:00
|
|
|
} else if (meta.type === 'promise') {
|
2016-06-21 00:54:15 +00:00
|
|
|
// Add default handler to prevent unhandled rejections in main process
|
|
|
|
// Instead they should appear in the renderer process
|
|
|
|
value.then(function () {}, function () {})
|
|
|
|
|
2018-07-10 08:15:40 +00:00
|
|
|
meta.then = valueToMeta(sender, contextId, function (onFulfilled, onRejected) {
|
2016-05-23 22:07:01 +00:00
|
|
|
value.then(onFulfilled, onRejected)
|
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
} else if (meta.type === 'error') {
|
2016-03-24 20:34:36 +00:00
|
|
|
meta.members = plainObjectToMeta(value)
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Error.name is not part of own properties.
|
2016-01-12 02:40:23 +00:00
|
|
|
meta.members.push({
|
|
|
|
name: 'name',
|
|
|
|
value: value.name
|
2016-03-24 20:34:36 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
} else if (meta.type === 'date') {
|
2016-03-24 20:34:36 +00:00
|
|
|
meta.value = value.getTime()
|
2016-01-12 02:40:23 +00:00
|
|
|
} else {
|
2016-03-24 20:34:36 +00:00
|
|
|
meta.type = 'value'
|
|
|
|
meta.value = value
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:34:36 +00:00
|
|
|
return meta
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Convert object to meta by value.
|
2016-06-21 00:54:15 +00:00
|
|
|
const plainObjectToMeta = function (obj) {
|
2016-03-24 20:34:36 +00:00
|
|
|
return Object.getOwnPropertyNames(obj).map(function (name) {
|
2016-01-12 02:40:23 +00:00
|
|
|
return {
|
|
|
|
name: name,
|
|
|
|
value: obj[name]
|
2016-03-24 20:34:36 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Convert Error into meta data.
|
2018-07-10 08:15:40 +00:00
|
|
|
const exceptionToMeta = function (sender, contextId, error) {
|
2016-01-12 02:40:23 +00:00
|
|
|
return {
|
|
|
|
type: 'exception',
|
|
|
|
message: error.message,
|
2018-04-24 12:40:19 +00:00
|
|
|
stack: error.stack || error,
|
2018-07-10 08:15:40 +00:00
|
|
|
cause: valueToMeta(sender, contextId, error.cause)
|
2016-03-24 20:34:36 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-11-17 17:55:13 +00:00
|
|
|
const throwRPCError = function (message) {
|
|
|
|
const error = new Error(message)
|
|
|
|
error.code = 'EBADRPC'
|
2016-11-17 18:22:29 +00:00
|
|
|
error.errno = -72
|
2016-11-17 17:55:13 +00:00
|
|
|
throw error
|
|
|
|
}
|
|
|
|
|
2018-05-18 02:09:25 +00:00
|
|
|
const removeRemoteListenersAndLogWarning = (sender, meta, callIntoRenderer) => {
|
2017-01-06 21:09:48 +00:00
|
|
|
let message = `Attempting to call a function in a renderer window that has been closed or released.` +
|
2017-01-06 22:14:52 +00:00
|
|
|
`\nFunction provided here: ${meta.location}`
|
2017-01-06 21:09:48 +00:00
|
|
|
|
2018-05-21 01:00:56 +00:00
|
|
|
if (sender instanceof EventEmitter) {
|
2017-01-19 00:09:46 +00:00
|
|
|
const remoteEvents = sender.eventNames().filter((eventName) => {
|
|
|
|
return sender.listeners(eventName).includes(callIntoRenderer)
|
2017-01-13 20:28:11 +00:00
|
|
|
})
|
2017-01-19 00:09:46 +00:00
|
|
|
|
|
|
|
if (remoteEvents.length > 0) {
|
|
|
|
message += `\nRemote event names: ${remoteEvents.join(', ')}`
|
|
|
|
remoteEvents.forEach((eventName) => {
|
|
|
|
sender.removeListener(eventName, callIntoRenderer)
|
|
|
|
})
|
|
|
|
}
|
2017-01-06 21:09:48 +00:00
|
|
|
}
|
|
|
|
|
2017-01-19 00:09:46 +00:00
|
|
|
console.warn(message)
|
2017-01-06 21:09:48 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Convert array of meta data from renderer into array of real values.
|
2018-07-10 08:15:40 +00:00
|
|
|
const unwrapArgs = function (sender, contextId, args) {
|
2016-06-21 00:54:15 +00:00
|
|
|
const metaToValue = function (meta) {
|
2016-01-12 02:40:23 +00:00
|
|
|
switch (meta.type) {
|
|
|
|
case 'value':
|
2016-03-24 20:34:36 +00:00
|
|
|
return meta.value
|
2016-01-12 02:40:23 +00:00
|
|
|
case 'remote-object':
|
2016-03-24 20:34:36 +00:00
|
|
|
return objectsRegistry.get(meta.id)
|
2016-01-12 02:40:23 +00:00
|
|
|
case 'array':
|
2018-07-10 08:15:40 +00:00
|
|
|
return unwrapArgs(sender, contextId, meta.value)
|
2016-01-12 02:40:23 +00:00
|
|
|
case 'buffer':
|
2018-05-24 12:05:46 +00:00
|
|
|
return bufferUtils.metaToBuffer(meta.value)
|
2016-01-12 02:40:23 +00:00
|
|
|
case 'date':
|
2016-03-24 20:34:36 +00:00
|
|
|
return new Date(meta.value)
|
2016-01-12 02:40:23 +00:00
|
|
|
case 'promise':
|
|
|
|
return Promise.resolve({
|
|
|
|
then: metaToValue(meta.then)
|
2016-03-24 20:34:36 +00:00
|
|
|
})
|
2016-03-25 19:28:43 +00:00
|
|
|
case 'object': {
|
2016-03-24 20:34:36 +00:00
|
|
|
let ret = {}
|
|
|
|
Object.defineProperty(ret.constructor, 'name', { value: meta.name })
|
2016-02-22 03:51:41 +00:00
|
|
|
|
2018-06-27 01:00:05 +00:00
|
|
|
for (const {name, value} of meta.members) {
|
|
|
|
ret[name] = metaToValue(value)
|
2016-03-24 20:34:36 +00:00
|
|
|
}
|
|
|
|
return ret
|
2016-03-25 19:28:43 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
case 'function-with-return-value':
|
2018-06-27 01:00:05 +00:00
|
|
|
const returnValue = metaToValue(meta.value)
|
2016-03-24 20:34:36 +00:00
|
|
|
return function () {
|
|
|
|
return returnValue
|
|
|
|
}
|
2016-03-25 19:28:43 +00:00
|
|
|
case 'function': {
|
2018-07-10 08:15:40 +00:00
|
|
|
// Merge contextId and meta.id, since meta.id can be the same in
|
2016-04-26 07:10:27 +00:00
|
|
|
// different webContents.
|
2018-07-10 08:15:40 +00:00
|
|
|
const objectId = [contextId, meta.id]
|
2016-04-26 07:10:27 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Cache the callbacks in renderer.
|
2016-04-26 07:10:27 +00:00
|
|
|
if (rendererFunctions.has(objectId)) {
|
|
|
|
return rendererFunctions.get(objectId)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-01-13 09:07:18 +00:00
|
|
|
|
2018-07-23 09:08:36 +00:00
|
|
|
const processId = sender.getProcessId()
|
2016-03-24 20:34:36 +00:00
|
|
|
let callIntoRenderer = function (...args) {
|
2018-07-23 09:08:36 +00:00
|
|
|
if (!sender.isDestroyed() && processId === sender.getProcessId()) {
|
2018-07-10 08:15:40 +00:00
|
|
|
sender.send('ELECTRON_RENDERER_CALLBACK', contextId, meta.id, valueToMeta(sender, contextId, args))
|
2016-03-24 20:34:36 +00:00
|
|
|
} else {
|
2018-05-18 02:09:25 +00:00
|
|
|
removeRemoteListenersAndLogWarning(this, meta, callIntoRenderer)
|
2016-03-24 20:34:36 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-09 13:05:46 +00:00
|
|
|
Object.defineProperty(callIntoRenderer, 'length', { value: meta.length })
|
2016-04-26 07:10:27 +00:00
|
|
|
|
2018-07-10 08:15:40 +00:00
|
|
|
v8Util.setRemoteCallbackFreer(callIntoRenderer, contextId, meta.id, sender)
|
2016-04-26 07:10:27 +00:00
|
|
|
rendererFunctions.set(objectId, callIntoRenderer)
|
2016-03-24 20:34:36 +00:00
|
|
|
return callIntoRenderer
|
2016-03-25 19:28:43 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
default:
|
2016-03-24 20:34:36 +00:00
|
|
|
throw new TypeError(`Unknown type: ${meta.type}`)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:34:36 +00:00
|
|
|
}
|
|
|
|
return args.map(metaToValue)
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:44:21 +00:00
|
|
|
// Call a function and send reply asynchronously if it's a an asynchronous
|
|
|
|
// style function and the caller didn't pass a callback.
|
2018-07-10 08:15:40 +00:00
|
|
|
const callFunction = function (event, contextId, func, caller, args) {
|
2018-06-27 01:00:05 +00:00
|
|
|
const funcMarkedAsync = v8Util.getHiddenValue(func, 'asynchronous')
|
|
|
|
const funcPassedCallback = typeof args[args.length - 1] === 'function'
|
2016-01-12 02:40:23 +00:00
|
|
|
try {
|
|
|
|
if (funcMarkedAsync && !funcPassedCallback) {
|
2016-03-24 20:34:36 +00:00
|
|
|
args.push(function (ret) {
|
2018-07-10 08:15:40 +00:00
|
|
|
event.returnValue = valueToMeta(event.sender, contextId, ret, true)
|
2016-03-24 20:34:36 +00:00
|
|
|
})
|
2016-06-21 00:54:15 +00:00
|
|
|
func.apply(caller, args)
|
2016-01-12 02:40:23 +00:00
|
|
|
} else {
|
2018-06-27 01:00:05 +00:00
|
|
|
const ret = func.apply(caller, args)
|
2018-07-10 08:15:40 +00:00
|
|
|
event.returnValue = valueToMeta(event.sender, contextId, ret, true)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-01-19 22:49:40 +00:00
|
|
|
} catch (error) {
|
2016-01-14 18:44:21 +00:00
|
|
|
// Catch functions thrown further down in function invocation and wrap
|
|
|
|
// them with the function name so it's easier to trace things like
|
|
|
|
// `Error processing argument -1.`
|
2018-06-27 01:00:05 +00:00
|
|
|
const funcName = func.name || 'anonymous'
|
|
|
|
const err = new Error(`Could not call remote function '${funcName}'. Check that the function signature is correct. Underlying error: ${error.message}`)
|
2018-04-24 12:40:19 +00:00
|
|
|
err.cause = error
|
|
|
|
throw err
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:34:36 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-07-10 08:15:40 +00:00
|
|
|
ipcMain.on('ELECTRON_BROWSER_REQUIRE', function (event, contextId, module) {
|
2016-01-12 02:40:23 +00:00
|
|
|
try {
|
2018-07-10 08:15:40 +00:00
|
|
|
event.returnValue = valueToMeta(event.sender, contextId, process.mainModule.require(module))
|
2016-01-19 22:49:40 +00:00
|
|
|
} catch (error) {
|
2018-07-10 08:15:40 +00:00
|
|
|
event.returnValue = exceptionToMeta(event.sender, contextId, error)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:34:36 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-07-10 08:15:40 +00:00
|
|
|
ipcMain.on('ELECTRON_BROWSER_GET_BUILTIN', function (event, contextId, module) {
|
2016-01-12 02:40:23 +00:00
|
|
|
try {
|
2018-07-10 08:15:40 +00:00
|
|
|
event.returnValue = valueToMeta(event.sender, contextId, electron[module])
|
2016-01-19 22:49:40 +00:00
|
|
|
} catch (error) {
|
2018-07-10 08:15:40 +00:00
|
|
|
event.returnValue = exceptionToMeta(event.sender, contextId, error)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:34:36 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-07-10 08:15:40 +00:00
|
|
|
ipcMain.on('ELECTRON_BROWSER_GLOBAL', function (event, contextId, name) {
|
2016-01-12 02:40:23 +00:00
|
|
|
try {
|
2018-07-10 08:15:40 +00:00
|
|
|
event.returnValue = valueToMeta(event.sender, contextId, global[name])
|
2016-01-19 22:49:40 +00:00
|
|
|
} catch (error) {
|
2018-07-10 08:15:40 +00:00
|
|
|
event.returnValue = exceptionToMeta(event.sender, contextId, error)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:34:36 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-07-10 08:15:40 +00:00
|
|
|
ipcMain.on('ELECTRON_BROWSER_CURRENT_WINDOW', function (event, contextId) {
|
2016-01-12 02:40:23 +00:00
|
|
|
try {
|
2018-07-10 08:15:40 +00:00
|
|
|
event.returnValue = valueToMeta(event.sender, contextId, event.sender.getOwnerBrowserWindow())
|
2016-01-19 22:49:40 +00:00
|
|
|
} catch (error) {
|
2018-07-10 08:15:40 +00:00
|
|
|
event.returnValue = exceptionToMeta(event.sender, contextId, error)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:34:36 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-07-10 08:15:40 +00:00
|
|
|
ipcMain.on('ELECTRON_BROWSER_CURRENT_WEB_CONTENTS', function (event, contextId) {
|
|
|
|
event.returnValue = valueToMeta(event.sender, contextId, event.sender)
|
2016-03-24 20:34:36 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-07-10 08:15:40 +00:00
|
|
|
ipcMain.on('ELECTRON_BROWSER_CONSTRUCTOR', function (event, contextId, id, args) {
|
2016-01-12 02:40:23 +00:00
|
|
|
try {
|
2018-07-10 08:15:40 +00:00
|
|
|
args = unwrapArgs(event.sender, contextId, args)
|
2016-03-24 20:34:36 +00:00
|
|
|
let constructor = objectsRegistry.get(id)
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-11-17 17:55:13 +00:00
|
|
|
if (constructor == null) {
|
|
|
|
throwRPCError(`Cannot call constructor on missing remote object ${id}`)
|
|
|
|
}
|
|
|
|
|
2018-07-10 08:15:40 +00:00
|
|
|
event.returnValue = valueToMeta(event.sender, contextId, new constructor(...args))
|
2016-01-19 22:49:40 +00:00
|
|
|
} catch (error) {
|
2018-07-10 08:15:40 +00:00
|
|
|
event.returnValue = exceptionToMeta(event.sender, contextId, error)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:34:36 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-07-10 08:15:40 +00:00
|
|
|
ipcMain.on('ELECTRON_BROWSER_FUNCTION_CALL', function (event, contextId, id, args) {
|
2016-01-12 02:40:23 +00:00
|
|
|
try {
|
2018-07-10 08:15:40 +00:00
|
|
|
args = unwrapArgs(event.sender, contextId, args)
|
2016-03-24 20:34:36 +00:00
|
|
|
let func = objectsRegistry.get(id)
|
2016-11-17 17:55:13 +00:00
|
|
|
|
|
|
|
if (func == null) {
|
|
|
|
throwRPCError(`Cannot call function on missing remote object ${id}`)
|
|
|
|
}
|
|
|
|
|
2018-07-10 08:15:40 +00:00
|
|
|
callFunction(event, contextId, func, global, args)
|
2016-01-19 22:49:40 +00:00
|
|
|
} catch (error) {
|
2018-07-10 08:15:40 +00:00
|
|
|
event.returnValue = exceptionToMeta(event.sender, contextId, error)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:34:36 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-07-10 08:15:40 +00:00
|
|
|
ipcMain.on('ELECTRON_BROWSER_MEMBER_CONSTRUCTOR', function (event, contextId, id, method, args) {
|
2016-01-12 02:40:23 +00:00
|
|
|
try {
|
2018-07-10 08:15:40 +00:00
|
|
|
args = unwrapArgs(event.sender, contextId, args)
|
2016-11-17 17:55:13 +00:00
|
|
|
let object = objectsRegistry.get(id)
|
|
|
|
|
|
|
|
if (object == null) {
|
|
|
|
throwRPCError(`Cannot call constructor '${method}' on missing remote object ${id}`)
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-07-10 08:15:40 +00:00
|
|
|
event.returnValue = valueToMeta(event.sender, contextId, new object[method](...args))
|
2016-01-19 22:49:40 +00:00
|
|
|
} catch (error) {
|
2018-07-10 08:15:40 +00:00
|
|
|
event.returnValue = exceptionToMeta(event.sender, contextId, error)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:34:36 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-07-10 08:15:40 +00:00
|
|
|
ipcMain.on('ELECTRON_BROWSER_MEMBER_CALL', function (event, contextId, id, method, args) {
|
2016-01-12 02:40:23 +00:00
|
|
|
try {
|
2018-07-10 08:15:40 +00:00
|
|
|
args = unwrapArgs(event.sender, contextId, args)
|
2016-03-24 20:34:36 +00:00
|
|
|
let obj = objectsRegistry.get(id)
|
2016-11-17 17:55:13 +00:00
|
|
|
|
|
|
|
if (obj == null) {
|
|
|
|
throwRPCError(`Cannot call function '${method}' on missing remote object ${id}`)
|
|
|
|
}
|
|
|
|
|
2018-07-10 08:15:40 +00:00
|
|
|
callFunction(event, contextId, obj[method], obj, args)
|
2016-01-19 22:49:40 +00:00
|
|
|
} catch (error) {
|
2018-07-10 08:15:40 +00:00
|
|
|
event.returnValue = exceptionToMeta(event.sender, contextId, error)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:34:36 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-07-10 08:15:40 +00:00
|
|
|
ipcMain.on('ELECTRON_BROWSER_MEMBER_SET', function (event, contextId, id, name, args) {
|
2016-01-12 02:40:23 +00:00
|
|
|
try {
|
2018-07-10 08:15:40 +00:00
|
|
|
args = unwrapArgs(event.sender, contextId, args)
|
2017-04-03 21:30:21 +00:00
|
|
|
let obj = objectsRegistry.get(id)
|
2016-11-17 17:55:13 +00:00
|
|
|
|
|
|
|
if (obj == null) {
|
|
|
|
throwRPCError(`Cannot set property '${name}' on missing remote object ${id}`)
|
|
|
|
}
|
|
|
|
|
2017-04-03 21:30:21 +00:00
|
|
|
obj[name] = args[0]
|
2016-03-24 20:34:36 +00:00
|
|
|
event.returnValue = null
|
2016-01-19 22:49:40 +00:00
|
|
|
} catch (error) {
|
2018-07-10 08:15:40 +00:00
|
|
|
event.returnValue = exceptionToMeta(event.sender, contextId, error)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:34:36 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-07-10 08:15:40 +00:00
|
|
|
ipcMain.on('ELECTRON_BROWSER_MEMBER_GET', function (event, contextId, id, name) {
|
2016-01-12 02:40:23 +00:00
|
|
|
try {
|
2016-03-24 20:34:36 +00:00
|
|
|
let obj = objectsRegistry.get(id)
|
2016-11-17 20:23:00 +00:00
|
|
|
|
|
|
|
if (obj == null) {
|
|
|
|
throwRPCError(`Cannot get property '${name}' on missing remote object ${id}`)
|
|
|
|
}
|
|
|
|
|
2018-07-10 08:15:40 +00:00
|
|
|
event.returnValue = valueToMeta(event.sender, contextId, obj[name])
|
2016-01-19 22:49:40 +00:00
|
|
|
} catch (error) {
|
2018-07-10 08:15:40 +00:00
|
|
|
event.returnValue = exceptionToMeta(event.sender, contextId, error)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:34:36 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-07-10 08:15:40 +00:00
|
|
|
ipcMain.on('ELECTRON_BROWSER_DEREFERENCE', function (event, contextId, id) {
|
2018-07-24 07:21:38 +00:00
|
|
|
objectsRegistry.remove(event.sender, contextId, id)
|
2016-03-24 20:34:36 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-07-24 07:21:38 +00:00
|
|
|
ipcMain.on('ELECTRON_BROWSER_CONTEXT_RELEASE', (event, contextId) => {
|
|
|
|
objectsRegistry.clear(event.sender, contextId)
|
|
|
|
event.returnValue = null
|
2017-05-15 11:57:23 +00:00
|
|
|
})
|
|
|
|
|
2018-07-10 08:15:40 +00:00
|
|
|
ipcMain.on('ELECTRON_BROWSER_GUEST_WEB_CONTENTS', function (event, contextId, guestInstanceId) {
|
2016-01-12 02:40:23 +00:00
|
|
|
try {
|
2016-03-24 20:34:36 +00:00
|
|
|
let guestViewManager = require('./guest-view-manager')
|
2018-07-10 08:15:40 +00:00
|
|
|
event.returnValue = valueToMeta(event.sender, contextId, guestViewManager.getGuest(guestInstanceId))
|
2016-01-19 22:49:40 +00:00
|
|
|
} catch (error) {
|
2018-07-10 08:15:40 +00:00
|
|
|
event.returnValue = exceptionToMeta(event.sender, contextId, error)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:34:36 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-07-10 08:15:40 +00:00
|
|
|
ipcMain.on('ELECTRON_BROWSER_ASYNC_CALL_TO_GUEST_VIEW', function (event, contextId, requestId, guestInstanceId, method, ...args) {
|
2016-01-12 02:40:23 +00:00
|
|
|
try {
|
2016-03-24 20:34:36 +00:00
|
|
|
let guestViewManager = require('./guest-view-manager')
|
|
|
|
let guest = guestViewManager.getGuest(guestInstanceId)
|
2016-02-25 18:05:01 +00:00
|
|
|
if (requestId) {
|
2016-03-24 20:34:36 +00:00
|
|
|
const responseCallback = function (result) {
|
2016-04-06 23:21:26 +00:00
|
|
|
event.sender.send(`ELECTRON_RENDERER_ASYNC_CALL_TO_GUEST_VIEW_RESPONSE_${requestId}`, result)
|
2016-03-24 20:34:36 +00:00
|
|
|
}
|
|
|
|
args.push(responseCallback)
|
2016-02-25 18:05:01 +00:00
|
|
|
}
|
2016-03-24 20:34:36 +00:00
|
|
|
guest[method].apply(guest, args)
|
2016-01-19 22:49:40 +00:00
|
|
|
} catch (error) {
|
2018-07-10 08:15:40 +00:00
|
|
|
event.returnValue = exceptionToMeta(event.sender, contextId, error)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:34:36 +00:00
|
|
|
})
|
2016-05-18 12:19:50 +00:00
|
|
|
|
2016-05-28 12:13:00 +00:00
|
|
|
ipcMain.on('ELECTRON_BROWSER_SEND_TO', function (event, sendToAll, webContentsId, channel, ...args) {
|
2016-05-18 12:19:50 +00:00
|
|
|
let contents = webContents.fromId(webContentsId)
|
|
|
|
if (!contents) {
|
|
|
|
console.error(`Sending message to WebContents with unknown ID ${webContentsId}`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-28 12:13:00 +00:00
|
|
|
if (sendToAll) {
|
|
|
|
contents.sendToAll(channel, ...args)
|
|
|
|
} else {
|
|
|
|
contents.send(channel, ...args)
|
|
|
|
}
|
2016-05-18 12:19:50 +00:00
|
|
|
})
|
2016-12-02 01:34:14 +00:00
|
|
|
|
|
|
|
// Implements window.close()
|
|
|
|
ipcMain.on('ELECTRON_BROWSER_WINDOW_CLOSE', function (event) {
|
2017-08-17 17:56:37 +00:00
|
|
|
const window = event.sender.getOwnerBrowserWindow()
|
|
|
|
if (window) {
|
|
|
|
window.close()
|
2017-08-15 21:59:48 +00:00
|
|
|
}
|
2017-08-17 17:56:37 +00:00
|
|
|
event.returnValue = null
|
2016-12-02 01:34:14 +00:00
|
|
|
})
|
2018-05-21 12:56:05 +00:00
|
|
|
|
2018-08-10 22:19:49 +00:00
|
|
|
ipcMain.on('ELECTRON_BROWSER_SANDBOX_LOAD', function (event) {
|
|
|
|
const preloadPath = event.sender._getPreloadPath()
|
2018-05-21 12:56:05 +00:00
|
|
|
let preloadSrc = null
|
|
|
|
let preloadError = null
|
|
|
|
if (preloadPath) {
|
|
|
|
try {
|
|
|
|
preloadSrc = fs.readFileSync(preloadPath).toString()
|
|
|
|
} catch (err) {
|
|
|
|
preloadError = {stack: err ? err.stack : (new Error(`Failed to load "${preloadPath}"`)).stack}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
event.returnValue = {
|
2018-08-10 22:19:49 +00:00
|
|
|
preloadSrc,
|
|
|
|
preloadError,
|
2018-05-21 12:56:05 +00:00
|
|
|
platform: process.platform,
|
|
|
|
env: process.env
|
|
|
|
}
|
|
|
|
})
|