michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_dom_textdecoder_h_ michael@0: #define mozilla_dom_textdecoder_h_ michael@0: michael@0: #include "mozilla/dom/NonRefcountedDOMObject.h" michael@0: #include "mozilla/dom/TextDecoderBinding.h" michael@0: #include "mozilla/dom/TypedArray.h" michael@0: #include "nsIUnicodeDecoder.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: class ErrorResult; michael@0: michael@0: namespace dom { michael@0: michael@0: class TextDecoder MOZ_FINAL michael@0: : public NonRefcountedDOMObject michael@0: { michael@0: public: michael@0: // The WebIDL constructor. michael@0: static TextDecoder* michael@0: Constructor(const GlobalObject& aGlobal, michael@0: const nsAString& aEncoding, michael@0: const TextDecoderOptions& aOptions, michael@0: ErrorResult& aRv) michael@0: { michael@0: nsAutoPtr txtDecoder(new TextDecoder()); michael@0: txtDecoder->Init(aEncoding, aOptions.mFatal, aRv); michael@0: if (aRv.Failed()) { michael@0: return nullptr; michael@0: } michael@0: return txtDecoder.forget(); michael@0: } michael@0: michael@0: TextDecoder() michael@0: : mFatal(false) michael@0: { michael@0: MOZ_COUNT_CTOR(TextDecoder); michael@0: } michael@0: michael@0: ~TextDecoder() michael@0: { michael@0: MOZ_COUNT_DTOR(TextDecoder); michael@0: } michael@0: michael@0: JSObject* WrapObject(JSContext* aCx, bool* aTookOwnership) michael@0: { michael@0: return TextDecoderBinding::Wrap(aCx, this, aTookOwnership); michael@0: } michael@0: michael@0: /** michael@0: * Validates provided label and throws an exception if invalid label. michael@0: * michael@0: * @param aLabel The encoding label (case insensitive) provided. michael@0: * @param aFatal indicates whether to throw an 'EncodingError' michael@0: * exception or not when decoding. michael@0: * @return aRv EncodingError exception else null. michael@0: */ michael@0: void Init(const nsAString& aLabel, const bool aFatal, ErrorResult& aRv); michael@0: michael@0: /** michael@0: * Performs initialization with a Gecko-canonical encoding name (as opposed michael@0: * to a label.) michael@0: * michael@0: * @param aEncoding A Gecko-canonical encoding name michael@0: * @param aFatal indicates whether to throw an 'EncodingError' michael@0: * exception or not when decoding. michael@0: */ michael@0: void InitWithEncoding(const nsACString& aEncoding, const bool aFatal); michael@0: michael@0: /** michael@0: * Return the encoding name. michael@0: * michael@0: * @param aEncoding, current encoding. michael@0: */ michael@0: void GetEncoding(nsAString& aEncoding); michael@0: michael@0: /** michael@0: * Decodes incoming byte stream of characters in charset indicated by michael@0: * encoding. michael@0: * michael@0: * The encoding algorithm state is reset if aOptions.mStream is not set. michael@0: * michael@0: * If the fatal flag is set then a decoding error will throw EncodingError. michael@0: * Else the decoder will return a decoded string with replacement michael@0: * character(s) for unidentified character(s). michael@0: * michael@0: * @param aView, incoming byte stream of characters to be decoded to michael@0: * to UTF-16 code points. michael@0: * @param aOptions, indicates if streaming or not. michael@0: * @param aOutDecodedString, decoded string of UTF-16 code points. michael@0: * @param aRv, error result. michael@0: */ michael@0: void Decode(const char* aInput, const int32_t aLength, michael@0: const bool aStream, nsAString& aOutDecodedString, michael@0: ErrorResult& aRv); michael@0: michael@0: void Decode(nsAString& aOutDecodedString, michael@0: ErrorResult& aRv) { michael@0: Decode(nullptr, 0, false, aOutDecodedString, aRv); michael@0: } michael@0: michael@0: void Decode(const ArrayBufferView& aView, michael@0: const TextDecodeOptions& aOptions, michael@0: nsAString& aOutDecodedString, michael@0: ErrorResult& aRv) { michael@0: aView.ComputeLengthAndData(); michael@0: Decode(reinterpret_cast(aView.Data()), aView.Length(), michael@0: aOptions.mStream, aOutDecodedString, aRv); michael@0: } michael@0: michael@0: private: michael@0: nsCString mEncoding; michael@0: nsCOMPtr mDecoder; michael@0: bool mFatal; michael@0: }; michael@0: michael@0: } // dom michael@0: } // mozilla michael@0: michael@0: #endif // mozilla_dom_textdecoder_h_