From 06a45944359709b7110bb0b6ce1b46bbef91c6af Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Tue, 19 Mar 2019 19:59:33 +0100 Subject: [PATCH] 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 --- lib/browser/api/dialog.js | 5 ---- lib/browser/rpc-server.js | 48 ++++++++++++++------------------------- spec/api-ipc-main-spec.js | 2 +- 3 files changed, 18 insertions(+), 37 deletions(-) diff --git a/lib/browser/api/dialog.js b/lib/browser/api/dialog.js index 79d54813127c..e5d47c579c2a 100644 --- a/lib/browser/api/dialog.js +++ b/lib/browser/api/dialog.js @@ -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) diff --git a/lib/browser/rpc-server.js b/lib/browser/rpc-server.js index 131e25c52545..b0c1a30168b4 100644 --- a/lib/browser/rpc-server.js +++ b/lib/browser/rpc-server.js @@ -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) { @@ -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) { @@ -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) { diff --git a/spec/api-ipc-main-spec.js b/spec/api-ipc-main-spec.js index de7fb222e1e7..40deecbd9c7f 100644 --- a/spec/api-ipc-main-spec.js +++ b/spec/api-ipc-main-spec.js @@ -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() })