Bug 725052 - Check if a password is in the deleted database before inserting it. r=rnewman
authorWes Johnston <wjohnston@mozilla.com>
Thu, 08 Mar 2012 10:25:44 -0800
changeset 88812 11dea4451258e2c3d0d8b4d6ac3e93694d535072
parent 88811 34da6373715ffbedf4d2c636690fa607e00dee3e
child 88813 1d78e84e678cdcca09a1bf79b4e3dda565374816
push id7023
push userwjohnston@mozilla.com
push dateMon, 12 Mar 2012 18:54:01 +0000
treeherdermozilla-inbound@1d78e84e678c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrnewman
bugs725052
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 725052 - Check if a password is in the deleted database before inserting it. r=rnewman
mobile/android/base/db/FormHistoryProvider.java.in
mobile/android/base/db/GeckoProvider.java.in
mobile/android/base/db/PasswordsProvider.java.in
--- a/mobile/android/base/db/FormHistoryProvider.java.in
+++ b/mobile/android/base/db/FormHistoryProvider.java.in
@@ -49,16 +49,19 @@ public class FormHistoryProvider extends
 
     private static HashMap<String, String> FORM_HISTORY_PROJECTION_MAP;
     private static HashMap<String, String> DELETED_FORM_HISTORY_PROJECTION_MAP;
 
     // This should be kept in sync with the db version in toolkit/components/satchel/nsFormHistory.js
     private static int DB_VERSION = 4;
     private static String DB_FILENAME = "formhistory.sqlite";
 
+    private static final String WHERE_GUID_IS_NULL = BrowserContract.DeletedFormHistory.GUID + " IS NULL";
+    private static final String WHERE_GUID_IS_VALUE = BrowserContract.DeletedFormHistory.GUID + " = ?";
+
     static {
         URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
         URI_MATCHER.addURI(BrowserContract.FORM_HISTORY_AUTHORITY, "formhistory", FORM_HISTORY);
         URI_MATCHER.addURI(BrowserContract.FORM_HISTORY_AUTHORITY, "deleted-formhistory", DELETED_FORM_HISTORY);
         FORM_HISTORY_PROJECTION_MAP = new HashMap<String, String>();
         DELETED_FORM_HISTORY_PROJECTION_MAP = new HashMap<String, String>();
     }
 
@@ -139,14 +142,32 @@ public class FormHistoryProvider extends
                 throw new UnsupportedOperationException("Unknown insert URI " + uri);
         }
     }
 
     public void initGecko() {
         GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("FormHistory:Init", null));
     }
 
-    public void onPreInsert(ContentValues values, Uri uri) { }
+    @Override
+    public void onPreInsert(ContentValues values, Uri uri, SQLiteBridge db) {
+        if (!values.containsKey(FormHistory.GUID)) {
+            return;
+        }
+        String guid = values.getAsString(FormHistory.GUID);
+        try {
+            if (guid == null) {
+                db.delete(TABLE_DELETED_FORM_HISTORY, WHERE_GUID_IS_NULL, null);
+                return;
+            }
+            String[] args = new String[] { guid };
+            db.delete(TABLE_DELETED_FORM_HISTORY, WHERE_GUID_IS_VALUE, args);
+        } catch(SQLiteBridgeException ex) {
+            Log.w(getLogTag(), "Error removing entry with GUID " + guid, ex);
+        }
+     }
 
-    public void onPreUpdate(ContentValues values, Uri uri) { }
+    @Override
+    public void onPreUpdate(ContentValues values, Uri uri, SQLiteBridge db) { }
 
-    public void onPostQuery(Cursor cursor, Uri uri) { }
+    @Override
+    public void onPostQuery(Cursor cursor, Uri uri, SQLiteBridge db) { }
 }
--- a/mobile/android/base/db/GeckoProvider.java.in
+++ b/mobile/android/base/db/GeckoProvider.java.in
@@ -217,17 +217,17 @@ public abstract class GeckoProvider exte
         // If we can not get a SQLiteBridge instance, its likely that the database
         // has not been set up and Gecko is not running. We return null and expect
         // callers to try again later
         if (db == null)
             return null;
 
         setupDefaults(uri, values);
 
-        onPreInsert(values, uri);
+        onPreInsert(values, uri, db);
 
         try {
             id = db.insert(getTable(uri), null, values);
         } catch(SQLiteBridgeException ex) {
             Log.e(mLogTag, "Error inserting in db", ex);
         }
 
         return ContentUris.withAppendedId(uri, id);
@@ -240,17 +240,17 @@ public abstract class GeckoProvider exte
         final SQLiteBridge db = getDatabase(uri);
 
         // If we can not get a SQLiteBridge instance, its likely that the database
         // has not been set up and Gecko is not running. We return null and expect
         // callers to try again later
         if (db == null)
             return updated;
 
-        onPreUpdate(values, uri);
+        onPreUpdate(values, uri, db);
 
         try {
             updated = db.update(getTable(uri), values, selection, selectionArgs);
         } catch(SQLiteBridgeException ex) {
             Log.e(mLogTag, "Error updating table", ex);
         }
 
         return updated;
@@ -267,30 +267,30 @@ public abstract class GeckoProvider exte
         // callers to try again later
         if (db == null)
             return cursor;
 
         sortOrder = getSortOrder(uri, sortOrder);
 
         try {
             cursor = db.query(getTable(uri), projection, selection, selectionArgs, null, null, sortOrder, null);
-            onPostQuery(cursor, uri);
+            onPostQuery(cursor, uri, db);
         } catch (SQLiteBridgeException ex) {
             Log.e(mLogTag, "Error querying database", ex);
         }
 
         return cursor;
     }
 
     public abstract String getTable(Uri uri);
 
     public abstract String getSortOrder(Uri uri, String aRequested);
 
     public abstract void setupDefaults(Uri uri, ContentValues values);
 
     public abstract void initGecko();
 
