1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/encoding/TextDecoder.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,124 @@ 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_textdecoder_h_ 1.9 +#define mozilla_dom_textdecoder_h_ 1.10 + 1.11 +#include "mozilla/dom/NonRefcountedDOMObject.h" 1.12 +#include "mozilla/dom/TextDecoderBinding.h" 1.13 +#include "mozilla/dom/TypedArray.h" 1.14 +#include "nsIUnicodeDecoder.h" 1.15 + 1.16 +namespace mozilla { 1.17 + 1.18 +class ErrorResult; 1.19 + 1.20 +namespace dom { 1.21 + 1.22 +class TextDecoder MOZ_FINAL 1.23 + : public NonRefcountedDOMObject 1.24 +{ 1.25 +public: 1.26 + // The WebIDL constructor. 1.27 + static TextDecoder* 1.28 + Constructor(const GlobalObject& aGlobal, 1.29 + const nsAString& aEncoding, 1.30 + const TextDecoderOptions& aOptions, 1.31 + ErrorResult& aRv) 1.32 + { 1.33 + nsAutoPtr<TextDecoder> txtDecoder(new TextDecoder()); 1.34 + txtDecoder->Init(aEncoding, aOptions.mFatal, aRv); 1.35 + if (aRv.Failed()) { 1.36 + return nullptr; 1.37 + } 1.38 + return txtDecoder.forget(); 1.39 + } 1.40 + 1.41 + TextDecoder() 1.42 + : mFatal(false) 1.43 + { 1.44 + MOZ_COUNT_CTOR(TextDecoder); 1.45 + } 1.46 + 1.47 + ~TextDecoder() 1.48 + { 1.49 + MOZ_COUNT_DTOR(TextDecoder); 1.50 + } 1.51 + 1.52 + JSObject* WrapObject(JSContext* aCx, bool* aTookOwnership) 1.53 + { 1.54 + return TextDecoderBinding::Wrap(aCx, this, aTookOwnership); 1.55 + } 1.56 + 1.57 + /** 1.58 + * Validates provided label and throws an exception if invalid label. 1.59 + * 1.60 + * @param aLabel The encoding label (case insensitive) provided. 1.61 + * @param aFatal indicates whether to throw an 'EncodingError' 1.62 + * exception or not when decoding. 1.63 + * @return aRv EncodingError exception else null. 1.64 + */ 1.65 + void Init(const nsAString& aLabel, const bool aFatal, ErrorResult& aRv); 1.66 + 1.67 + /** 1.68 + * Performs initialization with a Gecko-canonical encoding name (as opposed 1.69 + * to a label.) 1.70 + * 1.71 + * @param aEncoding A Gecko-canonical encoding name 1.72 + * @param aFatal indicates whether to throw an 'EncodingError' 1.73 + * exception or not when decoding. 1.74 + */ 1.75 + void InitWithEncoding(const nsACString& aEncoding, const bool aFatal); 1.76 + 1.77 + /** 1.78 + * Return the encoding name. 1.79 + * 1.80 + * @param aEncoding, current encoding. 1.81 + */ 1.82 + void GetEncoding(nsAString& aEncoding); 1.83 + 1.84 + /** 1.85 + * Decodes incoming byte stream of characters in charset indicated by 1.86 + * encoding. 1.87 + * 1.88 + * The encoding algorithm state is reset if aOptions.mStream is not set. 1.89 + * 1.90 + * If the fatal flag is set then a decoding error will throw EncodingError. 1.91 + * Else the decoder will return a decoded string with replacement 1.92 + * character(s) for unidentified character(s). 1.93 + * 1.94 + * @param aView, incoming byte stream of characters to be decoded to 1.95 + * to UTF-16 code points. 1.96 + * @param aOptions, indicates if streaming or not. 1.97 + * @param aOutDecodedString, decoded string of UTF-16 code points. 1.98 + * @param aRv, error result. 1.99 + */ 1.100 + void Decode(const char* aInput, const int32_t aLength, 1.101 + const bool aStream, nsAString& aOutDecodedString, 1.102 + ErrorResult& aRv); 1.103 + 1.104 + void Decode(nsAString& aOutDecodedString, 1.105 + ErrorResult& aRv) { 1.106 + Decode(nullptr, 0, false, aOutDecodedString, aRv); 1.107 + } 1.108 + 1.109 + void Decode(const ArrayBufferView& aView, 1.110 + const TextDecodeOptions& aOptions, 1.111 + nsAString& aOutDecodedString, 1.112 + ErrorResult& aRv) { 1.113 + aView.ComputeLengthAndData(); 1.114 + Decode(reinterpret_cast<char*>(aView.Data()), aView.Length(), 1.115 + aOptions.mStream, aOutDecodedString, aRv); 1.116 + } 1.117 + 1.118 +private: 1.119 + nsCString mEncoding; 1.120 + nsCOMPtr<nsIUnicodeDecoder> mDecoder; 1.121 + bool mFatal; 1.122 +}; 1.123 + 1.124 +} // dom 1.125 +} // mozilla 1.126 + 1.127 +#endif // mozilla_dom_textdecoder_h_