TextEncoder.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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_textencoder_h_
  6. #define mozilla_dom_textencoder_h_
  7. #include "mozilla/dom/NonRefcountedDOMObject.h"
  8. #include "mozilla/dom/TextEncoderBinding.h"
  9. #include "mozilla/dom/TypedArray.h"
  10. #include "nsIUnicodeEncoder.h"
  11. namespace mozilla {
  12. class ErrorResult;
  13. namespace dom {
  14. class TextEncoder final : public NonRefcountedDOMObject
  15. {
  16. public:
  17. // The WebIDL constructor.
  18. static TextEncoder*
  19. Constructor(const GlobalObject& aGlobal,
  20. ErrorResult& aRv)
  21. {
  22. nsAutoPtr<TextEncoder> txtEncoder(new TextEncoder());
  23. txtEncoder->Init();
  24. return txtEncoder.forget();
  25. }
  26. TextEncoder()
  27. {
  28. }
  29. virtual
  30. ~TextEncoder()
  31. {}
  32. bool WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto, JS::MutableHandle<JSObject*> aReflector)
  33. {
  34. return TextEncoderBinding::Wrap(aCx, this, aGivenProto, aReflector);
  35. }
  36. protected:
  37. void Init();
  38. public:
  39. /**
  40. * Return the encoding name.
  41. *
  42. * @param aEncoding, current encoding.
  43. */
  44. void GetEncoding(nsAString& aEncoding);
  45. /**
  46. * Encodes incoming utf-16 code units/ DOM string to utf-8.
  47. *
  48. * @param aCx Javascript context.
  49. * @param aObj the wrapper of the TextEncoder
  50. * @param aString utf-16 code units to be encoded.
  51. * @return JSObject* The Uint8Array wrapped in a JS object. Returned via
  52. * the aRetval out param.
  53. */
  54. void Encode(JSContext* aCx,
  55. JS::Handle<JSObject*> aObj,
  56. const nsAString& aString,
  57. JS::MutableHandle<JSObject*> aRetval,
  58. ErrorResult& aRv);
  59. private:
  60. nsCOMPtr<nsIUnicodeEncoder> mEncoder;
  61. };
  62. } // namespace dom
  63. } // namespace mozilla
  64. #endif // mozilla_dom_textencoder_h_