Bug 919935 - Add a deCOMtaminated way to instantiate nsIUnicodeDecoders and nsIUnicodeEncoders. r=emk.
authorHenri Sivonen <hsivonen@hsivonen.fi>
Tue, 26 Nov 2013 09:31:52 +0200
changeset 172095 04500118250742378ac412dcbfc50509d26b4cc1
parent 172094 577cdd1d7dc39cb514bb927814e11529fe4fbb45
child 172096 966f9d2901e9e45e5ea4f7587c02c991d2d9a0c8
push id3224
push userlsblakk@mozilla.com
push dateTue, 04 Feb 2014 01:06:49 +0000
treeherdermozilla-beta@60c04d0987f1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersemk
bugs919935
milestone28.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 919935 - Add a deCOMtaminated way to instantiate nsIUnicodeDecoders and nsIUnicodeEncoders. r=emk.
content/base/src/EventSource.cpp
content/base/src/WebSocket.cpp
content/base/src/nsContentUtils.cpp
content/base/src/nsDOMFile.cpp
content/base/src/nsDOMFileReader.cpp
content/base/src/nsScriptLoader.cpp
content/base/src/nsXMLHttpRequest.cpp
content/html/content/src/nsFormSubmission.cpp
dom/encoding/EncodingUtils.cpp
dom/encoding/EncodingUtils.h
dom/encoding/TextDecoder.cpp
dom/encoding/TextEncoder.cpp
dom/src/json/nsJSON.cpp
intl/uconv/idl/nsICharsetConverterManager.idl
parser/html/nsHtml5StreamParser.cpp
parser/htmlparser/src/nsParser.cpp
parser/htmlparser/src/nsParser.h
parser/htmlparser/src/nsParserModule.cpp
parser/htmlparser/src/nsScanner.cpp
widget/cocoa/nsDragService.mm
widget/windows/nsFilePicker.cpp
widget/windows/nsFilePicker.h
xpfe/appshell/src/nsAppShellService.cpp
--- a/content/base/src/EventSource.cpp
+++ b/content/base/src/EventSource.cpp
@@ -19,17 +19,17 @@
 #include "nsContentPolicyUtils.h"
 #include "nsIStringBundle.h"
 #include "nsIConsoleService.h"
 #include "nsIObserverService.h"
 #include "nsIScriptObjectPrincipal.h"
 #include "nsJSUtils.h"
 #include "nsIAsyncVerifyRedirectCallback.h"
 #include "nsIScriptError.h"
-#include "nsICharsetConverterManager.h"
+#include "mozilla/dom/EncodingUtils.h"
 #include "nsIChannelPolicy.h"
 #include "nsIContentSecurityPolicy.h"
 #include "nsContentUtils.h"
 #include "nsCxPusher.h"
 #include "mozilla/Preferences.h"
 #include "xpcpublic.h"
 #include "nsCrossSiteListenerProxy.h"
 #include "nsWrapperCacheInlines.h"
@@ -259,23 +259,17 @@ EventSource::Init(nsISupports* aOwner,
   mOriginalURL = NS_ConvertUTF8toUTF16(spec);
   mSrc = srcURI;
   mOrigin = origin;
 
   mReconnectionTime =
     Preferences::GetInt("dom.server-events.default-reconnection-time",
                         DEFAULT_RECONNECTION_TIME_VALUE);
 
-  nsCOMPtr<nsICharsetConverterManager> convManager =
-    do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  rv = convManager->GetUnicodeDecoderRaw("UTF-8",
-                                         getter_AddRefs(mUnicodeDecoder));
-  NS_ENSURE_SUCCESS(rv, rv);
+  mUnicodeDecoder = EncodingUtils::DecoderForEncoding("UTF-8");
 
   // the constructor should throw a SYNTAX_ERROR only if it fails resolving the
   // url parameter, so we don't care about the InitChannelAndRequestEventSource
   // result.
   InitChannelAndRequestEventSource();
 
   return NS_OK;
 }
--- a/content/base/src/WebSocket.cpp
+++ b/content/base/src/WebSocket.cpp
@@ -16,17 +16,16 @@
 #include "nsXPCOM.h"
 #include "nsIXPConnect.h"
 #include "nsContentUtils.h"
 #include "nsCxPusher.h"
 #include "nsEventDispatcher.h"
 #include "nsError.h"
 #include "nsIScriptObjectPrincipal.h"
 #include "nsIURL.h"
-#include "nsICharsetConverterManager.h"
 #include "nsIUnicodeEncoder.h"
 #include "nsThreadUtils.h"
 #include "nsIDOMMessageEvent.h"
 #include "nsIPromptFactory.h"
 #include "nsIWindowWatcher.h"
 #include "nsIPrompt.h"
 #include "nsIStringBundle.h"
 #include "nsIConsoleService.h"
--- a/content/base/src/nsContentUtils.cpp
+++ b/content/base/src/nsContentUtils.cpp
@@ -78,17 +78,16 @@
 #include "nsGkAtoms.h"
 #include "nsHostObjectProtocolHandler.h"
 #include "nsHtml5Module.h"
 #include "nsHtml5StringParser.h"
 #include "nsIAsyncVerifyRedirectCallback.h"
 #include "nsICategoryManager.h"
 #include "nsIChannelEventSink.h"
 #include "nsIChannelPolicy.h"
-#include "nsICharsetConverterManager.h"
 #include "nsICharsetDetectionObserver.h"
 #include "nsICharsetDetector.h"
 #include "nsIChromeRegistry.h"
 #include "nsIConsoleService.h"
 #include "nsIContent.h"
 #include "nsIContentSecurityPolicy.h"
 #include "nsIContentSink.h"
 #include "nsIContentViewer.h"
