Bug 1286746 - Invoke port.onDisconnect if there are no onConnect listeners. r=kmag
authorRob Wu <rob@robwu.nl>
Fri, 15 Jul 2016 17:46:00 -0700
changeset 333350 c208ce0321e8891ff81c35d64cca89dc977f8b1d
parent 333349 232d6b6ae7908041cdb09fff963990a1503d63d8
child 333351 02afaa4b6e393c3798bf767bb86c82d30fcb0ccf
push id10033
push userraliiev@mozilla.com
push dateMon, 19 Sep 2016 13:50:26 +0000
treeherdermozilla-aurora@5dddbefdf759 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskmag
bugs1286746
milestone50.0a1
Bug 1286746 - Invoke port.onDisconnect if there are no onConnect listeners. r=kmag MozReview-Commit-ID: DPs36oFm25J
toolkit/components/extensions/ExtensionUtils.jsm
toolkit/components/extensions/test/xpcshell/test_ext_runtime_connect_no_receiver.js
toolkit/components/extensions/test/xpcshell/xpcshell.ini
--- a/toolkit/components/extensions/ExtensionUtils.jsm
+++ b/toolkit/components/extensions/ExtensionUtils.jsm
@@ -1141,26 +1141,30 @@ Port.prototype = {
   handleDisconnection() {
     this.messageManager.removeMessageListener(this.disconnectName, this);
     this.context.forgetOnClose(this);
     this.disconnected = true;
   },
 
   receiveMessage(msg) {
     if (msg.name == this.disconnectName) {
-      if (this.disconnected) {
-        return;
-      }
+      this.disconnectByOtherEnd();
+    }
+  },
 
-      for (let listener of this.disconnectListeners) {
-        listener();
-      }
+  disconnectByOtherEnd() {
+    if (this.disconnected) {
+      return;
+    }
 
-      this.handleDisconnection();
+    for (let listener of this.disconnectListeners) {
+      listener();
     }
+
+    this.handleDisconnection();
   },
 
   disconnect() {
     if (this.disconnected) {
       // disconnect() may be called without side effects even after the port is
       // closed - https://developer.chrome.com/extensions/runtime#type-Port
       return;
     }
@@ -1275,18 +1279,18 @@ Messenger.prototype = {
     }).api();
   },
 
   connect(messageManager, name, recipient) {
     // TODO(robwu): Use a process ID instead of the process type. bugzil.la/1287626
     let portId = `${gNextPortId++}-${Services.appinfo.processType}`;
     let port = new Port(this.context, messageManager, name, portId, null);
     let msg = {name, portId};
-    // TODO: Disconnect the port if no response?
-    this._sendMessage(messageManager, "Extension:Connect", msg, recipient);
+    this._sendMessage(messageManager, "Extension:Connect", msg, recipient)
+      .catch(e => port.disconnectByOtherEnd());
     return port.api();
   },
 
   onConnect(name) {
     return new SingletonEventManager(this.context, name, callback => {
       let listener = {
         messageFilterPermissive: this.filter,
 
new file mode 100644
--- /dev/null
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_runtime_connect_no_receiver.js
@@ -0,0 +1,22 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* test_connect_without_listener() {
+  function background() {
+    let port = browser.runtime.connect();
+    port.onDisconnect.addListener(() => {
+      browser.test.notifyPass("port.onDisconnect was called");
+    });
+  }
+  let extensionData = {
+    background,
+  };
+
+  let extension = ExtensionTestUtils.loadExtension(extensionData);
+  yield extension.startup();
+
+  yield extension.awaitFinish("port.onDisconnect was called");
+
+  yield extension.unload();
+});
--- a/toolkit/components/extensions/test/xpcshell/xpcshell.ini
+++ b/toolkit/components/extensions/test/xpcshell/xpcshell.ini
@@ -30,16 +30,17 @@ skip-if = os == "android"
 [test_ext_idle.js]
 [test_ext_json_parser.js]
 [test_ext_localStorage.js]
 [test_ext_manifest_content_security_policy.js]
 [test_ext_manifest_incognito.js]
 [test_ext_native_messaging.js]
 skip-if = os == "android"
 [test_ext_onmessage_removelistener.js]
+[test_ext_runtime_connect_no_receiver.js]
 [test_ext_runtime_getPlatformInfo.js]
 [test_ext_runtime_sendMessage.js]
 [test_ext_schemas.js]
 [test_ext_simple.js]
 [test_ext_storage.js]
 [test_getAPILevelForWindow.js]
 [test_locale_converter.js]
 [test_locale_data.js]