1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
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_WritableStreamDefaultController_h
8 #define mozilla_dom_WritableStreamDefaultController_h
10 #include "js/TypeDecls.h"
11 #include "mozilla/AlreadyAddRefed.h"
12 #include "mozilla/Attributes.h"
13 #include "mozilla/ErrorResult.h"
14 #include "mozilla/dom/BindingDeclarations.h"
15 #include "mozilla/dom/QueuingStrategyBinding.h"
16 #include "mozilla/dom/QueueWithSizes.h"
17 #include "mozilla/dom/ReadRequest.h"
18 #include "mozilla/dom/UnderlyingSinkCallbackHelpers.h"
19 #include "nsCycleCollectionParticipant.h"
20 #include "nsWrapperCache.h"
21 #include "mozilla/dom/Nullable.h"
23 #include "nsISupports.h"
25 namespace mozilla::dom
{
29 struct UnderlyingSink
;
31 class WritableStreamDefaultController final
: public nsISupports
,
32 public nsWrapperCache
{
34 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
35 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(WritableStreamDefaultController
)
37 explicit WritableStreamDefaultController(nsISupports
* aGlobal
,
38 WritableStream
& aStream
);
41 ~WritableStreamDefaultController();
44 nsIGlobalObject
* GetParentObject() const { return mGlobal
; }
46 JSObject
* WrapObject(JSContext
* aCx
,
47 JS::Handle
<JSObject
*> aGivenProto
) override
;
49 // WebIDL methods/properties
51 AbortSignal
* Signal() { return mSignal
; }
53 MOZ_CAN_RUN_SCRIPT
void Error(JSContext
* aCx
, JS::Handle
<JS::Value
> aError
,
57 MOZ_CAN_RUN_SCRIPT
virtual already_AddRefed
<Promise
> AbortSteps(
58 JSContext
* aCx
, JS::Handle
<JS::Value
> aReason
, ErrorResult
& aRv
);
61 virtual void ErrorSteps();
63 // Internal Slot Accessors
65 QueueWithSizes
& Queue() { return mQueue
; }
67 double QueueTotalSize() const { return mQueueTotalSize
; }
68 void SetQueueTotalSize(double aQueueTotalSize
) {
69 mQueueTotalSize
= aQueueTotalSize
;
72 void SetSignal(AbortSignal
* aSignal
);
74 bool Started() const { return mStarted
; }
75 void SetStarted(bool aStarted
) { mStarted
= aStarted
; }
77 double StrategyHWM() const { return mStrategyHWM
; }
78 void SetStrategyHWM(double aStrategyHWM
) { mStrategyHWM
= aStrategyHWM
; }
80 QueuingStrategySize
* StrategySizeAlgorithm() const {
81 return mStrategySizeAlgorithm
;
83 void SetStrategySizeAlgorithm(QueuingStrategySize
* aStrategySizeAlgorithm
) {
84 mStrategySizeAlgorithm
= aStrategySizeAlgorithm
;
87 UnderlyingSinkAlgorithmsBase
* GetAlgorithms() { return mAlgorithms
; }
88 void SetAlgorithms(UnderlyingSinkAlgorithmsBase
& aAlgorithms
) {
89 mAlgorithms
= &aAlgorithms
;
92 WritableStream
* Stream() { return mStream
; }
94 // WritableStreamDefaultControllerGetBackpressure
95 // https://streams.spec.whatwg.org/#writable-stream-default-controller-get-backpressure
96 bool GetBackpressure() const {
97 // Step 1. Let desiredSize be !
98 // WritableStreamDefaultControllerGetDesiredSize(controller).
99 double desiredSize
= GetDesiredSize();
100 // Step 2. Return true if desiredSize ≤ 0, or false otherwise.
101 return desiredSize
<= 0;
104 // WritableStreamDefaultControllerGetDesiredSize
105 // https://streams.spec.whatwg.org/#writable-stream-default-controller-get-desired-size
106 double GetDesiredSize() const { return mStrategyHWM
- mQueueTotalSize
; }
108 // WritableStreamDefaultControllerClearAlgorithms
109 // https://streams.spec.whatwg.org/#writable-stream-default-controller-clear-algorithms
110 void ClearAlgorithms() {
111 // Step 1. Set controller.[[writeAlgorithm]] to undefined.
112 // Step 2. Set controller.[[closeAlgorithm]] to undefined.
113 // Step 3. Set controller.[[abortAlgorithm]] to undefined.
114 // (As written in the spec, this can happen multiple time. Try running
115 // wpt/streams/transform-streams/errors.any.js for example.)
116 if (RefPtr
<UnderlyingSinkAlgorithmsBase
> algorithms
=
117 mAlgorithms
.forget()) {
118 algorithms
->ReleaseObjects();
121 // Step 4. Set controller.[[strategySizeAlgorithm]] to undefined.
122 mStrategySizeAlgorithm
= nullptr;
126 nsCOMPtr
<nsIGlobalObject
> mGlobal
;
129 QueueWithSizes mQueue
= {};
130 double mQueueTotalSize
= 0.0;
131 RefPtr
<AbortSignal
> mSignal
;
132 bool mStarted
= false;
133 double mStrategyHWM
= 0.0;
135 RefPtr
<QueuingStrategySize
> mStrategySizeAlgorithm
;
136 RefPtr
<UnderlyingSinkAlgorithmsBase
> mAlgorithms
;
137 RefPtr
<WritableStream
> mStream
;
140 namespace streams_abstract
{
142 MOZ_CAN_RUN_SCRIPT
void SetUpWritableStreamDefaultController(
143 JSContext
* aCx
, WritableStream
* aStream
,
144 WritableStreamDefaultController
* aController
,
145 UnderlyingSinkAlgorithmsBase
* aAlgorithms
, double aHighWaterMark
,
146 QueuingStrategySize
* aSizeAlgorithm
, ErrorResult
& aRv
);
148 MOZ_CAN_RUN_SCRIPT
void SetUpWritableStreamDefaultControllerFromUnderlyingSink(
149 JSContext
* aCx
, WritableStream
* aStream
,
150 JS::Handle
<JSObject
*> aUnderlyingSink
, UnderlyingSink
& aUnderlyingSinkDict
,
151 double aHighWaterMark
, QueuingStrategySize
* aSizeAlgorithm
,
154 MOZ_CAN_RUN_SCRIPT
void WritableStreamDefaultControllerClose(
155 JSContext
* aCx
, WritableStreamDefaultController
* aController
,
158 MOZ_CAN_RUN_SCRIPT
void WritableStreamDefaultControllerWrite(
159 JSContext
* aCx
, WritableStreamDefaultController
* aController
,
160 JS::Handle
<JS::Value
> aChunk
, double chunkSize
, ErrorResult
& aRv
);
162 MOZ_CAN_RUN_SCRIPT
void WritableStreamDefaultControllerError(
163 JSContext
* aCx
, WritableStreamDefaultController
* aController
,
164 JS::Handle
<JS::Value
> aError
, ErrorResult
& aRv
);
166 MOZ_CAN_RUN_SCRIPT
void WritableStreamDefaultControllerErrorIfNeeded(
167 JSContext
* aCx
, WritableStreamDefaultController
* aController
,
168 JS::Handle
<JS::Value
> aError
, ErrorResult
& aRv
);
170 MOZ_CAN_RUN_SCRIPT
double WritableStreamDefaultControllerGetChunkSize(
171 JSContext
* aCx
, WritableStreamDefaultController
* aController
,
172 JS::Handle
<JS::Value
> aChunk
, ErrorResult
& aRv
);
174 } // namespace streams_abstract
176 } // namespace mozilla::dom
178 #endif // mozilla_dom_WritableStreamDefaultController_h