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:
@@ -488,8 +488,10 @@ class BrowserFrame extends Handler {
|
||||
}
|
||||
}
|
||||
}
|
||||
WebViewWorker.getHandler().sendEmptyMessage(
|
||||
WebViewWorker.MSG_TRIM_CACHE);
|
||||
if (!JniUtil.useChromiumHttpStack()) {
|
||||
WebViewWorker.getHandler().sendEmptyMessage(
|
||||
WebViewWorker.MSG_TRIM_CACHE);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ package android.webkit;
|
||||
|
||||
import android.net.http.Headers;
|
||||
import android.text.TextUtils;
|
||||
import android.webkit.JniUtil;
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
super(loadListener);
|
||||
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
|
||||
mCacheResult = result;
|
||||
}
|
||||
|
||||
|
||||
@@ -190,6 +190,11 @@ public final class CacheManager {
|
||||
* @param context The application context.
|
||||
*/
|
||||
static void init(Context context) {
|
||||
if (JniUtil.useChromiumHttpStack()) {
|
||||
// TODO: Need to init mBaseDir.
|
||||
return;
|
||||
}
|
||||
|
||||
mDataBase = WebViewDatabase.getInstance(context.getApplicationContext());
|
||||
mBaseDir = new File(context.getCacheDir(), "webviewCache");
|
||||
if (createCacheDirectory() && mClearCacheOnInit) {
|
||||
@@ -204,6 +209,8 @@ public final class CacheManager {
|
||||
* @return true if the cache directory didn't exist and was created.
|
||||
*/
|
||||
static private boolean createCacheDirectory() {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
|
||||
if (!mBaseDir.exists()) {
|
||||
if(!mBaseDir.mkdirs()) {
|
||||
Log.w(LOGTAG, "Unable to create webviewCache directory");
|
||||
@@ -245,6 +252,8 @@ public final class CacheManager {
|
||||
* @param disabled Whether the cache should be disabled
|
||||
*/
|
||||
static void setCacheDisabled(boolean disabled) {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
|
||||
if (disabled == mDisabled) {
|
||||
return;
|
||||
}
|
||||
@@ -269,6 +278,8 @@ public final class CacheManager {
|
||||
// only called from WebViewWorkerThread
|
||||
// make sure to call enableTransaction/disableTransaction in pair
|
||||
static boolean enableTransaction() {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
|
||||
if (++mRefCount == 1) {
|
||||
mDataBase.startCacheTransaction();
|
||||
return true;
|
||||
@@ -279,6 +290,8 @@ public final class CacheManager {
|
||||
// only called from WebViewWorkerThread
|
||||
// make sure to call enableTransaction/disableTransaction in pair
|
||||
static boolean disableTransaction() {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
|
||||
if (--mRefCount == 0) {
|
||||
mDataBase.endCacheTransaction();
|
||||
return true;
|
||||
@@ -289,12 +302,16 @@ public final class CacheManager {
|
||||
// only called from WebViewWorkerThread
|
||||
// make sure to call startTransaction/endTransaction in pair
|
||||
static boolean startTransaction() {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
|
||||
return mDataBase.startCacheTransaction();
|
||||
}
|
||||
|
||||
// only called from WebViewWorkerThread
|
||||
// make sure to call startTransaction/endTransaction in pair
|
||||
static boolean endTransaction() {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
|
||||
boolean ret = mDataBase.endCacheTransaction();
|
||||
if (++mTrimCacheCount >= TRIM_CACHE_INTERVAL) {
|
||||
mTrimCacheCount = 0;
|
||||
@@ -347,8 +364,12 @@ public final class CacheManager {
|
||||
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);
|
||||
if (result == null) {
|
||||
return null;
|
||||
@@ -415,6 +436,11 @@ public final class CacheManager {
|
||||
@Deprecated
|
||||
public static CacheResult createCacheFile(String url, int statusCode,
|
||||
Headers headers, String mimeType, boolean forceCache) {
|
||||
if (JniUtil.useChromiumHttpStack()) {
|
||||
// TODO: Implement this.
|
||||
return null;
|
||||
}
|
||||
|
||||
return createCacheFile(url, statusCode, headers, mimeType, 0,
|
||||
forceCache);
|
||||
}
|
||||
@@ -422,6 +448,8 @@ public final class CacheManager {
|
||||
static CacheResult createCacheFile(String url, int statusCode,
|
||||
Headers headers, String mimeType, long postIdentifier,
|
||||
boolean forceCache) {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
|
||||
if (!forceCache && mDisabled) {
|
||||
return null;
|
||||
}
|
||||
@@ -493,6 +521,11 @@ public final class CacheManager {
|
||||
return;
|
||||
}
|
||||
|
||||
if (JniUtil.useChromiumHttpStack()) {
|
||||
// TODO: Implement this.
|
||||
return;
|
||||
}
|
||||
|
||||
if (!cacheRet.outFile.exists()) {
|
||||
// the file in the cache directory can be removed by the system
|
||||
return;
|
||||
@@ -520,6 +553,8 @@ public final class CacheManager {
|
||||
}
|
||||
|
||||
static boolean cleanupCacheFile(CacheResult cacheRet) {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
|
||||
try {
|
||||
cacheRet.outStream.close();
|
||||
} catch (IOException e) {
|
||||
@@ -534,6 +569,8 @@ public final class CacheManager {
|
||||
* @return Whether the removal succeeded.
|
||||
*/
|
||||
static boolean removeAllCacheFiles() {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
|
||||
// Note, this is called before init() when the database is
|
||||
// created or upgraded.
|
||||
if (mBaseDir == null) {
|
||||
@@ -570,6 +607,8 @@ public final class CacheManager {
|
||||
}
|
||||
|
||||
static void trimCacheIfNeeded() {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
|
||||
if (mDataBase.getCacheTotalSize() > CACHE_THRESHOLD) {
|
||||
List<String> pathList = mDataBase.trimCache(CACHE_TRIM_AMOUNT);
|
||||
int size = pathList.size();
|
||||
@@ -603,6 +642,8 @@ public final class CacheManager {
|
||||
}
|
||||
|
||||
static void clearCache() {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
|
||||
// delete database
|
||||
mDataBase.clearCache();
|
||||
}
|
||||
@@ -617,12 +658,16 @@ public final class CacheManager {
|
||||
}
|
||||
|
||||
private static String getDatabaseKey(String url, long postIdentifier) {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
|
||||
if (postIdentifier == 0) return url;
|
||||
return postIdentifier + url;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private static void setupFiles(String url, CacheResult cacheRet) {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
|
||||
if (true) {
|
||||
// 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
|
||||
@@ -689,6 +734,8 @@ public final class CacheManager {
|
||||
}
|
||||
|
||||
private static void appendAsHex(int i, StringBuffer ret) {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
|
||||
String hex = Integer.toHexString(i);
|
||||
switch (hex.length()) {
|
||||
case 1:
|
||||
@@ -718,6 +765,8 @@ public final class CacheManager {
|
||||
|
||||
private static CacheResult parseHeaders(int statusCode, Headers headers,
|
||||
String mimeType) {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
|
||||
// if the contentLength is already larger than CACHE_MAX_SIZE, skip it
|
||||
if (headers.getContentLength() > CACHE_MAX_SIZE) return null;
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ import android.net.http.RequestHandle;
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
import android.webkit.CacheManager.CacheResult;
|
||||
import android.webkit.JniUtil;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -56,6 +57,8 @@ class FrameLoader {
|
||||
|
||||
FrameLoader(LoadListener listener, WebSettings settings,
|
||||
String method, WebResourceResponse interceptResponse) {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
|
||||
mListener = listener;
|
||||
mHeaders = null;
|
||||
mMethod = method;
|
||||
@@ -148,9 +151,10 @@ class FrameLoader {
|
||||
|
||||
}
|
||||
|
||||
/* package */
|
||||
static boolean handleLocalFile(String url, LoadListener loadListener,
|
||||
private static boolean handleLocalFile(String url, LoadListener loadListener,
|
||||
WebSettings settings) {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
|
||||
// Attempt to decode the percent-encoded url before passing to the
|
||||
// local loaders.
|
||||
try {
|
||||
|
||||
@@ -35,6 +35,7 @@ import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.util.Log;
|
||||
import android.webkit.CacheManager.CacheResult;
|
||||
import android.webkit.JniUtil;
|
||||
|
||||
import com.android.internal.R;
|
||||
|
||||
@@ -156,6 +157,8 @@ class LoadListener extends Handler implements EventHandler {
|
||||
int nativeLoader, boolean synchronous, boolean isMainPageLoader,
|
||||
boolean isMainResource, boolean userGesture, long postIdentifier,
|
||||
String username, String password) {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
|
||||
if (DebugFlags.LOAD_LISTENER) {
|
||||
Log.v(LOGTAG, "LoadListener constructor url=" + url);
|
||||
}
|
||||
@@ -991,6 +994,7 @@ class LoadListener extends Handler implements EventHandler {
|
||||
* URL.
|
||||
*/
|
||||
static boolean willLoadFromCache(String url, long identifier) {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
boolean inCache =
|
||||
CacheManager.getCacheFile(url, identifier, null) != null;
|
||||
if (DebugFlags.LOAD_LISTENER) {
|
||||
|
||||
@@ -38,6 +38,7 @@ import android.view.View;
|
||||
import android.webkit.DeviceMotionService;
|
||||
import android.webkit.DeviceMotionAndOrientationManager;
|
||||
import android.webkit.DeviceOrientationService;
|
||||
import android.webkit.JniUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
@@ -1201,15 +1202,19 @@ final class WebViewCore {
|
||||
Process.setThreadPriority(mTid,
|
||||
Process.THREAD_PRIORITY_BACKGROUND);
|
||||
pauseTimers();
|
||||
WebViewWorker.getHandler().sendEmptyMessage(
|
||||
WebViewWorker.MSG_PAUSE_CACHE_TRANSACTION);
|
||||
if (!JniUtil.useChromiumHttpStack()) {
|
||||
WebViewWorker.getHandler().sendEmptyMessage(
|
||||
WebViewWorker.MSG_PAUSE_CACHE_TRANSACTION);
|
||||
}
|
||||
break;
|
||||
|
||||
case RESUME_TIMERS:
|
||||
Process.setThreadPriority(mTid, mSavedPriority);
|
||||
resumeTimers();
|
||||
WebViewWorker.getHandler().sendEmptyMessage(
|
||||
WebViewWorker.MSG_RESUME_CACHE_TRANSACTION);
|
||||
if (!JniUtil.useChromiumHttpStack()) {
|
||||
WebViewWorker.getHandler().sendEmptyMessage(
|
||||
WebViewWorker.MSG_RESUME_CACHE_TRANSACTION);
|
||||
}
|
||||
break;
|
||||
|
||||
case ON_PAUSE:
|
||||
@@ -1733,7 +1738,7 @@ final class WebViewCore {
|
||||
|
||||
private void clearCache(boolean includeDiskFiles) {
|
||||
mBrowserFrame.clearCache();
|
||||
if (includeDiskFiles) {
|
||||
if (includeDiskFiles && !JniUtil.useChromiumHttpStack()) {
|
||||
CacheManager.removeAllCacheFiles();
|
||||
}
|
||||
}
|
||||
@@ -2150,12 +2155,14 @@ final class WebViewCore {
|
||||
// called by JNI
|
||||
private void sendNotifyProgressFinished() {
|
||||
sendUpdateTextEntry();
|
||||
// as CacheManager can behave based on database transaction, we need to
|
||||
// call tick() to trigger endTransaction
|
||||
WebViewWorker.getHandler().removeMessages(
|
||||
WebViewWorker.MSG_CACHE_TRANSACTION_TICKER);
|
||||
WebViewWorker.getHandler().sendEmptyMessage(
|
||||
WebViewWorker.MSG_CACHE_TRANSACTION_TICKER);
|
||||
if (!JniUtil.useChromiumHttpStack()) {
|
||||
// as CacheManager can behave based on database transaction, we need to
|
||||
// call tick() to trigger endTransaction
|
||||
WebViewWorker.getHandler().removeMessages(
|
||||
WebViewWorker.MSG_CACHE_TRANSACTION_TICKER);
|
||||
WebViewWorker.getHandler().sendEmptyMessage(
|
||||
WebViewWorker.MSG_CACHE_TRANSACTION_TICKER);
|
||||
}
|
||||
contentDraw();
|
||||
}
|
||||
|
||||
|
||||
@@ -33,6 +33,7 @@ import android.database.sqlite.SQLiteStatement;
|
||||
import android.util.Log;
|
||||
import android.webkit.CookieManager.Cookie;
|
||||
import android.webkit.CacheManager.CacheResult;
|
||||
import android.webkit.JniUtil;
|
||||
|
||||
public class WebViewDatabase {
|
||||
private static final String DATABASE_FILE = "webview.db";
|
||||
@@ -202,6 +203,17 @@ public class WebViewDatabase {
|
||||
return;
|
||||
}
|
||||
|
||||
initDatabase(context);
|
||||
if (!JniUtil.useChromiumHttpStack()) {
|
||||
initCacheDatabase(context);
|
||||
}
|
||||
|
||||
// Thread done, notify.
|
||||
mInitialized = true;
|
||||
notify();
|
||||
}
|
||||
|
||||
private void initDatabase(Context context) {
|
||||
try {
|
||||
mDatabase = context.openOrCreateDatabase(DATABASE_FILE, 0, null);
|
||||
} catch (SQLiteException e) {
|
||||
@@ -234,6 +246,10 @@ public class WebViewDatabase {
|
||||
// improves performance as database's ReentrantLock is
|
||||
// expansive
|
||||
mDatabase.setLockingEnabled(false);
|
||||
}
|
||||
|
||||
private void initCacheDatabase(Context context) {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
|
||||
try {
|
||||
mCacheDatabase = context.openOrCreateDatabase(
|
||||
@@ -306,10 +322,6 @@ public class WebViewDatabase {
|
||||
.getColumnIndex(CACHE_CONTENTDISPOSITION_COL);
|
||||
mCacheCrossDomainColIndex = mCacheInserter
|
||||
.getColumnIndex(CACHE_CROSSDOMAIN_COL);
|
||||
|
||||
// Thread done, notify.
|
||||
mInitialized = true;
|
||||
notify();
|
||||
}
|
||||
|
||||
private static void upgradeDatabase() {
|
||||
@@ -668,6 +680,8 @@ public class WebViewDatabase {
|
||||
* @return CacheResult The CacheManager.CacheResult
|
||||
*/
|
||||
CacheResult getCache(String url) {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
|
||||
if (url == null || !checkInitialized()) {
|
||||
return null;
|
||||
}
|
||||
@@ -708,6 +722,8 @@ public class WebViewDatabase {
|
||||
* @param url The url
|
||||
*/
|
||||
void removeCache(String url) {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
|
||||
if (url == null || !checkInitialized()) {
|
||||
return;
|
||||
}
|
||||
@@ -722,6 +738,8 @@ public class WebViewDatabase {
|
||||
* @param c The CacheManager.CacheResult
|
||||
*/
|
||||
void addCache(String url, CacheResult c) {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
|
||||
if (url == null || !checkInitialized()) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -125,6 +125,7 @@ final class WebViewWorker extends Handler {
|
||||
break;
|
||||
}
|
||||
case MSG_CREATE_CACHE: {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
CacheCreateData data = (CacheCreateData) msg.obj;
|
||||
CacheManager.CacheResult cache = CacheManager.createCacheFile(
|
||||
data.mUrl, data.mStatusCode, data.mHeaders,
|
||||
@@ -137,6 +138,7 @@ final class WebViewWorker extends Handler {
|
||||
break;
|
||||
}
|
||||
case MSG_UPDATE_CACHE_ENCODING: {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
CacheEncoding data = (CacheEncoding) msg.obj;
|
||||
CacheManager.CacheResult cache = mCacheResultMap
|
||||
.get(data.mListener);
|
||||
@@ -146,6 +148,7 @@ final class WebViewWorker extends Handler {
|
||||
break;
|
||||
}
|
||||
case MSG_APPEND_CACHE: {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
CacheData data = (CacheData) msg.obj;
|
||||
CacheManager.CacheResult cache = mCacheResultMap
|
||||
.get(data.mListener);
|
||||
@@ -168,6 +171,7 @@ final class WebViewWorker extends Handler {
|
||||
break;
|
||||
}
|
||||
case MSG_SAVE_CACHE: {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
CacheSaveData data = (CacheSaveData) msg.obj;
|
||||
CacheManager.CacheResult cache = mCacheResultMap
|
||||
.get(data.mListener);
|
||||
@@ -178,6 +182,7 @@ final class WebViewWorker extends Handler {
|
||||
break;
|
||||
}
|
||||
case MSG_REMOVE_CACHE: {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
LoadListener listener = (LoadListener) msg.obj;
|
||||
CacheManager.CacheResult cache = mCacheResultMap.get(listener);
|
||||
if (cache != null) {
|
||||
@@ -187,14 +192,17 @@ final class WebViewWorker extends Handler {
|
||||
break;
|
||||
}
|
||||
case MSG_TRIM_CACHE: {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
CacheManager.trimCacheIfNeeded();
|
||||
break;
|
||||
}
|
||||
case MSG_CLEAR_CACHE: {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
CacheManager.clearCache();
|
||||
break;
|
||||
}
|
||||
case MSG_CACHE_TRANSACTION_TICKER: {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
if (!mCacheTickersBlocked) {
|
||||
CacheManager.endTransaction();
|
||||
CacheManager.startTransaction();
|
||||
@@ -204,6 +212,7 @@ final class WebViewWorker extends Handler {
|
||||
break;
|
||||
}
|
||||
case MSG_PAUSE_CACHE_TRANSACTION: {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
if (CacheManager.disableTransaction()) {
|
||||
mCacheTickersBlocked = true;
|
||||
removeMessages(MSG_CACHE_TRANSACTION_TICKER);
|
||||
@@ -211,6 +220,7 @@ final class WebViewWorker extends Handler {
|
||||
break;
|
||||
}
|
||||
case MSG_RESUME_CACHE_TRANSACTION: {
|
||||
assert !JniUtil.useChromiumHttpStack();
|
||||
if (CacheManager.enableTransaction()) {
|
||||
mCacheTickersBlocked = false;
|
||||
sendEmptyMessageDelayed(MSG_CACHE_TRANSACTION_TICKER,
|
||||
|
||||
Reference in New Issue
Block a user