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 "nsAutoPtr.h"
12 #include "CacheFileChunk.h"
20 class CacheFileInputStream
: public nsIAsyncInputStream
21 , public nsISeekableStream
22 , public CacheFileChunkListener
24 NS_DECL_THREADSAFE_ISUPPORTS
25 NS_DECL_NSIINPUTSTREAM
26 NS_DECL_NSIASYNCINPUTSTREAM
27 NS_DECL_NSISEEKABLESTREAM
30 explicit CacheFileInputStream(CacheFile
*aFile
);
32 NS_IMETHOD
OnChunkRead(nsresult aResult
, CacheFileChunk
*aChunk
);
33 NS_IMETHOD
OnChunkWritten(nsresult aResult
, CacheFileChunk
*aChunk
);
34 NS_IMETHOD
OnChunkAvailable(nsresult aResult
, uint32_t aChunkIdx
,
35 CacheFileChunk
*aChunk
);
36 NS_IMETHOD
OnChunkUpdated(CacheFileChunk
*aChunk
);
39 size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf
) const;
41 uint32_t GetPosition() const { return mPos
; };
44 virtual ~CacheFileInputStream();
46 nsresult
CloseWithStatusLocked(nsresult aStatus
);
48 void EnsureCorrectChunk(bool aReleaseOnly
);
50 // CanRead returns negative value when output stream truncates the data before
51 // the input stream's mPos.
52 void CanRead(int64_t *aCanRead
, const char **aBuf
);
53 void NotifyListener();
54 void MaybeNotifyListener();
56 nsRefPtr
<CacheFile
> mFile
;
57 nsRefPtr
<CacheFileChunk
> mChunk
;
61 bool mWaitingForUpdate
;
62 int64_t mListeningForChunk
;
64 nsCOMPtr
<nsIInputStreamCallback
> mCallback
;
65 uint32_t mCallbackFlags
;
66 nsCOMPtr
<nsIEventTarget
> mCallbackTarget
;