work with NewEngine
authorDan Mills <thunder@mozilla.com>
Thu, 20 Nov 2008 09:21:12 +0900
changeset 45044 49d05eee6f251045336deb49aaeebb9ef9ab297d
parent 45043 47ce51a48cfdd6b83e76d466ada6cb139167ca96
child 45045 cdeaa180d10eb0ddce87b704a7377a2b61863d46
push idunknown
push userunknown
push dateunknown
work with NewEngine
services/sync/modules/engines/bookmarks.js
--- a/services/sync/modules/engines/bookmarks.js
+++ b/services/sync/modules/engines/bookmarks.js
@@ -737,17 +737,17 @@ BookmarksEngine.prototype = {
     if (!this.__core)
       this.__core = new BookmarksSyncCore(this._store);
     return this.__core;
   },
 
   __tracker: null,
   get _tracker() {
     if (!this.__tracker)
-      this.__tracker = new BookmarksTracker();
+      this.__tracker = new BookmarksTracker(this);
     return this.__tracker;
   }
 
   // XXX for sharing, will need to re-add code to get new shares before syncing,
   //     and updating incoming/outgoing shared folders after syncing
 };
 
 function BookmarksSyncCore(store) {
@@ -903,16 +903,40 @@ BookmarksStore.prototype = {
     case "unfiled":
       return this._bms.unfiledBookmarksFolder;
     default:
       return this._bms.getItemIdForGUID(GUID);
     }
     return null;
   },
 
+  _applyIncoming: function BStore__applyIncoming(record) {
+    let self = yield;
+
+    if (!this._lookup)
+      this.wrap();
+
+    yield record.decrypt(self.cb, ID.get('WeaveCryptoID').password);
+
+    this._log.trace("RECORD: " + record.id + " -> " + uneval(record.cleartext));
+
+    if (!this._lookup[record.id])
+      this._createCommand({GUID: record.id, data: record.cleartext});
+    else if (record.cleartext == "")
+      this._removeCommand({GUID: record.id});
+    else {
+      let data = Utils.deepCopy(record.cleartext);
+      delete data.GUID;
+      this._editCommand({GUID: record.id, data: data});
+    }
+  },
+  applyIncoming: function BStore_applyIncoming(onComplete, record) {
+    this._applyIncoming.async(this, onComplete, record);
+  },
+
   _createCommand: function BStore__createCommand(command) {
     let newId;
     let parentId = this._getItemIdForGUID(command.data.parentGUID);
 
     if (parentId < 0) {
       this._log.warn("Creating node with unknown parent -> reparenting to root");
       parentId = this._bms.bookmarksMenuFolder;
     }
@@ -1346,50 +1370,66 @@ BookmarksStore.prototype = {
  * engine. How the engine decides to set the score is upto it,
  * as long as the value between 0 and 100 actually corresponds
  * to its urgency to sync.
  *
  * Here's an example BookmarksTracker. We don't subclass getScore
  * because the observer methods take care of updating _score which
  * getScore returns by default.
  */
-function BookmarksTracker() {
-  this._init();
+function BookmarksTracker(engine) {
+  this._init(engine);
 }
 BookmarksTracker.prototype = {
   __proto__: Tracker.prototype,
   _logName: "BMTracker",
 
-  /* We don't care about the first three */
-  onBeginUpdateBatch: function BMT_onBeginUpdateBatch() {
+  QueryInterface: XPCOMUtils.generateQI([Ci.nsINavBookmarkObserver]),
 
+  _init: function BMT__init(engine) {
+    this._engine = engine;
+    this._log = Log4Moz.repository.getLogger("Service." + this._logName);
+    this._score = 0;
+    Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
+      getService(Ci.nsINavBookmarksService).
+      addObserver(this, false);
   },
-  onEndUpdateBatch: function BMT_onEndUpdateBatch() {
+
+  // FIXME: not getting any events whatsoever!
+
+  /* Every add/remove/change is worth 10 points */
 
-  },
-  onItemVisited: function BMT_onItemVisited() {
-
+  onItemAdded: function BMT_onEndUpdateBatch(itemId) {
+    this._log.debug("Adding item to queue: " + itemId);
+    this._score += 10;
+    let all = this._engine._store.wrap();
+    let record = yield this._engine._createRecord.async(this, self.cb, key, all[itemId]);
+    this._engine.outgoing.push(record);
   },
 
-  /* Every add or remove is worth 4 points,
-   * on the basis that adding or removing 20 bookmarks
-   * means its time to sync?
-   */
-  onItemAdded: function BMT_onEndUpdateBatch() {
-    this._score += 4;
-  },
-  onItemRemoved: function BMT_onItemRemoved() {
-    this._score += 4;
-  },
-  /* Changes are worth 2 points? */
-  onItemChanged: function BMT_onItemChanged() {
-    this._score += 2;
+  onItemRemoved: function BMT_onItemRemoved(itemId) {
+    this._log.debug("Adding item to queue: " + itemId);
+    this._score += 10;
+    let all = this._engine._store.wrap();
+    let record = yield this._engine._createRecord.async(this, self.cb, key, all[itemId]);
+    this._engine.outgoing.push(record);
   },
 
-  _init: function BMT__init() {
-    this._log = Log4Moz.repository.getLogger("Service." + this._logName);
-    this._score = 0;
+  onItemChanged: function BMT_onItemChanged(itemId) {
+    this._log.debug("Adding item to queue: " + itemId);
+    this._score += 10;
+    let all = this._engine._store.wrap();
+    let record = yield this._engine._createRecord.async(this, self.cb, key, all[itemId]);
+    this._engine.outgoing.push(record);
+  },
 
-    Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
-    getService(Ci.nsINavBookmarksService).
-    addObserver(this, false);
-  }
+  onItemMoved: function BMT_onItemMoved(itemId) {
+    this._log.debug("Adding item to queue: " + itemId);
+    this._score += 10;
+    let all = this._engine._store.wrap();
+    let record = yield this._engine._createRecord.async(this, self.cb, key, all[itemId]);
+    this._engine.outgoing.push(record);
+  },
+
+  onBeginUpdateBatch: function BMT_onBeginUpdateBatch() {},
+  onEndUpdateBatch: function BMT_onEndUpdateBatch() {},
+  onItemVisited: function BMT_onItemVisited() {}
 };