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"
20 class ArrayBufferViewOrArrayBuffer
;
22 class TextDecoder final
: public NonRefcountedDOMObject
{
24 // The WebIDL constructor.
25 static TextDecoder
* Constructor(const GlobalObject
& aGlobal
,
26 const nsAString
& aEncoding
,
27 const TextDecoderOptions
& aOptions
,
29 auto txtDecoder
= MakeUnique
<TextDecoder
>();
30 txtDecoder
->Init(aEncoding
, aOptions
, aRv
);
34 return txtDecoder
.release();
37 TextDecoder() : mFatal(false), mIgnoreBOM(false) {
38 MOZ_COUNT_CTOR(TextDecoder
);
41 MOZ_COUNTED_DTOR(TextDecoder
)
43 bool WrapObject(JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
,
44 JS::MutableHandle
<JSObject
*> aReflector
) {
45 return TextDecoder_Binding::Wrap(aCx
, this, aGivenProto
, aReflector
);
49 * Validates provided label and throws an exception if invalid label.
51 * @param aLabel The encoding label (case insensitive) provided.
52 * @param aOptions The TextDecoderOptions to use.
53 * @return aRv EncodingError exception else null.
55 void Init(const nsAString
& aLabel
, const TextDecoderOptions
& aOptions
,
59 * Performs initialization with a Gecko-canonical encoding name (as opposed
62 * @param aEncoding An Encoding object
63 * @param aOptions The TextDecoderOptions to use.
65 void InitWithEncoding(NotNull
<const Encoding
*> aEncoding
,
66 const TextDecoderOptions
& aOptions
);
69 * Return the encoding name.
71 * @param aEncoding, current encoding.
73 void GetEncoding(nsAString
& aEncoding
);
76 * Decodes incoming byte stream of characters in charset indicated by
79 * The encoding algorithm state is reset if aOptions.mStream is not set.
81 * If the fatal flag is set then a decoding error will throw EncodingError.
82 * Else the decoder will return a decoded string with replacement
83 * character(s) for unidentified character(s).
85 * @param aView, incoming byte stream of characters to be decoded to
86 * to UTF-16 code points.
87 * @param aOptions, indicates if streaming or not.
88 * @param aOutDecodedString, decoded string of UTF-16 code points.
89 * @param aRv, error result.
91 void Decode(mozilla::Span
<const uint8_t> aInput
, const bool aStream
,
92 nsAString
& aOutDecodedString
, ErrorResult
& aRv
);
94 void Decode(const Optional
<ArrayBufferViewOrArrayBuffer
>& aBuffer
,
95 const TextDecodeOptions
& aOptions
, nsAString
& aOutDecodedString
,
98 bool Fatal() const { return mFatal
; }
100 bool IgnoreBOM() const { return mIgnoreBOM
; }
104 mozilla::UniquePtr
<mozilla::Decoder
> mDecoder
;
110 } // namespace mozilla
112 #endif // mozilla_dom_textdecoder_h_