Bug 1493220 - Migrate mozIOSPreferences to use Array<> interface. r=jfkthame
authorZibi Braniecki <zbraniecki@mozilla.com>
Sat, 22 Sep 2018 07:49:05 +0000
changeset 493571 373999d17625f28ac0f39947bc7a6bc0511ebcae
parent 493570 c970761f46174d4d90e54448fa1d8f78c027840e
child 493572 0648f196a589a3ced8f8b9a8509cb7b0211180f1
push id9984
push userffxbld-merge
push dateMon, 15 Oct 2018 21:07:35 +0000
treeherdermozilla-beta@183d27ea8570 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjfkthame
bugs1493220
milestone64.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 1493220 - Migrate mozIOSPreferences to use Array<> interface. r=jfkthame Differential Revision: https://phabricator.services.mozilla.com/D6524
gfx/thebes/gfxPlatformFontList.cpp
intl/locale/LocaleService.cpp
intl/locale/OSPreferences.cpp
intl/locale/OSPreferences.h
intl/locale/mozIOSPreferences.idl
intl/locale/nsLanguageAtomService.cpp
intl/locale/tests/gtest/TestOSPreferences.cpp
intl/locale/tests/unit/test_osPreferences.js
toolkit/modules/Troubleshoot.jsm
--- a/gfx/thebes/gfxPlatformFontList.cpp
+++ b/gfx/thebes/gfxPlatformFontList.cpp
@@ -1280,17 +1280,17 @@ gfxPlatformFontList::AppendCJKPrefLangs(
         prefLocales.AppendElement(NS_LITERAL_CSTRING("ja"));
         prefLocales.AppendElement(NS_LITERAL_CSTRING("zh-CN"));
         prefLocales.AppendElement(NS_LITERAL_CSTRING("zh-TW"));
         prefLocales.AppendElement(NS_LITERAL_CSTRING("zh-HK"));
         prefLocales.AppendElement(NS_LITERAL_CSTRING("ko"));
 
         AutoTArray<nsCString,16> sysLocales;
         AutoTArray<nsCString,16> negLocales;
-        if (OSPreferences::GetInstance()->GetSystemLocales(sysLocales)) {
+        if (NS_SUCCEEDED(OSPreferences::GetInstance()->GetSystemLocales(sysLocales))) {
             LocaleService::GetInstance()->NegotiateLanguages(
                 sysLocales, prefLocales, NS_LITERAL_CSTRING(""),
                 LocaleService::kLangNegStrategyFiltering, negLocales);
             for (const auto& localeStr : negLocales) {
                 Locale locale(localeStr);
 
                 if (locale.GetLanguage().Equals("ja")) {
                     AppendPrefLang(tempPrefLangs, tempLen,
--- a/intl/locale/LocaleService.cpp
+++ b/intl/locale/LocaleService.cpp
@@ -623,17 +623,17 @@ LocaleService::GetAppLocaleAsBCP47(nsACS
 NS_IMETHODIMP
 LocaleService::GetRegionalPrefsLocales(nsTArray<nsCString>& aRetVal)
 {
   bool useOSLocales = Preferences::GetBool("intl.regional_prefs.use_os_locales", false);
 
   // If the user specified that they want to use OS Regional Preferences locales,
   // try to retrieve them and use.
   if (useOSLocales) {
-    if (OSPreferences::GetInstance()->GetRegionalPrefsLocales(aRetVal)) {
+    if (NS_SUCCEEDED(OSPreferences::GetInstance()->GetRegionalPrefsLocales(aRetVal))) {
       return NS_OK;
     }
 
     // If we fail to retrieve them, return the app locales.
     GetAppLocalesAsBCP47(aRetVal);
     return NS_OK;
   }
 
@@ -642,17 +642,17 @@ LocaleService::GetRegionalPrefsLocales(n
   // the OS Regional Preferences locale.
   //
   // This facilitates scenarios such as Firefox in "en-US" and User sets
   // regional prefs to "en-GB".
   nsAutoCString appLocale;
   AutoTArray<nsCString, 10> regionalPrefsLocales;
   LocaleService::GetInstance()->GetAppLocaleAsBCP47(appLocale);
 
-  if (!OSPreferences::GetInstance()->GetRegionalPrefsLocales(regionalPrefsLocales)) {
+  if (NS_FAILED(OSPreferences::GetInstance()->GetRegionalPrefsLocales(regionalPrefsLocales))) {
     GetAppLocalesAsBCP47(aRetVal);
     return NS_OK;
   }
 
   if (LocaleService::LanguagesMatch(appLocale, regionalPrefsLocales[0])) {
     aRetVal = regionalPrefsLocales;
     return NS_OK;
   }
--- a/intl/locale/OSPreferences.cpp
+++ b/intl/locale/OSPreferences.cpp
@@ -28,52 +28,16 @@ OSPreferences::GetInstance()
 {
   if (!sInstance) {
     sInstance = new OSPreferences();
     ClearOnShutdown(&sInstance);
   }
   return sInstance;
 }
 
-bool
-OSPreferences::GetSystemLocales(nsTArray<nsCString>& aRetVal)
-{
-  if (!mSystemLocales.IsEmpty()) {
-    aRetVal = mSystemLocales;
-    return true;
-  }
-
-  if (ReadSystemLocales(aRetVal)) {
-    mSystemLocales = aRetVal;
-    return true;
-  }
-
-  // If we failed to get the system locale, we still need
-  // to return something because there are tests out there that
-  // depend on system locale to be set.
-  aRetVal.AppendElement(NS_LITERAL_CSTRING("en-US"));
-  return false;
-}
-
-bool
-OSPreferences::GetRegionalPrefsLocales(nsTArray<nsCString>& aRetVal)
-{
-  if (!mRegionalPrefsLocales.IsEmpty()) {
-    aRetVal = mRegionalPrefsLocales;
-    return true;
-  }
-
-  if (ReadRegionalPrefsLocales(aRetVal)) {
-    mRegionalPrefsLocales = aRetVal;
-    return true;
-  }
-
-  return false;
-}
-
 void
 OSPreferences::Refresh()
 {
   nsTArray<nsCString> newLocales;
   ReadSystemLocales(newLocales);
 
   if (mSystemLocales != newLocales) {
     mSystemLocales = std::move(newLocales);
@@ -293,37 +257,33 @@ OSPreferences::GetDateTimeConnectorPatte
   udatpg_close(pg);
   return result;
 }
 
 /**
  * mozIOSPreferences methods
  */
 NS_IMETHODIMP
-OSPreferences::GetSystemLocales(uint32_t* aCount, char*** aOutArray)
+OSPreferences::GetSystemLocales(nsTArray<nsCString>& aRetVal)
 {
-  AutoTArray<nsCString,10> tempLocales;
-  nsTArray<nsCString>* systemLocalesPtr;
-
   if (!mSystemLocales.IsEmpty()) {
-    // use cached value
-    systemLocalesPtr = &mSystemLocales;
-  } else {
-    // get a (perhaps temporary/fallback/hack) value
-    GetSystemLocales(tempLocales);
-    systemLocalesPtr = &tempLocales;
-  }
-  *aCount = systemLocalesPtr->Length();
-  *aOutArray = static_cast<char**>(moz_xmalloc(*aCount * sizeof(char*)));
-
-  for (uint32_t i = 0; i < *aCount; i++) {
-    (*aOutArray)[i] = moz_xstrdup((*systemLocalesPtr)[i].get());
+    aRetVal = mSystemLocales;
+    return NS_OK;
   }
 
-  return NS_OK;
+  if (ReadSystemLocales(aRetVal)) {
+    mSystemLocales = aRetVal;
+    return NS_OK;
+  }
+
+  // If we failed to get the system locale, we still need
+  // to return something because there are tests out there that
+  // depend on system locale to be set.
+  aRetVal.AppendElement(NS_LITERAL_CSTRING("en-US"));
+  return NS_ERROR_FAILURE;
 }
 
 NS_IMETHODIMP
 OSPreferences::GetSystemLocale(nsACString& aRetVal)
 {
   if (!mSystemLocales.IsEmpty()) {
     aRetVal = mSystemLocales[0];
   } else {
@@ -332,37 +292,29 @@ OSPreferences::GetSystemLocale(nsACStrin
     if (!locales.IsEmpty()) {
       aRetVal = locales[0];
     }
   }
   return NS_OK;
 }
 
 NS_IMETHODIMP
-OSPreferences::GetRegionalPrefsLocales(uint32_t* aCount, char*** aOutArray)
+OSPreferences::GetRegionalPrefsLocales(nsTArray<nsCString>& aRetVal)
 {
-  AutoTArray<nsCString,10> tempLocales;
-  nsTArray<nsCString>* regionalPrefsLocalesPtr;
-
   if (!mRegionalPrefsLocales.IsEmpty()) {
-    // use cached value
-    regionalPrefsLocalesPtr = &mRegionalPrefsLocales;
-  } else {
-    // get a (perhaps temporary/fallback/hack) value
-    GetRegionalPrefsLocales(tempLocales);
-    regionalPrefsLocalesPtr = &tempLocales;
-  }
-  *aCount = regionalPrefsLocalesPtr->Length();
-  *aOutArray = static_cast<char**>(moz_xmalloc(*aCount * sizeof(char*)));
-
-  for (uint32_t i = 0; i < *aCount; i++) {
-    (*aOutArray)[i] = moz_xstrdup((*regionalPrefsLocalesPtr)[i].get());
+    aRetVal = mRegionalPrefsLocales;
+    return NS_OK;
   }
 
-  return NS_OK;
+  if (ReadRegionalPrefsLocales(aRetVal)) {
+    mRegionalPrefsLocales = aRetVal;
+    return NS_OK;
+  }
+
+  return NS_ERROR_FAILURE;
 }
 
 static OSPreferences::DateTimeFormatStyle
 ToDateTimeFormatStyle(int32_t aTimeFormat)
 {
   switch (aTimeFormat) {
     // See mozIOSPreferences.idl for the integer values here.
     case 0:
--- a/intl/locale/OSPreferences.h
+++ b/intl/locale/OSPreferences.h
@@ -76,64 +76,16 @@ public:
    * XPCOM constructor that exists to support usage from JS.
    */
   static already_AddRefed<OSPreferences> GetInstanceAddRefed()
   {
     return RefPtr<OSPreferences>(GetInstance()).forget();
   }
 
 
-  /**
-   * Returns a list of locales used by the host environment for UI
-   * localization.
-   *
-   * The result is a sorted list and we expect that the OS attempts to
-   * use the top locale from the list for which it has data.
-   *
-   * Each element of the list is a valid locale ID that can be passed to ICU
-   * and ECMA402 Intl APIs,
-   * At the same time each element is a valid BCP47 language tag that can be
-   * used for language negotiation.
-   *
-   * Example: ["en-US", "de", "pl", "sr-Cyrl", "zh-Hans-HK"]
-   *
-   * The return bool value indicates whether the function successfully
-   * resolved at least one locale.
-   *
-   * Usage:
-   *   nsTArray<nsCString> systemLocales;
-   *   OSPreferences::GetInstance()->GetSystemLocales(systemLocales);
-   *
-   * (See mozIOSPreferences.idl for a JS-callable version of this.)
-   */
-  bool GetSystemLocales(nsTArray<nsCString>& aRetVal);
-
-  /**
-   * Returns a list of locales used by host environment for regional
-   * preferences internationalization.
-   *
-   * The result is a sorted list and we expect that the OS attempts to
-   * use the top locale from the list for which it has data.
-   *
-   * Each element of the list is a valid locale ID that can be passed to ICU
-   * and ECMA402 Intl APIs,
-   *
-   * Example: ["en-US", "de", "pl", "sr-Cyrl", "zh-Hans-HK"]
-   *
-   * The return bool value indicates whether the function successfully
-   * resolved at least one locale.
-   *
-   * Usage:
-   *   nsTArray<nsCString> systemLocales;
-   *   OSPreferences::GetInstance()->GetRegionalPrefsLocales(regionalPrefsLocales);
-   *
-   * (See mozIOSPreferences.idl for a JS-callable version of this.)
-   */
-  bool GetRegionalPrefsLocales(nsTArray<nsCString>& aRetVal);
-
   static bool GetDateTimeConnectorPattern(const nsACString& aLocale,
                                           nsAString& aRetVal);
 
   /**
    * Triggers a refresh of retrieving data from host environment.
    *
    * If the result differs from the previous list, it will additionally
    * trigger global events for changed values:
--- a/intl/locale/mozIOSPreferences.idl
+++ b/intl/locale/mozIOSPreferences.idl
@@ -30,46 +30,40 @@ interface mozIOSPreferences : nsISupport
    * use the top locale from the list for which it has data.
    *
    * Each element of the list is a valid locale ID that can be passed to ICU
    * and ECMA402 Intl APIs,
    * At the same time each element is a valid BCP47 language tag that can be
    * used for language negotiation.
    *
    * Example: ["en-US", "de", "pl", "sr-Cyrl", "zh-Hans-HK"]
-   *
-   * (See OSPreferences.h for a more C++-friendly version of this.)
    */
-  void getSystemLocales([optional] out unsigned long aCount,
-                        [retval, array, size_is(aCount)] out string aOutArray);
+  readonly attribute Array<ACString> systemLocales;
 
   /**
    * Returns a list of locales used by host environment for regional
    * preferences internationalization.
    *
    * The result is a sorted list and we expect that the OS attempts to
    * use the top locale from the list for which it has data.
    *
    * Each element of the list is a valid locale ID that can be passed to ICU
    * and ECMA402 Intl APIs,
    *
    * Example: ["en-US", "de", "pl", "sr-Cyrl", "zh-Hans-HK"]
-   *
-   * (See OSPreferences.h for a more C++-friendly version of this.)
    */
-  void getRegionalPrefsLocales([optional] out unsigned long aCount,
-                               [retval, array, size_is(aCount)] out string aOutArray);
+  readonly attribute Array<ACString> regionalPrefsLocales;
 
   /**
    * Returns the best locale that the host environment is localized to.
    *
    * The result is a valid locale ID and it should be
    * used for all APIs that do not handle language negotiation.
    *
-   * In any scenario involving language negotiation, GetSystemLocales should
+   * In any scenario involving language negotiation, systemLocales should
    * be preferred over the single value.
    *
    * Example: "zh-Hans-HK"
    */
   readonly attribute ACString systemLocale;
 
   /**
    * Returns the best possible date/time pattern for the host environment
--- a/intl/locale/nsLanguageAtomService.cpp
+++ b/intl/locale/nsLanguageAtomService.cpp
@@ -60,18 +60,18 @@ nsLanguageAtomService::LookupCharSet(Not
 }
 
 nsAtom*
 nsLanguageAtomService::GetLocaleLanguage()
 {
   do {
     if (!mLocaleLanguage) {
       AutoTArray<nsCString, 10> regionalPrefsLocales;
-      if (OSPreferences::GetInstance()->GetRegionalPrefsLocales(
-                                          regionalPrefsLocales)) {
+      if (NS_SUCCEEDED(OSPreferences::GetInstance()->GetRegionalPrefsLocales(
+            regionalPrefsLocales))) {
         // use lowercase for all language atoms
         ToLowerCase(regionalPrefsLocales[0]);
         mLocaleLanguage = NS_Atomize(regionalPrefsLocales[0]);
       } else {
         nsAutoCString locale;
         OSPreferences::GetInstance()->GetSystemLocale(locale);
 
         ToLowerCase(locale); // use lowercase for all language atoms
--- a/intl/locale/tests/gtest/TestOSPreferences.cpp
+++ b/intl/locale/tests/gtest/TestOSPreferences.cpp
@@ -14,32 +14,32 @@ using namespace mozilla::intl;
  * we will be able to retrieve at least a single locale out of the system.
  *
  * In theory, that may not be true, but if we encounter such platform we should
  * decide how to handle this and special case and this test should make
  * it not happen without us noticing.
  */
 TEST(Intl_Locale_OSPreferences, GetSystemLocales) {
   nsTArray<nsCString> systemLocales;
-  ASSERT_TRUE(OSPreferences::GetInstance()->GetSystemLocales(systemLocales));
+  ASSERT_TRUE(NS_SUCCEEDED(OSPreferences::GetInstance()->GetSystemLocales(systemLocales)));
 
   ASSERT_FALSE(systemLocales.IsEmpty());
 }
 
 /**
  * We test that on all platforms we test against (irrelevant of the tier),
  * we will be able to retrieve at least a single locale out of the system.
  *
  * In theory, that may not be true, but if we encounter such platform we should
  * decide how to handle this and special case and this test should make
  * it not happen without us noticing.
  */
 TEST(Intl_Locale_OSPreferences, GetRegionalPrefsLocales) {
   nsTArray<nsCString> rgLocales;
-  ASSERT_TRUE(OSPreferences::GetInstance()->GetRegionalPrefsLocales(rgLocales));
+  ASSERT_TRUE(NS_SUCCEEDED(OSPreferences::GetInstance()->GetRegionalPrefsLocales(rgLocales)));
 
   ASSERT_FALSE(rgLocales.IsEmpty());
 }
 
 /**
  * We test that on all platforms we test against,
  * we will be able to retrieve a date and time pattern.
  *
--- a/intl/locale/tests/unit/test_osPreferences.js
+++ b/intl/locale/tests/unit/test_osPreferences.js
@@ -6,23 +6,23 @@ function run_test()
 {
   const osprefs =
     Cc["@mozilla.org/intl/ospreferences;1"]
     .getService(Ci.mozIOSPreferences);
 
   const systemLocale = osprefs.systemLocale;
   Assert.ok(systemLocale != "", "systemLocale is non-empty");
 
-  const systemLocales = osprefs.getSystemLocales();
+  const systemLocales = osprefs.systemLocales;
   Assert.ok(Array.isArray(systemLocales), "systemLocales returns an array");
 
   Assert.ok(systemLocale == systemLocales[0],
     "systemLocale matches first entry in systemLocales");
 
-  const rgLocales = osprefs.getRegionalPrefsLocales();
+  const rgLocales = osprefs.regionalPrefsLocales;
   Assert.ok(Array.isArray(rgLocales), "regionalPrefsLocales returns an array");
 
   const getDateTimePatternTests = [
     [osprefs.dateTimeFormatStyleNone, osprefs.dateTimeFormatStyleNone, ""],
     [osprefs.dateTimeFormatStyleShort, osprefs.dateTimeFormatStyleNone, ""],
     [osprefs.dateTimeFormatStyleNone, osprefs.dateTimeFormatStyleLong, "ar"],
     [osprefs.dateTimeFormatStyleFull, osprefs.dateTimeFormatStyleMedium, "ru"],
   ];
--- a/toolkit/modules/Troubleshoot.jsm
+++ b/toolkit/modules/Troubleshoot.jsm
@@ -620,18 +620,18 @@ var dataProviders = {
       localeService: {
         requested: Services.locale.requestedLocales,
         available: Services.locale.availableLocales,
         supported: Services.locale.appLocalesAsBCP47,
         regionalPrefs: Services.locale.regionalPrefsLocales,
         defaultLocale: Services.locale.defaultLocale,
       },
       osPrefs: {
-        systemLocales: osPrefs.getSystemLocales(),
-        regionalPrefsLocales: osPrefs.getRegionalPrefsLocales(),
+        systemLocales: osPrefs.systemLocales,
+        regionalPrefsLocales: osPrefs.regionalPrefsLocales,
       },
     });
   },
 };
 
 if (AppConstants.MOZ_CRASHREPORTER) {
   dataProviders.crashes = function crashes(done) {
     let CrashReports = ChromeUtils.import("resource://gre/modules/CrashReports.jsm").CrashReports;