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