Merge pull request #10909 from electron/update_remote

Simplify and remove excess code from remote module
This commit is contained in:
Shelley Vohr 2017-11-03 09:16:28 -04:00 committed by GitHub
commit 8736a41cfb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -9,16 +9,11 @@ const {ipcRenderer, isPromise, CallbacksRegistry} = require('electron')
const resolvePromise = Promise.resolve.bind(Promise) const resolvePromise = Promise.resolve.bind(Promise)
const callbacksRegistry = new CallbacksRegistry() const callbacksRegistry = new CallbacksRegistry()
const remoteObjectCache = v8Util.createIDWeakMap() const remoteObjectCache = v8Util.createIDWeakMap()
// Convert the arguments object into an array of meta data. // Convert the arguments object into an array of meta data.
const wrapArgs = function (args, visited) { function wrapArgs (args, visited = new Set()) {
if (visited == null) { const valueToMeta = (value) => {
visited = new Set()
}
const valueToMeta = function (value) {
// Check for circular reference. // Check for circular reference.
if (visited.has(value)) { if (visited.has(value)) {
return { return {
@ -62,7 +57,7 @@ const wrapArgs = function (args, visited) {
let meta = { let meta = {
type: 'object', type: 'object',
name: value.constructor != null ? value.constructor.name : '', name: value.constructor ? value.constructor.name : '',
members: [] members: []
} }
visited.add(value) visited.add(value)
@ -99,7 +94,7 @@ const wrapArgs = function (args, visited) {
// Populate object's members from descriptors. // Populate object's members from descriptors.
// The |ref| will be kept referenced by |members|. // The |ref| will be kept referenced by |members|.
// This matches |getObjectMemebers| in rpc-server. // This matches |getObjectMemebers| in rpc-server.
const setObjectMembers = function (ref, object, metaId, members) { function setObjectMembers (ref, object, metaId, members) {
if (!Array.isArray(members)) return if (!Array.isArray(members)) return
for (let member of members) { for (let member of members) {
@ -108,44 +103,41 @@ const setObjectMembers = function (ref, object, metaId, members) {
let descriptor = { enumerable: member.enumerable } let descriptor = { enumerable: member.enumerable }
if (member.type === 'method') { if (member.type === 'method') {
const remoteMemberFunction = function (...args) { const remoteMemberFunction = function (...args) {
let command
if (this && this.constructor === remoteMemberFunction) { if (this && this.constructor === remoteMemberFunction) {
// Constructor call. command = 'ELECTRON_BROWSER_MEMBER_CONSTRUCTOR'
let ret = ipcRenderer.sendSync('ELECTRON_BROWSER_MEMBER_CONSTRUCTOR', metaId, member.name, wrapArgs(args))
return metaToValue(ret)
} else { } else {
// Call member function. command = 'ELECTRON_BROWSER_MEMBER_CALL'
let ret = ipcRenderer.sendSync('ELECTRON_BROWSER_MEMBER_CALL', metaId, member.name, wrapArgs(args))
return metaToValue(ret)
} }
const ret = ipcRenderer.sendSync(command, metaId, member.name, wrapArgs(args))
return metaToValue(ret)
} }
let descriptorFunction = proxyFunctionProperties(remoteMemberFunction, metaId, member.name) let descriptorFunction = proxyFunctionProperties(remoteMemberFunction, metaId, member.name)
descriptor.get = function () { descriptor.get = () => {
descriptorFunction.ref = ref // The member should reference its object. descriptorFunction.ref = ref // The member should reference its object.
return descriptorFunction return descriptorFunction
} }
// Enable monkey-patch the method // Enable monkey-patch the method
descriptor.set = function (value) { descriptor.set = (value) => {
descriptorFunction = value descriptorFunction = value
return value return value
} }
descriptor.configurable = true descriptor.configurable = true
} else if (member.type === 'get') { } else if (member.type === 'get') {
descriptor.get = function () { descriptor.get = () => {
return metaToValue(ipcRenderer.sendSync('ELECTRON_BROWSER_MEMBER_GET', metaId, member.name)) const command = 'ELECTRON_BROWSER_MEMBER_GET'
const meta = ipcRenderer.sendSync(command, metaId, member.name)
return metaToValue(meta)
} }
// Only set setter when it is writable.
if (member.writable) { if (member.writable) {
descriptor.set = function (value) { descriptor.set = (value) => {
const args = wrapArgs([value]) const args = wrapArgs([value])
const meta = ipcRenderer.sendSync('ELECTRON_BROWSER_MEMBER_SET', metaId, member.name, args) const command = 'ELECTRON_BROWSER_MEMBER_SET'
// Meta will be non-null when a setter error occurred so parse it const meta = ipcRenderer.sendSync(command, metaId, member.name, args)
// to a value so it gets re-thrown. if (meta != null) metaToValue(meta)
if (meta != null) {
metaToValue(meta)
}
return value return value
} }
} }
@ -157,7 +149,7 @@ const setObjectMembers = function (ref, object, metaId, members) {
// Populate object's prototype from descriptor. // Populate object's prototype from descriptor.
// This matches |getObjectPrototype| in rpc-server. // This matches |getObjectPrototype| in rpc-server.
const setObjectPrototype = function (ref, object, metaId, descriptor) { function setObjectPrototype (ref, object, metaId, descriptor) {
if (descriptor === null) return if (descriptor === null) return
let proto = {} let proto = {}
setObjectMembers(ref, proto, metaId, descriptor.members) setObjectMembers(ref, proto, metaId, descriptor.members)
@ -166,14 +158,15 @@ const setObjectPrototype = function (ref, object, metaId, descriptor) {
} }
// Wrap function in Proxy for accessing remote properties // Wrap function in Proxy for accessing remote properties
const proxyFunctionProperties = function (remoteMemberFunction, metaId, name) { function proxyFunctionProperties (remoteMemberFunction, metaId, name) {
let loaded = false let loaded = false
// Lazily load function properties // Lazily load function properties
const loadRemoteProperties = () => { const loadRemoteProperties = () => {
if (loaded) return if (loaded) return
loaded = true loaded = true
const meta = ipcRenderer.sendSync('ELECTRON_BROWSER_MEMBER_GET', metaId, name) const command = 'ELECTRON_BROWSER_MEMBER_GET'
const meta = ipcRenderer.sendSync(command, metaId, name)
setObjectMembers(remoteMemberFunction, remoteMemberFunction, meta.id, meta.members) setObjectMembers(remoteMemberFunction, remoteMemberFunction, meta.id, meta.members)
} }
@ -186,13 +179,9 @@ const proxyFunctionProperties = function (remoteMemberFunction, metaId, name) {
get: (target, property, receiver) => { get: (target, property, receiver) => {
if (!target.hasOwnProperty(property)) loadRemoteProperties() if (!target.hasOwnProperty(property)) loadRemoteProperties()
const value = target[property] 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') { if (property === 'toString' && typeof value === 'function') {
return value.bind(target) return value.bind(target)
} }
return value return value
}, },
ownKeys: (target) => { ownKeys: (target) => {
@ -201,7 +190,7 @@ const proxyFunctionProperties = function (remoteMemberFunction, metaId, name) {
}, },
getOwnPropertyDescriptor: (target, property) => { getOwnPropertyDescriptor: (target, property) => {
let descriptor = Object.getOwnPropertyDescriptor(target, property) let descriptor = Object.getOwnPropertyDescriptor(target, property)
if (descriptor != null) return descriptor if (descriptor) return descriptor
loadRemoteProperties() loadRemoteProperties()
return Object.getOwnPropertyDescriptor(target, property) return Object.getOwnPropertyDescriptor(target, property)
} }
@ -209,66 +198,48 @@ const proxyFunctionProperties = function (remoteMemberFunction, metaId, name) {
} }
// Convert meta data from browser into real value. // Convert meta data from browser into real value.
const metaToValue = function (meta) { function metaToValue (meta) {
var el, i, len, ref1, results, ret const types = {
switch (meta.type) { value: () => meta.value,
case 'value': array: () => meta.members.map((member) => metaToValue(member)),
return meta.value buffer: () => Buffer.from(meta.value),
case 'array': promise: () => resolvePromise({then: metaToValue(meta.then)}),
ref1 = meta.members error: () => metaToPlainObject(meta),
results = [] date: () => new Date(meta.value),
for (i = 0, len = ref1.length; i < len; i++) { exception: () => { throw new Error(`${meta.message}\n${meta.stack}`) }
el = ref1[i]
results.push(metaToValue(el))
} }
return results
case 'buffer':
return Buffer.from(meta.value)
case 'promise':
return resolvePromise({then: metaToValue(meta.then)})
case 'error':
return metaToPlainObject(meta)
case 'date':
return new Date(meta.value)
case 'exception':
throw new Error(meta.message + '\n' + meta.stack)
default:
if (remoteObjectCache.has(meta.id)) return remoteObjectCache.get(meta.id)
if (meta.type === 'function') { if (meta.type in types) {
// A shadow class to represent the remote function object. return types[meta.type]()
let remoteFunction = function (...args) {
if (this && this.constructor === remoteFunction) {
// Constructor call.
let obj = ipcRenderer.sendSync('ELECTRON_BROWSER_CONSTRUCTOR', meta.id, wrapArgs(args))
// Returning object in constructor will replace constructed object
// with the returned object.
// http://stackoverflow.com/questions/1978049/what-values-can-a-constructor-return-to-avoid-returning-this
return metaToValue(obj)
} else { } else {
// Function call. let ret
let obj = ipcRenderer.sendSync('ELECTRON_BROWSER_FUNCTION_CALL', meta.id, wrapArgs(args)) if (remoteObjectCache.has(meta.id)) {
return metaToValue(obj) return remoteObjectCache.get(meta.id)
} }
// A shadow class to represent the remote function object.
if (meta.type === 'function') {
let remoteFunction = function (...args) {
let command
if (this && this.constructor === remoteFunction) {
command = 'ELECTRON_BROWSER_CONSTRUCTOR'
} else {
command = 'ELECTRON_BROWSER_FUNCTION_CALL'
}
const obj = ipcRenderer.sendSync(command, meta.id, wrapArgs(args))
return metaToValue(obj)
} }
ret = remoteFunction ret = remoteFunction
} else { } else {
ret = {} ret = {}
} }
// Populate delegate members.
setObjectMembers(ret, ret, meta.id, meta.members) setObjectMembers(ret, ret, meta.id, meta.members)
// Populate delegate prototype.
setObjectPrototype(ret, ret, meta.id, meta.proto) setObjectPrototype(ret, ret, meta.id, meta.proto)
// Set constructor.name to object's name.
Object.defineProperty(ret.constructor, 'name', { value: meta.name }) Object.defineProperty(ret.constructor, 'name', { value: meta.name })
// Track delegate object's life time, and tell the browser to clean up // Track delegate obj's lifetime & tell browser to clean up when object is GCed.
// when the object is GCed.
v8Util.setRemoteObjectFreer(ret, meta.id) v8Util.setRemoteObjectFreer(ret, meta.id)
// Remember object's id.
v8Util.setHiddenValue(ret, 'atomId', meta.id) v8Util.setHiddenValue(ret, 'atomId', meta.id)
remoteObjectCache.set(meta.id, ret) remoteObjectCache.set(meta.id, ret)
return ret return ret
@ -276,88 +247,81 @@ const metaToValue = function (meta) {
} }
// Construct a plain object from the meta. // Construct a plain object from the meta.
const metaToPlainObject = function (meta) { function metaToPlainObject (meta) {
var i, len, obj, ref1 const obj = (() => meta.type === 'error' ? new Error() : {})()
obj = (function () { for (let i = 0; i < meta.members.length; i++) {
switch (meta.type) { let {name, value} = meta.members[i]
case 'error':
return new Error()
default:
return {}
}
})()
ref1 = meta.members
for (i = 0, len = ref1.length; i < len; i++) {
let {name, value} = ref1[i]
obj[name] = value obj[name] = value
} }
return obj return obj
} }
// Browser calls a callback in renderer. // Browser calls a callback in renderer.
ipcRenderer.on('ELECTRON_RENDERER_CALLBACK', function (event, id, args) { ipcRenderer.on('ELECTRON_RENDERER_CALLBACK', (event, id, args) => {
callbacksRegistry.apply(id, metaToValue(args)) callbacksRegistry.apply(id, metaToValue(args))
}) })
// A callback in browser is released. // A callback in browser is released.
ipcRenderer.on('ELECTRON_RENDERER_RELEASE_CALLBACK', function (event, id) { ipcRenderer.on('ELECTRON_RENDERER_RELEASE_CALLBACK', (event, id) => {
callbacksRegistry.remove(id) callbacksRegistry.remove(id)
}) })
process.on('exit', () => { process.on('exit', () => {
ipcRenderer.sendSync('ELECTRON_BROWSER_CONTEXT_RELEASE') const command = 'ELECTRON_BROWSER_CONTEXT_RELEASE'
ipcRenderer.sendSync(command)
}) })
// Get remote module. exports.require = (module) => {
exports.require = function (module) { const command = 'ELECTRON_BROWSER_REQUIRE'
return metaToValue(ipcRenderer.sendSync('ELECTRON_BROWSER_REQUIRE', module)) const meta = ipcRenderer.sendSync(command, module)
return metaToValue(meta)
} }
// Alias to remote.require('electron').xxx. // Alias to remote.require('electron').xxx.
exports.getBuiltin = function (module) { exports.getBuiltin = (module) => {
return metaToValue(ipcRenderer.sendSync('ELECTRON_BROWSER_GET_BUILTIN', module)) const command = 'ELECTRON_BROWSER_GET_BUILTIN'
const meta = ipcRenderer.sendSync(command, module)
return metaToValue(meta)
} }
// Get current BrowserWindow. exports.getCurrentWindow = () => {
exports.getCurrentWindow = function () { const command = 'ELECTRON_BROWSER_CURRENT_WINDOW'
return metaToValue(ipcRenderer.sendSync('ELECTRON_BROWSER_CURRENT_WINDOW')) const meta = ipcRenderer.sendSync(command)
return metaToValue(meta)
} }
// Get current WebContents object. // Get current WebContents object.
exports.getCurrentWebContents = function () { exports.getCurrentWebContents = () => {
return metaToValue(ipcRenderer.sendSync('ELECTRON_BROWSER_CURRENT_WEB_CONTENTS')) return metaToValue(ipcRenderer.sendSync('ELECTRON_BROWSER_CURRENT_WEB_CONTENTS'))
} }
// Get a global object in browser. // Get a global object in browser.
exports.getGlobal = function (name) { exports.getGlobal = (name) => {
return metaToValue(ipcRenderer.sendSync('ELECTRON_BROWSER_GLOBAL', name)) const command = 'ELECTRON_BROWSER_GLOBAL'
const meta = ipcRenderer.sendSync(command, name)
return metaToValue(meta)
} }
// Get the process object in browser. // Get the process object in browser.
exports.__defineGetter__('process', function () { exports.__defineGetter__('process', () => exports.getGlobal('process'))
return exports.getGlobal('process')
})
// Create a funtion that will return the specifed value when called in browser. // Create a function that will return the specified value when called in browser.
exports.createFunctionWithReturnValue = function (returnValue) { exports.createFunctionWithReturnValue = (returnValue) => {
const func = function () { const func = () => returnValue
return returnValue
}
v8Util.setHiddenValue(func, 'returnValue', true) v8Util.setHiddenValue(func, 'returnValue', true)
return func return func
} }
// Get the guest WebContents from guestInstanceId. // Get the guest WebContents from guestInstanceId.
exports.getGuestWebContents = function (guestInstanceId) { exports.getGuestWebContents = (guestInstanceId) => {
const meta = ipcRenderer.sendSync('ELECTRON_BROWSER_GUEST_WEB_CONTENTS', guestInstanceId) const command = 'ELECTRON_BROWSER_GUEST_WEB_CONTENTS'
const meta = ipcRenderer.sendSync(command, guestInstanceId)
return metaToValue(meta) return metaToValue(meta)
} }
const addBuiltinProperty = (name) => { const addBuiltinProperty = (name) => {
Object.defineProperty(exports, name, { Object.defineProperty(exports, name, {
get: function () { get: () => exports.getBuiltin(name)
return exports.getBuiltin(name)
}
}) })
} }