Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0fc14043 authored by Leon Scroggins's avatar Leon Scroggins
Browse files

Close Cursors in finally blocks.

Fix for http://b/issue?id=2533750

Change-Id: I3ebcc6147e4035ce1c4bf6b76a359ce14196e357
parent 2f47989f
Loading
Loading
Loading
Loading
+210 −135
Original line number Diff line number Diff line
@@ -389,10 +389,17 @@ public class WebViewDatabase {
            return false;
        }

        Cursor cursor = mDatabase.query(mTableNames[tableId], ID_PROJECTION,
        Cursor cursor = null;
        boolean ret = false;
        try {
            cursor = mDatabase.query(mTableNames[tableId], ID_PROJECTION,
                    null, null, null, null, null);
        boolean ret = cursor.moveToFirst() == true;
        cursor.close();
            ret = cursor.moveToFirst() == true;
        } catch (IllegalStateException e) {
            Log.e(LOGTAG, "hasEntries", e);
        } finally {
            if (cursor != null) cursor.close();
        }
        return ret;
    }

@@ -420,7 +427,9 @@ public class WebViewDatabase {
            };
            final String selection = "(" + COOKIES_DOMAIN_COL
                    + " GLOB '*' || ?)";
            Cursor cursor = mDatabase.query(mTableNames[TABLE_COOKIES_ID],
            Cursor cursor = null;
            try {
                cursor = mDatabase.query(mTableNames[TABLE_COOKIES_ID],
                        columns, selection, new String[] { domain }, null, null,
                        null);
                if (cursor.moveToFirst()) {
@@ -446,7 +455,11 @@ public class WebViewDatabase {
                        list.add(cookie);
                    } while (cursor.moveToNext());
                }
            cursor.close();
            } catch (IllegalStateException e) {
                Log.e(LOGTAG, "getCookiesForDomain", e);
            } finally {
                if (cursor != null) cursor.close();
            }
            return list;
        }
    }
@@ -604,12 +617,12 @@ public class WebViewDatabase {
            return null;
        }

        Cursor cursor = mCacheDatabase.rawQuery("SELECT filepath, lastmodify, etag, expires, "
        Cursor cursor = null;
        final String query = "SELECT filepath, lastmodify, etag, expires, "
                + "expiresstring, mimetype, encoding, httpstatus, location, contentlength, "
                    + "contentdisposition FROM cache WHERE url = ?",
                new String[] { url });

                + "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,29 +703,43 @@ public class WebViewDatabase {
            return false;
        }

        Cursor cursor = mCacheDatabase.query("cache", ID_PROJECTION,
        Cursor cursor = null;
        boolean ret = false;
        try {
            cursor = mCacheDatabase.query("cache", ID_PROJECTION,
                    null, null, null, null, null);
        boolean ret = cursor.moveToFirst() == true;
        cursor.close();
            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);
        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);
            }
        cursor.close();
        } catch (IllegalStateException e) {
            Log.e(LOGTAG, "getCacheTotalSize", e);
        } finally {
            if (cursor != null) 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);
        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);
@@ -719,8 +748,10 @@ public class WebViewDatabase {
                    pathStr.append(", ?");
                }
                pathStr.append(")");
            SQLiteStatement statement = mCacheDatabase.compileStatement(pathStr
                    .toString());
                SQLiteStatement statement = null;
                try {
                    statement = mCacheDatabase.compileStatement(
                            pathStr.toString());
                    // as bindString() uses 1-based index, initialize index to 1
                    int index = 1;
                    do {
@@ -739,19 +770,29 @@ public class WebViewDatabase {
                        }
                    } 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.
                        // 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 SQLiteStatement", e);
                } finally {
                    if (statement != null) 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",
        Cursor cursor = null;
        try {
            cursor = mCacheDatabase.rawQuery("SELECT filepath FROM cache",
                    null);
            if (cursor != null && cursor.moveToFirst()) {
                pathList = new ArrayList<String>(cursor.getCount());
@@ -759,7 +800,11 @@ public class WebViewDatabase {
                    pathList.add(cursor.getString(0));
                } while (cursor.moveToNext());
            }
        cursor.close();
        } catch (IllegalStateException e) {
            Log.e(LOGTAG, "getAllCacheFileNames", e);
        } finally {
            if (cursor != null) cursor.close();
        }
        return pathList;
    }

