tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

CacheFileInputStream.h (2458B)


      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 CacheFileInputStream__h__
      6 #define CacheFileInputStream__h__
      7 
      8 #include "nsIAsyncInputStream.h"
      9 #include "nsISeekableStream.h"
     10 #include "nsCOMPtr.h"
     11 #include "CacheFileChunk.h"
     12 
     13 namespace mozilla {
     14 namespace net {
     15 
     16 class CacheFile;
     17 
     18 class CacheFileInputStream : public nsIAsyncInputStream,
     19                             public nsISeekableStream,
     20                             public CacheFileChunkListener {
     21  NS_DECL_THREADSAFE_ISUPPORTS
     22  NS_DECL_NSIINPUTSTREAM
     23  NS_DECL_NSIASYNCINPUTSTREAM
     24  NS_DECL_NSISEEKABLESTREAM
     25  NS_DECL_NSITELLABLESTREAM
     26 
     27 public:
     28  explicit CacheFileInputStream(CacheFile* aFile, nsISupports* aEntry,
     29                                bool aAlternativeData);
     30 
     31  NS_IMETHOD OnChunkRead(nsresult aResult, CacheFileChunk* aChunk) override;
     32  NS_IMETHOD OnChunkWritten(nsresult aResult, CacheFileChunk* aChunk) override;
     33  NS_IMETHOD OnChunkAvailable(nsresult aResult, uint32_t aChunkIdx,
     34                              CacheFileChunk* aChunk) override;
     35  NS_IMETHOD OnChunkUpdated(CacheFileChunk* aChunk) override;
     36 
     37  // Memory reporting
     38  size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
     39 
     40  uint32_t GetPosition() const { return mPos; };
     41  bool IsAlternativeData() const { return mAlternativeData; };
     42  int64_t GetChunkIdx() const {
     43    return mChunk ? static_cast<int64_t>(mChunk->Index()) : -1;
     44  };
     45 
     46 private:
     47  virtual ~CacheFileInputStream();
     48 
     49  void CloseWithStatusLocked(nsresult aStatus);
     50  void CleanUp();
     51  void ReleaseChunk();
     52  void EnsureCorrectChunk(bool aReleaseOnly);
     53 
     54  // CanRead returns negative value when output stream truncates the data before
     55  // the input stream's mPos.
     56  int64_t CanRead(CacheFileChunkReadHandle* aHandle);
     57  void NotifyListener();
     58  void MaybeNotifyListener();
     59 
     60  RefPtr<CacheFile> mFile;
     61  RefPtr<CacheFileChunk> mChunk;
     62  int64_t mPos;
     63  nsresult mStatus;
     64  bool mClosed : 1;
     65  bool mInReadSegments : 1;
     66  bool mWaitingForUpdate : 1;
     67  bool const mAlternativeData : 1;
     68  int64_t mListeningForChunk;
     69 
     70  nsCOMPtr<nsIInputStreamCallback> mCallback;
     71  uint32_t mCallbackFlags;
     72  nsCOMPtr<nsIEventTarget> mCallbackTarget;
     73  // Held purely for referencing purposes
     74  RefPtr<nsISupports> mCacheEntryHandle;
     75 };
     76 
     77 }  // namespace net
     78 }  // namespace mozilla
     79 
     80 #endif