commit 8b9b8eeb9849265df1e44309e39e010fef2147be
parent 5c29808a45323627e19ec37b73d890f8ed7b216a
Author: Kagami Sascha Rosylight <krosylight@proton.me>
Date: Mon, 20 Oct 2025 23:41:16 +0000
Bug 1994230 - Part 2: Split DecompressionStreamAlgorithms to a separate file r=smaug
Ultimately we should also put CompressionStreamAlgorithms to this file too, but for now we have only zlib compressions and thus did not do the same refactoring that we did in DecompressionStream for zstd.
That work can be done separately as I intend to make this patch stack reorganization-only for best readability.
Differential Revision: https://phabricator.services.mozilla.com/D268587
Diffstat:
5 files changed, 137 insertions(+), 93 deletions(-)
diff --git a/dom/compression/BaseAlgorithms.cpp b/dom/compression/BaseAlgorithms.cpp
@@ -0,0 +1,81 @@
+/* -*- 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/. */
+
+#include "BaseAlgorithms.h"
+
+#include "mozilla/dom/BufferSourceBinding.h"
+#include "mozilla/dom/BufferSourceBindingFwd.h"
+#include "mozilla/dom/TransformStreamDefaultController.h"
+#include "mozilla/dom/UnionTypes.h"
+
+namespace mozilla::dom::compression {
+
+// Step 3 of
+// https://wicg.github.io/compression/#dom-decompressionstream-decompressionstream
+// Let transformAlgorithm be an algorithm which takes a chunk argument and
+// runs the compress and enqueue a chunk algorithm with this and chunk.
+MOZ_CAN_RUN_SCRIPT
+void DecompressionStreamAlgorithms::TransformCallbackImpl(
+ JS::Handle<JS::Value> aChunk, TransformStreamDefaultController& aController,
+ ErrorResult& aRv) {
+ AutoJSAPI jsapi;
+ if (!jsapi.Init(aController.GetParentObject())) {
+ aRv.ThrowUnknownError("Internal error");
+ return;
+ }
+ JSContext* cx = jsapi.cx();
+
+ // https://compression.spec.whatwg.org/#decompress-and-enqueue-a-chunk
+
+ // Step 1: If chunk is not a BufferSource type, then throw a TypeError.
+ RootedUnion<OwningBufferSource> bufferSource(cx);
+ if (!bufferSource.Init(cx, aChunk)) {
+ aRv.MightThrowJSException();
+ aRv.StealExceptionFromJSContext(cx);
+ return;
+ }
+
+ // Step 2: Let buffer be the result of decompressing chunk with ds's format
+ // and context. If this results in an error, then throw a TypeError.
+ // Step 3 - 5: (Done in DecompressAndEnqueue)
+ ProcessTypedArraysFixed(
+ bufferSource,
+ [&](const Span<uint8_t>& aData) MOZ_CAN_RUN_SCRIPT_BOUNDARY {
+ DecompressAndEnqueue(cx, aData, Flush::No, aController, aRv);
+ });
+}
+
+// Step 4 of
+// https://compression.spec.whatwg.org/#dom-decompressionstream-decompressionstream
+// Let flushAlgorithm be an algorithm which takes no argument and runs the
+// compress flush and enqueue algorithm with this.
+MOZ_CAN_RUN_SCRIPT void DecompressionStreamAlgorithms::FlushCallbackImpl(
+ TransformStreamDefaultController& aController, ErrorResult& aRv) {
+ AutoJSAPI jsapi;
+ if (!jsapi.Init(aController.GetParentObject())) {
+ aRv.ThrowUnknownError("Internal error");
+ return;
+ }
+ JSContext* cx = jsapi.cx();
+
+ // https://wicg.github.io/compression/#decompress-flush-and-enqueue
+
+ // Step 1: Let buffer be the result of decompressing an empty input with
+ // ds's format and context, with the finish flag.
+ // Step 2 - 4: (Done in DecompressAndEnqueue)
+ DecompressAndEnqueue(cx, Span<const uint8_t>(), Flush::Yes, aController, aRv);
+}
+
+NS_IMPL_CYCLE_COLLECTION_INHERITED(DecompressionStreamAlgorithms,
+ TransformerAlgorithmsBase)
+NS_IMPL_ADDREF_INHERITED(DecompressionStreamAlgorithms,
+ TransformerAlgorithmsBase)
+NS_IMPL_RELEASE_INHERITED(DecompressionStreamAlgorithms,
+ TransformerAlgorithmsBase)
+NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DecompressionStreamAlgorithms)
+NS_INTERFACE_MAP_END_INHERITING(TransformerAlgorithmsBase)
+
+} // namespace mozilla::dom::compression
diff --git a/dom/compression/BaseAlgorithms.h b/dom/compression/BaseAlgorithms.h
@@ -0,0 +1,54 @@
+/* -*- 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_COMPRESSION_BASEALGORITHMS_H_
+#define DOM_COMPRESSION_BASEALGORITHMS_H_
+
+#include "mozilla/dom/TransformerCallbackHelpers.h"
+
+namespace mozilla::dom::compression {
+
+// A top-level, library-agnostic flush enum that should be converted
+// into the native flush values for a given (de)compression library
+// with a function defined below.
+enum class Flush : bool { No, Yes };
+
+class DecompressionStreamAlgorithms : public TransformerAlgorithmsWrapper {
+ public:
+ NS_DECL_ISUPPORTS_INHERITED
+ NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(DecompressionStreamAlgorithms,
+ TransformerAlgorithmsBase)
+
+ // Step 3 of
+ // https://wicg.github.io/compression/#dom-decompressionstream-decompressionstream
+ // Let transformAlgorithm be an algorithm which takes a chunk argument and
+ // runs the compress and enqueue a chunk algorithm with this and chunk.
+ MOZ_CAN_RUN_SCRIPT
+ void TransformCallbackImpl(JS::Handle<JS::Value> aChunk,
+ TransformStreamDefaultController& aController,
+ ErrorResult& aRv) override;
+
+ // Step 4 of
+ // https://compression.spec.whatwg.org/#dom-decompressionstream-decompressionstream
+ // Let flushAlgorithm be an algorithm which takes no argument and runs the
+ // compress flush and enqueue algorithm with this.
+ MOZ_CAN_RUN_SCRIPT void FlushCallbackImpl(
+ TransformStreamDefaultController& aController, ErrorResult& aRv) override;
+
+ protected:
+ static const uint16_t kBufferSize = 16384;
+
+ ~DecompressionStreamAlgorithms() = default;
+
+ MOZ_CAN_RUN_SCRIPT
+ virtual void DecompressAndEnqueue(
+ JSContext* aCx, Span<const uint8_t> aInput, Flush aFlush,
+ TransformStreamDefaultController& aController, ErrorResult& aRv) = 0;
+};
+
+} // namespace mozilla::dom::compression
+
+#endif // DOM_COMPRESSION_BASEALGORITHMS_H_
diff --git a/dom/compression/CompressionStreamHelper.h b/dom/compression/CompressionStreamHelper.h
@@ -7,17 +7,12 @@
#ifndef DOM_COMPRESSION_STREAM_HELPER_H_
#define DOM_COMPRESSION_STREAM_HELPER_H_
-#include "js/TypeDecls.h"
+#include "BaseAlgorithms.h"
#include "mozilla/dom/CompressionStreamBinding.h"
#include "zlib.h"
namespace mozilla::dom::compression {
-// A top-level, library-agnostic flush enum that should be converted
-// into the native flush values for a given (de)compression library
-// with a function defined below.
-enum class Flush : bool { No, Yes };
-
inline uint8_t intoZLibFlush(Flush aFlush) {
switch (aFlush) {
case Flush::No: {
diff --git a/dom/compression/DecompressionStream.cpp b/dom/compression/DecompressionStream.cpp
@@ -10,13 +10,10 @@
#include "js/TypeDecls.h"
#include "mozilla/Assertions.h"
#include "mozilla/StaticPrefs_dom.h"
-#include "mozilla/dom/BufferSourceBinding.h"
-#include "mozilla/dom/BufferSourceBindingFwd.h"
#include "mozilla/dom/DecompressionStreamBinding.h"
#include "mozilla/dom/ReadableStream.h"
#include "mozilla/dom/TextDecoderStream.h"
#include "mozilla/dom/TransformStream.h"
-#include "mozilla/dom/TransformerCallbackHelpers.h"
#include "mozilla/dom/UnionTypes.h"
#include "mozilla/dom/WritableStream.h"
#include "zstd/zstd.h"
@@ -24,90 +21,6 @@
namespace mozilla::dom {
using namespace compression;
-class DecompressionStreamAlgorithms : public TransformerAlgorithmsWrapper {
- public:
- NS_DECL_ISUPPORTS_INHERITED
- NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(DecompressionStreamAlgorithms,
- TransformerAlgorithmsBase)
-
- // Step 3 of
- // https://wicg.github.io/compression/#dom-decompressionstream-decompressionstream
- // Let transformAlgorithm be an algorithm which takes a chunk argument and
- // runs the compress and enqueue a chunk algorithm with this and chunk.
- MOZ_CAN_RUN_SCRIPT
- void TransformCallbackImpl(JS::Handle<JS::Value> aChunk,
- TransformStreamDefaultController& aController,
- ErrorResult& aRv) override {
- AutoJSAPI jsapi;
- if (!jsapi.Init(aController.GetParentObject())) {
- aRv.ThrowUnknownError("Internal error");
- return;
- }
- JSContext* cx = jsapi.cx();
-
- // https://compression.spec.whatwg.org/#decompress-and-enqueue-a-chunk
-
- // Step 1: If chunk is not a BufferSource type, then throw a TypeError.
- RootedUnion<OwningBufferSource> bufferSource(cx);
- if (!bufferSource.Init(cx, aChunk)) {
- aRv.MightThrowJSException();
- aRv.StealExceptionFromJSContext(cx);
- return;
- }
-
- // Step 2: Let buffer be the result of decompressing chunk with ds's format
- // and context. If this results in an error, then throw a TypeError.
- // Step 3 - 5: (Done in DecompressAndEnqueue)
- ProcessTypedArraysFixed(
- bufferSource,
- [&](const Span<uint8_t>& aData) MOZ_CAN_RUN_SCRIPT_BOUNDARY {
- DecompressAndEnqueue(cx, aData, Flush::No, aController, aRv);
- });
- }
-
- // Step 4 of
- // https://compression.spec.whatwg.org/#dom-decompressionstream-decompressionstream
- // Let flushAlgorithm be an algorithm which takes no argument and runs the
- // compress flush and enqueue algorithm with this.
- MOZ_CAN_RUN_SCRIPT void FlushCallbackImpl(
- TransformStreamDefaultController& aController,
- ErrorResult& aRv) override {
- AutoJSAPI jsapi;
- if (!jsapi.Init(aController.GetParentObject())) {
- aRv.ThrowUnknownError("Internal error");
- return;
- }
- JSContext* cx = jsapi.cx();
-
- // https://wicg.github.io/compression/#decompress-flush-and-enqueue
-
- // Step 1: Let buffer be the result of decompressing an empty input with
- // ds's format and context, with the finish flag.
- // Step 2 - 4: (Done in DecompressAndEnqueue)
- DecompressAndEnqueue(cx, Span<const uint8_t>(), Flush::Yes, aController,
- aRv);
- }
-
- protected:
- static const uint16_t kBufferSize = 16384;
-
- ~DecompressionStreamAlgorithms() = default;
-
- MOZ_CAN_RUN_SCRIPT
- virtual void DecompressAndEnqueue(
- JSContext* aCx, Span<const uint8_t> aInput, Flush,
- TransformStreamDefaultController& aController, ErrorResult& aRv) = 0;
-};
-
-NS_IMPL_CYCLE_COLLECTION_INHERITED(DecompressionStreamAlgorithms,
- TransformerAlgorithmsBase)
-NS_IMPL_ADDREF_INHERITED(DecompressionStreamAlgorithms,
- TransformerAlgorithmsBase)
-NS_IMPL_RELEASE_INHERITED(DecompressionStreamAlgorithms,
- TransformerAlgorithmsBase)
-NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DecompressionStreamAlgorithms)
-NS_INTERFACE_MAP_END_INHERITING(TransformerAlgorithmsBase)
-
// See the zlib manual in https://www.zlib.net/manual.html or in
// https://searchfox.org/mozilla-central/source/modules/zlib/src/zlib.h
class ZLibDecompressionStreamAlgorithms : public DecompressionStreamAlgorithms {
diff --git a/dom/compression/moz.build b/dom/compression/moz.build
@@ -13,6 +13,7 @@ EXPORTS.mozilla.dom += [
]
UNIFIED_SOURCES += [
+ "BaseAlgorithms.cpp",
"CompressionStream.cpp",
"DecompressionStream.cpp",
]