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 mozilla_dom_FetchStreamReader_h
8 #define mozilla_dom_FetchStreamReader_h
10 #include "js/RootingAPI.h"
11 #include "js/TypeDecls.h"
12 #include "mozilla/Attributes.h"
13 #include "mozilla/dom/FetchBinding.h"
14 #include "mozilla/dom/PromiseNativeHandler.h"
15 #include "nsIAsyncOutputStream.h"
16 #include "nsIGlobalObject.h"
18 namespace mozilla::dom
{
21 class ReadableStreamDefaultReader
;
22 class StrongWorkerRef
;
24 class FetchStreamReader final
: public nsIOutputStreamCallback
{
26 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
27 NS_DECL_CYCLE_COLLECTION_CLASS(FetchStreamReader
)
28 NS_DECL_NSIOUTPUTSTREAMCALLBACK
30 // This creates a nsIInputStream able to retrieve data from the ReadableStream
31 // object. The reading starts when StartConsuming() is called.
32 static nsresult
Create(JSContext
* aCx
, nsIGlobalObject
* aGlobal
,
33 FetchStreamReader
** aStreamReader
,
34 nsIInputStream
** aInputStream
);
37 void ChunkSteps(JSContext
* aCx
, JS::Handle
<JS::Value
> aChunk
,
40 void CloseSteps(JSContext
* aCx
, ErrorResult
& aRv
);
42 void ErrorSteps(JSContext
* aCx
, JS::Handle
<JS::Value
> aError
,
45 // Idempotently close the output stream and null out all state. If aCx is
46 // provided, the reader will also be canceled. aStatus must be a DOM error
47 // as understood by DOMException because it will be provided as the
48 // cancellation reason.
50 // This is a script boundary minimize annotation changes required while
51 // we figure out how to handle some more tricky annotation cases (for
52 // example, the destructor of this class. Tracking under Bug 1750656)
53 MOZ_CAN_RUN_SCRIPT_BOUNDARY
54 void CloseAndRelease(JSContext
* aCx
, nsresult aStatus
);
56 void StartConsuming(JSContext
* aCx
, ReadableStream
* aStream
,
60 explicit FetchStreamReader(nsIGlobalObject
* aGlobal
);
63 nsresult
MaybeGrabStrongWorkerRef(JSContext
* aCx
);
65 nsresult
WriteBuffer();
67 // Attempt to copy data from mBuffer into mPipeOut. Returns `true` if data was
68 // written, and AsyncWait callbacks or FetchReadRequest calls have been set up
69 // to write more data in the future, and `false` otherwise.
71 bool Process(JSContext
* aCx
);
73 void ReportErrorToConsole(JSContext
* aCx
, JS::Handle
<JS::Value
> aValue
);
75 nsCOMPtr
<nsIGlobalObject
> mGlobal
;
76 nsCOMPtr
<nsIEventTarget
> mOwningEventTarget
;
78 nsCOMPtr
<nsIAsyncOutputStream
> mPipeOut
;
80 RefPtr
<StrongWorkerRef
> mWorkerRef
;
81 // This is an additional refcount we add to `mWorkerRef` when we have a
82 // pending callback from mPipeOut.AsyncWait() which is guaranteed to fire when
83 // either we can write to the pipe or the stream has been closed. Because
84 // this callback must run on our owning worker thread, we must ensure that the
85 // worker thread lives long enough to process the runnable (and potentially
86 // release the last reference to this non-thread-safe object on this thread).
88 // By holding an additional refcount we can avoid creating a mini state
89 // machine around mWorkerRef which hopefully improves clarity.
90 RefPtr
<StrongWorkerRef
> mAsyncWaitWorkerRef
;
92 RefPtr
<ReadableStreamDefaultReader
> mReader
;
94 nsTArray
<uint8_t> mBuffer
;
95 uint32_t mBufferRemaining
= 0;
96 uint32_t mBufferOffset
= 0;
98 bool mHasOutstandingReadRequest
= false;
99 bool mStreamClosed
= false;
102 } // namespace mozilla::dom
104 #endif // mozilla_dom_FetchStreamReader_h