michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef CacheFileContextEvictor__h__ michael@0: #define CacheFileContextEvictor__h__ michael@0: michael@0: #include "nsTArray.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsAutoPtr.h" michael@0: michael@0: class nsIFile; michael@0: class nsILoadContextInfo; michael@0: michael@0: namespace mozilla { michael@0: namespace net { michael@0: michael@0: class CacheIndexIterator; michael@0: michael@0: struct CacheFileContextEvictorEntry michael@0: { michael@0: nsCOMPtr mInfo; michael@0: PRTime mTimeStamp; // in milliseconds michael@0: nsRefPtr mIterator; michael@0: }; michael@0: michael@0: class CacheFileContextEvictor michael@0: { michael@0: public: michael@0: NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CacheFileContextEvictor) michael@0: michael@0: CacheFileContextEvictor(); michael@0: virtual ~CacheFileContextEvictor(); michael@0: michael@0: nsresult Init(nsIFile *aCacheDirectory); michael@0: michael@0: // Returns number of contexts that are being evicted. michael@0: uint32_t ContextsCount(); michael@0: // Start evicting given context. michael@0: nsresult AddContext(nsILoadContextInfo *aLoadContextInfo); michael@0: // CacheFileIOManager calls this method when CacheIndex's state changes. We michael@0: // check whether the index is up to date and start or stop evicting according michael@0: // to index's state. michael@0: nsresult CacheIndexStateChanged(); michael@0: // CacheFileIOManager calls this method to check whether an entry file should michael@0: // be considered as evicted. It returns true when there is a matching context michael@0: // info to the given key and the last modified time of the entry file is michael@0: // earlier than the time stamp of the time when the context was added to the michael@0: // evictor. michael@0: nsresult WasEvicted(const nsACString &aKey, nsIFile *aFile, bool *_retval); michael@0: michael@0: private: michael@0: // Writes information about eviction of the given context to the disk. This is michael@0: // done for every context added to the evictor to be able to recover eviction michael@0: // after a shutdown or crash. When the context file is found after startup, we michael@0: // restore mTimeStamp from the last modified time of the file. michael@0: nsresult PersistEvictionInfoToDisk(nsILoadContextInfo *aLoadContextInfo); michael@0: // Once we are done with eviction for the given context, the eviction info is michael@0: // removed from the disk. michael@0: nsresult RemoveEvictInfoFromDisk(nsILoadContextInfo *aLoadContextInfo); michael@0: // Tries to load all contexts from the disk. This method is called just once michael@0: // after startup. michael@0: nsresult LoadEvictInfoFromDisk(); michael@0: nsresult GetContextFile(nsILoadContextInfo *aLoadContextInfo, michael@0: nsIFile **_retval); michael@0: michael@0: void CreateIterators(); michael@0: void CloseIterators(); michael@0: void StartEvicting(); michael@0: nsresult EvictEntries(); michael@0: michael@0: // Whether eviction is in progress michael@0: bool mEvicting; michael@0: // Whether index is up to date. We wait with eviction until the index finishes michael@0: // update process when it is outdated. NOTE: We also stop eviction in progress michael@0: // when the index is found outdated, the eviction is restarted again once the michael@0: // update process finishes. michael@0: bool mIndexIsUpToDate; michael@0: // Whether we already tried to restore unfinished jobs from previous run after michael@0: // startup. michael@0: static bool sDiskAlreadySearched; michael@0: // Array of contexts being evicted. michael@0: nsTArray > mEntries; michael@0: nsCOMPtr mCacheDirectory; michael@0: nsCOMPtr mEntriesDir; michael@0: }; michael@0: michael@0: } // net michael@0: } // mozilla michael@0: michael@0: #endif