diff --git a/toolkit/components/extensions/ext-c-runtime.js b/toolkit/components/extensions/ext-c-runtime.js index 8991034e6c3e..192d3c5aec91 100644 --- a/toolkit/components/extensions/ext-c-runtime.js +++ b/toolkit/components/extensions/ext-c-runtime.js @@ -24,6 +24,7 @@ this.runtime = class extends ExtensionAPI { sendMessage(...args) { let extensionId, message, options, responseCallback; + if (typeof args[args.length - 1] === "function") { responseCallback = args.pop(); } @@ -55,18 +56,18 @@ this.runtime = class extends ExtensionAPI { message = args[0]; } else if (args.length === 2) { // With two optional arguments, this is the ambiguous case, - // particularly sendMessage("string", {}); + // particularly sendMessage("string", {} or null) // Given that sending a message within the extension is generally // more common than sending the empty object to another extension, // we prefer that conclusion, as long as the second argument looks - // like valid options. + // like valid options object, or is null/undefined. let [validOpts] = checkOptions(args[1]); - if (validOpts) { + if (validOpts || args[1] == null) { [message, options] = args; } else { [extensionId, message] = args; } - } else if (args.length === 3) { + } else if (args.length === 3 || (args.length === 4 && args[3] == null)) { [extensionId, message, options] = args; } else if (args.length === 4 && !responseCallback) { return Promise.reject({message: "runtime.sendMessage's last argument is not a function"}); diff --git a/toolkit/components/extensions/test/xpcshell/test_ext_runtime_sendMessage_args.js b/toolkit/components/extensions/test/xpcshell/test_ext_runtime_sendMessage_args.js index d2bb437b6362..5134f71117e6 100644 --- a/toolkit/components/extensions/test/xpcshell/test_ext_runtime_sendMessage_args.js +++ b/toolkit/components/extensions/test/xpcshell/test_ext_runtime_sendMessage_args.js @@ -59,6 +59,10 @@ add_task(async function() { // Due to this insane design we parse its arguments manually. This // test is meant to cover all the combinations. + // A single null or undefined argument is allowed, and represents the message + extension1.sendMessage(null); + await checkLocalMessage(null); + // With one argument, it must be just the message extension1.sendMessage("message"); await checkLocalMessage("message"); @@ -70,13 +74,25 @@ add_task(async function() { extension1.sendMessage(ID2, {msg: "message"}); await checkRemoteMessage({msg: "message"}); - // And this case should be (message, options) + // And these should be (message, options) extension1.sendMessage("message", {}); await checkLocalMessage("message"); + // or (message, non-callback), pick your poison + extension1.sendMessage("message", undefined); + await checkLocalMessage("message"); + // With three arguments, we send a cross-extension message extension1.sendMessage(ID2, "message", {}); await checkRemoteMessage("message"); + // Even when the last one is null or undefined + extension1.sendMessage(ID2, "message", undefined); + await checkRemoteMessage("message"); + + // The four params case is unambigous, so we allow null as a (non-) callback + extension1.sendMessage(ID2, "message", {}, null); + await checkRemoteMessage("message"); + await Promise.all([extension1.unload(), extension2.unload()]); }); diff --git a/toolkit/components/extensions/test/xpcshell/test_ext_runtime_sendMessage_errors.js b/toolkit/components/extensions/test/xpcshell/test_ext_runtime_sendMessage_errors.js index 9b0e00de7f41..cc60ff6622ab 100644 --- a/toolkit/components/extensions/test/xpcshell/test_ext_runtime_sendMessage_errors.js +++ b/toolkit/components/extensions/test/xpcshell/test_ext_runtime_sendMessage_errors.js @@ -9,7 +9,7 @@ add_task(async function test_sendMessage_error() { let testCases = [ // [arguments, expected error string], [[], "runtime.sendMessage's message argument is missing"], - [[null, null, null, null], "runtime.sendMessage's last argument is not a function"], + [[null, null, null, 42], "runtime.sendMessage's last argument is not a function"], [[null, null, 1], "runtime.sendMessage's options argument is invalid"], [[1, null, null], "runtime.sendMessage's extensionId argument is invalid"], [[null, null, null, null, null], "runtime.sendMessage received too many arguments"],