Avoid superfluous calls to CacheManager with the Chromium HTTP stack

When using the Chromium HTTP stack, most of the calls to CacheManager
methods are not required, as we're not using its cache. These methods
are now marked with asserts to make this clear.

Also avoid creating the cache database in WebViewDatabase.

This will avoid creating the database databases/webviewCache.db and
the directory cache/webviewCache.

Bug: 3270236
Change-Id: I68f94dde16830ed817454d5e1af961f41b71d018
This commit is contained in:
Steve Block
2011-01-04 14:26:27 +00:00
parent b3b98d9b70
commit 808751fe7a
8 changed files with 118 additions and 20 deletions

View File

@@ -488,8 +488,10 @@ class BrowserFrame extends Handler {
} }
} }
} }
WebViewWorker.getHandler().sendEmptyMessage( if (!JniUtil.useChromiumHttpStack()) {
WebViewWorker.MSG_TRIM_CACHE); WebViewWorker.getHandler().sendEmptyMessage(
WebViewWorker.MSG_TRIM_CACHE);
}
break; break;
} }

View File

@@ -18,6 +18,7 @@ package android.webkit;
import android.net.http.Headers; import android.net.http.Headers;
import android.text.TextUtils; import android.text.TextUtils;
import android.webkit.JniUtil;
/** /**
* This class is a concrete implementation of StreamLoader that uses a * This class is a concrete implementation of StreamLoader that uses a
@@ -36,6 +37,9 @@ class CacheLoader extends StreamLoader {
*/ */
CacheLoader(LoadListener loadListener, CacheManager.CacheResult result) { CacheLoader(LoadListener loadListener, CacheManager.CacheResult result) {
super(loadListener); super(loadListener);
assert !JniUtil.useChromiumHttpStack();
mCacheResult = result; mCacheResult = result;
} }

View File

