TextEncoder.h (2079B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_textencoder_h_ 8 #define mozilla_dom_textencoder_h_ 9 10 #include "mozilla/Encoding.h" 11 #include "mozilla/dom/NonRefcountedDOMObject.h" 12 #include "mozilla/dom/TextEncoderBinding.h" 13 #include "mozilla/dom/TypedArray.h" 14 15 namespace mozilla { 16 class ErrorResult; 17 18 namespace dom { 19 20 class TextEncoder final : public NonRefcountedDOMObject { 21 public: 22 // The WebIDL constructor. 23 24 static UniquePtr<TextEncoder> Constructor(const GlobalObject& aGlobal) { 25 return MakeUnique<TextEncoder>(); 26 } 27 28 TextEncoder() = default; 29 30 virtual ~TextEncoder() = default; 31 32 bool WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto, 33 JS::MutableHandle<JSObject*> aReflector) { 34 return TextEncoder_Binding::Wrap(aCx, this, aGivenProto, aReflector); 35 } 36 37 public: 38 /** 39 * Return the encoding name. 40 * 41 * @param aEncoding, current encoding. 42 */ 43 void GetEncoding(nsACString& aEncoding); 44 45 /** 46 * Encodes incoming code units to utf-8. 47 * 48 * @param aCx Javascript context. 49 * @param aObj the wrapper of the TextEncoder 50 * @param aString already-encoded utf-8 code units to be returned, via 51 * UTF8String. 52 * @return JSObject* The Uint8Array wrapped in a JS object. Returned via 53 * the aRetval out param. 54 */ 55 void Encode(JSContext* aCx, JS::Handle<JSObject*> aObj, 56 const nsACString& aUtf8String, 57 JS::MutableHandle<JSObject*> aRetval, ErrorResult& aRv); 58 59 void EncodeInto(JSContext* aCx, JS::Handle<JSString*> aSrc, 60 const Uint8Array& aDst, TextEncoderEncodeIntoResult& aResult, 61 OOMReporter& aError); 62 }; 63 64 } // namespace dom 65 } // namespace mozilla 66 67 #endif // mozilla_dom_textencoder_h_