2008-05-28 20:11:39 -07:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Bookmarks Sync.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2007
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Dan Mills <thunder@mozilla.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
const EXPORTED_SYMBOLS = ['Resource', 'RemoteStore'];
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://weave/log4moz.js");
|
|
|
|
Cu.import("resource://weave/constants.js");
|
|
|
|
Cu.import("resource://weave/util.js");
|
2008-05-30 20:43:55 -07:00
|
|
|
Cu.import("resource://weave/crypto.js");
|
2008-05-28 20:11:39 -07:00
|
|
|
Cu.import("resource://weave/async.js");
|
2008-06-01 19:10:11 -07:00
|
|
|
Cu.import("resource://weave/identity.js");
|
2008-05-28 20:11:39 -07:00
|
|
|
Cu.import("resource://weave/dav.js");
|
|
|
|
Cu.import("resource://weave/stores.js");
|
|
|
|
|
|
|
|
Function.prototype.async = Async.sugar;
|
|
|
|
|
|
|
|
|
|
|
|
function RequestException(resource, action, request) {
|
|
|
|
this._resource = resource;
|
|
|
|
this._action = action;
|
|
|
|
this._request = request;
|
|
|
|
}
|
|
|
|
RequestException.prototype = {
|
|
|
|
get resource() { return this._resource; },
|
|
|
|
get action() { return this._action; },
|
|
|
|
get request() { return this._request; },
|
|
|
|
get status() { return this._request.status; },
|
|
|
|
toString: function ReqEx_toString() {
|
|
|
|
return "Could not " + this._action + " resource " + this._resource.path +
|
|
|
|
" (" + this._request.status + ")";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function Resource(path) {
|
2008-05-30 20:43:55 -07:00
|
|
|
this._init(path);
|
2008-05-28 20:11:39 -07:00
|
|
|
}
|
|
|
|
Resource.prototype = {
|
|
|
|
get identity() { return this._identity; },
|
|
|
|
set identity(value) { this._identity = value; },
|
|
|
|
|
|
|
|
get dav() { return this._dav; },
|
|
|
|
set dav(value) { this._dav = value; },
|
|
|
|
|
|
|
|
get path() { return this._path; },
|
|
|
|
set path(value) {
|
|
|
|
this._dirty = true;
|
|
|
|
this._path = value;
|
|
|
|
},
|
|
|
|
|
|
|
|
get data() { return this._data; },
|
|
|
|
set data(value) {
|
|
|
|
this._dirty = true;
|
|
|
|
this._data = value;
|
|
|
|
},
|
|
|
|
|
2008-05-30 20:43:55 -07:00
|
|
|
get lastRequest() { return this._lastRequest; },
|
2008-05-28 20:11:39 -07:00
|
|
|
get downloaded() { return this._downloaded; },
|
|
|
|
get dirty() { return this._dirty; },
|
|
|
|
|
2008-05-30 20:43:55 -07:00
|
|
|
pushFilter: function Res_pushFilter(filter) {
|
|
|
|
this._filters.push(filter);
|
|
|
|
},
|
|
|
|
|
|
|
|
popFilter: function Res_popFilter() {
|
|
|
|
return this._filters.pop();
|
|
|
|
},
|
|
|
|
|
|
|
|
clearFilters: function Res_clearFilters() {
|
|
|
|
this._filters = [];
|
|
|
|
},
|
|
|
|
|
|
|
|
_init: function Res__init(path) {
|
|
|
|
this._identity = null; // unused
|
|
|
|
this._dav = null; // unused
|
|
|
|
this._path = path;
|
|
|
|
this._data = null;
|
|
|
|
this._downloaded = false;
|
|
|
|
this._dirty = false;
|
|
|
|
this._filters = [];
|
|
|
|
this._lastRequest = null;
|
|
|
|
this._log = Log4Moz.Service.getLogger("Service.Resource");
|
|
|
|
},
|
|
|
|
|
2008-06-01 19:10:11 -07:00
|
|
|
// note: this is unused, and it's not clear whether it's useful or not
|
2008-05-28 20:11:39 -07:00
|
|
|
_sync: function Res__sync() {
|
|
|
|
let self = yield;
|
|
|
|
let ret;
|
|
|
|
|
|
|
|
// If we've set the locally stored value, upload it. If we
|
|
|
|
// haven't, and we haven't yet downloaded this resource, then get
|
|
|
|
// it. Otherwise do nothing (don't try to get it every time)
|
|
|
|
|
|
|
|
if (this.dirty) {
|
|
|
|
this.put(self.cb, this.data);
|
|
|
|
ret = yield;
|
|
|
|
|
|
|
|
} else if (!this.downloaded) {
|
|
|
|
this.get(self.cb);
|
|
|
|
ret = yield;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.done(ret);
|
|
|
|
},
|
|
|
|
sync: function Res_sync(onComplete) {
|
|
|
|
this._sync.async(this, onComplete);
|
|
|
|
},
|
|
|
|
|
|
|
|
_request: function Res__request(action, data) {
|
|
|
|
let self = yield;
|
|
|
|
let listener, timer;
|
|
|
|
let iter = 0;
|
2008-05-30 20:43:55 -07:00
|
|
|
|
|
|
|
if ("PUT" == action) {
|
|
|
|
for each (let filter in this._filters) {
|
|
|
|
filter.beforePUT.async(filter, self.cb, data);
|
|
|
|
data = yield;
|
|
|
|
}
|
|
|
|
}
|
2008-05-28 20:11:39 -07:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
switch (action) {
|
|
|
|
case "GET":
|
|
|
|
DAV.GET(this.path, self.cb);
|
|
|
|
break;
|
|
|
|
case "PUT":
|
|
|
|
DAV.PUT(this.path, data, self.cb);
|
|
|
|
break;
|
|
|
|
case "DELETE":
|
|
|
|
DAV.DELETE(this.path, self.cb);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw "Unknown request action for Resource";
|
|
|
|
}
|
2008-05-30 20:43:55 -07:00
|
|
|
this._lastRequest = yield;
|
2008-05-28 20:11:39 -07:00
|
|
|
|
|
|
|
if (action == "DELETE" &&
|
2008-05-30 20:43:55 -07:00
|
|
|
Utils.checkStatus(this._lastRequest.status, null, [[200,300],404])) {
|
2008-05-28 20:11:39 -07:00
|
|
|
this._dirty = false;
|
|
|
|
this._data = null;
|
|
|
|
break;
|
|
|
|
|
2008-05-30 20:43:55 -07:00
|
|
|
} else if (Utils.checkStatus(this._lastRequest.status)) {
|
|
|
|
this._log.debug(action + " request successful");
|
2008-05-28 20:11:39 -07:00
|
|
|
this._dirty = false;
|
|
|
|
if (action == "GET")
|
2008-05-30 20:43:55 -07:00
|
|
|
this._data = this._lastRequest.responseText;
|
2008-05-28 20:11:39 -07:00
|
|
|
else if (action == "PUT")
|
|
|
|
this._data = data;
|
|
|
|
break;
|
|
|
|
|
2008-05-30 20:43:55 -07:00
|
|
|
} else if (action == "GET" && this._lastRequest.status == 404) {
|
|
|
|
throw new RequestException(this, action, this._lastRequest);
|
2008-05-28 20:11:39 -07:00
|
|
|
|
|
|
|
} else if (iter >= 10) {
|
|
|
|
// iter too big? bail
|
2008-05-30 20:43:55 -07:00
|
|
|
throw new RequestException(this, action, this._lastRequest);
|
2008-05-28 20:11:39 -07:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// wait for a bit and try again
|
|
|
|
if (!timer) {
|
|
|
|
listener = new Utils.EventListener(self.cb);
|
|
|
|
timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
}
|
|
|
|
timer.initWithCallback(listener, iter * 100, timer.TYPE_ONE_SHOT);
|
|
|
|
yield;
|
|
|
|
iter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-30 20:43:55 -07:00
|
|
|
if ("GET" == action) {
|
|
|
|
for each (let filter in this._filters.reverse()) {
|
|
|
|
filter.afterGET.async(filter, self.cb, this._data);
|
|
|
|
this._data = yield;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.done(this._data);
|
2008-05-28 20:11:39 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
get: function Res_get(onComplete) {
|
|
|
|
this._request.async(this, onComplete, "GET");
|
|
|
|
},
|
|
|
|
|
|
|
|
put: function Res_put(onComplete, data) {
|
|
|
|
this._request.async(this, onComplete, "PUT", data);
|
|
|
|
},
|
|
|
|
|
|
|
|
delete: function Res_delete(onComplete) {
|
|
|
|
this._request.async(this, onComplete, "DELETE");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-05-30 20:43:55 -07:00
|
|
|
function ResourceFilter() {
|
|
|
|
this._log = Log4Moz.Service.getLogger("Service.ResourceFilter");
|
|
|
|
}
|
|
|
|
ResourceFilter.prototype = {
|
|
|
|
beforePUT: function ResFilter_beforePUT(data) {
|
|
|
|
let self = yield;
|
|
|
|
this._log.debug("Doing absolutely nothing")
|
|
|
|
self.done(data);
|
|
|
|
},
|
|
|
|
afterGET: function ResFilter_afterGET(data) {
|
|
|
|
let self = yield;
|
|
|
|
this._log.debug("Doing absolutely nothing")
|
|
|
|
self.done(data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function JsonFilter() {
|
|
|
|
this._log = Log4Moz.Service.getLogger("Service.JsonFilter");
|
|
|
|
}
|
|
|
|
JsonFilter.prototype = {
|
|
|
|
__proto__: new ResourceFilter(),
|
|
|
|
|
|
|
|
get _json() {
|
|
|
|
let json = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
|
|
|
|
this.__defineGetter__("_json", function() json);
|
|
|
|
return this._json;
|
|
|
|
},
|
2008-05-28 20:11:39 -07:00
|
|
|
|
2008-05-30 20:43:55 -07:00
|
|
|
beforePUT: function JsonFilter_beforePUT(data) {
|
|
|
|
let self = yield;
|
|
|
|
this._log.debug("Encoding data as JSON")
|
|
|
|
self.done(this._json.encode(data));
|
|
|
|
},
|
|
|
|
|
|
|
|
afterGET: function JsonFilter_afterGET(data) {
|
|
|
|
let self = yield;
|
|
|
|
this._log.debug("Decoding JSON data")
|
|
|
|
self.done(this._json.decode(data));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// FIXME: doesn't use the crypto algorithm stored in the status file
|
|
|
|
function PBECryptoFilter(identity) {
|
|
|
|
this._identity = identity;
|
|
|
|
this._log = Log4Moz.Service.getLogger("Service.PBECryptoFilter");
|
2008-05-28 20:11:39 -07:00
|
|
|
}
|
2008-05-30 20:43:55 -07:00
|
|
|
PBECryptoFilter.prototype = {
|
|
|
|
__proto__: new ResourceFilter(),
|
2008-05-28 20:11:39 -07:00
|
|
|
|
2008-05-30 20:43:55 -07:00
|
|
|
beforePUT: function PBEFilter_beforePUT(data) {
|
2008-05-28 20:11:39 -07:00
|
|
|
let self = yield;
|
2008-05-30 20:43:55 -07:00
|
|
|
this._log.debug("Encrypting data")
|
2008-06-01 19:10:11 -07:00
|
|
|
Crypto.PBEencrypt.async(Crypto, self.cb, data, ID.get(this._identity));
|
2008-05-30 20:43:55 -07:00
|
|
|
let ret = yield;
|
|
|
|
self.done(ret);
|
2008-05-28 20:11:39 -07:00
|
|
|
},
|
2008-05-30 20:43:55 -07:00
|
|
|
|
|
|
|
afterGET: function PBEFilter_afterGET(data) {
|
|
|
|
let self = yield;
|
|
|
|
this._log.debug("Decrypting data")
|
2008-06-01 19:10:11 -07:00
|
|
|
Crypto.PBEdecrypt.async(Crypto, self.cb, data, ID.get(this._identity));
|
2008-05-30 20:43:55 -07:00
|
|
|
let ret = yield;
|
|
|
|
self.done(ret);
|
2008-05-28 20:11:39 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-05-30 20:43:55 -07:00
|
|
|
function RemoteStore(serverPrefix, cryptoId) {
|
2008-05-28 20:11:39 -07:00
|
|
|
this._prefix = serverPrefix;
|
2008-05-30 20:43:55 -07:00
|
|
|
this._cryptoId = cryptoId;
|
|
|
|
this._init();
|
2008-05-28 20:11:39 -07:00
|
|
|
}
|
|
|
|
RemoteStore.prototype = {
|
2008-06-01 19:10:11 -07:00
|
|
|
_init: function RemoteStore__init() {
|
2008-05-30 20:43:55 -07:00
|
|
|
if (!this._prefix || !this._cryptoId)
|
|
|
|
return;
|
|
|
|
let json = new JsonFilter();
|
|
|
|
let crypto = new PBECryptoFilter(this._cryptoId);
|
|
|
|
this._status = new Resource(this._prefix + "status.json");
|
|
|
|
this._status.pushFilter(json);
|
|
|
|
this._keys = new Resource(this._prefix + "keys.json");
|
|
|
|
this._keys.pushFilter(new JsonFilter());
|
|
|
|
this._snapshot = new Resource(this._prefix + "snapshot.json");
|
|
|
|
this._snapshot.pushFilter(json);
|
|
|
|
this._snapshot.pushFilter(crypto);
|
|
|
|
this._deltas = new Resource(this._prefix + "deltas.json");
|
|
|
|
this._deltas.pushFilter(json);
|
|
|
|
this._deltas.pushFilter(crypto);
|
2008-05-28 20:11:39 -07:00
|
|
|
},
|
2008-05-30 20:43:55 -07:00
|
|
|
|
|
|
|
get status() this._status,
|
|
|
|
get keys() this._keys,
|
|
|
|
get snapshot() this._snapshot,
|
|
|
|
get deltas() this._deltas,
|
|
|
|
|
|
|
|
get serverPrefix() this._prefix,
|
|
|
|
set serverPrefix(value) {
|
|
|
|
this._prefix = value;
|
|
|
|
this._init();
|
2008-05-28 20:11:39 -07:00
|
|
|
},
|
2008-05-30 20:43:55 -07:00
|
|
|
|
|
|
|
get cryptoId() this._cryptoId,
|
|
|
|
set cryptoId(value) {
|
|
|
|
this._cryptoId = value;
|
|
|
|
this._init();
|
2008-05-28 20:11:39 -07:00
|
|
|
}
|
|
|
|
};
|