CacheFileContextEvictor.h (4119B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #ifndef CacheFileContextEvictor__h__ 6 #define CacheFileContextEvictor__h__ 7 8 #include "mozilla/UniquePtr.h" 9 #include "nsCOMPtr.h" 10 #include "nsString.h" 11 #include "nsTArray.h" 12 13 class nsIFile; 14 class nsILoadContextInfo; 15 16 namespace mozilla { 17 namespace net { 18 19 class CacheIndexIterator; 20 21 struct CacheFileContextEvictorEntry { 22 nsCOMPtr<nsILoadContextInfo> mInfo; 23 bool mPinned = false; 24 // Only one of mOrigin or mBaseDomain can be set at a time 25 nsString mOrigin; // Optional - for origin-based eviction 26 nsString mBaseDomain; // Optional - for domain-based eviction 27 PRTime mTimeStamp = 0; // in milliseconds 28 RefPtr<CacheIndexIterator> mIterator; 29 }; 30 31 class CacheFileContextEvictor { 32 public: 33 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CacheFileContextEvictor) 34 35 CacheFileContextEvictor(); 36 37 private: 38 virtual ~CacheFileContextEvictor(); 39 40 public: 41 nsresult Init(nsIFile* aCacheDirectory); 42 void Shutdown(); 43 44 // Returns number of contexts that are being evicted. 45 uint32_t ContextsCount(); 46 // Start evicting given context and an origin, if not empty. 47 nsresult AddContext(nsILoadContextInfo* aLoadContextInfo, bool aPinned, 48 const nsAString& aOrigin, const nsAString& aBaseDomain); 49 // CacheFileIOManager calls this method when CacheIndex's state changes. We 50 // check whether the index is up to date and start or stop evicting according 51 // to index's state. 52 void CacheIndexStateChanged(); 53 // CacheFileIOManager calls this method to check whether an entry file should 54 // be considered as evicted. It returns true when there is a matching context 55 // info to the given key and the last modified time of the entry file is 56 // earlier than the time stamp of the time when the context was added to the 57 // evictor. 58 void WasEvicted(const nsACString& aKey, nsIFile* aFile, 59 bool* aEvictedAsPinned, bool* aEvictedAsNonPinned); 60 61 private: 62 // Writes information about eviction of the given context to the disk. This is 63 // done for every context added to the evictor to be able to recover eviction 64 // after a shutdown or crash. When the context file is found after startup, we 65 // restore mTimeStamp from the last modified time of the file. 66 nsresult PersistEvictionInfoToDisk(nsILoadContextInfo* aLoadContextInfo, 67 bool aPinned, const nsAString& aOrigin, 68 const nsAString& aBaseDomain); 69 // Once we are done with eviction for the given context, the eviction info is 70 // removed from the disk. 71 nsresult RemoveEvictInfoFromDisk(nsILoadContextInfo* aLoadContextInfo, 72 bool aPinned, const nsAString& aOrigin, 73 const nsAString& aBaseDomain); 74 // Tries to load all contexts from the disk. This method is called just once 75 // after startup. 76 nsresult LoadEvictInfoFromDisk(); 77 nsresult GetContextFile(nsILoadContextInfo* aLoadContextInfo, bool aPinned, 78 const nsAString& aOrigin, 79 const nsAString& aBaseDomain, nsIFile** _retval); 80 81 void CreateIterators(); 82 void CloseIterators(); 83 void StartEvicting(); 84 void EvictEntries(); 85 86 // Whether eviction is in progress 87 bool mEvicting{false}; 88 // Whether index is up to date. We wait with eviction until the index finishes 89 // update process when it is outdated. NOTE: We also stop eviction in progress 90 // when the index is found outdated, the eviction is restarted again once the 91 // update process finishes. 92 bool mIndexIsUpToDate{false}; 93 // Whether we already tried to restore unfinished jobs from previous run after 94 // startup. 95 static bool sDiskAlreadySearched; 96 // Array of contexts being evicted. 97 nsTArray<UniquePtr<CacheFileContextEvictorEntry>> mEntries; 98 nsCOMPtr<nsIFile> mCacheDirectory; 99 nsCOMPtr<nsIFile> mEntriesDir; 100 }; 101 102 } // namespace net 103 } // namespace mozilla 104 105 #endif