Bug 1346674 - Migrate all uses of nsILocaleService::GetApplicationLocale to mozILocaleService::GetAppLocale. r?jfkthame draft
authorZibi Braniecki <gandalf@mozilla.com>
Sun, 12 Mar 2017 20:39:22 -0700
changeset 497838 83d6d7b96e709fd453842b3230fa73eadacc888a
parent 497714 d706713c339ac02c2114a98f7c26ecd449d8134b
child 549004 69d6fad5f18e90eb4ce293ee9e0e7e3cda4f7e8f
push id49026
push userzbraniecki@mozilla.com
push dateMon, 13 Mar 2017 22:24:07 +0000
reviewersjfkthame
bugs1346674
milestone55.0a1
Bug 1346674 - Migrate all uses of nsILocaleService::GetApplicationLocale to mozILocaleService::GetAppLocale. r?jfkthame MozReview-Commit-ID: 2E7mj3yQnxB
gfx/thebes/gfxAndroidPlatform.cpp
gfx/thebes/gfxDWriteFontList.cpp
gfx/thebes/gfxPlatform.cpp
gfx/thebes/gfxPlatformFontList.cpp
intl/locale/nsILanguageAtomService.h
intl/locale/nsLanguageAtomService.cpp
intl/locale/nsLanguageAtomService.h
js/xpconnect/src/XPCLocale.cpp
--- a/gfx/thebes/gfxAndroidPlatform.cpp
+++ b/gfx/thebes/gfxAndroidPlatform.cpp
@@ -4,41 +4,42 @@
  * 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 "base/basictypes.h"
 
 #include "gfxAndroidPlatform.h"
 #include "mozilla/gfx/2D.h"
 #include "mozilla/CountingAllocatorBase.h"
+#include "mozilla/intl/LocaleService.h"
 #include "mozilla/Preferences.h"
 
 #include "gfx2DGlue.h"
 #include "gfxFT2FontList.h"
 #include "gfxImageSurface.h"
 #include "gfxTextRun.h"
 #include "mozilla/dom/ContentChild.h"
 #include "nsXULAppAPI.h"
 #include "nsIScreen.h"
 #include "nsIScreenManager.h"
-#include "nsILocaleService.h"
 #include "nsServiceManagerUtils.h"
 #include "gfxPrefs.h"
 #include "cairo.h"
 #include "VsyncSource.h"
 
 #include "ft2build.h"
 #include FT_FREETYPE_H
 #include FT_MODULE_H
 
 #include "GeneratedJNINatives.h"
 
 using namespace mozilla;
 using namespace mozilla::dom;
 using namespace mozilla::gfx;
+using mozilla::intl::LocaleService;
 
 static FT_Library gPlatformFTLibrary = nullptr;
 
 class FreetypeReporter final : public nsIMemoryReporter,
                                public CountingAllocatorBase<FreetypeReporter>
 {
 private:
     ~FreetypeReporter() {}
@@ -127,39 +128,23 @@ static bool
 IsJapaneseLocale()
 {
     static bool sInitialized = false;
     static bool sIsJapanese = false;
 
     if (!sInitialized) {
         sInitialized = true;
 
-        do { // to allow 'break' to abandon this block if a call fails
-            nsresult rv;
-            nsCOMPtr<nsILocaleService> ls =
-                do_GetService(NS_LOCALESERVICE_CONTRACTID, &rv);
-            if (NS_FAILED(rv)) {
-                break;
-            }
-            nsCOMPtr<nsILocale> appLocale;
-            rv = ls->GetApplicationLocale(getter_AddRefs(appLocale));
-            if (NS_FAILED(rv)) {
-                break;
-            }
-            nsString localeStr;
-            rv = appLocale->
-                GetCategory(NS_LITERAL_STRING(NSILOCALE_MESSAGE), localeStr);
-            if (NS_FAILED(rv)) {
-                break;
-            }
-            const nsAString& lang = nsDependentSubstring(localeStr, 0, 2);
-            if (lang.EqualsLiteral("ja")) {
-                sIsJapanese = true;
-            }
-        } while (false);
+        nsAutoCString appLocale;
+        LocaleService::GetInstance()->GetAppLocaleAsLangTag(appLocale);
+
+        const nsDependentCSubstring lang(appLocale, 0, 2);
+        if (lang.EqualsLiteral("ja")) {
+            sIsJapanese = true;
+        }
     }
 
     return sIsJapanese;
 }
 
 void
 gfxAndroidPlatform::GetCommonFallbackFonts(uint32_t aCh, uint32_t aNextCh,
                                            Script aRunScript,
--- a/gfx/thebes/gfxDWriteFontList.cpp
+++ b/gfx/thebes/gfxDWriteFontList.cpp
@@ -1,20 +1,20 @@
 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  * 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/ArrayUtils.h"
 #include "mozilla/MemoryReporting.h"
+#include "mozilla/intl/LocaleService.h"
 
 #include "gfxDWriteFontList.h"
 #include "gfxDWriteFonts.h"
 #include "nsUnicharUtils.h"
-#include "nsILocaleService.h"
 #include "nsServiceManagerUtils.h"
 #include "nsCharSeparatedTokenizer.h"
 #include "mozilla/Preferences.h"
 #include "mozilla/Sprintf.h"
 #include "mozilla/Telemetry.h"
 #include "nsDirectoryServiceUtils.h"
 #include "nsDirectoryServiceDefs.h"
 #include "nsAppDirectoryServiceDefs.h"
@@ -22,16 +22,17 @@
 
 #include "gfxGDIFontList.h"
 
 #include "nsIWindowsRegKey.h"
 
 #include "harfbuzz/hb.h"
 
 using namespace mozilla;
+using mozilla::intl::LocaleService;
 
 #define LOG_FONTLIST(args) MOZ_LOG(gfxPlatform::GetLog(eGfxLog_fontlist), \
                                LogLevel::Debug, args)
 #define LOG_FONTLIST_ENABLED() MOZ_LOG_TEST( \
                                    gfxPlatform::GetLog(eGfxLog_fontlist), \
                                    LogLevel::Debug)
 
 #define LOG_FONTINIT(args) MOZ_LOG(gfxPlatform::GetLog(eGfxLog_fontinit), \
