diff options
author | Andrei Popescu <andreip@google.com> | 2009-07-22 16:46:55 +0100 |
---|---|---|
committer | Andrei Popescu <andreip@google.com> | 2009-07-23 13:32:43 +0100 |
commit | 20634ce1fa1af93df5d12b5485b87ecd294281bb (patch) | |
tree | 414dcf2c06e0aedd7b9de1a8ccbf975458416c5a /src/com/android/browser | |
parent | ccc5630febc648fe4930dba5d4080440c4f31e18 (diff) | |
download | packages_apps_Browser-20634ce1fa1af93df5d12b5485b87ecd294281bb.zip packages_apps_Browser-20634ce1fa1af93df5d12b5485b87ecd294281bb.tar.gz packages_apps_Browser-20634ce1fa1af93df5d12b5485b87ecd294281bb.tar.bz2 |
Enforce app cache maximum size
Diffstat (limited to 'src/com/android/browser')
-rw-r--r-- | src/com/android/browser/BrowserSettings.java | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/com/android/browser/BrowserSettings.java b/src/com/android/browser/BrowserSettings.java index 2301289..d2371cd 100644 --- a/src/com/android/browser/BrowserSettings.java +++ b/src/com/android/browser/BrowserSettings.java @@ -25,6 +25,7 @@ import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.preference.PreferenceActivity; import android.preference.PreferenceScreen; +import android.os.StatFs; import android.webkit.CookieManager; import android.webkit.WebView; import android.webkit.WebViewDatabase; @@ -77,6 +78,7 @@ class BrowserSettings extends Observable { // The Browser always enables Application Caches. private boolean appCacheEnabled = true; private String appCachePath; // default value set in loadFromDb(). + private long appCacheMaxSize = Long.MAX_VALUE; private boolean domStorageEnabled = true; private String jsFlags = ""; @@ -209,6 +211,7 @@ class BrowserSettings extends Observable { // Turn on Application Caches. s.setAppCachePath(b.appCachePath); s.setAppCacheEnabled(b.appCacheEnabled); + s.setAppCacheMaxSize(b.appCacheMaxSize); // Enable/Disable the error console. b.mTabControl.getBrowserActivity().setShouldShowErrorConsole( @@ -234,6 +237,8 @@ class BrowserSettings extends Observable { pluginsPath = ctx.getDir("plugins", 0).getPath(); // Set the default value for the Application Caches path. appCachePath = ctx.getDir("appcache", 0).getPath(); + // Determine the maximum size of the application cache. + appCacheMaxSize = getAppCacheMaxSize(); // Set the default value for the Database path. databasePath = ctx.getDir("databases", 0).getPath(); @@ -538,6 +543,35 @@ class BrowserSettings extends Observable { return url; } + private long getAppCacheMaxSize() { + StatFs dataPartition = new StatFs(appCachePath); + long freeSpace = dataPartition.getAvailableBlocks() + * dataPartition.getBlockSize(); + long fileSystemSize = dataPartition.getBlockCount() + * dataPartition.getBlockSize(); + return calculateAppCacheMaxSize(fileSystemSize, freeSpace); + } + + /*package*/ static long calculateAppCacheMaxSize(long fileSystemSizeBytes, + long freeSpaceBytes) { + if (fileSystemSizeBytes <= 0 + || freeSpaceBytes <= 0 + || freeSpaceBytes > fileSystemSizeBytes) { + return 0; + } + + long fileSystemSizeRatio = + 4 << ((int) Math.floor(Math.log10(fileSystemSizeBytes / (1024 * 1024)))); + long maxSizeBytes = (long) Math.min(Math.floor(fileSystemSizeBytes / fileSystemSizeRatio), + Math.floor(freeSpaceBytes / 4)); + // Round maxSizeBytes up to a multiple of 512KB (except when freeSpaceBytes < 1MB). + long maxSizeStepBytes = 512 * 1024; + if (freeSpaceBytes < maxSizeStepBytes * 2) { + return 0; + } + return (maxSizeStepBytes * ((maxSizeBytes / maxSizeStepBytes) + 1)); + } + // Private constructor that does nothing. private BrowserSettings() { } |