Bug 795542 - Part 1: Create Text(En|De)coderBase. r=bent
authorMasatoshi Kimura <VYV03354@nifty.ne.jp>
Sat, 22 Dec 2012 09:15:43 +0900
changeset 126702 905b2cfe26aeda9da0d1519fdb0b303a4a64e60c
parent 126701 d7c7bc64a114b0609621362677eb43a0a454a870
child 126703 ac143af3f698c4dc79710b724180c160864ae11a
push id297
push userlsblakk@mozilla.com
push dateTue, 26 Mar 2013 17:28:00 +0000
treeherdermozilla-release@64d7b45c34e6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbent
bugs795542
milestone20.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 795542 - Part 1: Create Text(En|De)coderBase. r=bent
dom/encoding/Makefile.in
dom/encoding/TextDecoder.cpp
dom/encoding/TextDecoder.h
dom/encoding/TextDecoderBase.h
dom/encoding/TextEncoder.cpp
dom/encoding/TextEncoder.h
dom/encoding/TextEncoderBase.h
--- a/dom/encoding/Makefile.in
+++ b/dom/encoding/Makefile.in
@@ -19,17 +19,19 @@ FORCE_STATIC_LIB = 1
 DIRS = \
   test \
   $(NULL)
 
 EXPORTS_NAMESPACES = mozilla/dom
 EXPORTS_mozilla/dom = \
   EncodingUtils.h \
   TextDecoder.h \
+  TextDecoderBase.h \
   TextEncoder.h \
+  TextEncoderBase.h \
   $(NULL)
 
 CPPSRCS = \
 	EncodingUtils.cpp \
 	TextDecoder.cpp \
 	TextEncoder.cpp \
 	$(NULL)
 
