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_textencoder_h_ michael@0: #define mozilla_dom_textencoder_h_ michael@0: michael@0: #include "mozilla/dom/NonRefcountedDOMObject.h" michael@0: #include "mozilla/dom/TextEncoderBinding.h" michael@0: #include "mozilla/dom/TypedArray.h" michael@0: #include "nsIUnicodeEncoder.h" michael@0: michael@0: namespace mozilla { michael@0: class ErrorResult; michael@0: michael@0: namespace dom { michael@0: michael@0: class TextEncoder MOZ_FINAL : public NonRefcountedDOMObject michael@0: { michael@0: public: michael@0: // The WebIDL constructor. michael@0: michael@0: static TextEncoder* michael@0: Constructor(const GlobalObject& aGlobal, michael@0: const nsAString& aEncoding, michael@0: ErrorResult& aRv) michael@0: { michael@0: nsAutoPtr txtEncoder(new TextEncoder()); michael@0: txtEncoder->Init(aEncoding, aRv); michael@0: if (aRv.Failed()) { michael@0: return nullptr; michael@0: } michael@0: return txtEncoder.forget(); michael@0: } michael@0: michael@0: TextEncoder() michael@0: { michael@0: } michael@0: michael@0: virtual michael@0: ~TextEncoder() michael@0: {} michael@0: michael@0: JSObject* WrapObject(JSContext* aCx, bool* aTookOwnership) michael@0: { michael@0: return TextEncoderBinding::Wrap(aCx, this, aTookOwnership); michael@0: } michael@0: michael@0: void Encode(JSContext* aCx, michael@0: JS::Handle aObj, michael@0: const nsAString& aString, michael@0: const TextEncodeOptions& aOptions, michael@0: JS::MutableHandle aRetval, michael@0: ErrorResult& aRv) { michael@0: TextEncoder::Encode(aCx, aObj, aString, aOptions.mStream, aRetval, aRv); michael@0: } michael@0: michael@0: protected: michael@0: michael@0: /** michael@0: * Validates provided encoding and throws an exception if invalid encoding. michael@0: * If no encoding is provided then mEncoding is default initialised to "utf-8". michael@0: * michael@0: * @param aEncoding Optional encoding (case insensitive) provided. michael@0: * (valid values are "utf-8", "utf-16", "utf-16be") michael@0: * Default value is "utf-8" if no encoding is provided. michael@0: * @return aRv EncodingError exception else null. michael@0: */ michael@0: void Init(const nsAString& aEncoding, ErrorResult& aRv); michael@0: michael@0: public: 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: * Encodes incoming utf-16 code units/ DOM string to the requested encoding. michael@0: * michael@0: * @param aCx Javascript context. michael@0: * @param aObj the wrapper of the TextEncoder michael@0: * @param aString utf-16 code units to be encoded. michael@0: * @param aOptions Streaming option. Initialised by default to false. michael@0: * If the streaming option is false, then the encoding michael@0: * algorithm state will get reset. If set to true then michael@0: * the previous encoding is reused/continued. michael@0: * @return JSObject* The Uint8Array wrapped in a JS object. Returned via michael@0: * the aRetval out param. michael@0: */ michael@0: void Encode(JSContext* aCx, michael@0: JS::Handle aObj, michael@0: const nsAString& aString, michael@0: const bool aStream, michael@0: JS::MutableHandle aRetval, michael@0: ErrorResult& aRv); michael@0: michael@0: private: michael@0: nsCString mEncoding; michael@0: nsCOMPtr mEncoder; michael@0: }; michael@0: michael@0: } // dom michael@0: } // mozilla michael@0: michael@0: #endif // mozilla_dom_textencoder_h_