1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/cache2/CacheStorageService.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,318 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#ifndef CacheStorageService__h__ 1.9 +#define CacheStorageService__h__ 1.10 + 1.11 +#include "nsICacheStorageService.h" 1.12 +#include "nsIMemoryReporter.h" 1.13 + 1.14 +#include "nsITimer.h" 1.15 +#include "nsClassHashtable.h" 1.16 +#include "nsString.h" 1.17 +#include "nsThreadUtils.h" 1.18 +#include "nsProxyRelease.h" 1.19 +#include "mozilla/Mutex.h" 1.20 +#include "mozilla/Atomics.h" 1.21 +#include "nsTArray.h" 1.22 + 1.23 +class nsIURI; 1.24 +class nsICacheEntryOpenCallback; 1.25 +class nsICacheEntryDoomCallback; 1.26 +class nsICacheStorageVisitor; 1.27 +class nsIRunnable; 1.28 +class nsIThread; 1.29 +class nsIEventTarget; 1.30 + 1.31 +namespace mozilla { 1.32 +namespace net { 1.33 + 1.34 +class CacheStorageService; 1.35 +class CacheStorage; 1.36 +class CacheEntry; 1.37 +class CacheEntryHandle; 1.38 +class CacheEntryTable; 1.39 + 1.40 +class CacheMemoryConsumer 1.41 +{ 1.42 +private: 1.43 + friend class CacheStorageService; 1.44 + uint32_t mReportedMemoryConsumption : 30; 1.45 + uint32_t mFlags : 2; 1.46 + 1.47 +private: 1.48 + CacheMemoryConsumer() MOZ_DELETE; 1.49 + 1.50 +protected: 1.51 + enum { 1.52 + // No special treatment, reports always to the disk-entries pool. 1.53 + NORMAL = 0, 1.54 + // This consumer is belonging to a memory-only cache entry, used to decide 1.55 + // which of the two disk and memory pools count this consumption at. 1.56 + MEMORY_ONLY = 1 << 0, 1.57 + // Prevent reports of this consumer at all, used for disk data chunks since 1.58 + // we throw them away as soon as the entry is not used by any consumer and 1.59 + // don't want to make them wipe the whole pool out during their short life. 1.60 + DONT_REPORT = 1 << 1 1.61 + }; 1.62 + 1.63 + CacheMemoryConsumer(uint32_t aFlags); 1.64 + ~CacheMemoryConsumer() { DoMemoryReport(0); } 1.65 + void DoMemoryReport(uint32_t aCurrentSize); 1.66 +}; 1.67 + 1.68 +class CacheStorageService : public nsICacheStorageService 1.69 + , public nsIMemoryReporter 1.70 + , public nsITimerCallback 1.71 +{ 1.72 +public: 1.73 + NS_DECL_THREADSAFE_ISUPPORTS 1.74 + NS_DECL_NSICACHESTORAGESERVICE 1.75 + NS_DECL_NSIMEMORYREPORTER 1.76 + NS_DECL_NSITIMERCALLBACK 1.77 + 1.78 + CacheStorageService(); 1.79 + 1.80 + void Shutdown(); 1.81 + void DropPrivateBrowsingEntries(); 1.82 + 1.83 + // Wipes out the new or the old cache directory completely. 1.84 + static void WipeCacheDirectory(uint32_t aVersion); 1.85 + 1.86 + static CacheStorageService* Self() { return sSelf; } 1.87 + static nsISupports* SelfISupports() { return static_cast<nsICacheStorageService*>(Self()); } 1.88 + nsresult Dispatch(nsIRunnable* aEvent); 1.89 + static bool IsRunning() { return sSelf && !sSelf->mShutdown; } 1.90 + static bool IsOnManagementThread(); 1.91 + already_AddRefed<nsIEventTarget> Thread() const; 1.92 + mozilla::Mutex& Lock() { return mLock; } 1.93 + 1.94 + // Memory reporting 1.95 + size_t SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const; 1.96 + size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const; 1.97 + MOZ_DEFINE_MALLOC_SIZE_OF(MallocSizeOf) 1.98 + 1.99 +private: 1.100 + virtual ~CacheStorageService(); 1.101 + void ShutdownBackground(); 1.102 + 1.103 +private: 1.104 + // The following methods may only be called on the management 1.105 + // thread. 1.106 + friend class CacheEntry; 1.107 + 1.108 + /** 1.109 + * Registers the entry in management ordered arrays, a mechanism 1.110 + * helping with weighted purge of entries. 1.111 + * Management arrays keep hard reference to the entry. Entry is 1.112 + * responsible to remove it self or the service is responsible to 1.113 + * remove the entry when it's no longer needed. 1.114 + */ 1.115 + void RegisterEntry(CacheEntry* aEntry); 1.116 + 1.117 + /** 1.118 + * Deregisters the entry from management arrays. References are 1.119 + * then released. 1.120 + */ 1.121 + void UnregisterEntry(CacheEntry* aEntry); 1.122 + 1.123 + /** 1.124 + * Removes the entry from the related entry hash table, if still present. 1.125 + */ 1.126 + bool RemoveEntry(CacheEntry* aEntry, bool aOnlyUnreferenced = false); 1.127 + 1.128 + /** 1.129 + * Tells the storage service whether this entry is only to be stored in 1.130 + * memory. 1.131 + */ 1.132 + void RecordMemoryOnlyEntry(CacheEntry* aEntry, 1.133 + bool aOnlyInMemory, 1.134 + bool aOverwrite); 1.135 + 1.136 +private: 1.137 + // Following methods are thread safe to call. 1.138 + friend class CacheStorage; 1.139 + 1.140 + /** 1.141 + * Get, or create when not existing and demanded, an entry for the storage 1.142 + * and uri+id extension. 1.143 + */ 1.144 + nsresult AddStorageEntry(CacheStorage const* aStorage, 1.145 + nsIURI* aURI, 1.146 + const nsACString & aIdExtension, 1.147 + bool aCreateIfNotExist, 1.148 + bool aReplace, 1.149 + CacheEntryHandle** aResult); 1.150 + 1.151 + /** 1.152 + * Removes the entry from the related entry hash table, if still present 1.153 + * and returns it. 1.154 + */ 1.155 + nsresult DoomStorageEntry(CacheStorage const* aStorage, 1.156 + nsIURI* aURI, 1.157 + const nsACString & aIdExtension, 1.158 + nsICacheEntryDoomCallback* aCallback); 1.159 + 1.160 + /** 1.161 + * Removes and returns entry table for the storage. 1.162 + */ 1.163 + nsresult DoomStorageEntries(CacheStorage const* aStorage, 1.164 + nsICacheEntryDoomCallback* aCallback); 1.165 + 1.166 + /** 1.167 + * Walk all entiries beloging to the storage. 1.168 + */ 1.169 + nsresult WalkStorageEntries(CacheStorage const* aStorage, 1.170 + bool aVisitEntries, 1.171 + nsICacheStorageVisitor* aVisitor); 1.172 + 1.173 +private: 1.174 + friend class CacheFileIOManager; 1.175 + 1.176 + /** 1.177 + * CacheFileIOManager uses this method to notify CacheStorageService that 1.178 + * an active entry was removed. This method is called even if the entry 1.179 + * removal was originated by CacheStorageService. 1.180 + */ 1.181 + void CacheFileDoomed(nsILoadContextInfo* aLoadContextInfo, 1.182 + const nsACString & aIdExtension, 1.183 + const nsACString & aURISpec); 1.184 + 1.185 +private: 1.186 + friend class CacheMemoryConsumer; 1.187 + 1.188 + /** 1.189 + * When memory consumption of this entry radically changes, this method 1.190 + * is called to reflect the size of allocated memory. This call may purge 1.191 + * unspecified number of entries from memory (but not from disk). 1.192 + */ 1.193 + void OnMemoryConsumptionChange(CacheMemoryConsumer* aConsumer, 1.194 + uint32_t aCurrentMemoryConsumption); 1.195 + 1.196 + /** 1.197 + * If not already pending, it schedules mPurgeTimer that fires after 1 second 1.198 + * and dispatches PurgeOverMemoryLimit(). 1.199 + */ 1.200 + void SchedulePurgeOverMemoryLimit(); 1.201 + 1.202 + /** 1.203 + * Called on the management thread, removes all expired and then least used 1.204 + * entries from the memory, first from the disk pool and then from the memory 1.205 + * pool. 1.206 + */ 1.207 + void PurgeOverMemoryLimit(); 1.208 + 1.209 +private: 1.210 + nsresult DoomStorageEntries(nsCSubstring const& aContextKey, 1.211 + nsILoadContextInfo* aContext, 1.212 + bool aDiskStorage, 1.213 + nsICacheEntryDoomCallback* aCallback); 1.214 + nsresult AddStorageEntry(nsCSubstring const& aContextKey, 1.215 + nsIURI* aURI, 1.216 + const nsACString & aIdExtension, 1.217 + bool aWriteToDisk, 1.218 + bool aCreateIfNotExist, 1.219 + bool aReplace, 1.220 + CacheEntryHandle** aResult); 1.221 + 1.222 + static CacheStorageService* sSelf; 1.223 + 1.224 + mozilla::Mutex mLock; 1.225 + 1.226 + bool mShutdown; 1.227 + 1.228 + // Accessible only on the service thread 1.229 + class MemoryPool 1.230 + { 1.231 + public: 1.232 + enum EType 1.233 + { 1.234 + DISK, 1.235 + MEMORY, 1.236 + } mType; 1.237 + 1.238 + MemoryPool(EType aType); 1.239 + ~MemoryPool(); 1.240 + 1.241 + nsTArray<nsRefPtr<CacheEntry> > mFrecencyArray; 1.242 + nsTArray<nsRefPtr<CacheEntry> > mExpirationArray; 1.243 + mozilla::Atomic<uint32_t> mMemorySize; 1.244 + 1.245 + bool OnMemoryConsumptionChange(uint32_t aSavedMemorySize, 1.246 + uint32_t aCurrentMemoryConsumption); 1.247 + /** 1.248 + * Purges entries from memory based on the frecency ordered array. 1.249 + */ 1.250 + void PurgeOverMemoryLimit(); 1.251 + void PurgeExpired(); 1.252 + void PurgeByFrecency(bool &aFrecencyNeedsSort, uint32_t aWhat); 1.253 + void PurgeAll(uint32_t aWhat); 1.254 + 1.255 + private: 1.256 + uint32_t const Limit() const; 1.257 + MemoryPool() MOZ_DELETE; 1.258 + }; 1.259 + 1.260 + MemoryPool mDiskPool; 1.261 + MemoryPool mMemoryPool; 1.262 + MemoryPool& Pool(bool aUsingDisk) 1.263 + { 1.264 + return aUsingDisk ? mDiskPool : mMemoryPool; 1.265 + } 1.266 + MemoryPool const& Pool(bool aUsingDisk) const 1.267 + { 1.268 + return aUsingDisk ? mDiskPool : mMemoryPool; 1.269 + } 1.270 + 1.271 + nsCOMPtr<nsITimer> mPurgeTimer; 1.272 + 1.273 + class PurgeFromMemoryRunnable : public nsRunnable 1.274 + { 1.275 + public: 1.276 + PurgeFromMemoryRunnable(CacheStorageService* aService, uint32_t aWhat) 1.277 + : mService(aService), mWhat(aWhat) { } 1.278 + 1.279 + private: 1.280 + virtual ~PurgeFromMemoryRunnable() { } 1.281 + 1.282 + NS_IMETHOD Run() 1.283 + { 1.284 + // TODO not all flags apply to both pools 1.285 + mService->Pool(true).PurgeAll(mWhat); 1.286 + mService->Pool(false).PurgeAll(mWhat); 1.287 + return NS_OK; 1.288 + } 1.289 + 1.290 + nsRefPtr<CacheStorageService> mService; 1.291 + uint32_t mWhat; 1.292 + }; 1.293 +}; 1.294 + 1.295 +template<class T> 1.296 +void ProxyRelease(nsCOMPtr<T> &object, nsIThread* thread) 1.297 +{ 1.298 + T* release; 1.299 + object.forget(&release); 1.300 + 1.301 + NS_ProxyRelease(thread, release); 1.302 +} 1.303 + 1.304 +template<class T> 1.305 +void ProxyReleaseMainThread(nsCOMPtr<T> &object) 1.306 +{ 1.307 + nsCOMPtr<nsIThread> mainThread = do_GetMainThread(); 1.308 + ProxyRelease(object, mainThread); 1.309 +} 1.310 + 1.311 +} // net 1.312 +} // mozilla 1.313 + 1.314 +#define NS_CACHE_STORAGE_SERVICE_CID \ 1.315 + { 0xea70b098, 0x5014, 0x4e21, \ 1.316 + { 0xae, 0xe1, 0x75, 0xe6, 0xb2, 0xc4, 0xb8, 0xe0 } } \ 1.317 + 1.318 +#define NS_CACHE_STORAGE_SERVICE_CONTRACTID \ 1.319 + "@mozilla.org/netwerk/cache-storage-service;1" 1.320 + 1.321 +#endif