Loading core/java/android/webkit/WebViewDatabase.java +154 −109 Original line number Original line Diff line number Diff line Loading @@ -173,16 +173,34 @@ public class WebViewDatabase { private static int mCacheTransactionRefcount; private static int mCacheTransactionRefcount; private WebViewDatabase() { // Initially true until the background thread completes. private boolean mInitialized = false; private WebViewDatabase(final Context context) { new Thread() { @Override public void run() { init(context); } }.start(); // Singleton only, use getInstance() // Singleton only, use getInstance() } } public static synchronized WebViewDatabase getInstance(Context context) { public static synchronized WebViewDatabase getInstance(Context context) { if (mInstance == null) { if (mInstance == null) { mInstance = new WebViewDatabase(); mInstance = new WebViewDatabase(context); } return mInstance; } private synchronized void init(Context context) { if (mInitialized) { return; } try { try { mDatabase = context mDatabase = context.openOrCreateDatabase(DATABASE_FILE, 0, null); .openOrCreateDatabase(DATABASE_FILE, 0, null); } catch (SQLiteException e) { } catch (SQLiteException e) { // try again by deleting the old db and create a new one // try again by deleting the old db and create a new one if (context.deleteDatabase(DATABASE_FILE)) { if (context.deleteDatabase(DATABASE_FILE)) { Loading @@ -193,7 +211,13 @@ public class WebViewDatabase { // mDatabase should not be null, // mDatabase should not be null, // the only case is RequestAPI test has problem to create db // the only case is RequestAPI test has problem to create db if (mDatabase != null && mDatabase.getVersion() != DATABASE_VERSION) { if (mDatabase == null) { mInitialized = true; notify(); return; } if (mDatabase.getVersion() != DATABASE_VERSION) { mDatabase.beginTransaction(); mDatabase.beginTransaction(); try { try { upgradeDatabase(); upgradeDatabase(); Loading @@ -203,11 +227,10 @@ public class WebViewDatabase { } } } } if (mDatabase != null) { // use per table Mutex lock, turn off database lock, this // use per table Mutex lock, turn off database lock, this // improves performance as database's ReentrantLock is expansive // improves performance as database's ReentrantLock is // expansive mDatabase.setLockingEnabled(false); mDatabase.setLockingEnabled(false); } try { try { mCacheDatabase = context.openOrCreateDatabase( mCacheDatabase = context.openOrCreateDatabase( Loading @@ -222,8 +245,13 @@ public class WebViewDatabase { // mCacheDatabase should not be null, // mCacheDatabase should not be null, // the only case is RequestAPI test has problem to create db // the only case is RequestAPI test has problem to create db if (mCacheDatabase != null if (mCacheDatabase == null) { && mCacheDatabase.getVersion() != CACHE_DATABASE_VERSION) { mInitialized = true; notify(); return; } if (mCacheDatabase.getVersion() != CACHE_DATABASE_VERSION) { mCacheDatabase.beginTransaction(); mCacheDatabase.beginTransaction(); try { try { upgradeCacheDatabase(); upgradeCacheDatabase(); Loading @@ -238,16 +266,16 @@ public class WebViewDatabase { CacheManager.removeAllCacheFiles(); CacheManager.removeAllCacheFiles(); } } if (mCacheDatabase != null) { // use read_uncommitted to speed up READ // use read_uncommitted to speed up READ mCacheDatabase.execSQL("PRAGMA read_uncommitted = true;"); mCacheDatabase.execSQL("PRAGMA read_uncommitted = true;"); // as only READ can be called in the non-WebViewWorkerThread, // as only READ can be called in the // and read_uncommitted is used, we can turn off database lock // non-WebViewWorkerThread, and read_uncommitted is used, // to use transaction. // we can turn off database lock to use transaction. mCacheDatabase.setLockingEnabled(false); mCacheDatabase.setLockingEnabled(false); // use InsertHelper for faster insertion // use InsertHelper for faster insertion mCacheInserter = new DatabaseUtils.InsertHelper(mCacheDatabase, mCacheInserter = new DatabaseUtils.InsertHelper(mCacheDatabase, "cache"); "cache"); mCacheUrlColIndex = mCacheInserter mCacheUrlColIndex = mCacheInserter .getColumnIndex(CACHE_URL_COL); .getColumnIndex(CACHE_URL_COL); Loading Loading @@ -275,10 +303,10 @@ public class WebViewDatabase { .getColumnIndex(CACHE_CONTENTDISPOSITION_COL); .getColumnIndex(CACHE_CONTENTDISPOSITION_COL); mCacheCrossDomainColIndex = mCacheInserter mCacheCrossDomainColIndex = mCacheInserter .getColumnIndex(CACHE_CROSSDOMAIN_COL); .getColumnIndex(CACHE_CROSSDOMAIN_COL); } } return mInstance; // Thread done, notify. mInitialized = true; notify(); } } private static void upgradeDatabase() { private static void upgradeDatabase() { Loading Loading @@ -391,8 +419,25 @@ public class WebViewDatabase { } } } } // Wait for the background initialization thread to complete and check the // database creation status. private boolean checkInitialized() { synchronized (this) { while (!mInitialized) { try { wait(); } catch (InterruptedException e) { Log.e(LOGTAG, "Caught exception while checking " + "initialization"); Log.e(LOGTAG, Log.getStackTraceString(e)); } } } return mDatabase != null; } private boolean hasEntries(int tableId) { private boolean hasEntries(int tableId) { if (mDatabase == null) { if (!checkInitialized()) { return false; return false; } } Loading Loading @@ -422,7 +467,7 @@ public class WebViewDatabase { */ */ ArrayList<Cookie> getCookiesForDomain(String domain) { ArrayList<Cookie> getCookiesForDomain(String domain) { ArrayList<Cookie> list = new ArrayList<Cookie>(); ArrayList<Cookie> list = new ArrayList<Cookie>(); if (domain == null || mDatabase == null) { if (domain == null || !checkInitialized()) { return list; return list; } } Loading Loading @@ -481,7 +526,7 @@ public class WebViewDatabase { * deleted. * deleted. */ */ void deleteCookies(String domain, String path, String name) { void deleteCookies(String domain, String path, String name) { if (domain == null || mDatabase == null) { if (domain == null || !checkInitialized()) { return; return; } } Loading @@ -501,7 +546,7 @@ public class WebViewDatabase { */ */ void addCookie(Cookie cookie) { void addCookie(Cookie cookie) { if (cookie.domain == null || cookie.path == null || cookie.name == null if (cookie.domain == null || cookie.path == null || cookie.name == null || mDatabase == null) { || !checkInitialized()) { return; return; } } Loading Loading @@ -534,7 +579,7 @@ public class WebViewDatabase { * Clear cookie database * Clear cookie database */ */ void clearCookies() { void clearCookies() { if (mDatabase == null) { if (!checkInitialized()) { return; return; } } Loading @@ -547,7 +592,7 @@ public class WebViewDatabase { * Clear session cookies, which means cookie doesn't have EXPIRES. * Clear session cookies, which means cookie doesn't have EXPIRES. */ */ void clearSessionCookies() { void clearSessionCookies() { if (mDatabase == null) { if (!checkInitialized()) { return; return; } } Loading @@ -564,7 +609,7 @@ public class WebViewDatabase { * @param now Time for now * @param now Time for now */ */ void clearExpiredCookies(long now) { void clearExpiredCookies(long now) { if (mDatabase == null) { if (!checkInitialized()) { return; return; } } Loading Loading @@ -620,7 +665,7 @@ public class WebViewDatabase { * @return CacheResult The CacheManager.CacheResult * @return CacheResult The CacheManager.CacheResult */ */ CacheResult getCache(String url) { CacheResult getCache(String url) { if (url == null || mCacheDatabase == null) { if (url == null || !checkInitialized()) { return null; return null; } } Loading Loading @@ -660,7 +705,7 @@ public class WebViewDatabase { * @param url The url * @param url The url */ */ void removeCache(String url) { void removeCache(String url) { if (url == null || mCacheDatabase == null) { if (url == null || !checkInitialized()) { return; return; } } Loading @@ -674,7 +719,7 @@ public class WebViewDatabase { * @param c The CacheManager.CacheResult * @param c The CacheManager.CacheResult */ */ void addCache(String url, CacheResult c) { void addCache(String url, CacheResult c) { if (url == null || mCacheDatabase == null) { if (url == null || !checkInitialized()) { return; return; } } Loading @@ -700,7 +745,7 @@ public class WebViewDatabase { * Clear cache database * Clear cache database */ */ void clearCache() { void clearCache() { if (mCacheDatabase == null) { if (!checkInitialized()) { return; return; } } Loading @@ -708,7 +753,7 @@ public class WebViewDatabase { } } boolean hasCache() { boolean hasCache() { if (mCacheDatabase == null) { if (!checkInitialized()) { return false; return false; } } Loading Loading @@ -831,7 +876,7 @@ public class WebViewDatabase { */ */ void setUsernamePassword(String schemePlusHost, String username, void setUsernamePassword(String schemePlusHost, String username, String password) { String password) { if (schemePlusHost == null || mDatabase == null) { if (schemePlusHost == null || !checkInitialized()) { return; return; } } Loading @@ -853,7 +898,7 @@ public class WebViewDatabase { * String[1] is password. Return null if it can't find anything. * String[1] is password. Return null if it can't find anything. */ */ String[] getUsernamePassword(String schemePlusHost) { String[] getUsernamePassword(String schemePlusHost) { if (schemePlusHost == null || mDatabase == null) { if (schemePlusHost == null || !checkInitialized()) { return null; return null; } } Loading Loading @@ -899,7 +944,7 @@ public class WebViewDatabase { * Clear password database * Clear password database */ */ public void clearUsernamePassword() { public void clearUsernamePassword() { if (mDatabase == null) { if (!checkInitialized()) { return; return; } } Loading @@ -924,7 +969,7 @@ public class WebViewDatabase { */ */ void setHttpAuthUsernamePassword(String host, String realm, String username, void setHttpAuthUsernamePassword(String host, String realm, String username, String password) { String password) { if (host == null || realm == null || mDatabase == null) { if (host == null || realm == null || !checkInitialized()) { return; return; } } Loading @@ -949,7 +994,7 @@ public class WebViewDatabase { * String[1] is password. Return null if it can't find anything. * String[1] is password. Return null if it can't find anything. */ */ String[] getHttpAuthUsernamePassword(String host, String realm) { String[] getHttpAuthUsernamePassword(String host, String realm) { if (host == null || realm == null || mDatabase == null){ if (host == null || realm == null || !checkInitialized()){ return null; return null; } } Loading Loading @@ -996,7 +1041,7 @@ public class WebViewDatabase { * Clear HTTP authentication password database * Clear HTTP authentication password database */ */ public void clearHttpAuthUsernamePassword() { public void clearHttpAuthUsernamePassword() { if (mDatabase == null) { if (!checkInitialized()) { return; return; } } Loading @@ -1017,7 +1062,7 @@ public class WebViewDatabase { * @param formdata The form data in HashMap * @param formdata The form data in HashMap */ */ void setFormData(String url, HashMap<String, String> formdata) { void setFormData(String url, HashMap<String, String> formdata) { if (url == null || formdata == null || mDatabase == null) { if (url == null || formdata == null || !checkInitialized()) { return; return; } } Loading Loading @@ -1066,7 +1111,7 @@ public class WebViewDatabase { */ */ ArrayList<String> getFormData(String url, String name) { ArrayList<String> getFormData(String url, String name) { ArrayList<String> values = new ArrayList<String>(); ArrayList<String> values = new ArrayList<String>(); if (url == null || name == null || mDatabase == null) { if (url == null || name == null || !checkInitialized()) { return values; return values; } } Loading Loading @@ -1126,7 +1171,7 @@ public class WebViewDatabase { * Clear form database * Clear form database */ */ public void clearFormData() { public void clearFormData() { if (mDatabase == null) { if (!checkInitialized()) { return; return; } } Loading Loading
core/java/android/webkit/WebViewDatabase.java +154 −109 Original line number Original line Diff line number Diff line Loading @@ -173,16 +173,34 @@ public class WebViewDatabase { private static int mCacheTransactionRefcount; private static int mCacheTransactionRefcount; private WebViewDatabase() { // Initially true until the background thread completes. private boolean mInitialized = false; private WebViewDatabase(final Context context) { new Thread() { @Override public void run() { init(context); } }.start(); // Singleton only, use getInstance() // Singleton only, use getInstance() } } public static synchronized WebViewDatabase getInstance(Context context) { public static synchronized WebViewDatabase getInstance(Context context) { if (mInstance == null) { if (mInstance == null) { mInstance = new WebViewDatabase(); mInstance = new WebViewDatabase(context); } return mInstance; } private synchronized void init(Context context) { if (mInitialized) { return; } try { try { mDatabase = context mDatabase = context.openOrCreateDatabase(DATABASE_FILE, 0, null); .openOrCreateDatabase(DATABASE_FILE, 0, null); } catch (SQLiteException e) { } catch (SQLiteException e) { // try again by deleting the old db and create a new one // try again by deleting the old db and create a new one if (context.deleteDatabase(DATABASE_FILE)) { if (context.deleteDatabase(DATABASE_FILE)) { Loading @@ -193,7 +211,13 @@ public class WebViewDatabase { // mDatabase should not be null, // mDatabase should not be null, // the only case is RequestAPI test has problem to create db // the only case is RequestAPI test has problem to create db if (mDatabase != null && mDatabase.getVersion() != DATABASE_VERSION) { if (mDatabase == null) { mInitialized = true; notify(); return; } if (mDatabase.getVersion() != DATABASE_VERSION) { mDatabase.beginTransaction(); mDatabase.beginTransaction(); try { try { upgradeDatabase(); upgradeDatabase(); Loading @@ -203,11 +227,10 @@ public class WebViewDatabase { } } } } if (mDatabase != null) { // use per table Mutex lock, turn off database lock, this // use per table Mutex lock, turn off database lock, this // improves performance as database's ReentrantLock is expansive // improves performance as database's ReentrantLock is // expansive mDatabase.setLockingEnabled(false); mDatabase.setLockingEnabled(false); } try { try { mCacheDatabase = context.openOrCreateDatabase( mCacheDatabase = context.openOrCreateDatabase( Loading @@ -222,8 +245,13 @@ public class WebViewDatabase { // mCacheDatabase should not be null, // mCacheDatabase should not be null, // the only case is RequestAPI test has problem to create db // the only case is RequestAPI test has problem to create db if (mCacheDatabase != null if (mCacheDatabase == null) { && mCacheDatabase.getVersion() != CACHE_DATABASE_VERSION) { mInitialized = true; notify(); return; } if (mCacheDatabase.getVersion() != CACHE_DATABASE_VERSION) { mCacheDatabase.beginTransaction(); mCacheDatabase.beginTransaction(); try { try { upgradeCacheDatabase(); upgradeCacheDatabase(); Loading @@ -238,16 +266,16 @@ public class WebViewDatabase { CacheManager.removeAllCacheFiles(); CacheManager.removeAllCacheFiles(); } } if (mCacheDatabase != null) { // use read_uncommitted to speed up READ // use read_uncommitted to speed up READ mCacheDatabase.execSQL("PRAGMA read_uncommitted = true;"); mCacheDatabase.execSQL("PRAGMA read_uncommitted = true;"); // as only READ can be called in the non-WebViewWorkerThread, // as only READ can be called in the // and read_uncommitted is used, we can turn off database lock // non-WebViewWorkerThread, and read_uncommitted is used, // to use transaction. // we can turn off database lock to use transaction. mCacheDatabase.setLockingEnabled(false); mCacheDatabase.setLockingEnabled(false); // use InsertHelper for faster insertion // use InsertHelper for faster insertion mCacheInserter = new DatabaseUtils.InsertHelper(mCacheDatabase, mCacheInserter = new DatabaseUtils.InsertHelper(mCacheDatabase, "cache"); "cache"); mCacheUrlColIndex = mCacheInserter mCacheUrlColIndex = mCacheInserter .getColumnIndex(CACHE_URL_COL); .getColumnIndex(CACHE_URL_COL); Loading Loading @@ -275,10 +303,10 @@ public class WebViewDatabase { .getColumnIndex(CACHE_CONTENTDISPOSITION_COL); .getColumnIndex(CACHE_CONTENTDISPOSITION_COL); mCacheCrossDomainColIndex = mCacheInserter mCacheCrossDomainColIndex = mCacheInserter .getColumnIndex(CACHE_CROSSDOMAIN_COL); .getColumnIndex(CACHE_CROSSDOMAIN_COL); } } return mInstance; // Thread done, notify. mInitialized = true; notify(); } } private static void upgradeDatabase() { private static void upgradeDatabase() { Loading Loading @@ -391,8 +419,25 @@ public class WebViewDatabase { } } } } // Wait for the background initialization thread to complete and check the // database creation status. private boolean checkInitialized() { synchronized (this) { while (!mInitialized) { try { wait(); } catch (InterruptedException e) { Log.e(LOGTAG, "Caught exception while checking " + "initialization"); Log.e(LOGTAG, Log.getStackTraceString(e)); } } } return mDatabase != null; } private boolean hasEntries(int tableId) { private boolean hasEntries(int tableId) { if (mDatabase == null) { if (!checkInitialized()) { return false; return false; } } Loading Loading @@ -422,7 +467,7 @@ public class WebViewDatabase { */ */ ArrayList<Cookie> getCookiesForDomain(String domain) { ArrayList<Cookie> getCookiesForDomain(String domain) { ArrayList<Cookie> list = new ArrayList<Cookie>(); ArrayList<Cookie> list = new ArrayList<Cookie>(); if (domain == null || mDatabase == null) { if (domain == null || !checkInitialized()) { return list; return list; } } Loading Loading @@ -481,7 +526,7 @@ public class WebViewDatabase { * deleted. * deleted. */ */ void deleteCookies(String domain, String path, String name) { void deleteCookies(String domain, String path, String name) { if (domain == null || mDatabase == null) { if (domain == null || !checkInitialized()) { return; return; } } Loading @@ -501,7 +546,7 @@ public class WebViewDatabase { */ */ void addCookie(Cookie cookie) { void addCookie(Cookie cookie) { if (cookie.domain == null || cookie.path == null || cookie.name == null if (cookie.domain == null || cookie.path == null || cookie.name == null || mDatabase == null) { || !checkInitialized()) { return; return; } } Loading Loading @@ -534,7 +579,7 @@ public class WebViewDatabase { * Clear cookie database * Clear cookie database */ */ void clearCookies() { void clearCookies() { if (mDatabase == null) { if (!checkInitialized()) { return; return; } } Loading @@ -547,7 +592,7 @@ public class WebViewDatabase { * Clear session cookies, which means cookie doesn't have EXPIRES. * Clear session cookies, which means cookie doesn't have EXPIRES. */ */ void clearSessionCookies() { void clearSessionCookies() { if (mDatabase == null) { if (!checkInitialized()) { return; return; } } Loading @@ -564,7 +609,7 @@ public class WebViewDatabase { * @param now Time for now * @param now Time for now */ */ void clearExpiredCookies(long now) { void clearExpiredCookies(long now) { if (mDatabase == null) { if (!checkInitialized()) { return; return; } } Loading Loading @@ -620,7 +665,7 @@ public class WebViewDatabase { * @return CacheResult The CacheManager.CacheResult * @return CacheResult The CacheManager.CacheResult */ */ CacheResult getCache(String url) { CacheResult getCache(String url) { if (url == null || mCacheDatabase == null) { if (url == null || !checkInitialized()) { return null; return null; } } Loading Loading @@ -660,7 +705,7 @@ public class WebViewDatabase { * @param url The url * @param url The url */ */ void removeCache(String url) { void removeCache(String url) { if (url == null || mCacheDatabase == null) { if (url == null || !checkInitialized()) { return; return; } } Loading @@ -674,7 +719,7 @@ public class WebViewDatabase { * @param c The CacheManager.CacheResult * @param c The CacheManager.CacheResult */ */ void addCache(String url, CacheResult c) { void addCache(String url, CacheResult c) { if (url == null || mCacheDatabase == null) { if (url == null || !checkInitialized()) { return; return; } } Loading @@ -700,7 +745,7 @@ public class WebViewDatabase { * Clear cache database * Clear cache database */ */ void clearCache() { void clearCache() { if (mCacheDatabase == null) { if (!checkInitialized()) { return; return; } } Loading @@ -708,7 +753,7 @@ public class WebViewDatabase { } } boolean hasCache() { boolean hasCache() { if (mCacheDatabase == null) { if (!checkInitialized()) { return false; return false; } } Loading Loading @@ -831,7 +876,7 @@ public class WebViewDatabase { */ */ void setUsernamePassword(String schemePlusHost, String username, void setUsernamePassword(String schemePlusHost, String username, String password) { String password) { if (schemePlusHost == null || mDatabase == null) { if (schemePlusHost == null || !checkInitialized()) { return; return; } } Loading @@ -853,7 +898,7 @@ public class WebViewDatabase { * String[1] is password. Return null if it can't find anything. * String[1] is password. Return null if it can't find anything. */ */ String[] getUsernamePassword(String schemePlusHost) { String[] getUsernamePassword(String schemePlusHost) { if (schemePlusHost == null || mDatabase == null) { if (schemePlusHost == null || !checkInitialized()) { return null; return null; } } Loading Loading @@ -899,7 +944,7 @@ public class WebViewDatabase { * Clear password database * Clear password database */ */ public void clearUsernamePassword() { public void clearUsernamePassword() { if (mDatabase == null) { if (!checkInitialized()) { return; return; } } Loading @@ -924,7 +969,7 @@ public class WebViewDatabase { */ */ void setHttpAuthUsernamePassword(String host, String realm, String username, void setHttpAuthUsernamePassword(String host, String realm, String username, String password) { String password) { if (host == null || realm == null || mDatabase == null) { if (host == null || realm == null || !checkInitialized()) { return; return; } } Loading @@ -949,7 +994,7 @@ public class WebViewDatabase { * String[1] is password. Return null if it can't find anything. * String[1] is password. Return null if it can't find anything. */ */ String[] getHttpAuthUsernamePassword(String host, String realm) { String[] getHttpAuthUsernamePassword(String host, String realm) { if (host == null || realm == null || mDatabase == null){ if (host == null || realm == null || !checkInitialized()){ return null; return null; } } Loading Loading @@ -996,7 +1041,7 @@ public class WebViewDatabase { * Clear HTTP authentication password database * Clear HTTP authentication password database */ */ public void clearHttpAuthUsernamePassword() { public void clearHttpAuthUsernamePassword() { if (mDatabase == null) { if (!checkInitialized()) { return; return; } } Loading @@ -1017,7 +1062,7 @@ public class WebViewDatabase { * @param formdata The form data in HashMap * @param formdata The form data in HashMap */ */ void setFormData(String url, HashMap<String, String> formdata) { void setFormData(String url, HashMap<String, String> formdata) { if (url == null || formdata == null || mDatabase == null) { if (url == null || formdata == null || !checkInitialized()) { return; return; } } Loading Loading @@ -1066,7 +1111,7 @@ public class WebViewDatabase { */ */ ArrayList<String> getFormData(String url, String name) { ArrayList<String> getFormData(String url, String name) { ArrayList<String> values = new ArrayList<String>(); ArrayList<String> values = new ArrayList<String>(); if (url == null || name == null || mDatabase == null) { if (url == null || name == null || !checkInitialized()) { return values; return values; } } Loading Loading @@ -1126,7 +1171,7 @@ public class WebViewDatabase { * Clear form database * Clear form database */ */ public void clearFormData() { public void clearFormData() { if (mDatabase == null) { if (!checkInitialized()) { return; return; } } Loading