summaryrefslogtreecommitdiff
path: root/core/java/android
diff options
context:
space:
mode:
authorGrace Kloba <klobag@google.com>2009-12-27 17:39:43 -0800
committerGrace Kloba <klobag@google.com>2009-12-27 18:25:21 -0800
commit998c05b3b5db124a31da1ac38af0e97bca114122 (patch)
treeae410a337032cafefc8fb378056fc100282e1d0e /core/java/android
parent4bc95d19c9ccf9cfe4c51b1e697db8cc1d86a579 (diff)
Cleanup the cache file when we decide not saving it.
This logic was lost when we switched back using FLASH instead of RAM for the cache. Add back the CACHE_MAX_SIZE to limit the maximum cacheable file. Fix http://b/issue?id=2346132
Diffstat (limited to 'core/java/android')
-rw-r--r--core/java/android/webkit/CacheManager.java16
-rw-r--r--core/java/android/webkit/LoadListener.java30
2 files changed, 39 insertions, 7 deletions
diff --git a/core/java/android/webkit/CacheManager.java b/core/java/android/webkit/CacheManager.java
index c4e26bc34461..22dca3a85210 100644
--- a/core/java/android/webkit/CacheManager.java
+++ b/core/java/android/webkit/CacheManager.java
@@ -56,6 +56,9 @@ public final class CacheManager {
private static long CACHE_THRESHOLD = 6 * 1024 * 1024;
private static long CACHE_TRIM_AMOUNT = 2 * 1024 * 1024;
+ // Limit the maximum cache file size to half of the normal capacity
+ static long CACHE_MAX_SIZE = (CACHE_THRESHOLD - CACHE_TRIM_AMOUNT) / 2;
+
private static boolean mDisabled;
// Reference count the enable/disable transaction
@@ -448,7 +451,6 @@ public final class CacheManager {
return;
}
- cacheRet.contentLength = cacheRet.outFile.length();
boolean redirect = checkCacheRedirect(cacheRet.httpStatusCode);
if (redirect) {
// location is in database, no need to keep the file
@@ -470,6 +472,15 @@ public final class CacheManager {
}
}
+ static boolean cleanupCacheFile(CacheResult cacheRet) {
+ try {
+ cacheRet.outStream.close();
+ } catch (IOException e) {
+ return false;
+ }
+ return cacheRet.outFile.delete();
+ }
+
/**
* remove all cache files
*
@@ -644,6 +655,9 @@ public final class CacheManager {
private static CacheResult parseHeaders(int statusCode, Headers headers,
String mimeType) {
+ // if the contentLength is already larger than CACHE_MAX_SIZE, skip it
+ if (headers.getContentLength() > CACHE_MAX_SIZE) return null;
+
// TODO: if authenticated or secure, return null
CacheResult ret = new CacheResult();
ret.httpStatusCode = statusCode;
diff --git a/core/java/android/webkit/LoadListener.java b/core/java/android/webkit/LoadListener.java
index c2b9f20a6ce2..cdc6608ea1f6 100644
--- a/core/java/android/webkit/LoadListener.java
+++ b/core/java/android/webkit/LoadListener.java
@@ -936,8 +936,11 @@ class LoadListener extends Handler implements EventHandler {
void downloadFile() {
// Setting the Cache Result to null ensures that this
// content is not added to the cache
- mCacheResult = null;
-
+ if (mCacheResult != null) {
+ CacheManager.cleanupCacheFile(mCacheResult);
+ mCacheResult = null;
+ }
+
// Inform the client that they should download a file
mBrowserFrame.getCallbackProxy().onDownloadStart(url(),
mBrowserFrame.getUserAgentString(),
@@ -1096,10 +1099,18 @@ class LoadListener extends Handler implements EventHandler {
if (c.mLength != 0) {
if (mCacheResult != null) {
- try {
- mCacheResult.outStream.write(c.mArray, 0, c.mLength);
- } catch (IOException e) {
+ mCacheResult.contentLength += c.mLength;
+ if (mCacheResult.contentLength > CacheManager.CACHE_MAX_SIZE) {
+ CacheManager.cleanupCacheFile(mCacheResult);
mCacheResult = null;
+ } else {
+ try {
+ mCacheResult.outStream
+ .write(c.mArray, 0, c.mLength);
+ } catch (IOException e) {
+ CacheManager.cleanupCacheFile(mCacheResult);
+ mCacheResult = null;
+ }
}
}
nativeAddData(c.mArray, c.mLength);
@@ -1117,6 +1128,8 @@ class LoadListener extends Handler implements EventHandler {
if (mCacheResult != null) {
if (getErrorID() == OK) {
CacheManager.saveCacheFile(mUrl, mPostIdentifier, mCacheResult);
+ } else {
+ CacheManager.cleanupCacheFile(mCacheResult);
}
// we need to reset mCacheResult to be null
@@ -1181,7 +1194,10 @@ class LoadListener extends Handler implements EventHandler {
mRequestHandle = null;
}
- mCacheResult = null;
+ if (mCacheResult != null) {
+ CacheManager.cleanupCacheFile(mCacheResult);
+ mCacheResult = null;
+ }
mCancelled = true;
clearNativeLoader();
@@ -1246,6 +1262,8 @@ class LoadListener extends Handler implements EventHandler {
if (getErrorID() == OK) {
CacheManager.saveCacheFile(mUrl, mPostIdentifier,
mCacheResult);
+ } else {
+ CacheManager.cleanupCacheFile(mCacheResult);
}
mCacheResult = null;
}