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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
/* -*- 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/. */
#ifndef mozilla_dom_WritableStream_h
#define mozilla_dom_WritableStream_h
#include "js/TypeDecls.h"
#include "js/Value.h"
#include "mozilla/Attributes.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/QueuingStrategyBinding.h"
#include "mozilla/dom/WritableStreamDefaultController.h"
#include "mozilla/dom/WritableStreamDefaultWriter.h"
#include "nsCycleCollectionParticipant.h"
#include "nsWrapperCache.h"
namespace mozilla::dom {
class Promise;
class WritableStreamDefaultController;
class WritableStreamDefaultWriter;
class UnderlyingSinkAlgorithmsBase;
class UniqueMessagePortId;
class MessagePort;
class WritableStream : public nsISupports, public nsWrapperCache {
public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(WritableStream)
friend class ReadableStream;
protected:
virtual ~WritableStream();
virtual void LastRelease() {}
// If one extends WritableStream with another cycle collectable class,
// calling HoldJSObjects and DropJSObjects should happen using 'this' of
// that extending class. And in that case Explicit should be passed to the
// constructor of WriteableStream so that it doesn't make those calls.
// See also https://bugzilla.mozilla.org/show_bug.cgi?id=1801214.
enum class HoldDropJSObjectsCaller { Implicit, Explicit };
explicit WritableStream(const GlobalObject& aGlobal,
HoldDropJSObjectsCaller aHoldDropCaller);
explicit WritableStream(nsIGlobalObject* aGlobal,
HoldDropJSObjectsCaller aHoldDropCaller);
public:
// Slot Getter/Setters:
bool Backpressure() const { return mBackpressure; }
void SetBackpressure(bool aBackpressure) { mBackpressure = aBackpressure; }
Promise* GetCloseRequest() { return mCloseRequest; }
void SetCloseRequest(Promise* aRequest) { mCloseRequest = aRequest; }
MOZ_KNOWN_LIVE WritableStreamDefaultController* Controller() {
return mController;
}
void SetController(WritableStreamDefaultController& aController) {
MOZ_ASSERT(!mController);
mController = &aController;
}
Promise* GetInFlightWriteRequest() const { return mInFlightWriteRequest; }
Promise* GetPendingAbortRequestPromise() const {
return mPendingAbortRequestPromise;
}
void SetPendingAbortRequest(Promise* aPromise, JS::Handle<JS::Value> aReason,
bool aWasAlreadyErroring) {
mPendingAbortRequestPromise = aPromise;
mPendingAbortRequestReason = aReason;
mPendingAbortRequestWasAlreadyErroring = aWasAlreadyErroring;
}
WritableStreamDefaultWriter* GetWriter() const { return mWriter; }
void SetWriter(WritableStreamDefaultWriter* aWriter) { mWriter = aWriter; }
enum class WriterState { Writable, Closed, Erroring, Errored };
WriterState State() const { return mState; }
void SetState(const WriterState& aState) { mState = aState; }
JS::Value StoredError() const { return mStoredError; }
void SetStoredError(JS::Handle<JS::Value> aStoredError) {
mStoredError = aStoredError;
}
void AppendWriteRequest(RefPtr<Promise>& aRequest) {
mWriteRequests.AppendElement(aRequest);
}
// CreateWritableStream
MOZ_CAN_RUN_SCRIPT static already_AddRefed<WritableStream> CreateAbstract(
JSContext* aCx, nsIGlobalObject* aGlobal,
UnderlyingSinkAlgorithmsBase* aAlgorithms, double aHighWaterMark,
QueuingStrategySize* aSizeAlgorithm, ErrorResult& aRv);
// WritableStreamCloseQueuedOrInFlight
bool CloseQueuedOrInFlight() const {
return mCloseRequest || mInFlightCloseRequest;
}
// WritableStreamDealWithRejection
MOZ_CAN_RUN_SCRIPT void DealWithRejection(JSContext* aCx,
JS::Handle<JS::Value> aError,
ErrorResult& aRv);
// WritableStreamFinishErroring
MOZ_CAN_RUN_SCRIPT void FinishErroring(JSContext* aCx, ErrorResult& aRv);
// WritableStreamFinishInFlightClose
void FinishInFlightClose();
// WritableStreamFinishInFlightCloseWithError
MOZ_CAN_RUN_SCRIPT void FinishInFlightCloseWithError(
JSContext* aCx, JS::Handle<JS::Value> aError, ErrorResult& aRv);
// WritableStreamFinishInFlightWrite
void FinishInFlightWrite();
// WritableStreamFinishInFlightWriteWithError
MOZ_CAN_RUN_SCRIPT void FinishInFlightWriteWithError(
JSContext* aCX, JS::Handle<JS::Value> aError, ErrorResult& aR);
// WritableStreamHasOperationMarkedInFlight
bool HasOperationMarkedInFlight() const {
return mInFlightWriteRequest || mInFlightCloseRequest;
}
// WritableStreamMarkCloseRequestInFlight
void MarkCloseRequestInFlight();
// WritableStreamMarkFirstWriteRequestInFlight
void MarkFirstWriteRequestInFlight();
// WritableStreamRejectCloseAndClosedPromiseIfNeeded
void RejectCloseAndClosedPromiseIfNeeded();
// WritableStreamStartErroring
MOZ_CAN_RUN_SCRIPT void StartErroring(JSContext* aCx,
JS::Handle<JS::Value> aReason,
ErrorResult& aRv);
// WritableStreamUpdateBackpressure
void UpdateBackpressure(bool aBackpressure);
// [Transferable]
// https://html.spec.whatwg.org/multipage/structured-data.html#transfer-steps
MOZ_CAN_RUN_SCRIPT bool Transfer(JSContext* aCx,
UniqueMessagePortId& aPortId);
// https://html.spec.whatwg.org/multipage/structured-data.html#transfer-receiving-steps
MOZ_CAN_RUN_SCRIPT static already_AddRefed<WritableStream>
ReceiveTransferImpl(JSContext* aCx, nsIGlobalObject* aGlobal,
MessagePort& aPort);
MOZ_CAN_RUN_SCRIPT static bool ReceiveTransfer(
JSContext* aCx, nsIGlobalObject* aGlobal, MessagePort& aPort,
JS::MutableHandle<JSObject*> aReturnObject);
// Public functions to implement other specs
// https://streams.spec.whatwg.org/#other-specs-ws
// https://streams.spec.whatwg.org/#writablestream-set-up
protected:
// Sets up the WritableStream. Intended for subclasses.
void SetUpNative(JSContext* aCx, UnderlyingSinkAlgorithmsWrapper& aAlgorithms,
Maybe<double> aHighWaterMark,
QueuingStrategySize* aSizeAlgorithm, ErrorResult& aRv);
public:
// Creates and sets up a WritableStream. Use SetUpNative for this purpose in
// subclasses.
static already_AddRefed<WritableStream> CreateNative(
JSContext* aCx, nsIGlobalObject& aGlobal,
UnderlyingSinkAlgorithmsWrapper& aAlgorithms,
Maybe<double> aHighWaterMark, QueuingStrategySize* aSizeAlgorithm,
ErrorResult& aRv);
// The following definitions must only be used on WritableStream instances
// initialized via the above set up algorithm:
// https://streams.spec.whatwg.org/#writablestream-error
MOZ_CAN_RUN_SCRIPT void ErrorNative(JSContext* aCx,
JS::Handle<JS::Value> aError,
ErrorResult& aRv);
// IDL layer functions
nsIGlobalObject* GetParentObject() const { return mGlobal; }
JSObject* WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) override;
// IDL methods
// TODO: Use MOZ_CAN_RUN_SCRIPT when IDL constructors can use it (bug 1749042)
MOZ_CAN_RUN_SCRIPT_BOUNDARY static already_AddRefed<WritableStream>
Constructor(const GlobalObject& aGlobal,
const Optional<JS::Handle<JSObject*>>& aUnderlyingSink,
const QueuingStrategy& aStrategy, ErrorResult& aRv);
bool Locked() const { return !!mWriter; }
MOZ_CAN_RUN_SCRIPT already_AddRefed<Promise> Abort(
JSContext* cx, JS::Handle<JS::Value> aReason, ErrorResult& aRv);
MOZ_CAN_RUN_SCRIPT already_AddRefed<Promise> Close(JSContext* aCx,
ErrorResult& aRv);
already_AddRefed<WritableStreamDefaultWriter> GetWriter(ErrorResult& aRv);
protected:
nsCOMPtr<nsIGlobalObject> mGlobal;
// Internal Slots:
private:
bool mBackpressure = false;
RefPtr<Promise> mCloseRequest;
RefPtr<WritableStreamDefaultController> mController;
RefPtr<Promise> mInFlightWriteRequest;
RefPtr<Promise> mInFlightCloseRequest;
// We inline all members of [[pendingAbortRequest]] in this class.
// The absence (i.e. undefined) of the [[pendingAbortRequest]]
// is indicated by mPendingAbortRequestPromise = nullptr.
RefPtr<Promise> mPendingAbortRequestPromise;
JS::Heap<JS::Value> mPendingAbortRequestReason;
bool mPendingAbortRequestWasAlreadyErroring = false;
WriterState mState = WriterState::Writable;
JS::Heap<JS::Value> mStoredError;
RefPtr<WritableStreamDefaultWriter> mWriter;
nsTArray<RefPtr<Promise>> mWriteRequests;
HoldDropJSObjectsCaller mHoldDropCaller;
};
namespace streams_abstract {
inline bool IsWritableStreamLocked(WritableStream* aStream) {
return aStream->Locked();
}
MOZ_CAN_RUN_SCRIPT already_AddRefed<Promise> WritableStreamAbort(
JSContext* aCx, WritableStream* aStream, JS::Handle<JS::Value> aReason,
ErrorResult& aRv);
MOZ_CAN_RUN_SCRIPT already_AddRefed<Promise> WritableStreamClose(
JSContext* aCx, WritableStream* aStream, ErrorResult& aRv);
already_AddRefed<Promise> WritableStreamAddWriteRequest(
WritableStream* aStream);
already_AddRefed<WritableStreamDefaultWriter>
AcquireWritableStreamDefaultWriter(WritableStream* aStream, ErrorResult& aRv);
} // namespace streams_abstract
} // namespace mozilla::dom
#endif // mozilla_dom_WritableStream_h
|