Bumping manifests a=b2g-bump
[gecko.git] / dom / encoding / TextDecoder.h
blob9d4e486a46e066681293247c92a347773d77095d
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef mozilla_dom_textdecoder_h_
7 #define mozilla_dom_textdecoder_h_
9 #include "mozilla/dom/NonRefcountedDOMObject.h"
10 #include "mozilla/dom/TextDecoderBinding.h"
11 #include "mozilla/dom/TypedArray.h"
12 #include "nsIUnicodeDecoder.h"
14 namespace mozilla {
16 class ErrorResult;
18 namespace dom {
20 class ArrayBufferViewOrArrayBuffer;
22 class TextDecoder MOZ_FINAL
23 : public NonRefcountedDOMObject
25 public:
26 // The WebIDL constructor.
27 static TextDecoder*
28 Constructor(const GlobalObject& aGlobal,
29 const nsAString& aEncoding,
30 const TextDecoderOptions& aOptions,
31 ErrorResult& aRv)
33 nsAutoPtr<TextDecoder> txtDecoder(new TextDecoder());
34 txtDecoder->Init(aEncoding, aOptions.mFatal, aRv);
35 if (aRv.Failed()) {
36 return nullptr;
38 return txtDecoder.forget();
41 TextDecoder()
42 : mFatal(false)
44 MOZ_COUNT_CTOR(TextDecoder);
47 ~TextDecoder()
49 MOZ_COUNT_DTOR(TextDecoder);
52 JSObject* WrapObject(JSContext* aCx, bool* aTookOwnership)
54 return TextDecoderBinding::Wrap(aCx, this, aTookOwnership);
57 /**
58 * Validates provided label and throws an exception if invalid label.
60 * @param aLabel The encoding label (case insensitive) provided.
61 * @param aFatal indicates whether to throw an 'EncodingError'
62 * exception or not when decoding.
63 * @return aRv EncodingError exception else null.
65 void Init(const nsAString& aLabel, const bool aFatal, ErrorResult& aRv);
67 /**
68 * Performs initialization with a Gecko-canonical encoding name (as opposed
69 * to a label.)
71 * @param aEncoding A Gecko-canonical encoding name
72 * @param aFatal indicates whether to throw an 'EncodingError'
73 * exception or not when decoding.
75 void InitWithEncoding(const nsACString& aEncoding, const bool aFatal);
77 /**
78 * Return the encoding name.
80 * @param aEncoding, current encoding.
82 void GetEncoding(nsAString& aEncoding);
84 /**
85 * Decodes incoming byte stream of characters in charset indicated by
86 * encoding.
88 * The encoding algorithm state is reset if aOptions.mStream is not set.
90 * If the fatal flag is set then a decoding error will throw EncodingError.
91 * Else the decoder will return a decoded string with replacement
92 * character(s) for unidentified character(s).
94 * @param aView, incoming byte stream of characters to be decoded to
95 * to UTF-16 code points.
96 * @param aOptions, indicates if streaming or not.
97 * @param aOutDecodedString, decoded string of UTF-16 code points.
98 * @param aRv, error result.
100 void Decode(const char* aInput, const int32_t aLength,
101 const bool aStream, nsAString& aOutDecodedString,
102 ErrorResult& aRv);
104 void Decode(const Optional<ArrayBufferViewOrArrayBuffer>& aBuffer,
105 const TextDecodeOptions& aOptions,
106 nsAString& aOutDecodedString,
107 ErrorResult& aRv);
109 bool Fatal() const {
110 return mFatal;
113 private:
114 nsCString mEncoding;
115 nsCOMPtr<nsIUnicodeDecoder> mDecoder;
116 bool mFatal;
119 } // dom
120 } // mozilla
122 #endif // mozilla_dom_textdecoder_h_