Bug 1022283 - Increase the timeout value of the NetworkLocationProvider and clean up its usage. r=kanru, a=1.4+
authorDoug Turner <doug.turner@gmail.com>
Sun, 08 Jun 2014 22:58:37 -0700
changeset 208346 3fecdb938683ea7b803adf6d9e2e5f05c92d15f8
parent 208345 da9599f0416f90fcae0fc9397da6582e9ac71353
child 208347 e4bad0e3a42a2af485a1ad6312b4db75e57418e6
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
bugs1022283
milestone32.0a2
Bug 1022283 - Increase the timeout value of the NetworkLocationProvider and clean up its usage. r=kanru, a=1.4+
dom/system/NetworkGeolocationProvider.js
--- a/dom/system/NetworkGeolocationProvider.js
+++ b/dom/system/NetworkGeolocationProvider.js
@@ -10,16 +10,27 @@ const Cc = Components.classes;
 
 const POSITION_UNAVAILABLE = Ci.nsIDOMGeoPositionError.POSITION_UNAVAILABLE;
 const SETTINGS_DEBUG_ENABLED = "geolocation.debugging.enabled";
 const SETTINGS_CHANGED_TOPIC = "mozsettings-changed";
 const SETTINGS_WIFI_ENABLED = "wifi.enabled";
 
 let gLoggingEnabled = false;
 
+/*
+   The gLocationRequestTimeout controls how long we wait on receiving an update
+   from the Wifi subsystem.  If this timer fires, we believe the Wifi scan has
+   had a problem and we no longer can use Wifi to position the user this time
+   around (we will continue to be hopeful that Wifi will recover).
+ 
+   This timeout value is also used when Wifi scanning is disabled (see
+   gWifiScanningEnabled).  In this case, we use this timer to collect cell/ip
+   data and xhr it to the location server.
+*/
+
 let gLocationRequestTimeout = 5000;
 
 let gWifiScanningEnabled = true;
 let gCellScanningEnabled = false;
 
 function LOG(aMsg) {
   if (gLoggingEnabled) {
     aMsg = "*** WIFI GEO: " + aMsg + "\n";
@@ -63,17 +74,17 @@ function WifiGeoPositionProvider() {
     gWifiScanningEnabled = Services.prefs.getBoolPref("geo.wifi.scan");
   } catch (e) {}
 
   try {
     gCellScanningEnabled = Services.prefs.getBoolPref("geo.cell.scan");
   } catch (e) {}
 
   this.wifiService = null;
-  this.timeoutTimer = null;
+  this.timer = null;
   this.started = false;
 }
 
 WifiGeoPositionProvider.prototype = {
   classID:          Components.ID("{77DA64D3-7458-4920-9491-86CC9914F904}"),
   QueryInterface:   XPCOMUtils.generateQI([Ci.nsIGeolocationProvider,
                                            Ci.nsIWifiListener,
                                            Ci.nsITimerCallback,
@@ -91,16 +102,28 @@ WifiGeoPositionProvider.prototype = {
         gLoggingEnabled = setting.value;
       } else if (setting.key == SETTINGS_WIFI_ENABLED) {
         gWifiScanningEnabled = setting.value;
       }
     } catch (e) {
     }
   },
 
+  resetTimer: function() {
+    if (this.timer) {
+      this.timer.cancel();
+      this.timer = null;
+    }
+    // wifi thread triggers WifiGeoPositionProvider to proceed, with no wifi, do manual timeout
+    this.timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
+    this.timer.initWithCallback(this,
+                                gLocationRequestTimeout,
+                                this.timer.TYPE_REPEATING_SLACK);
+  },
+
   startup:  function() {
     if (this.started)
       return;
 
     this.started = true;
     let self = this;
     let settingsCallback = {
       handle: function(name, result) {
@@ -136,38 +159,33 @@ WifiGeoPositionProvider.prototype = {
     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);
     }
 
-    if (!this.timeoutTimer) {
-      this.timeoutTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
-      this.timeoutTimer.initWithCallback(this,
-                                         gLocationRequestTimeout,
-                                         this.timeoutTimer.TYPE_REPEATING_SLACK);
-    }
+    this.resetTimer();
     LOG("startup called.");
   },
 
   watch: function(c) {
     this.listener = c;
   },
 
   shutdown: function() {
     LOG("shutdown called");
     if (this.started == false) {
       return;
     }
 
-    if (this.timeoutTimer) {
-      this.timeoutTimer.cancel();
-      this.timeoutTimer = null;
+    if (this.timer) {
+      this.timer.cancel();
+      this.timer = null;
     }
 
     if(this.wifiService) {
       this.wifiService.stopWatching(this);
       this.wifiService = null;
     }
 
     Services.obs.removeObserver(this, SETTINGS_CHANGED_TOPIC);
@@ -175,16 +193,20 @@ WifiGeoPositionProvider.prototype = {
     this.listener = null;
     this.started = false;
   },
 
   setHighAccuracy: function(enable) {
   },
 
   onChange: function(accessPoints) {
+
+    // we got some wifi data, rearm the timer.
+    this.resetTimer();
+
     function isPublic(ap) {
       let mask = "_nomap"
       let result = ap.ssid.indexOf(mask, ap.ssid.length - mask.length);
       if (result != -1) {
         LOG("Filtering out " + ap.ssid + " " + result);
       }
       return result;
     };
@@ -232,23 +254,18 @@ WifiGeoPositionProvider.prototype = {
         }
       }
       return result;
     } catch (e) {
       return null;
     }
   },
 
-  notify: function (timeoutTimer) {
-    // If Wifi scanning is disabled, then we can not depend on that for the
-    // heartbeat that drives location updates.  Instead, just use a timer which
-    // will drive the update.
-    if (gWifiScanningEnabled == false) {
-        this.sendLocationRequest(null);
-    }
+  notify: function (timer) {
+    this.sendLocationRequest(null);
   },
 
   sendLocationRequest: function (wifiData) {
     let url = Services.urlFormatter.formatURLPref("geo.wifi.uri");
     let listener = this.listener;
     LOG("Sending request: " + url + "\n");
 
     let xhr = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]