diff --git a/netwerk/cache/src/nsCacheDevice.h b/netwerk/cache/src/nsCacheDevice.h index 2fc92a6f173..350c0060ece 100644 --- a/netwerk/cache/src/nsCacheDevice.h +++ b/netwerk/cache/src/nsCacheDevice.h @@ -31,7 +31,7 @@ class nsCacheEntry; - +class nsICacheVisitor; class nsCacheDevice { public: @@ -53,6 +53,8 @@ public: virtual nsresult OnDataSizeChange( nsCacheEntry * entry, PRInt32 deltaSize ) = 0; // XXX need to define methods for enumerating entries + + virtual nsresult Visit(nsICacheVisitor * visitor) = 0; }; #endif // _nsCacheDevice_h_ diff --git a/netwerk/cache/src/nsDiskCacheDevice.cpp b/netwerk/cache/src/nsDiskCacheDevice.cpp index 3518f7b92bd..c8e90e4dbe4 100644 --- a/netwerk/cache/src/nsDiskCacheDevice.cpp +++ b/netwerk/cache/src/nsDiskCacheDevice.cpp @@ -45,6 +45,8 @@ static const char CACHE_DIR_PREF[] = { "browser.newcache.directory" }; static const char CACHE_DIR_PREF[] = { "browser.cache.directory" }; #endif +static const char CACHE_DISK_CAPACITY[] = { "browser.cache.disk_cache_size" }; + static int PR_CALLBACK cacheDirectoryChanged(const char *pref, void *closure) { nsresult rv; @@ -63,13 +65,50 @@ static int PR_CALLBACK cacheDirectoryChanged(const char *pref, void *closure) return NS_OK; } +static int PR_CALLBACK cacheCapacityChanged(const char *pref, void *closure) +{ + nsresult rv; + NS_WITH_SERVICE(nsIPref, prefs, NS_PREF_CONTRACTID, &rv); + if (NS_FAILED(rv)) + return rv; + + PRInt32 cacheCapacity; + rv = prefs->GetIntPref(CACHE_DISK_CAPACITY, &cacheCapacity); + if (NS_FAILED(rv)) + return rv; + + nsDiskCacheDevice* device = NS_STATIC_CAST(nsDiskCacheDevice*, closure); + device->setCacheCapacity(cacheCapacity); + + return NS_OK; +} + static nsresult installPrefListeners(nsDiskCacheDevice* device) { nsresult rv; NS_WITH_SERVICE(nsIPref, prefs, NS_PREF_CONTRACTID, &rv); if (NS_FAILED(rv)) return rv; - rv = prefs->RegisterCallback(CACHE_DIR_PREF, cacheDirectoryChanged, device); + + + rv = prefs->RegisterCallback(CACHE_DISK_CAPACITY, cacheCapacityChanged, device); + if (NS_FAILED(rv)) + return rv; + + PRInt32 cacheCapacity; + rv = prefs->GetIntPref(CACHE_DISK_CAPACITY, &cacheCapacity); + if (NS_FAILED(rv)) { +#if DEBUG + const kTenMegabytes = 10 * 1024 * 1024; + rv = prefs->SetIntPref(CACHE_DISK_CAPACITY, kTenMegabytes); +#else + return rv; +#endif + } else { + device->setCacheCapacity(cacheCapacity); + } + + rv = prefs->RegisterCallback(CACHE_DIR_PREF, cacheDirectoryChanged, device); if (NS_FAILED(rv)) return rv; @@ -91,6 +130,12 @@ static nsresult installPrefListeners(nsDiskCacheDevice* device) if (NS_FAILED(rv)) return rv; + // make sure the Cache directory exists. + PRBool exists; + rv = cacheDirectory->Exists(&exists); + if (NS_SUCCEEDED(rv) && !exists) + cacheDirectory->Create(nsIFile::DIRECTORY_TYPE, 0777); + rv = prefs->SetFileXPref(CACHE_DIR_PREF, cacheDirectory); if (NS_FAILED(rv)) return rv; @@ -185,7 +230,7 @@ ensureDiskCacheEntry(nsCacheEntry * entry) // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX nsDiskCacheDevice::nsDiskCacheDevice() - : mScannedEntries(PR_FALSE), mTotalCachedDataSize(LL_ZERO) + : mScannedEntries(PR_FALSE), mCacheCapacity(0), mCacheSize(0) { } @@ -347,21 +392,25 @@ nsDiskCacheDevice::GetTransportForEntry(nsCacheEntry * entry, nsresult nsDiskCacheDevice::OnDataSizeChange(nsCacheEntry * entry, PRInt32 deltaSize) { - PRInt64 deltaSize64; - LL_I2L(deltaSize64, deltaSize); - LL_ADD(mTotalCachedDataSize, mTotalCachedDataSize, deltaSize64); + mCacheSize += deltaSize; + return NS_OK; +} + +nsresult +nsDiskCacheDevice::Visit(nsICacheVisitor * visitor) +{ return NS_OK; } void nsDiskCacheDevice::setCacheDirectory(nsILocalFile* cacheDirectory) { mCacheDirectory = cacheDirectory; +} - // make sure the Cache directory exists. - PRBool exists; - nsresult rv = cacheDirectory->Exists(&exists); - if (NS_SUCCEEDED(rv) && !exists) - cacheDirectory->Create(nsIFile::DIRECTORY_TYPE, 0777); +void nsDiskCacheDevice::setCacheCapacity(PRUint32 capacity) +{ + // XXX start evicting entries if the new size is smaller! + mCacheCapacity = capacity; } nsresult nsDiskCacheDevice::getFileForKey(const char* key, PRBool meta, nsIFile ** result) diff --git a/netwerk/cache/src/nsDiskCacheDevice.h b/netwerk/cache/src/nsDiskCacheDevice.h index 6a1a9b71d32..ce5bc2994de 100644 --- a/netwerk/cache/src/nsDiskCacheDevice.h +++ b/netwerk/cache/src/nsDiskCacheDevice.h @@ -49,9 +49,12 @@ public: nsITransport ** result); virtual nsresult OnDataSizeChange(nsCacheEntry * entry, PRInt32 deltaSize); + + virtual nsresult Visit(nsICacheVisitor * visitor); /* private: */ void setCacheDirectory(nsILocalFile* directory); + void setCacheCapacity(PRUint32 capacity); private: nsresult getFileForKey(const char* key, PRBool meta, nsIFile**); @@ -63,10 +66,11 @@ private: nsresult deleteDiskCacheEntry(nsCacheEntry* entry); private: - nsCOMPtr mCacheDirectory; - nsCacheEntryHashTable mBoundEntries; - PRBool mScannedEntries; - PRUint64 mTotalCachedDataSize; + nsCOMPtr mCacheDirectory; + nsCacheEntryHashTable mBoundEntries; + PRBool mScannedEntries; + PRUint32 mCacheCapacity; + PRUint32 mCacheSize; }; #endif // _nsDiskCacheDevice_h_ diff --git a/netwerk/cache/src/nsMemoryCacheDevice.cpp b/netwerk/cache/src/nsMemoryCacheDevice.cpp index 11ba9f4f2f1..d8b31d17bd2 100644 --- a/netwerk/cache/src/nsMemoryCacheDevice.cpp +++ b/netwerk/cache/src/nsMemoryCacheDevice.cpp @@ -214,6 +214,13 @@ nsMemoryCacheDevice::OnDataSizeChange( nsCacheEntry * entry, PRInt32 deltaSize) } +nsresult +nsMemoryCacheDevice::Visit(nsICacheVisitor * visitor) +{ + return NS_OK; +} + + nsresult nsMemoryCacheDevice::AdjustMemoryLimits(PRUint32 softLimit, PRUint32 hardLimit) { diff --git a/netwerk/cache/src/nsMemoryCacheDevice.h b/netwerk/cache/src/nsMemoryCacheDevice.h index 70842256231..9f40b02ac58 100644 --- a/netwerk/cache/src/nsMemoryCacheDevice.h +++ b/netwerk/cache/src/nsMemoryCacheDevice.h @@ -50,6 +50,8 @@ public: virtual nsresult OnDataSizeChange( nsCacheEntry * entry, PRInt32 deltaSize ); + virtual nsresult Visit( nsICacheVisitor * visitor ); + static int PR_CALLBACK MemoryCacheSizeChanged(const char * pref, void * closure); private: