TextDecoder.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_dom_textdecoder_h_
  6. #define mozilla_dom_textdecoder_h_
  7. #include "mozilla/dom/NonRefcountedDOMObject.h"
  8. #include "mozilla/dom/TextDecoderBinding.h"
  9. #include "mozilla/dom/TypedArray.h"
  10. #include "nsAutoPtr.h"
  11. #include "nsIUnicodeDecoder.h"
  12. namespace mozilla {
  13. class ErrorResult;
  14. namespace dom {
  15. class ArrayBufferViewOrArrayBuffer;
  16. class TextDecoder final
  17. : public NonRefcountedDOMObject
  18. {
  19. public:
  20. // The WebIDL constructor.
  21. static TextDecoder*
  22. Constructor(const GlobalObject& aGlobal,
  23. const nsAString& aEncoding,
  24. const TextDecoderOptions& aOptions,
  25. ErrorResult& aRv)
  26. {
  27. nsAutoPtr<TextDecoder> txtDecoder(new TextDecoder());
  28. txtDecoder->Init(aEncoding, aOptions.mFatal, aRv);
  29. if (aRv.Failed()) {
  30. return nullptr;
  31. }
  32. return txtDecoder.forget();
  33. }
  34. TextDecoder()
  35. : mFatal(false)
  36. {
  37. MOZ_COUNT_CTOR(TextDecoder);
  38. }
  39. ~TextDecoder()
  40. {
  41. MOZ_COUNT_DTOR(TextDecoder);
  42. }
  43. bool WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto, JS::MutableHandle<JSObject*> aReflector)
  44. {
  45. return TextDecoderBinding::Wrap(aCx, this, aGivenProto, aReflector);
  46. }
  47. /**
  48. * Validates provided label and throws an exception if invalid label.
  49. *
  50. * @param aLabel The encoding label (case insensitive) provided.
  51. * @param aFatal indicates whether to throw an 'EncodingError'
  52. * exception or not when decoding.
  53. * @return aRv EncodingError exception else null.
  54. */
  55. void Init(const nsAString& aLabel, const bool aFatal, ErrorResult& aRv);
  56. /**
  57. * Performs initialization with a Gecko-canonical encoding name (as opposed
  58. * to a label.)
  59. *
  60. * @param aEncoding A Gecko-canonical encoding name
  61. * @param aFatal indicates whether to throw an 'EncodingError'
  62. * exception or not when decoding.
  63. */
  64. void InitWithEncoding(const nsACString& aEncoding, const bool aFatal);
  65. /**
  66. * Return the encoding name.
  67. *
  68. * @param aEncoding, current encoding.
  69. */
  70. void GetEncoding(nsAString& aEncoding);
  71. /**
  72. * Decodes incoming byte stream of characters in charset indicated by
  73. * encoding.
  74. *
  75. * The encoding algorithm state is reset if aOptions.mStream is not set.
  76. *
  77. * If the fatal flag is set then a decoding error will throw EncodingError.
  78. * Else the decoder will return a decoded string with replacement
  79. * character(s) for unidentified character(s).
  80. *
  81. * @param aView, incoming byte stream of characters to be decoded to
  82. * to UTF-16 code points.
  83. * @param aOptions, indicates if streaming or not.
  84. * @param aOutDecodedString, decoded string of UTF-16 code points.
  85. * @param aRv, error result.
  86. */
  87. void Decode(const char* aInput, const int32_t aLength,
  88. const bool aStream, nsAString& aOutDecodedString,
  89. ErrorResult& aRv);
  90. void Decode(const Optional<ArrayBufferViewOrArrayBuffer>& aBuffer,
  91. const TextDecodeOptions& aOptions,
  92. nsAString& aOutDecodedString,
  93. ErrorResult& aRv);
  94. bool Fatal() const {
  95. return mFatal;
  96. }
  97. private:
  98. nsCString mEncoding;
  99. nsCOMPtr<nsIUnicodeDecoder> mDecoder;
  100. bool mFatal;
  101. };
  102. } // namespace dom
  103. } // namespace mozilla
  104. #endif // mozilla_dom_textdecoder_h_