electron/lib/renderer/api/remote.js

359 lines
11 KiB
JavaScript
Raw Normal View History

2016-03-25 19:57:17 +00:00
'use strict'
// Note: Don't use destructuring assignment for `Buffer`, or we'll hit a
// browserify bug that makes the statement invalid, throwing an error in
// sandboxed renderer.
const Buffer = require('buffer').Buffer
2016-03-25 19:57:17 +00:00
const v8Util = process.atomBinding('v8_util')
const {ipcRenderer, isPromise, CallbacksRegistry} = require('electron')
2017-03-06 19:23:28 +00:00
const resolvePromise = Promise.resolve.bind(Promise)
2016-01-12 02:40:23 +00:00
2016-03-29 00:35:49 +00:00
const callbacksRegistry = new CallbacksRegistry()
const remoteObjectCache = v8Util.createIDWeakMap()
2017-10-25 13:51:21 +00:00
// lookup for ipc renderer send calls
const ipcs = {
'b_mem_con': 'ELECTRON_BROWSER_MEMBER_CONSTRUCTOR',
'b_mem_call': 'ELECTRON_BROWSER_MEMBER_CALL',
'b_mem_get': 'ELECTRON_BROWSER_MEMBER_GET',
'b_mem_set': 'ELECTRON_BROWSER_MEMBER_SET',
'b_con': 'ELECTRON_BROWSER_CONSTRUCTOR',
'b_func_call': 'ELECTRON_BROWSER_FUNCTION_CALL',
'rend_call': 'ELECTRON_RENDERER_CALLBACK',
'rend_rel_call': 'ELECTRON_RENDERER_RELEASE_CALLBACK',
2017-10-25 13:56:02 +00:00
'b_con_rel': 'ELECTRON_BROWSER_CONTEXT_RELEASE',
2017-10-25 13:51:21 +00:00
'b_req': 'ELECTRON_BROWSER_REQUIRE',
'b_get_built': 'ELECTRON_BROWSER_GET_BUILTIN',
'b_curr_win': 'ELECTRON_BROWSER_CURRENT_WINDOW',
'b_curr_web_con': 'ELECTRON_BROWSER_CURRENT_WEB_CONTENTS',
'b_glob': 'ELECTRON_BROWSER_GLOBAL',
'b_guest_web_con': 'ELECTRON_BROWSER_GUEST_WEB_CONTENTS'
}
2016-01-14 18:35:29 +00:00
// Convert the arguments object into an array of meta data.
2017-10-25 13:51:21 +00:00
function wrapArgs (args, visited) {
if (visited) visited = new Set()
const valueToMeta = function (value) {
// Check for circular reference.
if (visited.has(value)) {
2016-01-12 02:40:23 +00:00
return {
type: 'value',
value: null
}
}
if (Array.isArray(value)) {
visited.add(value)
let meta = {
2016-01-12 02:40:23 +00:00
type: 'array',
value: wrapArgs(value, visited)
2016-03-25 19:57:17 +00:00
}
visited.delete(value)
return meta
} else if (ArrayBuffer.isView(value)) {
2016-01-12 02:40:23 +00:00
return {
type: 'buffer',
2016-08-24 22:02:51 +00:00
value: Buffer.from(value)
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
} else if (value instanceof Date) {
return {
type: 'date',
value: value.getTime()
2016-03-25 19:57:17 +00:00
}
2017-10-25 13:51:21 +00:00
} else if (value && typeof value === 'object') {
2016-05-25 05:38:35 +00:00
if (isPromise(value)) {
return {
type: 'promise',
2016-05-23 22:07:01 +00:00
then: valueToMeta(function (onFulfilled, onRejected) {
value.then(onFulfilled, onRejected)
})
}
} else if (v8Util.getHiddenValue(value, 'atomId')) {
return {
type: 'remote-object',
id: v8Util.getHiddenValue(value, 'atomId')
}
}
let meta = {
2016-01-12 02:40:23 +00:00
type: 'object',
2017-10-25 13:51:21 +00:00
name: (value.constructor) ? value.constructor.name : '',
2016-01-12 02:40:23 +00:00
members: []
2016-03-25 19:57:17 +00:00
}
visited.add(value)
for (let prop in value) {
meta.members.push({
2016-01-12 02:40:23 +00:00
name: prop,
value: valueToMeta(value[prop])
2016-03-25 19:57:17 +00:00
})
2016-01-12 02:40:23 +00:00
}
visited.delete(value)
return meta
2016-01-12 02:40:23 +00:00
} else if (typeof value === 'function' && v8Util.getHiddenValue(value, 'returnValue')) {
return {
type: 'function-with-return-value',
value: valueToMeta(value())
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
} else if (typeof value === 'function') {
return {
type: 'function',
id: callbacksRegistry.add(value),
location: v8Util.getHiddenValue(value, 'location'),
length: value.length
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
} else {
return {
type: 'value',
value: value
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
}
2016-03-25 19:57:17 +00:00
}
return args.map(valueToMeta)
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
// Populate object's members from descriptors.
// The |ref| will be kept referenced by |members|.
// This matches |getObjectMemebers| in rpc-server.
2017-10-25 13:51:21 +00:00
function setObjectMembers (ref, object, metaId, members) {
if (!Array.isArray(members)) return
for (let member of members) {
2016-03-29 00:35:49 +00:00
if (object.hasOwnProperty(member.name)) continue
2016-03-25 19:57:17 +00:00
let descriptor = { enumerable: member.enumerable }
if (member.type === 'method') {
const remoteMemberFunction = function (...args) {
if (this && this.constructor === remoteMemberFunction) {
// Constructor call.
2017-10-25 13:51:21 +00:00
const ret = ipcRenderer.sendSync(ipcs['b_mem_con'], metaId, member.name, wrapArgs(args))
2016-03-25 19:57:17 +00:00
return metaToValue(ret)
} else {
// Call member function.
2017-10-25 13:51:21 +00:00
const ret = ipcRenderer.sendSync(ipcs['b_mem_call'], metaId, member.name, wrapArgs(args))
2016-03-25 19:57:17 +00:00
return metaToValue(ret)
}
2016-03-25 19:57:17 +00:00
}
let descriptorFunction = proxyFunctionProperties(remoteMemberFunction, metaId, member.name)
descriptor.get = function () {
descriptorFunction.ref = ref // The member should reference its object.
return descriptorFunction
}
// Enable monkey-patch the method
descriptor.set = function (value) {
descriptorFunction = value
return value
}
2016-03-25 19:57:17 +00:00
descriptor.configurable = true
} else if (member.type === 'get') {
2016-03-25 19:57:17 +00:00
descriptor.get = function () {
2017-10-25 13:51:21 +00:00
return metaToValue(ipcRenderer.sendSync(ipcs['b_mem_get'], metaId, member.name))
2016-03-25 19:57:17 +00:00
}
// Only set setter when it is writable.
if (member.writable) {
2016-03-25 19:57:17 +00:00
descriptor.set = function (value) {
2017-04-03 21:18:04 +00:00
const args = wrapArgs([value])
2017-10-25 13:51:21 +00:00
const meta = ipcRenderer.sendSync(ipcs['b_mem_set'], metaId, member.name, args)
// Meta will be non-null when a setter error occurred so parse it
// to a value so it gets re-thrown.
2017-10-25 13:51:21 +00:00
if (meta) metaToValue(meta)
2016-03-25 19:57:17 +00:00
return value
}
}
}
2016-03-25 19:57:17 +00:00
Object.defineProperty(object, member.name, descriptor)
}
2016-03-25 19:57:17 +00:00
}
// Populate object's prototype from descriptor.
// This matches |getObjectPrototype| in rpc-server.
2017-10-25 13:51:21 +00:00
function setObjectPrototype (ref, object, metaId, descriptor) {
if (!descriptor) return
2016-03-25 19:57:17 +00:00
let proto = {}
setObjectMembers(ref, proto, metaId, descriptor.members)
setObjectPrototype(ref, proto, metaId, descriptor.proto)
2016-03-25 19:57:17 +00:00
Object.setPrototypeOf(object, proto)
}
// Wrap function in Proxy for accessing remote properties
2017-10-25 13:51:21 +00:00
function proxyFunctionProperties (remoteMemberFunction, metaId, name) {
let loaded = false
// Lazily load function properties
const loadRemoteProperties = () => {
if (loaded) return
loaded = true
2017-10-25 13:51:21 +00:00
const meta = ipcRenderer.sendSync(ipcs['b_mem_get'], metaId, name)
setObjectMembers(remoteMemberFunction, remoteMemberFunction, meta.id, meta.members)
}
return new Proxy(remoteMemberFunction, {
set: (target, property, value, receiver) => {
if (property !== 'ref') loadRemoteProperties()
target[property] = value
return true
},
get: (target, property, receiver) => {
if (!target.hasOwnProperty(property)) loadRemoteProperties()
const value = target[property]
// Bind toString to target if it is a function to avoid
// Function.prototype.toString is not generic errors
if (property === 'toString' && typeof value === 'function') {
return value.bind(target)
}
return value
},
ownKeys: (target) => {
loadRemoteProperties()
return Object.getOwnPropertyNames(target)
},
getOwnPropertyDescriptor: (target, property) => {
let descriptor = Object.getOwnPropertyDescriptor(target, property)
2017-10-25 13:51:21 +00:00
if (descriptor) return descriptor
loadRemoteProperties()
return Object.getOwnPropertyDescriptor(target, property)
}
})
}
2016-01-14 18:35:29 +00:00
// Convert meta data from browser into real value.
2017-10-25 13:51:21 +00:00
function metaToValue (meta) {
const types = {
'value': () => meta.value,
'array': () => {
const members = meta.members
return members.map((member) => metaToValue(member))
},
'buffer': () => Buffer.from(meta.value),
2017-10-25 13:56:02 +00:00
'promise': () => resolvePromise({then: metaToValue(meta.then)}),
2017-10-25 13:51:21 +00:00
'error': () => metaToPlainObject(meta),
'date': () => new Date(meta.value),
'exception': () => new Error(`${meta.message}\n${meta.stack}`)
}
if (meta.type in types) {
types[meta.type]()
} else {
let ret
if (remoteObjectCache.has(meta.id)) return remoteObjectCache.get(meta.id)
// A shadow class to represent the remote function object.
if (meta.type === 'function') {
let remoteFunction = function (...args) {
if (this && this.constructor === remoteFunction) {
2017-10-25 13:56:02 +00:00
return metaToValue(ipcRenderer.sendSync(ipcs['b_con'], meta.id, wrapArgs(args)))
2017-10-25 13:51:21 +00:00
} else {
2017-10-25 13:56:02 +00:00
return metaToValue(ipcRenderer.sendSync(ipcs['b_func_call'], meta.id, wrapArgs(args)))
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
}
2017-10-25 13:51:21 +00:00
ret = remoteFunction
} else {
ret = {}
}
2016-01-12 02:40:23 +00:00
2017-10-25 13:51:21 +00:00
setObjectMembers(ret, ret, meta.id, meta.members)
setObjectPrototype(ret, ret, meta.id, meta.proto)
2017-10-25 13:51:21 +00:00
// Set constructor.name to object's name.
Object.defineProperty(ret.constructor, 'name', { value: meta.name })
2016-01-12 02:40:23 +00:00
2017-10-25 13:51:21 +00:00
// Track delegate obj's lifetime & tell browser to clean up when object is GCed.
v8Util.setRemoteObjectFreer(ret, meta.id)
v8Util.setHiddenValue(ret, 'atomId', meta.id)
remoteObjectCache.set(meta.id, ret)
return ret
2016-01-12 02:40:23 +00:00
}
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Construct a plain object from the meta.
2017-10-25 13:51:21 +00:00
function metaToPlainObject (meta) {
if (meta.type === 'error') return new Error()
const obj = {}
const members = meta.members
members.forEach((key, val) => { obj[key] = val })
2016-03-25 19:57:17 +00:00
return obj
}
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Browser calls a callback in renderer.
2017-10-25 13:51:21 +00:00
ipcRenderer.on(ipcs['rend_call'], (event, id, args) => {
2016-05-19 22:28:08 +00:00
callbacksRegistry.apply(id, metaToValue(args))
2016-03-25 19:57:17 +00:00
})
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// A callback in browser is released.
2017-10-25 13:51:21 +00:00
ipcRenderer.on(ipcs['rend_rel_call'], (event, id) => {
2016-05-19 22:28:08 +00:00
callbacksRegistry.remove(id)
2016-03-25 19:57:17 +00:00
})
2016-01-12 02:40:23 +00:00
process.on('exit', () => {
2017-10-25 13:51:21 +00:00
ipcRenderer.sendSync(ipcs['b_con_rel'])
})
2016-01-14 19:10:12 +00:00
// Get remote module.
2016-03-25 19:57:17 +00:00
exports.require = function (module) {
2017-10-25 13:51:21 +00:00
return metaToValue(ipcRenderer.sendSync(ipcs['b_req'], module))
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Alias to remote.require('electron').xxx.
2016-03-25 19:57:17 +00:00
exports.getBuiltin = function (module) {
2017-10-25 13:51:21 +00:00
return metaToValue(ipcRenderer.sendSync(ipcs['b_get_built'], module))
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
// Get current BrowserWindow.
2016-03-25 19:57:17 +00:00
exports.getCurrentWindow = function () {
2017-10-25 13:51:21 +00:00
return metaToValue(ipcRenderer.sendSync(ipcs['b_curr_win']))
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Get current WebContents object.
2016-03-25 19:57:17 +00:00
exports.getCurrentWebContents = function () {
2017-10-25 13:51:21 +00:00
return metaToValue(ipcRenderer.sendSync(ipcs['b_curr_web_cont']))
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Get a global object in browser.
2016-03-25 19:57:17 +00:00
exports.getGlobal = function (name) {
2017-10-25 13:51:21 +00:00
return metaToValue(ipcRenderer.sendSync(ipcs['b_glob'], name))
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Get the process object in browser.
2016-03-25 19:57:17 +00:00
exports.__defineGetter__('process', function () {
return exports.getGlobal('process')
})
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Create a funtion that will return the specifed value when called in browser.
2016-03-25 19:57:17 +00:00
exports.createFunctionWithReturnValue = function (returnValue) {
2017-10-25 13:51:21 +00:00
const func = () => returnValue
2016-03-25 19:57:17 +00:00
v8Util.setHiddenValue(func, 'returnValue', true)
return func
}
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Get the guest WebContents from guestInstanceId.
2016-03-25 19:57:17 +00:00
exports.getGuestWebContents = function (guestInstanceId) {
2017-10-25 13:51:21 +00:00
const meta = ipcRenderer.sendSync(ipcs['b_guest_web_con'], guestInstanceId)
2016-03-25 19:57:17 +00:00
return metaToValue(meta)
}
const addBuiltinProperty = (name) => {
Object.defineProperty(exports, name, {
get: function () {
return exports.getBuiltin(name)
}
})
}
const browserModules =
require('../../common/api/module-list').concat(
require('../../browser/api/module-list'))
// And add a helper receiver for each one.
browserModules
.filter((m) => !m.private)
.map((m) => m.name)
.forEach(addBuiltinProperty)