@@ -267,39 +268,28 @@ gfxDWriteFontFamily::ReadFaceNames(gfxPl
     }
 }
 
 void
 gfxDWriteFontFamily::LocalizedName(nsAString &aLocalizedName)
 {
     aLocalizedName.AssignLiteral("Unknown Font");
     HRESULT hr;
-    nsresult rv;
-    nsCOMPtr<nsILocaleService> ls = do_GetService(NS_LOCALESERVICE_CONTRACTID,
-                                                  &rv);
-    nsCOMPtr<nsILocale> locale;
-    rv = ls->GetApplicationLocale(getter_AddRefs(locale));
-    nsString localeName;
-    if (NS_SUCCEEDED(rv)) {
-        rv = locale->GetCategory(NS_LITERAL_STRING(NSILOCALE_MESSAGE), 
-                                 localeName);
-    }
-    if (NS_FAILED(rv)) {
-        localeName.AssignLiteral("en-us");
-    }
+    nsAutoCString locale;
+    LocaleService::GetInstance()->GetAppLocaleAsLangTag(locale);
 
     RefPtr<IDWriteLocalizedStrings> names;
 
     hr = mDWFamily->GetFamilyNames(getter_AddRefs(names));
     if (FAILED(hr)) {
         return;
     }
     UINT32 idx = 0;
     BOOL exists;
-    hr = names->FindLocaleName(localeName.get(),
+    hr = names->FindLocaleName(NS_ConvertUTF8toUTF16(locale).get(),
                                &idx,
                                &exists);
     if (FAILED(hr)) {
         return;
     }
     if (!exists) {
         // Use english is localized is not found.
         hr = names->FindLocaleName(L"en-us", &idx, &exists);
--- a/gfx/thebes/gfxPlatform.cpp
+++ b/gfx/thebes/gfxPlatform.cpp
@@ -62,17 +62,16 @@
 #include "gfxGraphiteShaper.h"
 #include "gfx2DGlue.h"
 #include "gfxGradientCache.h"
 #include "gfxUtils.h" // for NextPowerOfTwo
 
 #include "nsUnicodeRange.h"
 #include "nsServiceManagerUtils.h"
 #include "nsTArray.h"
-#include "nsILocaleService.h"
 #include "nsIObserverService.h"
 #include "nsIScreenManager.h"
 #include "FrameMetrics.h"
 #include "MainThreadUtils.h"
 #ifdef MOZ_CRASHREPORTER
 #include "nsExceptionHandler.h"
 #endif
 
--- a/gfx/thebes/gfxPlatformFontList.cpp
+++ b/gfx/thebes/gfxPlatformFontList.cpp
@@ -1,38 +1,39 @@
 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  * 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/Logging.h"
+#include "mozilla/intl/LocaleService.h"
 
 #include "gfxPlatformFontList.h"
 #include "gfxTextRun.h"
 #include "gfxUserFontSet.h"
 
 #include "nsCRT.h"
 #include "nsGkAtoms.h"
-#include "nsILocaleService.h"
 #include "nsServiceManagerUtils.h"
 #include "nsUnicharUtils.h"
 #include "nsUnicodeRange.h"
 #include "nsUnicodeProperties.h"
 
 #include "mozilla/Attributes.h"
 #include "mozilla/Likely.h"
 #include "mozilla/MemoryReporting.h"
 #include "mozilla/Preferences.h"
 #include "mozilla/Telemetry.h"
 #include "mozilla/TimeStamp.h"
 #include "mozilla/gfx/2D.h"
 
 #include <locale.h>
 
 using namespace mozilla;
+using mozilla::intl::LocaleService;
 
 #define LOG_FONTLIST(args) MOZ_LOG(gfxPlatform::GetLog(eGfxLog_fontlist), \
                                LogLevel::Debug, args)
 #define LOG_FONTLIST_ENABLED() MOZ_LOG_TEST( \
                                    gfxPlatform::GetLog(eGfxLog_fontlist), \
                                    LogLevel::Debug)
 #define LOG_FONTINIT(args) MOZ_LOG(gfxPlatform::GetLog(eGfxLog_fontinit), \
                                LogLevel::Debug, args)
