ReadableStreamDefaultController.h (5774B)
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_ReadableStreamDefaultController_h 8 #define mozilla_dom_ReadableStreamDefaultController_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/ReadableStreamControllerBase.h" 20 #include "mozilla/dom/UnderlyingSourceCallbackHelpers.h" 21 #include "nsCycleCollectionParticipant.h" 22 #include "nsIGlobalObject.h" 23 #include "nsISupports.h" 24 #include "nsTArray.h" 25 #include "nsWrapperCache.h" 26 27 namespace mozilla::dom { 28 29 class ReadableStream; 30 class ReadableStreamDefaultReader; 31 struct UnderlyingSource; 32 class ReadableStreamGenericReader; 33 34 class ReadableStreamDefaultController final 35 : public ReadableStreamControllerBase, 36 public nsWrapperCache { 37 public: 38 NS_DECL_ISUPPORTS_INHERITED 39 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED( 40 ReadableStreamDefaultController, ReadableStreamControllerBase) 41 42 public: 43 explicit ReadableStreamDefaultController(nsIGlobalObject* aGlobal); 44 45 protected: 46 ~ReadableStreamDefaultController() override; 47 48 public: 49 bool IsDefault() override { return true; } 50 bool IsByte() override { return false; } 51 ReadableStreamDefaultController* AsDefault() override { return this; } 52 ReadableByteStreamController* AsByte() override { return nullptr; } 53 54 JSObject* WrapObject(JSContext* aCx, 55 JS::Handle<JSObject*> aGivenProto) override; 56 57 Nullable<double> GetDesiredSize(); 58 59 MOZ_CAN_RUN_SCRIPT void Close(JSContext* aCx, ErrorResult& aRv); 60 61 MOZ_CAN_RUN_SCRIPT void Enqueue(JSContext* aCx, JS::Handle<JS::Value> aChunk, 62 ErrorResult& aRv); 63 64 void Error(JSContext* aCx, JS::Handle<JS::Value> aError, ErrorResult& aRv); 65 66 MOZ_CAN_RUN_SCRIPT already_AddRefed<Promise> CancelSteps( 67 JSContext* aCx, JS::Handle<JS::Value> aReason, ErrorResult& aRv) override; 68 MOZ_CAN_RUN_SCRIPT void PullSteps(JSContext* aCx, ReadRequest* aReadRequest, 69 ErrorResult& aRv) override; 70 71 void ReleaseSteps() override; 72 73 // Internal Slot Accessors 74 bool CloseRequested() const { return mCloseRequested; } 75 void SetCloseRequested(bool aCloseRequested) { 76 mCloseRequested = aCloseRequested; 77 } 78 79 bool PullAgain() const { return mPullAgain; } 80 void SetPullAgain(bool aPullAgain) { mPullAgain = aPullAgain; } 81 82 bool Pulling() const { return mPulling; } 83 void SetPulling(bool aPulling) { mPulling = aPulling; } 84 85 QueueWithSizes& Queue() { return mQueue; } 86 87 double QueueTotalSize() const { return mQueueTotalSize; } 88 void SetQueueTotalSize(double aQueueTotalSize) { 89 mQueueTotalSize = aQueueTotalSize; 90 } 91 92 bool Started() const { return mStarted; } 93 void SetStarted(bool aStarted) { mStarted = aStarted; } 94 95 double StrategyHWM() const { return mStrategyHWM; } 96 void SetStrategyHWM(double aStrategyHWM) { mStrategyHWM = aStrategyHWM; } 97 98 QueuingStrategySize* StrategySizeAlgorithm() const { 99 return mStrategySizeAlgorithm; 100 } 101 void setStrategySizeAlgorithm(QueuingStrategySize* aStrategySizeAlgorithm) { 102 mStrategySizeAlgorithm = aStrategySizeAlgorithm; 103 } 104 105 private: 106 // Internal Slots: 107 bool mCloseRequested = false; 108 bool mPullAgain = false; 109 bool mPulling = false; 110 QueueWithSizes mQueue = {}; 111 double mQueueTotalSize = 0.0; 112 bool mStarted = false; 113 double mStrategyHWM = false; 114 RefPtr<QueuingStrategySize> mStrategySizeAlgorithm; 115 }; 116 117 namespace streams_abstract { 118 119 MOZ_CAN_RUN_SCRIPT void SetUpReadableStreamDefaultController( 120 JSContext* aCx, ReadableStream* aStream, 121 ReadableStreamDefaultController* aController, 122 UnderlyingSourceAlgorithmsBase* aAlgorithms, double aHighWaterMark, 123 QueuingStrategySize* aSizeAlgorithm, ErrorResult& aRv); 124 125 MOZ_CAN_RUN_SCRIPT void 126 SetupReadableStreamDefaultControllerFromUnderlyingSource( 127 JSContext* aCx, ReadableStream* aStream, 128 JS::Handle<JSObject*> aUnderlyingSource, 129 UnderlyingSource& aUnderlyingSourceDict, double aHighWaterMark, 130 QueuingStrategySize* aSizeAlgorithm, ErrorResult& aRv); 131 132 MOZ_CAN_RUN_SCRIPT void ReadableStreamDefaultControllerEnqueue( 133 JSContext* aCx, ReadableStreamDefaultController* aController, 134 JS::Handle<JS::Value> aChunk, ErrorResult& aRv); 135 136 MOZ_CAN_RUN_SCRIPT void ReadableStreamDefaultControllerClose( 137 JSContext* aCx, ReadableStreamDefaultController* aController, 138 ErrorResult& aRv); 139 140 MOZ_CAN_RUN_SCRIPT void ReadableStreamDefaultReaderRead( 141 JSContext* aCx, ReadableStreamGenericReader* reader, ReadRequest* aRequest, 142 ErrorResult& aRv); 143 144 void ReadableStreamDefaultControllerError( 145 JSContext* aCx, ReadableStreamDefaultController* aController, 146 JS::Handle<JS::Value> aValue, ErrorResult& aRv); 147 148 Nullable<double> ReadableStreamDefaultControllerGetDesiredSize( 149 ReadableStreamDefaultController* aController); 150 151 enum class CloseOrEnqueue { Close, Enqueue }; 152 153 bool ReadableStreamDefaultControllerCanCloseOrEnqueueAndThrow( 154 ReadableStreamDefaultController* aController, 155 CloseOrEnqueue aCloseOrEnqueue, ErrorResult& aRv); 156 157 bool ReadableStreamDefaultControllerShouldCallPull( 158 ReadableStreamDefaultController* aController); 159 160 } // namespace streams_abstract 161 162 } // namespace mozilla::dom 163 164 #endif // mozilla_dom_ReadableStreamDefaultController_h