--- a/content/base/src/nsDOMFile.cpp
+++ b/content/base/src/nsDOMFile.cpp
@@ -7,17 +7,16 @@
 #include "nsDOMFile.h"
 
 #include "nsCExternalHandlerService.h"
 #include "nsContentCID.h"
 #include "nsContentUtils.h"
 #include "nsDOMClassInfoID.h"
 #include "nsError.h"
 #include "nsICharsetDetector.h"
-#include "nsICharsetConverterManager.h"
 #include "nsIClassInfo.h"
 #include "nsIConverterInputStream.h"
 #include "nsIDocument.h"
 #include "nsIFileStreams.h"
 #include "nsIInputStream.h"
 #include "nsIIPCSerializableInputStream.h"
 #include "nsIMemoryReporter.h"
 #include "nsIMIMEService.h"
--- a/content/base/src/nsDOMFileReader.cpp
+++ b/content/base/src/nsDOMFileReader.cpp
@@ -5,17 +5,16 @@
 
 #include "nsDOMFileReader.h"
 
 #include "nsContentCID.h"
 #include "nsContentUtils.h"
 #include "nsDOMClassInfoID.h"
 #include "nsDOMFile.h"
 #include "nsError.h"
-#include "nsICharsetConverterManager.h"
 #include "nsIConverterInputStream.h"
 #include "nsIDocument.h"
 #include "nsIFile.h"
 #include "nsIFileStreams.h"
 #include "nsIInputStream.h"
 #include "nsIMIMEService.h"
 #include "nsIUnicodeDecoder.h"
 #include "nsNetCID.h"
