Bug 777203 - Respond to requests by sending our response directly back to the manager instead of broadcasting the response everywhere. r=gwagner
authorBlake Kaplan <mrbkap@gmail.com>
Wed, 22 Aug 2012 11:34:42 -0700
changeset 105100 517169ca9082facd14f12058c71ebad9b9e7a0cd
parent 105099 4666c4ccea092752dd84d4354359c1d084786b0e
child 105101 497c8c08b98171c2fb261601b8a99222c13affd2
push id55
push usershu@rfrn.org
push dateThu, 30 Aug 2012 01:33:09 +0000
reviewersgwagner
bugs777203
milestone17.0a1
Bug 777203 - Respond to requests by sending our response directly back to the manager instead of broadcasting the response everywhere. r=gwagner
dom/wifi/WifiWorker.js
--- a/dom/wifi/WifiWorker.js
+++ b/dom/wifi/WifiWorker.js
@@ -1873,73 +1873,76 @@ WifiWorker.prototype = {
   },
 
   // nsIWifi
 
   _fireEvent: function(message, data) {
     this._mm.sendAsyncMessage("WifiManager:" + message, data);
   },
 
-  _sendMessage: function(message, success, data, rid, mid) {
-    this._mm.sendAsyncMessage(message + (success ? ":OK" : ":NO"),
-                              { data: data, rid: rid, mid: mid });
+  _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) {
-    let msg = aMessage.json;
+    let msg = aMessage.json || {};
+    msg.manager = aMessage.target.QueryInterface(Ci.nsIFrameMessageManager);
+
     switch (aMessage.name) {
       case "WifiManager:setEnabled":
-        this.setWifiEnabled(msg.data, msg.rid, msg.mid);
+        this.setWifiEnabled(msg);
         break;
       case "WifiManager:getNetworks":
-        this.getNetworks(msg.rid, msg.mid);
+        this.getNetworks(msg);
         break;
       case "WifiManager:associate":
-        this.associate(msg.data, msg.rid, msg.mid);
+        this.associate(msg);
         break;
       case "WifiManager:forget":
-        this.forget(msg.data, msg.rid, msg.mid);
+        this.forget(msg);
         break;
       case "WifiManager:wps":
-        this.wps(msg.data, msg.rid, msg.mid);
+        this.wps(msg);
         break;
       case "WifiManager:getState": {
+        // TODO add aMessage.target to our map of targets.
         let net = this.currentNetwork ? netToDOM(this.currentNetwork) : null;
         return { network: net,
                  connectionInfo: this._lastConnectionInfo,
                  enabled: WifiManager.enabled,
                  status: translateState(WifiManager.state) };
       }
     }
   },
 
