2008-11-03 14:37:51 -08: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 Weave.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2008
|
|
|
|
* 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 ***** */
|
|
|
|
|
2009-02-11 23:53:37 -08:00
|
|
|
const EXPORTED_SYMBOLS = ['WBORecord', 'RecordManager', 'Records'];
|
2008-11-03 14:37:51 -08:00
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2010-06-16 14:30:08 -07:00
|
|
|
Cu.import("resource://services-sync/log4moz.js");
|
|
|
|
Cu.import("resource://services-sync/resource.js");
|
|
|
|
Cu.import("resource://services-sync/util.js");
|
2008-11-03 14:37:51 -08:00
|
|
|
|
2009-01-06 13:54:18 -08:00
|
|
|
function WBORecord(uri) {
|
2010-02-11 15:25:31 -08:00
|
|
|
this.data = {};
|
|
|
|
this.payload = {};
|
|
|
|
if (uri)
|
|
|
|
this.uri = uri;
|
2008-11-03 14:37:51 -08:00
|
|
|
}
|
|
|
|
WBORecord.prototype = {
|
|
|
|
_logName: "Record.WBO",
|
|
|
|
|
2009-01-27 13:35:10 -08:00
|
|
|
// NOTE: baseUri must have a trailing slash, or baseUri.resolve() will omit
|
|
|
|
// the collection name
|
2009-01-27 16:36:00 -08:00
|
|
|
get uri() {
|
|
|
|
return Utils.makeURI(this.baseUri.resolve(encodeURI(this.id)));
|
|
|
|
},
|
2009-01-27 13:35:10 -08:00
|
|
|
set uri(value) {
|
|
|
|
if (typeof(value) != "string")
|
|
|
|
value = value.spec;
|
|
|
|
let foo = value.split('/');
|
2009-06-04 15:30:36 -07:00
|
|
|
this.id = foo.pop();
|
2009-01-27 13:35:10 -08:00
|
|
|
this.baseUri = Utils.makeURI(foo.join('/') + '/');
|
|
|
|
},
|
|
|
|
|
2008-12-28 19:59:44 -08:00
|
|
|
get sortindex() {
|
|
|
|
if (this.data.sortindex)
|
|
|
|
return this.data.sortindex;
|
|
|
|
return 0;
|
|
|
|
},
|
2008-12-23 11:20:20 -08:00
|
|
|
|
2009-09-10 11:05:13 -07:00
|
|
|
deserialize: function deserialize(json) {
|
|
|
|
this.data = json.constructor.toString() == String ? JSON.parse(json) : json;
|
2009-04-03 10:38:47 -07:00
|
|
|
|
2010-03-16 16:31:56 -07:00
|
|
|
try {
|
|
|
|
// The payload is likely to be JSON, but if not, keep it as a string
|
2009-04-03 10:38:47 -07:00
|
|
|
this.payload = JSON.parse(this.payload);
|
2010-03-16 16:31:56 -07:00
|
|
|
}
|
|
|
|
catch(ex) {}
|
2009-01-27 13:35:10 -08:00
|
|
|
},
|
|
|
|
|
2009-09-10 11:05:13 -07:00
|
|
|
toJSON: function toJSON() {
|
2010-03-16 16:31:56 -07:00
|
|
|
// Copy fields from data to be stringified, making sure payload is a string
|
2009-09-10 11:05:13 -07:00
|
|
|
let obj = {};
|
|
|
|
for (let [key, val] in Iterator(this.data))
|
2010-03-16 16:31:56 -07:00
|
|
|
obj[key] = key == "payload" ? JSON.stringify(val) : val;
|
2009-09-10 11:05:13 -07:00
|
|
|
return obj;
|
|
|
|
},
|
|
|
|
|
2009-04-03 10:38:47 -07:00
|
|
|
toString: function WBORec_toString() "{ " + [
|
|
|
|
"id: " + this.id,
|
2009-08-15 00:56:27 -07:00
|
|
|
"index: " + this.sortindex,
|
2009-04-03 10:38:47 -07:00
|
|
|
"modified: " + this.modified,
|
2010-03-16 16:31:56 -07:00
|
|
|
"payload: " + JSON.stringify(this.payload)
|
2009-04-03 10:38:47 -07:00
|
|
|
].join("\n ") + " }",
|
2008-11-03 14:37:51 -08:00
|
|
|
};
|
2008-11-19 16:16:08 -08:00
|
|
|
|
2010-03-16 16:31:56 -07:00
|
|
|
Utils.deferGetSet(WBORecord, "data", ["id", "modified", "sortindex", "payload"]);
|
2009-04-13 14:39:29 -07:00
|
|
|
|
2009-02-11 23:53:37 -08:00
|
|
|
Utils.lazy(this, 'Records', RecordManager);
|
|
|
|
|
2009-01-27 13:35:10 -08:00
|
|
|
function RecordManager() {
|
2010-02-11 15:25:31 -08:00
|
|
|
this._log = Log4Moz.repository.getLogger(this._logName);
|
|
|
|
this._records = {};
|
2009-01-27 13:35:10 -08:00
|
|
|
}
|
|
|
|
RecordManager.prototype = {
|
|
|
|
_recordType: WBORecord,
|
|
|
|
_logName: "RecordMgr",
|
|
|
|
|
2009-06-04 16:25:12 -07:00
|
|
|
import: function RecordMgr_import(url) {
|
|
|
|
this._log.trace("Importing record: " + (url.spec ? url.spec : url));
|
|
|
|
try {
|
2009-08-26 15:32:46 -07:00
|
|
|
// Clear out the last response with empty object if GET fails
|
|
|
|
this.response = {};
|
|
|
|
this.response = new Resource(url).get();
|
2009-01-27 13:35:10 -08:00
|
|
|
|
2009-08-26 17:49:23 -07:00
|
|
|
// Don't parse and save the record on failure
|
|
|
|
if (!this.response.success)
|
|
|
|
return null;
|
|
|
|
|
2009-06-04 16:25:12 -07:00
|
|
|
let record = new this._recordType();
|
2009-08-26 15:32:46 -07:00
|
|
|
record.deserialize(this.response);
|
|
|
|
record.uri = url;
|
2009-01-27 13:35:10 -08:00
|
|
|
|
2009-06-04 16:25:12 -07:00
|
|
|
return this.set(url, record);
|
|
|
|
}
|
|
|
|
catch(ex) {
|
2009-07-24 15:28:42 -07:00
|
|
|
this._log.debug("Failed to import record: " + Utils.exceptionStr(ex));
|
2009-06-04 16:25:12 -07:00
|
|
|
return null;
|
|
|
|
}
|
2009-01-27 13:35:10 -08:00
|
|
|
},
|
|
|
|
|
2009-06-04 16:50:57 -07:00
|
|
|
get: function RecordMgr_get(url) {
|
2009-08-25 08:52:52 -07:00
|
|
|
// Use a url string as the key to the hash
|
2009-06-04 16:50:57 -07:00
|
|
|
let spec = url.spec ? url.spec : url;
|
|
|
|
if (spec in this._records)
|
|
|
|
return this._records[spec];
|
|
|
|
return this.import(url);
|
2009-01-27 13:35:10 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function RegordMgr_set(url, record) {
|
2009-03-07 11:04:42 -08:00
|
|
|
let spec = url.spec ? url.spec : url;
|
2009-06-04 16:25:12 -07:00
|
|
|
return this._records[spec] = record;
|
2009-01-27 13:35:10 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
contains: function RegordMgr_contains(url) {
|
2009-08-25 08:52:52 -07:00
|
|
|
if ((url.spec || url) in this._records)
|
2009-01-27 13:35:10 -08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2009-02-19 04:06:08 -08:00
|
|
|
clearCache: function recordMgr_clearCache() {
|
|
|
|
this._records = {};
|
|
|
|
},
|
|
|
|
|
2009-01-27 13:35:10 -08:00
|
|
|
del: function RegordMgr_del(url) {
|
|
|
|
delete this._records[url];
|
2008-11-19 16:16:08 -08:00
|
|
|
}
|
|
|
|
};
|