--- a/dom/encoding/TextDecoder.cpp
+++ b/dom/encoding/TextDecoder.cpp
@@ -9,34 +9,33 @@
 #include "nsServiceManagerUtils.h"
 
 namespace mozilla {
 namespace dom {
 
 static const PRUnichar kReplacementChar = static_cast<PRUnichar>(0xFFFD);
 
 void
-TextDecoder::Init(const nsAString& aEncoding,
-                  const TextDecoderOptions& aFatal,
-                  ErrorResult& aRv)
+TextDecoderBase::Init(const nsAString& aEncoding, const bool aFatal,
+                      ErrorResult& aRv)
 {
   nsAutoString label(aEncoding);
   EncodingUtils::TrimSpaceCharacters(label);
 
   // Let encoding be the result of getting an encoding from label.
   // If encoding is failure, throw a TypeError.
   if (!EncodingUtils::FindEncodingForLabel(label, mEncoding)) {
     aRv.ThrowTypeError(MSG_ENCODING_NOT_SUPPORTED, &label);
     return;
   }
 
   // If the constructor is called with an options argument,
   // and the fatal property of the dictionary is set,
   // set the internal fatal flag of the decoder object.
-  mFatal = aFatal.mFatal;
+  mFatal = aFatal;
 
   // Create a decoder object for mEncoding.
   nsCOMPtr<nsICharsetConverterManager> ccm =
     do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID);
   if (!ccm) {
     aRv.Throw(NS_ERROR_UNEXPECTED);
     return;
   }
@@ -48,20 +47,20 @@ TextDecoder::Init(const nsAString& aEnco
   }
 
   if (mFatal) {
     mDecoder->SetInputErrorBehavior(nsIUnicodeDecoder::kOnError_Signal);
   }
 }
 
 void
-TextDecoder::Decode(const ArrayBufferView* aView,
-                    const TextDecodeOptions& aOptions,
-                    nsAString& aOutDecodedString,
-                    ErrorResult& aRv)
+TextDecoderBase::Decode(const ArrayBufferView* aView,
+                        const bool aStream,
+                        nsAString& aOutDecodedString,
+                        ErrorResult& aRv)
 {
   const char* data;
   int32_t length;
   // If view is not specified, let view be a Uint8Array of length 0.
   if (!aView) {
     data = EmptyCString().BeginReading();
     length = EmptyCString().Length();
   } else {
@@ -89,17 +88,17 @@ TextDecoder::Decode(const ArrayBufferVie
 
   rv = mDecoder->Convert(data, &length, buf, &outLen);
   MOZ_ASSERT(mFatal || rv != NS_ERROR_ILLEGAL_INPUT);
   buf[outLen] = 0;
   aOutDecodedString.Append(buf, outLen);
 
   // If the internal streaming flag of the decoder object is not set,
   // then reset the encoding algorithm state to the default values
-  if (!aOptions.mStream) {
+  if (!aStream) {
     mDecoder->Reset();
     if (rv == NS_OK_UDEC_MOREINPUT) {
       if (mFatal) {
         aRv.Throw(NS_ERROR_DOM_ENCODING_DECODE_ERR);
       } else {
         // Need to emit a decode error manually
         // to simulate the EOF handling of the Encoding spec.
         aOutDecodedString.Append(kReplacementChar);
@@ -108,17 +107,17 @@ TextDecoder::Decode(const ArrayBufferVie
   }
 
   if (NS_FAILED(rv)) {
     aRv.Throw(NS_ERROR_DOM_ENCODING_DECODE_ERR);
   }
 }
 
 void
-TextDecoder::GetEncoding(nsAString& aEncoding)
+TextDecoderBase::GetEncoding(nsAString& aEncoding)
 {
   CopyASCIItoUTF16(mEncoding, aEncoding);
   nsContentUtils::ASCIIToLower(aEncoding);
 }
 
 NS_IMPL_CYCLE_COLLECTING_ADDREF(TextDecoder)
 NS_IMPL_CYCLE_COLLECTING_RELEASE(TextDecoder)
 
--- a/dom/encoding/TextDecoder.h
+++ b/dom/encoding/TextDecoder.h
@@ -1,118 +1,74 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef mozilla_dom_textdecoder_h_
 #define mozilla_dom_textdecoder_h_
 
-#include "jsapi.h"
-#include "mozilla/dom/BindingUtils.h"
+#include "mozilla/dom/TextDecoderBase.h"
 #include "mozilla/dom/TextDecoderBinding.h"
-#include "mozilla/dom/TypedArray.h"
-#include "mozilla/ErrorResult.h"
-#include "nsIUnicodeDecoder.h"
-#include "nsString.h"
-
-#include "nsCOMPtr.h"
-#include "nsCycleCollectionParticipant.h"
 
 namespace mozilla {
 namespace dom {
 
-class TextDecoder : public nsISupports, public nsWrapperCache
+class TextDecoder MOZ_FINAL
+  : public nsISupports, public nsWrapperCache, public TextDecoderBase
 {
 public:
   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(TextDecoder)
 
   // The WebIDL constructor.
   static already_AddRefed<TextDecoder>
   Constructor(nsISupports* aGlobal,
               const nsAString& aEncoding,
-              const TextDecoderOptions& aFatal,
+              const TextDecoderOptions& aOptions,
               ErrorResult& aRv)
   {
     nsRefPtr<TextDecoder> txtDecoder = new TextDecoder(aGlobal);
-    txtDecoder->Init(aEncoding, aFatal, aRv);
+    txtDecoder->Init(aEncoding, aOptions.mFatal, aRv);
     if (aRv.Failed()) {
       return nullptr;
     }
     return txtDecoder.forget();
   }
 
   TextDecoder(nsISupports* aGlobal)
-    : mGlobal(aGlobal), mFatal(false)
+    : mGlobal(aGlobal)
   {
     MOZ_ASSERT(aGlobal);
     SetIsDOMBinding();
   }
 
   virtual
   ~TextDecoder()
   {}
 
   virtual JSObject*
-  WrapObject(JSContext* aCx, JSObject* aScope, bool* aTriedToWrap)
+  WrapObject(JSContext* aCx, JSObject* aScope, bool* aTriedToWrap) MOZ_OVERRIDE
   {
     return TextDecoderBinding::Wrap(aCx, aScope, this, aTriedToWrap);
   }
 
   nsISupports*
   GetParentObject()
   {
     return mGlobal;
   }
 
-  /**
-   * Return the encoding name.
-   *
-   * @param aEncoding, current encoding.
-   */
-  void GetEncoding(nsAString& aEncoding);
-
-  /**
-   * Decodes incoming byte stream of characters in charset indicated by
-   * encoding.
-   *
-   * The encoding algorithm state is reset if aOptions.stream is not set.
-   *
-   * If the fatal flag is set then a decoding error will throw EncodingError.
-   * Else the decoder will return a decoded string with replacement
-   * character(s) for unidentified character(s).
-   *
-   * @param      aView, incoming byte stream of characters to be decoded to
-   *                    to UTF-16 code points.
-   * @param      aOptions, indicates if streaming or not.
-   * @param      aOutDecodedString, decoded string of UTF-16 code points.
-   * @param      aRv, error result.
-   */
   void Decode(const ArrayBufferView* aView,
               const TextDecodeOptions& aOptions,
               nsAString& aOutDecodedString,
-              ErrorResult& aRv);
+              ErrorResult& aRv) {
+    return TextDecoderBase::Decode(aView, aOptions.mStream,
+                                   aOutDecodedString, aRv);
+  }
 
 private:
-  nsCString mEncoding;
-  nsCOMPtr<nsIUnicodeDecoder> mDecoder;
   nsCOMPtr<nsISupports> mGlobal;
-  bool mFatal;
-
-  /**
-   * Validates provided encoding and throws an exception if invalid encoding.
-   * If no encoding is provided then mEncoding is default initialised to "utf-8".
-   *
-   * @param aEncoding    Optional encoding (case insensitive) provided.
-   *                     Default value is "utf-8" if no encoding is provided.
-   * @param aFatal       aFatal, indicates whether to throw an 'EncodingError'
-   *                     exception or not.
-   * @return aRv         EncodingError exception else null.
-   */
-  void Init(const nsAString& aEncoding,
-            const TextDecoderOptions& aFatal,
-            ErrorResult& aRv);
 };
 
 } // dom
 } // mozilla
 
 #endif // mozilla_dom_textdecoder_h_
copy from dom/encoding/TextDecoder.h
copy to dom/encoding/TextDecoderBase.h
--- a/dom/encoding/TextDecoder.h
+++ b/dom/encoding/TextDecoderBase.h
@@ -1,118 +1,76 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
-#ifndef mozilla_dom_textdecoder_h_
-#define mozilla_dom_textdecoder_h_
+#ifndef mozilla_dom_textdecoderbase_h_
+#define mozilla_dom_textdecoderbase_h_
 
-#include "jsapi.h"
 #include "mozilla/dom/BindingUtils.h"
-#include "mozilla/dom/TextDecoderBinding.h"
 #include "mozilla/dom/TypedArray.h"
-#include "mozilla/ErrorResult.h"
 #include "nsIUnicodeDecoder.h"
-#include "nsString.h"
-
-#include "nsCOMPtr.h"
-#include "nsCycleCollectionParticipant.h"
 
 namespace mozilla {
+class ErrorResult;
+
 namespace dom {
 
-class TextDecoder : public nsISupports, public nsWrapperCache
+class TextDecoderBase
 {
-public:
-  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
-  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(TextDecoder)
-
-  // The WebIDL constructor.
-  static already_AddRefed<TextDecoder>
-  Constructor(nsISupports* aGlobal,
-              const nsAString& aEncoding,
-              const TextDecoderOptions& aFatal,
-              ErrorResult& aRv)
-  {
-    nsRefPtr<TextDecoder> txtDecoder = new TextDecoder(aGlobal);
-    txtDecoder->Init(aEncoding, aFatal, aRv);
-    if (aRv.Failed()) {
-      return nullptr;
-    }
-    return txtDecoder.forget();
-  }
-
-  TextDecoder(nsISupports* aGlobal)
-    : mGlobal(aGlobal), mFatal(false)
-  {
-    MOZ_ASSERT(aGlobal);
-    SetIsDOMBinding();
-  }
+protected:
+  TextDecoderBase()
+    : mFatal(false)
+  {}
 
   virtual
-  ~TextDecoder()
+  ~TextDecoderBase()
   {}
 
-  virtual JSObject*
-  WrapObject(JSContext* aCx, JSObject* aScope, bool* aTriedToWrap)
-  {
-    return TextDecoderBinding::Wrap(aCx, aScope, this, aTriedToWrap);
-  }
+  /**
+   * Validates provided encoding and throws an exception if invalid encoding.
+   * If no encoding is provided then mEncoding is default initialised to "utf-8".
+   *
+   * @param aEncoding    Optional encoding (case insensitive) provided.
+   *                     Default value is "utf-8" if no encoding is provided.
+   * @param aFatal       aFatal, indicates whether to throw an 'EncodingError'
+   *                     exception or not.
+   * @return aRv         EncodingError exception else null.
+   */
+  void Init(const nsAString& aEncoding, const bool aFatal, ErrorResult& aRv);
 
-  nsISupports*
-  GetParentObject()
-  {
-    return mGlobal;
-  }
-
+public:
   /**
    * Return the encoding name.
    *
    * @param aEncoding, current encoding.
    */
   void GetEncoding(nsAString& aEncoding);
 
   /**
    * Decodes incoming byte stream of characters in charset indicated by
    * encoding.
    *
-   * The encoding algorithm state is reset if aOptions.stream is not set.
+   * The encoding algorithm state is reset if aOptions.mStream is not set.
    *
    * If the fatal flag is set then a decoding error will throw EncodingError.
    * Else the decoder will return a decoded string with replacement
    * character(s) for unidentified character(s).
    *
    * @param      aView, incoming byte stream of characters to be decoded to
    *                    to UTF-16 code points.
    * @param      aOptions, indicates if streaming or not.
    * @param      aOutDecodedString, decoded string of UTF-16 code points.
    * @param      aRv, error result.
    */
-  void Decode(const ArrayBufferView* aView,
-              const TextDecodeOptions& aOptions,
-              nsAString& aOutDecodedString,
-              ErrorResult& aRv);
+  void Decode(const ArrayBufferView* aView, const bool aStream,
+              nsAString& aOutDecodedString, ErrorResult& aRv);
 
 private:
   nsCString mEncoding;
   nsCOMPtr<nsIUnicodeDecoder> mDecoder;
-  nsCOMPtr<nsISupports> mGlobal;
   bool mFatal;
-
-  /**
-   * Validates provided encoding and throws an exception if invalid encoding.
-   * If no encoding is provided then mEncoding is default initialised to "utf-8".
-   *
-   * @param aEncoding    Optional encoding (case insensitive) provided.
-   *                     Default value is "utf-8" if no encoding is provided.
-   * @param aFatal       aFatal, indicates whether to throw an 'EncodingError'
-   *                     exception or not.
-   * @return aRv         EncodingError exception else null.
-   */
-  void Init(const nsAString& aEncoding,
-            const TextDecoderOptions& aFatal,
-            ErrorResult& aRv);
 };
 
 } // dom
 } // mozilla
 
-#endif // mozilla_dom_textdecoder_h_
+#endif // mozilla_dom_textdecoderbase_h_
--- a/dom/encoding/TextEncoder.cpp
+++ b/dom/encoding/TextEncoder.cpp
@@ -7,18 +7,17 @@
 #include "nsContentUtils.h"
 #include "nsICharsetConverterManager.h"
 #include "nsServiceManagerUtils.h"
 
 namespace mozilla {
 namespace dom {
 
 void
-TextEncoder::Init(const nsAString& aEncoding,
-                  ErrorResult& aRv)
+TextEncoderBase::Init(const nsAString& aEncoding, ErrorResult& aRv)
 {
   nsAutoString label(aEncoding);
   EncodingUtils::TrimSpaceCharacters(label);
 
   // Let encoding be the result of getting an encoding from label.
   // If encoding is failure, or is none of utf-8, utf-16, and utf-16be,
   // throw a TypeError.
   if (!EncodingUtils::FindEncodingForLabel(label, mEncoding)) {
@@ -44,20 +43,20 @@ TextEncoder::Init(const nsAString& aEnco
   ccm->GetUnicodeEncoderRaw(mEncoding.get(), getter_AddRefs(mEncoder));
   if (!mEncoder) {
     aRv.Throw(NS_ERROR_UNEXPECTED);
     return;
   }
 }
 
 JSObject*
-TextEncoder::Encode(JSContext* aCx,
-                    const nsAString& aString,
-                    const TextEncodeOptions& aOptions,
-                    ErrorResult& aRv)
+TextEncoderBase::Encode(JSContext* aCx,
+                        const nsAString& aString,
+                        const bool aStream,
+                        ErrorResult& aRv)
 {
   // Run the steps of the encoding algorithm.
   int32_t srcLen = aString.Length();
   int32_t maxLen;
   const PRUnichar* data = PromiseFlatString(aString).get();
   nsresult rv = mEncoder->GetMaxLength(data, srcLen, &maxLen);
   if (NS_FAILED(rv)) {
     aRv.Throw(rv);
@@ -72,43 +71,42 @@ TextEncoder::Encode(JSContext* aCx,
     return nullptr;
   }
 
   int32_t dstLen = maxLen;
   rv = mEncoder->Convert(data, &srcLen, buf, &dstLen);
 
   // If the internal streaming flag is not set, then reset
   // the encoding algorithm state to the default values for encoding.
-  if (!aOptions.mStream) {
+  if (!aStream) {
     int32_t finishLen = maxLen - dstLen;
     rv = mEncoder->Finish(buf + dstLen, &finishLen);
     if (NS_SUCCEEDED(rv)) {
       dstLen += finishLen;
     }
   }
 
   JSObject* outView = nullptr;
   if (NS_SUCCEEDED(rv)) {
     buf[dstLen] = '\0';
-    outView = Uint8Array::Create(aCx, this, dstLen,
-                                 reinterpret_cast<uint8_t*>(buf.get()));
+    outView = CreateUint8Array(aCx, buf, dstLen);
     if (!outView) {
       aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
       return nullptr;
     }
   }
 
   if (NS_FAILED(rv)) {
     aRv.Throw(rv);
   }
   return outView;
 }
 
 void
-TextEncoder::GetEncoding(nsAString& aEncoding)
+TextEncoderBase::GetEncoding(nsAString& aEncoding)
 {
   CopyASCIItoUTF16(mEncoding, aEncoding);
   nsContentUtils::ASCIIToLower(aEncoding);
 }
 
 NS_IMPL_CYCLE_COLLECTING_ADDREF(TextEncoder)
 NS_IMPL_CYCLE_COLLECTING_RELEASE(TextEncoder)
 
--- a/dom/encoding/TextEncoder.h
+++ b/dom/encoding/TextEncoder.h
@@ -1,30 +1,23 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef mozilla_dom_textencoder_h_
 #define mozilla_dom_textencoder_h_
 
-#include "jsapi.h"
-#include "mozilla/dom/BindingUtils.h"
+#include "mozilla/dom/TextEncoderBase.h"
 #include "mozilla/dom/TextEncoderBinding.h"
-#include "mozilla/dom/TypedArray.h"
-#include "mozilla/ErrorResult.h"
-#include "nsIUnicodeEncoder.h"
-#include "nsString.h"
-
-#include "nsCOMPtr.h"
-#include "nsCycleCollectionParticipant.h"
 
 namespace mozilla {
 namespace dom {
 
-class TextEncoder : public nsISupports, public nsWrapperCache
+class TextEncoder MOZ_FINAL
+  : public nsISupports, public nsWrapperCache, public TextEncoderBase
 {
 public:
   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(TextEncoder)
 
   // The WebIDL constructor.
   static already_AddRefed<TextEncoder>
   Constructor(nsISupports* aGlobal,
@@ -46,63 +39,42 @@ public:
     SetIsDOMBinding();
   }
 
   virtual
   ~TextEncoder()
   {}
 
   virtual JSObject*
-  WrapObject(JSContext* aCx, JSObject* aScope, bool* aTriedToWrap)
+  WrapObject(JSContext* aCx, JSObject* aScope, bool* aTriedToWrap) MOZ_OVERRIDE
   {
     return TextEncoderBinding::Wrap(aCx, aScope, this, aTriedToWrap);
   }
 
   nsISupports*
   GetParentObject()
   {
     return mGlobal;
   }
 
-  /**
-   * Return the encoding name.
-   *
-   * @param aEncoding, current encoding.
-   */
-  void GetEncoding(nsAString& aEncoding);
-
-  /**
-   * Encodes incoming utf-16 code units/ DOM string to the requested encoding.
-   *
-   * @param aCx        Javascript context.
-   * @param aString    utf-16 code units to be encoded.
-   * @param aOptions   Streaming option. Initialised by default to false.
-   *                   If the streaming option is false, then the encoding
-   *                   algorithm state will get reset. If set to true then
-   *                   the previous encoding is reused/continued.
-   * @return JSObject* The Uint8Array wrapped in a JS object.
-   */
   JSObject* Encode(JSContext* aCx,
                    const nsAString& aString,
                    const TextEncodeOptions& aOptions,
-                   ErrorResult& aRv);
-private:
-  nsCString mEncoding;
-  nsCOMPtr<nsIUnicodeEncoder> mEncoder;
-  nsCOMPtr<nsISupports> mGlobal;
+                   ErrorResult& aRv) {
+    return TextEncoderBase::Encode(aCx, aString, aOptions.mStream, aRv);
+  }
 
-  /**
-   * Validates provided encoding and throws an exception if invalid encoding.
-   * If no encoding is provided then mEncoding is default initialised to "utf-8".
-   *
-   * @param aEncoding    Optional encoding (case insensitive) provided.
-   *                     (valid values are "utf-8", "utf-16", "utf-16be")
-   *                     Default value is "utf-8" if no encoding is provided.
-   * @return aRv         EncodingError exception else null.
-   */
-  void Init(const nsAString& aEncoding,
-            ErrorResult& aRv);
+protected:
+  virtual JSObject*
+  CreateUint8Array(JSContext* aCx, char* aBuf, uint32_t aLen) MOZ_OVERRIDE
+  {
+    return Uint8Array::Create(aCx, this, aLen,
+                              reinterpret_cast<uint8_t*>(aBuf));
+  }
+
+private:
+  nsCOMPtr<nsISupports> mGlobal;
 };
 
 } // dom
 } // mozilla
 
 #endif // mozilla_dom_textencoder_h_
copy from dom/encoding/TextEncoder.h
copy to dom/encoding/TextEncoderBase.h
--- a/dom/encoding/TextEncoder.h
+++ b/dom/encoding/TextEncoderBase.h
@@ -1,72 +1,46 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
-#ifndef mozilla_dom_textencoder_h_
-#define mozilla_dom_textencoder_h_
+#ifndef mozilla_dom_textencoderbase_h_
+#define mozilla_dom_textencoderbase_h_
 
-#include "jsapi.h"
 #include "mozilla/dom/BindingUtils.h"
-#include "mozilla/dom/TextEncoderBinding.h"
 #include "mozilla/dom/TypedArray.h"
-#include "mozilla/ErrorResult.h"
 #include "nsIUnicodeEncoder.h"
-#include "nsString.h"
-
-#include "nsCOMPtr.h"
-#include "nsCycleCollectionParticipant.h"
 
 namespace mozilla {
+class ErrorResult;
+
 namespace dom {
 
-class TextEncoder : public nsISupports, public nsWrapperCache
+class TextEncoderBase
 {
-public:
-  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
-  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(TextEncoder)
-
-  // The WebIDL constructor.
-  static already_AddRefed<TextEncoder>
-  Constructor(nsISupports* aGlobal,
-              const nsAString& aEncoding,
-              ErrorResult& aRv)
-  {
-    nsRefPtr<TextEncoder> txtEncoder = new TextEncoder(aGlobal);
-    txtEncoder->Init(aEncoding, aRv);
-    if (aRv.Failed()) {
-      return nullptr;
-    }
-    return txtEncoder.forget();
-  }
-
-  TextEncoder(nsISupports* aGlobal)
-    : mGlobal(aGlobal)
-  {
-    MOZ_ASSERT(aGlobal);
-    SetIsDOMBinding();
-  }
+protected:
+  TextEncoderBase()
+  {}
 
   virtual
-  ~TextEncoder()
+  ~TextEncoderBase()
   {}
 
-  virtual JSObject*
-  WrapObject(JSContext* aCx, JSObject* aScope, bool* aTriedToWrap)
-  {
-    return TextEncoderBinding::Wrap(aCx, aScope, this, aTriedToWrap);
-  }
+  /**
+   * Validates provided encoding and throws an exception if invalid encoding.
+   * If no encoding is provided then mEncoding is default initialised to "utf-8".
+   *
+   * @param aEncoding    Optional encoding (case insensitive) provided.
+   *                     (valid values are "utf-8", "utf-16", "utf-16be")
+   *                     Default value is "utf-8" if no encoding is provided.
+   * @return aRv         EncodingError exception else null.
+   */
+  void Init(const nsAString& aEncoding, ErrorResult& aRv);
 
-  nsISupports*
-  GetParentObject()
-  {
-    return mGlobal;
-  }
-
+public:
   /**
    * Return the encoding name.
    *
    * @param aEncoding, current encoding.
    */
   void GetEncoding(nsAString& aEncoding);
 
   /**
@@ -75,34 +49,24 @@ public:
    * @param aCx        Javascript context.
    * @param aString    utf-16 code units to be encoded.
    * @param aOptions   Streaming option. Initialised by default to false.
    *                   If the streaming option is false, then the encoding
    *                   algorithm state will get reset. If set to true then
    *                   the previous encoding is reused/continued.
    * @return JSObject* The Uint8Array wrapped in a JS object.
    */
-  JSObject* Encode(JSContext* aCx,
-                   const nsAString& aString,
-                   const TextEncodeOptions& aOptions,
-                   ErrorResult& aRv);
+  JSObject* Encode(JSContext* aCx, const nsAString& aString,
+                   const bool aStream, ErrorResult& aRv);
+
+protected:
+  virtual JSObject*
+  CreateUint8Array(JSContext* aCx, char* aBuf, uint32_t aLen) = 0;
+
 private:
   nsCString mEncoding;
   nsCOMPtr<nsIUnicodeEncoder> mEncoder;
-  nsCOMPtr<nsISupports> mGlobal;
-
-  /**
-   * Validates provided encoding and throws an exception if invalid encoding.
-   * If no encoding is provided then mEncoding is default initialised to "utf-8".
-   *
-   * @param aEncoding    Optional encoding (case insensitive) provided.
-   *                     (valid values are "utf-8", "utf-16", "utf-16be")
-   *                     Default value is "utf-8" if no encoding is provided.
-   * @return aRv         EncodingError exception else null.
-   */
-  void Init(const nsAString& aEncoding,
-            ErrorResult& aRv);
 };
 
 } // dom
 } // mozilla
 
-#endif // mozilla_dom_textencoder_h_
+#endif // mozilla_dom_textencoderbase_h_