Bug 1917371 - Consolidate unit_base_updater and unit_service_updater tests, r=bytesiz...
[gecko.git] / xpcom / io / SlicedInputStream.h
blob30b6c118ab54c6fe52efcee269e810f0234debd3
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"
12 #include "nsCOMPtr.h"
13 #include "nsIAsyncInputStream.h"
14 #include "nsICloneableInputStream.h"
15 #include "nsIIPCSerializableInputStream.h"
16 #include "nsISeekableStream.h"
17 #include "nsIInputStreamLength.h"
19 namespace mozilla {
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 {
31 public:
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.
59 SlicedInputStream();
61 private:
62 ~SlicedInputStream();
64 void SetSourceStream(already_AddRefed<nsIInputStream> aInputStream);
66 uint64_t AdjustRange(uint64_t aRange);
68 nsCOMPtr<nsIInputStream> mInputStream;
70 // Raw pointers because these are just QI of mInputStream.
71 nsICloneableInputStream* mWeakCloneableInputStream;
72 nsIIPCSerializableInputStream* mWeakIPCSerializableInputStream;
73 nsISeekableStream* mWeakSeekableInputStream;
74 nsITellableStream* mWeakTellableInputStream;
75 nsIAsyncInputStream* mWeakAsyncInputStream;
76 nsIInputStreamLength* mWeakInputStreamLength;
77 nsIAsyncInputStreamLength* mWeakAsyncInputStreamLength;
79 uint64_t mStart;
80 uint64_t mLength;
81 uint64_t mCurPos;
83 bool mClosed;
85 // These four are used for AsyncWait. They are protected by mutex because
86 // touched on multiple threads.
87 nsCOMPtr<nsIInputStreamCallback> mAsyncWaitCallback MOZ_GUARDED_BY(mMutex);
88 nsCOMPtr<nsIEventTarget> mAsyncWaitEventTarget MOZ_GUARDED_BY(mMutex);
89 uint32_t mAsyncWaitFlags MOZ_GUARDED_BY(mMutex);
90 uint32_t mAsyncWaitRequestedCount MOZ_GUARDED_BY(mMutex);
92 // This is use for nsIAsyncInputStreamLength::AsyncWait.
93 // This is protected by mutex.
94 nsCOMPtr<nsIInputStreamLengthCallback> mAsyncWaitLengthCallback
95 MOZ_GUARDED_BY(mMutex);
97 Mutex mMutex;
100 } // namespace mozilla
102 #endif // SlicedInputStream_h