Source code

Revision control

Other Tools

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