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 "nsISupportsBase.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 mAlgorithms
= nullptr;
116 // Step 4. Set controller.[[strategySizeAlgorithm]] to undefined.
117 mStrategySizeAlgorithm
= nullptr;
121 nsCOMPtr
<nsIGlobalObject
> mGlobal
;
124 QueueWithSizes mQueue
= {};
125 double mQueueTotalSize
= 0.0;
126 RefPtr
<AbortSignal
> mSignal
;
127 bool mStarted
= false;
128 double mStrategyHWM
= 0.0;
130 RefPtr
<QueuingStrategySize
> mStrategySizeAlgorithm
;
131 RefPtr
<UnderlyingSinkAlgorithmsBase
> mAlgorithms
;
132 RefPtr
<WritableStream
> mStream
;
135 MOZ_CAN_RUN_SCRIPT
void SetUpWritableStreamDefaultController(
136 JSContext
* aCx
, WritableStream
* aStream
,
137 WritableStreamDefaultController
* aController
,
138 UnderlyingSinkAlgorithmsBase
* aSinkCallbacks
, double aHighWaterMark
,
139 QueuingStrategySize
* aSizeAlgorithm
, ErrorResult
& aRv
);
141 MOZ_CAN_RUN_SCRIPT
void SetUpWritableStreamDefaultControllerFromUnderlyingSink(
142 JSContext
* aCx
, WritableStream
* aStream
,
143 JS::Handle
<JSObject
*> aUnderlyingSink
, UnderlyingSink
& aUnderlyingSinkDict
,
144 double aHighWaterMark
, QueuingStrategySize
* aSizeAlgorithm
,
147 MOZ_CAN_RUN_SCRIPT
void WritableStreamDefaultControllerClose(
148 JSContext
* aCx
, WritableStreamDefaultController
* aController
,
151 MOZ_CAN_RUN_SCRIPT
void WritableStreamDefaultControllerWrite(
152 JSContext
* aCx
, WritableStreamDefaultController
* aController
,
153 JS::Handle
<JS::Value
> aChunk
, double chunkSize
, ErrorResult
& aRv
);
155 MOZ_CAN_RUN_SCRIPT
void WritableStreamDefaultControllerError(
156 JSContext
* aCx
, WritableStreamDefaultController
* aController
,
157 JS::Handle
<JS::Value
> aError
, ErrorResult
& aRv
);
159 MOZ_CAN_RUN_SCRIPT
void WritableStreamDefaultControllerErrorIfNeeded(
160 JSContext
* aCx
, WritableStreamDefaultController
* aController
,
161 JS::Handle
<JS::Value
> aError
, ErrorResult
& aRv
);
163 MOZ_CAN_RUN_SCRIPT
double WritableStreamDefaultControllerGetChunkSize(
164 JSContext
* aCx
, WritableStreamDefaultController
* aController
,
165 JS::Handle
<JS::Value
> aChunk
, ErrorResult
& aRv
);
167 } // namespace mozilla::dom
169 #endif // mozilla_dom_WritableStreamDefaultController_h