Bug 1313980 Avoid listening on browser-specific MM with connectNative() r?kmag
MozReview-Commit-ID: Al57DUrys63
--- a/toolkit/components/extensions/NativeMessaging.jsm
+++ b/toolkit/components/extensions/NativeMessaging.jsm
@@ -226,17 +226,17 @@ this.NativeApp = class extends EventEmit
* and receive messages from the port's creator.
* @param {string} portId A unique internal ID that identifies the port.
* @param {object} sender The object describing the creator of the connection
* request.
* @param {string} application The name of the native messaging host.
*/
static onConnectNative(context, messageManager, portId, sender, application) {
let app = new NativeApp(context, application);
- let port = new ExtensionUtils.Port(context, messageManager, [messageManager], "", portId, sender, sender);
+ let port = new ExtensionUtils.Port(context, messageManager, [Services.mm], "", portId, sender, sender);
app.once("disconnect", (what, err) => port.disconnect(err));
/* eslint-disable mozilla/balanced-listeners */
app.on("message", (what, msg) => port.postMessage(msg));
/* eslint-enable mozilla/balanced-listeners */
port.registerOnMessage(msg => app.send(msg));
port.registerOnDisconnect(msg => app.close());
--- a/toolkit/components/extensions/test/xpcshell/test_ext_native_messaging.js
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_native_messaging.js
@@ -471,8 +471,44 @@ add_task(function* test_stderr() {
yield waitForSubprocessExit();
});
let lines = STDERR_LINES.map(line => messages.findIndex(msg => msg.message.includes(line)));
notEqual(lines[0], -1, "Saw first line of stderr output on the console");
notEqual(lines[1], -1, "Saw second line of stderr output on the console");
notEqual(lines[0], lines[1], "Stderr output lines are separated in the console");
});
+
+// Test that calling connectNative() multiple times works
+// (bug 1313980 was a previous regression in this area)
+add_task(function* test_multiple_connects() {
+ async function background() {
+ function once() {
+ return new Promise(resolve => {
+ let MSG = "hello";
+ let port = browser.runtime.connectNative("echo");
+
+ port.onMessage.addListener(msg => {
+ browser.test.assertEq(MSG, msg, "Got expected message back");
+ port.disconnect();
+ resolve();
+ });
+ port.postMessage(MSG);
+ });
+ }
+
+ await once();
+ await once();
+ browser.test.notifyPass("multiple-connect");
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ background,
+ manifest: {
+ applications: {gecko: {id: ID}},
+ permissions: ["nativeMessaging"],
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("multiple-connect");
+ yield extension.unload();
+});