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_textdecoder_h_ |
michael@0 | 6 | #define mozilla_dom_textdecoder_h_ |
michael@0 | 7 | |
michael@0 | 8 | #include "mozilla/dom/NonRefcountedDOMObject.h" |
michael@0 | 9 | #include "mozilla/dom/TextDecoderBinding.h" |
michael@0 | 10 | #include "mozilla/dom/TypedArray.h" |
michael@0 | 11 | #include "nsIUnicodeDecoder.h" |
michael@0 | 12 | |
michael@0 | 13 | namespace mozilla { |
michael@0 | 14 | |
michael@0 | 15 | class ErrorResult; |
michael@0 | 16 | |
michael@0 | 17 | namespace dom { |
michael@0 | 18 | |
michael@0 | 19 | class TextDecoder MOZ_FINAL |
michael@0 | 20 | : public NonRefcountedDOMObject |
michael@0 | 21 | { |
michael@0 | 22 | public: |
michael@0 | 23 | // The WebIDL constructor. |
michael@0 | 24 | static TextDecoder* |
michael@0 | 25 | Constructor(const GlobalObject& aGlobal, |
michael@0 | 26 | const nsAString& aEncoding, |
michael@0 | 27 | const TextDecoderOptions& aOptions, |
michael@0 | 28 | ErrorResult& aRv) |
michael@0 | 29 | { |
michael@0 | 30 | nsAutoPtr<TextDecoder> txtDecoder(new TextDecoder()); |
michael@0 | 31 | txtDecoder->Init(aEncoding, aOptions.mFatal, aRv); |
michael@0 | 32 | if (aRv.Failed()) { |
michael@0 | 33 | return nullptr; |
michael@0 | 34 | } |
michael@0 | 35 | return txtDecoder.forget(); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | TextDecoder() |
michael@0 | 39 | : mFatal(false) |
michael@0 | 40 | { |
michael@0 | 41 | MOZ_COUNT_CTOR(TextDecoder); |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | ~TextDecoder() |
michael@0 | 45 | { |
michael@0 | 46 | MOZ_COUNT_DTOR(TextDecoder); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | JSObject* WrapObject(JSContext* aCx, bool* aTookOwnership) |
michael@0 | 50 | { |
michael@0 | 51 | return TextDecoderBinding::Wrap(aCx, this, aTookOwnership); |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | /** |
michael@0 | 55 | * Validates provided label and throws an exception if invalid label. |
michael@0 | 56 | * |
michael@0 | 57 | * @param aLabel The encoding label (case insensitive) provided. |
michael@0 | 58 | * @param aFatal indicates whether to throw an 'EncodingError' |
michael@0 | 59 | * exception or not when decoding. |
michael@0 | 60 | * @return aRv EncodingError exception else null. |
michael@0 | 61 | */ |
michael@0 | 62 | void Init(const nsAString& aLabel, const bool aFatal, ErrorResult& aRv); |
michael@0 | 63 | |
michael@0 | 64 | /** |
michael@0 | 65 | * Performs initialization with a Gecko-canonical encoding name (as opposed |
michael@0 | 66 | * to a label.) |
michael@0 | 67 | * |
michael@0 | 68 | * @param aEncoding A Gecko-canonical encoding name |
michael@0 | 69 | * @param aFatal indicates whether to throw an 'EncodingError' |
michael@0 | 70 | * exception or not when decoding. |
michael@0 | 71 | */ |
michael@0 | 72 | void InitWithEncoding(const nsACString& aEncoding, const bool aFatal); |
michael@0 | 73 | |
michael@0 | 74 | /** |
michael@0 | 75 | * Return the encoding name. |
michael@0 | 76 | * |
michael@0 | 77 | * @param aEncoding, current encoding. |
michael@0 | 78 | */ |
michael@0 | 79 | void GetEncoding(nsAString& aEncoding); |
michael@0 | 80 | |
michael@0 | 81 | /** |
michael@0 | 82 | * Decodes incoming byte stream of characters in charset indicated by |
michael@0 | 83 | * encoding. |
michael@0 | 84 | * |
michael@0 | 85 | * The encoding algorithm state is reset if aOptions.mStream is not set. |
michael@0 | 86 | * |
michael@0 | 87 | * If the fatal flag is set then a decoding error will throw EncodingError. |
michael@0 | 88 | * Else the decoder will return a decoded string with replacement |
michael@0 | 89 | * character(s) for unidentified character(s). |
michael@0 | 90 | * |
michael@0 | 91 | * @param aView, incoming byte stream of characters to be decoded to |
michael@0 | 92 | * to UTF-16 code points. |
michael@0 | 93 | * @param aOptions, indicates if streaming or not. |
michael@0 | 94 | * @param aOutDecodedString, decoded string of UTF-16 code points. |
michael@0 | 95 | * @param aRv, error result. |
michael@0 | 96 | */ |
michael@0 | 97 | void Decode(const char* aInput, const int32_t aLength, |
michael@0 | 98 | const bool aStream, nsAString& aOutDecodedString, |
michael@0 | 99 | ErrorResult& aRv); |
michael@0 | 100 | |
michael@0 | 101 | void Decode(nsAString& aOutDecodedString, |
michael@0 | 102 | ErrorResult& aRv) { |
michael@0 | 103 | Decode(nullptr, 0, false, aOutDecodedString, aRv); |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | void Decode(const ArrayBufferView& aView, |
michael@0 | 107 | const TextDecodeOptions& aOptions, |
michael@0 | 108 | nsAString& aOutDecodedString, |
michael@0 | 109 | ErrorResult& aRv) { |
michael@0 | 110 | aView.ComputeLengthAndData(); |
michael@0 | 111 | Decode(reinterpret_cast<char*>(aView.Data()), aView.Length(), |
michael@0 | 112 | aOptions.mStream, aOutDecodedString, aRv); |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | private: |
michael@0 | 116 | nsCString mEncoding; |
michael@0 | 117 | nsCOMPtr<nsIUnicodeDecoder> mDecoder; |
michael@0 | 118 | bool mFatal; |
michael@0 | 119 | }; |
michael@0 | 120 | |
michael@0 | 121 | } // dom |
michael@0 | 122 | } // mozilla |
michael@0 | 123 | |
michael@0 | 124 | #endif // mozilla_dom_textdecoder_h_ |