Bug 1022279 - NetworkLocationProvider should respect wifi.enabled settings. r=kanru, a=1.4+
authorDoug Turner <doug.turner@gmail.com>
Sun, 08 Jun 2014 22:56:29 -0700
changeset 208345 da9599f0416f90fcae0fc9397da6582e9ac71353
parent 208344 bb6c3edb30e326324510f6ed6f5687c68f080ad1
child 208346 3fecdb938683ea7b803adf6d9e2e5f05c92d15f8
push id494
push userraliiev@mozilla.com
push dateMon, 25 Aug 2014 18:42:16 +0000
treeherdermozilla-release@a3cc3e46b571 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskanru, 1
bugs1022279
milestone32.0a2
Bug 1022279 - NetworkLocationProvider should respect wifi.enabled settings. r=kanru, a=1.4+
dom/system/NetworkGeolocationProvider.js
--- a/dom/system/NetworkGeolocationProvider.js
+++ b/dom/system/NetworkGeolocationProvider.js
@@ -4,18 +4,19 @@
 
 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
 Components.utils.import("resource://gre/modules/Services.jsm");
 
 const Ci = Components.interfaces;
 const Cc = Components.classes;
 
 const POSITION_UNAVAILABLE = Ci.nsIDOMGeoPositionError.POSITION_UNAVAILABLE;
-const SETTING_DEBUG_ENABLED = "geolocation.debugging.enabled";
-const SETTING_CHANGED_TOPIC = "mozsettings-changed";
+const SETTINGS_DEBUG_ENABLED = "geolocation.debugging.enabled";
+const SETTINGS_CHANGED_TOPIC = "mozsettings-changed";
+const SETTINGS_WIFI_ENABLED = "wifi.enabled";
 
 let gLoggingEnabled = false;
 
 let gLocationRequestTimeout = 5000;
 
 let gWifiScanningEnabled = true;
 let gCellScanningEnabled = false;
 
@@ -75,66 +76,82 @@ WifiGeoPositionProvider.prototype = {
   classID:          Components.ID("{77DA64D3-7458-4920-9491-86CC9914F904}"),
   QueryInterface:   XPCOMUtils.generateQI([Ci.nsIGeolocationProvider,
                                            Ci.nsIWifiListener,
                                            Ci.nsITimerCallback,
                                            Ci.nsIObserver]),
   listener: null,
 
   observe: function(aSubject, aTopic, aData) {
-    if (aTopic != SETTING_CHANGED_TOPIC) {
+    if (aTopic != SETTINGS_CHANGED_TOPIC) {
       return;
     }
 
     try {
       let setting = JSON.parse(aData);
-      if (setting.key != SETTING_DEBUG_ENABLED) {
-          return;
+      if (setting.key == SETTINGS_DEBUG_ENABLED) {
+        gLoggingEnabled = setting.value;
+      } else if (setting.key == SETTINGS_WIFI_ENABLED) {
+        gWifiScanningEnabled = setting.value;
       }
-      gLoggingEnabled = setting.value;
     } catch (e) {
     }
   },
 
   startup:  function() {
     if (this.started)
       return;
 
     this.started = true;
+    let self = this;
     let settingsCallback = {
       handle: function(name, result) {
-        gLoggingEnabled = result && result.value === true ? true : false;
+        if (name == SETTINGS_DEBUG_ENABLED) {
+          gLoggingEnabled = result;
+        } else if (name == SETTINGS_WIFI_ENABLED) {
+          gWifiScanningEnabled = result;
+          if (self.wifiService) {
+            self.wifiService.stopWatching(self);
+          }
+          if (gWifiScanningEnabled) {
+            self.wifiService = Cc["@mozilla.org/wifi/monitor;1"].getService(Ci.nsIWifiMonitor);
+            self.wifiService.startWatching(self);
+          }
+        }
       },
       
       handleError: function(message) {
         gLoggingEnabled = false;
         LOG("settings callback threw an exception, dropping");
       }
     };
 
     try {
-      Services.obs.addObserver(this, SETTING_CHANGED_TOPIC, false);
+      Services.obs.addObserver(this, SETTINGS_CHANGED_TOPIC, false);
       let settings = Cc["@mozilla.org/settingsService;1"].getService(Ci.nsISettingsService);
-      settings.createLock().get(SETTING_DEBUG_ENABLED, settingsCallback);
+      settings.createLock().get(SETTINGS_WIFI_ENABLED, settingsCallback);
+      settings.createLock().get(SETTINGS_DEBUG_ENABLED, settingsCallback);
     } catch(ex) {
       // This platform doesn't have the settings interface, and that is just peachy
     }
 
     if (gWifiScanningEnabled && Cc["@mozilla.org/wifi/monitor;1"]) {
       if (this.wifiService) {
         this.wifiService.stopWatching(this);
       }
       this.wifiService = Cc["@mozilla.org/wifi/monitor;1"].getService(Ci.nsIWifiMonitor);
       this.wifiService.startWatching(this);
     }
-    // wifi thread triggers WifiGeoPositionProvider to proceed, with no wifi, do manual timeout
-    this.timeoutTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
-    this.timeoutTimer.initWithCallback(this,
-                                       gLocationRequestTimeout,
-                                       this.timeoutTimer.TYPE_REPEATING_SLACK);
+
+    if (!this.timeoutTimer) {
+      this.timeoutTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
+      this.timeoutTimer.initWithCallback(this,
+                                         gLocationRequestTimeout,
+                                         this.timeoutTimer.TYPE_REPEATING_SLACK);
+    }
     LOG("startup called.");
   },
 
   watch: function(c) {
     this.listener = c;
   },
 
   shutdown: function() {
@@ -148,17 +165,17 @@ WifiGeoPositionProvider.prototype = {
       this.timeoutTimer = null;
     }
 
     if(this.wifiService) {
       this.wifiService.stopWatching(this);
       this.wifiService = null;
     }
 
-    Services.obs.removeObserver(this, SETTING_CHANGED_TOPIC);
+    Services.obs.removeObserver(this, SETTINGS_CHANGED_TOPIC);
 
     this.listener = null;
     this.started = false;
   },
 
   setHighAccuracy: function(enable) {
   },