tor-browser

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

TextDecoder.h (3784B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      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 file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef mozilla_dom_textdecoder_h_
      8 #define mozilla_dom_textdecoder_h_
      9 
     10 #include "mozilla/Encoding.h"
     11 #include "mozilla/ErrorResult.h"
     12 #include "mozilla/UniquePtr.h"
     13 #include "mozilla/dom/BufferSourceBindingFwd.h"
     14 #include "mozilla/dom/NonRefcountedDOMObject.h"
     15 #include "mozilla/dom/TextDecoderBinding.h"
     16 #include "mozilla/dom/TypedArray.h"
     17 
     18 namespace mozilla::dom {
     19 
     20 class TextDecoderCommon {
     21 public:
     22  /**
     23   * Decodes incoming byte stream of characters in charset indicated by
     24   * encoding.
     25   *
     26   * The encoding algorithm state is reset if aOptions.mStream is not set.
     27   *
     28   * If the fatal flag is set then a decoding error will throw EncodingError.
     29   * Else the decoder will return a decoded string with replacement
     30   * character(s) for unidentified character(s).
     31   *
     32   * @param      aInput, incoming byte stream of characters to be decoded to
     33   *                    to UTF-16 code points.
     34   * @param      aStream, indicates if streaming or not.
     35   * @param      aOutDecodedString, decoded string of UTF-16 code points.
     36   * @param      aRv, error result.
     37   */
     38  void DecodeNative(mozilla::Span<const uint8_t> aInput, const bool aStream,
     39                    nsAString& aOutDecodedString, ErrorResult& aRv);
     40 
     41  /**
     42   * Return the encoding name.
     43   *
     44   * @param aEncoding, current encoding.
     45   */
     46  void GetEncoding(nsAString& aEncoding);
     47 
     48  bool Fatal() const { return mFatal; }
     49 
     50  bool IgnoreBOM() const { return mIgnoreBOM; }
     51 
     52 protected:
     53  mozilla::UniquePtr<mozilla::Decoder> mDecoder;
     54  nsCString mEncoding;
     55  bool mFatal = false;
     56  bool mIgnoreBOM = false;
     57 };
     58 
     59 class TextDecoder final : public NonRefcountedDOMObject,
     60                          public TextDecoderCommon {
     61 public:
     62  // The WebIDL constructor.
     63  static UniquePtr<TextDecoder> Constructor(const GlobalObject& aGlobal,
     64                                            const nsAString& aEncoding,
     65                                            const TextDecoderOptions& aOptions,
     66                                            ErrorResult& aRv) {
     67    auto txtDecoder = MakeUnique<TextDecoder>();
     68    txtDecoder->Init(aEncoding, aOptions, aRv);
     69    if (aRv.Failed()) {
     70      return nullptr;
     71    }
     72    return txtDecoder;
     73  }
     74 
     75  TextDecoder() { MOZ_COUNT_CTOR(TextDecoder); }
     76 
     77  MOZ_COUNTED_DTOR(TextDecoder)
     78 
     79  bool WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto,
     80                  JS::MutableHandle<JSObject*> aReflector) {
     81    return TextDecoder_Binding::Wrap(aCx, this, aGivenProto, aReflector);
     82  }
     83 
     84  /**
     85   * Validates provided label and throws an exception if invalid label.
     86   *
     87   * @param aLabel       The encoding label (case insensitive) provided.
     88   * @param aOptions     The TextDecoderOptions to use.
     89   * @return aRv         EncodingError exception else null.
     90   */
     91  void Init(const nsAString& aLabel, const TextDecoderOptions& aOptions,
     92            ErrorResult& aRv);
     93 
     94  /**
     95   * Performs initialization with a Gecko-canonical encoding name (as opposed
     96   * to a label.)
     97   *
     98   * @param aEncoding    An Encoding object
     99   * @param aOptions     The TextDecoderOptions to use.
    100   */
    101  void InitWithEncoding(NotNull<const Encoding*> aEncoding,
    102                        const TextDecoderOptions& aOptions);
    103 
    104  void Decode(const Optional<BufferSource>& aBuffer,
    105              const TextDecodeOptions& aOptions, nsAString& aOutDecodedString,
    106              ErrorResult& aRv);
    107 
    108 private:
    109 };
    110 
    111 }  // namespace mozilla::dom
    112 
    113 #endif  // mozilla_dom_textdecoder_h_