@@ -1085,50 +1086,34 @@ gfxPlatformFontList::AppendCJKPrefLangs(
                         break;
                     default:
                         break;
                 }
                 p++;
             }
         }
 
-        do { // to allow 'break' to abort this block if a call fails
-            nsresult rv;
-            nsCOMPtr<nsILocaleService> ls =
-                do_GetService(NS_LOCALESERVICE_CONTRACTID, &rv);
-            if (NS_FAILED(rv))
-                break;
-
-            nsCOMPtr<nsILocale> appLocale;
-            rv = ls->GetApplicationLocale(getter_AddRefs(appLocale));
-            if (NS_FAILED(rv))
-                break;
+        nsAutoCString localeStr;
+        LocaleService::GetInstance()->GetAppLocaleAsLangTag(localeStr);
 
-            nsString localeStr;
-            rv = appLocale->
-                GetCategory(NS_LITERAL_STRING(NSILOCALE_MESSAGE), localeStr);
-            if (NS_FAILED(rv))
-                break;
-
-            const nsAString& lang = Substring(localeStr, 0, 2);
-            if (lang.EqualsLiteral("ja")) {
-                AppendPrefLang(tempPrefLangs, tempLen, eFontPrefLang_Japanese);
-            } else if (lang.EqualsLiteral("zh")) {
-                const nsAString& region = Substring(localeStr, 3, 2);
-                if (region.EqualsLiteral("CN")) {
-                    AppendPrefLang(tempPrefLangs, tempLen, eFontPrefLang_ChineseCN);
-                } else if (region.EqualsLiteral("TW")) {
-                    AppendPrefLang(tempPrefLangs, tempLen, eFontPrefLang_ChineseTW);
-                } else if (region.EqualsLiteral("HK")) {
-                    AppendPrefLang(tempPrefLangs, tempLen, eFontPrefLang_ChineseHK);
-                }
-            } else if (lang.EqualsLiteral("ko")) {
-                AppendPrefLang(tempPrefLangs, tempLen, eFontPrefLang_Korean);
+        const nsACString& lang = Substring(localeStr, 0, 2);
+        if (lang.EqualsLiteral("ja")) {
+            AppendPrefLang(tempPrefLangs, tempLen, eFontPrefLang_Japanese);
+        } else if (lang.EqualsLiteral("zh")) {
+            const nsACString& region = Substring(localeStr, 3, 2);
+            if (region.EqualsLiteral("CN")) {
+                AppendPrefLang(tempPrefLangs, tempLen, eFontPrefLang_ChineseCN);
+            } else if (region.EqualsLiteral("TW")) {
+                AppendPrefLang(tempPrefLangs, tempLen, eFontPrefLang_ChineseTW);
+            } else if (region.EqualsLiteral("HK")) {
+                AppendPrefLang(tempPrefLangs, tempLen, eFontPrefLang_ChineseHK);
             }
-        } while (0);
+        } else if (lang.EqualsLiteral("ko")) {
+            AppendPrefLang(tempPrefLangs, tempLen, eFontPrefLang_Korean);
+        }
 
         // last resort... (the order is same as old gfx.)
         AppendPrefLang(tempPrefLangs, tempLen, eFontPrefLang_Japanese);
         AppendPrefLang(tempPrefLangs, tempLen, eFontPrefLang_Korean);
         AppendPrefLang(tempPrefLangs, tempLen, eFontPrefLang_ChineseCN);
         AppendPrefLang(tempPrefLangs, tempLen, eFontPrefLang_ChineseHK);
         AppendPrefLang(tempPrefLangs, tempLen, eFontPrefLang_ChineseTW);
 
--- a/intl/locale/nsILanguageAtomService.h
+++ b/intl/locale/nsILanguageAtomService.h
@@ -28,17 +28,17 @@ class nsILanguageAtomService : public ns
  public: 
   NS_DECLARE_STATIC_IID_ACCESSOR(NS_ILANGUAGEATOMSERVICE_IID)
 
   virtual nsIAtom* LookupLanguage(const nsACString &aLanguage,
                                   nsresult *aError = nullptr) = 0;
   virtual already_AddRefed<nsIAtom>
   LookupCharSet(const nsACString& aCharSet) = 0;
 
-  virtual nsIAtom* GetLocaleLanguage(nsresult *aError = nullptr) = 0;
+  virtual nsIAtom* GetLocaleLanguage() = 0;
 
   virtual nsIAtom* GetLanguageGroup(nsIAtom *aLanguage,
                                     nsresult *aError = nullptr) = 0;
 };
 
 NS_DEFINE_STATIC_IID_ACCESSOR(nsILanguageAtomService,
                               NS_ILANGUAGEATOMSERVICE_IID)
 
