Bug 718238 - Part 3: correct handling of deletion. Generate valid records for deleted bookmarks and history items. r=nalexander
authorRichard Newman <rnewman@mozilla.com>
Thu, 23 Feb 2012 08:14:05 -0800
changeset 87547 e24cbd1e7bd02bc193212f1471584035978e647e
parent 87546 91fe68e377fb358c0cd03523a41ad36edd54334d
child 87548 592043eae457788454fb5cdf09a45f8c99226e75
push id22130
push userrnewman@mozilla.com
push dateFri, 24 Feb 2012 02:35:54 +0000
treeherdermozilla-central@d23600a1d4a7 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnalexander
bugs718238
milestone13.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 718238 - Part 3: correct handling of deletion. Generate valid records for deleted bookmarks and history items. r=nalexander
mobile/android/base/sync/repositories/domain/BookmarkRecord.java
mobile/android/base/sync/repositories/domain/HistoryRecord.java
mobile/android/base/sync/repositories/domain/Record.java
--- a/mobile/android/base/sync/repositories/domain/BookmarkRecord.java
+++ b/mobile/android/base/sync/repositories/domain/BookmarkRecord.java
@@ -145,19 +145,23 @@ public class BookmarkRecord extends Reco
   @Override
   public void initFromPayload(CryptoRecord payload) {
     ExtendedJSONObject p = payload.payload;
 
     // All.
     this.guid = payload.guid;
     checkGUIDs(p);
 
+    final Object del = p.get("deleted");
+    if (del instanceof Boolean) {
+      this.deleted = (Boolean) del;
+    }
+
     this.collection    = payload.collection;
     this.lastModified  = payload.lastModified;
-    this.deleted       = payload.deleted;
 
     this.type          = (String) p.get("type");
     this.title         = (String) p.get("title");
     this.description   = (String) p.get("description");
     this.parentID      = (String) p.get("parentid");
     this.parentName    = (String) p.get("parentName");
 
     // Bookmark.
@@ -204,28 +208,33 @@ public class BookmarkRecord extends Reco
     return AndroidBrowserBookmarksDataAccessor.TYPE_FOLDER.equalsIgnoreCase(this.type);
   }
 
   @Override
   public CryptoRecord getPayload() {
     CryptoRecord rec = new CryptoRecord(this);
     rec.payload = new ExtendedJSONObject();
     rec.payload.put("id", this.guid);
-    rec.payload.put("type", this.type);
-    rec.payload.put("title", this.title);
-    rec.payload.put("description", this.description);
-    rec.payload.put("parentid", this.parentID);
-    rec.payload.put("parentName", this.parentName);
-    if (isBookmark()) {
-      rec.payload.put("bmkUri", bookmarkURI);
-      rec.payload.put("keyword", keyword);
-      rec.payload.put("tags", this.tags);
-    }
-    if (isFolder()) {
-      rec.payload.put("children", this.children);
+
+    if (this.deleted) {
+      rec.payload.put("deleted", true);
+    } else {
+      putPayload(rec, "type", this.type);
+      putPayload(rec, "title", this.title);
+      putPayload(rec, "description", this.description);
+      putPayload(rec, "parentid", this.parentID);
+      putPayload(rec, "parentName", this.parentName);
+
+      if (isBookmark()) {
+        rec.payload.put("bmkUri", bookmarkURI);
+        rec.payload.put("keyword", keyword);
+        rec.payload.put("tags", this.tags);
+      } else if (isFolder()) {
+        rec.payload.put("children", this.children);
+      }
     }
     return rec;
   }
 
   private void trace(String s) {
     Logger.trace(LOG_TAG, s);
   }
 
--- a/mobile/android/base/sync/repositories/domain/HistoryRecord.java
+++ b/mobile/android/base/sync/repositories/domain/HistoryRecord.java
@@ -111,37 +111,45 @@ public class HistoryRecord extends Recor
   @Override
   public void initFromPayload(CryptoRecord payload) {
     ExtendedJSONObject p = payload.payload;
 
     this.guid = payload.guid;
     this.checkGUIDs(p);
 
     this.lastModified  = payload.lastModified;
-    this.deleted       = payload.deleted;
+    final Object del = p.get("deleted");
+    if (del instanceof Boolean) {
+      this.deleted = (Boolean) del;
+    }
 
     this.histURI = (String) p.get("histUri");
     this.title   = (String) p.get("title");
     try {
       this.visits = p.getArray("visits");
     } catch (NonArrayJSONException e) {
       Logger.error(LOG_TAG, "Got non-array visits in history record " + this.guid, e);
       this.visits = new JSONArray();
     }
   }
 
   @Override
   public CryptoRecord getPayload() {
     CryptoRecord rec = new CryptoRecord(this);
     rec.payload = new ExtendedJSONObject();
     Logger.debug(LOG_TAG, "Getting payload for history record " + this.guid + " (" + this.guid.length() + ").");
-    rec.payload.put("id",      this.guid);
-    rec.payload.put("title",   this.title);
-    rec.payload.put("histUri", this.histURI);             // TODO: encoding?
-    rec.payload.put("visits",  this.visits);
+
+    if (this.deleted) {
+      rec.payload.put("deleted", true);
+    } else {
+      putPayload(rec, "id",      this.guid);
+      putPayload(rec, "title",   this.title);
+      putPayload(rec, "histUri", this.histURI);             // TODO: encoding?
+      rec.payload.put("visits",  this.visits);
+    }
     return rec;
   }
 
 
   /**
    * We consider two history records to be congruent if they represent the
    * same history record regardless of visits.
    */
--- a/mobile/android/base/sync/repositories/domain/Record.java
+++ b/mobile/android/base/sync/repositories/domain/Record.java
@@ -235,16 +235,30 @@ public abstract class Record {
     try {
       return this.toJSONString().getBytes("UTF-8");
     } catch (UnsupportedEncodingException e) {
       // Can't happen.
       return null;
     }
   }
 
+  /**
+   * Utility for safely populating an output CryptoRecord.
+   *
+   * @param rec
+   * @param key
+   * @param value
+   */
+  protected void putPayload(CryptoRecord rec, String key, String value) {
+    if (value == null) {
+      return;
+    }
+    rec.payload.put(key, value);
+  }
+
   protected void checkGUIDs(ExtendedJSONObject payload) {
     String payloadGUID = (String) payload.get("id");
     if (this.guid == null ||
         payloadGUID == null) {
       String detailMessage = "Inconsistency: either envelope or payload GUID missing.";
       throw new IllegalStateException(detailMessage);
     }
     if (!this.guid.equals(payloadGUID)) {