1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/dom/TransformStreamDefaultController.h"
#include "TransformerCallbackHelpers.h"
#include "mozilla/Attributes.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/ReadableStream.h"
#include "mozilla/dom/ReadableStreamDefaultController.h"
#include "mozilla/dom/TransformStream.h"
#include "mozilla/dom/TransformStreamDefaultControllerBinding.h"
#include "nsWrapperCache.h"
namespace mozilla::dom {
using namespace streams_abstract;
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(TransformStreamDefaultController, mGlobal,
mStream, mTransformerAlgorithms)
NS_IMPL_CYCLE_COLLECTING_ADDREF(TransformStreamDefaultController)
NS_IMPL_CYCLE_COLLECTING_RELEASE(TransformStreamDefaultController)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TransformStreamDefaultController)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
TransformStream* TransformStreamDefaultController::Stream() { return mStream; }
void TransformStreamDefaultController::SetStream(TransformStream& aStream) {
MOZ_ASSERT(!mStream);
mStream = &aStream;
}
TransformerAlgorithmsBase* TransformStreamDefaultController::Algorithms() {
return mTransformerAlgorithms;
}
void TransformStreamDefaultController::SetAlgorithms(
TransformerAlgorithmsBase* aTransformerAlgorithms) {
mTransformerAlgorithms = aTransformerAlgorithms;
}
TransformStreamDefaultController::TransformStreamDefaultController(
nsIGlobalObject* aGlobal)
: mGlobal(aGlobal) {
mozilla::HoldJSObjects(this);
}
TransformStreamDefaultController::~TransformStreamDefaultController() {
mozilla::DropJSObjects(this);
}
JSObject* TransformStreamDefaultController::WrapObject(
JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
return TransformStreamDefaultController_Binding::Wrap(aCx, this, aGivenProto);
}
// https://streams.spec.whatwg.org/#ts-default-controller-desired-size
Nullable<double> TransformStreamDefaultController::GetDesiredSize() const {
// Step 1. Let readableController be
// this.[[stream]].[[readable]].[[controller]].
RefPtr<ReadableStreamDefaultController> readableController =
mStream->Readable()->Controller()->AsDefault();
// Step 2. Return !
// ReadableStreamDefaultControllerGetDesiredSize(readableController).
return ReadableStreamDefaultControllerGetDesiredSize(readableController);
}
// https://streams.spec.whatwg.org/#rs-default-controller-has-backpressure
// Looks like a readable stream thing but the spec explicitly says this is for
// TransformStream.
static bool ReadableStreamDefaultControllerHasBackpressure(
ReadableStreamDefaultController* aController) {
// Step 1: If ! ReadableStreamDefaultControllerShouldCallPull(controller) is
// true, return false.
// Step 2: Otherwise, return true.
return !ReadableStreamDefaultControllerShouldCallPull(aController);
}
void TransformStreamDefaultController::Enqueue(JSContext* aCx,
JS::Handle<JS::Value> aChunk,
ErrorResult& aRv) {
// Step 1: Perform ? TransformStreamDefaultControllerEnqueue(this, chunk).
// Inlining TransformStreamDefaultControllerEnqueue here.
// https://streams.spec.whatwg.org/#transform-stream-default-controller-enqueue
// Step 1: Let stream be controller.[[stream]].
RefPtr<TransformStream> stream = mStream;
// Step 2: Let readableController be stream.[[readable]].[[controller]].
RefPtr<ReadableStreamDefaultController> readableController =
stream->Readable()->Controller()->AsDefault();
// Step 3: If !
// ReadableStreamDefaultControllerCanCloseOrEnqueue(readableController) is
// false, throw a TypeError exception.
if (!ReadableStreamDefaultControllerCanCloseOrEnqueueAndThrow(
readableController, CloseOrEnqueue::Enqueue, aRv)) {
return;
}
// Step 4: Let enqueueResult be
// ReadableStreamDefaultControllerEnqueue(readableController, chunk).
ErrorResult rv;
ReadableStreamDefaultControllerEnqueue(aCx, readableController, aChunk, rv);
// Step 5: If enqueueResult is an abrupt completion,
if (rv.MaybeSetPendingException(aCx)) {
JS::Rooted<JS::Value> error(aCx);
if (!JS_GetPendingException(aCx, &error)) {
// Uncatchable exception; we should mark aRv and return.
aRv.StealExceptionFromJSContext(aCx);
return;
}
JS_ClearPendingException(aCx);
// Step 5.1: Perform ! TransformStreamErrorWritableAndUnblockWrite(stream,
// enqueueResult.[[Value]]).
TransformStreamErrorWritableAndUnblockWrite(aCx, stream, error, aRv);
// Step 5.2: Throw stream.[[readable]].[[storedError]].
JS::Rooted<JS::Value> storedError(aCx, stream->Readable()->StoredError());
aRv.MightThrowJSException();
aRv.ThrowJSException(aCx, storedError);
return;
}
// Step 6: Let backpressure be !
// ReadableStreamDefaultControllerHasBackpressure(readableController).
bool backpressure =
ReadableStreamDefaultControllerHasBackpressure(readableController);
// Step 7: If backpressure is not stream.[[backpressure]],
if (backpressure != stream->Backpressure()) {
// Step 7.1: Assert: backpressure is true.
MOZ_ASSERT(backpressure);
// Step 7.2: Perform ! TransformStreamSetBackpressure(true).
stream->SetBackpressure(true);
}
}
// https://streams.spec.whatwg.org/#ts-default-controller-error
void TransformStreamDefaultController::Error(JSContext* aCx,
JS::Handle<JS::Value> aError,
ErrorResult& aRv) {
// Step 1: Perform ? TransformStreamDefaultControllerError(this, e).
// Inlining TransformStreamDefaultControllerError here.
// https://streams.spec.whatwg.org/#transform-stream-default-controller-error
// Perform ! TransformStreamError(controller.[[stream]], e).
// mStream is set in initialization step and only modified in cycle
// collection.
// TODO: Move mStream initialization to a method/constructor and make it
// MOZ_KNOWN_LIVE again. (See bug 1769854)
TransformStreamError(aCx, MOZ_KnownLive(mStream), aError, aRv);
}
// https://streams.spec.whatwg.org/#ts-default-controller-terminate
void TransformStreamDefaultController::Terminate(JSContext* aCx,
ErrorResult& aRv) {
// Step 1: Perform ? TransformStreamDefaultControllerTerminate(this).
// Inlining TransformStreamDefaultControllerTerminate here.
// https://streams.spec.whatwg.org/#transform-stream-default-controller-terminate
// Step 1: Let stream be controller.[[stream]].
RefPtr<TransformStream> stream = mStream;
// Step 2: Let readableController be stream.[[readable]].[[controller]].
RefPtr<ReadableStreamDefaultController> readableController =
stream->Readable()->Controller()->AsDefault();
// Step 3: Perform ! ReadableStreamDefaultControllerClose(readableController).
ReadableStreamDefaultControllerClose(aCx, readableController, aRv);
// Step 4: Let error be a TypeError exception indicating that the stream has
// been terminated.
ErrorResult rv;
rv.ThrowTypeError("Terminating the stream");
JS::Rooted<JS::Value> error(aCx);
MOZ_ALWAYS_TRUE(ToJSValue(aCx, std::move(rv), &error));
// Step 5: Perform ! TransformStreamErrorWritableAndUnblockWrite(stream,
// error).
TransformStreamErrorWritableAndUnblockWrite(aCx, stream, error, aRv);
}
namespace streams_abstract {
// https://streams.spec.whatwg.org/#set-up-transform-stream-default-controller
void SetUpTransformStreamDefaultController(
JSContext* aCx, TransformStream& aStream,
TransformStreamDefaultController& aController,
TransformerAlgorithmsBase& aTransformerAlgorithms) {
// Step 1. Assert: stream implements TransformStream.
// Step 2. Assert: stream.[[controller]] is undefined.
MOZ_ASSERT(!aStream.Controller());
// Step 3. Set controller.[[stream]] to stream.
aController.SetStream(aStream);
// Step 4. Set stream.[[controller]] to controller.
aStream.SetController(aController);
// Step 5. Set controller.[[transformAlgorithm]] to transformAlgorithm.
// Step 6. Set controller.[[flushAlgorithm]] to flushAlgorithm.
aController.SetAlgorithms(&aTransformerAlgorithms);
}
// https://streams.spec.whatwg.org/#set-up-transform-stream-default-controller-from-transformer
void SetUpTransformStreamDefaultControllerFromTransformer(
JSContext* aCx, TransformStream& aStream,
JS::Handle<JSObject*> aTransformer, Transformer& aTransformerDict) {
// Step 1. Let controller be a new TransformStreamDefaultController.
auto controller =
MakeRefPtr<TransformStreamDefaultController>(aStream.GetParentObject());
// Step 2 - 5:
auto algorithms = MakeRefPtr<TransformerAlgorithms>(
aStream.GetParentObject(), aTransformer, aTransformerDict);
// Step 6: Perform ! SetUpTransformStreamDefaultController(stream, controller,
// transformAlgorithm, flushAlgorithm).
SetUpTransformStreamDefaultController(aCx, aStream, *controller, *algorithms);
}
} // namespace streams_abstract
} // namespace mozilla::dom
|