--- a/intl/locale/nsLanguageAtomService.cpp
+++ b/intl/locale/nsLanguageAtomService.cpp
@@ -1,24 +1,25 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* 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 "nsLanguageAtomService.h"
-#include "nsILocaleService.h"
 #include "nsUConvPropertySearch.h"
 #include "nsUnicharUtils.h"
 #include "nsIAtom.h"
 #include "mozilla/ArrayUtils.h"
 #include "mozilla/Services.h"
+#include "mozilla/intl/LocaleService.h"
 #include "nsServiceManagerUtils.h"
 #include "mozilla/dom/EncodingUtils.h"
 
 using namespace mozilla;
+using mozilla::intl::LocaleService;
 
 static constexpr nsUConvProp kLangGroups[] = {
 #include "langGroups.properties.h"
 };
 
 NS_IMPL_ISUPPORTS(nsLanguageAtomService, nsILanguageAtomService)
 
 nsLanguageAtomService::nsLanguageAtomService()
@@ -40,47 +41,28 @@ already_AddRefed<nsIAtom>
 nsLanguageAtomService::LookupCharSet(const nsACString& aCharSet)
 {
   nsAutoCString group;
   mozilla::dom::EncodingUtils::LangGroupForEncoding(aCharSet, group);
   return NS_Atomize(group);
 }
 
 nsIAtom*
