Bumping manifests a=b2g-bump
[gecko.git] / dom / encoding / TextDecoder.h
blobaf40306ac8e6fb9415633112af40712f7223e32a
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"
13 namespace mozilla {
15 class ErrorResult;
17 namespace dom {
19 class TextDecoder MOZ_FINAL
20 : public NonRefcountedDOMObject
22 public:
23 // The WebIDL constructor.
24 static TextDecoder*
25 Constructor(const GlobalObject& aGlobal,
26 const nsAString& aEncoding,
27 const TextDecoderOptions& aOptions,
28 ErrorResult& aRv)
30 nsAutoPtr<TextDecoder> txtDecoder(new TextDecoder());
31 txtDecoder->Init(aEncoding, aOptions.mFatal, aRv);
32 if (aRv.Failed()) {
33 return nullptr;
35 return txtDecoder.forget();
38 TextDecoder()
39 : mFatal(false)
41 MOZ_COUNT_CTOR(TextDecoder);
44 ~TextDecoder()
46 MOZ_COUNT_DTOR(TextDecoder);
49 JSObject* WrapObject(JSContext* aCx, bool* aTookOwnership)
51 return TextDecoderBinding::Wrap(aCx, this, aTookOwnership);
54 /**
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);
64 /**
65 * Performs initialization with a Gecko-canonical encoding name (as opposed
66 * to a label.)
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);
74 /**
75 * Return the encoding name.
77 * @param aEncoding, current encoding.
79 void GetEncoding(nsAString& aEncoding);
81 /**
82 * Decodes incoming byte stream of characters in charset indicated by
83 * encoding.
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,
99 ErrorResult& aRv);
101 void Decode(nsAString& aOutDecodedString,
102 ErrorResult& aRv) {
103 Decode(nullptr, 0, false, aOutDecodedString, aRv);
106 void Decode(const ArrayBufferView& aView,
107 const TextDecodeOptions& aOptions,
108 nsAString& aOutDecodedString,
109 ErrorResult& aRv) {
110 aView.ComputeLengthAndData();
111 Decode(reinterpret_cast<char*>(aView.Data()), aView.Length(),
112 aOptions.mStream, aOutDecodedString, aRv);
115 private:
116 nsCString mEncoding;
117 nsCOMPtr<nsIUnicodeDecoder> mDecoder;
118 bool mFatal;
121 } // dom
122 } // mozilla
124 #endif // mozilla_dom_textdecoder_h_