@@ -190,6 +190,11 @@ public final class CacheManager {
* @param context The application context. * @param context The application context.
*/ */
static void init(Context context) { static void init(Context context) {
if (JniUtil.useChromiumHttpStack()) {
// TODO: Need to init mBaseDir.
return;
}
mDataBase = WebViewDatabase.getInstance(context.getApplicationContext()); mDataBase = WebViewDatabase.getInstance(context.getApplicationContext());
mBaseDir = new File(context.getCacheDir(), "webviewCache"); mBaseDir = new File(context.getCacheDir(), "webviewCache");
if (createCacheDirectory() && mClearCacheOnInit) { if (createCacheDirectory() && mClearCacheOnInit) {
@@ -204,6 +209,8 @@ public final class CacheManager {
* @return true if the cache directory didn't exist and was created. * @return true if the cache directory didn't exist and was created.
*/ */
static private boolean createCacheDirectory() { static private boolean createCacheDirectory() {
assert !JniUtil.useChromiumHttpStack();
if (!mBaseDir.exists()) { if (!mBaseDir.exists()) {
if(!mBaseDir.mkdirs()) { if(!mBaseDir.mkdirs()) {
Log.w(LOGTAG, "Unable to create webviewCache directory"); Log.w(LOGTAG, "Unable to create webviewCache directory");
@@ -245,6 +252,8 @@ public final class CacheManager {
* @param disabled Whether the cache should be disabled * @param disabled Whether the cache should be disabled
*/ */
static void setCacheDisabled(boolean disabled) { static void setCacheDisabled(boolean disabled) {
assert !JniUtil.useChromiumHttpStack();
if (disabled == mDisabled) { if (disabled == mDisabled) {
return; return;
} }
@@ -269,6 +278,8 @@ public final class CacheManager {
// only called from WebViewWorkerThread // only called from WebViewWorkerThread
// make sure to call enableTransaction/disableTransaction in pair // make sure to call enableTransaction/disableTransaction in pair
static boolean enableTransaction() { static boolean enableTransaction() {
assert !JniUtil.useChromiumHttpStack();
if (++mRefCount == 1) { if (++mRefCount == 1) {
mDataBase.startCacheTransaction(); mDataBase.startCacheTransaction();
return true; return true;
@@ -279,6 +290,8 @@ public final class CacheManager {
// only called from WebViewWorkerThread // only called from WebViewWorkerThread
// make sure to call enableTransaction/disableTransaction in pair // make sure to call enableTransaction/disableTransaction in pair
static boolean disableTransaction() { static boolean disableTransaction() {
assert !JniUtil.useChromiumHttpStack();
if (--mRefCount == 0) { if (--mRefCount == 0) {
mDataBase.endCacheTransaction(); mDataBase.endCacheTransaction();
return true; return true;
@@ -289,12 +302,16 @@ public final class CacheManager {
// only called from WebViewWorkerThread // only called from WebViewWorkerThread
// make sure to call startTransaction/endTransaction in pair // make sure to call startTransaction/endTransaction in pair
static boolean startTransaction() { static boolean startTransaction() {
assert !JniUtil.useChromiumHttpStack();
return mDataBase.startCacheTransaction(); return mDataBase.startCacheTransaction();
} }
// only called from WebViewWorkerThread // only called from WebViewWorkerThread
// make sure to call startTransaction/endTransaction in pair // make sure to call startTransaction/endTransaction in pair
static boolean endTransaction() { static boolean endTransaction() {
assert !JniUtil.useChromiumHttpStack();
boolean ret = mDataBase.endCacheTransaction(); boolean ret = mDataBase.endCacheTransaction();
if (++mTrimCacheCount >= TRIM_CACHE_INTERVAL) { if (++mTrimCacheCount >= TRIM_CACHE_INTERVAL) {
mTrimCacheCount = 0; mTrimCacheCount = 0;
@@ -347,8 +364,12 @@ public final class CacheManager {
return null; return null;
} }
String databaseKey = getDatabaseKey(url, postIdentifier); if (JniUtil.useChromiumHttpStack()) {
// TODO: Implement this.
return null;
}
String databaseKey = getDatabaseKey(url, postIdentifier);
CacheResult result = mDataBase.getCache(databaseKey); CacheResult result = mDataBase.getCache(databaseKey);
if (result == null) { if (result == null) {
return null; return null;
@@ -415,6 +436,11 @@ public final class CacheManager {
@Deprecated @Deprecated
public static CacheResult createCacheFile(String url, int statusCode, public static CacheResult createCacheFile(String url, int statusCode,
Headers headers, String mimeType, boolean forceCache) { Headers headers, String mimeType, boolean forceCache) {
if (JniUtil.useChromiumHttpStack()) {
// TODO: Implement this.
return null;
}
return createCacheFile(url, statusCode, headers, mimeType, 0, return createCacheFile(url, statusCode, headers, mimeType, 0,
forceCache); forceCache);
} }
@@ -422,6 +448,8 @@ public final class CacheManager {
static CacheResult createCacheFile(String url, int statusCode, static CacheResult createCacheFile(String url, int statusCode,
Headers headers, String mimeType, long postIdentifier, Headers headers, String mimeType, long postIdentifier,
boolean forceCache) { boolean forceCache) {
assert !JniUtil.useChromiumHttpStack();
if (!forceCache && mDisabled) { if (!forceCache && mDisabled) {
return null; return null;
} }
@@ -493,6 +521,11 @@ public final class CacheManager {
return; return;
} }
if (JniUtil.useChromiumHttpStack()) {
// TODO: Implement this.
return;
}
if (!cacheRet.outFile.exists()) { if (!cacheRet.outFile.exists()) {
// the file in the cache directory can be removed by the system // the file in the cache directory can be removed by the system
return; return;
@@ -520,6 +553,8 @@ public final class CacheManager {
} }
static boolean cleanupCacheFile(CacheResult cacheRet) { static boolean cleanupCacheFile(CacheResult cacheRet) {
assert !JniUtil.useChromiumHttpStack();
try { try {
cacheRet.outStream.close(); cacheRet.outStream.close();
} catch (IOException e) { } catch (IOException e) {
@@ -534,6 +569,8 @@ public final class CacheManager {
* @return Whether the removal succeeded. * @return Whether the removal succeeded.
*/ */
static boolean removeAllCacheFiles() { static boolean removeAllCacheFiles() {
assert !JniUtil.useChromiumHttpStack();
// Note, this is called before init() when the database is // Note, this is called before init() when the database is
// created or upgraded. // created or upgraded.
if (mBaseDir == null) { if (mBaseDir == null) {
@@ -570,6 +607,8 @@ public final class CacheManager {
} }
static void trimCacheIfNeeded() { static void trimCacheIfNeeded() {
assert !JniUtil.useChromiumHttpStack();
if (mDataBase.getCacheTotalSize() > CACHE_THRESHOLD) { if (mDataBase.getCacheTotalSize() > CACHE_THRESHOLD) {
List<String> pathList = mDataBase.trimCache(CACHE_TRIM_AMOUNT); List<String> pathList = mDataBase.trimCache(CACHE_TRIM_AMOUNT);
int size = pathList.size(); int size = pathList.size();
@@ -603,6 +642,8 @@ public final class CacheManager {
} }
static void clearCache() { static void clearCache() {
assert !JniUtil.useChromiumHttpStack();
// delete database // delete database
mDataBase.clearCache(); mDataBase.clearCache();
} }
@@ -617,12 +658,16 @@ public final class CacheManager {
} }
private static String getDatabaseKey(String url, long postIdentifier) { private static String getDatabaseKey(String url, long postIdentifier) {
assert !JniUtil.useChromiumHttpStack();
if (postIdentifier == 0) return url; if (postIdentifier == 0) return url;
return postIdentifier + url; return postIdentifier + url;
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
private static void setupFiles(String url, CacheResult cacheRet) { private static void setupFiles(String url, CacheResult cacheRet) {
assert !JniUtil.useChromiumHttpStack();
if (true) { if (true) {
// Note: SHA1 is much stronger hash. But the cost of setupFiles() is // Note: SHA1 is much stronger hash. But the cost of setupFiles() is
// 3.2% cpu time for a fresh load of nytimes.com. While a simple // 3.2% cpu time for a fresh load of nytimes.com. While a simple
@@ -689,6 +734,8 @@ public final class CacheManager {
} }
private static void appendAsHex(int i, StringBuffer ret) { private static void appendAsHex(int i, StringBuffer ret) {
assert !JniUtil.useChromiumHttpStack();
String hex = Integer.toHexString(i); String hex = Integer.toHexString(i);
switch (hex.length()) { switch (hex.length()) {
case 1: case 1:
@@ -718,6 +765,8 @@ public final class CacheManager {
private static CacheResult parseHeaders(int statusCode, Headers headers, private static CacheResult parseHeaders(int statusCode, Headers headers,
String mimeType) { String mimeType) {
assert !JniUtil.useChromiumHttpStack();
// if the contentLength is already larger than CACHE_MAX_SIZE, skip it // if the contentLength is already larger than CACHE_MAX_SIZE, skip it
if (headers.getContentLength() > CACHE_MAX_SIZE) return null; if (headers.getContentLength() > CACHE_MAX_SIZE) return null;

View File

@@ -22,6 +22,7 @@ import android.net.http.RequestHandle;
import android.os.Build; import android.os.Build;
import android.util.Log; import android.util.Log;
import android.webkit.CacheManager.CacheResult; import android.webkit.CacheManager.CacheResult;
import android.webkit.JniUtil;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@@ -56,6 +57,8 @@ class FrameLoader {
FrameLoader(LoadListener listener, WebSettings settings, FrameLoader(LoadListener listener, WebSettings settings,
String method, WebResourceResponse interceptResponse) { String method, WebResourceResponse interceptResponse) {
assert !JniUtil.useChromiumHttpStack();
mListener = listener; mListener = listener;
mHeaders = null; mHeaders = null;
mMethod = method; mMethod = method;
@@ -148,9 +151,10 @@ class FrameLoader {
} }
/* package */ private static boolean handleLocalFile(String url, LoadListener loadListener,
static boolean handleLocalFile(String url, LoadListener loadListener,
WebSettings settings) { WebSettings settings) {
assert !JniUtil.useChromiumHttpStack();
// Attempt to decode the percent-encoded url before passing to the // Attempt to decode the percent-encoded url before passing to the
// local loaders. // local loaders.
try { try {

View File

@@ -35,6 +35,7 @@ import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.util.Log; import android.util.Log;
import android.webkit.CacheManager.CacheResult; import android.webkit.CacheManager.CacheResult;
import android.webkit.JniUtil;
import com.android.internal.R; import com.android.internal.R;
@@ -156,6 +157,8 @@ class LoadListener extends Handler implements EventHandler {
int nativeLoader, boolean synchronous, boolean isMainPageLoader, int nativeLoader, boolean synchronous, boolean isMainPageLoader,
boolean isMainResource, boolean userGesture, long postIdentifier, boolean isMainResource, boolean userGesture, long postIdentifier,
String username, String password) { String username, String password) {
assert !JniUtil.useChromiumHttpStack();
if (DebugFlags.LOAD_LISTENER) { if (DebugFlags.LOAD_LISTENER) {
Log.v(LOGTAG, "LoadListener constructor url=" + url); Log.v(LOGTAG, "LoadListener constructor url=" + url);
} }
@@ -991,6 +994,7 @@ class LoadListener extends Handler implements EventHandler {
* URL. * URL.
*/ */
static boolean willLoadFromCache(String url, long identifier) { static boolean willLoadFromCache(String url, long identifier) {
assert !JniUtil.useChromiumHttpStack();
boolean inCache = boolean inCache =
CacheManager.getCacheFile(url, identifier, null) != null; CacheManager.getCacheFile(url, identifier, null) != null;
if (DebugFlags.LOAD_LISTENER) { if (DebugFlags.LOAD_LISTENER) {

View File

@@ -38,6 +38,7 @@ import android.view.View;
import android.webkit.DeviceMotionService; import android.webkit.DeviceMotionService;
import android.webkit.DeviceMotionAndOrientationManager; import android.webkit.DeviceMotionAndOrientationManager;
import android.webkit.DeviceOrientationService; import android.webkit.DeviceOrientationService;
import android.webkit.JniUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@@ -1201,15 +1202,19 @@ final class WebViewCore {
Process.setThreadPriority(mTid, Process.setThreadPriority(mTid,
Process.THREAD_PRIORITY_BACKGROUND); Process.THREAD_PRIORITY_BACKGROUND);
pauseTimers(); pauseTimers();
WebViewWorker.getHandler().sendEmptyMessage( if (!JniUtil.useChromiumHttpStack()) {
WebViewWorker.MSG_PAUSE_CACHE_TRANSACTION); WebViewWorker.getHandler().sendEmptyMessage(
WebViewWorker.MSG_PAUSE_CACHE_TRANSACTION);
}
break; break;
case RESUME_TIMERS: case RESUME_TIMERS:
Process.setThreadPriority(mTid, mSavedPriority); Process.setThreadPriority(mTid, mSavedPriority);
resumeTimers(); resumeTimers();
WebViewWorker.getHandler().sendEmptyMessage( if (!JniUtil.useChromiumHttpStack()) {
WebViewWorker.MSG_RESUME_CACHE_TRANSACTION); WebViewWorker.getHandler().sendEmptyMessage(
WebViewWorker.MSG_RESUME_CACHE_TRANSACTION);
}
break; break;
case ON_PAUSE: case ON_PAUSE:
@@ -1733,7 +1738,7 @@ final class WebViewCore {
private void clearCache(boolean includeDiskFiles) { private void clearCache(boolean includeDiskFiles) {
mBrowserFrame.clearCache(); mBrowserFrame.clearCache();
if (includeDiskFiles) { if (includeDiskFiles && !JniUtil.useChromiumHttpStack()) {
CacheManager.removeAllCacheFiles(); CacheManager.removeAllCacheFiles();
} }
} }
@@ -2150,12 +2155,14 @@ final class WebViewCore {
// called by JNI // called by JNI
private void sendNotifyProgressFinished() { private void sendNotifyProgressFinished() {
sendUpdateTextEntry(); sendUpdateTextEntry();
// as CacheManager can behave based on database transaction, we need to if (!JniUtil.useChromiumHttpStack()) {
// call tick() to trigger endTransaction // as CacheManager can behave based on database transaction, we need to
WebViewWorker.getHandler().removeMessages( // call tick() to trigger endTransaction
WebViewWorker.MSG_CACHE_TRANSACTION_TICKER); WebViewWorker.getHandler().removeMessages(
WebViewWorker.getHandler().sendEmptyMessage( WebViewWorker.MSG_CACHE_TRANSACTION_TICKER);
WebViewWorker.MSG_CACHE_TRANSACTION_TICKER); WebViewWorker.getHandler().sendEmptyMessage(
WebViewWorker.MSG_CACHE_TRANSACTION_TICKER);
}
contentDraw(); contentDraw();
} }

View File

@@ -33,6 +33,7 @@ import android.database.sqlite.SQLiteStatement;
import android.util.Log; import android.util.Log;
import android.webkit.CookieManager.Cookie; import android.webkit.CookieManager.Cookie;
import android.webkit.CacheManager.CacheResult; import android.webkit.CacheManager.CacheResult;
import android.webkit.JniUtil;
public class WebViewDatabase { public class WebViewDatabase {
private static final String DATABASE_FILE = "webview.db"; private static final String DATABASE_FILE = "webview.db";
@@ -202,6 +203,17 @@ public class WebViewDatabase {
return; return;
} }
initDatabase(context);
if (!JniUtil.useChromiumHttpStack()) {
initCacheDatabase(context);
}
// Thread done, notify.
mInitialized = true;
notify();
}
private void initDatabase(Context context) {
try { try {
mDatabase = context.openOrCreateDatabase(DATABASE_FILE, 0, null); mDatabase = context.openOrCreateDatabase(DATABASE_FILE, 0, null);
} catch (SQLiteException e) { } catch (SQLiteException e) {
@@ -234,6 +246,10 @@ public class WebViewDatabase {
// improves performance as database's ReentrantLock is // improves performance as database's ReentrantLock is
// expansive // expansive
mDatabase.setLockingEnabled(false); mDatabase.setLockingEnabled(false);
}
private void initCacheDatabase(Context context) {
assert !JniUtil.useChromiumHttpStack();
try { try {
mCacheDatabase = context.openOrCreateDatabase( mCacheDatabase = context.openOrCreateDatabase(
@@ -306,10 +322,6 @@ public class WebViewDatabase {
.getColumnIndex(CACHE_CONTENTDISPOSITION_COL); .getColumnIndex(CACHE_CONTENTDISPOSITION_COL);
mCacheCrossDomainColIndex = mCacheInserter mCacheCrossDomainColIndex = mCacheInserter
.getColumnIndex(CACHE_CROSSDOMAIN_COL); .getColumnIndex(CACHE_CROSSDOMAIN_COL);
// Thread done, notify.
mInitialized = true;
notify();
} }
private static void upgradeDatabase() { private static void upgradeDatabase() {
@@ -668,6 +680,8 @@ public class WebViewDatabase {
* @return CacheResult The CacheManager.CacheResult * @return CacheResult The CacheManager.CacheResult
*/ */
CacheResult getCache(String url) { CacheResult getCache(String url) {
assert !JniUtil.useChromiumHttpStack();
if (url == null || !checkInitialized()) { if (url == null || !checkInitialized()) {
return null; return null;
} }
@@ -708,6 +722,8 @@ public class WebViewDatabase {
* @param url The url * @param url The url
*/ */
void removeCache(String url) { void removeCache(String url) {
assert !JniUtil.useChromiumHttpStack();
if (url == null || !checkInitialized()) { if (url == null || !checkInitialized()) {
return; return;
} }
@@ -722,6 +738,8 @@ 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) {
assert !JniUtil.useChromiumHttpStack();
if (url == null || !checkInitialized()) { if (url == null || !checkInitialized()) {
return; return;
} }

View File

@@ -125,6 +125,7 @@ final class WebViewWorker extends Handler {
break; break;
} }
case MSG_CREATE_CACHE: { case MSG_CREATE_CACHE: {
assert !JniUtil.useChromiumHttpStack();
CacheCreateData data = (CacheCreateData) msg.obj; CacheCreateData data = (CacheCreateData) msg.obj;
CacheManager.CacheResult cache = CacheManager.createCacheFile( CacheManager.CacheResult cache = CacheManager.createCacheFile(
data.mUrl, data.mStatusCode, data.mHeaders, data.mUrl, data.mStatusCode, data.mHeaders,
@@ -137,6 +138,7 @@ final class WebViewWorker extends Handler {
break; break;
} }
case MSG_UPDATE_CACHE_ENCODING: { case MSG_UPDATE_CACHE_ENCODING: {
assert !JniUtil.useChromiumHttpStack();
CacheEncoding data = (CacheEncoding) msg.obj; CacheEncoding data = (CacheEncoding) msg.obj;
CacheManager.CacheResult cache = mCacheResultMap CacheManager.CacheResult cache = mCacheResultMap
.get(data.mListener); .get(data.mListener);
@@ -146,6 +148,7 @@ final class WebViewWorker extends Handler {
break; break;
} }
case MSG_APPEND_CACHE: { case MSG_APPEND_CACHE: {
assert !JniUtil.useChromiumHttpStack();
CacheData data = (CacheData) msg.obj; CacheData data = (CacheData) msg.obj;
CacheManager.CacheResult cache = mCacheResultMap CacheManager.CacheResult cache = mCacheResultMap
.get(data.mListener); .get(data.mListener);
@@ -168,6 +171,7 @@ final class WebViewWorker extends Handler {
break; break;
} }
case MSG_SAVE_CACHE: { case MSG_SAVE_CACHE: {
assert !JniUtil.useChromiumHttpStack();
CacheSaveData data = (CacheSaveData) msg.obj; CacheSaveData data = (CacheSaveData) msg.obj;
CacheManager.CacheResult cache = mCacheResultMap CacheManager.CacheResult cache = mCacheResultMap
.get(data.mListener); .get(data.mListener);
@@ -178,6 +182,7 @@ final class WebViewWorker extends Handler {
break; break;
} }
case MSG_REMOVE_CACHE: { case MSG_REMOVE_CACHE: {
assert !JniUtil.useChromiumHttpStack();
LoadListener listener = (LoadListener) msg.obj; LoadListener listener = (LoadListener) msg.obj;
CacheManager.CacheResult cache = mCacheResultMap.get(listener); CacheManager.CacheResult cache = mCacheResultMap.get(listener);
if (cache != null) { if (cache != null) {
@@ -187,14 +192,17 @@ final class WebViewWorker extends Handler {
break; break;
} }
case MSG_TRIM_CACHE: { case MSG_TRIM_CACHE: {
assert !JniUtil.useChromiumHttpStack();
CacheManager.trimCacheIfNeeded(); CacheManager.trimCacheIfNeeded();
break; break;
} }
case MSG_CLEAR_CACHE: { case MSG_CLEAR_CACHE: {
assert !JniUtil.useChromiumHttpStack();
CacheManager.clearCache(); CacheManager.clearCache();
break; break;
} }
case MSG_CACHE_TRANSACTION_TICKER: { case MSG_CACHE_TRANSACTION_TICKER: {
assert !JniUtil.useChromiumHttpStack();
if (!mCacheTickersBlocked) { if (!mCacheTickersBlocked) {
CacheManager.endTransaction(); CacheManager.endTransaction();
CacheManager.startTransaction(); CacheManager.startTransaction();
@@ -204,6 +212,7 @@ final class WebViewWorker extends Handler {
break; break;
} }
case MSG_PAUSE_CACHE_TRANSACTION: { case MSG_PAUSE_CACHE_TRANSACTION: {
assert !JniUtil.useChromiumHttpStack();
if (CacheManager.disableTransaction()) { if (CacheManager.disableTransaction()) {
mCacheTickersBlocked = true; mCacheTickersBlocked = true;
removeMessages(MSG_CACHE_TRANSACTION_TICKER); removeMessages(MSG_CACHE_TRANSACTION_TICKER);
@@ -211,6 +220,7 @@ final class WebViewWorker extends Handler {
break; break;
} }
case MSG_RESUME_CACHE_TRANSACTION: { case MSG_RESUME_CACHE_TRANSACTION: {
assert !JniUtil.useChromiumHttpStack();
if (CacheManager.enableTransaction()) { if (CacheManager.enableTransaction()) {
mCacheTickersBlocked = false; mCacheTickersBlocked = false;
sendEmptyMessageDelayed(MSG_CACHE_TRANSACTION_TICKER, sendEmptyMessageDelayed(MSG_CACHE_TRANSACTION_TICKER,