Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #ifndef mozilla_dom_textencoder_h_ |
michael@0 | 6 | #define mozilla_dom_textencoder_h_ |
michael@0 | 7 | |
michael@0 | 8 | #include "mozilla/dom/NonRefcountedDOMObject.h" |
michael@0 | 9 | #include "mozilla/dom/TextEncoderBinding.h" |
michael@0 | 10 | #include "mozilla/dom/TypedArray.h" |
michael@0 | 11 | #include "nsIUnicodeEncoder.h" |
michael@0 | 12 | |
michael@0 | 13 | namespace mozilla { |
michael@0 | 14 | class ErrorResult; |
michael@0 | 15 | |
michael@0 | 16 | namespace dom { |
michael@0 | 17 | |
michael@0 | 18 | class TextEncoder MOZ_FINAL : public NonRefcountedDOMObject |
michael@0 | 19 | { |
michael@0 | 20 | public: |
michael@0 | 21 | // The WebIDL constructor. |
michael@0 | 22 | |
michael@0 | 23 | static TextEncoder* |
michael@0 | 24 | Constructor(const GlobalObject& aGlobal, |
michael@0 | 25 | const nsAString& aEncoding, |
michael@0 | 26 | ErrorResult& aRv) |
michael@0 | 27 | { |
michael@0 | 28 | nsAutoPtr<TextEncoder> txtEncoder(new TextEncoder()); |
michael@0 | 29 | txtEncoder->Init(aEncoding, aRv); |
michael@0 | 30 | if (aRv.Failed()) { |
michael@0 | 31 | return nullptr; |
michael@0 | 32 | } |
michael@0 | 33 | return txtEncoder.forget(); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | TextEncoder() |
michael@0 | 37 | { |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | virtual |
michael@0 | 41 | ~TextEncoder() |
michael@0 | 42 | {} |
michael@0 | 43 | |
michael@0 | 44 | JSObject* WrapObject(JSContext* aCx, bool* aTookOwnership) |
michael@0 | 45 | { |
michael@0 | 46 | return TextEncoderBinding::Wrap(aCx, this, aTookOwnership); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | void Encode(JSContext* aCx, |
michael@0 | 50 | JS::Handle<JSObject*> aObj, |
michael@0 | 51 | const nsAString& aString, |
michael@0 | 52 | const TextEncodeOptions& aOptions, |
michael@0 | 53 | JS::MutableHandle<JSObject*> aRetval, |
michael@0 | 54 | ErrorResult& aRv) { |
michael@0 | 55 | TextEncoder::Encode(aCx, aObj, aString, aOptions.mStream, aRetval, aRv); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | protected: |
michael@0 | 59 | |
michael@0 | 60 | /** |
michael@0 | 61 | * Validates provided encoding and throws an exception if invalid encoding. |
michael@0 | 62 | * If no encoding is provided then mEncoding is default initialised to "utf-8". |
michael@0 | 63 | * |
michael@0 | 64 | * @param aEncoding Optional encoding (case insensitive) provided. |
michael@0 | 65 | * (valid values are "utf-8", "utf-16", "utf-16be") |
michael@0 | 66 | * Default value is "utf-8" if no encoding is provided. |
michael@0 | 67 | * @return aRv EncodingError exception else null. |
michael@0 | 68 | */ |
michael@0 | 69 | void Init(const nsAString& aEncoding, ErrorResult& aRv); |
michael@0 | 70 | |
michael@0 | 71 | public: |
michael@0 | 72 | /** |
michael@0 | 73 | * Return the encoding name. |
michael@0 | 74 | * |
michael@0 | 75 | * @param aEncoding, current encoding. |
michael@0 | 76 | */ |
michael@0 | 77 | void GetEncoding(nsAString& aEncoding); |
michael@0 | 78 | |
michael@0 | 79 | /** |
michael@0 | 80 | * Encodes incoming utf-16 code units/ DOM string to the requested encoding. |
michael@0 | 81 | * |
michael@0 | 82 | * @param aCx Javascript context. |
michael@0 | 83 | * @param aObj the wrapper of the TextEncoder |
michael@0 | 84 | * @param aString utf-16 code units to be encoded. |
michael@0 | 85 | * @param aOptions Streaming option. Initialised by default to false. |
michael@0 | 86 | * If the streaming option is false, then the encoding |
michael@0 | 87 | * algorithm state will get reset. If set to true then |
michael@0 | 88 | * the previous encoding is reused/continued. |
michael@0 | 89 | * @return JSObject* The Uint8Array wrapped in a JS object. Returned via |
michael@0 | 90 | * the aRetval out param. |
michael@0 | 91 | */ |
michael@0 | 92 | void Encode(JSContext* aCx, |
michael@0 | 93 | JS::Handle<JSObject*> aObj, |
michael@0 | 94 | const nsAString& aString, |
michael@0 | 95 | const bool aStream, |
michael@0 | 96 | JS::MutableHandle<JSObject*> aRetval, |
michael@0 | 97 | ErrorResult& aRv); |
michael@0 | 98 | |
michael@0 | 99 | private: |
michael@0 | 100 | nsCString mEncoding; |
michael@0 | 101 | nsCOMPtr<nsIUnicodeEncoder> mEncoder; |
michael@0 | 102 | }; |
michael@0 | 103 | |
michael@0 | 104 | } // dom |
michael@0 | 105 | } // mozilla |
michael@0 | 106 | |
michael@0 | 107 | #endif // mozilla_dom_textencoder_h_ |