Bug 1748835 [wpt PR 32273] - Avoid reftest-wait timeout if ::target-text is unsupport...
[gecko.git] / xpcom / io / NonBlockingAsyncInputStream.h
blobb026e39331b55f055bb5680c0990614d77cf91e4
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"
12 #include "nsCOMPtr.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.
24 namespace mozilla {
26 class NonBlockingAsyncInputStream final : public nsIAsyncInputStream,
27 public nsICloneableInputStream,
28 public nsIIPCSerializableInputStream,
29 public nsISeekableStream {
30 public:
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);
43 private:
44 explicit NonBlockingAsyncInputStream(
45 already_AddRefed<nsIInputStream> aInputStream);
46 ~NonBlockingAsyncInputStream();
48 template <typename M>
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;
68 Mutex mLock;
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.
86 bool mClosed;
89 } // namespace mozilla
91 #endif // NonBlockingAsyncInputStream_h