From dde90bad3ecb5dadf7958a84abc0eaa017d13155 Mon Sep 17 00:00:00 2001 From: Bharath Thiruveedula Date: Tue, 8 Oct 2013 08:22:42 -0400 Subject: [PATCH] Bug 684722 - Invoke geolocation error handlers when xhr fails. r=jdm --- dom/src/geolocation/nsGeolocation.cpp | 18 ++++--- dom/system/NetworkGeolocationProvider.js | 14 +++++- .../geolocation/geolocation_common.js | 5 ++ dom/tests/mochitest/geolocation/mochitest.ini | 1 + .../geolocation/network_geolocation.sjs | 6 +++ .../geolocation/test_errorcheck.html | 50 +++++++++++++++++++ 6 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 dom/tests/mochitest/geolocation/test_errorcheck.html diff --git a/dom/src/geolocation/nsGeolocation.cpp b/dom/src/geolocation/nsGeolocation.cpp index d3f98705672a..add5db4f2a8f 100644 --- a/dom/src/geolocation/nsGeolocation.cpp +++ b/dom/src/geolocation/nsGeolocation.cpp @@ -79,6 +79,7 @@ class nsGeolocationRequest void SendLocation(nsIDOMGeoPosition* location); bool WantsHighAccuracy() {return mOptions && mOptions->mEnableHighAccuracy;} void SetTimeoutTimer(); + void NotifyErrorAndShutdown(uint16_t); nsIPrincipal* GetPrincipal(); ~nsGeolocationRequest(); @@ -366,6 +367,13 @@ NS_IMPL_CYCLE_COLLECTION_3(nsGeolocationRequest, mCallback, mErrorCallback, mLoc NS_IMETHODIMP nsGeolocationRequest::Notify(nsITimer* aTimer) +{ + NotifyErrorAndShutdown(nsIDOMGeoPositionError::TIMEOUT); + return NS_OK; +} + +void +nsGeolocationRequest::NotifyErrorAndShutdown(uint16_t aErrorCode) { MOZ_ASSERT(!mShutdown, "timeout after shutdown"); @@ -374,13 +382,11 @@ nsGeolocationRequest::Notify(nsITimer* aTimer) mLocator->RemoveRequest(this); } - NotifyError(nsIDOMGeoPositionError::TIMEOUT); + NotifyError(aErrorCode); if (!mShutdown) { SetTimeoutTimer(); } - - return NS_OK; } NS_IMETHODIMP @@ -1160,13 +1166,13 @@ Geolocation::NotifyError(uint16_t aErrorCode) } for (uint32_t i = mPendingCallbacks.Length(); i > 0; i--) { - mPendingCallbacks[i-1]->NotifyError(aErrorCode); - RemoveRequest(mPendingCallbacks[i-1]); + mPendingCallbacks[i-1]->NotifyErrorAndShutdown(aErrorCode); + //NotifyErrorAndShutdown() removes the request from the array } // notify everyone that is watching for (uint32_t i = 0; i < mWatchingCallbacks.Length(); i++) { - mWatchingCallbacks[i]->NotifyError(aErrorCode); + mWatchingCallbacks[i]->NotifyErrorAndShutdown(aErrorCode); } return NS_OK; diff --git a/dom/system/NetworkGeolocationProvider.js b/dom/system/NetworkGeolocationProvider.js index b17b550b4867..dd3b928d8f27 100755 --- a/dom/system/NetworkGeolocationProvider.js +++ b/dom/system/NetworkGeolocationProvider.js @@ -170,22 +170,30 @@ WifiGeoPositionProvider.prototype = { // This is a background load - xhr.open("POST", url, true); + try { + xhr.open("POST", url, true); + } catch (e) { + triggerError(); + return; + } xhr.setRequestHeader("Content-Type", "application/json; charset=UTF-8"); xhr.responseType = "json"; xhr.mozBackgroundRequest = true; xhr.channel.loadFlags = Ci.nsIChannel.LOAD_ANONYMOUS; xhr.onerror = function() { LOG("onerror: " + xhr); + triggerError(); }; xhr.onload = function() { LOG("gls returned status: " + xhr.status + " --> " + JSON.stringify(xhr.response)); if (xhr.channel instanceof Ci.nsIHttpChannel && xhr.status != 200) { + triggerError(); return; } if (!xhr.response || !xhr.response.location) { + triggerError(); return; } @@ -218,4 +226,8 @@ WifiGeoPositionProvider.prototype = { }, }; +function triggerError() { + Cc["@mozilla.org/geolocation/service;1"].getService(Ci.nsIGeolocationUpdate) + .notifyError(Ci.nsIDOMGeoPositionError.POSITION_UNAVAILABLE); +} this.NSGetFactory = XPCOMUtils.generateNSGetFactory([WifiGeoPositionProvider]); diff --git a/dom/tests/mochitest/geolocation/geolocation_common.js b/dom/tests/mochitest/geolocation/geolocation_common.js index a45cbe437aa5..318ec2321c42 100644 --- a/dom/tests/mochitest/geolocation/geolocation_common.js +++ b/dom/tests/mochitest/geolocation/geolocation_common.js @@ -51,6 +51,11 @@ function delay_geolocationProvider(delay, callback) SpecialPowers.pushPrefEnv({"set": [["geo.wifi.uri", "http://mochi.test:8888/tests/dom/tests/mochitest/geolocation/network_geolocation.sjs?delay=" + delay]]}, callback); } +function send404_geolocationProvider(callback) +{ + SpecialPowers.pushPrefEnv({"set": [["geo.wifi.uri", "http://mochi.test:8888/tests/dom/tests/mochitest/geolocation/network_geolocation.sjs?action=send404"]]}, callback); +} + function check_geolocation(location) { ok(location, "Check to see if this location is non-null"); diff --git a/dom/tests/mochitest/geolocation/mochitest.ini b/dom/tests/mochitest/geolocation/mochitest.ini index c503a512b933..f8f19348fc2e 100644 --- a/dom/tests/mochitest/geolocation/mochitest.ini +++ b/dom/tests/mochitest/geolocation/mochitest.ini @@ -12,6 +12,7 @@ support-files = [test_cancelWatch.html] [test_clearWatch.html] [test_clearWatch_invalid.html] +[test_errorcheck.html] [test_geolocation_is_undefined_when_pref_is_off.html] [test_handlerSpinsEventLoop.html] [test_manyCurrentConcurrent.html] diff --git a/dom/tests/mochitest/geolocation/network_geolocation.sjs b/dom/tests/mochitest/geolocation/network_geolocation.sjs index 18f913a8c766..db07c95758dc 100644 --- a/dom/tests/mochitest/geolocation/network_geolocation.sjs +++ b/dom/tests/mochitest/geolocation/network_geolocation.sjs @@ -37,6 +37,7 @@ function handleRequest(request, response) var params = parseQueryString(request.queryString); if (params.action == "stop-responding") { + response.processAsync(); return; } @@ -64,6 +65,11 @@ function handleRequest(request, response) if ('delay' in params) { delay = params.delay; } + if (params.action === "send404") { + response.setStatusLine("1.0", 404, "Not Found"); + response.finish(); + return; + } timer = Components.classes["@mozilla.org/timer;1"].createInstance(Components.interfaces.nsITimer); timer.initWithCallback(function() { response.write(position); diff --git a/dom/tests/mochitest/geolocation/test_errorcheck.html b/dom/tests/mochitest/geolocation/test_errorcheck.html new file mode 100644 index 000000000000..a4e10d1e33bd --- /dev/null +++ b/dom/tests/mochitest/geolocation/test_errorcheck.html @@ -0,0 +1,50 @@ + + + + + Test for ErrorChecking + + + + + + +Mozilla Bug 684722 +

+ +
+
+
+ + +