1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef SlicedInputStream_h
8 #define SlicedInputStream_h
10 #include "mozilla/Attributes.h"
11 #include "mozilla/Mutex.h"
13 #include "nsIAsyncInputStream.h"
14 #include "nsICloneableInputStream.h"
15 #include "nsIIPCSerializableInputStream.h"
16 #include "nsISeekableStream.h"
17 #include "nsIInputStreamLength.h"
21 // A wrapper for a slice of an underlying input stream.
23 class SlicedInputStream final
: public nsIAsyncInputStream
,
24 public nsICloneableInputStream
,
25 public nsIIPCSerializableInputStream
,
26 public nsISeekableStream
,
27 public nsIInputStreamCallback
,
28 public nsIInputStreamLength
,
29 public nsIAsyncInputStreamLength
,
30 public nsIInputStreamLengthCallback
{
32 NS_DECL_THREADSAFE_ISUPPORTS
33 NS_DECL_NSIINPUTSTREAM
34 NS_DECL_NSIASYNCINPUTSTREAM
35 NS_DECL_NSICLONEABLEINPUTSTREAM
36 NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
37 NS_DECL_NSISEEKABLESTREAM
38 NS_DECL_NSITELLABLESTREAM
39 NS_DECL_NSIINPUTSTREAMCALLBACK
40 NS_DECL_NSIINPUTSTREAMLENGTH
41 NS_DECL_NSIASYNCINPUTSTREAMLENGTH
42 NS_DECL_NSIINPUTSTREAMLENGTHCALLBACK
44 // Create an input stream whose data comes from a slice of aInputStream. The
45 // slice begins at aStart bytes beyond aInputStream's current position, and
46 // extends for a maximum of aLength bytes. If aInputStream contains fewer
47 // than aStart bytes, reading from SlicedInputStream returns no data. If
48 // aInputStream contains more than aStart bytes, but fewer than aStart +
49 // aLength bytes, reading from SlicedInputStream returns as many bytes as can
50 // be consumed from aInputStream after reading aLength bytes.
52 // aInputStream should not be read from after constructing a
53 // SlicedInputStream wrapper around it.
55 SlicedInputStream(already_AddRefed
<nsIInputStream
> aInputStream
,
56 uint64_t aStart
, uint64_t aLength
);
58 // This CTOR is meant to be used just for IPC.
65 void SerializeInternal(mozilla::ipc::InputStreamParams
& aParams
,
66 FileDescriptorArray
& aFileDescriptors
,
67 bool aDelayedStart
, uint32_t aMaxSize
,
68 uint32_t* aSizeUsed
, M
* aManager
);
70 void SetSourceStream(already_AddRefed
<nsIInputStream
> aInputStream
);
72 uint64_t AdjustRange(uint64_t aRange
);
74 nsCOMPtr
<nsIInputStream
> mInputStream
;
76 // Raw pointers because these are just QI of mInputStream.
77 nsICloneableInputStream
* mWeakCloneableInputStream
;
78 nsIIPCSerializableInputStream
* mWeakIPCSerializableInputStream
;
79 nsISeekableStream
* mWeakSeekableInputStream
;
80 nsITellableStream
* mWeakTellableInputStream
;
81 nsIAsyncInputStream
* mWeakAsyncInputStream
;
82 nsIInputStreamLength
* mWeakInputStreamLength
;
83 nsIAsyncInputStreamLength
* mWeakAsyncInputStreamLength
;
91 // These four are used for AsyncWait. They are protected by mutex because
92 // touched on multiple threads.
93 nsCOMPtr
<nsIInputStreamCallback
> mAsyncWaitCallback
;
94 nsCOMPtr
<nsIEventTarget
> mAsyncWaitEventTarget
;
95 uint32_t mAsyncWaitFlags
;
96 uint32_t mAsyncWaitRequestedCount
;
98 // This is use for nsIAsyncInputStreamLength::AsyncWait.
99 // This is protected by mutex.
100 nsCOMPtr
<nsIInputStreamLengthCallback
> mAsyncWaitLengthCallback
;
105 } // namespace mozilla
107 #endif // SlicedInputStream_h