Bug 768900 - Protect against errors when quering Android DBs. r=mfinkle
authorGian-Carlo Pascutto <gpascutto@mozilla.com>
Mon, 02 Jul 2012 08:59:37 +0200
changeset 98116 eb110cad75a5a3dd07640d19a906160ec97ac50a
parent 98115 20137a4f2187a2ea5e4a37a023f0f1aac3cf84c5
child 98117 d9892b7747b5942cc300cfa92ccf5c0569e09dde
push id23024
push userryanvm@gmail.com
push dateMon, 02 Jul 2012 22:29:23 +0000
treeherdermozilla-central@26aa29fdd888 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle
bugs768900
milestone16.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 768900 - Protect against errors when quering Android DBs. r=mfinkle
mobile/android/base/AndroidImport.java
--- a/mobile/android/base/AndroidImport.java
+++ b/mobile/android/base/AndroidImport.java
@@ -45,84 +45,98 @@ class AndroidImport implements Runnable 
         mOperations = new ArrayList<ContentProviderOperation>();
         mCr = mContext.getContentResolver();
         mDB = new LocalBrowserDB(GeckoProfile.get(context).getName());
         mImportBookmarks = doBookmarks;
         mImportHistory = doHistory;
     }
 
     public void mergeBookmarks() {
-        Cursor cursor = mCr.query(Browser.BOOKMARKS_URI,
-                                  null,
-                                  Browser.BookmarkColumns.BOOKMARK + " = 1",
-                                  null,
-                                  null);
+        Cursor cursor = null;
+        try {
+            cursor = mCr.query(Browser.BOOKMARKS_URI,
+                               null,
+                               Browser.BookmarkColumns.BOOKMARK + " = 1",
+                               null,
+                               null);
 
-        final int faviconCol = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.FAVICON);
-        final int titleCol = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.TITLE);
-        final int urlCol = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.URL);
-        // http://code.google.com/p/android/issues/detail?id=17969
-        final int createCol = cursor.getColumnIndex(Browser.BookmarkColumns.CREATED);
+            if (cursor != null) {
+                final int faviconCol = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.FAVICON);
+                final int titleCol = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.TITLE);
+                final int urlCol = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.URL);
+                // http://code.google.com/p/android/issues/detail?id=17969
+                final int createCol = cursor.getColumnIndex(Browser.BookmarkColumns.CREATED);
 
-        cursor.moveToFirst();
-        while (!cursor.isAfterLast()) {
-            String url = cursor.getString(urlCol);
-            String title = cursor.getString(titleCol);
-            long created;
-            if (createCol >= 0) {
-                created = cursor.getLong(createCol);
-            } else {
-                created = System.currentTimeMillis();
+                cursor.moveToFirst();
+                while (!cursor.isAfterLast()) {
+                    String url = cursor.getString(urlCol);
+                    String title = cursor.getString(titleCol);
+                    long created;
+                    if (createCol >= 0) {
+                        created = cursor.getLong(createCol);
+                    } else {
+                        created = System.currentTimeMillis();
+                    }
+                    // Need to set it to the current time so Sync picks it up.
+                    long modified = System.currentTimeMillis();
+                    byte[] data = cursor.getBlob(faviconCol);
+                    mDB.updateBookmarkInBatch(mCr, mOperations,
+                                              url, title, null, -1,
+                                              created, modified,
+                                              BrowserContract.Bookmarks.DEFAULT_POSITION,
+                                              null, Bookmarks.TYPE_BOOKMARK);
+                    if (data != null) {
+                        mDB.updateFaviconInBatch(mCr, mOperations, url, null, null, data);
+                    }
+                    cursor.moveToNext();
+                }
             }
-            // Need to set it to the current time so Sync picks it up.
-            long modified = System.currentTimeMillis();
-            byte[] data = cursor.getBlob(faviconCol);
-            mDB.updateBookmarkInBatch(mCr, mOperations,
-                                      url, title, null, -1,
-                                      created, modified,
-                                      BrowserContract.Bookmarks.DEFAULT_POSITION,
-                                      null, Bookmarks.TYPE_BOOKMARK);
-            if (data != null) {
-                mDB.updateFaviconInBatch(mCr, mOperations, url, null, null, data);
-            }
-            cursor.moveToNext();
+        } finally {
+            if (cursor != null)
+                cursor.close();
         }
-        cursor.close();
 
         flushBatchOperations();
     }
 
     public void mergeHistory() {
-        Cursor cursor = mCr.query(Browser.BOOKMARKS_URI,
-                                  null,
-                                  Browser.BookmarkColumns.BOOKMARK + " = 0 AND " +
-                                  Browser.BookmarkColumns.VISITS + " > 0",
-                                  null,
-                                  null);
+        Cursor cursor = null;
+        try {
+            cursor = mCr.query(Browser.BOOKMARKS_URI,
+                               null,
+                               Browser.BookmarkColumns.BOOKMARK + " = 0 AND " +
+                               Browser.BookmarkColumns.VISITS + " > 0",
+                               null,
+                               null);
 
-        final int dateCol = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.DATE);
-        final int faviconCol = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.FAVICON);
-        final int titleCol = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.TITLE);
-        final int urlCol = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.URL);
-        final int visitsCol = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.VISITS);
+            if (cursor != null) {
+                final int dateCol = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.DATE);
+                final int faviconCol = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.FAVICON);
+                final int titleCol = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.TITLE);
+                final int urlCol = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.URL);
+                final int visitsCol = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.VISITS);
 
-        cursor.moveToFirst();
-        while (!cursor.isAfterLast()) {
-            String url = cursor.getString(urlCol);
-            String title = cursor.getString(titleCol);
-            long date = cursor.getLong(dateCol);
-            int visits = cursor.getInt(visitsCol);
-            byte[] data = cursor.getBlob(faviconCol);
-            mDB.updateHistoryInBatch(mCr, mOperations, url, title, date, visits);
-            if (data != null) {
-                mDB.updateFaviconInBatch(mCr, mOperations, url, null, null, data);
+                cursor.moveToFirst();
+                while (!cursor.isAfterLast()) {
+                    String url = cursor.getString(urlCol);
+                    String title = cursor.getString(titleCol);
+                    long date = cursor.getLong(dateCol);
+                    int visits = cursor.getInt(visitsCol);
+                    byte[] data = cursor.getBlob(faviconCol);
+                    mDB.updateHistoryInBatch(mCr, mOperations, url, title, date, visits);
+                    if (data != null) {
+                        mDB.updateFaviconInBatch(mCr, mOperations, url, null, null, data);
+                    }
+                    cursor.moveToNext();
+                }
             }
-            cursor.moveToNext();
+        } finally {
+            if (cursor != null)
+                cursor.close();
         }
-        cursor.close();
 
         flushBatchOperations();
     }
 
     protected void flushBatchOperations() {
         Log.d(LOGTAG, "Flushing " + mOperations.size() + " DB operations");
         try {
             // We don't really care for the results, this is best-effort.