-    public abstract void onPreInsert(ContentValues values, Uri uri);
+    public abstract void onPreInsert(ContentValues values, Uri uri, SQLiteBridge db);
 
-    public abstract void onPreUpdate(ContentValues values, Uri uri);
+    public abstract void onPreUpdate(ContentValues values, Uri uri, SQLiteBridge db);
 
-    public abstract void onPostQuery(Cursor cursor, Uri uri);
+    public abstract void onPostQuery(Cursor cursor, Uri uri, SQLiteBridge db);
 }
--- a/mobile/android/base/db/PasswordsProvider.java.in
+++ b/mobile/android/base/db/PasswordsProvider.java.in
@@ -54,16 +54,19 @@ public class PasswordsProvider extends G
 
     private static HashMap<String, String> PASSWORDS_PROJECTION_MAP;
     private static HashMap<String, String> DELETED_PASSWORDS_PROJECTION_MAP;
 
     // this should be kept in sync with the version in toolkit/components/passwordmgr/storage-mozStorage.js
     private static final int DB_VERSION = 5;
     private static final String DB_FILENAME = "signons.sqlite";
 
+    private static final String WHERE_GUID_IS_NULL = BrowserContract.DeletedPasswords.GUID + " IS NULL";
+    private static final String WHERE_GUID_IS_VALUE = BrowserContract.DeletedPasswords.GUID + " = ?";
+
     static {
         URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
 
         // content://org.mozilla.gecko.providers.browser/passwords/#
         URI_MATCHER.addURI(BrowserContract.PASSWORDS_AUTHORITY, "passwords", PASSWORDS);
 
         PASSWORDS_PROJECTION_MAP = new HashMap<String, String>();
         PASSWORDS_PROJECTION_MAP.put(Passwords.ID, Passwords.ID);
@@ -205,41 +208,58 @@ public class PasswordsProvider extends G
           else result = NSSBridge.encrypt(mContext, initialValue);
         } else {
           if (profilePath != null) result = NSSBridge.decrypt(mContext, profilePath, initialValue);
           else result = NSSBridge.decrypt(mContext, initialValue);            
         }
         return result;
     }
 
-    public void onPreInsert(ContentValues values, Uri uri) {
-        if (values.containsKey(Passwords.ENCRYPTED_PASSWORD)) {
-            String res = doCrypto(values.getAsString(Passwords.ENCRYPTED_PASSWORD), uri, true);
-            values.put(Passwords.ENCRYPTED_PASSWORD, res);
+    @Override
+    public void onPreInsert(ContentValues values, Uri uri, SQLiteBridge db) {
+        if (values.containsKey(Passwords.GUID)) {
+            String guid = values.getAsString(Passwords.GUID);
+            try {
+                if (guid == null) {
+                    db.delete(TABLE_DELETED_PASSWORDS, WHERE_GUID_IS_NULL, null);
+                    return;
+                }
+                String[] args = new String[] { guid };
+                db.delete(TABLE_DELETED_PASSWORDS, WHERE_GUID_IS_VALUE, args);
+            } catch(SQLiteBridgeException ex) {
+                Log.w(getLogTag(), "Error removing entry with GUID " + guid, ex);
+            }
         }
 
-        if (values.containsKey(Passwords.ENCRYPTED_USERNAME)) {
-            String res = doCrypto(values.getAsString(Passwords.ENCRYPTED_USERNAME), uri, true);
-            values.put(Passwords.ENCRYPTED_USERNAME, res);
-        }
-    }
- 
-    public void onPreUpdate(ContentValues values, Uri uri) {
         if (values.containsKey(Passwords.ENCRYPTED_PASSWORD)) {
             String res = doCrypto(values.getAsString(Passwords.ENCRYPTED_PASSWORD), uri, true);
             values.put(Passwords.ENCRYPTED_PASSWORD, res);
         }
 
         if (values.containsKey(Passwords.ENCRYPTED_USERNAME)) {
             String res = doCrypto(values.getAsString(Passwords.ENCRYPTED_USERNAME), uri, true);
             values.put(Passwords.ENCRYPTED_USERNAME, res);
         }
     }
 
-    public void onPostQuery(Cursor cursor, Uri uri) {
+    @Override
+    public void onPreUpdate(ContentValues values, Uri uri, SQLiteBridge db) {
+        if (values.containsKey(Passwords.ENCRYPTED_PASSWORD)) {
+            String res = doCrypto(values.getAsString(Passwords.ENCRYPTED_PASSWORD), uri, true);
+            values.put(Passwords.ENCRYPTED_PASSWORD, res);
+        }
+
+        if (values.containsKey(Passwords.ENCRYPTED_USERNAME)) {
+            String res = doCrypto(values.getAsString(Passwords.ENCRYPTED_USERNAME), uri, true);
+            values.put(Passwords.ENCRYPTED_USERNAME, res);
+        }
+    }
+
+    @Override
+    public void onPostQuery(Cursor cursor, Uri uri, SQLiteBridge db) {
         int passwordIndex = -1;
         int usernameIndex = -1;
         String profilePath = null;
 
         try {
             passwordIndex = cursor.getColumnIndexOrThrow(Passwords.ENCRYPTED_PASSWORD);
         } catch(Exception ex) { }
         try {