CacheFileInputStream.h 2.5 KB

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