-nsLanguageAtomService::GetLocaleLanguage(nsresult *aError)
+nsLanguageAtomService::GetLocaleLanguage()
 {
-  nsresult res = NS_OK;
-
   do {
     if (!mLocaleLanguage) {
-      nsCOMPtr<nsILocaleService> localeService;
-      localeService = do_GetService(NS_LOCALESERVICE_CONTRACTID);
-      if (!localeService) {
-        res = NS_ERROR_FAILURE;
-        break;
-      }
+      nsAutoCString locale;
+      LocaleService::GetInstance()->GetAppLocaleAsLangTag(locale);
 
-      nsCOMPtr<nsILocale> locale;
-      res = localeService->GetApplicationLocale(getter_AddRefs(locale));
-      if (NS_FAILED(res))
-        break;
-
-      nsAutoString loc;
-      res = locale->GetCategory(NS_LITERAL_STRING(NSILOCALE_MESSAGE), loc);
-      if (NS_FAILED(res))
-        break;
-
-      ToLowerCase(loc); // use lowercase for all language atoms
-      mLocaleLanguage = NS_Atomize(loc);
+      ToLowerCase(locale); // use lowercase for all language atoms
+      mLocaleLanguage = NS_Atomize(locale);
     }
   } while (0);
 
-  if (aError)
-    *aError = res;
-
   return mLocaleLanguage;
 }
 
 nsIAtom*
 nsLanguageAtomService::GetLanguageGroup(nsIAtom *aLanguage,
                                         nsresult *aError)
 {
   nsIAtom *retVal;
--- a/intl/locale/nsLanguageAtomService.h
+++ b/intl/locale/nsLanguageAtomService.h
@@ -21,17 +21,17 @@ public:
 
   // nsILanguageAtomService
   virtual nsIAtom*
     LookupLanguage(const nsACString &aLanguage, nsresult *aError) override;
 
   virtual already_AddRefed<nsIAtom>
     LookupCharSet(const nsACString& aCharSet) override;
 
-  virtual nsIAtom* GetLocaleLanguage(nsresult *aError) override;
+  virtual nsIAtom* GetLocaleLanguage() override;
 
   virtual nsIAtom* GetLanguageGroup(nsIAtom *aLanguage,
                                                 nsresult *aError) override;
 
   nsLanguageAtomService();
 
 private:
   ~nsLanguageAtomService() { }
--- a/js/xpconnect/src/XPCLocale.cpp
+++ b/js/xpconnect/src/XPCLocale.cpp
@@ -6,29 +6,30 @@
 
 #include "mozilla/Assertions.h"
 
 #include "jsapi.h"
 
 #include "nsCollationCID.h"
 #include "nsJSUtils.h"
 #include "nsIPlatformCharset.h"
-#include "nsILocaleService.h"
 #include "nsICollation.h"
 #include "nsUnicharUtils.h"
 #include "nsComponentManagerUtils.h"
 #include "nsServiceManagerUtils.h"
 #include "mozilla/dom/EncodingUtils.h"
+#include "mozilla/intl/LocaleService.h"
 #include "mozilla/Preferences.h"
 #include "nsIUnicodeDecoder.h"
 
 #include "xpcpublic.h"
 
 using namespace JS;
 using mozilla::dom::EncodingUtils;
+using mozilla::intl::LocaleService;
 
 /**
  * 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
@@ -159,37 +160,28 @@ private:
 
   bool
   ToUnicode(JSContext* cx, const char* src, MutableHandleValue rval)
   {
     nsresult rv;
 
     if (!mDecoder) {
       // use app default locale
-      nsCOMPtr<nsILocaleService> localeService =
-        do_GetService(NS_LOCALESERVICE_CONTRACTID, &rv);
-      if (NS_SUCCEEDED(rv)) {
-        nsCOMPtr<nsILocale> appLocale;
-        rv = localeService->GetApplicationLocale(getter_AddRefs(appLocale));
-        if (NS_SUCCEEDED(rv)) {
-          nsAutoString localeStr;
-          rv = appLocale->
-               GetCategory(NS_LITERAL_STRING(NSILOCALE_TIME), localeStr);
-          MOZ_ASSERT(NS_SUCCEEDED(rv), "failed to get app locale info");
+      nsAutoCString appLocale;
+      LocaleService::GetInstance()->GetAppLocaleAsLangTag(appLocale);
+      NS_ConvertUTF8toUTF16 localeStr(appLocale);
 
-          nsCOMPtr<nsIPlatformCharset> platformCharset =
-            do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &rv);
+      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);
-            }
-          }
+      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;
@@ -247,32 +239,20 @@ xpc_LocalizeContext(JSContext* cx)
   // Check a pref to see if we should use US English locale regardless
   // of the system locale.
   if (Preferences::GetBool("javascript.use_us_english_locale", false)) {
     return JS_SetDefaultLocale(cx, "en-US");
   }
 
   // No pref has been found, so get the default locale from the
   // application's locale.
-  nsCOMPtr<nsILocaleService> localeService =
-    do_GetService(NS_LOCALESERVICE_CONTRACTID);
-  if (!localeService)
-    return false;
+  nsAutoCString appLocaleStr;
+  LocaleService::GetInstance()->GetAppLocaleAsBCP47(appLocaleStr);
 
-  nsCOMPtr<nsILocale> appLocale;
-  nsresult rv = localeService->GetApplicationLocale(getter_AddRefs(appLocale));
-  if (NS_FAILED(rv))
-    return false;
-
-  nsAutoString localeStr;
-  rv = appLocale->GetCategory(NS_LITERAL_STRING(NSILOCALE_TIME), localeStr);
-  MOZ_ASSERT(NS_SUCCEEDED(rv), "failed to get app locale info");
-  NS_LossyConvertUTF16toASCII locale(localeStr);
-
-  return JS_SetDefaultLocale(cx, locale.get());
+  return JS_SetDefaultLocale(cx, appLocaleStr.get());
 }
 
 void
 xpc_DelocalizeContext(JSContext* cx)
 {
   const XPCLocaleCallbacks* lc = XPCLocaleCallbacks::This(cx);
   JS_SetLocaleCallbacks(cx, nullptr);
   delete lc;