Merge weave to weave-partial-view.

This commit is contained in:
Edward Lee 2009-09-10 12:42:29 -07:00
Родитель 695967c675 554e7266d1
Коммит a364a3c310
8 изменённых файлов: 43 добавлений и 123 удалений

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

@ -59,7 +59,6 @@ Collection.prototype = {
_Coll_init: function Coll_init(uri) {
this._init(uri);
this.pushFilter(new JsonFilter());
this._full = true;
this._ids = null;
this._older = 0;

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

@ -176,7 +176,7 @@ PubKeyManager.prototype = {
uploadKeypair: function PubKeyManager_uploadKeypair(keys) {
for each (let key in keys)
new Resource(key.uri).put(key.serialize());
new Resource(key.uri).put(key);
}
};

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

@ -79,21 +79,8 @@ WBORecord.prototype = {
return 0;
},
serialize: function WBORec_serialize() {
// Convert the payload into a string because the server expects that
let payload = this.payload;
this.payload = this.deleted ? "" : JSON.stringify(payload);
let ret = JSON.stringify(this.data);
// Restore the original payload
this.payload = payload;
return ret;
},
deserialize: function WBORec_deserialize(json) {
this.data = JSON.parse(json);
deserialize: function deserialize(json) {
this.data = json.constructor.toString() == String ? JSON.parse(json) : json;
// Empty string payloads are deleted records
if (this.payload === "")
@ -102,6 +89,16 @@ WBORecord.prototype = {
this.payload = JSON.parse(this.payload);
},
toJSON: function toJSON() {
// Copy fields from data to except payload which needs to be a string
let obj = {};
for (let [key, val] in Iterator(this.data))
if (key != "payload")
obj[key] = val;
obj.payload = this.deleted ? "" : JSON.stringify(this.payload);
return obj;
},
toString: function WBORec_toString() "{ " + [
"id: " + this.id,
"parent: " + this.parentid,

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

@ -343,7 +343,7 @@ SyncEngine.prototype = {
meta.generateIV();
meta.addUnwrappedKey(pubkey, symkey);
let res = new Resource(meta.uri);
let resp = res.put(meta.serialize());
let resp = res.put(meta);
if (!resp.success)
throw resp;
@ -557,7 +557,7 @@ SyncEngine.prototype = {
this._log.trace("Outgoing: " + out);
out.encrypt(ID.get("WeaveCryptoID"));
up.pushData(JSON.parse(out.serialize())); // FIXME: inefficient
up.pushData(out);
// Partial upload
if ((++count % MAX_UPLOAD_RECORDS) == 0)

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

@ -35,7 +35,7 @@
*
* ***** END LICENSE BLOCK ***** */
const EXPORTED_SYMBOLS = ['Resource', 'JsonFilter'];
const EXPORTED_SYMBOLS = ["Resource"];
const Cc = Components.classes;
const Ci = Components.interfaces;
@ -125,29 +125,12 @@ Resource.prototype = {
this._data = value;
},
// ** {{{ Resource.filters }}} **
//
// Filters are used to perform pre and post processing on
// requests made for resources. Use these methods to add,
// remove and clear filters applied to the resource.
_filters: null,
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(uri) {
this._log = Log4Moz.repository.getLogger(this._logName);
this._log.level =
Log4Moz.Level[Utils.prefs.getCharPref("log.logger.network.resources")];
this.uri = uri;
this._headers = {'Content-type': 'text/plain'};
this._filters = [];
},
// ** {{{ Resource._createRequest }}} **
@ -181,26 +164,6 @@ Resource.prototype = {
_onProgress: function Res__onProgress(channel) {},
// ** {{{ Resource.filterUpload }}} **
//
// Apply pre-request filters. Currently, this is done before
// any PUT request.
filterUpload: function Resource_filterUpload() {
this._data = this._filters.reduce(function(data, filter) {
return filter.beforePUT(data);
}, this._data);
},
// ** {{{ Resource.filterDownload }}} **
//
// Apply post-request filters. Currently, this done after
// any GET request.
filterDownload: function Resource_filterDownload() {
this._data = this._filters.reduceRight(function(data, filter) {
return filter.afterGET(data);
}, this._data);
},
// ** {{{ Resource._request }}} **
//
// Perform a particular HTTP request on the resource. This method
@ -216,7 +179,10 @@ Resource.prototype = {
// PUT and POST are trreated differently because
// they have payload data.
if ("PUT" == action || "POST" == action) {
this.filterUpload();
// Convert non-string bodies into JSON
if (this._data.constructor.toString() != String)
this._data = JSON.stringify(this._data);
this._log.debug(action + " Length: " + this._data.length);
this._log.trace(action + " Body: " + this._data);
@ -271,14 +237,8 @@ Resource.prototype = {
if (success) {
this._log.debug(action + " success: " + status);
switch (action) {
case "GET":
case "POST":
if (this._log.level <= Log4Moz.Level.Trace)
this._log.trace(action + " Body: " + this._data);
this.filterDownload();
break;
}
if (this._log.level <= Log4Moz.Level.Trace)
this._log.trace(action + " Body: " + this._data);
}
else
this._log.debug(action + " fail: " + status + " " + this._data);
@ -292,6 +252,10 @@ Resource.prototype = {
ret.headers = headers;
ret.status = status;
ret.success = success;
// Make a lazy getter to convert the json response into an object
Utils.lazy2(ret, "obj", function() JSON.parse(ret));
return ret;
},
@ -364,30 +328,6 @@ ChannelListener.prototype = {
}
};
// = JsonFilter =
//
// Currently, the only filter used in conjunction with
// {{{Resource.filters}}}. It simply encodes outgoing records
// as JSON, and decodes incoming JSON into JS objects.
function JsonFilter() {
let level = "Debug";
try { level = Utils.prefs.getCharPref("log.logger.network.jsonFilter"); }
catch (e) { /* ignore unset prefs */ }
this._log = Log4Moz.repository.getLogger("Net.JsonFilter");
this._log.level = Log4Moz.Level[level];
}
JsonFilter.prototype = {
beforePUT: function JsonFilter_beforePUT(data) {
this._log.trace("Encoding data as JSON");
return JSON.stringify(data);
},
afterGET: function JsonFilter_afterGET(data) {
this._log.trace("Decoding JSON data");
return JSON.parse(data);
}
};
// = badCertListener =
//
// We use this listener to ignore bad HTTPS

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

@ -548,7 +548,7 @@ WeaveSvc.prototype = {
privkey.payload.iv, newphrase);
privkey.payload.keyData = newkey;
let resp = new Resource(privkey.uri).put(privkey.serialize());
let resp = new Resource(privkey.uri).put(privkey);
if (!resp.success)
throw resp;
@ -1076,9 +1076,8 @@ WeaveSvc.prototype = {
throw "aborting sync, failed to get collections";
// Convert the response to an object and read out the modified times
info = JSON.parse(info);
for each (let engine in [Clients].concat(Engines.getEnabled()))
engine.lastModified = info[engine.name] || 0;
engine.lastModified = info.obj[engine.name] || 0;
this._log.debug("Refreshing client list");
Clients.sync();
@ -1201,7 +1200,7 @@ WeaveSvc.prototype = {
this._log.debug("Setting meta payload storage version to " + WEAVE_VERSION);
meta.payload.storageVersion = WEAVE_VERSION;
let resp = new Resource(meta.uri).put(meta.serialize());
let resp = new Resource(meta.uri).put(meta);
if (!resp.success)
throw resp;
},
@ -1214,13 +1213,9 @@ WeaveSvc.prototype = {
*/
wipeServer: function WeaveSvc_wipeServer(engines)
this._catch(this._notify("wipe-server", "", function() {
// Grab all the collections for the user
let res = new Resource(this.infoURL);
res.get();
// Get the array of collections and delete each one
let allCollections = JSON.parse(res.data);
for (let name in allCollections) {
// Grab all the collections for the user and delete each one
let info = new Resource(this.infoURL).get();
for (let name in info.obj) {
try {
// If we have a list of engines, make sure it's one we want
if (engines && engines.indexOf(name) == -1)

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

@ -56,32 +56,21 @@ ClientRecord.prototype = {
this._WBORec_init(uri);
},
_escape: function ClientRecord__escape(toAscii) {
// Escape-to-ascii or unescape-from-ascii each value
if (this.payload != null)
for (let [key, val] in Iterator(this.payload))
this.payload[key] = (toAscii ? escape : unescape)(val);
},
serialize: function ClientRecord_serialize() {
// Convert potential non-ascii to ascii before serializing
this._escape(true);
let ret = WBORecord.prototype.serialize.apply(this, arguments);
// Restore the original data for normal use
this._escape(false);
return ret;
},
deserialize: function ClientRecord_deserialize(json) {
// Deserialize then convert potential escaped non-ascii
WBORecord.prototype.deserialize.apply(this, arguments);
this._escape(false);
let data = JSON.parse(json, function(key, val) key == "payload" ?
unescape(val) : val);
WBORecord.prototype.deserialize.call(this, data);
},
toJSON: function toJSON() {
let obj = WBORecord.prototype.toJSON.call(this);
obj.payload = escape(obj.payload);
return obj;
},
// engines.js uses cleartext to determine if records _isEqual
// XXX Bug 482669 Implement .equals() for SyncEngine to compare records
get cleartext() this.serialize(),
get cleartext() JSON.stringify(this),
// XXX engines.js calls encrypt/decrypt for all records, so define these:
encrypt: function ClientRecord_encrypt(passphrase) {},

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

@ -11,7 +11,7 @@ function run_test() {
do_check_eq(record.id, "ascii");
do_check_eq(record.payload.name, "wéävê");
let serialized = record.serialize();
let serialized = JSON.stringify(record);
let checkCount = 0;
_("Checking for all ASCII:", serialized);
Array.forEach(serialized, function(ch) {