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/dom/FetchBinding.h"
13 #include "mozilla/dom/PromiseNativeHandler.h"
14 #include "nsIAsyncOutputStream.h"
15 #include "nsIGlobalObject.h"
22 class FetchStreamReader final
: public nsIOutputStreamCallback
,
23 public PromiseNativeHandler
{
25 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
26 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(
27 FetchStreamReader
, nsIOutputStreamCallback
)
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
);
36 void ResolvedCallback(JSContext
* aCx
, JS::Handle
<JS::Value
> aValue
) override
;
38 void RejectedCallback(JSContext
* aCx
, JS::Handle
<JS::Value
> aValue
) override
;
40 // Idempotently close the output stream and null out all state. If aCx is
41 // provided, the reader will also be canceled. aStatus must be a DOM error
42 // as understood by DOMException because it will be provided as the
43 // cancellation reason.
44 void CloseAndRelease(JSContext
* aCx
, nsresult aStatus
);
46 void StartConsuming(JSContext
* aCx
, JS::HandleObject aStream
,
47 JS::MutableHandle
<JSObject
*> aReader
, ErrorResult
& aRv
);
50 explicit FetchStreamReader(nsIGlobalObject
* aGlobal
);
53 nsresult
WriteBuffer();
55 void ReportErrorToConsole(JSContext
* aCx
, JS::Handle
<JS::Value
> aValue
);
57 nsCOMPtr
<nsIGlobalObject
> mGlobal
;
58 nsCOMPtr
<nsIEventTarget
> mOwningEventTarget
;
60 nsCOMPtr
<nsIAsyncOutputStream
> mPipeOut
;
62 RefPtr
<WeakWorkerRef
> mWorkerRef
;
64 JS::Heap
<JSObject
*> mReader
;
66 nsTArray
<uint8_t> mBuffer
;
67 uint32_t mBufferRemaining
;
68 uint32_t mBufferOffset
;
74 } // namespace mozilla
76 #endif // mozilla_dom_FetchStreamReader_h