Bug 708151 - Change GUID generation to be more compliant with Sync (r=rnewman, a=mfinkle)
authorLucas Rocha <lucasr@mozilla.com>
Tue, 13 Dec 2011 14:46:58 +0000
changeset 84136 617f56ac76e61706074beb9551f79e4d387dde34
parent 84135 aae06ac0f572c007919c7db02678a5fd6aba629b
child 84137 892998b307696babf22246350b2df47d1067a15c
push id519
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 00:38:35 +0000
treeherdermozilla-beta@788ea1ef610b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrnewman, mfinkle
bugs708151
milestone11.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 708151 - Change GUID generation to be more compliant with Sync (r=rnewman, a=mfinkle)
mobile/android/base/db/BrowserProvider.java
--- a/mobile/android/base/db/BrowserProvider.java
+++ b/mobile/android/base/db/BrowserProvider.java
@@ -34,17 +34,17 @@
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 package org.mozilla.gecko.db;
 
 import java.io.File;
 import java.util.HashMap;
-import java.util.UUID;
+import java.util.Random;
 
 import org.mozilla.gecko.GeckoApp;
 import org.mozilla.gecko.db.BrowserContract.Bookmarks;
 import org.mozilla.gecko.db.BrowserContract.CommonColumns;
 import org.mozilla.gecko.db.BrowserContract.History;
 import org.mozilla.gecko.db.BrowserContract.Images;
 import org.mozilla.gecko.db.BrowserContract.Schema;
 import org.mozilla.gecko.db.BrowserContract.SyncColumns;
@@ -58,16 +58,17 @@ import android.content.UriMatcher;
 import android.database.Cursor;
 import android.database.MatrixCursor;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;
 import android.database.sqlite.SQLiteQueryBuilder;
 import android.net.Uri;
 import android.os.Build;
 import android.text.TextUtils;
+import android.util.Base64;
 import android.util.Log;
 
 public class BrowserProvider extends ContentProvider {
     private static final String LOGTAG = "GeckoBrowserProvider";
 
     static final String DATABASE_NAME = "browser.db";
 
     static final int DATABASE_VERSION = 1;
@@ -187,16 +188,30 @@ public class BrowserProvider extends Con
     static final String qualifyColumn(String table, String column) {
         return table + "." + column + " AS " + column;
     }
 
     static final String qualifyColumnValue(String table, String column) {
         return table + "." + column;
     }
 
+    public static String generateGuid() {
+        byte[] encodedBytes = Base64.encode(generateRandomBytes(9), Base64.URL_SAFE);
+        return new String(encodedBytes);
+    }
+
+    private static byte[] generateRandomBytes(int length) {
+        byte[] bytes = new byte[length];
+
+        Random random = new Random(System.nanoTime());
+        random.nextBytes(bytes);
+
+        return bytes;
+    }
+
     // This is available in Android >= 11. Implemented locally to be
     // compatible with older versions.
     public static String concatenateWhere(String a, String b) {
         if (TextUtils.isEmpty(a)) {
             return b;
         }
 
         if (TextUtils.isEmpty(b)) {
@@ -599,17 +614,17 @@ public class BrowserProvider extends Con
                 Log.d(LOGTAG, "Insert on BOOKMARKS: " + uri);
 
                 long now = System.currentTimeMillis();
                 values.put(Bookmarks.DATE_CREATED, now);
                 values.put(Bookmarks.DATE_MODIFIED, now);
 
                 // Generate GUID for new bookmark. Don't override specified GUIDs.
                 if (!values.containsKey(Bookmarks.GUID)) {
-                  values.put(Bookmarks.GUID, UUID.randomUUID().toString());
+                  values.put(Bookmarks.GUID, generateGuid());
                 }
 
                 if (!values.containsKey(Bookmarks.POSITION)) {
                     Log.d(LOGTAG, "Inserting bookmark with no position for URI");
                     values.put(Bookmarks.POSITION, Long.toString(Long.MIN_VALUE));
                 }
 
                 String url = values.getAsString(Bookmarks.URL);
@@ -632,17 +647,17 @@ public class BrowserProvider extends Con
                 Log.d(LOGTAG, "Insert on HISTORY: " + uri);
 
                 long now = System.currentTimeMillis();
                 values.put(History.DATE_CREATED, now);
                 values.put(History.DATE_MODIFIED, now);
 
                 // Generate GUID for new history entry. Don't override specified GUIDs.
                 if (!values.containsKey(History.GUID)) {
-                  values.put(History.GUID, UUID.randomUUID().toString());
+                  values.put(History.GUID, generateGuid());
                 }
 
                 String url = values.getAsString(History.URL);
 
                 ContentValues imageValues = extractImageValues(values,
                         values.getAsString(History.URL));
 
                 if (imageValues != null) {
@@ -659,17 +674,17 @@ public class BrowserProvider extends Con
             case IMAGES: {
                 Log.d(LOGTAG, "Insert on IMAGES: " + uri);
 
                 long now = System.currentTimeMillis();
                 values.put(History.DATE_CREATED, now);
                 values.put(History.DATE_MODIFIED, now);
 
                 // Generate GUID for new history entry
-                values.put(History.GUID, UUID.randomUUID().toString());
+                values.put(History.GUID, generateGuid());
 
                 String url = values.getAsString(Images.URL);
 
                 Log.d(LOGTAG, "Inserting image in database with URL: " + url);
                 id = db.insertOrThrow(TABLE_IMAGES, Images.URL, values);
                 break;
             }
 
@@ -1080,17 +1095,17 @@ public class BrowserProvider extends Con
             values.put(Images.IS_DELETED, 0);
 
         Log.d(LOGTAG, "Trying to update image for URL: " + url);
         int updated = db.update(TABLE_IMAGES, values, selection, selectionArgs);
 
         if (updated == 0 && insertIfNeeded) {
             // Generate GUID for new image, if one is not already provided.
             if (!values.containsKey(Images.GUID)) {
-              values.put(Images.GUID, UUID.randomUUID().toString());
+              values.put(Images.GUID, generateGuid());
             }
 
             values.put(Images.DATE_CREATED, now);
             values.put(Images.DATE_MODIFIED, now);
 
             Log.d(LOGTAG, "No update, inserting image for URL: " + url);
             db.insert(TABLE_IMAGES, Images.FAVICON, values);
             updated = 1;