Bug 943283 - Use NS_CopyNativeToUnicode instead of nsIUnicodeDecoder. r?jfkthame draft
authorMakoto Kato <m_kato@ga2.so-net.ne.jp>
Tue, 28 Mar 2017 18:07:36 +0900
changeset 552337 3e66d4cd1333ebec89fadb8c6a5ee2767aed4b4f
parent 552146 5182b2c4b963ed87d038c7d9a4021463917076cd
child 621785 3f753bbcbb78fd9f34f18a5b176873084d646764
push id51319
push userm_kato@ga2.so-net.ne.jp
push dateTue, 28 Mar 2017 09:16:30 +0000
reviewersjfkthame
bugs943283, 1346674
milestone55.0a1
Bug 943283 - Use NS_CopyNativeToUnicode instead of nsIUnicodeDecoder. r?jfkthame By bug 1346674, we don't use NSILOCALE_* for unicode conversion in XPC/JS. So we should use NS_CopyNativeToUnicode to get a rid of nsIPlatformCharset. MozReview-Commit-ID: Kp2jijN8On9
js/xpconnect/src/XPCLocale.cpp
--- a/js/xpconnect/src/XPCLocale.cpp
+++ b/js/xpconnect/src/XPCLocale.cpp
@@ -5,33 +5,28 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "mozilla/Assertions.h"
 
 #include "jsapi.h"
 
 #include "nsCollationCID.h"
 #include "nsJSUtils.h"
-#include "nsIPlatformCharset.h"
 #include "nsICollation.h"
+#include "nsNativeCharsetUtils.h"
 #include "nsUnicharUtils.h"
 #include "nsComponentManagerUtils.h"
 #include "nsServiceManagerUtils.h"
-#include "mozilla/dom/EncodingUtils.h"
 #include "mozilla/intl/LocaleService.h"
-#include "mozilla/intl/OSPreferences.h"
 #include "mozilla/Preferences.h"
-#include "nsIUnicodeDecoder.h"
 
 #include "xpcpublic.h"
 
 using namespace JS;
-using mozilla::dom::EncodingUtils;
 using mozilla::intl::LocaleService;
-using mozilla::intl::OSPreferences;
 
 /**
  * JS locale callbacks implemented by XPCOM modules.  These are theoretically
  * safe for use on multiple threads.  Unfortunately, the intl code underlying
  * these XPCOM modules doesn't yet support this, so in practice
  * XPCLocaleCallbacks are limited to the main thread.
  */
 struct XPCLocaleCallbacks : public JSLocaleCallbacks
@@ -160,82 +155,42 @@ private:
     return true;
   }
 
   bool
   ToUnicode(JSContext* cx, const char* src, MutableHandleValue rval)
   {
     nsresult rv;
 
-    if (!mDecoder) {
-      // This code is only used by our prioprietary toLocaleFormat method
-      // and should be removed once we get rid of it.
-      // toLocaleFormat is used in non-ICU scenarios where we don't have
-      // access to any other date/time than the OS one, so we have to also
-      // use the OS locale for unicode conversions.
-      // See bug 1349470 for more details.
-      nsAutoCString osLocale;
-      OSPreferences::GetInstance()->GetSystemLocale(osLocale);
-      NS_ConvertUTF8toUTF16 localeStr(osLocale);
-
-      nsCOMPtr<nsIPlatformCharset> platformCharset =
-        do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &rv);
-
-      if (NS_SUCCEEDED(rv)) {
-        nsAutoCString charset;
-        rv = platformCharset->GetDefaultCharsetForLocale(localeStr, charset);
-        if (NS_SUCCEEDED(rv)) {
-          mDecoder = EncodingUtils::DecoderForEncoding(charset);
-        }
-      }
-    }
-
-    int32_t srcLength = strlen(src);
-
-    if (mDecoder) {
-      int32_t unicharLength = srcLength;
-      char16_t* unichars =
-        (char16_t*)JS_malloc(cx, (srcLength + 1) * sizeof(char16_t));
-      if (unichars) {
-        rv = mDecoder->Convert(src, &srcLength, unichars, &unicharLength);
-        if (NS_SUCCEEDED(rv)) {
-          // terminate the returned string
-          unichars[unicharLength] = 0;
-
-          // nsIUnicodeDecoder::Convert may use fewer than srcLength PRUnichars
-          if (unicharLength + 1 < srcLength + 1) {
-            char16_t* shrunkUnichars =
-              (char16_t*)JS_realloc(cx, unichars,
-                                     (srcLength + 1) * sizeof(char16_t),
-                                     (unicharLength + 1) * sizeof(char16_t));
-            if (shrunkUnichars)
-              unichars = shrunkUnichars;
-          }
-          JSString* str = JS_NewUCString(cx, reinterpret_cast<char16_t*>(unichars), unicharLength);
-          if (str) {
-            rval.setString(str);
-            return true;
-          }
-        }
-        JS_free(cx, unichars);
-      }
+    // This code is only used by our prioprietary toLocaleFormat method
+    // and should be removed once we get rid of it.
+    // toLocaleFormat is used in non-ICU scenarios where we don't have
+    // access to any other date/time than the OS one, so we have to also
+    // use the OS locale for unicode conversions.
+    // See bug 1349470 for more details.
+    nsAutoString result;
+    NS_CopyNativeToUnicode(nsDependentCString(src), result);
+    JSString* ucstr =
+      JS_NewUCStringCopyN(cx, result.get(), result.Length());
+    if (ucstr) {
+      rval.setString(ucstr);
+      return true;
     }
 
     xpc::Throw(cx, NS_ERROR_OUT_OF_MEMORY);
     return false;
   }
 
   void AssertThreadSafety() const
   {
     MOZ_ASSERT(mThread == PR_GetCurrentThread(),
                "XPCLocaleCallbacks used unsafely!");
   }
 
   nsCOMPtr<nsICollation> mCollation;
-  nsCOMPtr<nsIUnicodeDecoder> mDecoder;
 #ifdef DEBUG
   PRThread* mThread;
 #endif
 };
 
 bool
 xpc_LocalizeContext(JSContext* cx)
 {