some more cleanup/refactoring; add history engine/core/store (history sync\!)

This commit is contained in:
Dan Mills 2007-12-14 18:07:25 -08:00
Родитель 8c9defe994
Коммит dee6b23c4d
11 изменённых файлов: 673 добавлений и 445 удалений

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

@ -34,18 +34,12 @@
* *
* ***** END LICENSE BLOCK ***** */ * ***** END LICENSE BLOCK ***** */
const EXPORTED_SYMBOLS = ['Cc', 'Ci', 'Cr', 'Cu', const EXPORTED_SYMBOLS = ['MODE_RDONLY', 'MODE_WRONLY',
'MODE_RDONLY', 'MODE_WRONLY',
'MODE_CREATE', 'MODE_APPEND', 'MODE_TRUNCATE', 'MODE_CREATE', 'MODE_APPEND', 'MODE_TRUNCATE',
'PERMS_FILE', 'PERMS_DIRECTORY', 'PERMS_FILE', 'PERMS_DIRECTORY',
'STORAGE_FORMAT_VERSION', 'STORAGE_FORMAT_VERSION',
'ONE_BYTE', 'ONE_KILOBYTE', 'ONE_MEGABYTE']; 'ONE_BYTE', 'ONE_KILOBYTE', 'ONE_MEGABYTE'];
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
const Cu = Components.utils;
const MODE_RDONLY = 0x01; const MODE_RDONLY = 0x01;
const MODE_WRONLY = 0x02; const MODE_WRONLY = 0x02;
const MODE_CREATE = 0x08; const MODE_CREATE = 0x08;

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

@ -41,12 +41,11 @@ const Ci = Components.interfaces;
const Cr = Components.results; const Cr = Components.results;
const Cu = Components.utils; const Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://weave/log4moz.js"); Cu.import("resource://weave/log4moz.js");
Cu.import("resource://weave/constants.js"); Cu.import("resource://weave/constants.js");
Cu.import("resource://weave/util.js"); Cu.import("resource://weave/util.js");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
function WeaveCrypto() { function WeaveCrypto() {
this._init(); this._init();
} }

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