@@ -530,24 +529,19 @@ nsDOMFileReader::GetAsDataURL(nsIDOMBlob
 
 nsresult
 nsDOMFileReader::ConvertStream(const char *aFileData,
                                uint32_t aDataLen,
                                const char *aCharset,
                                nsAString &aResult)
 {
   nsresult rv;
-  nsCOMPtr<nsICharsetConverterManager> charsetConverter = 
-    do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
-  NS_ENSURE_SUCCESS(rv, rv);
 
-  nsCOMPtr<nsIUnicodeDecoder> unicodeDecoder;
-  rv = charsetConverter->GetUnicodeDecoderRaw(aCharset,
-                                              getter_AddRefs(unicodeDecoder));
-  NS_ENSURE_SUCCESS(rv, rv);
+  nsCOMPtr<nsIUnicodeDecoder> unicodeDecoder =
+    EncodingUtils::DecoderForEncoding(aCharset);
 
   int32_t destLength;
   rv = unicodeDecoder->GetMaxLength(aFileData, aDataLen, &destLength);
   NS_ENSURE_SUCCESS(rv, rv);
 
   if (!aResult.SetLength(destLength, fallible_t()))
     return NS_ERROR_OUT_OF_MEMORY;
 
--- a/content/base/src/nsScriptLoader.cpp
+++ b/content/base/src/nsScriptLoader.cpp
@@ -6,17 +6,16 @@
 
 /*
  * A class that handles loading and evaluation of <script> elements.
  */
 
 #include "jsapi.h"
 #include "jsfriendapi.h"
 #include "nsScriptLoader.h"
-#include "nsICharsetConverterManager.h"
 #include "nsIUnicodeDecoder.h"
 #include "nsIContent.h"
 #include "mozilla/dom/Element.h"
 #include "nsGkAtoms.h"
 #include "nsNetUtil.h"
 #include "nsIJSRuntimeService.h"
 #include "nsIScriptGlobalObject.h"
 #include "nsIScriptContext.h"
@@ -1177,56 +1176,48 @@ nsScriptLoader::ConvertToUTF16(nsIChanne
   // The encoding info precedence is as follows from high to low:
   // The BOM
   // HTTP Content-Type (if name recognized)
   // charset attribute (if name recognized)
   // The encoding of the document
 
   nsAutoCString charset;
 
-  nsCOMPtr<nsICharsetConverterManager> charsetConv =
-    do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID);
-
   nsCOMPtr<nsIUnicodeDecoder> unicodeDecoder;
 
   if (DetectByteOrderMark(aData, aLength, charset)) {
     // charset is now "UTF-8" or "UTF-16". The UTF-16 decoder will re-sniff
     // the BOM for endianness. Both the UTF-16 and the UTF-8 decoder will
     // take care of swallowing the BOM.
-    charsetConv->GetUnicodeDecoderRaw(charset.get(),
-                                      getter_AddRefs(unicodeDecoder));
+    unicodeDecoder = EncodingUtils::DecoderForEncoding(charset);
   }
 
   if (!unicodeDecoder &&
       aChannel &&
       NS_SUCCEEDED(aChannel->GetContentCharset(charset)) &&
       EncodingUtils::FindEncodingForLabel(charset, charset)) {
-    charsetConv->GetUnicodeDecoderRaw(charset.get(),
-                                      getter_AddRefs(unicodeDecoder));
+    unicodeDecoder = EncodingUtils::DecoderForEncoding(charset);
   }
 
   if (!unicodeDecoder &&
       EncodingUtils::FindEncodingForLabel(aHintCharset, charset)) {
-    charsetConv->GetUnicodeDecoderRaw(charset.get(),
-                                      getter_AddRefs(unicodeDecoder));
+    unicodeDecoder = EncodingUtils::DecoderForEncoding(charset);
   }
 
   if (!unicodeDecoder && aDocument) {
     charset = aDocument->GetDocumentCharacterSet();
-    charsetConv->GetUnicodeDecoderRaw(charset.get(),
-                                      getter_AddRefs(unicodeDecoder));
+    unicodeDecoder = EncodingUtils::DecoderForEncoding(charset);
   }
 
   if (!unicodeDecoder) {
     // Curiously, there are various callers that don't pass aDocument. The
     // fallback in the old code was ISO-8859-1, which behaved like
     // windows-1252. Saying windows-1252 for clarity and for compliance
     // with the Encoding Standard.
-    charsetConv->GetUnicodeDecoderRaw("windows-1252",
-                                      getter_AddRefs(unicodeDecoder));
+    unicodeDecoder = EncodingUtils::DecoderForEncoding("windows-1252");
   }
 
   int32_t unicodeLength = 0;
 
   nsresult rv =
     unicodeDecoder->GetMaxLength(reinterpret_cast<const char*>(aData),
                                  aLength, &unicodeLength);
   NS_ENSURE_SUCCESS(rv, rv);
--- a/content/base/src/nsXMLHttpRequest.cpp
+++ b/content/base/src/nsXMLHttpRequest.cpp
@@ -5,17 +5,16 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsXMLHttpRequest.h"
 
 #include "mozilla/dom/XMLHttpRequestUploadBinding.h"
 #include "mozilla/MemoryReporting.h"
 #include "mozilla/Util.h"
 #include "nsDOMBlobBuilder.h"
-#include "nsICharsetConverterManager.h"
 #include "nsIDOMDocument.h"
 #include "nsIDOMProgressEvent.h"
 #include "nsIJARChannel.h"
 #include "nsLayoutCID.h"
 #include "nsReadableUtils.h"
 
 #include "nsIURI.h"
 #include "nsILoadGroup.h"
@@ -53,16 +52,17 @@
 #include "nsAsyncRedirectVerifyHelper.h"
 #include "nsStringBuffer.h"
 #include "nsDOMFile.h"
 #include "nsIFileChannel.h"
 #include "mozilla/Telemetry.h"
 #include "jsfriendapi.h"
 #include "GeckoProfiler.h"
 #include "mozilla/dom/EncodingUtils.h"
+#include "nsIUnicodeDecoder.h"
 #include "mozilla/dom/XMLHttpRequestBinding.h"
 #include "mozilla/Attributes.h"
 #include "nsIPermissionManager.h"
 #include "nsMimeTypes.h"
 #include "nsIHttpChannelInternal.h"
 #include "nsCharSeparatedTokenizer.h"
 #include "nsFormData.h"
 #include "nsStreamListenerWrapper.h"
@@ -628,23 +628,19 @@ nsXMLHttpRequest::DetectCharset()
 
   if (mResponseType == XML_HTTP_RESPONSE_TYPE_JSON &&
       !mResponseCharset.EqualsLiteral("UTF-8")) {
     // The XHR spec says only UTF-8 is supported for responseType == "json"
     LogMessage("JSONCharsetWarning", GetOwner());
     mResponseCharset.AssignLiteral("UTF-8");
   }
 
-  nsresult rv;
-  nsCOMPtr<nsICharsetConverterManager> ccm =
-    do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  return ccm->GetUnicodeDecoderRaw(mResponseCharset.get(),
-                                   getter_AddRefs(mDecoder));
+  mDecoder = EncodingUtils::DecoderForEncoding(mResponseCharset);
+
+  return NS_OK;
 }
 
 nsresult
 nsXMLHttpRequest::AppendToResponseText(const char * aSrcBuffer,
                                        uint32_t aSrcBufferLen)
 {
   NS_ENSURE_STATE(mDecoder);
 
@@ -719,30 +715,17 @@ nsXMLHttpRequest::GetResponseText(nsStri
     aResponseText = mResponseText;
     return;
   }
 
   if (mResponseCharset != mResponseXML->GetDocumentCharacterSet()) {
     mResponseCharset = mResponseXML->GetDocumentCharacterSet();
     mResponseText.Truncate();
     mResponseBodyDecodedPos = 0;
-
-    nsresult rv;
-    nsCOMPtr<nsICharsetConverterManager> ccm =
-      do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
-    if (NS_FAILED(rv)) {
-      aRv.Throw(rv);
-      return;
-    }
-
-    aRv = ccm->GetUnicodeDecoderRaw(mResponseCharset.get(),
-                                    getter_AddRefs(mDecoder));
-    if (aRv.Failed()) {
-      return;
-    }
+    mDecoder = EncodingUtils::DecoderForEncoding(mResponseCharset);
   }
 
   NS_ASSERTION(mResponseBodyDecodedPos < mResponseBody.Length(),
                "Unexpected mResponseBodyDecodedPos");
   aRv = AppendToResponseText(mResponseBody.get() + mResponseBodyDecodedPos,
                              mResponseBody.Length() - mResponseBodyDecodedPos);
   if (aRv.Failed()) {
     return;
--- a/content/html/content/src/nsFormSubmission.cpp
+++ b/content/html/content/src/nsFormSubmission.cpp
@@ -22,17 +22,16 @@
 #include "nsIFile.h"
 #include "nsIDOMFile.h"
 #include "nsDirectoryServiceDefs.h"
 #include "nsStringStream.h"
 #include "nsIURI.h"
 #include "nsIURL.h"
 #include "nsNetUtil.h"
 #include "nsLinebreakConverter.h"
-#include "nsICharsetConverterManager.h"
 #include "nsEscape.h"
 #include "nsUnicharUtils.h"
 #include "nsIMultiplexInputStream.h"
 #include "nsIMIMEInputStream.h"
 #include "nsIMIMEService.h"
 #include "nsIConsoleService.h"
 #include "nsIScriptError.h"
 #include "nsIStringBundle.h"
--- a/dom/encoding/EncodingUtils.cpp
+++ b/dom/encoding/EncodingUtils.cpp
@@ -1,16 +1,19 @@
 /* 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/. */
 
 #include "mozilla/dom/EncodingUtils.h"
 
 #include "mozilla/Util.h" // ArrayLength
 #include "nsUConvPropertySearch.h"
+#include "nsIUnicodeDecoder.h"
+#include "nsIUnicodeEncoder.h"
+#include "nsComponentManagerUtils.h"
 
 namespace mozilla {
 namespace dom {
 
 static const char* labelsEncodings[][3] = {
 #include "labelsencodings.properties.h"
 };
 
@@ -39,10 +42,32 @@ EncodingUtils::IsAsciiCompatible(const n
            aPreferredName.LowerCaseEqualsLiteral("utf-16be") ||
            aPreferredName.LowerCaseEqualsLiteral("utf-16le") ||
            aPreferredName.LowerCaseEqualsLiteral("replacement") ||
            aPreferredName.LowerCaseEqualsLiteral("hz-gb-2312") ||
            aPreferredName.LowerCaseEqualsLiteral("utf-7") ||
            aPreferredName.LowerCaseEqualsLiteral("x-imap4-modified-utf7"));
 }
 
+already_AddRefed<nsIUnicodeDecoder>
+EncodingUtils::DecoderForEncoding(const nsACString& aEncoding)
+{
+  nsAutoCString contractId(NS_UNICODEDECODER_CONTRACTID_BASE);
+  contractId.Append(aEncoding);
+
+  nsCOMPtr<nsIUnicodeDecoder> decoder = do_CreateInstance(contractId.get());
+  MOZ_ASSERT(decoder, "Tried to create decoder for unknown encoding.");
+  return decoder.forget();
+}
+
+already_AddRefed<nsIUnicodeEncoder>
+EncodingUtils::EncoderForEncoding(const nsACString& aEncoding)
+{
+  nsAutoCString contractId(NS_UNICODEENCODER_CONTRACTID_BASE);
+  contractId.Append(aEncoding);
+
+  nsCOMPtr<nsIUnicodeEncoder> encoder = do_CreateInstance(contractId.get());
+  MOZ_ASSERT(encoder, "Tried to create encoder for unknown encoding.");
+  return encoder.forget();
+}
+
 } // namespace dom
 } // namespace mozilla
--- a/dom/encoding/EncodingUtils.h
+++ b/dom/encoding/EncodingUtils.h
@@ -3,16 +3,19 @@
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef mozilla_dom_encodingutils_h_
 #define mozilla_dom_encodingutils_h_
 
 #include "nsDataHashtable.h"
 #include "nsString.h"
 
+class nsIUnicodeDecoder;
+class nsIUnicodeEncoder;
+
 namespace mozilla {
 namespace dom {
 
 class EncodingUtils
 {
 public:
 
   /**
@@ -57,16 +60,60 @@ public:
    * Check is the encoding is ASCII-compatible in the sense that Basic Latin
    * encodes to ASCII bytes. (The reverse may not be true!)
    *
    * @param aPreferredName a preferred encoding label
    * @return whether the encoding is ASCII-compatible
    */
   static bool IsAsciiCompatible(const nsACString& aPreferredName);
 
+  /**
+   * Instantiates a decoder for an encoding. The input must be a
+   * Gecko-canonical encoding name.
+   * @param aEncoding a Gecko-canonical encoding name
+   * @return a decoder
+   */
+  static already_AddRefed<nsIUnicodeDecoder>
+  DecoderForEncoding(const char* aEncoding)
+  {
+    nsDependentCString encoding(aEncoding);
+    return DecoderForEncoding(encoding);
+  }
+
+  /**
+   * Instantiates a decoder for an encoding. The input must be a
+   * Gecko-canonical encoding name
+   * @param aEncoding a Gecko-canonical encoding name
+   * @return a decoder
+   */
+  static already_AddRefed<nsIUnicodeDecoder>
+  DecoderForEncoding(const nsACString& aEncoding);
+
+  /**
+   * Instantiates an encoder for an encoding. The input must be a
+   * Gecko-canonical encoding name.
+   * @param aEncoding a Gecko-canonical encoding name
+   * @return an encoder
+   */
+  static already_AddRefed<nsIUnicodeEncoder>
+  EncoderForEncoding(const char* aEncoding)
+  {
+    nsDependentCString encoding(aEncoding);
+    return EncoderForEncoding(encoding);
+  }
+
+  /**
+   * Instantiates an encoder for an encoding. The input must be a
+   * Gecko-canonical encoding name.
+   * @param aEncoding a Gecko-canonical encoding name
+   * @return an encoder
+   */
+  static already_AddRefed<nsIUnicodeEncoder>
+  EncoderForEncoding(const nsACString& aEncoding);
+
 private:
   EncodingUtils() MOZ_DELETE;
 };
 
 } // dom
 } // mozilla
 
 #endif // mozilla_dom_encodingutils_h_
--- a/dom/encoding/TextDecoder.cpp
+++ b/dom/encoding/TextDecoder.cpp
@@ -1,17 +1,15 @@
 /* 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/. */
 
 #include "mozilla/dom/TextDecoder.h"
 #include "mozilla/dom/EncodingUtils.h"
 #include "nsContentUtils.h"
-#include "nsICharsetConverterManager.h"
-#include "nsServiceManagerUtils.h"
 
 namespace mozilla {
 namespace dom {
 
 static const PRUnichar kReplacementChar = static_cast<PRUnichar>(0xFFFD);
 
 void
 TextDecoder::Init(const nsAString& aEncoding, const bool aFatal,
@@ -29,28 +27,17 @@ TextDecoder::Init(const nsAString& aEnco
   }
 
   // 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;
 
   // Create a decoder object for mEncoding.
-  nsCOMPtr<nsICharsetConverterManager> ccm =
-    do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID);
-  if (!ccm) {
-    aRv.Throw(NS_ERROR_UNEXPECTED);
-    return;
-  }
-
-  ccm->GetUnicodeDecoderRaw(mEncoding.get(), getter_AddRefs(mDecoder));
-  if (!mDecoder) {
-    aRv.Throw(NS_ERROR_UNEXPECTED);
-    return;
-  }
+  mDecoder = EncodingUtils::DecoderForEncoding(mEncoding);
 
   if (mFatal) {
     mDecoder->SetInputErrorBehavior(nsIUnicodeDecoder::kOnError_Signal);
   }
 }
 
 void
 TextDecoder::Decode(const char* aInput, const int32_t aLength,
--- a/dom/encoding/TextEncoder.cpp
+++ b/dom/encoding/TextEncoder.cpp
@@ -1,17 +1,15 @@
 /* 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/. */
 
 #include "mozilla/dom/TextEncoder.h"
 #include "mozilla/dom/EncodingUtils.h"
 #include "nsContentUtils.h"
-#include "nsICharsetConverterManager.h"
-#include "nsServiceManagerUtils.h"
 
 namespace mozilla {
 namespace dom {
 
 void
 TextEncoder::Init(const nsAString& aEncoding, ErrorResult& aRv)
 {
   nsAutoString label(aEncoding);
@@ -28,28 +26,17 @@ TextEncoder::Init(const nsAString& aEnco
   if (!mEncoding.EqualsLiteral("UTF-8") &&
       !mEncoding.EqualsLiteral("UTF-16LE") &&
       !mEncoding.EqualsLiteral("UTF-16BE")) {
     aRv.ThrowTypeError(MSG_DOM_ENCODING_NOT_UTF);
     return;
   }
 
   // Create an encoder object for mEncoding.
-  nsCOMPtr<nsICharsetConverterManager> ccm =
-    do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID);
-  if (!ccm) {
-    aRv.Throw(NS_ERROR_UNEXPECTED);
-    return;
-  }
-
-  ccm->GetUnicodeEncoderRaw(mEncoding.get(), getter_AddRefs(mEncoder));
-  if (!mEncoder) {
-    aRv.Throw(NS_ERROR_UNEXPECTED);
-    return;
-  }
+  mEncoder = EncodingUtils::EncoderForEncoding(mEncoding);
 }
 
 JSObject*
 TextEncoder::Encode(JSContext* aCx,
                     JS::Handle<JSObject*> aObj,
                     const nsAString& aString,
                     const bool aStream,
                     ErrorResult& aRv)
--- a/dom/src/json/nsJSON.cpp
+++ b/dom/src/json/nsJSON.cpp
@@ -1,34 +1,37 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=2 sw=2 et tw=79: */
 /* 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/. */
 
 #include "jsapi.h"
 #include "js/OldDebugAPI.h"
-#include "nsIServiceManager.h"
 #include "nsJSON.h"
 #include "nsIXPConnect.h"
 #include "nsIXPCScriptable.h"
 #include "nsStreamUtils.h"
 #include "nsIInputStream.h"
 #include "nsStringStream.h"
-#include "nsICharsetConverterManager.h"
+#include "mozilla/dom/EncodingUtils.h"
+#include "nsIUnicodeEncoder.h"
+#include "nsIUnicodeDecoder.h"
 #include "nsXPCOMStrings.h"
 #include "nsNetUtil.h"
 #include "nsContentUtils.h"
 #include "nsIScriptError.h"
 #include "nsCRTGlue.h"
 #include "nsAutoPtr.h"
 #include "nsIScriptSecurityManager.h"
 #include "mozilla/Maybe.h"
 #include <algorithm>
 
+using mozilla::dom::EncodingUtils;
+
 #define JSON_STREAM_BUFSIZE 4096
 
 NS_INTERFACE_MAP_BEGIN(nsJSON)
   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIJSON)
   NS_INTERFACE_MAP_ENTRY(nsIJSON)
 NS_INTERFACE_MAP_END
 
 NS_IMPL_ADDREF(nsJSON)
@@ -270,21 +273,17 @@ nsJSONWriter::~nsJSONWriter()
   delete [] mBuffer;
 }
 
 nsresult
 nsJSONWriter::SetCharset(const char* aCharset)
 {
   nsresult rv = NS_OK;
   if (mStream) {
-    nsCOMPtr<nsICharsetConverterManager> ccm =
-      do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
-    NS_ENSURE_SUCCESS(rv, rv);
-    rv = ccm->GetUnicodeEncoder(aCharset, getter_AddRefs(mEncoder));
-    NS_ENSURE_SUCCESS(rv, rv);
+    mEncoder = EncodingUtils::EncoderForEncoding(aCharset);
     rv = mEncoder->SetOutputErrorBehavior(nsIUnicodeEncoder::kOnError_Signal,
                                           nullptr, '\0');
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   return rv;
 }
 
@@ -596,21 +595,17 @@ nsJSONListener::ProcessBytes(const char*
         // Not enough bytes to sniff, assume UTF-8
         charset = "UTF-8";
       }
     }
 
     // We should have a unicode charset by now
     rv = CheckCharset(charset.get());
     NS_ENSURE_SUCCESS(rv, rv);
-    nsCOMPtr<nsICharsetConverterManager> ccm =
-        do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
-    NS_ENSURE_SUCCESS(rv, rv);
-    rv = ccm->GetUnicodeDecoderRaw(charset.get(), getter_AddRefs(mDecoder));
-    NS_ENSURE_SUCCESS(rv, rv);
+    mDecoder = EncodingUtils::DecoderForEncoding(charset);
 
     // consume the sniffed bytes
     rv = ConsumeConverted(mSniffBuffer.get(), mSniffBuffer.Length());
     NS_ENSURE_SUCCESS(rv, rv);
     mSniffBuffer.Truncate();
   }
 
   if (!aBuffer)
--- a/intl/uconv/idl/nsICharsetConverterManager.idl
+++ b/intl/uconv/idl/nsICharsetConverterManager.idl
@@ -18,21 +18,24 @@
 #define NS_CHARSETCONVERTERMANAGER_CONTRACTID "@mozilla.org/charset-converter-manager;1"
 %}
 
 interface nsIUnicodeDecoder;
 interface nsIUnicodeEncoder;
 interface nsIUTF8StringEnumerator;
 
 /**
+ * DON'T ADD NEW USES OF THIS INTERFACE TO MOZILLA-CENTRAL. Use 
+ * mozilla::dom::EncodingUtils instead.
  *
  * Here Charsets are identified by ASCII strings. Charset alias
  * resolution is provided by default in most methods. "Raw"
  * versions that do not need this resolution are also provided.
  *
+ * @deprecated Use mozilla::dom::EncodingUtils in mozilla-central instead.
  * @created         21/Feb/2000
  * @author  Catalin Rotaru [CATA]
  */
 [scriptable, uuid(a0550d46-8d9c-47dd-acc7-c083620dff12)]
 interface nsICharsetConverterManager : nsISupports
 {
     /**
      * Get the Unicode decoder for the given charset.
--- a/parser/html/nsHtml5StreamParser.cpp
+++ b/parser/html/nsHtml5StreamParser.cpp
@@ -2,19 +2,16 @@
 /* vim: set sw=2 ts=2 et tw=79: */
 /* 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/. */
 
 #include "mozilla/DebugOnly.h"
 
 #include "nsHtml5StreamParser.h"
-#include "nsICharsetConverterManager.h"
-#include "nsServiceManagerUtils.h"
-#include "nsEncoderDecoderUtils.h"
 #include "nsContentUtils.h"
 #include "nsHtml5Tokenizer.h"
 #include "nsIHttpChannel.h"
 #include "nsHtml5Parser.h"
 #include "nsHtml5TreeBuilder.h"
 #include "nsHtml5AtomTable.h"
 #include "nsHtml5Module.h"
 #include "nsHtml5RefPtr.h"
@@ -299,27 +296,17 @@ nsHtml5StreamParser::SetViewSourceTitle(
 
 nsresult
 nsHtml5StreamParser::SetupDecodingAndWriteSniffingBufferAndCurrentSegment(const uint8_t* aFromSegment, // can be null
                                                                           uint32_t aCount,
                                                                           uint32_t* aWriteCount)
 {
   NS_ASSERTION(IsParserThread(), "Wrong thread!");
   nsresult rv = NS_OK;
-  nsCOMPtr<nsICharsetConverterManager> convManager = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
-  NS_ENSURE_SUCCESS(rv, rv);
-  rv = convManager->GetUnicodeDecoderRaw(mCharset.get(),
-                                         getter_AddRefs(mUnicodeDecoder));
-  if (rv == NS_ERROR_UCONV_NOCONV) {
-    mCharset.AssignLiteral("windows-1252"); // lower case is the raw form
-    mCharsetSource = kCharsetFromFallback;
-    rv = convManager->GetUnicodeDecoderRaw(mCharset.get(), getter_AddRefs(mUnicodeDecoder));
-    mTreeBuilder->SetDocumentCharset(mCharset, mCharsetSource);
-  }
-  NS_ENSURE_SUCCESS(rv, rv);
+  mUnicodeDecoder = EncodingUtils::DecoderForEncoding(mCharset);
   if (mSniffingBuffer) {
     uint32_t writeCount;
     rv = WriteStreamBytes(mSniffingBuffer, mSniffingLength, &writeCount);
     NS_ENSURE_SUCCESS(rv, rv);
     mSniffingBuffer = nullptr;
   }
   mMetaScanner = nullptr;
   if (aFromSegment) {
@@ -327,29 +314,25 @@ nsHtml5StreamParser::SetupDecodingAndWri
   }
   return rv;
 }
 
 nsresult
 nsHtml5StreamParser::SetupDecodingFromBom(const char* aDecoderCharsetName)
 {
   NS_ASSERTION(IsParserThread(), "Wrong thread!");
-  nsresult rv = NS_OK;
-  nsCOMPtr<nsICharsetConverterManager> convManager = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
-  NS_ENSURE_SUCCESS(rv, rv);
-  rv = convManager->GetUnicodeDecoderRaw(aDecoderCharsetName, getter_AddRefs(mUnicodeDecoder));
-  NS_ENSURE_SUCCESS(rv, rv);
   mCharset.Assign(aDecoderCharsetName);
+  mUnicodeDecoder = EncodingUtils::DecoderForEncoding(mCharset);
   mCharsetSource = kCharsetFromByteOrderMark;
   mFeedChardet = false;
   mTreeBuilder->SetDocumentCharset(mCharset, mCharsetSource);
   mSniffingBuffer = nullptr;
   mMetaScanner = nullptr;
   mBomState = BOM_SNIFFING_OVER;
-  return rv;
+  return NS_OK;
 }
 
 void
 nsHtml5StreamParser::SniffBOMlessUTF16BasicLatin(const uint8_t* aFromSegment,
                                                  uint32_t aCountToSniffingLimit)
 {
   // Avoid underspecified heuristic craziness for XHR
   if (mMode == LOAD_AS_DATA) {
@@ -982,26 +965,17 @@ nsHtml5StreamParser::OnStartRequest(nsIR
     return NS_OK;
   }
 
   // We are reloading a document.open()ed doc.
   mReparseForbidden = true;
   mFeedChardet = false;
 
   // Instantiate the converter here to avoid BOM sniffing.
-  nsCOMPtr<nsICharsetConverterManager> convManager =
-    do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
-  NS_ENSURE_SUCCESS(rv, rv);
-  rv = convManager->GetUnicodeDecoderRaw(mCharset.get(),
-                                         getter_AddRefs(mUnicodeDecoder));
-  // if we failed to get a decoder, there will be fallback, so don't propagate
-  //  the error.
-  if (NS_FAILED(rv)) {
-    mCharsetSource = kCharsetFromFallback;
-  }
+  mUnicodeDecoder = EncodingUtils::DecoderForEncoding(mCharset);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsHtml5StreamParser::CheckListenerChain()
 {
   NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
   if (!mObserver) {
--- a/parser/htmlparser/src/nsParser.cpp
+++ b/parser/htmlparser/src/nsParser.cpp
@@ -9,17 +9,16 @@
 #include "nsString.h"
 #include "nsCRT.h"
 #include "nsScanner.h"
 #include "plstr.h"
 #include "nsIStringStream.h"
 #include "nsIChannel.h"
 #include "nsICachingChannel.h"
 #include "nsICacheEntryDescriptor.h"
-#include "nsICharsetConverterManager.h"
 #include "nsIInputStream.h"
 #include "CNavDTD.h"
 #include "prenv.h"
 #include "prlock.h"
 #include "prcvar.h"
 #include "nsParserCIID.h"
 #include "nsReadableUtils.h"
 #include "nsCOMPtr.h"
@@ -123,46 +122,16 @@ public:
   {
     mParser->HandleParserContinueEvent(this);
     return NS_OK;
   }
 };
 
 //-------------- End ParseContinue Event Definition ------------------------
 
-nsICharsetConverterManager* nsParser::sCharsetConverterManager = nullptr;
-
-/**
- *  This gets called when the htmlparser module is initialized.
- */
-// static
-nsresult
-nsParser::Init()
-{
-  nsresult rv;
-
-  nsCOMPtr<nsICharsetConverterManager> charsetConverter =
-    do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  charsetConverter.swap(sCharsetConverterManager);
-
-  return NS_OK;
-}
-
-
-/**
- *  This gets called when the htmlparser module is shutdown.
- */
-// static
-void nsParser::Shutdown()
-{
-  NS_IF_RELEASE(sCharsetConverterManager);
-}
-
 /**
  *  default constructor
  */
 nsParser::nsParser()
 {
   Initialize(true);
 }
 
--- a/parser/htmlparser/src/nsParser.h
+++ b/parser/htmlparser/src/nsParser.h
@@ -48,17 +48,16 @@
 #include "nsParserCIID.h"
 #include "nsITokenizer.h"
 #include "nsHTMLTags.h"
 #include "nsIContentSink.h"
 #include "nsCOMArray.h"
 #include "nsCycleCollectionParticipant.h"
 #include "nsWeakReference.h"
 
-class nsICharsetConverterManager;
 class nsIDTD;
 class nsScanner;
 class nsIRunnable;
 
 #ifdef _MSC_VER
 #pragma warning( disable : 4275 )
 #endif
 
@@ -295,20 +294,16 @@ class nsParser : public nsIParser,
     nsresult PostContinueEvent();
 
     /**
      *  Fired when the continue parse event is triggered.
      *  @update  kmcclusk 5/18/98
      */
     void HandleParserContinueEvent(class nsParserContinueEvent *);
 
-    static nsICharsetConverterManager* GetCharsetConverterManager() {
-      return sCharsetConverterManager;
-    }
-
     virtual void Reset() {
       Cleanup();
       Initialize();
     }
 
     bool IsScriptExecuting() {
       return mSink && mSink->IsScriptExecuting();
     }
@@ -393,14 +388,12 @@ protected:
     uint16_t            mFlags;
 
     nsString            mUnusedInput;
     nsCString           mCharset;
     nsCString           mCommandStr;
 
     bool                mProcessingNetworkData;
     bool                mIsAboutBlank;
-
-    static nsICharsetConverterManager* sCharsetConverterManager;
 };
 
 #endif 
 
--- a/parser/htmlparser/src/nsParserModule.cpp
+++ b/parser/htmlparser/src/nsParserModule.cpp
@@ -79,25 +79,24 @@ Initialize()
 #ifdef DEBUG
   CheckElementTable();
 #endif
 
 #ifdef DEBUG
   nsHTMLTags::TestTagTable();
 #endif
 
-  return nsParser::Init();
+  return rv;
 }
 
 static void
 Shutdown()
 {
   nsHTMLTags::ReleaseTable();
   nsHTMLEntities::ReleaseTable();
-  nsParser::Shutdown();
 }
 
 static mozilla::Module kParserModule = {
   mozilla::Module::kVersion,
   kParserCIDs,
   kParserContracts,
   nullptr,
   nullptr,
--- a/parser/htmlparser/src/nsScanner.cpp
+++ b/parser/htmlparser/src/nsScanner.cpp
@@ -5,18 +5,16 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 //#define __INCREMENTAL 1
 
 #include "mozilla/DebugOnly.h"
 
 #include "nsScanner.h"
 #include "nsDebug.h"
-#include "nsIServiceManager.h"
-#include "nsICharsetConverterManager.h"
 #include "nsReadableUtils.h"
 #include "nsIInputStream.h"
 #include "nsIFile.h"
 #include "nsNetUtil.h"
 #include "nsUTF8Utils.h" // for LossyConvertEncoding
 #include "nsCRT.h"
 #include "nsParser.h"
 #include "nsCharsetSource.h"
@@ -127,29 +125,20 @@ nsresult nsScanner::SetDocumentCharset(c
   if (!mCharset.IsEmpty() && charsetName.Equals(mCharset)) {
     return NS_OK; // no difference, don't change it
   }
 
   // different, need to change it
 
   mCharset.Assign(charsetName);
 
-  NS_ASSERTION(nsParser::GetCharsetConverterManager(),
-               "Must have the charset converter manager!");
+  mUnicodeDecoder = EncodingUtils::DecoderForEncoding(mCharset);
+  mUnicodeDecoder->SetInputErrorBehavior(nsIUnicodeDecoder::kOnError_Signal);
 
-  nsresult res = nsParser::GetCharsetConverterManager()->
-    GetUnicodeDecoderRaw(mCharset.get(), getter_AddRefs(mUnicodeDecoder));
-  if (NS_SUCCEEDED(res) && mUnicodeDecoder)
-  {
-     // We need to detect conversion error of character to support XML
-     // encoding error.
-     mUnicodeDecoder->SetInputErrorBehavior(nsIUnicodeDecoder::kOnError_Signal);
-  }
-
-  return res;
+  return NS_OK;
 }
 
 
 /**
  *  default destructor
  *  
  *  @update  gess 3/25/98
  *  @param   
--- a/widget/cocoa/nsDragService.mm
+++ b/widget/cocoa/nsDragService.mm
@@ -17,17 +17,16 @@
 #include "nsISupportsPrimitives.h"
 #include "nsCOMPtr.h"
 #include "nsPrimitiveHelpers.h"
 #include "nsLinebreakConverter.h"
 #include "nsIMacUtils.h"
 #include "nsIDOMNode.h"
 #include "nsRect.h"
 #include "nsPoint.h"
-#include "nsICharsetConverterManager.h"
 #include "nsIIOService.h"
 #include "nsNetUtil.h"
 #include "nsIDocument.h"
 #include "nsIContent.h"
 #include "nsView.h"
 #include "gfxASurface.h"
 #include "gfxContext.h"
 #include "nsCocoaUtils.h"
--- a/widget/windows/nsFilePicker.cpp
+++ b/widget/windows/nsFilePicker.cpp
@@ -11,18 +11,16 @@
 #include <cderr.h>
 
 #include "mozilla/WindowsVersion.h"
 #include "nsReadableUtils.h"
 #include "nsNetUtil.h"
 #include "nsWindow.h"
 #include "nsILoadContext.h"
 #include "nsIServiceManager.h"
-#include "nsIPlatformCharset.h"
-#include "nsICharsetConverterManager.h"
 #include "nsIURL.h"
 #include "nsIStringBundle.h"
 #include "nsEnumeratorUtils.h"
 #include "nsCRT.h"
 #include "nsString.h"
 #include "nsToolkit.h"
 #include "WinUtils.h"
 #include "nsPIDOMWindow.h"
--- a/widget/windows/nsFilePicker.h
+++ b/widget/windows/nsFilePicker.h
@@ -20,17 +20,16 @@
 #define _WIN32_IE _WIN32_IE_IE70
 #endif
 
 #include "nsIFile.h"
 #include "nsITimer.h"
 #include "nsISimpleEnumerator.h"
 #include "nsCOMArray.h"
 #include "nsAutoPtr.h"
-#include "nsICharsetConverterManager.h"
 #include "nsBaseFilePicker.h"
 #include "nsString.h"
 #include "nsdefs.h"
 #include <commdlg.h>
 #include <shobjidl.h>
 
 class nsILoadContext;
 
--- a/xpfe/appshell/src/nsAppShellService.cpp
+++ b/xpfe/appshell/src/nsAppShellService.cpp
@@ -29,19 +29,16 @@
 #include "nsIRequestObserver.h"
 
 /* For implementing GetHiddenWindowAndJSContext */
 #include "nsIScriptGlobalObject.h"
 #include "nsIScriptContext.h"
 
 #include "nsAppShellService.h"
 #include "nsISupportsPrimitives.h"
-#include "nsIPlatformCharset.h"
-#include "nsICharsetConverterManager.h"
-#include "nsIUnicodeDecoder.h"
 #include "nsIChromeRegistry.h"
 #include "nsILoadContext.h"
 #include "nsIWebNavigation.h"
 
 #include "mozilla/Attributes.h"
 #include "mozilla/Preferences.h"
 #include "mozilla/StartupTimeline.h"