From e2ab1a509763555bad2d3af02c21ee70d8be1a92 Mon Sep 17 00:00:00 2001 From: "Adam Roach [:abr]" Date: Mon, 18 Aug 2014 12:43:37 -0500 Subject: [PATCH] Bug 1055139 - Retrieve Simple Push Server URL from Loop Server r=mhammond,Standard8 --- .../components/loop/MozLoopPushHandler.jsm | 49 +++++++++++++++++-- .../components/loop/test/mochitest/head.js | 8 +++ .../test/xpcshell/test_looppush_initialize.js | 2 + .../test/xpcshell/test_loopservice_expiry.js | 2 + .../xpcshell/test_loopservice_initialize.js | 2 + .../test/xpcshell/test_loopservice_locales.js | 2 + .../xpcshell/test_loopservice_loop_prefs.js | 2 + 7 files changed, 64 insertions(+), 3 deletions(-) diff --git a/browser/components/loop/MozLoopPushHandler.jsm b/browser/components/loop/MozLoopPushHandler.jsm index 978aacc2986..378a630697f 100644 --- a/browser/components/loop/MozLoopPushHandler.jsm +++ b/browser/components/loop/MozLoopPushHandler.jsm @@ -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(); + } }, /** diff --git a/browser/components/loop/test/mochitest/head.js b/browser/components/loop/test/mochitest/head.js index 993753a5959..3582f385cae 100644 --- a/browser/components/loop/test/mochitest/head.js +++ b/browser/components/loop/test/mochitest/head.js @@ -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. diff --git a/browser/components/loop/test/xpcshell/test_looppush_initialize.js b/browser/components/loop/test/xpcshell/test_looppush_initialize.js index 179e8627364..31564ade00d 100644 --- a/browser/components/loop/test/xpcshell/test_looppush_initialize.js +++ b/browser/components/loop/test/xpcshell/test_looppush_initialize.js @@ -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 diff --git a/browser/components/loop/test/xpcshell/test_loopservice_expiry.js b/browser/components/loop/test/xpcshell/test_loopservice_expiry.js index 71fecd996a4..03899399439 100644 --- a/browser/components/loop/test/xpcshell/test_loopservice_expiry.js +++ b/browser/components/loop/test/xpcshell/test_loopservice_expiry.js @@ -7,6 +7,8 @@ function expiryTimePref() { function run_test() { + setupFakeLoopServer(); + Services.prefs.setIntPref("loop.urlsExpiryTimeSeconds", 0); MozLoopService.noteCallUrlExpiry(1000); diff --git a/browser/components/loop/test/xpcshell/test_loopservice_initialize.js b/browser/components/loop/test/xpcshell/test_loopservice_initialize.js index 8df92d1ba47..a30f88ff5c8 100644 --- a/browser/components/loop/test/xpcshell/test_loopservice_initialize.js +++ b/browser/components/loop/test/xpcshell/test_loopservice_initialize.js @@ -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() { diff --git a/browser/components/loop/test/xpcshell/test_loopservice_locales.js b/browser/components/loop/test/xpcshell/test_loopservice_locales.js index 85491a9a928..7aa8156c5b8 100644 --- a/browser/components/loop/test/xpcshell/test_loopservice_locales.js +++ b/browser/components/loop/test/xpcshell/test_loopservice_locales.js @@ -22,6 +22,8 @@ function test_getStrings() { function run_test() { + setupFakeLoopServer(); + test_locale(); test_getStrings(); } diff --git a/browser/components/loop/test/xpcshell/test_loopservice_loop_prefs.js b/browser/components/loop/test/xpcshell/test_loopservice_loop_prefs.js index c9af9a6a4dc..3dd09ce5612 100644 --- a/browser/components/loop/test/xpcshell/test_loopservice_loop_prefs.js +++ b/browser/components/loop/test/xpcshell/test_loopservice_loop_prefs.js @@ -89,6 +89,8 @@ function test_getLoopBoolPref_not_found() function run_test() { + setupFakeLoopServer(); + test_getLoopCharPref(); test_getLoopCharPref_not_found(); test_getLoopCharPref_non_coercible_type();