1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef mozilla_dom_textdecoder_h_
6 #define mozilla_dom_textdecoder_h_
8 #include "mozilla/dom/NonRefcountedDOMObject.h"
9 #include "mozilla/dom/TextDecoderBinding.h"
10 #include "mozilla/dom/TypedArray.h"
11 #include "nsIUnicodeDecoder.h"
19 class TextDecoder MOZ_FINAL
20 : public NonRefcountedDOMObject
23 // The WebIDL constructor.
25 Constructor(const GlobalObject
& aGlobal
,
26 const nsAString
& aEncoding
,
27 const TextDecoderOptions
& aOptions
,
30 nsAutoPtr
<TextDecoder
> txtDecoder(new TextDecoder());
31 txtDecoder
->Init(aEncoding
, aOptions
.mFatal
, aRv
);
35 return txtDecoder
.forget();
41 MOZ_COUNT_CTOR(TextDecoder
);
46 MOZ_COUNT_DTOR(TextDecoder
);
49 JSObject
* WrapObject(JSContext
* aCx
, bool* aTookOwnership
)
51 return TextDecoderBinding::Wrap(aCx
, this, aTookOwnership
);
55 * Validates provided label and throws an exception if invalid label.
57 * @param aLabel The encoding label (case insensitive) provided.
58 * @param aFatal indicates whether to throw an 'EncodingError'
59 * exception or not when decoding.
60 * @return aRv EncodingError exception else null.
62 void Init(const nsAString
& aLabel
, const bool aFatal
, ErrorResult
& aRv
);
65 * Performs initialization with a Gecko-canonical encoding name (as opposed
68 * @param aEncoding A Gecko-canonical encoding name
69 * @param aFatal indicates whether to throw an 'EncodingError'
70 * exception or not when decoding.
72 void InitWithEncoding(const nsACString
& aEncoding
, const bool aFatal
);
75 * Return the encoding name.
77 * @param aEncoding, current encoding.
79 void GetEncoding(nsAString
& aEncoding
);
82 * Decodes incoming byte stream of characters in charset indicated by
85 * The encoding algorithm state is reset if aOptions.mStream is not set.
87 * If the fatal flag is set then a decoding error will throw EncodingError.
88 * Else the decoder will return a decoded string with replacement
89 * character(s) for unidentified character(s).
91 * @param aView, incoming byte stream of characters to be decoded to
92 * to UTF-16 code points.
93 * @param aOptions, indicates if streaming or not.
94 * @param aOutDecodedString, decoded string of UTF-16 code points.
95 * @param aRv, error result.
97 void Decode(const char* aInput
, const int32_t aLength
,
98 const bool aStream
, nsAString
& aOutDecodedString
,
101 void Decode(nsAString
& aOutDecodedString
,
103 Decode(nullptr, 0, false, aOutDecodedString
, aRv
);
106 void Decode(const ArrayBufferView
& aView
,
107 const TextDecodeOptions
& aOptions
,
108 nsAString
& aOutDecodedString
,
110 aView
.ComputeLengthAndData();
111 Decode(reinterpret_cast<char*>(aView
.Data()), aView
.Length(),
112 aOptions
.mStream
, aOutDecodedString
, aRv
);
117 nsCOMPtr
<nsIUnicodeDecoder
> mDecoder
;
124 #endif // mozilla_dom_textdecoder_h_