зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1368669 - Support explicit null callback for runtime.sendMessage r=aswan
MozReview-Commit-ID: Lauyk877pIQ --HG-- extra : rebase_source : b69b4bc5fded80fec6893ffe56408c82df023602
This commit is contained in:
Родитель
e828570043
Коммит
97b67aa72b
|
@ -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"});
|
||||
|
|
|
@ -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()]);
|
||||
});
|
||||
|
|
|
@ -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"],
|
||||
|
|
Загрузка…
Ссылка в новой задаче