chore: remove special handling for dialog methods in remote module (#17412)

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

* refactor: remove callFunction helper
This commit is contained in:
Milan Burda 2019-03-19 19:59:33 +01:00 committed by Samuel Attard
parent f4c3151815
commit 06a4594435
3 changed files with 18 additions and 37 deletions

View file

@ -221,8 +221,3 @@ module.exports.showMessageBox = deprecate.promisify(module.exports.showMessageBo
module.exports.showOpenDialog = deprecate.promisify(module.exports.showOpenDialog) module.exports.showOpenDialog = deprecate.promisify(module.exports.showOpenDialog)
module.exports.showSaveDialog = deprecate.promisify(module.exports.showSaveDialog) module.exports.showSaveDialog = deprecate.promisify(module.exports.showSaveDialog)
module.exports.showCertificateTrustDialog = deprecate.promisify(module.exports.showCertificateTrustDialog) 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)

View file

@ -242,32 +242,6 @@ const unwrapArgs = function (sender, frameId, contextId, args) {
return args.map(metaToValue) 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 isRemoteModuleEnabledCache = new WeakMap()
const isRemoteModuleEnabled = function (contents) { const isRemoteModuleEnabled = function (contents) {
@ -401,7 +375,13 @@ handleRemoteCommand('ELECTRON_BROWSER_FUNCTION_CALL', function (event, contextId
throwRPCError(`Cannot call function on missing remote object ${id}`) 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) { handleRemoteCommand('ELECTRON_BROWSER_MEMBER_CONSTRUCTOR', function (event, contextId, id, method, args) {
@ -417,13 +397,19 @@ handleRemoteCommand('ELECTRON_BROWSER_MEMBER_CONSTRUCTOR', function (event, cont
handleRemoteCommand('ELECTRON_BROWSER_MEMBER_CALL', function (event, contextId, id, method, args) { handleRemoteCommand('ELECTRON_BROWSER_MEMBER_CALL', function (event, contextId, id, method, args) {
args = unwrapArgs(event.sender, event.frameId, contextId, args) args = unwrapArgs(event.sender, event.frameId, contextId, args)
const obj = objectsRegistry.get(id) const object = objectsRegistry.get(id)
if (obj == null) { if (object == null) {
throwRPCError(`Cannot call function '${method}' on missing remote object ${id}`) 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) { handleRemoteCommand('ELECTRON_BROWSER_MEMBER_SET', function (event, contextId, id, name, args) {

View file

@ -77,7 +77,7 @@ describe('ipc main module', () => {
}) })
ipcMain.once('error-message', (event, message) => { 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() expect(correctMsgStart).to.be.true()
done() done()
}) })