Bug 1022279 - NetworkLocationProvider should respect wifi.enabled settings. r=kanru
authorDoug Turner <doug.turner@gmail.com>
Sun, 08 Jun 2014 22:56:29 -0700
changeset 187788 819fc3d1f260f62015ff1f42ceb2eaf571a4be8f
parent 187787 ba5f018980686303317d579d65608984213faac0
child 187789 c935b461efdf2510eb3b20e6182535bee49a45ba
push id26934
push userryanvm@gmail.com
push dateTue, 10 Jun 2014 04:45:09 +0000
treeherdermozilla-central@9dc0ffca10f4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskanru
bugs1022279
milestone32.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1022279 - NetworkLocationProvider should respect wifi.enabled settings. r=kanru
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) {
   },