Bug 1313980 Avoid listening on browser-specific MM with connectNative() r?kmag draft
authorAndrew Swan <aswan@mozilla.com>
Thu, 10 Nov 2016 15:17:51 -0800
changeset 437478 92884736a45e98c3c5224c74abbec449548e9098
parent 437246 d38d06f85ef59c5dbb5d4a1a8d895957a78714de
child 536648 a1c8009f5b38490d097d4c01dcaf5c01708e7984
push id35422
push useraswan@mozilla.com
push dateThu, 10 Nov 2016 23:18:52 +0000
reviewerskmag
bugs1313980
milestone52.0a1
Bug 1313980 Avoid listening on browser-specific MM with connectNative() r?kmag MozReview-Commit-ID: Al57DUrys63
toolkit/components/extensions/NativeMessaging.jsm
toolkit/components/extensions/test/xpcshell/test_ext_native_messaging.js
--- 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();
+});