1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/encoding/TextEncoder.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,107 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#ifndef mozilla_dom_textencoder_h_ 1.9 +#define mozilla_dom_textencoder_h_ 1.10 + 1.11 +#include "mozilla/dom/NonRefcountedDOMObject.h" 1.12 +#include "mozilla/dom/TextEncoderBinding.h" 1.13 +#include "mozilla/dom/TypedArray.h" 1.14 +#include "nsIUnicodeEncoder.h" 1.15 + 1.16 +namespace mozilla { 1.17 +class ErrorResult; 1.18 + 1.19 +namespace dom { 1.20 + 1.21 +class TextEncoder MOZ_FINAL : public NonRefcountedDOMObject 1.22 +{ 1.23 +public: 1.24 + // The WebIDL constructor. 1.25 + 1.26 + static TextEncoder* 1.27 + Constructor(const GlobalObject& aGlobal, 1.28 + const nsAString& aEncoding, 1.29 + ErrorResult& aRv) 1.30 + { 1.31 + nsAutoPtr<TextEncoder> txtEncoder(new TextEncoder()); 1.32 + txtEncoder->Init(aEncoding, aRv); 1.33 + if (aRv.Failed()) { 1.34 + return nullptr; 1.35 + } 1.36 + return txtEncoder.forget(); 1.37 + } 1.38 + 1.39 + TextEncoder() 1.40 + { 1.41 + } 1.42 + 1.43 + virtual 1.44 + ~TextEncoder() 1.45 + {} 1.46 + 1.47 + JSObject* WrapObject(JSContext* aCx, bool* aTookOwnership) 1.48 + { 1.49 + return TextEncoderBinding::Wrap(aCx, this, aTookOwnership); 1.50 + } 1.51 + 1.52 + void Encode(JSContext* aCx, 1.53 + JS::Handle<JSObject*> aObj, 1.54 + const nsAString& aString, 1.55 + const TextEncodeOptions& aOptions, 1.56 + JS::MutableHandle<JSObject*> aRetval, 1.57 + ErrorResult& aRv) { 1.58 + TextEncoder::Encode(aCx, aObj, aString, aOptions.mStream, aRetval, aRv); 1.59 + } 1.60 + 1.61 +protected: 1.62 + 1.63 + /** 1.64 + * Validates provided encoding and throws an exception if invalid encoding. 1.65 + * If no encoding is provided then mEncoding is default initialised to "utf-8". 1.66 + * 1.67 + * @param aEncoding Optional encoding (case insensitive) provided. 1.68 + * (valid values are "utf-8", "utf-16", "utf-16be") 1.69 + * Default value is "utf-8" if no encoding is provided. 1.70 + * @return aRv EncodingError exception else null. 1.71 + */ 1.72 + void Init(const nsAString& aEncoding, ErrorResult& aRv); 1.73 + 1.74 +public: 1.75 + /** 1.76 + * Return the encoding name. 1.77 + * 1.78 + * @param aEncoding, current encoding. 1.79 + */ 1.80 + void GetEncoding(nsAString& aEncoding); 1.81 + 1.82 + /** 1.83 + * Encodes incoming utf-16 code units/ DOM string to the requested encoding. 1.84 + * 1.85 + * @param aCx Javascript context. 1.86 + * @param aObj the wrapper of the TextEncoder 1.87 + * @param aString utf-16 code units to be encoded. 1.88 + * @param aOptions Streaming option. Initialised by default to false. 1.89 + * If the streaming option is false, then the encoding 1.90 + * algorithm state will get reset. If set to true then 1.91 + * the previous encoding is reused/continued. 1.92 + * @return JSObject* The Uint8Array wrapped in a JS object. Returned via 1.93 + * the aRetval out param. 1.94 + */ 1.95 + void Encode(JSContext* aCx, 1.96 + JS::Handle<JSObject*> aObj, 1.97 + const nsAString& aString, 1.98 + const bool aStream, 1.99 + JS::MutableHandle<JSObject*> aRetval, 1.100 + ErrorResult& aRv); 1.101 + 1.102 +private: 1.103 + nsCString mEncoding; 1.104 + nsCOMPtr<nsIUnicodeEncoder> mEncoder; 1.105 +}; 1.106 + 1.107 +} // dom 1.108 +} // mozilla 1.109 + 1.110 +#endif // mozilla_dom_textencoder_h_