Bug 1055139 - Retrieve Simple Push Server URL from Loop Server r=mhammond,Standard8

This commit is contained in:
Adam Roach [:abr] 2014-08-18 12:43:37 -05:00
parent 6c0351ccd7
commit e2ab1a5097
7 changed files with 64 additions and 3 deletions

View File

@ -21,7 +21,7 @@ XPCOMUtils.defineLazyModuleGetter(this, "console",
*/
let MozLoopPushHandler = {
// This is the uri of the push server.
pushServerUri: Services.prefs.getCharPref("services.push.serverURL"),
pushServerUri: undefined,
// This is the channel id we're using for notifications
channelID: "8b1081ce-9b35-42b5-b8f5-3ff8cb813a50",
// This is the UserAgent UUID assigned by the PushServer
@ -211,8 +211,51 @@ let MozLoopPushHandler = {
}
this._websocket.protocol = "push-notification";
let uri = Services.io.newURI(this.pushServerUri, null, null);
this._websocket.asyncOpen(uri, this.pushServerUri, this, null);
let performOpen = () => {
let uri = Services.io.newURI(this.pushServerUri, null, null);
this._websocket.asyncOpen(uri, this.pushServerUri, this, null);
}
let pushServerURLFetchError = () => {
console.warn("MozLoopPushHandler - Could not retrieve push server URL from Loop server; using default");
this.pushServerUri = Services.prefs.getCharPref("services.push.serverURL");
performOpen();
}
if (!this.pushServerUri) {
// Get push server to use from the Loop server
let pushUrlEndpoint = Services.prefs.getCharPref("loop.server") + "/push-server-config";
let req = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].
createInstance(Ci.nsIXMLHttpRequest);
req.open("GET", pushUrlEndpoint);
req.onload = () => {
if (req.status >= 200 && req.status < 300) {
let pushServerConfig;
try {
pushServerConfig = JSON.parse(req.responseText);
} catch (e) {
console.warn("MozLoopPushHandler - Error parsing JSON response for push server URL");
pushServerURLFetchError();
}
if (pushServerConfig.pushServerURI) {
this.pushServerUri = pushServerConfig.pushServerURI;
performOpen();
} else {
console.warn("MozLoopPushHandler - push server URL config lacks pushServerURI parameter");
pushServerURLFetchError();
}
} else {
console.warn("MozLoopPushHandler - push server URL retrieve error: " + req.status);
pushServerURLFetchError();
}
};
req.onerror = pushServerURLFetchError;
req.send();
} else {
// this.pushServerUri already set -- just open the channel
performOpen();
}
},
/**

View File

@ -57,9 +57,17 @@ function loadLoopPanel() {
Services.prefs.setCharPref("services.push.serverURL", "ws://localhost/");
Services.prefs.setCharPref("loop.server", "http://localhost/");
// Turn off the network for loop tests, so that we don't
// try to access the remote servers. If we want to turn this
// back on in future, be careful to check for intermittent
// failures.
let wasOffline = Services.io.offline;
Services.io.offline = true;
registerCleanupFunction(function() {
Services.prefs.clearUserPref("services.push.serverURL");
Services.prefs.clearUserPref("loop.server");
Services.io.offline = wasOffline;
});
// Turn off animations to make tests quicker.

View File

@ -54,6 +54,8 @@
});
function run_test() {
setupFakeLoopServer();
Services.prefs.setCharPref("services.push.serverURL", kServerPushUrl);
Services.prefs.setIntPref("loop.retry_delay.start", 10); // 10 ms
Services.prefs.setIntPref("loop.retry_delay.limit", 20); // 20 ms

View File

@ -7,6 +7,8 @@ function expiryTimePref() {
function run_test()
{
setupFakeLoopServer();
Services.prefs.setIntPref("loop.urlsExpiryTimeSeconds", 0);
MozLoopService.noteCallUrlExpiry(1000);

View File

@ -50,6 +50,8 @@ add_task(function test_initialize_starts_timer() {
function run_test()
{
setupFakeLoopServer();
// Override MozLoopService's initializeTimer, so that we can verify the timeout is called
// correctly.
MozLoopService.initializeTimerFunc = function() {

View File

@ -22,6 +22,8 @@ function test_getStrings() {
function run_test()
{
setupFakeLoopServer();
test_locale();
test_getStrings();
}

View File

@ -89,6 +89,8 @@ function test_getLoopBoolPref_not_found()
function run_test()
{
setupFakeLoopServer();
test_getLoopCharPref();
test_getLoopCharPref_not_found();
test_getLoopCharPref_non_coercible_type();