Bug 1866777 - Disable test_race_cache_with_network.js on windows opt for frequent...
[gecko.git] / netwerk / cache2 / CacheFile.h
blob97f986c143125c0d1858c1ee4d3b9b586435aabf
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 CacheFile__h__
6 #define CacheFile__h__
8 #include "CacheFileChunk.h"
9 #include "CacheFileIOManager.h"
10 #include "CacheFileMetadata.h"
11 #include "nsRefPtrHashtable.h"
12 #include "nsClassHashtable.h"
13 #include "mozilla/Mutex.h"
15 class nsIAsyncOutputStream;
16 class nsICacheEntry;
17 class nsICacheEntryMetaDataVisitor;
18 class nsIInputStream;
19 class nsIOutputStream;
21 namespace mozilla {
22 namespace net {
24 class CacheFileInputStream;
25 class CacheFileOutputStream;
26 class CacheOutputCloseListener;
27 class MetadataWriteTimer;
29 namespace CacheFileUtils {
30 class CacheFileLock;
33 #define CACHEFILELISTENER_IID \
34 { /* 95e7f284-84ba-48f9-b1fc-3a7336b4c33c */ \
35 0x95e7f284, 0x84ba, 0x48f9, { \
36 0xb1, 0xfc, 0x3a, 0x73, 0x36, 0xb4, 0xc3, 0x3c \
37 } \
40 class CacheFileListener : public nsISupports {
41 public:
42 NS_DECLARE_STATIC_IID_ACCESSOR(CACHEFILELISTENER_IID)
44 NS_IMETHOD OnFileReady(nsresult aResult, bool aIsNew) = 0;
45 NS_IMETHOD OnFileDoomed(nsresult aResult) = 0;
48 NS_DEFINE_STATIC_IID_ACCESSOR(CacheFileListener, CACHEFILELISTENER_IID)
50 class MOZ_CAPABILITY("mutex") CacheFile final
51 : public CacheFileChunkListener,
52 public CacheFileIOListener,
53 public CacheFileMetadataListener {
54 public:
55 NS_DECL_THREADSAFE_ISUPPORTS
57 CacheFile();
59 nsresult Init(const nsACString& aKey, bool aCreateNew, bool aMemoryOnly,
60 bool aSkipSizeCheck, bool aPriority, bool aPinned,
61 CacheFileListener* aCallback);
63 NS_IMETHOD OnChunkRead(nsresult aResult, CacheFileChunk* aChunk) override;
64 NS_IMETHOD OnChunkWritten(nsresult aResult, CacheFileChunk* aChunk) override;
65 NS_IMETHOD OnChunkAvailable(nsresult aResult, uint32_t aChunkIdx,
66 CacheFileChunk* aChunk) override;
67 NS_IMETHOD OnChunkUpdated(CacheFileChunk* aChunk) override;
69 NS_IMETHOD OnFileOpened(CacheFileHandle* aHandle, nsresult aResult) override;
70 NS_IMETHOD OnDataWritten(CacheFileHandle* aHandle, const char* aBuf,
71 nsresult aResult) override;
72 NS_IMETHOD OnDataRead(CacheFileHandle* aHandle, char* aBuf,
73 nsresult aResult) override;
74 NS_IMETHOD OnFileDoomed(CacheFileHandle* aHandle, nsresult aResult) override;
75 NS_IMETHOD OnEOFSet(CacheFileHandle* aHandle, nsresult aResult) override;
76 NS_IMETHOD OnFileRenamed(CacheFileHandle* aHandle, nsresult aResult) override;
77 virtual bool IsKilled() override;
79 NS_IMETHOD OnMetadataRead(nsresult aResult) override;
80 NS_IMETHOD OnMetadataWritten(nsresult aResult) override;
82 NS_IMETHOD OpenInputStream(nsICacheEntry* aCacheEntryHandle,
83 nsIInputStream** _retval);
84 NS_IMETHOD OpenAlternativeInputStream(nsICacheEntry* aCacheEntryHandle,
85 const char* aAltDataType,
86 nsIInputStream** _retval);
87 NS_IMETHOD OpenOutputStream(CacheOutputCloseListener* aCloseListener,
88 nsIOutputStream** _retval);
89 NS_IMETHOD OpenAlternativeOutputStream(
90 CacheOutputCloseListener* aCloseListener, const char* aAltDataType,
91 nsIAsyncOutputStream** _retval);
92 NS_IMETHOD SetMemoryOnly();
93 NS_IMETHOD Doom(CacheFileListener* aCallback);
95 void Kill() { mKill = true; }
96 nsresult ThrowMemoryCachedData();
98 nsresult GetAltDataSize(int64_t* aSize);
99 nsresult GetAltDataType(nsACString& aType);
101 // metadata forwarders
102 nsresult GetElement(const char* aKey, char** _retval);
103 nsresult SetElement(const char* aKey, const char* aValue);
104 nsresult VisitMetaData(nsICacheEntryMetaDataVisitor* aVisitor);
105 nsresult ElementsSize(uint32_t* _retval);
106 nsresult SetExpirationTime(uint32_t aExpirationTime);
107 nsresult GetExpirationTime(uint32_t* _retval);
108 nsresult SetFrecency(uint32_t aFrecency);
109 nsresult GetFrecency(uint32_t* _retval);
110 nsresult SetNetworkTimes(uint64_t aOnStartTime, uint64_t aOnStopTime);
111 nsresult SetContentType(uint8_t aContentType);
112 nsresult GetOnStartTime(uint64_t* _retval);
113 nsresult GetOnStopTime(uint64_t* _retval);
114 nsresult GetLastModified(uint32_t* _retval);
115 nsresult GetLastFetched(uint32_t* _retval);
116 nsresult GetFetchCount(uint32_t* _retval);
117 nsresult GetDiskStorageSizeInKB(uint32_t* aDiskStorageSize);
118 // Called by upper layers to indicated the entry has been fetched,
119 // i.e. delivered to the consumer.
120 nsresult OnFetched();
122 bool DataSize(int64_t* aSize);
123 void Key(nsACString& aKey);
124 bool IsDoomed();
125 bool IsPinned();
126 // Returns true when there is a potentially unfinished write operation.
127 bool IsWriteInProgress();
128 bool EntryWouldExceedLimit(int64_t aOffset, int64_t aSize, bool aIsAltData);
130 // Memory reporting
131 size_t SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
132 size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
134 private:
135 friend class CacheFileIOManager;
136 friend class CacheFileChunk;
137 friend class CacheFileInputStream;
138 friend class CacheFileOutputStream;
139 friend class CacheFileAutoLock;
140 friend class MetadataWriteTimer;
142 virtual ~CacheFile();
144 void Lock() MOZ_CAPABILITY_ACQUIRE() { mLock->Lock().Lock(); }
145 void Unlock() MOZ_CAPABILITY_RELEASE() {
146 // move the elements out of mObjsToRelease
147 // so that they can be released after we unlock
148 nsTArray<RefPtr<nsISupports>> objs = std::move(mObjsToRelease);
150 mLock->Lock().Unlock();
152 void AssertOwnsLock() const MOZ_ASSERT_CAPABILITY(this) {
153 mLock->Lock().AssertCurrentThreadOwns();
155 void ReleaseOutsideLock(RefPtr<nsISupports> aObject);
157 enum ECallerType { READER = 0, WRITER = 1, PRELOADER = 2 };
159 nsresult DoomLocked(CacheFileListener* aCallback);
161 nsresult GetChunkLocked(uint32_t aIndex, ECallerType aCaller,
162 CacheFileChunkListener* aCallback,
163 CacheFileChunk** _retval);
165 void PreloadChunks(uint32_t aIndex);
166 bool ShouldCacheChunk(uint32_t aIndex);
167 bool MustKeepCachedChunk(uint32_t aIndex);
169 nsresult DeactivateChunk(CacheFileChunk* aChunk);
170 void RemoveChunkInternal(CacheFileChunk* aChunk, bool aCacheChunk);
172 bool OutputStreamExists(bool aAlternativeData);
173 // Returns number of bytes that are available and can be read by input stream
174 // without waiting for the data. The amount is counted from the start of
175 // aIndex chunk and it is guaranteed that this data won't be released by
176 // CleanUpCachedChunks().
177 int64_t BytesFromChunk(uint32_t aIndex, bool aAlternativeData);
178 nsresult Truncate(int64_t aOffset);
180 void RemoveInput(CacheFileInputStream* aInput, nsresult aStatus);
181 void RemoveOutput(CacheFileOutputStream* aOutput, nsresult aStatus);
182 nsresult NotifyChunkListener(CacheFileChunkListener* aCallback,
183 nsIEventTarget* aTarget, nsresult aResult,
184 uint32_t aChunkIdx, CacheFileChunk* aChunk);
185 void QueueChunkListener(uint32_t aIndex, CacheFileChunkListener* aCallback);
186 nsresult NotifyChunkListeners(uint32_t aIndex, nsresult aResult,
187 CacheFileChunk* aChunk);
188 bool HaveChunkListeners(uint32_t aIndex);
189 void NotifyListenersAboutOutputRemoval();
191 bool IsDirty() MOZ_REQUIRES(this);
192 void WriteMetadataIfNeeded();
193 void WriteMetadataIfNeededLocked(bool aFireAndForget = false)
194 MOZ_REQUIRES(this);
195 void PostWriteTimer() MOZ_REQUIRES(this);
197 void CleanUpCachedChunks() MOZ_REQUIRES(this);
199 nsresult PadChunkWithZeroes(uint32_t aChunkIdx);
201 void SetError(nsresult aStatus);
202 nsresult SetAltMetadata(const char* aAltMetadata);
204 nsresult InitIndexEntry();
206 bool mOpeningFile MOZ_GUARDED_BY(this){false};
207 bool mReady MOZ_GUARDED_BY(this){false};
208 bool mMemoryOnly MOZ_GUARDED_BY(this){false};
209 bool mSkipSizeCheck MOZ_GUARDED_BY(this){false};
210 bool mOpenAsMemoryOnly MOZ_GUARDED_BY(this){false};
211 bool mPinned MOZ_GUARDED_BY(this){false};
212 bool mPriority MOZ_GUARDED_BY(this){false};
213 bool mDataAccessed MOZ_GUARDED_BY(this){false};
214 bool mDataIsDirty MOZ_GUARDED_BY(this){false};
215 bool mWritingMetadata MOZ_GUARDED_BY(this){false};
216 bool mPreloadWithoutInputStreams MOZ_GUARDED_BY(this){true};
217 uint32_t mPreloadChunkCount MOZ_GUARDED_BY(this){0};
218 nsresult mStatus MOZ_GUARDED_BY(this){NS_OK};
219 // Size of the whole data including eventual alternative data represenation.
220 int64_t mDataSize MOZ_GUARDED_BY(this){-1};
222 // If there is alternative data present, it contains size of the original
223 // data, i.e. offset where alternative data starts. Otherwise it is -1.
224 int64_t mAltDataOffset MOZ_GUARDED_BY(this){-1};
226 nsCString mKey MOZ_GUARDED_BY(this);
227 nsCString mAltDataType
228 MOZ_GUARDED_BY(this); // The type of the saved alt-data. May be empty.
230 RefPtr<CacheFileHandle> mHandle MOZ_GUARDED_BY(this);
231 RefPtr<CacheFileMetadata> mMetadata MOZ_GUARDED_BY(this);
232 nsCOMPtr<CacheFileListener> mListener MOZ_GUARDED_BY(this);
233 nsCOMPtr<CacheFileIOListener> mDoomAfterOpenListener MOZ_GUARDED_BY(this);
234 Atomic<bool, Relaxed> mKill{false};
236 nsRefPtrHashtable<nsUint32HashKey, CacheFileChunk> mChunks
237 MOZ_GUARDED_BY(this);
238 nsClassHashtable<nsUint32HashKey, ChunkListeners> mChunkListeners
239 MOZ_GUARDED_BY(this);
240 nsRefPtrHashtable<nsUint32HashKey, CacheFileChunk> mCachedChunks
241 MOZ_GUARDED_BY(this);
242 // We can truncate data only if there is no input/output stream beyond the
243 // truncate position, so only unused chunks can be thrown away. But it can
244 // happen that we need to throw away a chunk that is still in mChunks (i.e.
245 // an active chunk) because deactivation happens with a small delay. We cannot
246 // delete such chunk immediately but we need to ensure that such chunk won't
247 // be returned by GetChunkLocked, so we move this chunk into mDiscardedChunks
248 // and mark it as discarded.
249 nsTArray<RefPtr<CacheFileChunk>> mDiscardedChunks MOZ_GUARDED_BY(this);
251 nsTArray<CacheFileInputStream*> mInputs MOZ_GUARDED_BY(this);
252 CacheFileOutputStream* mOutput MOZ_GUARDED_BY(this){nullptr};
254 nsTArray<RefPtr<nsISupports>> mObjsToRelease MOZ_GUARDED_BY(this);
255 RefPtr<CacheFileUtils::CacheFileLock> mLock;
258 class MOZ_RAII MOZ_SCOPED_CAPABILITY CacheFileAutoLock {
259 public:
260 explicit CacheFileAutoLock(CacheFile* aFile) MOZ_CAPABILITY_ACQUIRE(aFile)
261 : mFile(aFile), mLocked(true) {
262 mFile->Lock();
264 ~CacheFileAutoLock() MOZ_CAPABILITY_RELEASE() {
265 if (mLocked) {
266 mFile->Unlock();
269 void Lock() MOZ_CAPABILITY_ACQUIRE() {
270 MOZ_ASSERT(!mLocked);
271 mFile->Lock();
272 mLocked = true;
274 void Unlock() MOZ_CAPABILITY_RELEASE() {
275 MOZ_ASSERT(mLocked);
276 mFile->Unlock();
277 mLocked = false;
280 private:
281 RefPtr<CacheFile> mFile;
282 bool mLocked;
285 } // namespace net
286 } // namespace mozilla
288 #endif