Bug 684722 - Invoke geolocation error handlers when xhr fails. r=jdm

This commit is contained in:
Bharath Thiruveedula 2013-10-31 11:26:41 -04:00
parent 6c32b05cf1
commit afdb93168c
7 changed files with 85 additions and 7 deletions

View File

@ -80,6 +80,7 @@ class nsGeolocationRequest
void SendLocation(nsIDOMGeoPosition* location);
bool WantsHighAccuracy() {return !mShutdown && mOptions && mOptions->mEnableHighAccuracy;}
void SetTimeoutTimer();
void NotifyErrorAndShutdown(uint16_t);
nsIPrincipal* GetPrincipal();
~nsGeolocationRequest();
@ -350,6 +351,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");
@ -358,13 +366,11 @@ nsGeolocationRequest::Notify(nsITimer* aTimer)
mLocator->RemoveRequest(this);
}
NotifyError(nsIDOMGeoPositionError::TIMEOUT);
NotifyError(aErrorCode);
if (!mShutdown) {
SetTimeoutTimer();
}
return NS_OK;
}
NS_IMETHODIMP
@ -1134,13 +1140,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;

View File

@ -175,22 +175,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;
}
@ -223,4 +231,8 @@ WifiGeoPositionProvider.prototype = {
},
};
function triggerError() {
Cc["@mozilla.org/geolocation/service;1"].getService(Ci.nsIGeolocationUpdate)
.notifyError(Ci.nsIDOMGeoPositionError.POSITION_UNAVAILABLE);
}
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([WifiGeoPositionProvider]);

View File

@ -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");

View File

@ -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]

View File

@ -37,6 +37,7 @@ function handleRequest(request, response)
var params = parseQueryString(request.queryString);
if (params.action == "stop-responding") {
response.processAsync();
return;
}
@ -64,6 +65,10 @@ function handleRequest(request, response)
if ('delay' in params) {
delay = params.delay;
}
if (params.action === "send404") {
response.setStatusLine("1.0", 404, "Not Found");
position = '';
}
timer = Components.classes["@mozilla.org/timer;1"].createInstance(Components.interfaces.nsITimer);
timer.initWithCallback(function() {
response.write(position);

View File

@ -0,0 +1,48 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=684722
-->
<head>
<title>Test for ErrorChecking </title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="geolocation_common.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=684722">Mozilla Bug 684722</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
SimpleTest.waitForExplicitFinish();
resume_geolocationProvider(function() {
force_prompt(true, test1);
});
function test1() {
send404_geolocationProvider(test2);
}
function errorCallback(error) {
is(error.code,
SpecialPowers.Ci.nsIDOMGeoPositionError.POSITION_UNAVAILABLE, "Geolocation error handler fired");
SimpleTest.finish();
}
function successCallback(position) {
}
function test2() {
navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
}
</script>
</pre>
</body>
</html>

View File

@ -184,6 +184,7 @@
"dom/tests/mochitest/geolocation/test_allowWatch.html": "TIMED_OUT",
"dom/tests/mochitest/geolocation/test_cachedPosition.html": "TIMED_OUT",
"dom/tests/mochitest/geolocation/test_clearWatch.html": "TIMED_OUT",
"dom/tests/mochitest/geolocation/test_errorcheck.html": "TIMED_OUT",
"dom/tests/mochitest/geolocation/test_handlerSpinsEventLoop.html": "Don't run modal tests on Android",
"dom/tests/mochitest/geolocation/test_manyCurrentConcurrent.html": "TIMED_OUT",
"dom/tests/mochitest/geolocation/test_manyCurrentSerial.html": "TIMED_OUT",