@ -41,11 +41,10 @@ const Ci = Components.interfaces;
const Cr = Components.results; const Cr = Components.results;
const Cu = Components.utils; const Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://weave/log4moz.js"); Cu.import("resource://weave/log4moz.js");
Cu.import("resource://weave/util.js"); Cu.import("resource://weave/util.js");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Function.prototype.async = generatorAsync; Function.prototype.async = generatorAsync;
/* /*
@ -123,7 +122,7 @@ DAVCollection.prototype = {
this._log.warn("_makeRequest: got status " + ret.status); this._log.warn("_makeRequest: got status " + ret.status);
} catch (e) { } catch (e) {
this._log.error("Exception caught: " + e.message); this._log.error("Exception caught: " + (e.message? e.message : e));
} finally { } finally {
generatorDone(this, self, onComplete, ret); generatorDone(this, self, onComplete, ret);
@ -139,6 +138,55 @@ DAVCollection.prototype = {
"<" + this._baseURL + "> (<" + this._token + ">)" : ''}; "<" + this._baseURL + "> (<" + this._token + ">)" : ''};
}, },
// mkdir -p
_mkcol: function DC__mkcol(path, onComplete) {
let [self, cont] = yield;
let ret;
try {
let components = path.split('/');
let path2 = '';
for (let i = 0; i < components.length; i++) {
// trailing slashes will cause an empty path component at the end
if (components[i] == '')
break;
path2 = path2 + components[i];
// check if it exists first
this._makeRequest.async(this, cont, "GET", path2 + "/", this._defaultHeaders);
ret = yield;
if (!(ret.status == 404 || ret.status == 500)) { // FIXME: 500 is a services.m.c oddity
this._log.debug("Skipping creation of path " + path2 +
" (got status " + ret.status + ")");
} else {
this._log.debug("Creating path: " + path2);
let gen = this._makeRequest.async(this, cont, "MKCOL", path2,
this._defaultHeaders);
ret = yield;
if (ret.status != 201) {
this._log.debug(ret.responseText);
throw 'request failed: ' + ret.status;
}
}
// add slash *after* the request, trailing slashes cause a 412!
path2 = path2 + "/";
}
} catch (e) {
this._log.error("Exception caught: " + (e.message? e.message : e));
} finally {
generatorDone(this, self, onComplete, ret);
yield; // onComplete is responsible for closing the generator
}
this._log.warn("generator not properly closed");
},
GET: function DC_GET(path, onComplete) { GET: function DC_GET(path, onComplete) {
return this._makeRequest.async(this, onComplete, "GET", path, return this._makeRequest.async(this, onComplete, "GET", path,
this._defaultHeaders); this._defaultHeaders);
@ -154,6 +202,10 @@ DAVCollection.prototype = {
this._defaultHeaders); this._defaultHeaders);
}, },
MKCOL: function DC_MKCOL(path, onComplete) {
return this._mkcol.async(this, path, onComplete);
},
PROPFIND: function DC_PROPFIND(path, data, onComplete) { PROPFIND: function DC_PROPFIND(path, data, onComplete) {
let headers = {'Content-type': 'text/xml; charset="utf-8"', let headers = {'Content-type': 'text/xml; charset="utf-8"',
'Depth': '0'}; 'Depth': '0'};
@ -202,7 +254,7 @@ DAVCollection.prototype = {
this._loggedIn = true; this._loggedIn = true;
} catch (e) { } catch (e) {
this._log.error("Exception caught: " + e.message); this._log.error("Exception caught: " + (e.message? e.message : e));
} finally { } finally {
if (this._loggedIn) if (this._loggedIn)
@ -244,7 +296,7 @@ DAVCollection.prototype = {
ret = token.textContent; ret = token.textContent;
} catch (e) { } catch (e) {
this._log.error("Exception caught: " + e.message); this._log.error("Exception caught: " + (e.message? e.message : e));
} finally { } finally {
if (ret) if (ret)
@ -286,7 +338,7 @@ DAVCollection.prototype = {
this._token = token.textContent; this._token = token.textContent;
} catch (e){ } catch (e){
this._log.error("Exception caught: " + e.message); this._log.error("Exception caught: " + (e.message? e.message : e));
} finally { } finally {
if (this._token) if (this._token)
@ -318,7 +370,7 @@ DAVCollection.prototype = {
this._token = null; this._token = null;
} catch (e){ } catch (e){
this._log.error("Exception caught: " + e.message); this._log.error("Exception caught: " + (e.message? e.message : e));
} finally { } finally {
if (this._token) { if (this._token) {
@ -353,7 +405,7 @@ DAVCollection.prototype = {
unlocked = yield; unlocked = yield;
} catch (e){ } catch (e){
this._log.error("Exception caught: " + e.message); this._log.error("Exception caught: " + (e.message? e.message : e));
} finally { } finally {
if (unlocked) if (unlocked)
@ -380,7 +432,7 @@ DAVCollection.prototype = {
} }
} catch (e){ } catch (e){
this._log.error("Exception caught: " + e.message); this._log.error("Exception caught: " + (e.message? e.message : e));
} finally { } finally {
generatorDone(this, self, onComplete, stolen); generatorDone(this, self, onComplete, stolen);

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

@ -34,13 +34,14 @@
* *
* ***** END LICENSE BLOCK ***** */ * ***** END LICENSE BLOCK ***** */
const EXPORTED_SYMBOLS = ['BookmarksEngine']; const EXPORTED_SYMBOLS = ['Engine', 'BookmarksEngine', 'HistoryEngine'];
const Cc = Components.classes; const Cc = Components.classes;
const Ci = Components.interfaces; const Ci = Components.interfaces;
const Cr = Components.results; const Cr = Components.results;
const Cu = Components.utils; const Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://weave/log4moz.js"); Cu.import("resource://weave/log4moz.js");
Cu.import("resource://weave/constants.js"); Cu.import("resource://weave/constants.js");
Cu.import("resource://weave/util.js"); Cu.import("resource://weave/util.js");
@ -51,11 +52,24 @@ Cu.import("resource://weave/syncCores.js");
Function.prototype.async = generatorAsync; Function.prototype.async = generatorAsync;
let Crypto = new WeaveCrypto(); let Crypto = new WeaveCrypto();
function BookmarksEngine(davCollection, cryptoId) { function Engine(davCollection, cryptoId) {
this._init(davCollection, cryptoId); //this._init(davCollection, cryptoId);
} }
BookmarksEngine.prototype = { Engine.prototype = {
_logName: "BmkEngine", // "default-engine";
get name() { throw "name property must be overridden in subclasses"; },
// "DefaultEngine";
get logName() { throw "logName property must be overridden in subclasses"; },
// "user-data/default-engine/";
get serverPrefix() { throw "serverPrefix property must be overridden in subclasses"; },
// These can be overridden in subclasses, but don't need to be (assuming
// serverPrefix is not shared with anything else)
get statusFile() { return this.serverPrefix + "status.json"; },
get snapshotFile() { return this.serverPrefix + "snapshot.json"; },
get deltasFile() { return this.serverPrefix + "deltas.json"; },
__os: null, __os: null,
get _os() { get _os() {
@ -65,24 +79,25 @@ BookmarksEngine.prototype = {
return this.__os; return this.__os;
}, },
__store: null, // _core, and _store need to be overridden in subclasses
get _store() {
if (!this.__store)
this.__store = new BookmarksStore();
return this.__store;
},
__core: null, __core: null,
get _core() { get _core() {
if (!this.__core) if (!this.__core)
this.__core = new BookmarksSyncCore(); this.__core = new SyncCore();
return this.__core; return this.__core;
}, },
__store: null,
get _store() {
if (!this.__store)
this.__store = new Store();
return this.__store;
},
__snapshot: null, __snapshot: null,
get _snapshot() { get _snapshot() {
if (!this.__snapshot) if (!this.__snapshot)
this.__snapshot = new SnapshotStore(); this.__snapshot = new SnapshotStore(this.name);
return this.__snapshot; return this.__snapshot;
}, },
set _snapshot(value) { set _snapshot(value) {
@ -92,235 +107,98 @@ BookmarksEngine.prototype = {
_init: function BmkEngine__init(davCollection, cryptoId) { _init: function BmkEngine__init(davCollection, cryptoId) {
this._dav = davCollection; this._dav = davCollection;
this._cryptoId = cryptoId; this._cryptoId = cryptoId;
this._log = Log4Moz.Service.getLogger("Service." + this._logName); this._log = Log4Moz.Service.getLogger("Service." + this.logName);
this._osPrefix = "weave:" + this.name + ":";
this._snapshot.load(); this._snapshot.load();
}, },
_checkStatus: function BmkEngine__checkStatus(code, msg) { _checkStatus: function BmkEngine__checkStatus(code, msg, ok404) {
if (code >= 200 && code < 300) if (code >= 200 && code < 300)
return; return;
if (ok404 && code == 404)
return;
this._log.error(msg + " Error code: " + code); this._log.error(msg + " Error code: " + code);
throw 'checkStatus failed'; throw 'checkStatus failed';
}, },
/* Get the deltas/combined updates from the server _resetServer: function Engine__resetServer(onComplete) {
* Returns:
* status:
* -1: error
* 0: ok
* These fields may be null when status is -1:
* formatVersion:
* version of the data format itself. For compatibility checks.
* maxVersion:
* the latest version on the server
* snapVersion:
* the version of the current snapshot on the server (deltas not applied)
* snapEncryption:
* encryption algorithm currently used on the server-stored snapshot
* deltasEncryption:
* encryption algorithm currently used on the server-stored deltas
* snapshot:
* full snapshot of the latest server version (deltas applied)
* deltas:
* all of the individual deltas on the server
* updates:
* the relevant deltas (from our snapshot version to current),
* combined into a single set.
*/
_getServerData: function BmkEngine__getServerData(onComplete) {
let [self, cont] = yield; let [self, cont] = yield;
let ret = {status: -1, let done = false;
formatVersion: null, maxVersion: null, snapVersion: null,
snapEncryption: null, deltasEncryption: null,
snapshot: null, deltas: null, updates: null};
try { try {
this._log.info("Getting bookmarks status from server"); this._log.debug("Resetting server data");
this._dav.GET("bookmarks-status.json", cont); this._os.notifyObservers(null, this._osPrefix + "reset-server:start", "");
let resp = yield;
let status = resp.status;
switch (status) { this._dav.lock.async(this._dav, cont);
case 200: let locked = yield;
this._log.info("Got bookmarks status from server"); if (locked)
this._log.debug("Lock acquired");
let status = eval(resp.responseText); else {
let deltas, allDeltas; this._log.warn("Could not acquire lock, aborting server reset");
let snap = new SnapshotStore();
// Bail out if the server has a newer format version than we can parse
if (status.formatVersion > STORAGE_FORMAT_VERSION) {
this._log.error("Server uses storage format v" + status.formatVersion +
", this client understands up to v" + STORAGE_FORMAT_VERSION);
generatorDone(this, self, onComplete, ret)
return; return;
} }
if (status.formatVersion == 0) { // try to delete all 3, check status after
ret.snapEncryption = status.snapEncryption = "none"; this._dav.DELETE(this.statusFile, cont);
ret.deltasEncryption = status.deltasEncryption = "none"; let statusResp = yield;
} this._dav.DELETE(this.snapshotFile, cont);
let snapshotResp = yield;
this._dav.DELETE(this.deltasFile, cont);
let deltasResp = yield;
if (status.GUID != this._snapshot.GUID) { this._dav.unlock.async(this._dav, cont);
this._log.info("Remote/local sync GUIDs do not match. " + let unlocked = yield;
"Forcing initial sync.");
this._store.resetGUIDs();
this._snapshot.data = {};
this._snapshot.version = -1;
this._snapshot.GUID = status.GUID;
}
if (this._snapshot.version < status.snapVersion) { checkStatus(statusResp.status, "Could not delete status file.", true);
if (this._snapshot.version >= 0) checkStatus(snapshotResp.status, "Could not delete snapshot file.", true);
this._log.info("Local snapshot is out of date"); checkStatus(deltasResp.status, "Could not delete deltas file.", true);
this._log.info("Downloading server snapshot"); this._log.debug("Server files deleted");
this._dav.GET("bookmarks-snapshot.json", cont); done = true;
resp = yield;
this._checkStatus(resp.status, "Could not download snapshot.");
snap.data = Crypto.PBEdecrypt(resp.responseText,
this._cryptoId,
status.snapEncryption);
this._log.info("Downloading server deltas");
this._dav.GET("bookmarks-deltas.json", cont);
resp = yield;
this._checkStatus(resp.status, "Could not download deltas.");
allDeltas = Crypto.PBEdecrypt(resp.responseText,
this._cryptoId,
status.deltasEncryption);
deltas = eval(uneval(allDeltas));
} else if (this._snapshot.version >= status.snapVersion &&
this._snapshot.version < status.maxVersion) {
snap.data = eval(uneval(this._snapshot.data));
this._log.info("Downloading server deltas");
this._dav.GET("bookmarks-deltas.json", cont);
resp = yield;
this._checkStatus(resp.status, "Could not download deltas.");
allDeltas = Crypto.PBEdecrypt(resp.responseText,
this._cryptoId,
status.deltasEncryption);
deltas = allDeltas.slice(this._snapshot.version - status.snapVersion);
} else if (this._snapshot.version == status.maxVersion) {
snap.data = eval(uneval(this._snapshot.data));
// FIXME: could optimize this case by caching deltas file
this._log.info("Downloading server deltas");
this._dav.GET("bookmarks-deltas.json", cont);
resp = yield;
this._checkStatus(resp.status, "Could not download deltas.");
allDeltas = Crypto.PBEdecrypt(resp.responseText,
this._cryptoId,
status.deltasEncryption);
deltas = [];
} else { // this._snapshot.version > status.maxVersion
this._log.error("Server snapshot is older than local snapshot");
return;
}
for (var i = 0; i < deltas.length; i++) {
snap.applyCommands(deltas[i]);
}
ret.status = 0;
ret.formatVersion = status.formatVersion;
ret.maxVersion = status.maxVersion;
ret.snapVersion = status.snapVersion;
ret.snapEncryption = status.snapEncryption;
ret.deltasEncryption = status.deltasEncryption;
ret.snapshot = snap.data;
ret.deltas = allDeltas;
this._core.detectUpdates(cont, this._snapshot.data, snap.data);
ret.updates = yield;
break;
case 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, cont);
let uploadStatus = yield;
if (!uploadStatus)
return;
this._log.info("Initial upload to server successful");
this.snapshot.save();
ret.status = 0;
ret.formatVersion = STORAGE_FORMAT_VERSION;
ret.maxVersion = this._snapshot.version;
ret.snapVersion = this._snapshot.version;
ret.snapEncryption = Crypto.defaultAlgorithm;
ret.deltasEncryption = Crypto.defaultAlgorithm;
ret.snapshot = eval(uneval(this._snapshot.data));
ret.deltas = [];
ret.updates = [];
break;
default:
this._log.error("Could not get bookmarks.status: unknown HTTP status code " +
status);
break;
}
} catch (e) { } catch (e) {
if (e != 'checkStatus failed' && if (e != 'checkStatus failed')
e != 'decrypt failed') this._log.error("Exception caught: " + (e.message? e.message : e));
this._log.error("Exception caught: " + e.message);
} finally { } finally {
generatorDone(this, self, onComplete, ret) if (done) {
this._log.debug("Server reset completed successfully");
this._os.notifyObservers(null, this._osPrefix + "reset-server:end", "");
} else {
this._log.debug("Server reset failed");
this._os.notifyObservers(null, this._osPrefix + "reset-server:error", "");
}
generatorDone(this, self, onComplete, done)
yield; // onComplete is responsible for closing the generator yield; // onComplete is responsible for closing the generator
} }
this._log.warn("generator not properly closed"); this._log.warn("generator not properly closed");
}, },
_fullUpload: function BmkEngine__fullUpload(onComplete) { _resetClient: function Engine__resetClient(onComplete) {
let [self, cont] = yield; let [self, cont] = yield;
let ret = false; let done = false;
try { try {
let data = Crypto.PBEencrypt(this._snapshot.serialize(), this._log.debug("Resetting client state");
this._cryptoId); this._os.notifyObservers(null, this._osPrefix + "reset-client:start", "");
this._dav.PUT("bookmarks-snapshot.json", data, cont);
resp = yield;
this._checkStatus(resp.status, "Could not upload snapshot.");
this._dav.PUT("bookmarks-deltas.json", uneval([]), cont); this._snapshot.data = {};
resp = yield; this._snapshot.version = -1;
this._checkStatus(resp.status, "Could not upload deltas."); this._snapshot.save();
done = true;
let c = 0;
for (GUID in this._snapshot.data)
c++;
this._dav.PUT("bookmarks-status.json",
uneval({GUID: this._snapshot.GUID,
formatVersion: STORAGE_FORMAT_VERSION,
snapVersion: this._snapshot.version,
maxVersion: this._snapshot.version,
snapEncryption: Crypto.defaultAlgorithm,
deltasEncryption: "none",
bookmarksCount: c}), cont);
resp = yield;
this._checkStatus(resp.status, "Could not upload status file.");
this._log.info("Full upload to server successful");
ret = true;
} catch (e) { } catch (e) {
if (e != 'checkStatus failed') this._log.error("Exception caught: " + (e.message? e.message : e));
this._log.error("Exception caught: " + e.message);
} finally { } finally {
generatorDone(this, self, onComplete, ret) if (done) {
this._log.debug("Client reset completed successfully");
this._os.notifyObservers(null, this._osPrefix + "reset-client:end", "");
} else {
this._log.debug("Client reset failed");
this._os.notifyObservers(null, this._osPrefix + "reset-client:error", "");
}
generatorDone(this, self, onComplete, done);
yield; // onComplete is responsible for closing the generator yield; // onComplete is responsible for closing the generator
} }
this._log.warn("generator not properly closed"); this._log.warn("generator not properly closed");
@ -360,7 +238,7 @@ BookmarksEngine.prototype = {
try { try {
this._log.info("Beginning sync"); this._log.info("Beginning sync");
this._os.notifyObservers(null, "bookmarks-sync:sync-start", ""); this._os.notifyObservers(null, this._osPrefix + "sync:start", "");
this._dav.lock.async(this._dav, cont); this._dav.lock.async(this._dav, cont);
locked = yield; locked = yield;
@ -372,6 +250,11 @@ BookmarksEngine.prototype = {
return; return;
} }
// Before we get started, make sure we have a remote directory to play in
this._dav.MKCOL(this.serverPrefix, cont);
let ret = yield;
this._checkStatus(ret.status, "Could not create remote folder.");
// 1) Fetch server deltas // 1) Fetch server deltas
this._getServerData.async(this, cont); this._getServerData.async(this, cont);
let server = yield; let server = yield;
@ -409,7 +292,7 @@ BookmarksEngine.prototype = {
this._log.info("Reconciling client/server updates"); this._log.info("Reconciling client/server updates");
this._core.reconcile(cont, localUpdates, server.updates); this._core.reconcile(cont, localUpdates, server.updates);
let ret = yield; ret = yield;
let clientChanges = ret.propagations[0]; let clientChanges = ret.propagations[0];
let serverChanges = ret.propagations[1]; let serverChanges = ret.propagations[1];
@ -466,7 +349,6 @@ BookmarksEngine.prototype = {
this._snapshot.data = eval(uneval(savedSnap)); this._snapshot.data = eval(uneval(savedSnap));
this._snapshot.version = savedVersion; this._snapshot.version = savedVersion;
} }
this._snapshot.save(); this._snapshot.save();
} }
@ -508,21 +390,21 @@ BookmarksEngine.prototype = {
} else { } else {
let data = Crypto.PBEencrypt(serializeCommands(server.deltas), let data = Crypto.PBEencrypt(serializeCommands(server.deltas),
this._cryptoId); this._cryptoId);
this._dav.PUT("bookmarks-deltas.json", data, cont); this._dav.PUT(this.deltasFile, data, cont);
let deltasPut = yield; let deltasPut = yield;
let c = 0; let c = 0;
for (GUID in this._snapshot.data) for (GUID in this._snapshot.data)
c++; c++;
this._dav.PUT("bookmarks-status.json", this._dav.PUT(this.statusFile,
uneval({GUID: this._snapshot.GUID, uneval({GUID: this._snapshot.GUID,
formatVersion: STORAGE_FORMAT_VERSION, formatVersion: STORAGE_FORMAT_VERSION,
snapVersion: server.snapVersion, snapVersion: server.snapVersion,
maxVersion: this._snapshot.version, maxVersion: this._snapshot.version,
snapEncryption: server.snapEncryption, snapEncryption: server.snapEncryption,
deltasEncryption: Crypto.defaultAlgorithm, deltasEncryption: Crypto.defaultAlgorithm,
Bookmarkscount: c}), cont); itemCount: c}), cont);
let statusPut = yield; let statusPut = yield;
if (deltasPut.status >= 200 && deltasPut.status < 300 && if (deltasPut.status >= 200 && deltasPut.status < 300 &&
@ -541,7 +423,7 @@ BookmarksEngine.prototype = {
synced = true; synced = true;
} catch (e) { } catch (e) {
this._log.error("Exception caught: " + e.message); this._log.error("Exception caught: " + (e.message? e.message : e));
} finally { } finally {
let ok = false; let ok = false;
@ -550,10 +432,10 @@ BookmarksEngine.prototype = {
ok = yield; ok = yield;
} }
if (ok && synced) { if (ok && synced) {
this._os.notifyObservers(null, "bookmarks-sync:sync-end", ""); this._os.notifyObservers(null, this._osPrefix + "sync:end", "");
generatorDone(this, self, onComplete, true); generatorDone(this, self, onComplete, true);
} else { } else {
this._os.notifyObservers(null, "bookmarks-sync:sync-error", ""); this._os.notifyObservers(null, this._osPrefix + "sync:error", "");
generatorDone(this, self, onComplete, false); generatorDone(this, self, onComplete, false);
} }
yield; // onComplete is responsible for closing the generator yield; // onComplete is responsible for closing the generator
@ -561,112 +443,290 @@ BookmarksEngine.prototype = {
this._log.warn("generator not properly closed"); this._log.warn("generator not properly closed");
}, },
_resetServer: function BmkEngine__resetServer(onComplete) { /* Get the deltas/combined updates from the server
* Returns:
* status:
* -1: error
* 0: ok
* These fields may be null when status is -1:
* formatVersion:
* version of the data format itself. For compatibility checks.
* maxVersion:
* the latest version on the server
* snapVersion:
* the version of the current snapshot on the server (deltas not applied)
* snapEncryption:
* encryption algorithm currently used on the server-stored snapshot
* deltasEncryption:
* encryption algorithm currently used on the server-stored deltas
* snapshot:
* full snapshot of the latest server version (deltas applied)
* deltas:
* all of the individual deltas on the server
* updates:
* the relevant deltas (from our snapshot version to current),
* combined into a single set.
*/
_getServerData: function BmkEngine__getServerData(onComplete) {
let [self, cont] = yield; let [self, cont] = yield;
let done = false; let ret = {status: -1,
formatVersion: null, maxVersion: null, snapVersion: null,
snapEncryption: null, deltasEncryption: null,
snapshot: null, deltas: null, updates: null};
try { try {
this._log.debug("Resetting server data"); this._log.debug("Getting status file from server");
this._os.notifyObservers(null, "bookmarks-sync:reset-server-start", ""); this._dav.GET(this.statusFile, cont);
let resp = yield;
let status = resp.status;
this._dav.lock.async(this._dav, cont); switch (status) {
let locked = yield; case 200:
if (locked) this._log.info("Got status file from server");
this._log.debug("Lock acquired");
else { let status = eval(resp.responseText);
this._log.warn("Could not acquire lock, aborting server reset"); let deltas, allDeltas;
let snap = new SnapshotStore();
// Bail out if the server has a newer format version than we can parse
if (status.formatVersion > STORAGE_FORMAT_VERSION) {
this._log.error("Server uses storage format v" + status.formatVersion +
", this client understands up to v" + STORAGE_FORMAT_VERSION);
generatorDone(this, self, onComplete, ret)
return; return;
} }
this._dav.DELETE("bookmarks-status.json", cont); if (status.formatVersion == 0) {
let statusResp = yield; ret.snapEncryption = status.snapEncryption = "none";
this._dav.DELETE("bookmarks-snapshot.json", cont); ret.deltasEncryption = status.deltasEncryption = "none";
let snapshotResp = yield;
this._dav.DELETE("bookmarks-deltas.json", cont);
let deltasResp = yield;
this._dav.unlock.async(this._dav, cont);
let unlocked = yield;
function ok(code) {
if (code >= 200 && code < 300)
return true;
if (code == 404)
return true;
return false;
} }
if (!(ok(statusResp.status) && ok(snapshotResp.status) && if (status.GUID != this._snapshot.GUID) {
ok(deltasResp.status))) { this._log.info("Remote/local sync GUIDs do not match. " +
this._log.error("Could delete server data, response codes " + "Forcing initial sync.");
statusResp.status + ", " + snapshotResp.status + ", " + this._store.resetGUIDs();
deltasResp.status);
return;
}
this._log.debug("Server files deleted");
done = true;
} catch (e) {
this._log.error("Exception caught: " + e.message);
} finally {
if (done) {
this._log.debug("Server reset completed successfully");
this._os.notifyObservers(null, "bookmarks-sync:reset-server-end", "");
} else {
this._log.debug("Server reset failed");
this._os.notifyObservers(null, "bookmarks-sync:reset-server-error", "");
}
generatorDone(this, self, onComplete, done)
yield; // onComplete is responsible for closing the generator
}
this._log.warn("generator not properly closed");
},
_resetClient: function BmkEngine__resetClient(onComplete) {
let [self, cont] = yield;
let done = false;
try {
this._log.debug("Resetting client state");
this._os.notifyObservers(null, "bookmarks-sync:reset-client-start", "");
this._snapshot.data = {}; this._snapshot.data = {};
this._snapshot.version = -1; this._snapshot.version = -1;
this.snapshot.save(); this._snapshot.GUID = status.GUID;
done = true; }
if (this._snapshot.version < status.snapVersion) {
if (this._snapshot.version >= 0)
this._log.info("Local snapshot is out of date");
this._log.info("Downloading server snapshot");
this._dav.GET(this.snapshotFile, cont);
resp = yield;
this._checkStatus(resp.status, "Could not download snapshot.");
snap.data = Crypto.PBEdecrypt(resp.responseText,
this._cryptoId,
status.snapEncryption);
this._log.info("Downloading server deltas");
this._dav.GET(this.deltasFile, cont);
resp = yield;
this._checkStatus(resp.status, "Could not download deltas.");
allDeltas = Crypto.PBEdecrypt(resp.responseText,
this._cryptoId,
status.deltasEncryption);
deltas = eval(uneval(allDeltas));
} else if (this._snapshot.version >= status.snapVersion &&
this._snapshot.version < status.maxVersion) {
snap.data = eval(uneval(this._snapshot.data));
this._log.info("Downloading server deltas");
this._dav.GET(this.deltasFile, cont);
resp = yield;
this._checkStatus(resp.status, "Could not download deltas.");
allDeltas = Crypto.PBEdecrypt(resp.responseText,
this._cryptoId,
status.deltasEncryption);
deltas = allDeltas.slice(this._snapshot.version - status.snapVersion);
} else if (this._snapshot.version == status.maxVersion) {
snap.data = eval(uneval(this._snapshot.data));
// FIXME: could optimize this case by caching deltas file
this._log.info("Downloading server deltas");
this._dav.GET(this.deltasFile, cont);
resp = yield;
this._checkStatus(resp.status, "Could not download deltas.");
allDeltas = Crypto.PBEdecrypt(resp.responseText,
this._cryptoId,
status.deltasEncryption);
deltas = [];
} else { // this._snapshot.version > status.maxVersion
this._log.error("Server snapshot is older than local snapshot");
return;
}
for (var i = 0; i < deltas.length; i++) {
snap.applyCommands(deltas[i]);
}
ret.status = 0;
ret.formatVersion = status.formatVersion;
ret.maxVersion = status.maxVersion;
ret.snapVersion = status.snapVersion;
ret.snapEncryption = status.snapEncryption;
ret.deltasEncryption = status.deltasEncryption;
ret.snapshot = snap.data;
ret.deltas = allDeltas;
this._core.detectUpdates(cont, this._snapshot.data, snap.data);
ret.updates = yield;
break;
case 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, cont);
let uploadStatus = yield;
if (!uploadStatus)
return;
this._log.info("Initial upload to server successful");
this._snapshot.save();
ret.status = 0;
ret.formatVersion = STORAGE_FORMAT_VERSION;
ret.maxVersion = this._snapshot.version;
ret.snapVersion = this._snapshot.version;
ret.snapEncryption = Crypto.defaultAlgorithm;
ret.deltasEncryption = Crypto.defaultAlgorithm;
ret.snapshot = eval(uneval(this._snapshot.data));
ret.deltas = [];
ret.updates = [];
break;
default:
this._log.error("Could not get status file: unknown HTTP status code " +
status);
break;
}
} catch (e) { } catch (e) {
this._log.error("Exception caught: " + e.message); if (e != 'checkStatus failed' &&
e != 'decrypt failed')
this._log.error("Exception caught: " + (e.message? e.message : e));
} finally { } finally {
if (done) { generatorDone(this, self, onComplete, ret)
this._log.debug("Client reset completed successfully");
this._os.notifyObservers(null, "bookmarks-sync:reset-client-end", "");
} else {
this._log.debug("Client reset failed");
this._os.notifyObservers(null, "bookmarks-sync:reset-client-error", "");
}
generatorDone(this, self, onComplete, done);
yield; // onComplete is responsible for closing the generator yield; // onComplete is responsible for closing the generator
} }
this._log.warn("generator not properly closed"); this._log.warn("generator not properly closed");
}, },
sync: function BmkEngine_sync(onComplete) { _fullUpload: function Engine__fullUpload(onComplete) {
let [self, cont] = yield;
let ret = false;
try {
let data = Crypto.PBEencrypt(this._snapshot.serialize(),
this._cryptoId);
this._dav.PUT(this.snapshotFile, data, cont);
resp = yield;
this._checkStatus(resp.status, "Could not upload snapshot.");
this._dav.PUT(this.deltasFile, uneval([]), cont);
resp = yield;
this._checkStatus(resp.status, "Could not upload deltas.");
let c = 0;
for (GUID in this._snapshot.data)
c++;
this._dav.PUT(this.statusFile,
uneval({GUID: this._snapshot.GUID,
formatVersion: STORAGE_FORMAT_VERSION,
snapVersion: this._snapshot.version,
maxVersion: this._snapshot.version,
snapEncryption: Crypto.defaultAlgorithm,
deltasEncryption: "none",
itemCount: c}), cont);
resp = yield;
this._checkStatus(resp.status, "Could not upload status file.");
this._log.info("Full upload to server successful");
ret = true;
} catch (e) {
if (e != 'checkStatus failed')
this._log.error("Exception caught: " + (e.message? e.message : e));
} finally {
generatorDone(this, self, onComplete, ret)
yield; // onComplete is responsible for closing the generator
}
this._log.warn("generator not properly closed");
},
sync: function Engine_sync(onComplete) {
return this._sync.async(this, onComplete); return this._sync.async(this, onComplete);
}, },
resetServer: function BmkEngine_resetServer(onComplete) { resetServer: function Engine_resetServer(onComplete) {
return this._resetServer.async(this, onComplete); return this._resetServer.async(this, onComplete);
}, },
resetClient: function BmkEngine_resetClient(onComplete) { resetClient: function Engine_resetClient(onComplete) {
return this._resetClient.async(this, onComplete); return this._resetClient.async(this, onComplete);
} }
}; };
function BookmarksEngine(davCollection, cryptoId) {
this._init(davCollection, cryptoId);
}
BookmarksEngine.prototype = {
get name() { return "bookmarks-engine"; },
get logName() { return "BmkEngine"; },
get serverPrefix() { return "user-data/bookmarks/"; },
__core: null,
get _core() {
if (!this.__core)
this.__core = new BookmarksSyncCore();
return this.__core;
},
__store: null,
get _store() {
if (!this.__store)
this.__store = new BookmarksStore();
return this.__store;
}
};
BookmarksEngine.prototype.__proto__ = new Engine();
function HistoryEngine(davCollection, cryptoId) {
this._init(davCollection, cryptoId);
}
HistoryEngine.prototype = {
get name() { return "history-engine"; },
get logName() { return "HistEngine"; },
get serverPrefix() { return "user-data/history/"; },
__core: null,
get _core() {
if (!this.__core)
this.__core = new HistorySyncCore();
return this.__core;
},
__store: null,
get _store() {
if (!this.__store)
this.__store = new HistoryStore();
return this.__store;
}
};
HistoryEngine.prototype.__proto__ = new Engine();
serializeCommands: function serializeCommands(commands) { serializeCommands: function serializeCommands(commands) {
let json = uneval(commands); let json = uneval(commands);
json = json.replace(/ {action/g, "\n {action"); json = json.replace(/ {action/g, "\n {action");

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

@ -41,6 +41,7 @@ const Ci = Components.interfaces;
const Cr = Components.results; const Cr = Components.results;
const Cu = Components.utils; const Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://weave/log4moz.js"); Cu.import("resource://weave/log4moz.js");
Cu.import("resource://weave/constants.js"); Cu.import("resource://weave/constants.js");
Cu.import("resource://weave/util.js"); Cu.import("resource://weave/util.js");

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

@ -36,30 +36,28 @@
* *
* ***** END LICENSE BLOCK ***** */ * ***** END LICENSE BLOCK ***** */
const EXPORTED_SYMBOLS = ['Log4Moz'];
const Cc = Components.classes; const Cc = Components.classes;
const Ci = Components.interfaces; const Ci = Components.interfaces;
const Cr = Components.results; const Cr = Components.results;
const Cu = Components.utils; const Cu = Components.utils;
const MODE_RDONLY = 0x01; Cu.import("resource://gre/modules/XPCOMUtils.jsm");
const MODE_WRONLY = 0x02; Cu.import("resource://weave/constants.js");
const MODE_CREATE = 0x08;
const MODE_APPEND = 0x10;
const MODE_TRUNCATE = 0x20;
const PERMS_FILE = 0644; const Log4Moz = {};
const PERMS_DIRECTORY = 0755; Log4Moz.Level = {};
Log4Moz.Level.Fatal = 70;
Log4Moz.Level.Error = 60;
Log4Moz.Level.Warn = 50;
Log4Moz.Level.Info = 40;
Log4Moz.Level.Config = 30;
Log4Moz.Level.Debug = 20;
Log4Moz.Level.Trace = 10;
Log4Moz.Level.All = 0;
const LEVEL_FATAL = 70; Log4Moz.Level.Desc = {
const LEVEL_ERROR = 60;
const LEVEL_WARN = 50;
const LEVEL_INFO = 40;
const LEVEL_CONFIG = 30;
const LEVEL_DEBUG = 20;
const LEVEL_TRACE = 10;
const LEVEL_ALL = 0;
const LEVEL_DESC = {
70: "FATAL", 70: "FATAL",
60: "ERROR", 60: "ERROR",
50: "WARN", 50: "WARN",
@ -70,12 +68,6 @@ const LEVEL_DESC = {
0: "ALL" 0: "ALL"
}; };
const ONE_BYTE = 1;
const ONE_KILOBYTE = 1024 * ONE_BYTE;
const ONE_MEGABYTE = 1024 * ONE_KILOBYTE;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
/* /*
* LogMessage * LogMessage
* Encapsulates a single log event's data * Encapsulates a single log event's data
@ -87,11 +79,11 @@ function LogMessage(loggerName, level, message){
this.time = Date.now(); this.time = Date.now();
} }
LogMessage.prototype = { LogMessage.prototype = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]), // Ci.ILogMessage, QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]),
get levelDesc() { get levelDesc() {
if (this.level in LEVEL_DESC) if (this.level in Log4Moz.Level.Desc)
return LEVEL_DESC[this.level]; return Log4Moz.Level.Desc[this.level];
return "UNKNOWN"; return "UNKNOWN";
}, },
@ -112,7 +104,7 @@ function Logger(name, repository) {
this._appenders = []; this._appenders = [];
} }
Logger.prototype = { Logger.prototype = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]), // Ci.ILogger, QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]),
parent: null, parent: null,
@ -123,7 +115,7 @@ Logger.prototype = {
if (this.parent) if (this.parent)
return this.parent.level; return this.parent.level;
dump("log4moz warning: root logger configuration error: no level defined\n"); dump("log4moz warning: root logger configuration error: no level defined\n");
return LEVEL_ALL; return Log4Moz.Level.All;
}, },
set level(level) { set level(level) {
this._level = level; this._level = level;
@ -154,25 +146,25 @@ Logger.prototype = {
}, },
fatal: function Logger_fatal(string) { fatal: function Logger_fatal(string) {
this.log(new LogMessage(this._name, LEVEL_FATAL, string)); this.log(new LogMessage(this._name, Log4Moz.Level.Fatal, string));
}, },
error: function Logger_error(string) { error: function Logger_error(string) {
this.log(new LogMessage(this._name, LEVEL_ERROR, string)); this.log(new LogMessage(this._name, Log4Moz.Level.Error, string));
}, },
warn: function Logger_warn(string) { warn: function Logger_warn(string) {
this.log(new LogMessage(this._name, LEVEL_WARN, string)); this.log(new LogMessage(this._name, Log4Moz.Level.Warn, string));
}, },
info: function Logger_info(string) { info: function Logger_info(string) {
this.log(new LogMessage(this._name, LEVEL_INFO, string)); this.log(new LogMessage(this._name, Log4Moz.Level.Info, string));
}, },
config: function Logger_config(string) { config: function Logger_config(string) {
this.log(new LogMessage(this._name, LEVEL_CONFIG, string)); this.log(new LogMessage(this._name, Log4Moz.Level.Config, string));
}, },
debug: function Logger_debug(string) { debug: function Logger_debug(string) {
this.log(new LogMessage(this._name, LEVEL_DEBUG, string)); this.log(new LogMessage(this._name, Log4Moz.Level.Debug, string));
}, },
trace: function Logger_trace(string) { trace: function Logger_trace(string) {
this.log(new LogMessage(this._name, LEVEL_TRACE, string)); this.log(new LogMessage(this._name, Log4Moz.Level.Trace, string));
} }
}; };
@ -183,7 +175,7 @@ Logger.prototype = {
function LoggerRepository() {} function LoggerRepository() {}
LoggerRepository.prototype = { LoggerRepository.prototype = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]), // Ci.ILoggerRepository, QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]),
_loggers: {}, _loggers: {},
@ -191,7 +183,7 @@ LoggerRepository.prototype = {
get rootLogger() { get rootLogger() {
if (!this._rootLogger) { if (!this._rootLogger) {
this._rootLogger = new Logger("root", this); this._rootLogger = new Logger("root", this);
this._rootLogger.level = LEVEL_ALL; this._rootLogger.level = Log4Moz.Level.All;
} }
return this._rootLogger; return this._rootLogger;
}, },
@ -245,7 +237,7 @@ LoggerRepository.prototype = {
// Abstract formatter // Abstract formatter
function Formatter() {} function Formatter() {}
Formatter.prototype = { Formatter.prototype = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]), // Ci.IFormatter, QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]),
format: function Formatter_format(message) {} format: function Formatter_format(message) {}
}; };
@ -287,9 +279,9 @@ function Appender(formatter) {
this._formatter = formatter? formatter : new BasicFormatter(); this._formatter = formatter? formatter : new BasicFormatter();
} }
Appender.prototype = { Appender.prototype = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]), // Ci.IAppender, QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]),
_level: LEVEL_ALL, _level: Log4Moz.Level.All,
get level() { return this._level; }, get level() { return this._level; },
set level(level) { this._level = level; }, set level(level) { this._level = level; },
@ -331,7 +323,7 @@ function ConsoleAppender(formatter) {
} }
ConsoleAppender.prototype = { ConsoleAppender.prototype = {
doAppend: function CApp_doAppend(message) { doAppend: function CApp_doAppend(message) {
if (message.level > LEVEL_WARN) { if (message.level > Log4Moz.Level.Warn) {
Cu.reportError(message); Cu.reportError(message);
return; return;
} }
@ -446,7 +438,7 @@ Log4MozService.prototype = {
//classDescription: "Log4moz Logging Service", //classDescription: "Log4moz Logging Service",
//contractID: "@mozilla.org/log4moz/service;1", //contractID: "@mozilla.org/log4moz/service;1",
//classID: Components.ID("{a60e50d7-90b8-4a12-ad0c-79e6a1896978}"), //classID: Components.ID("{a60e50d7-90b8-4a12-ad0c-79e6a1896978}"),
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]), //Ci.ILog4MozService, QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]),
get rootLogger() { get rootLogger() {
return this._repository.rootLogger; return this._repository.rootLogger;
@ -492,20 +484,4 @@ Log4MozService.prototype = {
} }
}; };
const EXPORTED_SYMBOLS = ['Log4Moz'];
const Log4Moz = {};
Log4Moz.Level = {};
Log4Moz.Level.Fatal = LEVEL_FATAL;
Log4Moz.Level.Error = LEVEL_ERROR;
Log4Moz.Level.Warn = LEVEL_WARN;
Log4Moz.Level.Info = LEVEL_INFO;
Log4Moz.Level.Config = LEVEL_CONFIG;
Log4Moz.Level.Debug = LEVEL_DEBUG;
Log4Moz.Level.Trace = LEVEL_TRACE;
Log4Moz.Level.All = LEVEL_ALL;
Log4Moz.Level.Desc = LEVEL_DESC;
Log4Moz.Service = new Log4MozService(); Log4Moz.Service = new Log4MozService();

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

@ -41,6 +41,7 @@ const Ci = Components.interfaces;
const Cr = Components.results; const Cr = Components.results;
const Cu = Components.utils; const Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://weave/log4moz.js"); Cu.import("resource://weave/log4moz.js");
Cu.import("resource://weave/constants.js"); Cu.import("resource://weave/constants.js");
Cu.import("resource://weave/util.js"); Cu.import("resource://weave/util.js");
@ -49,7 +50,6 @@ Cu.import("resource://weave/engines.js");
Cu.import("resource://weave/dav.js"); Cu.import("resource://weave/dav.js");
Cu.import("resource://weave/identity.js"); Cu.import("resource://weave/identity.js");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Function.prototype.async = generatorAsync; Function.prototype.async = generatorAsync;
@ -84,6 +84,8 @@ WeaveSyncService.prototype = {
return this.__dav; return this.__dav;
}, },
// FIXME: engines should be loaded dynamically somehow / need API to register
__bmkEngine: null, __bmkEngine: null,
get _bmkEngine() { get _bmkEngine() {
if (!this.__bmkEngine) if (!this.__bmkEngine)
@ -91,6 +93,13 @@ WeaveSyncService.prototype = {
return this.__bmkEngine; return this.__bmkEngine;
}, },
__histEngine: null,
get _histEngine() {
if (!this.__histEngine)
this.__histEngine = new HistoryEngine(this._dav, this._cryptoId);
return this.__histEngine;
},
// Logger object // Logger object
_log: null, _log: null,
@ -163,7 +172,7 @@ WeaveSyncService.prototype = {
return null; return null;
}, },
_init: function BSS__init() { _init: function WeaveSync__init() {
this._initLogs(); this._initLogs();
this._log.info("Weave Sync Service Initializing"); this._log.info("Weave Sync Service Initializing");
@ -202,7 +211,7 @@ WeaveSyncService.prototype = {
} }
}, },
_enableSchedule: function BSS__enableSchedule() { _enableSchedule: function WeaveSync__enableSchedule() {
this._scheduleTimer = Cc["@mozilla.org/timer;1"]. this._scheduleTimer = Cc["@mozilla.org/timer;1"].
createInstance(Ci.nsITimer); createInstance(Ci.nsITimer);
let listener = new EventListener(bind2(this, this._onSchedule)); let listener = new EventListener(bind2(this, this._onSchedule));
@ -210,16 +219,16 @@ WeaveSyncService.prototype = {
this._scheduleTimer.TYPE_REPEATING_SLACK); this._scheduleTimer.TYPE_REPEATING_SLACK);
}, },
_disableSchedule: function BSS__disableSchedule() { _disableSchedule: function WeaveSync__disableSchedule() {
this._scheduleTimer = null; this._scheduleTimer = null;
}, },
_onSchedule: function BSS__onSchedule() { _onSchedule: function WeaveSync__onSchedule() {
this._log.info("Running scheduled sync"); this._log.info("Running scheduled sync");
this.sync(); this.sync();
}, },
_initLogs: function BSS__initLogs() { _initLogs: function WeaveSync__initLogs() {
this._log = Log4Moz.Service.getLogger("Service.Main"); this._log = Log4Moz.Service.getLogger("Service.Main");
let formatter = Log4Moz.Service.newFormatter("basic"); let formatter = Log4Moz.Service.newFormatter("basic");
@ -249,7 +258,7 @@ WeaveSyncService.prototype = {
root.addAppender(vapp); root.addAppender(vapp);
}, },
_lock: function BSS__lock() { _lock: function weaveSync__lock() {
if (this._locked) { if (this._locked) {
this._log.warn("Service lock failed: already locked"); this._log.warn("Service lock failed: already locked");
return false; return false;
@ -259,14 +268,14 @@ WeaveSyncService.prototype = {
return true; return true;
}, },
_unlock: function BSS__unlock() { _unlock: function WeaveSync__unlock() {
this._locked = false; this._locked = false;
this._log.debug("Service lock released"); this._log.debug("Service lock released");
}, },
// IBookmarksSyncService internal implementation // IBookmarksSyncService internal implementation
_login: function BSS__login(onComplete) { _login: function WeaveSync__login(onComplete) {
let [self, cont] = yield; let [self, cont] = yield;
let success = false; let success = false;
@ -309,7 +318,7 @@ WeaveSyncService.prototype = {
this._log.warn("generator not properly closed"); this._log.warn("generator not properly closed");
}, },
_resetLock: function BSS__resetLock(onComplete) { _resetLock: function WeaveSync__resetLock(onComplete) {
let [self, cont] = yield; let [self, cont] = yield;
let success = false; let success = false;
@ -321,7 +330,7 @@ WeaveSyncService.prototype = {
success = yield; success = yield;
} catch (e) { } catch (e) {
this._log.error("Exception caught: " + e.message); this._log.error("Exception caught: " + (e.message? e.message : e));
} finally { } finally {
if (success) { if (success) {
@ -332,7 +341,75 @@ WeaveSyncService.prototype = {
this._os.notifyObservers(null, "bookmarks-sync:lock-reset-error", ""); this._os.notifyObservers(null, "bookmarks-sync:lock-reset-error", "");
} }
generatorDone(this, self, onComplete, success); generatorDone(this, self, onComplete, success);
yield; // onComplete is responsible for closing the generator yield; // generatorDone is responsible for closing the generator
}
this._log.warn("generator not properly closed");
},
_sync: function WeaveSync__sync() {
let [self, cont] = yield;
try {
if (!this._lock())
return;
this._bmkEngine.sync(cont);
yield;
this._histEngine.sync(cont);
yield;
this._unlock();
} catch (e) {
this._log.error("Exception caught: " + (e.message? e.message : e));
} finally {
generatorDone(this, self);
yield; // generatorDone is responsible for closing the generator
}
this._log.warn("generator not properly closed");
},
_resetServer: function WeaveSync__resetServer() {
let [self, cont] = yield;
try {
if (!this._lock())
return;
this._bmkEngine.resetServer(cont);
this._histEngine.resetServer(cont);
this._unlock();
} catch (e) {
this._log.error("Exception caught: " + (e.message? e.message : e));
} finally {
generatorDone(this, self);
yield; // generatorDone is responsible for closing the generator
}
this._log.warn("generator not properly closed");
},
_resetClient: function WeaveSync__resetClient() {
let [self, cont] = yield;
try {
if (!this._lock())
return;
this._bmkEngine.resetClient(cont);
this._histEngine.resetClient(cont);
this._unlock();
} catch (e) {
this._log.error("Exception caught: " + (e.message? e.message : e));
} finally {
generatorDone(this, self);
yield; // generatorDone is responsible for closing the generator
} }
this._log.warn("generator not properly closed"); this._log.warn("generator not properly closed");
}, },
@ -341,7 +418,7 @@ WeaveSyncService.prototype = {
// nsIObserver // nsIObserver
observe: function BSS__observe(subject, topic, data) { observe: function WeaveSync__observe(subject, topic, data) {
switch (topic) { switch (topic) {
case "browser.places.sync.enabled": case "browser.places.sync.enabled":
switch (data) { switch (data) {
@ -379,7 +456,7 @@ WeaveSyncService.prototype = {
// These are global (for all engines) // These are global (for all engines)
login: function BSS_login(password, passphrase) { login: function WeaveSync_login(password, passphrase) {
if (!this._lock()) if (!this._lock())
return; return;
// cache password & passphrase // cache password & passphrase
@ -390,7 +467,7 @@ WeaveSyncService.prototype = {
this._login.async(this, function() {self._unlock()}); this._login.async(this, function() {self._unlock()});
}, },
logout: function BSS_logout() { logout: function WeaveSync_logout() {
this._log.info("Logging out"); this._log.info("Logging out");
this._dav.logout(); this._dav.logout();
this._mozId.setTempPassword(null); // clear cached password this._mozId.setTempPassword(null); // clear cached password
@ -398,7 +475,7 @@ WeaveSyncService.prototype = {
this._os.notifyObservers(null, "bookmarks-sync:logout", ""); this._os.notifyObservers(null, "bookmarks-sync:logout", "");
}, },
resetLock: function BSS_resetLock() { resetLock: function WeaveSync_resetLock() {
if (!this._lock()) if (!this._lock())
return; return;
let self = this; let self = this;
@ -407,24 +484,7 @@ WeaveSyncService.prototype = {
// These are per-engine // These are per-engine
sync: function BSS_sync() { sync: function WeaveSync_sync() { this._sync.async(this); },
if (!this._lock()) resetServer: function WeaveSync_resetServer() { this._resetServer.async(this); },
return; resetClient: function WeaveSync_resetClient() { this._resetClient.async(this); }
let self = this;
this._bmkEngine.sync(function() {self._unlock()});
},
resetServer: function BSS_resetServer() {
if (!this._lock())
return;
let self = this;
this._bmkEngine.resetServer(function() {self._unlock()});
},
resetClient: function BSS_resetClient() {
if (!this._lock())
return;
let self = this;
this._bmkEngine.resetClient(function() {self._unlock()});
}
}; };

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

@ -34,20 +34,19 @@
* *
* ***** END LICENSE BLOCK ***** */ * ***** END LICENSE BLOCK ***** */
const EXPORTED_SYMBOLS = ['Store', 'SnapshotStore', 'BookmarksStore']; const EXPORTED_SYMBOLS = ['Store', 'SnapshotStore', 'BookmarksStore',
'HistoryStore'];
const Cc = Components.classes; const Cc = Components.classes;
const Ci = Components.interfaces; const Ci = Components.interfaces;
const Cr = Components.results; const Cr = Components.results;
const Cu = Components.utils; const Cu = Components.utils;
addModuleAlias("weave", "{340c2bbc-ce74-4362-90b5-7c26312808ef}"); Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://weave/log4moz.js"); Cu.import("resource://weave/log4moz.js");
Cu.import("resource://weave/constants.js"); Cu.import("resource://weave/constants.js");
Cu.import("resource://weave/util.js"); Cu.import("resource://weave/util.js");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
/* /*
* Data Stores * Data Stores
* These can wrap, serialize items and apply commands * These can wrap, serialize items and apply commands
@ -67,15 +66,46 @@ Store.prototype = {
}, },
applyCommands: function Store_applyCommands(commandList) { applyCommands: function Store_applyCommands(commandList) {
for (var i = 0; i < commandList.length; i++) {
var command = commandList[i];
this._log.debug("Processing command: " + uneval(command));
switch (command["action"]) {
case "create":
this._createCommand(command);
break;
case "remove":
this._removeCommand(command);
break;
case "edit":
this._editCommand(command);
break;
default:
this._log.error("unknown action in command: " + command["action"]);
break;
}
}
},
resetGUIDs: function Store_resetGUIDs() {
} }
}; };
function SnapshotStore() { function SnapshotStore(name) {
this._init(); this._init(name);
} }
SnapshotStore.prototype = { SnapshotStore.prototype = {
_logName: "SStore", _logName: "SStore",
_filename: null,
get filename() {
if (this._filename === null)
throw "filename is null";
return this._filename;
},
set filename(value) {
this._filename = value + ".json";
},
__dirSvc: null, __dirSvc: null,
get _dirSvc() { get _dirSvc() {
if (!this.__dirSvc) if (!this.__dirSvc)
@ -108,13 +138,25 @@ SnapshotStore.prototype = {
this._GUID = GUID; this._GUID = GUID;
}, },
_init: function SStore__init(name) {
this.filename = name;
this._log = Log4Moz.Service.getLogger("Service." + this._logName);
},
save: function SStore_save() { save: function SStore_save() {
this._log.info("Saving snapshot to disk"); this._log.info("Saving snapshot to disk");
let file = this._dirSvc.get("ProfD", Ci.nsIFile); let file = this._dirSvc.get("ProfD", Ci.nsIFile);
file.append("bm-sync-snapshot.json"); file.append("weave-snapshots");
file.QueryInterface(Ci.nsILocalFile);
file.QueryInterface(Ci.nsILocalFile);
if (!file.exists())
file.create(file.DIRECTORY_TYPE, PERMS_DIRECTORY);
file.QueryInterface(Ci.nsIFile);
file.append(this.filename);
file.QueryInterface(Ci.nsILocalFile);
if (!file.exists()) if (!file.exists())
file.create(file.NORMAL_FILE_TYPE, PERMS_FILE); file.create(file.NORMAL_FILE_TYPE, PERMS_FILE);
@ -133,7 +175,8 @@ SnapshotStore.prototype = {
load: function SStore_load() { load: function SStore_load() {
let file = this._dirSvc.get("ProfD", Ci.nsIFile); let file = this._dirSvc.get("ProfD", Ci.nsIFile);
file.append("bm-sync-snapshot.json"); file.append("weave-snapshots");
file.append(this.filename);
if (!file.exists()) if (!file.exists())
return; return;
@ -539,18 +582,57 @@ BookmarksStore.prototype = {
}; };
BookmarksStore.prototype.__proto__ = new Store(); BookmarksStore.prototype.__proto__ = new Store();
function addModuleAlias(alias, extensionId) { function HistoryStore() {
let ioSvc = Cc["@mozilla.org/network/io-service;1"] this._init();
.getService(Ci.nsIIOService);
let resProt = ioSvc.getProtocolHandler("resource")
.QueryInterface(Ci.nsIResProtocolHandler);
if (!resProt.hasSubstitution(alias)) {
let extMgr = Cc["@mozilla.org/extensions/manager;1"]
.getService(Ci.nsIExtensionManager);
let loc = extMgr.getInstallLocation(extensionId);
let extD = loc.getItemLocation(extensionId);
extD.append("modules");
resProt.setSubstitution(alias, ioSvc.newFileURI(extD));
}
} }
HistoryStore.prototype = {
_logName: "HistStore",
__hsvc: null,
get _hsvc() {
if (!this.__hsvc)
this.__hsvc = Cc["@mozilla.org/browser/nav-history-service;1"].
getService(Ci.nsINavHistoryService);
return this.__hsvc;
},
_createCommand: function HistStore__createCommand(command) {
this._log.info(" -> creating history entry: " + command.GUID);
this._hsvc.addVisit(makeURI(command.data.URI), command.data.time,
0, this._hsvc.TRANSITION_LINK, false, 0);
this._hsvc.addVisit(makeURI(command.data.URI), command.data.title,
command.data.accessCount, false, false);
},
_removeCommand: function HistStore__removeCommand(command) {
this._log.info(" -> NOT removing history entry: " + command.GUID);
// skip removals
},
_editCommand: function HistStore__editCommand(command) {
this._log.info(" -> FIXME: NOT editing history entry: " + command.GUID);
// FIXME: implement!
},
wrap: function HistStore_wrap() {
let query = this._hsvc.getNewQuery();
query.minVisits = 1;
let root = this._hsvc.executeQuery(query,
this._hsvc.getNewQueryOptions()).root;
root.QueryInterface(Ci.nsINavHistoryQueryResultNode);
root.containerOpen = true;
let items = {};
for (let i = 0; i < root.childCount; i++) {
let item = root.getChild(i);
items[item.uri] = {parentGUID: '',
title: item.title,
URI: item.uri,
time: item.time,
accessCount: item.accessCount,
dateAdded: item.dateAdded,
};
}
return items;
}
};
HistoryStore.prototype.__proto__ = new Store();

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

@ -34,14 +34,14 @@
* *
* ***** END LICENSE BLOCK ***** */ * ***** END LICENSE BLOCK ***** */
const EXPORTED_SYMBOLS = ['SyncCore', 'BookmarksSyncCore']; const EXPORTED_SYMBOLS = ['SyncCore', 'BookmarksSyncCore', 'HistorySyncCore'];
const Cc = Components.classes; const Cc = Components.classes;
const Ci = Components.interfaces; const Ci = Components.interfaces;
const Cr = Components.results; const Cr = Components.results;
const Cu = Components.utils; const Cu = Components.utils;
addModuleAlias("weave", "{340c2bbc-ce74-4362-90b5-7c26312808ef}"); Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://weave/log4moz.js"); Cu.import("resource://weave/log4moz.js");
Cu.import("resource://weave/constants.js"); Cu.import("resource://weave/constants.js");
Cu.import("resource://weave/util.js"); Cu.import("resource://weave/util.js");
@ -332,7 +332,6 @@ BookmarksSyncCore.prototype = {
return this.__bms; return this.__bms;
}, },
// NOTE: Needs to be subclassed
_itemExists: function BSC__itemExists(GUID) { _itemExists: function BSC__itemExists(GUID) {
return this._bms.getItemIdForGUID(GUID) >= 0; return this._bms.getItemIdForGUID(GUID) >= 0;
}, },
@ -396,18 +395,23 @@ BookmarksSyncCore.prototype = {
}; };
BookmarksSyncCore.prototype.__proto__ = new SyncCore(); BookmarksSyncCore.prototype.__proto__ = new SyncCore();
function addModuleAlias(alias, extensionId) { function HistorySyncCore() {
let ioSvc = Cc["@mozilla.org/network/io-service;1"] this._init();
.getService(Ci.nsIIOService);
let resProt = ioSvc.getProtocolHandler("resource")
.QueryInterface(Ci.nsIResProtocolHandler);
if (!resProt.hasSubstitution(alias)) {
let extMgr = Cc["@mozilla.org/extensions/manager;1"]
.getService(Ci.nsIExtensionManager);
let loc = extMgr.getInstallLocation(extensionId);
let extD = loc.getItemLocation(extensionId);
extD.append("modules");
resProt.setSubstitution(alias, ioSvc.newFileURI(extD));
}
} }
HistorySyncCore.prototype = {
_logName: "HistSync",
_itemExists: function BSC__itemExists(GUID) {
// we don't care about already-existing items; just try to re-add them
return false;
},
_commandLike: function BSC_commandLike(a, b) {
// History commands never qualify for likeness. We will always
// take the union of all client/server items. We use the URL as
// the GUID, so the same sites will map to the same item (same
// GUID), without our intervention.
return false;
}
};
HistorySyncCore.prototype.__proto__ = new SyncCore();

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

@ -42,11 +42,10 @@ const Ci = Components.interfaces;
const Cr = Components.results; const Cr = Components.results;
const Cu = Components.utils; const Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://weave/constants.js"); Cu.import("resource://weave/constants.js");
Cu.import("resource://weave/log4moz.js"); Cu.import("resource://weave/log4moz.js");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
/* /*
* Utility functions * Utility functions
*/ */

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

@ -41,6 +41,7 @@ const Ci = Components.interfaces;
const Cr = Components.results; const Cr = Components.results;
const Cu = Components.utils; const Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://weave/service.js"); Cu.import("resource://weave/service.js");
let Weave = {}; let Weave = {};