revert ipc lookup table

This commit is contained in:
Shelley Vohr 2017-10-25 15:36:16 -04:00
parent c0f2a7b44a
commit d4880b135a
No known key found for this signature in database
GPG key ID: F13993A75599653C

View file

@ -11,25 +11,6 @@ const resolvePromise = Promise.resolve.bind(Promise)
const callbacksRegistry = new CallbacksRegistry() const callbacksRegistry = new CallbacksRegistry()
const remoteObjectCache = v8Util.createIDWeakMap() const remoteObjectCache = v8Util.createIDWeakMap()
// 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',
'b_con_rel': 'ELECTRON_BROWSER_CONTEXT_RELEASE',
'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'
}
// Convert the arguments object into an array of meta data. // Convert the arguments object into an array of meta data.
function wrapArgs (args, visited) { function wrapArgs (args, visited) {
if (visited) visited = new Set() if (visited) visited = new Set()
@ -126,11 +107,11 @@ function setObjectMembers (ref, object, metaId, members) {
const remoteMemberFunction = function (...args) { const remoteMemberFunction = function (...args) {
if (this && this.constructor === remoteMemberFunction) { if (this && this.constructor === remoteMemberFunction) {
// Constructor call. // Constructor call.
const ret = ipcRenderer.sendSync(ipcs['b_mem_con'], metaId, member.name, wrapArgs(args)) const ret = ipcRenderer.sendSync('ELECTRON_BROWSER_MEMBER_CONSTRUCTOR', metaId, member.name, wrapArgs(args))
return metaToValue(ret) return metaToValue(ret)
} else { } else {
// Call member function. // Call member function.
const ret = ipcRenderer.sendSync(ipcs['b_mem_call'], metaId, member.name, wrapArgs(args)) const ret = ipcRenderer.sendSync('ELECTRON_BROWSER_MEMBER_CALL', metaId, member.name, wrapArgs(args))
return metaToValue(ret) return metaToValue(ret)
} }
} }
@ -149,14 +130,14 @@ function setObjectMembers (ref, object, metaId, members) {
descriptor.configurable = true descriptor.configurable = true
} else if (member.type === 'get') { } else if (member.type === 'get') {
descriptor.get = function () { descriptor.get = function () {
return metaToValue(ipcRenderer.sendSync(ipcs['b_mem_get'], metaId, member.name)) return metaToValue(ipcRenderer.sendSync('ELECTRON_BROWSER_MEMBER_GET', metaId, member.name))
} }
// Only set setter when it is writable. // Only set setter when it is writable.
if (member.writable) { if (member.writable) {
descriptor.set = function (value) { descriptor.set = function (value) {
const args = wrapArgs([value]) const args = wrapArgs([value])
const meta = ipcRenderer.sendSync(ipcs['b_mem_set'], metaId, member.name, args) const meta = ipcRenderer.sendSync('ELECTRON_BROWSER_MEMBER_SET', metaId, member.name, args)
// Meta will be non-null when a setter error occurred so parse it // Meta will be non-null when a setter error occurred so parse it
// to a value so it gets re-thrown. // to a value so it gets re-thrown.
if (meta) metaToValue(meta) if (meta) metaToValue(meta)
@ -187,7 +168,7 @@ function proxyFunctionProperties (remoteMemberFunction, metaId, name) {
const loadRemoteProperties = () => { const loadRemoteProperties = () => {
if (loaded) return if (loaded) return
loaded = true loaded = true
const meta = ipcRenderer.sendSync(ipcs['b_mem_get'], metaId, name) const meta = ipcRenderer.sendSync('ELECTRON_BROWSER_MEMBER_GET', metaId, name)
setObjectMembers(remoteMemberFunction, remoteMemberFunction, meta.id, meta.members) setObjectMembers(remoteMemberFunction, remoteMemberFunction, meta.id, meta.members)
} }
@ -247,9 +228,9 @@ function metaToValue (meta) {
if (meta.type === 'function') { if (meta.type === 'function') {
let remoteFunction = function (...args) { let remoteFunction = function (...args) {
if (this && this.constructor === remoteFunction) { if (this && this.constructor === remoteFunction) {
return metaToValue(ipcRenderer.sendSync(ipcs['b_con'], meta.id, wrapArgs(args))) return metaToValue(ipcRenderer.sendSync('ELECTRON_BROWSER_CONSTRUCTOR', meta.id, wrapArgs(args)))
} else { } else {
return metaToValue(ipcRenderer.sendSync(ipcs['b_func_call'], meta.id, wrapArgs(args))) return metaToValue(ipcRenderer.sendSync('ELECTRON_BROWSER_FUNCTION_CALL', meta.id, wrapArgs(args)))
} }
} }
ret = remoteFunction ret = remoteFunction
@ -283,42 +264,42 @@ function metaToPlainObject (meta) {
} }
// Browser calls a callback in renderer. // Browser calls a callback in renderer.
ipcRenderer.on(ipcs['rend_call'], (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(ipcs['rend_rel_call'], (event, id) => { ipcRenderer.on('ELECTRON_RENDERER_RELEASE_CALLBACK', (event, id) => {
callbacksRegistry.remove(id) callbacksRegistry.remove(id)
}) })
process.on('exit', () => { process.on('exit', () => {
ipcRenderer.sendSync(ipcs['b_con_rel']) ipcRenderer.sendSync('ELECTRON_BROWSER_CONTEXT_RELEASE')
}) })
// Get remote module. // Get remote module.
exports.require = function (module) { exports.require = function (module) {
return metaToValue(ipcRenderer.sendSync(ipcs['b_req'], module)) return metaToValue(ipcRenderer.sendSync('ELECTRON_BROWSER_REQUIRE', module))
} }
// Alias to remote.require('electron').xxx. // Alias to remote.require('electron').xxx.
exports.getBuiltin = function (module) { exports.getBuiltin = function (module) {
return metaToValue(ipcRenderer.sendSync(ipcs['b_get_built'], module)) return metaToValue(ipcRenderer.sendSync('ELECTRON_BROWSER_GET_BUILTIN', module))
} }
// Get current BrowserWindow. // Get current BrowserWindow.
exports.getCurrentWindow = function () { exports.getCurrentWindow = function () {
return metaToValue(ipcRenderer.sendSync(ipcs['b_curr_win'])) return metaToValue(ipcRenderer.sendSync('ELECTRON_BROWSER_CURRENT_WINDOW'))
} }
// Get current WebContents object. // Get current WebContents object.
exports.getCurrentWebContents = function () { exports.getCurrentWebContents = function () {
return metaToValue(ipcRenderer.sendSync(ipcs['b_curr_web_cont'])) 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 = function (name) {
return metaToValue(ipcRenderer.sendSync(ipcs['b_glob'], name)) return metaToValue(ipcRenderer.sendSync('ELECTRON_BROWSER_GLOBAL', name))
} }
// Get the process object in browser. // Get the process object in browser.
@ -335,7 +316,7 @@ exports.createFunctionWithReturnValue = function (returnValue) {
// Get the guest WebContents from guestInstanceId. // Get the guest WebContents from guestInstanceId.
exports.getGuestWebContents = function (guestInstanceId) { exports.getGuestWebContents = function (guestInstanceId) {
const meta = ipcRenderer.sendSync(ipcs['b_guest_web_con'], guestInstanceId) const meta = ipcRenderer.sendSync('ELECTRON_BROWSER_GUEST_WEB_CONTENTS', guestInstanceId)
return metaToValue(meta) return metaToValue(meta)
} }