tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

BaseAlgorithms.h (3755B)


      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 DOM_COMPRESSION_BASEALGORITHMS_H_
      8 #define DOM_COMPRESSION_BASEALGORITHMS_H_
      9 
     10 #include "js/TypeDecls.h"
     11 #include "mozilla/dom/TransformerCallbackHelpers.h"
     12 
     13 namespace mozilla::dom::compression {
     14 
     15 // A top-level, library-agnostic flush enum that should be converted
     16 // into the native flush values for a given (de)compression library
     17 // with a function defined below.
     18 enum class Flush : bool { No, Yes };
     19 
     20 class CompressionStreamAlgorithms : public TransformerAlgorithmsWrapper {
     21 public:
     22  // Step 3 of
     23  // https://wicg.github.io/compression/#dom-decompressionstream-decompressionstream
     24  // Let transformAlgorithm be an algorithm which takes a chunk argument and
     25  // runs the compress and enqueue a chunk algorithm with this and chunk.
     26  MOZ_CAN_RUN_SCRIPT
     27  void TransformCallbackImpl(JS::Handle<JS::Value> aChunk,
     28                             TransformStreamDefaultController& aController,
     29                             ErrorResult& aRv) override;
     30 
     31  // Step 4 of
     32  // https://compression.spec.whatwg.org/#dom-decompressionstream-decompressionstream
     33  // Let flushAlgorithm be an algorithm which takes no argument and runs the
     34  // compress flush and enqueue algorithm with this.
     35  MOZ_CAN_RUN_SCRIPT void FlushCallbackImpl(
     36      TransformStreamDefaultController& aController, ErrorResult& aRv) override;
     37 
     38 protected:
     39  static const uint16_t kBufferSize = 16384;
     40 
     41  ~CompressionStreamAlgorithms() = default;
     42 
     43  virtual void Compress(JSContext* aCx, Span<const uint8_t> aInput,
     44                        JS::MutableHandleVector<JSObject*> aOutput,
     45                        Flush aFlush, ErrorResult& aRv) = 0;
     46 
     47 private:
     48  MOZ_CAN_RUN_SCRIPT void CompressAndEnqueue(
     49      JSContext* aCx, Span<const uint8_t> aInput, Flush aFlush,
     50      TransformStreamDefaultController& aController, ErrorResult& aRv);
     51 };
     52 
     53 class DecompressionStreamAlgorithms : public TransformerAlgorithmsWrapper {
     54 public:
     55  // Step 3 of
     56  // https://wicg.github.io/compression/#dom-decompressionstream-decompressionstream
     57  // Let transformAlgorithm be an algorithm which takes a chunk argument and
     58  // runs the compress and enqueue a chunk algorithm with this and chunk.
     59  MOZ_CAN_RUN_SCRIPT
     60  void TransformCallbackImpl(JS::Handle<JS::Value> aChunk,
     61                             TransformStreamDefaultController& aController,
     62                             ErrorResult& aRv) override;
     63 
     64  // Step 4 of
     65  // https://compression.spec.whatwg.org/#dom-decompressionstream-decompressionstream
     66  // Let flushAlgorithm be an algorithm which takes no argument and runs the
     67  // compress flush and enqueue algorithm with this.
     68  MOZ_CAN_RUN_SCRIPT void FlushCallbackImpl(
     69      TransformStreamDefaultController& aController, ErrorResult& aRv) override;
     70 
     71 protected:
     72  static const uint16_t kBufferSize = 16384;
     73 
     74  ~DecompressionStreamAlgorithms() = default;
     75 
     76  /**
     77   * @return true if the input is fully consumed, else false
     78   */
     79  virtual bool Decompress(JSContext* aCx, Span<const uint8_t> aInput,
     80                          JS::MutableHandleVector<JSObject*> aOutput,
     81                          Flush aFlush, ErrorResult& aRv) = 0;
     82 
     83  bool mObservedStreamEnd = false;
     84 
     85 private:
     86  MOZ_CAN_RUN_SCRIPT void DecompressAndEnqueue(
     87      JSContext* aCx, Span<const uint8_t> aInput, Flush aFlush,
     88      TransformStreamDefaultController& aController, ErrorResult& aRv);
     89 };
     90 
     91 }  // namespace mozilla::dom::compression
     92 
     93 #endif  // DOM_COMPRESSION_BASEALGORITHMS_H_