WritableStreamDefaultController.h (6435B)
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/. */ 6 7 #ifndef mozilla_dom_WritableStreamDefaultController_h 8 #define mozilla_dom_WritableStreamDefaultController_h 9 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/Nullable.h" 16 #include "mozilla/dom/QueueWithSizes.h" 17 #include "mozilla/dom/QueuingStrategyBinding.h" 18 #include "mozilla/dom/ReadRequest.h" 19 #include "mozilla/dom/UnderlyingSinkCallbackHelpers.h" 20 #include "nsCycleCollectionParticipant.h" 21 #include "nsISupports.h" 22 #include "nsTArray.h" 23 #include "nsWrapperCache.h" 24 25 namespace mozilla::dom { 26 27 class AbortSignal; 28 class WritableStream; 29 struct UnderlyingSink; 30 31 class WritableStreamDefaultController final : public nsISupports, 32 public nsWrapperCache { 33 public: 34 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 35 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(WritableStreamDefaultController) 36 37 explicit WritableStreamDefaultController(nsISupports* aGlobal, 38 WritableStream& aStream); 39 40 protected: 41 ~WritableStreamDefaultController(); 42 43 public: 44 nsIGlobalObject* GetParentObject() const { return mGlobal; } 45 46 JSObject* WrapObject(JSContext* aCx, 47 JS::Handle<JSObject*> aGivenProto) override; 48 49 // WebIDL methods/properties 50 51 AbortSignal* Signal() { return mSignal; } 52 53 MOZ_CAN_RUN_SCRIPT void Error(JSContext* aCx, JS::Handle<JS::Value> aError, 54 ErrorResult& aRv); 55 56 // [[AbortSteps]] 57 MOZ_CAN_RUN_SCRIPT virtual already_AddRefed<Promise> AbortSteps( 58 JSContext* aCx, JS::Handle<JS::Value> aReason, ErrorResult& aRv); 59 60 // [[ErrorSteps]] 61 virtual void ErrorSteps(); 62 63 // Internal Slot Accessors 64 65 QueueWithSizes& Queue() { return mQueue; } 66 67 double QueueTotalSize() const { return mQueueTotalSize; } 68 void SetQueueTotalSize(double aQueueTotalSize) { 69 mQueueTotalSize = aQueueTotalSize; 70 } 71 72 void SetSignal(AbortSignal* aSignal); 73 74 bool Started() const { return mStarted; } 75 void SetStarted(bool aStarted) { mStarted = aStarted; } 76 77 double StrategyHWM() const { return mStrategyHWM; } 78 void SetStrategyHWM(double aStrategyHWM) { mStrategyHWM = aStrategyHWM; } 79 80 QueuingStrategySize* StrategySizeAlgorithm() const { 81 return mStrategySizeAlgorithm; 82 } 83 void SetStrategySizeAlgorithm(QueuingStrategySize* aStrategySizeAlgorithm) { 84 mStrategySizeAlgorithm = aStrategySizeAlgorithm; 85 } 86 87 UnderlyingSinkAlgorithmsBase* GetAlgorithms() { return mAlgorithms; } 88 void SetAlgorithms(UnderlyingSinkAlgorithmsBase& aAlgorithms) { 89 mAlgorithms = &aAlgorithms; 90 } 91 92 WritableStream* Stream() { return mStream; } 93 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; 102 } 103 104 // WritableStreamDefaultControllerGetDesiredSize 105 // https://streams.spec.whatwg.org/#writable-stream-default-controller-get-desired-size 106 double GetDesiredSize() const { return mStrategyHWM - mQueueTotalSize; } 107 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(); 119 } 120 121 // Step 4. Set controller.[[strategySizeAlgorithm]] to undefined. 122 mStrategySizeAlgorithm = nullptr; 123 } 124 125 private: 126 nsCOMPtr<nsIGlobalObject> mGlobal; 127 128 // Internal Slots 129 QueueWithSizes mQueue = {}; 130 double mQueueTotalSize = 0.0; 131 RefPtr<AbortSignal> mSignal; 132 bool mStarted = false; 133 double mStrategyHWM = 0.0; 134 135 RefPtr<QueuingStrategySize> mStrategySizeAlgorithm; 136 RefPtr<UnderlyingSinkAlgorithmsBase> mAlgorithms; 137 RefPtr<WritableStream> mStream; 138 }; 139 140 namespace streams_abstract { 141 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); 147 148 MOZ_CAN_RUN_SCRIPT void SetUpWritableStreamDefaultControllerFromUnderlyingSink( 149 JSContext* aCx, WritableStream* aStream, 150 JS::Handle<JSObject*> aUnderlyingSink, UnderlyingSink& aUnderlyingSinkDict, 151 double aHighWaterMark, QueuingStrategySize* aSizeAlgorithm, 152 ErrorResult& aRv); 153 154 MOZ_CAN_RUN_SCRIPT void WritableStreamDefaultControllerClose( 155 JSContext* aCx, WritableStreamDefaultController* aController, 156 ErrorResult& aRv); 157 158 MOZ_CAN_RUN_SCRIPT void WritableStreamDefaultControllerWrite( 159 JSContext* aCx, WritableStreamDefaultController* aController, 160 JS::Handle<JS::Value> aChunk, double chunkSize, ErrorResult& aRv); 161 162 MOZ_CAN_RUN_SCRIPT void WritableStreamDefaultControllerError( 163 JSContext* aCx, WritableStreamDefaultController* aController, 164 JS::Handle<JS::Value> aError, ErrorResult& aRv); 165 166 MOZ_CAN_RUN_SCRIPT void WritableStreamDefaultControllerErrorIfNeeded( 167 JSContext* aCx, WritableStreamDefaultController* aController, 168 JS::Handle<JS::Value> aError, ErrorResult& aRv); 169 170 MOZ_CAN_RUN_SCRIPT double WritableStreamDefaultControllerGetChunkSize( 171 JSContext* aCx, WritableStreamDefaultController* aController, 172 JS::Handle<JS::Value> aChunk, ErrorResult& aRv); 173 174 } // namespace streams_abstract 175 176 } // namespace mozilla::dom 177 178 #endif // mozilla_dom_WritableStreamDefaultController_h