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 CacheFileInputStream__h__
6 #define CacheFileInputStream__h__
8 #include "nsIAsyncInputStream.h"
9 #include "nsISeekableStream.h"
11 #include "CacheFileChunk.h"
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
28 explicit CacheFileInputStream(CacheFile
* aFile
, nsISupports
* aEntry
,
29 bool aAlternativeData
);
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
;
38 size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf
) const;
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;
47 virtual ~CacheFileInputStream();
49 void CloseWithStatusLocked(nsresult aStatus
);
52 void EnsureCorrectChunk(bool aReleaseOnly
);
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();
60 RefPtr
<CacheFile
> mFile
;
61 RefPtr
<CacheFileChunk
> mChunk
;
65 bool mInReadSegments
: 1;
66 bool mWaitingForUpdate
: 1;
67 bool const mAlternativeData
: 1;
68 int64_t mListeningForChunk
;
70 nsCOMPtr
<nsIInputStreamCallback
> mCallback
;
71 uint32_t mCallbackFlags
;
72 nsCOMPtr
<nsIEventTarget
> mCallbackTarget
;
73 // Held purely for referencing purposes
74 RefPtr
<nsISupports
> mCacheEntryHandle
;
78 } // namespace mozilla