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.
This commit is contained in:
Gregory Szorc 2012-08-29 14:43:40 -07:00
parent bb5d240869
commit 5628a0cd3b
4 changed files with 106 additions and 105 deletions

View File

@ -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() {},
};

View File

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

View File

@ -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",
const EXPORTED_SYMBOLS = [
"ErrorHandler",
"SendCredentialsController"];
"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() {}
};

View File

@ -1,8 +1,9 @@
/* Any copyright is dedicated to the Public Domain.
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");