CloudServices/Sync/FxSync/Developer/ClientAPI: Difference between revisions

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 class ==
== 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 in the case of the bookmark engine, a single browser tab in the case of the tab engine, and so on.
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 a class called <tt>CryptoWrapper</tt> defined in <tt>services/sync/modules/base_records/crypto.js</tt>, which handles all the encryption and decryption of your record for you.  All you have to do is write a class that extends <tt>CryptoWrapper</tt> and maintains a property called <tt>cleartext</tt>.  <tt>cleartext</tt> must be a JSON-dictionary-style object; put into it all values that you want to have encrypted, stored on the server, decrypted, and synced up.
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 Record class.
You may find it useful to write getters and setters for various properties of your record implementation.


The skeleton of a sample Record class:
The skeleton of a sample record implementation:


<pre>
<pre>
  function FooRecord(uri) {
  function FooRecord(collection, id) {
   CryptoWrapper.call(this, uri);
   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>


canmove, Confirmed users
725

edits