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