Skip to content

Commit

Permalink
chore: remove special handling for dialog methods in remote module (e…
Browse files Browse the repository at this point in the history
…lectron#17412)

* chore: remove special handling for dialog methods in remote module

* refactor: remove callFunction helper
  • Loading branch information
miniak authored and kiku-jw committed May 16, 2019
1 parent 202d568 commit c614383
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 37 deletions.
5 changes: 0 additions & 5 deletions lib/browser/api/dialog.js
Expand Up @@ -221,8 +221,3 @@ module.exports.showMessageBox = deprecate.promisify(module.exports.showMessageBo
module.exports.showOpenDialog = deprecate.promisify(module.exports.showOpenDialog)
module.exports.showSaveDialog = deprecate.promisify(module.exports.showSaveDialog)
module.exports.showCertificateTrustDialog = deprecate.promisify(module.exports.showCertificateTrustDialog)

// Mark standard asynchronous functions.
v8Util.setHiddenValue(module.exports.showMessageBox, 'asynchronous', true)
v8Util.setHiddenValue(module.exports.showOpenDialog, 'asynchronous', true)
v8Util.setHiddenValue(module.exports.showSaveDialog, 'asynchronous', true)
48 changes: 17 additions & 31 deletions lib/browser/rpc-server.js
Expand Up @@ -242,32 +242,6 @@ const unwrapArgs = function (sender, frameId, contextId, args) {
return args.map(metaToValue)
}

// Call a function and send reply asynchronously if it's a an asynchronous
// style function and the caller didn't pass a callback.
const callFunction = function (event, contextId, func, caller, args) {
const funcMarkedAsync = v8Util.getHiddenValue(func, 'asynchronous')
const funcPassedCallback = typeof args[args.length - 1] === 'function'
try {
if (funcMarkedAsync && !funcPassedCallback) {
args.push(function (ret) {
event.returnValue = valueToMeta(event.sender, contextId, ret, true)
})
func.apply(caller, args)
} else {
const ret = func.apply(caller, args)
return valueToMeta(event.sender, contextId, ret, true)
}
} catch (error) {
// 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.`
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}`)
err.cause = error
throw err
}
}

const isRemoteModuleEnabledCache = new WeakMap()

const isRemoteModuleEnabled = function (contents) {
Expand Down Expand Up @@ -401,7 +375,13 @@ handleRemoteCommand('ELECTRON_BROWSER_FUNCTION_CALL', function (event, contextId
throwRPCError(`Cannot call function on missing remote object ${id}`)
}

return callFunction(event, contextId, func, global, args)
try {
return valueToMeta(event.sender, contextId, func(...args), true)
} catch (error) {
const err = new Error(`Could not call remote function '${func.name || 'anonymous'}'. Check that the function signature is correct. Underlying error: ${error.message}`)
err.cause = error
throw err
}
})

handleRemoteCommand('ELECTRON_BROWSER_MEMBER_CONSTRUCTOR', function (event, contextId, id, method, args) {
Expand All @@ -417,13 +397,19 @@ handleRemoteCommand('ELECTRON_BROWSER_MEMBER_CONSTRUCTOR', function (event, cont

handleRemoteCommand('ELECTRON_BROWSER_MEMBER_CALL', function (event, contextId, id, method, args) {
args = unwrapArgs(event.sender, event.frameId, contextId, args)
const obj = objectsRegistry.get(id)
const object = objectsRegistry.get(id)

if (obj == null) {
throwRPCError(`Cannot call function '${method}' on missing remote object ${id}`)
if (object == null) {
throwRPCError(`Cannot call method '${method}' on missing remote object ${id}`)
}

return callFunction(event, contextId, obj[method], obj, args)
try {
return valueToMeta(event.sender, contextId, object[method](...args), true)
} catch (error) {
const err = new Error(`Could not call remote method '${method}'. Check that the method signature is correct. Underlying error: ${error.message}`)
err.cause = error
throw err
}
})

handleRemoteCommand('ELECTRON_BROWSER_MEMBER_SET', function (event, contextId, id, name, args) {
Expand Down
2 changes: 1 addition & 1 deletion spec/api-ipc-main-spec.js
Expand Up @@ -77,7 +77,7 @@ describe('ipc main module', () => {
})

ipcMain.once('error-message', (event, message) => {
const correctMsgStart = message.startsWith('Cannot call function \'getURL\' on missing remote object')
const correctMsgStart = message.startsWith('Cannot call method \'getURL\' on missing remote object')
expect(correctMsgStart).to.be.true()
done()
})
Expand Down

0 comments on commit c614383

Please sign in to comment.