Move remote init code ('fullUpload') into RemoteStore; make RemoteStore hold an Engine object (tightly coupling them); make the server prefix and identity properties of Engine public

This commit is contained in:
Dan Mills 2008-06-14 17:07:06 +09:00
Родитель 0c475dfe37
Коммит 25f6da01d1
2 изменённых файлов: 75 добавлений и 112 удалений

Просмотреть файл

@ -105,7 +105,7 @@ Engine.prototype = {
get _remote() {
if (!this.__remote)
this.__remote = new RemoteStore(this.serverPrefix, 'Engine:' + this.name);
this.__remote = new RemoteStore(this);
return this.__remote;
},
@ -161,7 +161,7 @@ Engine.prototype = {
this.__snapshot = value;
},
get _pbeId() {
get pbeId() {
let id = ID.get('Engine:PBE:' + this.name);
if (!id)
id = ID.get('Engine:PBE:default');
@ -170,15 +170,15 @@ Engine.prototype = {
return id;
},
get _engineId() {
get engineId() {
let id = ID.get('Engine:' + this.name);
if (!id ||
id.username != this._pbeId.username || id.realm != this._pbeId.realm) {
id.username != this.pbeId.username || id.realm != this.pbeId.realm) {
let password = null;
if (id)
password = id.password;
id = new Identity(this._pbeId.realm + ' - ' + this.logName,
this._pbeId.username, password);
id = new Identity(this.pbeId.realm + ' - ' + this.logName,
this.pbeId.username, password);
ID.set('Engine:' + this.name, id);
}
return id;
@ -194,15 +194,10 @@ Engine.prototype = {
_getSymKey: function Engine__getSymKey() {
let self = yield;
if ("none" == Utils.prefs.getCharPref("encryption"))
return;
this._remote.keys.getKey(self.cb, this._pbeId);
let symkey = yield;
this._engineId.setTempPassword(symkey);
self.done();
let symkey = yield this._remote.keys.getKey(self.cb, this.pbeId);
this.engineId.setTempPassword(symkey);
},
_serializeCommands: function Engine__serializeCommands(commands) {
@ -392,13 +387,8 @@ Engine.prototype = {
this._snapshot.data = newSnapshot;
this._snapshot.version = ++server.maxVersion;
/*
if (server.formatVersion != ENGINE_STORAGE_FORMAT_VERSION) {
this._fullUpload.async(this, self.cb);
let status = yield;
if (!status)
this._log.error("Could not upload files to server"); // eep?
*/
if (server.formatVersion != ENGINE_STORAGE_FORMAT_VERSION)
yield this._remote.initialize(self.cb, this._snapshot);
this._remote.appendDelta(self.cb, serverDelta);
yield;
@ -421,6 +411,15 @@ Engine.prototype = {
self.done(true);
},
_initialUpload: function Engine__initialUpload() {
this._log.info("Initial upload to server");
this._snapshot.data = this._store.wrap();
this._snapshot.version = 0;
this._snapshot.GUID = null; // in case there are other snapshots out there
yield this._remote.initialize(self.cb, this._snapshot);
this._snapshot.save();
},
/* Get the deltas/combined updates from the server
* Returns:
* status:
@ -445,7 +444,7 @@ Engine.prototype = {
* the relevant deltas (from our snapshot version to current),
* combined into a single set.
*/
_getServerData: function BmkEngine__getServerData() {
_getServerData: function Engine__getServerData() {
let self = yield;
let status;
@ -456,20 +455,7 @@ Engine.prototype = {
this._log.info("Got status file from server");
} catch (e if e.message.status == 404) {
this._log.info("Server has no status file, Initial upload to server");
this._snapshot.data = this._store.wrap();
this._snapshot.version = 0;
this._snapshot.GUID = null; // in case there are other snapshots out there
this._fullUpload.async(this, self.cb);
let uploadStatus = yield;
if (!uploadStatus)
throw "Initial upload failed";
this._log.info("Initial upload to server successful");
this._snapshot.save();
this._initialUpload.async(this, self.cb);
self.done({status: 0,
formatVersion: ENGINE_STORAGE_FORMAT_VERSION,
maxVersion: this._snapshot.version,
@ -583,55 +569,6 @@ Engine.prototype = {
self.done(ret);
},
_fullUpload: function Engine__fullUpload() {
let self = yield;
let ret = false;
Crypto.PBEkeygen.async(Crypto, self.cb);
let symkey = yield;
this._engineId.setTempPassword(symkey);
if (!this._engineId.password)
throw "Could not generate a symmetric encryption key";
let enckey = this._engineId.password;
if ("none" != Utils.prefs.getCharPref("encryption")) {
Crypto.RSAencrypt.async(Crypto, self.cb,
this._engineId.password, this._pbeId);
enckey = yield;
}
if (!enckey)
throw "Could not encrypt symmetric encryption key";
let keys = {ring: {}};
keys.ring[this._engineId.username] = enckey;
this._remote.keys.put(self.cb, keys);
yield;
this._remote.snapshot.put(self.cb, this._snapshot.wrap());
yield;
this._remote.deltas.put(self.cb, []);
yield;
let c = 0;
for (GUID in this._snapshot.data)
c++;
this._remote.status.put(self.cb,
{GUID: this._snapshot.GUID,
formatVersion: ENGINE_STORAGE_FORMAT_VERSION,
snapVersion: this._snapshot.version,
maxVersion: this._snapshot.version,
snapEncryption: Crypto.defaultAlgorithm,
deltasEncryption: "none",
itemCount: c});
yield;
this._log.info("Full upload to server successful");
ret = true;
self.done(ret);
},
_share: function Engine__share(guid, username) {
/* This should be overridden by the engine subclass for each datatype.
Implementation should share the data node identified by guid,

Просмотреть файл

@ -282,7 +282,7 @@ CryptoFilter.prototype = {
beforePUT: function CryptoFilter_beforePUT(data) {
let self = yield;
this._log.debug("Encrypting data");
Crypto.PBEencrypt.async(Crypto, self.cb, data, ID.get(this._remote.cryptoId));
Crypto.PBEencrypt.async(Crypto, self.cb, data, this._remote.engineId);
let ret = yield;
self.done(ret);
},
@ -293,7 +293,7 @@ CryptoFilter.prototype = {
if (!this._remote.status.data)
throw "Remote status must be initialized before crypto filter can be used"
let alg = this._remote.status.data[this._algProp];
Crypto.PBEdecrypt.async(Crypto, self.cb, data, ID.get(this._remote.cryptoId));
Crypto.PBEdecrypt.async(Crypto, self.cb, data, this._remote.engineId);
let ret = yield;
self.done(ret);
}
@ -366,26 +366,14 @@ Deltas.prototype = {
}
};
function RemoteStore(serverPrefix, cryptoId) {
this._prefix = serverPrefix;
this._cryptoId = cryptoId;
function RemoteStore(engine) {
this._engine = engine;
this._log = Log4Moz.Service.getLogger("Service.RemoteStore");
}
RemoteStore.prototype = {
get serverPrefix() this._prefix,
set serverPrefix(value) {
this._prefix = value;
this.status.serverPrefix = value;
this.keys.serverPrefix = value;
this.snapshot.serverPrefix = value;
this.deltas.serverPrefix = value;
},
get cryptoId() this._cryptoId,
set cryptoId(value) {
this.__cryptoId = value;
// FIXME: do we need to reset anything here?
},
get serverPrefix() this._engine.serverPrefix,
get engineId() this._engine.engineId,
get pbeId() this._engine.pbeId,
get status() {
let status = new Status(this);
@ -411,15 +399,11 @@ RemoteStore.prototype = {
return deltas;
},
_initSession: function RStore__initSession(serverPrefix, cryptoId) {
_initSession: function RStore__initSession() {
let self = yield;
if (serverPrefix)
this.serverPrefix = serverPrefix;
if (cryptoId)
this.cryptoId = cryptoId;
if (!this.serverPrefix || !this.cryptoId)
throw "RemoteStore: cannot initialize without a server prefix and crypto ID";
if (!this.serverPrefix || !this.engineId)
throw "Cannot initialize RemoteStore: engine has no server prefix or crypto ID";
this.status.data = null;
this.keys.data = null;
@ -438,8 +422,8 @@ RemoteStore.prototype = {
this._inited = true;
},
initSession: function RStore_initSession(onComplete, serverPrefix, cryptoId) {
this._initSession.async(this, onComplete, serverPrefix, cryptoId);
initSession: function RStore_initSession(onComplete) {
this._initSession.async(this, onComplete);
},
closeSession: function RStore_closeSession() {
@ -450,6 +434,48 @@ RemoteStore.prototype = {
this.deltas.data = null;
},
_initialize: function RStore__initialize(snapshot) {
let self = yield;
let symkey;
if ("none" != Utils.prefs.getCharPref("encryption")) {
symkey = yield Crypto.PBEkeygen.async(Crypto, self.cb);
if (!symkey)
throw "Could not generate a symmetric encryption key";
this.engineId.setTempPassword(symkey);
symkey = yield Crypto.RSAencrypt.async(Crypto, self.cb,
this.engineId.password,
this.pbeId);
if (!symkey)
throw "Could not encrypt symmetric encryption key";
}
let keys = {ring: {}};
keys.ring[this.engineId.username] = symkey;
yield this._remote.keys.put(self.cb, keys);
this.snapshot.put(self.cb, snapshot.data);
this.deltas.put(self.cb, []);
let c = 0;
for (GUID in snapshot.data)
c++;
yield this.status.put(self.cb,
{GUID: snapshot.GUID,
formatVersion: ENGINE_STORAGE_FORMAT_VERSION,
snapVersion: snapshot.version,
maxVersion: snapshot.version,
snapEncryption: Crypto.defaultAlgorithm,
deltasEncryption: Crypto.defaultAlgorithm,
itemCount: c});
this._log.info("Full upload to server successful");
},
initialize: function RStore_initialize(onComplete, snapshot) {
this._initialize.async(this, onComplete, snapshot);
},
_appendDelta: function RStore__appendDelta(delta) {
let self = yield;
if (this.deltas.data == null) {