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"
14 class nsILoadContextInfo
;
19 class CacheIndexIterator
;
21 struct CacheFileContextEvictorEntry
{
22 nsCOMPtr
<nsILoadContextInfo
> mInfo
;
24 nsString mOrigin
; // it can be empty
25 PRTime mTimeStamp
= 0; // in milliseconds
26 RefPtr
<CacheIndexIterator
> mIterator
;
29 class CacheFileContextEvictor
{
31 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CacheFileContextEvictor
)
33 CacheFileContextEvictor();
36 virtual ~CacheFileContextEvictor();
39 nsresult
Init(nsIFile
* aCacheDirectory
);
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
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
56 void WasEvicted(const nsACString
& aKey
, nsIFile
* aFile
,
57 bool* aEvictedAsPinned
, bool* aEvictedAsNonPinned
);
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
72 nsresult
LoadEvictInfoFromDisk();
73 nsresult
GetContextFile(nsILoadContextInfo
* aLoadContextInfo
, bool aPinned
,
74 const nsAString
& aOrigin
, nsIFile
** _retval
);
76 void CreateIterators();
77 void CloseIterators();
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
90 static bool sDiskAlreadySearched
;
91 // Array of contexts being evicted.
92 nsTArray
<UniquePtr
<CacheFileContextEvictorEntry
>> mEntries
;
93 nsCOMPtr
<nsIFile
> mCacheDirectory
;
94 nsCOMPtr
<nsIFile
> mEntriesDir
;
98 } // namespace mozilla