diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /dom/streams/TransformerCallbackHelpers.h | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/streams/TransformerCallbackHelpers.h')
-rw-r--r-- | dom/streams/TransformerCallbackHelpers.h | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/dom/streams/TransformerCallbackHelpers.h b/dom/streams/TransformerCallbackHelpers.h new file mode 100644 index 0000000000..8b12a31b0f --- /dev/null +++ b/dom/streams/TransformerCallbackHelpers.h @@ -0,0 +1,102 @@ +/* -*- 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 DOM_STREAMS_TRANSFORMERCALLBACKHELPERS_H_ +#define DOM_STREAMS_TRANSFORMERCALLBACKHELPERS_H_ + +#include "mozilla/HoldDropJSObjects.h" +#include "mozilla/dom/TransformerBinding.h" +#include "nsCycleCollectionParticipant.h" + +namespace mozilla::dom { + +class Promise; + +class TransformerAlgorithmsBase : public nsISupports { + public: + NS_DECL_CYCLE_COLLECTING_ISUPPORTS + NS_DECL_CYCLE_COLLECTION_CLASS(TransformerAlgorithmsBase) + + MOZ_CAN_RUN_SCRIPT virtual already_AddRefed<Promise> TransformCallback( + JSContext* aCx, JS::Handle<JS::Value> aChunk, + TransformStreamDefaultController& aController, ErrorResult& aRv) = 0; + + MOZ_CAN_RUN_SCRIPT virtual already_AddRefed<Promise> FlushCallback( + JSContext* aCx, TransformStreamDefaultController& aController, + ErrorResult& aRv) = 0; + + protected: + virtual ~TransformerAlgorithmsBase() = default; +}; + +// https://streams.spec.whatwg.org/#set-up-transform-stream-default-controller-from-transformer +class TransformerAlgorithms final : public TransformerAlgorithmsBase { + public: + NS_DECL_ISUPPORTS_INHERITED + NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED( + TransformerAlgorithms, TransformerAlgorithmsBase) + + TransformerAlgorithms(nsIGlobalObject* aGlobal, + JS::Handle<JSObject*> aTransformer, + Transformer& aTransformerDict) + : mGlobal(aGlobal), mTransformer(aTransformer) { + // Step 4. (Step 2 is implicitly done through the initialization of + // mTransformCallback to null) + if (aTransformerDict.mTransform.WasPassed()) { + mTransformCallback = aTransformerDict.mTransform.Value(); + } + + // Step 5. (Step 3 is implicitly done through the initialization of + // mTransformCallback to null) + if (aTransformerDict.mFlush.WasPassed()) { + mFlushCallback = aTransformerDict.mFlush.Value(); + } + + mozilla::HoldJSObjects(this); + }; + + MOZ_CAN_RUN_SCRIPT already_AddRefed<Promise> TransformCallback( + JSContext* aCx, JS::Handle<JS::Value> aChunk, + TransformStreamDefaultController& aController, ErrorResult& aRv) override; + + MOZ_CAN_RUN_SCRIPT already_AddRefed<Promise> FlushCallback( + JSContext* aCx, TransformStreamDefaultController& aController, + ErrorResult& aRv) override; + + protected: + ~TransformerAlgorithms() { mozilla::DropJSObjects(this); } + + private: + // Virtually const, but are cycle collected + nsCOMPtr<nsIGlobalObject> mGlobal; + JS::Heap<JSObject*> mTransformer; + MOZ_KNOWN_LIVE RefPtr<TransformerTransformCallback> mTransformCallback; + MOZ_KNOWN_LIVE RefPtr<TransformerFlushCallback> mFlushCallback; +}; + +// https://streams.spec.whatwg.org/#transformstream-set-up +class TransformerAlgorithmsWrapper : public TransformerAlgorithmsBase { + MOZ_CAN_RUN_SCRIPT already_AddRefed<Promise> TransformCallback( + JSContext*, JS::Handle<JS::Value> aChunk, + TransformStreamDefaultController& aController, ErrorResult& aRv) final; + + MOZ_CAN_RUN_SCRIPT already_AddRefed<Promise> FlushCallback( + JSContext*, TransformStreamDefaultController& aController, + ErrorResult& aRv) final; + + MOZ_CAN_RUN_SCRIPT virtual void TransformCallbackImpl( + JS::Handle<JS::Value> aChunk, + TransformStreamDefaultController& aController, ErrorResult& aRv) = 0; + + MOZ_CAN_RUN_SCRIPT virtual void FlushCallbackImpl( + TransformStreamDefaultController& aController, ErrorResult& aRv) { + // flushAlgorithm is optional, do nothing by default + } +}; + +} // namespace mozilla::dom + +#endif // DOM_STREAMS_TRANSFORMERCALLBACKHELPERS_H_ |