Bug 852041 - Part 2: Use BookmarkJSONUtils.exportToFile in services/sync. r=rnewman
authorRaymond Lee <raymond@raysquare.com>
Tue, 09 Apr 2013 17:15:34 +0800
changeset 128314 7b3b57c68f998929243c068a743fd26411ad228a
parent 128313 d946c9ecdea6e72f3788aaa53640b5c3fdfeb98c
child 128315 a3bdc9a0259fe7a3dacf8ba64c4e0bf8daed11ff
push idunknown
push userunknown
push dateunknown
reviewersrnewman
bugs852041
milestone23.0a1
Bug 852041 - Part 2: Use BookmarkJSONUtils.exportToFile in services/sync. r=rnewman
services/sync/modules/engines/bookmarks.js
services/sync/tests/unit/test_bookmark_engine.js
--- a/services/sync/modules/engines/bookmarks.js
+++ b/services/sync/modules/engines/bookmarks.js
@@ -12,16 +12,17 @@ const Cu = Components.utils;
 
 Cu.import("resource://gre/modules/PlacesUtils.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://services-common/async.js");
 Cu.import("resource://services-sync/constants.js");
 Cu.import("resource://services-sync/engines.js");
 Cu.import("resource://services-sync/record.js");
 Cu.import("resource://services-sync/util.js");
+Cu.import("resource://gre/modules/Task.jsm");
 
 const ALLBOOKMARKS_ANNO    = "AllBookmarks";
 const DESCRIPTION_ANNO     = "bookmarkProperties/description";
 const SIDEBAR_ANNO         = "bookmarkProperties/loadInSidebar";
 const MOBILEROOT_ANNO      = "mobile/bookmarksRoot";
 const MOBILE_ANNO          = "MobileBookmarks";
 const EXCLUDEBACKUP_ANNO   = "places/excludeFromBackup";
 const SMART_BOOKMARKS_ANNO = "Places/SmartBookmark";
