1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
/* -*- 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://compression.spec.whatwg.org/#dom-compressionstream-compressionstream
// 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 CompressionStreamAlgorithms::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/#compress-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 - 5: (Done in CompressAndEnqueue)
ProcessTypedArraysFixed(
bufferSource,
[&](const Span<uint8_t>& aData) MOZ_CAN_RUN_SCRIPT_BOUNDARY {
CompressAndEnqueue(cx, aData, Flush::No, aController, aRv);
});
}
// Step 4 of
// https://compression.spec.whatwg.org/#dom-compressionstream-compressionstream
// Let flushAlgorithm be an algorithm which takes no argument and runs the
// compress flush and enqueue algorithm with this.
MOZ_CAN_RUN_SCRIPT void CompressionStreamAlgorithms::FlushCallbackImpl(
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/#compress-flush-and-enqueue
// Step 1 - 4: (Done in CompressAndEnqueue)
CompressAndEnqueue(cx, Span<const uint8_t>(), Flush::Yes, aController, aRv);
}
// Shared by:
// https://compression.spec.whatwg.org/#compress-and-enqueue-a-chunk
// https://compression.spec.whatwg.org/#compress-flush-and-enqueue
MOZ_CAN_RUN_SCRIPT void CompressionStreamAlgorithms::CompressAndEnqueue(
JSContext* aCx, Span<const uint8_t> aInput, Flush aFlush,
TransformStreamDefaultController& aController, ErrorResult& aRv) {
MOZ_ASSERT_IF(aFlush == Flush::Yes, !aInput.Length());
JS::RootedVector<JSObject*> array(aCx);
// Step 2: Let buffer be the result of compressing chunk with cs’s
// format and context.
// Step 3: If buffer is empty, return. (implicit as array will be empty then)
// Step 4: Let arrays be the result of buffer into one or more non-empty
// pieces and converting them into Uint8Arrays.
Compress(aCx, aInput, &array, aFlush, aRv);
if (aRv.Failed()) {
return;
}
// Step 5: For each Uint8Array array, enqueue array in cs's transform.
for (const auto& view : array) {
JS::Rooted<JS::Value> value(aCx, JS::ObjectValue(*view));
aController.Enqueue(aCx, value, aRv);
if (aRv.Failed()) {
return;
}
}
}
// Step 3 of
// https://compression.spec.whatwg.org/#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://compression.spec.whatwg.org/#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 - 6: (Done in DecompressAndEnqueue)
DecompressAndEnqueue(cx, Span<const uint8_t>(), Flush::Yes, aController, aRv);
}
// Shared by:
// https://compression.spec.whatwg.org/#decompress-and-enqueue-a-chunk
// https://compression.spec.whatwg.org/#decompress-flush-and-enqueue
MOZ_CAN_RUN_SCRIPT void DecompressionStreamAlgorithms::DecompressAndEnqueue(
JSContext* aCx, Span<const uint8_t> aInput, Flush aFlush,
TransformStreamDefaultController& aController, ErrorResult& aRv) {
MOZ_ASSERT_IF(aFlush == Flush::Yes, !aInput.Length());
JS::RootedVector<JSObject*> array(aCx);
// 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: If buffer is empty, return.
// (Skipping, see https://github.com/whatwg/compression/issues/78)
// Step 4: Let arrays be the result of splitting buffer into one or more
// non-empty pieces and converting them into Uint8Arrays.
bool fullyConsumed = Decompress(aCx, aInput, &array, aFlush, aRv);
if (aRv.Failed()) {
return;
}
// Step 5: For each Uint8Array array, enqueue array in ds's transform.
for (const auto& view : array) {
JS::Rooted<JS::Value> value(aCx, JS::ObjectValue(*view));
aController.Enqueue(aCx, value, aRv);
if (aRv.Failed()) {
return;
}
}
// Step 6: If the end of the compressed input has been reached, and ds's
// context has not fully consumed chunk, then throw a TypeError.
if (mObservedStreamEnd && !fullyConsumed) {
aRv.ThrowTypeError("Unexpected input after the end of stream");
return;
}
// Step 3 of
// https://compression.spec.whatwg.org/#decompress-flush-and-enqueue
// If the end of the compressed input has not been reached, then throw a
// TypeError.
if (aFlush == Flush::Yes && !mObservedStreamEnd) {
aRv.ThrowTypeError("The input is ended without reaching the stream end");
return;
}
}
} // namespace mozilla::dom::compression
|