-  getNetworks: function(rid, mid) {
+  getNetworks: function(msg) {
     const message = "WifiManager:getNetworks:Return";
     if (WifiManager.state === "UNINITIALIZED") {
-      this._sendMessage(message, false, "Wifi is disabled", rid, mid);
+      this._sendMessage(message, false, "Wifi is disabled", msg);
       return;
     }
 
     this.waitForScan((function (networks) {
-      this._sendMessage(message, networks !== null, networks, rid, mid);
+      this._sendMessage(message, networks !== null, networks, msg);
     }).bind(this));
     WifiManager.scan(true, function() {});
   },
 
   _notifyAfterStateChange: function(success, newState) {
     // First, notify all of the requests that were trying to make this change.
     let state = this._stateRequests[0].enabled;
 
     // If the new state is not the same as state, then we weren't processing
     // the first request (we were racing somehow) so don't notify.
     if (!success || state === newState) {
       do {
         let req = this._stateRequests.shift();
         this._sendMessage("WifiManager:setEnabled:Return",
-                          success, state, req.rid, req.mid);
+                          success, state, req);
 
         // Don't remove more than one request if the previous one failed.
       } while (success &&
                this._stateRequests.length &&
                this._stateRequests[0].enabled === state);
     }
 
     // If there were requests queued after this one, run them.
@@ -1969,51 +1972,53 @@ WifiWorker.prototype = {
 
     // If we're enabling ourselves, then wait until we've connected to the
     // supplicant to notify. If we're disabling, we take care of this in
     // supplicantlost.
     if (WifiManager.supplicantStarted)
       WifiManager.start();
   },
 
-  setWifiEnabled: function(enable, rid, mid) {
+  setWifiEnabled: function(msg) {
     // There are two problems that we're trying to solve here:
     //   - If we get multiple requests to turn on and off wifi before the
     //     current request has finished, then we need to queue up the requests
     //     and handle each on/off request in turn.
     //   - Because we can't pass a callback to WifiManager.start, we need to
     //     have a way to communicate with our onsupplicantconnection callback.
-    this._stateRequests.push({ enabled: enable, rid: rid, mid: mid });
+    msg.enabled = msg.data;
+    this._stateRequests.push(msg);
     if (this._stateRequests.length === 1)
-      WifiManager.setWifiEnabled(enable, this._setWifiEnabledCallback.bind(this));
+      WifiManager.setWifiEnabled(msg.enabled, this._setWifiEnabledCallback.bind(this));
   },
 
-  associate: function(network, rid, mid) {
+  associate: function(msg) {
     const MAX_PRIORITY = 9999;
     const message = "WifiManager:associate:Return";
+    let network = msg.data;
     if (WifiManager.state === "UNINITIALIZED") {
-      this._sendMessage(message, false, "Wifi is disabled", rid, mid);
+      this._sendMessage(message, false, "Wifi is disabled", msg);
       return;
     }
 
     let privnet = network;
     let self = this;
     function networkReady() {
       // saveConfig now before we disable most of the other networks.
       function selectAndConnect() {
         WifiManager.enableNetwork(privnet.netId, true, function (ok) {
           if (ok)
             self._needToEnableNetworks = true;
           if (WifiManager.state === "DISCONNECTED" ||
               WifiManager.state === "SCANNING") {
             WifiManager.reconnect(function (ok) {
-              self._sendMessage(message, ok, ok, rid, mid);
+              self._sendMessage(message, ok, ok, msg);
             });
           } else {
-            self._sendMessage(message, ok, ok, rid, mid);
+            self._sendMessage(message, ok, ok, msg);
           }
         });
       }
 
       if (self._highestPriority >= MAX_PRIORITY)
         self._reprioritizeNetworks(selectAndConnect);
       else
         WifiManager.saveConfig(selectAndConnect);
@@ -2027,99 +2032,101 @@ WifiWorker.prototype = {
 
     netFromDOM(privnet, configured);
 
     privnet.priority = ++this._highestPriority;
     if (configured) {
       privnet.netId = configured.netId;
       WifiManager.updateNetwork(privnet, (function(ok) {
         if (!ok) {
-          this._sendMessage(message, false, "Network is misconfigured", rid, mid);
+          this._sendMessage(message, false, "Network is misconfigured", msg);
           return;
         }
 
         networkReady();
       }).bind(this));
     } else {
       // networkReady, above, calls saveConfig. We want to remember the new
       // network as being enabled, which isn't the default, so we explicitly
       // set it to being "enabled" before we add it and save the
       // configuration.
       privnet.disabled = 0;
       WifiManager.addNetwork(privnet, (function(ok) {
         if (!ok) {
-          this._sendMessage(message, false, "Network is misconfigured", rid, mid);
+          this._sendMessage(message, false, "Network is misconfigured", msg);
           return;
         }
 
         this.configuredNetworks[ssid] = privnet;
         networkReady();
       }).bind(this));
     }
   },
 
-  forget: function(network, rid, mid) {
+  forget: function(msg) {
     const message = "WifiManager:forget:Return";
+    let network = msg.data;
     if (WifiManager.state === "UNINITIALIZED") {
-      this._sendMessage(message, false, "Wifi is disabled", rid, mid);
+      this._sendMessage(message, false, "Wifi is disabled", msg);
       return;
     }
 
     let ssid = network.ssid;
     if (!(ssid in this.configuredNetworks)) {
-      this._sendMessage(message, false, "Trying to forget an unknown network", rid, mid);
+      this._sendMessage(message, false, "Trying to forget an unknown network", msg);
       return;
     }
 
     let self = this;
     let configured = this.configuredNetworks[ssid];
     this._reconnectOnDisconnect = (this.currentNetwork &&
                                    (this.currentNetwork.ssid === ssid));
     WifiManager.removeNetwork(configured.netId, function(ok) {
       if (!ok) {
-        self._sendMessage(message, false, "Unable to remove the network", rid, mid);
+        self._sendMessage(message, false, "Unable to remove the network", msg);
         self._reconnectOnDisconnect = false;
         return;
       }
 
       WifiManager.saveConfig(function() {
         self._reloadConfiguredNetworks(function() {
-          self._sendMessage(message, true, true, rid, mid);
+          self._sendMessage(message, true, true, msg);
         });
       });
     });
   },
 
-  wps: function(detail, rid, mid) {
+  wps: function(msg) {
     const message = "WifiManager:wps:Return";
     let self = this;
+    let detail = msg.data;
     if (detail.method === "pbc") {
       WifiManager.wpsPbc(function(ok) {
         if (ok)
-          self._sendMessage(message, true, true, rid, mid);
+          self._sendMessage(message, true, true, msg);
         else
-          self._sendMessage(message, false, "WPS PBC failed", rid, mid);
+          self._sendMessage(message, false, "WPS PBC failed", msg);
       });
     } else if (detail.method === "pin") {
       WifiManager.wpsPin(detail.pin, function(pin) {
         if (pin)
-          self._sendMessage(message, true, pin, rid, mid);
+          self._sendMessage(message, true, pin, msg);
         else
-          self._sendMessage(message, false, "WPS PIN failed", rid, mid);
+          self._sendMessage(message, false, "WPS PIN failed", msg);
       });
     } else if (detail.method === "cancel") {
       WifiManager.wpsCancel(function(ok) {
         if (ok)
-          self._sendMessage(message, true, true, rid, mid);
+          self._sendMessage(message, true, true, msg);
         else
-          self._sendMessage(message, false, "WPS Cancel failed", rid, mid);
+          self._sendMessage(message, false, "WPS Cancel failed", msg);
       });
     } else {
       self._sendMessage(message, false, "Invalid WPS method=" + detail.method,
-                        rid, mid);
+                        msg);
     }
   },
 
   // This is a bit ugly, but works. In particular, this depends on the fact
   // that RadioManager never actually tries to get the worker from us.
   get worker() { throw "Not implemented"; },
 
   shutdown: function() {