Bug 777203 - Keep a list of managers that are interested in wifi events and send notifications only to them instead of broadcasting notifications to the world. r=gwagner DONTBUILD
authorBlake Kaplan <mrbkap@gmail.com>
Wed, 22 Aug 2012 12:37:11 -0700
changeset 105101 497c8c08b98171c2fb261601b8a99222c13affd2
parent 105100 517169ca9082facd14f12058c71ebad9b9e7a0cd
child 105102 53e7eb3528cc3f9643481a3282b02cea11076e6c
push id55
push usershu@rfrn.org
push dateThu, 30 Aug 2012 01:33:09 +0000
reviewersgwagner
bugs777203
milestone17.0a1
Bug 777203 - Keep a list of managers that are interested in wifi events and send notifications only to them instead of broadcasting notifications to the world. r=gwagner DONTBUILD
dom/wifi/DOMWifiManager.js
dom/wifi/WifiWorker.js
--- a/dom/wifi/DOMWifiManager.js
+++ b/dom/wifi/DOMWifiManager.js
@@ -103,16 +103,18 @@ DOMWifiManager.prototype = {
     }
   },
 
   uninit: function() {
     this._onStatusChange = null;
     this._onConnectionInfoUpdate = null;
     this._onEnabled = null;
     this._onDisabled = null;
+
+    this._mm.sendAsyncMessage("WifiManager:managerFinished");
   },
 
   _sendMessageForRequest: function(name, data, request) {
     let id = this.getRequestId(request);
     this._mm.sendAsyncMessage(name, { data: data, rid: id, mid: this._id });
   },
 
   receiveMessage: function(aMessage) {
--- a/dom/wifi/WifiWorker.js
+++ b/dom/wifi/WifiWorker.js
@@ -1324,17 +1324,18 @@ let netToDOM;
 let netFromDOM;
 
 function WifiWorker() {
   var self = this;
 
   this._mm = Cc["@mozilla.org/parentprocessmessagemanager;1"].getService(Ci.nsIFrameMessageManager);
   const messages = ["WifiManager:setEnabled", "WifiManager:getNetworks",
                     "WifiManager:associate", "WifiManager:forget",
-                    "WifiManager:wps", "WifiManager:getState"];
+                    "WifiManager:wps", "WifiManager:getState",
+                    "WifiManager:managerFinished"];
 
   messages.forEach((function(msgName) {
     this._mm.addMessageListener(msgName, this);
   }).bind(this));
 
   this.wantScanResults = [];
 
   this._needToEnableNetworks = false;
@@ -1869,18 +1870,21 @@ WifiWorker.prototype = {
           });
         });
       }
     }
   },
 
   // nsIWifi
 
+  _domManagers: [],
   _fireEvent: function(message, data) {
-    this._mm.sendAsyncMessage("WifiManager:" + message, data);
+    this._domManagers.forEach(function(obj) {
+      obj.manager.sendAsyncMessage("WifiManager:" + message, data);
+    });
   },
 
   _sendMessage: function(message, success, data, msg) {
     msg.manager.sendAsyncMessage(message + (success ? ":OK" : ":NO"),
                                  { data: data, rid: msg.rid, mid: msg.mid });
   },
 
   receiveMessage: function MessageManager_receiveMessage(aMessage) {
@@ -1899,23 +1903,48 @@ WifiWorker.prototype = {
         break;
       case "WifiManager:forget":
         this.forget(msg);
         break;
       case "WifiManager:wps":
         this.wps(msg);
         break;
       case "WifiManager:getState": {
-        // TODO add aMessage.target to our map of targets.
         let net = this.currentNetwork ? netToDOM(this.currentNetwork) : null;
+        let i;
+        for (i = 0; i < this._domManagers.length; ++i) {
+          let obj = this._domManagers[i];
+          if (obj.manager === msg.manager) {
+            obj.count++;
+            break;
+          }
+        }
+
+        if (i === this._domManagers.length) {
+          this._domManagers.push({ manager: msg.manager, count: 1 });
+        }
+
         return { network: net,
                  connectionInfo: this._lastConnectionInfo,
                  enabled: WifiManager.enabled,
                  status: translateState(WifiManager.state) };
       }
+      case "WifiManager:managerFinished": {
+        for (let i = 0; i < this._domManagers.length; ++i) {
+          let obj = this._domManagers[i];
+          if (obj.manager === msg.manager) {
+            if (--obj.count === 0) {
+              this._domManagers.splice(i, 1);
+            }
+            break;
+          }
+        }
+
+        break;
+      }
     }
   },
 
   getNetworks: function(msg) {
     const message = "WifiManager:getNetworks:Return";
     if (WifiManager.state === "UNINITIALIZED") {
       this._sendMessage(message, false, "Wifi is disabled", msg);
       return;