1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/cache2/CacheFileContextEvictor.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,91 @@ 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 CacheFileContextEvictor__h__ 1.9 +#define CacheFileContextEvictor__h__ 1.10 + 1.11 +#include "nsTArray.h" 1.12 +#include "nsCOMPtr.h" 1.13 +#include "nsAutoPtr.h" 1.14 + 1.15 +class nsIFile; 1.16 +class nsILoadContextInfo; 1.17 + 1.18 +namespace mozilla { 1.19 +namespace net { 1.20 + 1.21 +class CacheIndexIterator; 1.22 + 1.23 +struct CacheFileContextEvictorEntry 1.24 +{ 1.25 + nsCOMPtr<nsILoadContextInfo> mInfo; 1.26 + PRTime mTimeStamp; // in milliseconds 1.27 + nsRefPtr<CacheIndexIterator> mIterator; 1.28 +}; 1.29 + 1.30 +class CacheFileContextEvictor 1.31 +{ 1.32 +public: 1.33 + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CacheFileContextEvictor) 1.34 + 1.35 + CacheFileContextEvictor(); 1.36 + virtual ~CacheFileContextEvictor(); 1.37 + 1.38 + nsresult Init(nsIFile *aCacheDirectory); 1.39 + 1.40 + // Returns number of contexts that are being evicted. 1.41 + uint32_t ContextsCount(); 1.42 + // Start evicting given context. 1.43 + nsresult AddContext(nsILoadContextInfo *aLoadContextInfo); 1.44 + // CacheFileIOManager calls this method when CacheIndex's state changes. We 1.45 + // check whether the index is up to date and start or stop evicting according 1.46 + // to index's state. 1.47 + nsresult CacheIndexStateChanged(); 1.48 + // CacheFileIOManager calls this method to check whether an entry file should 1.49 + // be considered as evicted. It returns true when there is a matching context 1.50 + // info to the given key and the last modified time of the entry file is 1.51 + // earlier than the time stamp of the time when the context was added to the 1.52 + // evictor. 1.53 + nsresult WasEvicted(const nsACString &aKey, nsIFile *aFile, bool *_retval); 1.54 + 1.55 +private: 1.56 + // Writes information about eviction of the given context to the disk. This is 1.57 + // done for every context added to the evictor to be able to recover eviction 1.58 + // after a shutdown or crash. When the context file is found after startup, we 1.59 + // restore mTimeStamp from the last modified time of the file. 1.60 + nsresult PersistEvictionInfoToDisk(nsILoadContextInfo *aLoadContextInfo); 1.61 + // Once we are done with eviction for the given context, the eviction info is 1.62 + // removed from the disk. 1.63 + nsresult RemoveEvictInfoFromDisk(nsILoadContextInfo *aLoadContextInfo); 1.64 + // Tries to load all contexts from the disk. This method is called just once 1.65 + // after startup. 1.66 + nsresult LoadEvictInfoFromDisk(); 1.67 + nsresult GetContextFile(nsILoadContextInfo *aLoadContextInfo, 1.68 + nsIFile **_retval); 1.69 + 1.70 + void CreateIterators(); 1.71 + void CloseIterators(); 1.72 + void StartEvicting(); 1.73 + nsresult EvictEntries(); 1.74 + 1.75 + // Whether eviction is in progress 1.76 + bool mEvicting; 1.77 + // Whether index is up to date. We wait with eviction until the index finishes 1.78 + // update process when it is outdated. NOTE: We also stop eviction in progress 1.79 + // when the index is found outdated, the eviction is restarted again once the 1.80 + // update process finishes. 1.81 + bool mIndexIsUpToDate; 1.82 + // Whether we already tried to restore unfinished jobs from previous run after 1.83 + // startup. 1.84 + static bool sDiskAlreadySearched; 1.85 + // Array of contexts being evicted. 1.86 + nsTArray<nsAutoPtr<CacheFileContextEvictorEntry> > mEntries; 1.87 + nsCOMPtr<nsIFile> mCacheDirectory; 1.88 + nsCOMPtr<nsIFile> mEntriesDir; 1.89 +}; 1.90 + 1.91 +} // net 1.92 +} // mozilla 1.93 + 1.94 +#endif