1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef NonBlockingAsyncInputStream_h
7 #define NonBlockingAsyncInputStream_h
9 #include "mozilla/Attributes.h"
10 #include "mozilla/Maybe.h"
11 #include "mozilla/Mutex.h"
13 #include "nsIAsyncInputStream.h"
14 #include "nsICloneableInputStream.h"
15 #include "nsIIPCSerializableInputStream.h"
16 #include "nsISeekableStream.h"
18 // This class aims to wrap a non-blocking and non-async inputStream and expose
19 // it as nsIAsyncInputStream.
20 // Probably you don't want to use this class directly. Instead use
21 // NS_MakeAsyncNonBlockingInputStream() as it will handle different stream
22 // variants without requiring you to special-case them yourself.
26 class NonBlockingAsyncInputStream final
: public nsIAsyncInputStream
,
27 public nsICloneableInputStream
,
28 public nsIIPCSerializableInputStream
,
29 public nsISeekableStream
{
31 NS_DECL_THREADSAFE_ISUPPORTS
32 NS_DECL_NSIINPUTSTREAM
33 NS_DECL_NSIASYNCINPUTSTREAM
34 NS_DECL_NSICLONEABLEINPUTSTREAM
35 NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
36 NS_DECL_NSISEEKABLESTREAM
37 NS_DECL_NSITELLABLESTREAM
39 // |aInputStream| must be a non-blocking, non-async inputSteam.
40 static nsresult
Create(already_AddRefed
<nsIInputStream
> aInputStream
,
41 nsIAsyncInputStream
** aAsyncInputStream
);
44 explicit NonBlockingAsyncInputStream(
45 already_AddRefed
<nsIInputStream
> aInputStream
);
46 ~NonBlockingAsyncInputStream();
49 void SerializeInternal(mozilla::ipc::InputStreamParams
& aParams
,
50 FileDescriptorArray
& aFileDescriptors
,
51 bool aDelayedStart
, uint32_t aMaxSize
,
52 uint32_t* aSizeUsed
, M
* aManager
);
54 class AsyncWaitRunnable
;
56 void RunAsyncWaitCallback(AsyncWaitRunnable
* aRunnable
,
57 already_AddRefed
<nsIInputStreamCallback
> aCallback
);
59 nsCOMPtr
<nsIInputStream
> mInputStream
;
61 // Raw pointers because these are just QI of mInputStream.
62 nsICloneableInputStream
* MOZ_NON_OWNING_REF mWeakCloneableInputStream
;
63 nsIIPCSerializableInputStream
* MOZ_NON_OWNING_REF
64 mWeakIPCSerializableInputStream
;
65 nsISeekableStream
* MOZ_NON_OWNING_REF mWeakSeekableInputStream
;
66 nsITellableStream
* MOZ_NON_OWNING_REF mWeakTellableInputStream
;
70 struct WaitClosureOnly
{
71 WaitClosureOnly(AsyncWaitRunnable
* aRunnable
, nsIEventTarget
* aEventTarget
);
73 RefPtr
<AsyncWaitRunnable
> mRunnable
;
74 nsCOMPtr
<nsIEventTarget
> mEventTarget
;
77 // This is set when AsyncWait is called with a callback and with
78 // WAIT_CLOSURE_ONLY as flag.
79 // This is protected by mLock.
80 Maybe
<WaitClosureOnly
> mWaitClosureOnly
;
82 // This is protected by mLock.
83 RefPtr
<AsyncWaitRunnable
> mAsyncWaitCallback
;
85 // This is protected by mLock.
89 } // namespace mozilla
91 #endif // NonBlockingAsyncInputStream_h