Bug 1313980 Avoid listening on browser-specific MM with connectNative() r=kmag
authorAndrew Swan <aswan@mozilla.com>
Thu, 10 Nov 2016 15:17:51 -0800
changeset 352227 a8a3319572786338113ac02ba70e950c45c238ae
parent 352226 44af2c3d71ea627858f269f61c80a82800e6ffe1
child 352228 6a47316764f049e364e93403c60481a04ff57b31
push id6795
push userjlund@mozilla.com
push dateMon, 23 Jan 2017 14:19:46 +0000
treeherdermozilla-esr52@76101b503191 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
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();
+});