Bug 1866777 - Disable test_race_cache_with_network.js on windows opt for frequent...
[gecko.git] / netwerk / cache2 / CacheFileContextEvictor.h
blob518d3b7d5ffc0419a7cfc0e40fd1547de28c4836
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/. */
5 #ifndef CacheFileContextEvictor__h__
6 #define CacheFileContextEvictor__h__
8 #include "mozilla/UniquePtr.h"
9 #include "nsCOMPtr.h"
10 #include "nsString.h"
11 #include "nsTArray.h"
13 class nsIFile;
14 class nsILoadContextInfo;
16 namespace mozilla {
17 namespace net {
19 class CacheIndexIterator;
21 struct CacheFileContextEvictorEntry {
22 nsCOMPtr<nsILoadContextInfo> mInfo;
23 bool mPinned = false;
24 nsString mOrigin; // it can be empty
25 PRTime mTimeStamp = 0; // in milliseconds
26 RefPtr<CacheIndexIterator> mIterator;
29 class CacheFileContextEvictor {
30 public:
31 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CacheFileContextEvictor)
33 CacheFileContextEvictor();
35 private:
36 virtual ~CacheFileContextEvictor();
38 public:
39 nsresult Init(nsIFile* aCacheDirectory);
40 void Shutdown();
42 // Returns number of contexts that are being evicted.
43 uint32_t ContextsCount();
44 // Start evicting given context and an origin, if not empty.
45 nsresult AddContext(nsILoadContextInfo* aLoadContextInfo, bool aPinned,
46 const nsAString& aOrigin);
47 // CacheFileIOManager calls this method when CacheIndex's state changes. We
48 // check whether the index is up to date and start or stop evicting according
49 // to index's state.
50 void CacheIndexStateChanged();
51 // CacheFileIOManager calls this method to check whether an entry file should
52 // be considered as evicted. It returns true when there is a matching context
53 // info to the given key and the last modified time of the entry file is
54 // earlier than the time stamp of the time when the context was added to the
55 // evictor.
56 void WasEvicted(const nsACString& aKey, nsIFile* aFile,
57 bool* aEvictedAsPinned, bool* aEvictedAsNonPinned);
59 private:
60 // Writes information about eviction of the given context to the disk. This is
61 // done for every context added to the evictor to be able to recover eviction
62 // after a shutdown or crash. When the context file is found after startup, we
63 // restore mTimeStamp from the last modified time of the file.
64 nsresult PersistEvictionInfoToDisk(nsILoadContextInfo* aLoadContextInfo,
65 bool aPinned, const nsAString& aOrigin);
66 // Once we are done with eviction for the given context, the eviction info is
67 // removed from the disk.
68 nsresult RemoveEvictInfoFromDisk(nsILoadContextInfo* aLoadContextInfo,
69 bool aPinned, const nsAString& aOrigin);
70 // Tries to load all contexts from the disk. This method is called just once
71 // after startup.
72 nsresult LoadEvictInfoFromDisk();
73 nsresult GetContextFile(nsILoadContextInfo* aLoadContextInfo, bool aPinned,
74 const nsAString& aOrigin, nsIFile** _retval);
76 void CreateIterators();
77 void CloseIterators();
78 void StartEvicting();
79 void EvictEntries();
81 // Whether eviction is in progress
82 bool mEvicting{false};
83 // Whether index is up to date. We wait with eviction until the index finishes
84 // update process when it is outdated. NOTE: We also stop eviction in progress
85 // when the index is found outdated, the eviction is restarted again once the
86 // update process finishes.
87 bool mIndexIsUpToDate{false};
88 // Whether we already tried to restore unfinished jobs from previous run after
89 // startup.
90 static bool sDiskAlreadySearched;
91 // Array of contexts being evicted.
92 nsTArray<UniquePtr<CacheFileContextEvictorEntry>> mEntries;
93 nsCOMPtr<nsIFile> mCacheDirectory;
94 nsCOMPtr<nsIFile> mEntriesDir;
97 } // namespace net
98 } // namespace mozilla
100 #endif