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