/* -*- 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 TransformCallback( JSContext* aCx, JS::Handle aChunk, TransformStreamDefaultController& aController, ErrorResult& aRv) = 0; MOZ_CAN_RUN_SCRIPT virtual already_AddRefed 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 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 TransformCallback( JSContext* aCx, JS::Handle aChunk, TransformStreamDefaultController& aController, ErrorResult& aRv) override; MOZ_CAN_RUN_SCRIPT already_AddRefed FlushCallback( JSContext* aCx, TransformStreamDefaultController& aController, ErrorResult& aRv) override; protected: ~TransformerAlgorithms() { mozilla::DropJSObjects(this); } private: // Virtually const, but are cycle collected nsCOMPtr mGlobal; JS::Heap mTransformer; MOZ_KNOWN_LIVE RefPtr mTransformCallback; MOZ_KNOWN_LIVE RefPtr mFlushCallback; }; // https://streams.spec.whatwg.org/#transformstream-set-up class TransformerAlgorithmsWrapper : public TransformerAlgorithmsBase { MOZ_CAN_RUN_SCRIPT already_AddRefed TransformCallback( JSContext*, JS::Handle aChunk, TransformStreamDefaultController& aController, ErrorResult& aRv) final; MOZ_CAN_RUN_SCRIPT already_AddRefed FlushCallback( JSContext*, TransformStreamDefaultController& aController, ErrorResult& aRv) final; MOZ_CAN_RUN_SCRIPT virtual void TransformCallbackImpl( JS::Handle aChunk, TransformStreamDefaultController& aController, ErrorResult& aRv) = 0; MOZ_CAN_RUN_SCRIPT virtual void FlushCallbackImpl( TransformStreamDefaultController& aController, ErrorResult& aRv) = 0; }; } // namespace mozilla::dom #endif // DOM_STREAMS_TRANSFORMERCALLBACKHELPERS_H_