From 5628a0cd3b8224e95893834c7a9df7b023578ef2 Mon Sep 17 00:00:00 2001 From: Gregory Szorc Date: Wed, 29 Aug 2012 14:43:40 -0700 Subject: [PATCH] Bug 785225 - Park 6: Move StorageCredentialsController to jpakeclient.js; r=rnewman I'm not sure why it was in policies.js, as it has everything to do with J-PAKE exchange. --- services/sync/modules/jpakeclient.js | 103 +++++++++++++++++- services/sync/modules/main.js | 4 +- services/sync/modules/policies.js | 99 +---------------- .../unit/test_sendcredentials_controller.js | 5 +- 4 files changed, 106 insertions(+), 105 deletions(-) diff --git a/services/sync/modules/jpakeclient.js b/services/sync/modules/jpakeclient.js index c0698747734..86366f182f6 100644 --- a/services/sync/modules/jpakeclient.js +++ b/services/sync/modules/jpakeclient.js @@ -2,18 +2,17 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -const Cc = Components.classes; -const Ci = Components.interfaces; -const Cr = Components.results; -const Cu = Components.utils; +const EXPORTED_SYMBOLS = ["JPAKEClient", "SendCredentialsController"]; + +const {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components; Cu.import("resource://services-common/log4moz.js"); Cu.import("resource://services-common/rest.js"); Cu.import("resource://services-sync/constants.js"); +Cu.import("resource://services-sync/main.js"); +Cu.import("resource://services-sync/policies.js"); Cu.import("resource://services-sync/util.js"); -const EXPORTED_SYMBOLS = ["JPAKEClient"]; - const REQUEST_TIMEOUT = 60; // 1 minute const KEYEXCHANGE_VERSION = 3; @@ -681,3 +680,95 @@ JPAKEClient.prototype = { } }; + + +/** + * Send credentials over an active J-PAKE channel. + * + * This object is designed to take over as the JPAKEClient controller, + * presumably replacing one that is UI-based which would either cause + * DOM objects to leak or the JPAKEClient to be GC'ed when the DOM + * context disappears. This object stays alive for the duration of the + * transfer by being strong-ref'ed as an nsIObserver. + * + * Credentials are sent after the first sync has been completed + * (successfully or not.) + * + * Usage: + * + * jpakeclient.controller = new SendCredentialsController(jpakeclient); + * + */ +function SendCredentialsController(jpakeclient) { + this._log = Log4Moz.repository.getLogger("Sync.SendCredentialsController"); + this._log.level = Log4Moz.Level[Svc.Prefs.get("log.logger.service.main")]; + + this._log.trace("Loading."); + this.jpakeclient = jpakeclient; + + // Register ourselves as observers the first Sync finishing (either + // successfully or unsuccessfully, we don't care) or for removing + // this device's sync configuration, in case that happens while we + // haven't finished the first sync yet. + Services.obs.addObserver(this, "weave:service:sync:finish", false); + Services.obs.addObserver(this, "weave:service:sync:error", false); + Services.obs.addObserver(this, "weave:service:start-over", false); +} +SendCredentialsController.prototype = { + + unload: function unload() { + this._log.trace("Unloading."); + try { + Services.obs.removeObserver(this, "weave:service:sync:finish"); + Services.obs.removeObserver(this, "weave:service:sync:error"); + Services.obs.removeObserver(this, "weave:service:start-over"); + } catch (ex) { + // Ignore. + } + }, + + observe: function observe(subject, topic, data) { + switch (topic) { + case "weave:service:sync:finish": + case "weave:service:sync:error": + Utils.nextTick(this.sendCredentials, this); + break; + case "weave:service:start-over": + // This will call onAbort which will call unload(). + this.jpakeclient.abort(); + break; + } + }, + + sendCredentials: function sendCredentials() { + this._log.trace("Sending credentials."); + let credentials = {account: Weave.Identity.account, + password: Weave.Identity.basicPassword, + synckey: Weave.Identity.syncKey, + serverURL: Weave.Service.serverURL}; + this.jpakeclient.sendAndComplete(credentials); + }, + + // JPAKEClient controller API + + onComplete: function onComplete() { + this._log.debug("Exchange was completed successfully!"); + this.unload(); + + // Schedule a Sync for soonish to fetch the data uploaded by the + // device with which we just paired. + SyncScheduler.scheduleNextSync(SyncScheduler.activeInterval); + }, + + onAbort: function onAbort(error) { + // It doesn't really matter why we aborted, but the channel is closed + // for sure, so we won't be able to do anything with it. + this._log.debug("Exchange was aborted with error: " + error); + this.unload(); + }, + + // Irrelevant methods for this controller: + displayPIN: function displayPIN() {}, + onPairingStart: function onPairingStart() {}, + onPaired: function onPaired() {}, +}; diff --git a/services/sync/modules/main.js b/services/sync/modules/main.js index 467435afb9f..df4fd498141 100644 --- a/services/sync/modules/main.js +++ b/services/sync/modules/main.js @@ -8,9 +8,9 @@ let Weave = {}; Components.utils.import("resource://services-sync/constants.js", Weave); let lazies = { "identity.js": ["Identity"], - "jpakeclient.js": ["JPAKEClient"], + "jpakeclient.js": ["JPAKEClient", "SendCredentialsController"], "notifications.js": ["Notifications", "Notification", "NotificationButton"], - "policies.js": ["SyncScheduler", "SendCredentialsController"], + "policies.js": ["SyncScheduler"], "service.js": ["Service"], "status.js": ["Status"], "util.js": ['Utils', 'Svc'] diff --git a/services/sync/modules/policies.js b/services/sync/modules/policies.js index d572196ca21..e04e52a39d0 100644 --- a/services/sync/modules/policies.js +++ b/services/sync/modules/policies.js @@ -2,9 +2,10 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -const EXPORTED_SYMBOLS = ["SyncScheduler", - "ErrorHandler", - "SendCredentialsController"]; +const EXPORTED_SYMBOLS = [ + "ErrorHandler", + "SyncScheduler", +]; const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; @@ -816,95 +817,3 @@ ErrorHandler.prototype = { } }, }; - - -/** - * Send credentials over an active J-PAKE channel. - * - * This object is designed to take over as the JPAKEClient controller, - * presumably replacing one that is UI-based which would either cause - * DOM objects to leak or the JPAKEClient to be GC'ed when the DOM - * context disappears. This object stays alive for the duration of the - * transfer by being strong-ref'ed as an nsIObserver. - * - * Credentials are sent after the first sync has been completed - * (successfully or not.) - * - * Usage: - * - * jpakeclient.controller = new SendCredentialsController(jpakeclient); - * - */ -function SendCredentialsController(jpakeclient) { - this._log = Log4Moz.repository.getLogger("Sync.SendCredentialsController"); - this._log.level = Log4Moz.Level[Svc.Prefs.get("log.logger.service.main")]; - - this._log.trace("Loading."); - this.jpakeclient = jpakeclient; - - // Register ourselves as observers the first Sync finishing (either - // successfully or unsuccessfully, we don't care) or for removing - // this device's sync configuration, in case that happens while we - // haven't finished the first sync yet. - Services.obs.addObserver(this, "weave:service:sync:finish", false); - Services.obs.addObserver(this, "weave:service:sync:error", false); - Services.obs.addObserver(this, "weave:service:start-over", false); -} -SendCredentialsController.prototype = { - - unload: function unload() { - this._log.trace("Unloading."); - try { - Services.obs.removeObserver(this, "weave:service:sync:finish"); - Services.obs.removeObserver(this, "weave:service:sync:error"); - Services.obs.removeObserver(this, "weave:service:start-over"); - } catch (ex) { - // Ignore. - } - }, - - observe: function observe(subject, topic, data) { - switch (topic) { - case "weave:service:sync:finish": - case "weave:service:sync:error": - Utils.nextTick(this.sendCredentials, this); - break; - case "weave:service:start-over": - // This will call onAbort which will call unload(). - this.jpakeclient.abort(); - break; - } - }, - - sendCredentials: function sendCredentials() { - this._log.trace("Sending credentials."); - let credentials = {account: Weave.Identity.account, - password: Weave.Identity.basicPassword, - synckey: Weave.Identity.syncKey, - serverURL: Weave.Service.serverURL}; - this.jpakeclient.sendAndComplete(credentials); - }, - - // JPAKEClient controller API - - onComplete: function onComplete() { - this._log.debug("Exchange was completed successfully!"); - this.unload(); - - // Schedule a Sync for soonish to fetch the data uploaded by the - // device with which we just paired. - SyncScheduler.scheduleNextSync(SyncScheduler.activeInterval); - }, - - onAbort: function onAbort(error) { - // It doesn't really matter why we aborted, but the channel is closed - // for sure, so we won't be able to do anything with it. - this._log.debug("Exchange was aborted with error: " + error); - this.unload(); - }, - - // Irrelevant methods for this controller: - displayPIN: function displayPIN() {}, - onPairingStart: function onPairingStart() {}, - onPaired: function onPaired() {} -}; diff --git a/services/sync/tests/unit/test_sendcredentials_controller.js b/services/sync/tests/unit/test_sendcredentials_controller.js index c0f3abd4b50..5508d4939db 100644 --- a/services/sync/tests/unit/test_sendcredentials_controller.js +++ b/services/sync/tests/unit/test_sendcredentials_controller.js @@ -1,8 +1,9 @@ /* Any copyright is dedicated to the Public Domain. - http://creativecommons.org/publicdomain/zero/1.0/ */ + http://creativecommons.org/publicdomain/zero/1.0/ */ -Cu.import("resource://services-sync/policies.js"); Cu.import("resource://services-sync/constants.js"); +Cu.import("resource://services-sync/policies.js"); +Cu.import("resource://services-sync/jpakeclient.js"); Cu.import("resource://services-sync/service.js"); Cu.import("resource://services-sync/util.js");