@@ -809,7 +854,9 @@ public class WebViewDatabase {
        final String selection = "(" + PASSWORD_HOST_COL + " == ?)";
        synchronized (mPasswordLock) {
            String[] ret = null;
            Cursor cursor = mDatabase.query(mTableNames[TABLE_PASSWORD_ID],
            Cursor cursor = null;
            try {
                cursor = mDatabase.query(mTableNames[TABLE_PASSWORD_ID],
                        columns, selection, new String[] { schemePlusHost }, null,
                        null, null);
                if (cursor.moveToFirst()) {
@@ -819,7 +866,11 @@ public class WebViewDatabase {
                    ret[1] = cursor.getString(
                            cursor.getColumnIndex(PASSWORD_PASSWORD_COL));
                }
            cursor.close();
            } catch (IllegalStateException e) {
                Log.e(LOGTAG, "getUsernamePassword", e);
            } finally {
                if (cursor != null) cursor.close();
            }
            return ret;
        }
    }
@@ -900,7 +951,9 @@ public class WebViewDatabase {
                + HTTPAUTH_REALM_COL + " == ?)";
        synchronized (mHttpAuthLock) {
            String[] ret = null;
            Cursor cursor = mDatabase.query(mTableNames[TABLE_HTTPAUTH_ID],
            Cursor cursor = null;
            try {
                cursor = mDatabase.query(mTableNames[TABLE_HTTPAUTH_ID],
                        columns, selection, new String[] { host, realm }, null,
                        null, null);
                if (cursor.moveToFirst()) {
@@ -910,7 +963,11 @@ public class WebViewDatabase {
                    ret[1] = cursor.getString(
                            cursor.getColumnIndex(HTTPAUTH_PASSWORD_COL));
                }
            cursor.close();
            } catch (IllegalStateException e) {
                Log.e(LOGTAG, "getHttpAuthUsernamePassword", e);
            } finally {
                if (cursor != null) cursor.close();
            }
            return ret;
        }
    }
@@ -958,7 +1015,9 @@ public class WebViewDatabase {
        final String selection = "(" + FORMURL_URL_COL + " == ?)";
        synchronized (mFormLock) {
            long urlid = -1;
            Cursor cursor = mDatabase.query(mTableNames[TABLE_FORMURL_ID],
            Cursor cursor = null;
            try {
                cursor = mDatabase.query(mTableNames[TABLE_FORMURL_ID],
                        ID_PROJECTION, selection, new String[] { url }, null, null,
                        null);
                if (cursor.moveToFirst()) {
@@ -969,7 +1028,11 @@ public class WebViewDatabase {
                    urlid = mDatabase.insert(
                            mTableNames[TABLE_FORMURL_ID], null, c);
                }
            cursor.close();
            } catch (IllegalStateException e) {
                Log.e(LOGTAG, "setFormData", e);
            } finally {
                if (cursor != null) 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],
            Cursor cursor = null;
            try {
                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(
                    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);
                                new String[] { Long.toString(urlid), name },
                                null, null, null);
                        if (dataCursor.moveToFirst()) {
                    int valueCol =
                            dataCursor.getColumnIndex(FORMDATA_VALUE_COL);
                            int valueCol = dataCursor.getColumnIndex(
                                    FORMDATA_VALUE_COL);
                            do {
                                values.add(dataCursor.getString(valueCol));
                            } while (dataCursor.moveToNext());
                        }
                dataCursor.close();
                    } catch (IllegalStateException e) {
                        Log.e(LOGTAG, "getFormData dataCursor", e);
                    } finally {
                        if (dataCursor != null) dataCursor.close();
                    }
                }
            } catch (IllegalStateException e) {
                Log.e(LOGTAG, "getFormData cursor", e);
            } finally {
                if (cursor != null) cursor.close();
            }
            cursor.close();
            return values;
        }
    }