no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / dom / encoding / TextDecoder.h
blob1e2c3a17108ca7b5077086c8f06613a9f70c08c5
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/. */
7 #ifndef mozilla_dom_textdecoder_h_
8 #define mozilla_dom_textdecoder_h_
10 #include "mozilla/dom/NonRefcountedDOMObject.h"
11 #include "mozilla/dom/TextDecoderBinding.h"
12 #include "mozilla/dom/TypedArray.h"
13 #include "mozilla/Encoding.h"
14 #include "mozilla/ErrorResult.h"
15 #include "mozilla/UniquePtr.h"
17 namespace mozilla::dom {
19 class ArrayBufferViewOrArrayBuffer;
21 class TextDecoderCommon {
22 public:
23 /**
24 * Decodes incoming byte stream of characters in charset indicated by
25 * encoding.
27 * The encoding algorithm state is reset if aOptions.mStream is not set.
29 * If the fatal flag is set then a decoding error will throw EncodingError.
30 * Else the decoder will return a decoded string with replacement
31 * character(s) for unidentified character(s).
33 * @param aInput, incoming byte stream of characters to be decoded to
34 * to UTF-16 code points.
35 * @param aStream, indicates if streaming or not.
36 * @param aOutDecodedString, decoded string of UTF-16 code points.
37 * @param aRv, error result.
39 void DecodeNative(mozilla::Span<const uint8_t> aInput, const bool aStream,
40 nsAString& aOutDecodedString, ErrorResult& aRv);
42 /**
43 * Return the encoding name.
45 * @param aEncoding, current encoding.
47 void GetEncoding(nsAString& aEncoding);
49 bool Fatal() const { return mFatal; }
51 bool IgnoreBOM() const { return mIgnoreBOM; }
53 protected:
54 mozilla::UniquePtr<mozilla::Decoder> mDecoder;
55 nsCString mEncoding;
56 bool mFatal = false;
57 bool mIgnoreBOM = false;
60 class TextDecoder final : public NonRefcountedDOMObject,
61 public TextDecoderCommon {
62 public:
63 // The WebIDL constructor.
64 static UniquePtr<TextDecoder> Constructor(const GlobalObject& aGlobal,
65 const nsAString& aEncoding,
66 const TextDecoderOptions& aOptions,
67 ErrorResult& aRv) {
68 auto txtDecoder = MakeUnique<TextDecoder>();
69 txtDecoder->Init(aEncoding, aOptions, aRv);
70 if (aRv.Failed()) {
71 return nullptr;
73 return txtDecoder;
76 TextDecoder() { MOZ_COUNT_CTOR(TextDecoder); }
78 MOZ_COUNTED_DTOR(TextDecoder)
80 bool WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto,
81 JS::MutableHandle<JSObject*> aReflector) {
82 return TextDecoder_Binding::Wrap(aCx, this, aGivenProto, aReflector);
85 /**
86 * Validates provided label and throws an exception if invalid label.
88 * @param aLabel The encoding label (case insensitive) provided.
89 * @param aOptions The TextDecoderOptions to use.
90 * @return aRv EncodingError exception else null.
92 void Init(const nsAString& aLabel, const TextDecoderOptions& aOptions,
93 ErrorResult& aRv);
95 /**
96 * Performs initialization with a Gecko-canonical encoding name (as opposed
97 * to a label.)
99 * @param aEncoding An Encoding object
100 * @param aOptions The TextDecoderOptions to use.
102 void InitWithEncoding(NotNull<const Encoding*> aEncoding,
103 const TextDecoderOptions& aOptions);
105 void Decode(const Optional<ArrayBufferViewOrArrayBuffer>& aBuffer,
106 const TextDecodeOptions& aOptions, nsAString& aOutDecodedString,
107 ErrorResult& aRv);
109 private:
112 } // namespace mozilla::dom
114 #endif // mozilla_dom_textdecoder_h_