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 CacheEntry__h__
6 #define CacheEntry__h__
7 
8 #include "nsICacheEntry.h"
9 #include "CacheFile.h"
10 
11 #include "nsIRunnable.h"
12 #include "nsIOutputStream.h"
13 #include "nsICacheEntryOpenCallback.h"
14 #include "nsICacheEntryDoomCallback.h"
15 
16 #include "nsCOMPtr.h"
17 #include "nsRefPtrHashtable.h"
18 #include "nsHashKeys.h"
19 #include "nsString.h"
20 #include "nsCOMArray.h"
21 #include "nsThreadUtils.h"
22 #include "mozilla/Attributes.h"
23 #include "mozilla/Mutex.h"
24 #include "mozilla/TimeStamp.h"
25 
PRTimeToSeconds(PRTime t_usec)26 static inline uint32_t PRTimeToSeconds(PRTime t_usec) {
27   return uint32_t(t_usec / PR_USEC_PER_SEC);
28 }
29 
30 #define NowInSeconds() PRTimeToSeconds(PR_Now())
31 
32 class nsIOutputStream;
33 class nsIURI;
34 class nsIThread;
35 
36 namespace mozilla {
37 namespace net {
38 
39 class CacheStorageService;
40 class CacheStorage;
41 class CacheOutputCloseListener;
42 class CacheEntryHandle;
43 
44 class CacheEntry final : public nsIRunnable, public CacheFileListener {
45  public:
46   NS_DECL_THREADSAFE_ISUPPORTS
47   NS_DECL_NSIRUNNABLE
48 
49   static uint64_t GetNextId();
50 
51   CacheEntry(const nsACString& aStorageID, const nsACString& aURI,
52              const nsACString& aEnhanceID, bool aUseDisk, bool aSkipSizeCheck,
53              bool aPin);
54 
55   void AsyncOpen(nsICacheEntryOpenCallback* aCallback, uint32_t aFlags);
56 
57   CacheEntryHandle* NewHandle();
58   // For a new and recreated entry w/o a callback, we need to wrap it
59   // with a handle to detect writing consumer is gone.
60   CacheEntryHandle* NewWriteHandle();
61 
62   // Forwarded to from CacheEntryHandle : nsICacheEntry
63   nsresult GetKey(nsACString& aKey);
64   nsresult GetCacheEntryId(uint64_t* aCacheEntryId);
65   nsresult GetPersistent(bool* aPersistToDisk);
66   nsresult GetFetchCount(int32_t* aFetchCount);
67   nsresult GetLastFetched(uint32_t* aLastFetched);
68   nsresult GetLastModified(uint32_t* aLastModified);
69   nsresult GetExpirationTime(uint32_t* aExpirationTime);
70   nsresult SetExpirationTime(uint32_t expirationTime);
71   nsresult GetOnStartTime(uint64_t* aTime);
72   nsresult GetOnStopTime(uint64_t* aTime);
73   nsresult SetNetworkTimes(uint64_t onStartTime, uint64_t onStopTime);
74   nsresult SetContentType(uint8_t aContentType);
75   nsresult ForceValidFor(uint32_t aSecondsToTheFuture);
76   nsresult GetIsForcedValid(bool* aIsForcedValid);
77   nsresult OpenInputStream(int64_t offset, nsIInputStream** _retval);
78   nsresult OpenOutputStream(int64_t offset, int64_t predictedSize,
79                             nsIOutputStream** _retval);
80   nsresult GetSecurityInfo(nsISupports** aSecurityInfo);
81   nsresult SetSecurityInfo(nsISupports* aSecurityInfo);
82   nsresult GetStorageDataSize(uint32_t* aStorageDataSize);
83   nsresult AsyncDoom(nsICacheEntryDoomCallback* aCallback);
84   nsresult GetMetaDataElement(const char* key, char** aRetval);
85   nsresult SetMetaDataElement(const char* key, const char* value);
86   nsresult VisitMetaData(nsICacheEntryMetaDataVisitor* visitor);
87   nsresult MetaDataReady(void);
88   nsresult SetValid(void);
89   nsresult GetDiskStorageSizeInKB(uint32_t* aDiskStorageSizeInKB);
90   nsresult Recreate(bool aMemoryOnly, nsICacheEntry** _retval);
91   nsresult GetDataSize(int64_t* aDataSize);
92   nsresult GetAltDataSize(int64_t* aDataSize);
93   nsresult GetAltDataType(nsACString& aAltDataType);
94   nsresult OpenAlternativeOutputStream(const nsACString& type,
95                                        int64_t predictedSize,
96                                        nsIAsyncOutputStream** _retval);
97   nsresult OpenAlternativeInputStream(const nsACString& type,
98                                       nsIInputStream** _retval);
99   nsresult GetLoadContextInfo(nsILoadContextInfo** aInfo);
100   nsresult Close(void);
101   nsresult MarkValid(void);
102   nsresult MaybeMarkValid(void);
103   nsresult HasWriteAccess(bool aWriteAllowed, bool* aWriteAccess);
104 
105  public:
106   uint32_t GetMetadataMemoryConsumption();
GetStorageID()107   nsCString const& GetStorageID() const { return mStorageID; }
GetEnhanceID()108   nsCString const& GetEnhanceID() const { return mEnhanceID; }
GetURI()109   nsCString const& GetURI() const { return mURI; }
110   // Accessible at any time
IsUsingDisk()111   bool IsUsingDisk() const { return mUseDisk; }
112   bool IsReferenced() const;
113   bool IsFileDoomed();
IsDoomed()114   bool IsDoomed() const { return mIsDoomed; }
IsPinned()115   bool IsPinned() const { return mPinned; }
116 
117   // Methods for entry management (eviction from memory),
118   // called only on the management thread.
119 
120   // TODO make these inline
121   double GetFrecency() const;
122   uint32_t GetExpirationTime() const;
UseCount()123   uint32_t UseCount() const { return mUseCount; }
124 
125   bool IsRegistered() const;
126   bool CanRegister() const;
127   void SetRegistered(bool aRegistered);
128 
LoadStart()129   TimeStamp const& LoadStart() const { return mLoadStart; }
130 
131   enum EPurge {
132     PURGE_DATA_ONLY_DISK_BACKED,
133     PURGE_WHOLE_ONLY_DISK_BACKED,
134     PURGE_WHOLE,
135   };
136 
137   bool DeferOrBypassRemovalOnPinStatus(bool aPinned);
138   bool Purge(uint32_t aWhat);
139   void PurgeAndDoom();
140   void DoomAlreadyRemoved();
141 
142   nsresult HashingKeyWithStorage(nsACString& aResult) const;
143   nsresult HashingKey(nsACString& aResult) const;
144 
145   static nsresult HashingKey(const nsACString& aStorageID,
146                              const nsACString& aEnhanceID, nsIURI* aURI,
147                              nsACString& aResult);
148 
149   static nsresult HashingKey(const nsACString& aStorageID,
150                              const nsACString& aEnhanceID,
151                              const nsACString& aURISpec, nsACString& aResult);
152 
153   // Accessed only on the service management thread
154   double mFrecency{0};
155   ::mozilla::Atomic<uint32_t, ::mozilla::Relaxed> mSortingExpirationTime{
156       uint32_t(-1)};
157 
158   // Memory reporting
159   size_t SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
160   size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
161 
162  private:
163   virtual ~CacheEntry();
164 
165   // CacheFileListener
166   NS_IMETHOD OnFileReady(nsresult aResult, bool aIsNew) override;
167   NS_IMETHOD OnFileDoomed(nsresult aResult) override;
168 
169   // Keep the service alive during life-time of an entry
170   RefPtr<CacheStorageService> mService;
171 
172   // We must monitor when a cache entry whose consumer is responsible
173   // for writing it the first time gets released.  We must then invoke
174   // waiting callbacks to not break the chain.
175   class Callback {
176    public:
177     Callback(CacheEntry* aEntry, nsICacheEntryOpenCallback* aCallback,
178              bool aReadOnly, bool aCheckOnAnyThread, bool aSecret);
179     // Special constructor for Callback objects added to the chain
180     // just to ensure proper defer dooming (recreation) of this entry.
181     Callback(CacheEntry* aEntry, bool aDoomWhenFoundInPinStatus);
182     Callback(Callback const& aThat);
183     ~Callback();
184 
185     // Called when this callback record changes it's owning entry,
186     // mainly during recreation.
187     void ExchangeEntry(CacheEntry* aEntry);
188 
189     // Returns true when an entry is about to be "defer" doomed and this is
190     // a "defer" callback.
191     bool DeferDoom(bool* aDoom) const;
192 
193     // We are raising reference count here to take into account the pending
194     // callback (that virtually holds a ref to this entry before it gets
195     // it's pointer).
196     RefPtr<CacheEntry> mEntry;
197     nsCOMPtr<nsICacheEntryOpenCallback> mCallback;
198     nsCOMPtr<nsIEventTarget> mTarget;
199     bool mReadOnly : 1;
200     bool mRevalidating : 1;
201     bool mCheckOnAnyThread : 1;
202     bool mRecheckAfterWrite : 1;
203     bool mNotWanted : 1;
204     bool mSecret : 1;
205 
206     // These are set only for the defer-doomer Callback instance inserted
207     // to the callback chain.  When any of these is set and also any of
208     // the corressponding flags on the entry is set, this callback will
209     // indicate (via DeferDoom()) the entry have to be recreated/doomed.
210     bool mDoomWhenFoundPinned : 1;
211     bool mDoomWhenFoundNonPinned : 1;
212 
213     nsresult OnCheckThread(bool* aOnCheckThread) const;
214     nsresult OnAvailThread(bool* aOnAvailThread) const;
215   };
216 
217   // Since OnCacheEntryAvailable must be invoked on the main thread
218   // we need a runnable for it...
219   class AvailableCallbackRunnable : public Runnable {
220    public:
AvailableCallbackRunnable(CacheEntry * aEntry,Callback const & aCallback)221     AvailableCallbackRunnable(CacheEntry* aEntry, Callback const& aCallback)
222         : Runnable("CacheEntry::AvailableCallbackRunnable"),
223           mEntry(aEntry),
224           mCallback(aCallback) {}
225 
226    private:
Run()227     NS_IMETHOD Run() override {
228       mEntry->InvokeAvailableCallback(mCallback);
229       return NS_OK;
230     }
231 
232     RefPtr<CacheEntry> mEntry;
233     Callback mCallback;
234   };
235 
236   // Since OnCacheEntryDoomed must be invoked on the main thread
237   // we need a runnable for it...
238   class DoomCallbackRunnable : public Runnable {
239    public:
DoomCallbackRunnable(CacheEntry * aEntry,nsresult aRv)240     DoomCallbackRunnable(CacheEntry* aEntry, nsresult aRv)
241         : Runnable("net::CacheEntry::DoomCallbackRunnable"),
242           mEntry(aEntry),
243           mRv(aRv) {}
244 
245    private:
Run()246     NS_IMETHOD Run() override {
247       nsCOMPtr<nsICacheEntryDoomCallback> callback;
248       {
249         mozilla::MutexAutoLock lock(mEntry->mLock);
250         mEntry->mDoomCallback.swap(callback);
251       }
252 
253       if (callback) callback->OnCacheEntryDoomed(mRv);
254       return NS_OK;
255     }
256 
257     RefPtr<CacheEntry> mEntry;
258     nsresult mRv;
259   };
260 
261   // Starts the load or just invokes the callback, bypasses (when required)
262   // if busy.  Returns true on job done, false on bypass.
263   bool Open(Callback& aCallback, bool aTruncate, bool aPriority,
264             bool aBypassIfBusy);
265   // Loads from disk asynchronously
266   bool Load(bool aTruncate, bool aPriority);
267 
268   void RememberCallback(Callback& aCallback);
269   void InvokeCallbacksLock();
270   void InvokeCallbacks();
271   bool InvokeCallbacks(bool aReadOnly);
272   bool InvokeCallback(Callback& aCallback);
273   void InvokeAvailableCallback(Callback const& aCallback);
274   void OnFetched(Callback const& aCallback);
275 
276   nsresult OpenOutputStreamInternal(int64_t offset, nsIOutputStream** _retval);
277   nsresult OpenInputStreamInternal(int64_t offset, const char* aAltDataType,
278                                    nsIInputStream** _retval);
279 
280   void OnHandleClosed(CacheEntryHandle const* aHandle);
281 
282  private:
283   friend class CacheEntryHandle;
284   // Increment/decrements the number of handles keeping this entry.
AddHandleRef()285   void AddHandleRef() { ++mHandlesCount; }
ReleaseHandleRef()286   void ReleaseHandleRef() { --mHandlesCount; }
287   // Current number of handles keeping this entry.
HandlesCount()288   uint32_t HandlesCount() const { return mHandlesCount; }
289 
290  private:
291   friend class CacheOutputCloseListener;
292   void OnOutputClosed();
293 
294  private:
295   // Schedules a background operation on the management thread.
296   // When executed on the management thread directly, the operation(s)
297   // is (are) executed immediately.
298   void BackgroundOp(uint32_t aOperation, bool aForceAsync = false);
299   void StoreFrecency(double aFrecency);
300 
301   // Called only from DoomAlreadyRemoved()
302   void DoomFile();
303   // When this entry is doomed the first time, this method removes
304   // any force-valid timing info for this entry.
305   void RemoveForcedValidity();
306 
307   already_AddRefed<CacheEntryHandle> ReopenTruncated(
308       bool aMemoryOnly, nsICacheEntryOpenCallback* aCallback);
309   void TransferCallbacks(CacheEntry& aFromEntry);
310 
311   mozilla::Mutex mLock{"CacheEntry"};
312 
313   // Reflects the number of existing handles for this entry
314   ::mozilla::ThreadSafeAutoRefCnt mHandlesCount;
315 
316   nsTArray<Callback> mCallbacks;
317   nsCOMPtr<nsICacheEntryDoomCallback> mDoomCallback;
318 
319   RefPtr<CacheFile> mFile;
320 
321   // Using ReleaseAcquire since we only control access to mFile with this.
322   // When mFileStatus is read and found success it is ensured there is mFile and
323   // that it is after a successful call to Init().
324   Atomic<nsresult, ReleaseAcquire> mFileStatus{NS_ERROR_NOT_INITIALIZED};
325   nsCString mURI;
326   nsCString mEnhanceID;
327   nsCString mStorageID;
328 
329   // mUseDisk, mSkipSizeCheck, mIsDoomed are plain "bool", not "bool:1",
330   // so as to avoid bitfield races with the byte containing
331   // mSecurityInfoLoaded et al.  See bug 1278524.
332   //
333   // Whether it's allowed to persist the data to disk
334   bool const mUseDisk;
335   // Whether it should skip max size check.
336   bool const mSkipSizeCheck;
337   // Set when entry is doomed with AsyncDoom() or DoomAlreadyRemoved().
338   bool mIsDoomed{false};
339 
340   // Following flags are all synchronized with the cache entry lock.
341 
342   // Whether security info has already been looked up in metadata.
343   bool mSecurityInfoLoaded : 1;
344   // Prevents any callback invocation
345   bool mPreventCallbacks : 1;
346   // true: after load and an existing file, or after output stream has been
347   //       opened.
348   //       note - when opening an input stream, and this flag is false, output
349   //       stream is open along ; this makes input streams on new entries
350   //       behave correctly when EOF is reached (WOULD_BLOCK is returned).
351   // false: after load and a new file, or dropped to back to false when a
352   //        writer fails to open an output stream.
353   bool mHasData : 1;
354   // The indication of pinning this entry was open with
355   bool mPinned : 1;
356   // Whether the pinning state of the entry is known (equals to the actual state
357   // of the cache file)
358   bool mPinningKnown : 1;
359 
360   static char const* StateString(uint32_t aState);
361 
362   enum EState {       // transiting to:
363     NOTLOADED = 0,    // -> LOADING | EMPTY
364     LOADING = 1,      // -> EMPTY | READY
365     EMPTY = 2,        // -> WRITING
366     WRITING = 3,      // -> EMPTY | READY
367     READY = 4,        // -> REVALIDATING
368     REVALIDATING = 5  // -> READY
369   };
370 
371   // State of this entry.
372   EState mState{NOTLOADED};
373 
374   enum ERegistration {
375     NEVERREGISTERED = 0,  // The entry has never been registered
376     REGISTERED = 1,       // The entry is stored in the memory pool index
377     DEREGISTERED = 2      // The entry has been removed from the pool
378   };
379 
380   // Accessed only on the management thread.  Records the state of registration
381   // this entry in the memory pool intermediate cache.
382   ERegistration mRegistration{NEVERREGISTERED};
383 
384   // If a new (empty) entry is requested to open an input stream before
385   // output stream has been opened, we must open output stream internally
386   // on CacheFile and hold until writer releases the entry or opens the output
387   // stream for read (then we trade him mOutputStream).
388   nsCOMPtr<nsIOutputStream> mOutputStream;
389 
390   // Weak reference to the current writter.  There can be more then one
391   // writer at a time and OnHandleClosed() must be processed only for the
392   // current one.
393   CacheEntryHandle* mWriter{nullptr};
394 
395   // Background thread scheduled operation.  Set (under the lock) one
396   // of this flags to tell the background thread what to do.
397   class Ops {
398    public:
399     static uint32_t const REGISTER = 1 << 0;
400     static uint32_t const FRECENCYUPDATE = 1 << 1;
401     static uint32_t const CALLBACKS = 1 << 2;
402     static uint32_t const UNREGISTER = 1 << 3;
403 
404     Ops() = default;
Grab()405     uint32_t Grab() {
406       uint32_t flags = mFlags;
407       mFlags = 0;
408       return flags;
409     }
Set(uint32_t aFlags)410     bool Set(uint32_t aFlags) {
411       if (mFlags & aFlags) return false;
412       mFlags |= aFlags;
413       return true;
414     }
415 
416    private:
417     uint32_t mFlags{0};
418   } mBackgroundOperations;
419 
420   nsCOMPtr<nsISupports> mSecurityInfo;
421   mozilla::TimeStamp mLoadStart;
422   uint32_t mUseCount{0};
423 
424   const uint64_t mCacheEntryId;
425 };
426 
427 class CacheEntryHandle final : public nsICacheEntry {
428  public:
429   explicit CacheEntryHandle(CacheEntry* aEntry);
Entry()430   CacheEntry* Entry() const { return mEntry; }
431 
432   NS_DECL_THREADSAFE_ISUPPORTS
433 
434   // Default implementation is simply safely forwarded.
GetKey(nsACString & aKey)435   NS_IMETHOD GetKey(nsACString& aKey) override { return mEntry->GetKey(aKey); }
GetCacheEntryId(uint64_t * aCacheEntryId)436   NS_IMETHOD GetCacheEntryId(uint64_t* aCacheEntryId) override {
437     return mEntry->GetCacheEntryId(aCacheEntryId);
438   }
GetPersistent(bool * aPersistent)439   NS_IMETHOD GetPersistent(bool* aPersistent) override {
440     return mEntry->GetPersistent(aPersistent);
441   }
GetFetchCount(int32_t * aFetchCount)442   NS_IMETHOD GetFetchCount(int32_t* aFetchCount) override {
443     return mEntry->GetFetchCount(aFetchCount);
444   }
GetLastFetched(uint32_t * aLastFetched)445   NS_IMETHOD GetLastFetched(uint32_t* aLastFetched) override {
446     return mEntry->GetLastFetched(aLastFetched);
447   }
GetLastModified(uint32_t * aLastModified)448   NS_IMETHOD GetLastModified(uint32_t* aLastModified) override {
449     return mEntry->GetLastModified(aLastModified);
450   }
GetExpirationTime(uint32_t * aExpirationTime)451   NS_IMETHOD GetExpirationTime(uint32_t* aExpirationTime) override {
452     return mEntry->GetExpirationTime(aExpirationTime);
453   }
SetExpirationTime(uint32_t expirationTime)454   NS_IMETHOD SetExpirationTime(uint32_t expirationTime) override {
455     return mEntry->SetExpirationTime(expirationTime);
456   }
GetOnStartTime(uint64_t * aOnStartTime)457   NS_IMETHOD GetOnStartTime(uint64_t* aOnStartTime) override {
458     return mEntry->GetOnStartTime(aOnStartTime);
459   }
GetOnStopTime(uint64_t * aOnStopTime)460   NS_IMETHOD GetOnStopTime(uint64_t* aOnStopTime) override {
461     return mEntry->GetOnStopTime(aOnStopTime);
462   }
SetNetworkTimes(uint64_t onStartTime,uint64_t onStopTime)463   NS_IMETHOD SetNetworkTimes(uint64_t onStartTime,
464                              uint64_t onStopTime) override {
465     return mEntry->SetNetworkTimes(onStartTime, onStopTime);
466   }
SetContentType(uint8_t contentType)467   NS_IMETHOD SetContentType(uint8_t contentType) override {
468     return mEntry->SetContentType(contentType);
469   }
ForceValidFor(uint32_t aSecondsToTheFuture)470   NS_IMETHOD ForceValidFor(uint32_t aSecondsToTheFuture) override {
471     return mEntry->ForceValidFor(aSecondsToTheFuture);
472   }
GetIsForcedValid(bool * aIsForcedValid)473   NS_IMETHOD GetIsForcedValid(bool* aIsForcedValid) override {
474     return mEntry->GetIsForcedValid(aIsForcedValid);
475   }
OpenInputStream(int64_t offset,nsIInputStream ** _retval)476   NS_IMETHOD OpenInputStream(int64_t offset,
477                              nsIInputStream** _retval) override {
478     return mEntry->OpenInputStream(offset, _retval);
479   }
OpenOutputStream(int64_t offset,int64_t predictedSize,nsIOutputStream ** _retval)480   NS_IMETHOD OpenOutputStream(int64_t offset, int64_t predictedSize,
481                               nsIOutputStream** _retval) override {
482     return mEntry->OpenOutputStream(offset, predictedSize, _retval);
483   }
GetSecurityInfo(nsISupports ** aSecurityInfo)484   NS_IMETHOD GetSecurityInfo(nsISupports** aSecurityInfo) override {
485     return mEntry->GetSecurityInfo(aSecurityInfo);
486   }
SetSecurityInfo(nsISupports * aSecurityInfo)487   NS_IMETHOD SetSecurityInfo(nsISupports* aSecurityInfo) override {
488     return mEntry->SetSecurityInfo(aSecurityInfo);
489   }
GetStorageDataSize(uint32_t * aStorageDataSize)490   NS_IMETHOD GetStorageDataSize(uint32_t* aStorageDataSize) override {
491     return mEntry->GetStorageDataSize(aStorageDataSize);
492   }
AsyncDoom(nsICacheEntryDoomCallback * listener)493   NS_IMETHOD AsyncDoom(nsICacheEntryDoomCallback* listener) override {
494     return mEntry->AsyncDoom(listener);
495   }
GetMetaDataElement(const char * key,char ** _retval)496   NS_IMETHOD GetMetaDataElement(const char* key, char** _retval) override {
497     return mEntry->GetMetaDataElement(key, _retval);
498   }
SetMetaDataElement(const char * key,const char * value)499   NS_IMETHOD SetMetaDataElement(const char* key, const char* value) override {
500     return mEntry->SetMetaDataElement(key, value);
501   }
VisitMetaData(nsICacheEntryMetaDataVisitor * visitor)502   NS_IMETHOD VisitMetaData(nsICacheEntryMetaDataVisitor* visitor) override {
503     return mEntry->VisitMetaData(visitor);
504   }
MetaDataReady(void)505   NS_IMETHOD MetaDataReady(void) override { return mEntry->MetaDataReady(); }
SetValid(void)506   NS_IMETHOD SetValid(void) override { return mEntry->SetValid(); }
GetDiskStorageSizeInKB(uint32_t * aDiskStorageSizeInKB)507   NS_IMETHOD GetDiskStorageSizeInKB(uint32_t* aDiskStorageSizeInKB) override {
508     return mEntry->GetDiskStorageSizeInKB(aDiskStorageSizeInKB);
509   }
Recreate(bool aMemoryOnly,nsICacheEntry ** _retval)510   NS_IMETHOD Recreate(bool aMemoryOnly, nsICacheEntry** _retval) override {
511     return mEntry->Recreate(aMemoryOnly, _retval);
512   }
GetDataSize(int64_t * aDataSize)513   NS_IMETHOD GetDataSize(int64_t* aDataSize) override {
514     return mEntry->GetDataSize(aDataSize);
515   }
GetAltDataSize(int64_t * aAltDataSize)516   NS_IMETHOD GetAltDataSize(int64_t* aAltDataSize) override {
517     return mEntry->GetAltDataSize(aAltDataSize);
518   }
GetAltDataType(nsACString & aType)519   NS_IMETHOD GetAltDataType(nsACString& aType) override {
520     return mEntry->GetAltDataType(aType);
521   }
OpenAlternativeOutputStream(const nsACString & type,int64_t predictedSize,nsIAsyncOutputStream ** _retval)522   NS_IMETHOD OpenAlternativeOutputStream(
523       const nsACString& type, int64_t predictedSize,
524       nsIAsyncOutputStream** _retval) override {
525     return mEntry->OpenAlternativeOutputStream(type, predictedSize, _retval);
526   }
OpenAlternativeInputStream(const nsACString & type,nsIInputStream ** _retval)527   NS_IMETHOD OpenAlternativeInputStream(const nsACString& type,
528                                         nsIInputStream** _retval) override {
529     return mEntry->OpenAlternativeInputStream(type, _retval);
530   }
GetLoadContextInfo(nsILoadContextInfo ** aLoadContextInfo)531   NS_IMETHOD GetLoadContextInfo(
532       nsILoadContextInfo** aLoadContextInfo) override {
533     return mEntry->GetLoadContextInfo(aLoadContextInfo);
534   }
Close(void)535   NS_IMETHOD Close(void) override { return mEntry->Close(); }
MarkValid(void)536   NS_IMETHOD MarkValid(void) override { return mEntry->MarkValid(); }
MaybeMarkValid(void)537   NS_IMETHOD MaybeMarkValid(void) override { return mEntry->MaybeMarkValid(); }
HasWriteAccess(bool aWriteAllowed,bool * _retval)538   NS_IMETHOD HasWriteAccess(bool aWriteAllowed, bool* _retval) override {
539     return mEntry->HasWriteAccess(aWriteAllowed, _retval);
540   }
541 
542   // Specific implementation:
543   NS_IMETHOD Dismiss() override;
544 
545  private:
546   virtual ~CacheEntryHandle();
547   RefPtr<CacheEntry> mEntry;
548 
549   // This is |false| until Dismiss() was called and prevents OnHandleClosed
550   // being called more than once.
551   Atomic<bool, ReleaseAcquire> mClosed{false};
552 };
553 
554 class CacheOutputCloseListener final : public Runnable {
555  public:
556   void OnOutputClosed();
557 
558  private:
559   friend class CacheEntry;
560 
561   virtual ~CacheOutputCloseListener() = default;
562 
563   NS_DECL_NSIRUNNABLE
564   explicit CacheOutputCloseListener(CacheEntry* aEntry);
565 
566  private:
567   RefPtr<CacheEntry> mEntry;
568 };
569 
570 }  // namespace net
571 }  // namespace mozilla
572 
573 #endif
574