canmove, Confirmed users
725
edits
Line 20: | Line 20: | ||
After implementing your objects, you'll have to register them with the Sync service. | After implementing your objects, you'll have to register them with the Sync service. | ||
== Writing a Record | == Writing a Record object == | ||
A Record object is a wrapper around a single instance of whatever data it is that you are syncing -- a single bookmark | A Record object is a wrapper around a single instance of whatever data it is that you are syncing -- a single bookmark, history URL, password or form data entry. For some sync engines, it makes sense to bundle all data into one record. The tabs and preferences engines work that way. | ||
There's | There's an object called <tt>CryptoWrapper</tt> defined in <tt>services/sync/modules/record.js</tt>, which handles all the encryption and decryption of your record for you. All you have to do is write an object that extends <tt>CryptoWrapper</tt> and maintains a property called <tt>cleartext</tt>. <tt>cleartext</tt> must be a JSON-able object. Put into it all values that you want to have encrypted, stored on the server, decrypted, and synced up. | ||
You may find it useful to write getters and setters for various properties of your | You may find it useful to write getters and setters for various properties of your record implementation. | ||
The skeleton of a sample | The skeleton of a sample record implementation: | ||
<pre> | <pre> | ||
function FooRecord( | function FooRecord(collection, id) { | ||
CryptoWrapper.call(this, | CryptoWrapper.call(this, collection, id); | ||
} | } | ||
FooRecord.prototype = { | FooRecord.prototype = { | ||
__proto__: CryptoWrapper.prototype, | __proto__: CryptoWrapper.prototype, | ||
_logName: "Record.Foo", | _logName: "Record.Foo", | ||
ttl: FOO_TTL, // optional | |||
get bar() this.cleartext.bar, | get bar() this.cleartext.bar, | ||
set bar(value) { | set bar(value) { | ||
this.cleartext.bar = value; | this.cleartext.bar = value; | ||
}, | |||
get baz() this.cleartext.baz, | |||
set baz(value) { | |||
this.cleartext.baz = value; | |||
} | } | ||
}; | }; | ||
</pre> | |||
To save all that typing for declaring the getters and setters, you can also use <tt>Utils.deferGetSet</tt>: | |||
<pre> | |||
function FooRecord(collection, id) { | |||
CryptoWrapper.call(this, collection, id); | |||
} | |||
FooRecord.prototype = { | |||
__proto__: CryptoWrapper.prototype, | |||
_logName: "Record.Foo", | |||
ttl: FOO_TTL // optional | |||
}; | |||
Utils.deferGetSet(FooRec, "cleartext", ["bar", "baz"]); | |||
</pre> | </pre> | ||