diff options
| author | Leon Scroggins <scroggo@google.com> | 2010-03-22 14:00:38 -0400 |
|---|---|---|
| committer | Leon Scroggins <scroggo@google.com> | 2010-03-22 16:33:07 -0400 |
| commit | 0fc140431d620cc7d9f68f43ab3b3a0e36adf224 (patch) | |
| tree | 25252f7a4a9c11a1e18b85843b1c05cebf9d0ad0 /core/java/android/webkit/WebViewDatabase.java | |
| parent | 2f47989f65144764dd4d3284fadda5677aa9fc8e (diff) | |
Close Cursors in finally blocks.
Fix for http://b/issue?id=2533750
Change-Id: I3ebcc6147e4035ce1c4bf6b76a359ce14196e357
Diffstat (limited to 'core/java/android/webkit/WebViewDatabase.java')
| -rw-r--r-- | core/java/android/webkit/WebViewDatabase.java | 345 |
1 files changed, 210 insertions, 135 deletions
diff --git a/core/java/android/webkit/WebViewDatabase.java b/core/java/android/webkit/WebViewDatabase.java index a8709312b3b4..a75e6f0a1ec9 100644 --- a/core/java/android/webkit/WebViewDatabase.java +++ b/core/java/android/webkit/WebViewDatabase.java @@ -389,10 +389,17 @@ public class WebViewDatabase { return false; } - Cursor cursor = mDatabase.query(mTableNames[tableId], ID_PROJECTION, - null, null, null, null, null); - boolean ret = cursor.moveToFirst() == true; - cursor.close(); + Cursor cursor = null; + boolean ret = false; + try { + cursor = mDatabase.query(mTableNames[tableId], ID_PROJECTION, + null, null, null, null, null); + ret = cursor.moveToFirst() == true; + } catch (IllegalStateException e) { + Log.e(LOGTAG, "hasEntries", e); + } finally { + if (cursor != null) cursor.close(); + } return ret; } @@ -420,33 +427,39 @@ public class WebViewDatabase { }; final String selection = "(" + COOKIES_DOMAIN_COL + " GLOB '*' || ?)"; - Cursor cursor = mDatabase.query(mTableNames[TABLE_COOKIES_ID], - columns, selection, new String[] { domain }, null, null, - null); - if (cursor.moveToFirst()) { - int domainCol = cursor.getColumnIndex(COOKIES_DOMAIN_COL); - int pathCol = cursor.getColumnIndex(COOKIES_PATH_COL); - int nameCol = cursor.getColumnIndex(COOKIES_NAME_COL); - int valueCol = cursor.getColumnIndex(COOKIES_VALUE_COL); - int expiresCol = cursor.getColumnIndex(COOKIES_EXPIRES_COL); - int secureCol = cursor.getColumnIndex(COOKIES_SECURE_COL); - do { - Cookie cookie = new Cookie(); - cookie.domain = cursor.getString(domainCol); - cookie.path = cursor.getString(pathCol); - cookie.name = cursor.getString(nameCol); - cookie.value = cursor.getString(valueCol); - if (cursor.isNull(expiresCol)) { - cookie.expires = -1; - } else { - cookie.expires = cursor.getLong(expiresCol); - } - cookie.secure = cursor.getShort(secureCol) != 0; - cookie.mode = Cookie.MODE_NORMAL; - list.add(cookie); - } while (cursor.moveToNext()); + Cursor cursor = null; + try { + cursor = mDatabase.query(mTableNames[TABLE_COOKIES_ID], + columns, selection, new String[] { domain }, null, null, + null); + if (cursor.moveToFirst()) { + int domainCol = cursor.getColumnIndex(COOKIES_DOMAIN_COL); + int pathCol = cursor.getColumnIndex(COOKIES_PATH_COL); + int nameCol = cursor.getColumnIndex(COOKIES_NAME_COL); + int valueCol = cursor.getColumnIndex(COOKIES_VALUE_COL); + int expiresCol = cursor.getColumnIndex(COOKIES_EXPIRES_COL); + int secureCol = cursor.getColumnIndex(COOKIES_SECURE_COL); + do { + Cookie cookie = new Cookie(); + cookie.domain = cursor.getString(domainCol); + cookie.path = cursor.getString(pathCol); + cookie.name = cursor.getString(nameCol); + cookie.value = cursor.getString(valueCol); + if (cursor.isNull(expiresCol)) { + cookie.expires = -1; + } else { + cookie.expires = cursor.getLong(expiresCol); + } + cookie.secure = cursor.getShort(secureCol) != 0; + cookie.mode = Cookie.MODE_NORMAL; + list.add(cookie); + } while (cursor.moveToNext()); + } + } catch (IllegalStateException e) { + Log.e(LOGTAG, "getCookiesForDomain", e); + } finally { + if (cursor != null) cursor.close(); } - cursor.close(); return list; } } @@ -604,12 +617,12 @@ public class WebViewDatabase { return null; } - Cursor cursor = mCacheDatabase.rawQuery("SELECT filepath, lastmodify, etag, expires, " - + "expiresstring, mimetype, encoding, httpstatus, location, contentlength, " - + "contentdisposition FROM cache WHERE url = ?", - new String[] { url }); - + Cursor cursor = null; + final String query = "SELECT filepath, lastmodify, etag, expires, " + + "expiresstring, mimetype, encoding, httpstatus, location, contentlength, " + + "contentdisposition FROM cache WHERE url = ?"; try { + cursor = mCacheDatabase.rawQuery(query, new String[] { url }); if (cursor.moveToFirst()) { CacheResult ret = new CacheResult(); ret.localPath = cursor.getString(0); @@ -625,6 +638,8 @@ public class WebViewDatabase { ret.contentdisposition = cursor.getString(10); return ret; } + } catch (IllegalStateException e) { + Log.e(LOGTAG, "getCache", e); } finally { if (cursor != null) cursor.close(); } @@ -688,78 +703,108 @@ public class WebViewDatabase { return false; } - Cursor cursor = mCacheDatabase.query("cache", ID_PROJECTION, - null, null, null, null, null); - boolean ret = cursor.moveToFirst() == true; - cursor.close(); + Cursor cursor = null; + boolean ret = false; + try { + cursor = mCacheDatabase.query("cache", ID_PROJECTION, + null, null, null, null, null); + ret = cursor.moveToFirst() == true; + } catch (IllegalStateException e) { + Log.e(LOGTAG, "hasCache", e); + } finally { + if (cursor != null) cursor.close(); + } return ret; } long getCacheTotalSize() { long size = 0; - Cursor cursor = mCacheDatabase.rawQuery( - "SELECT SUM(contentlength) as sum FROM cache", null); - if (cursor.moveToFirst()) { - size = cursor.getLong(0); + Cursor cursor = null; + final String query = "SELECT SUM(contentlength) as sum FROM cache"; + try { + cursor = mCacheDatabase.rawQuery(query, null); + if (cursor.moveToFirst()) { + size = cursor.getLong(0); + } + } catch (IllegalStateException e) { + Log.e(LOGTAG, "getCacheTotalSize", e); + } finally { + if (cursor != null) cursor.close(); } - cursor.close(); return size; } List<String> trimCache(long amount) { ArrayList<String> pathList = new ArrayList<String>(100); - Cursor cursor = mCacheDatabase.rawQuery( - "SELECT contentlength, filepath FROM cache ORDER BY expires ASC", - null); - if (cursor.moveToFirst()) { - int batchSize = 100; - StringBuilder pathStr = new StringBuilder(20 + 16 * batchSize); - pathStr.append("DELETE FROM cache WHERE filepath IN (?"); - for (int i = 1; i < batchSize; i++) { - pathStr.append(", ?"); - } - pathStr.append(")"); - SQLiteStatement statement = mCacheDatabase.compileStatement(pathStr - .toString()); - // as bindString() uses 1-based index, initialize index to 1 - int index = 1; - do { - long length = cursor.getLong(0); - if (length == 0) { - continue; + Cursor cursor = null; + final String query = "SELECT contentlength, filepath FROM cache ORDER BY expires ASC"; + try { + cursor = mCacheDatabase.rawQuery(query, null); + if (cursor.moveToFirst()) { + int batchSize = 100; + StringBuilder pathStr = new StringBuilder(20 + 16 * batchSize); + pathStr.append("DELETE FROM cache WHERE filepath IN (?"); + for (int i = 1; i < batchSize; i++) { + pathStr.append(", ?"); } - amount -= length; - String filePath = cursor.getString(1); - statement.bindString(index, filePath); - pathList.add(filePath); - if (index++ == batchSize) { - statement.execute(); - statement.clearBindings(); - index = 1; + pathStr.append(")"); + SQLiteStatement statement = null; + try { + statement = mCacheDatabase.compileStatement( + pathStr.toString()); + // as bindString() uses 1-based index, initialize index to 1 + int index = 1; + do { + long length = cursor.getLong(0); + if (length == 0) { + continue; + } + amount -= length; + String filePath = cursor.getString(1); + statement.bindString(index, filePath); + pathList.add(filePath); + if (index++ == batchSize) { + statement.execute(); + statement.clearBindings(); + index = 1; + } + } while (cursor.moveToNext() && amount > 0); + if (index > 1) { + // there may be old bindings from the previous statement + // if index is less than batchSize, which is Ok. + statement.execute(); + } + } catch (IllegalStateException e) { + Log.e(LOGTAG, "trimCache SQLiteStatement", e); + } finally { + if (statement != null) statement.close(); } - } while (cursor.moveToNext() && amount > 0); - if (index > 1) { - // there may be old bindings from the previous statement if - // index is less than batchSize, which is Ok. - statement.execute(); } - statement.close(); + } catch (IllegalStateException e) { + Log.e(LOGTAG, "trimCache Cursor", e); + } finally { + if (cursor != null) cursor.close(); } - cursor.close(); return pathList; } List<String> getAllCacheFileNames() { ArrayList<String> pathList = null; - Cursor cursor = mCacheDatabase.rawQuery("SELECT filepath FROM cache", - null); - if (cursor != null && cursor.moveToFirst()) { - pathList = new ArrayList<String>(cursor.getCount()); - do { - pathList.add(cursor.getString(0)); - } while (cursor.moveToNext()); + Cursor cursor = null; + try { + cursor = mCacheDatabase.rawQuery("SELECT filepath FROM cache", + null); + if (cursor != null && cursor.moveToFirst()) { + pathList = new ArrayList<String>(cursor.getCount()); + do { + pathList.add(cursor.getString(0)); + } while (cursor.moveToNext()); + } + } catch (IllegalStateException e) { + Log.e(LOGTAG, "getAllCacheFileNames", e); + } finally { + if (cursor != null) cursor.close(); } - cursor.close(); return pathList; } @@ -809,17 +854,23 @@ public class WebViewDatabase { final String selection = "(" + PASSWORD_HOST_COL + " == ?)"; synchronized (mPasswordLock) { String[] ret = null; - Cursor cursor = mDatabase.query(mTableNames[TABLE_PASSWORD_ID], - columns, selection, new String[] { schemePlusHost }, null, - null, null); - if (cursor.moveToFirst()) { - ret = new String[2]; - ret[0] = cursor.getString( - cursor.getColumnIndex(PASSWORD_USERNAME_COL)); - ret[1] = cursor.getString( - cursor.getColumnIndex(PASSWORD_PASSWORD_COL)); + Cursor cursor = null; + try { + cursor = mDatabase.query(mTableNames[TABLE_PASSWORD_ID], + columns, selection, new String[] { schemePlusHost }, null, + null, null); + if (cursor.moveToFirst()) { + ret = new String[2]; + ret[0] = cursor.getString( + cursor.getColumnIndex(PASSWORD_USERNAME_COL)); + ret[1] = cursor.getString( + cursor.getColumnIndex(PASSWORD_PASSWORD_COL)); + } + } catch (IllegalStateException e) { + Log.e(LOGTAG, "getUsernamePassword", e); + } finally { + if (cursor != null) cursor.close(); } - cursor.close(); return ret; } } @@ -900,17 +951,23 @@ public class WebViewDatabase { + HTTPAUTH_REALM_COL + " == ?)"; synchronized (mHttpAuthLock) { String[] ret = null; - Cursor cursor = mDatabase.query(mTableNames[TABLE_HTTPAUTH_ID], - columns, selection, new String[] { host, realm }, null, - null, null); - if (cursor.moveToFirst()) { - ret = new String[2]; - ret[0] = cursor.getString( - cursor.getColumnIndex(HTTPAUTH_USERNAME_COL)); - ret[1] = cursor.getString( - cursor.getColumnIndex(HTTPAUTH_PASSWORD_COL)); + Cursor cursor = null; + try { + cursor = mDatabase.query(mTableNames[TABLE_HTTPAUTH_ID], + columns, selection, new String[] { host, realm }, null, + null, null); + if (cursor.moveToFirst()) { + ret = new String[2]; + ret[0] = cursor.getString( + cursor.getColumnIndex(HTTPAUTH_USERNAME_COL)); + ret[1] = cursor.getString( + cursor.getColumnIndex(HTTPAUTH_PASSWORD_COL)); + } + } catch (IllegalStateException e) { + Log.e(LOGTAG, "getHttpAuthUsernamePassword", e); + } finally { + if (cursor != null) cursor.close(); } - cursor.close(); return ret; } } @@ -958,18 +1015,24 @@ public class WebViewDatabase { final String selection = "(" + FORMURL_URL_COL + " == ?)"; synchronized (mFormLock) { long urlid = -1; - Cursor cursor = mDatabase.query(mTableNames[TABLE_FORMURL_ID], - ID_PROJECTION, selection, new String[] { url }, null, null, - null); - if (cursor.moveToFirst()) { - urlid = cursor.getLong(cursor.getColumnIndex(ID_COL)); - } else { - ContentValues c = new ContentValues(); - c.put(FORMURL_URL_COL, url); - urlid = mDatabase.insert( - mTableNames[TABLE_FORMURL_ID], null, c); + Cursor cursor = null; + try { + cursor = mDatabase.query(mTableNames[TABLE_FORMURL_ID], + ID_PROJECTION, selection, new String[] { url }, null, null, + null); + if (cursor.moveToFirst()) { + urlid = cursor.getLong(cursor.getColumnIndex(ID_COL)); + } else { + ContentValues c = new ContentValues(); + c.put(FORMURL_URL_COL, url); + urlid = mDatabase.insert( + mTableNames[TABLE_FORMURL_ID], null, c); + } + } catch (IllegalStateException e) { + Log.e(LOGTAG, "setFormData", e); + } finally { + if (cursor != null) cursor.close(); } - cursor.close(); if (urlid >= 0) { Set<Entry<String, String>> set = formdata.entrySet(); Iterator<Entry<String, String>> iter = set.iterator(); @@ -1002,27 +1065,39 @@ public class WebViewDatabase { final String dataSelection = "(" + FORMDATA_URLID_COL + " == ?) AND (" + FORMDATA_NAME_COL + " == ?)"; synchronized (mFormLock) { - Cursor cursor = mDatabase.query(mTableNames[TABLE_FORMURL_ID], - ID_PROJECTION, urlSelection, new String[] { url }, null, - null, null); - if (cursor.moveToFirst()) { - long urlid = cursor.getLong(cursor.getColumnIndex(ID_COL)); - Cursor dataCursor = mDatabase.query( - mTableNames[TABLE_FORMDATA_ID], - new String[] { ID_COL, FORMDATA_VALUE_COL }, - dataSelection, - new String[] { Long.toString(urlid), name }, null, + Cursor cursor = null; + try { + cursor = mDatabase.query(mTableNames[TABLE_FORMURL_ID], + ID_PROJECTION, urlSelection, new String[] { url }, null, null, null); - if (dataCursor.moveToFirst()) { - int valueCol = - dataCursor.getColumnIndex(FORMDATA_VALUE_COL); - do { - values.add(dataCursor.getString(valueCol)); - } while (dataCursor.moveToNext()); + if (cursor.moveToFirst()) { + long urlid = cursor.getLong(cursor.getColumnIndex(ID_COL)); + Cursor dataCursor = null; + try { + dataCursor = mDatabase.query( + mTableNames[TABLE_FORMDATA_ID], + new String[] { ID_COL, FORMDATA_VALUE_COL }, + dataSelection, + new String[] { Long.toString(urlid), name }, + null, null, null); + if (dataCursor.moveToFirst()) { + int valueCol = dataCursor.getColumnIndex( + FORMDATA_VALUE_COL); + do { + values.add(dataCursor.getString(valueCol)); + } while (dataCursor.moveToNext()); + } + } catch (IllegalStateException e) { + Log.e(LOGTAG, "getFormData dataCursor", e); + } finally { + if (dataCursor != null) dataCursor.close(); + } } - dataCursor.close(); + } catch (IllegalStateException e) { + Log.e(LOGTAG, "getFormData cursor", e); + } finally { + if (cursor != null) cursor.close(); } - cursor.close(); return values; } } |