@@ -338,40 +339,45 @@ BookmarksEngine.prototype = {
     
     this._log.trace("No dupe found for key " + key + "/" + altKey + ".");
     return undefined;
   },
 
   _syncStartup: function _syncStart() {
     SyncEngine.prototype._syncStartup.call(this);
 
-    // For first-syncs, make a backup for the user to restore
-    if (this.lastSync == 0) {
-      PlacesUtils.archiveBookmarksFile(null, true);
-    }
+    let cb = Async.makeSpinningCallback();
+    Task.spawn(function() {
+      // For first-syncs, make a backup for the user to restore
+      if (this.lastSync == 0) {
+        yield PlacesUtils.archiveBookmarksFile(null, true);
+      }
 
-    this.__defineGetter__("_guidMap", function() {
-      // Create a mapping of folder titles and separator positions to GUID.
-      // We do this lazily so that we don't do any work unless we reconcile
-      // incoming items.
-      let guidMap;
-      try {
-        guidMap = this._buildGUIDMap();
-      } catch (ex) {
-        this._log.warn("Got exception \"" + Utils.exceptionStr(ex) +
-                       "\" building GUID map." +
-                       " Skipping all other incoming items.");
-        throw {code: Engine.prototype.eEngineAbortApplyIncoming,
-               cause: ex};
-      }
-      delete this._guidMap;
-      return this._guidMap = guidMap;
-    });
+      this.__defineGetter__("_guidMap", function() {
+        // Create a mapping of folder titles and separator positions to GUID.
+        // We do this lazily so that we don't do any work unless we reconcile
+        // incoming items.
+        let guidMap;
+        try {
+          guidMap = this._buildGUIDMap();
+        } catch (ex) {
+          this._log.warn("Got exception \"" + Utils.exceptionStr(ex) +
+                         "\" building GUID map." +
+                         " Skipping all other incoming items.");
+          throw {code: Engine.prototype.eEngineAbortApplyIncoming,
+                 cause: ex};
+        }
+        delete this._guidMap;
+        return this._guidMap = guidMap;
+      });
 
-    this._store._childrenToOrder = {};
+      this._store._childrenToOrder = {};
+      cb();
+    }.bind(this));
+    cb.wait();
   },
 
   _processIncoming: function (newitems) {
     try {
       SyncEngine.prototype._processIncoming.call(this, newitems);
     } finally {
       // Reorder children.
       this._tracker.ignoreAll = true;
@@ -1266,25 +1272,29 @@ BookmarksStore.prototype = {
     for each (let guid in kSpecialIds.guids) {
       if (guid != "places" && guid != "tags")
         this._getChildren(guid, items);
     }
     return items;
   },
 
   wipe: function BStore_wipe() {
-    // Save a backup before clearing out all bookmarks.
-    PlacesUtils.archiveBookmarksFile(null, true);
-
-    for each (let guid in kSpecialIds.guids)
-      if (guid != "places") {
-        let id = kSpecialIds.specialIdForGUID(guid);
-        if (id)
-          PlacesUtils.bookmarks.removeFolderChildren(id);
-      }
+    let cb = Async.makeSpinningCallback();
+    Task.spawn(function() {
+      // Save a backup before clearing out all bookmarks.
+      yield PlacesUtils.archiveBookmarksFile(null, true);
+      for each (let guid in kSpecialIds.guids)
+        if (guid != "places") {
+          let id = kSpecialIds.specialIdForGUID(guid);
+          if (id)
+            PlacesUtils.bookmarks.removeFolderChildren(id);
+        }
+      cb();
+    });
+    cb.wait();
   }
 };
 
 function BookmarksTracker(name, engine) {
   Tracker.call(this, name, engine);
 
   Svc.Obs.add("places-shutdown", this);
   Svc.Obs.add("weave:engine:start-tracking", this);
--- a/services/sync/tests/unit/test_bookmark_engine.js
+++ b/services/sync/tests/unit/test_bookmark_engine.js
@@ -1,19 +1,21 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 Cu.import("resource://gre/modules/PlacesUtils.jsm");
+Cu.import("resource://gre/modules/BookmarkJSONUtils.jsm");
 Cu.import("resource://services-common/async.js");
 Cu.import("resource://services-common/log4moz.js");
 Cu.import("resource://services-sync/engines.js");
 Cu.import("resource://services-sync/engines/bookmarks.js");
 Cu.import("resource://services-sync/service.js");
 Cu.import("resource://services-sync/util.js");
 Cu.import("resource://testing-common/services/sync/utils.js");
+Cu.import("resource://gre/modules/commonjs/sdk/core/promise.js");
 
 Service.engineManager.register(BookmarksEngine);
 var syncTesting = new SyncTestingInfrastructure();
 
 add_test(function bad_record_allIDs() {
   let syncTesting = new SyncTestingInfrastructure();
 
   _("Ensure that bad Places queries don't cause an error in getAllIDs.");
@@ -157,17 +159,17 @@ add_test(function test_processIncoming_e
   } finally {
     store.wipe();
     Svc.Prefs.resetBranch("");
     Service.recordManager.clearCache();
     server.stop(run_next_test);
   }
 });
 
-add_test(function test_restorePromptsReupload() {
+add_task(function test_restorePromptsReupload() {
   _("Ensure that restoring from a backup will reupload all records.");
   new SyncTestingInfrastructure();
 
   let engine = new BookmarksEngine(Service);
   let store  = engine._store;
   let server = serverForFoo(engine);
 
   let collection = server.user("foo").collection("bookmarks");
@@ -196,17 +198,17 @@ add_test(function test_restorePromptsReu
 
     let backupFile = dirSvc.get("TmpD", Ci.nsILocalFile);
 
     _("Make a backup.");
     backupFile.append("t_b_e_" + Date.now() + ".json");
 
     _("Backing up to file " + backupFile.path);
     backupFile.create(Ci.nsILocalFile.NORMAL_FILE_TYPE, 0600);
-    PlacesUtils.backupBookmarksToFile(backupFile);
+    yield BookmarkJSONUtils.exportToFile(backupFile);
 
     _("Create a different record and sync.");
     let bmk2_id = PlacesUtils.bookmarks.insertBookmark(
       folder1_id, tburi, PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Thunderbird!");
     let bmk2_guid = store.GUIDForId(bmk2_id);
     _("Get Thunderbird!: " + bmk2_id + ", " + bmk2_guid);
 
     PlacesUtils.bookmarks.removeItem(bmk1_id);
@@ -284,17 +286,19 @@ add_test(function test_restorePromptsReu
     _("Our old friend Folder 1 is still in play.");
     do_check_eq(folderWBOs.length, 1);
     do_check_eq(folderWBOs[0].title, "Folder 1");
 
   } finally {
     store.wipe();
     Svc.Prefs.resetBranch("");
     Service.recordManager.clearCache();
-    server.stop(run_next_test);
+    let deferred = Promise.defer();
+    server.stop(deferred.resolve);
+    yield deferred.promise;
   }
 });
 
 function FakeRecord(constructor, r) {
   constructor.call(this, "bookmarks", r.id);
   for (let x in r) {
     this[x] = r[x];
   }