From c2634d44385b50130b0cb9f4621dcaeb975fe12e Mon Sep 17 00:00:00 2001 From: Zibi Braniecki <zbraniecki@mozilla.com> Date: Sat, 22 Sep 2018 07:49:05 +0000 Subject: [PATCH] Bug 1493220 - Migrate mozIOSPreferences to use Array<> interface. r=jfkthame Differential Revision: https://phabricator.services.mozilla.com/D6524 --HG-- extra : moz-landing-system : lando --- gfx/thebes/gfxPlatformFontList.cpp | 2 +- intl/locale/LocaleService.cpp | 4 +- intl/locale/OSPreferences.cpp | 84 ++++--------------- intl/locale/OSPreferences.h | 48 ----------- intl/locale/mozIOSPreferences.idl | 12 +-- intl/locale/nsLanguageAtomService.cpp | 4 +- intl/locale/tests/gtest/TestOSPreferences.cpp | 4 +- intl/locale/tests/unit/test_osPreferences.js | 4 +- toolkit/modules/Troubleshoot.jsm | 4 +- 9 files changed, 32 insertions(+), 134 deletions(-) diff --git a/gfx/thebes/gfxPlatformFontList.cpp b/gfx/thebes/gfxPlatformFontList.cpp index d3a34efbdd40c..b91fb83bd9d39 100644 --- a/gfx/thebes/gfxPlatformFontList.cpp +++ b/gfx/thebes/gfxPlatformFontList.cpp @@ -1285,7 +1285,7 @@ gfxPlatformFontList::AppendCJKPrefLangs(eFontPrefLang aPrefLangs[], uint32_t &aL 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); diff --git a/intl/locale/LocaleService.cpp b/intl/locale/LocaleService.cpp index dae1a85aaa905..076423acac398 100644 --- a/intl/locale/LocaleService.cpp +++ b/intl/locale/LocaleService.cpp @@ -628,7 +628,7 @@ LocaleService::GetRegionalPrefsLocales(nsTArray<nsCString>& aRetVal) // 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; } @@ -647,7 +647,7 @@ LocaleService::GetRegionalPrefsLocales(nsTArray<nsCString>& aRetVal) 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; } diff --git a/intl/locale/OSPreferences.cpp b/intl/locale/OSPreferences.cpp index 72d84425f021f..ab75746d953a3 100644 --- a/intl/locale/OSPreferences.cpp +++ b/intl/locale/OSPreferences.cpp @@ -33,42 +33,6 @@ OSPreferences::GetInstance() 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() { @@ -298,27 +262,23 @@ OSPreferences::GetDateTimeConnectorPattern(const nsACString& aLocale, * 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; + aRetVal = mSystemLocales; + return NS_OK; } - *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()); + if (ReadSystemLocales(aRetVal)) { + mSystemLocales = aRetVal; + return NS_OK; } - 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 @@ -337,27 +297,19 @@ OSPreferences::GetSystemLocale(nsACString& aRetVal) } 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; + aRetVal = mRegionalPrefsLocales; + return NS_OK; } - *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()); + if (ReadRegionalPrefsLocales(aRetVal)) { + mRegionalPrefsLocales = aRetVal; + return NS_OK; } - return NS_OK; + return NS_ERROR_FAILURE; } static OSPreferences::DateTimeFormatStyle diff --git a/intl/locale/OSPreferences.h b/intl/locale/OSPreferences.h index d8a467c5df3a9..dfe59942658ff 100644 --- a/intl/locale/OSPreferences.h +++ b/intl/locale/OSPreferences.h @@ -81,54 +81,6 @@ public: } - /** - * 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); diff --git a/intl/locale/mozIOSPreferences.idl b/intl/locale/mozIOSPreferences.idl index 0a16e76c566f3..9f893c79777a3 100644 --- a/intl/locale/mozIOSPreferences.idl +++ b/intl/locale/mozIOSPreferences.idl @@ -35,11 +35,8 @@ interface mozIOSPreferences : nsISupports * 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 @@ -52,11 +49,8 @@ interface mozIOSPreferences : nsISupports * 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. @@ -64,7 +58,7 @@ interface mozIOSPreferences : nsISupports * 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" diff --git a/intl/locale/nsLanguageAtomService.cpp b/intl/locale/nsLanguageAtomService.cpp index 77dded12c415b..db3090879e6d9 100644 --- a/intl/locale/nsLanguageAtomService.cpp +++ b/intl/locale/nsLanguageAtomService.cpp @@ -65,8 +65,8 @@ 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]); diff --git a/intl/locale/tests/gtest/TestOSPreferences.cpp b/intl/locale/tests/gtest/TestOSPreferences.cpp index 6d245a00b9d60..08ddccb4279ea 100644 --- a/intl/locale/tests/gtest/TestOSPreferences.cpp +++ b/intl/locale/tests/gtest/TestOSPreferences.cpp @@ -19,7 +19,7 @@ using namespace mozilla::intl; */ 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()); } @@ -34,7 +34,7 @@ TEST(Intl_Locale_OSPreferences, GetSystemLocales) { */ 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()); } diff --git a/intl/locale/tests/unit/test_osPreferences.js b/intl/locale/tests/unit/test_osPreferences.js index 6ba7afc04e8c8..2bd60f26e9320 100644 --- a/intl/locale/tests/unit/test_osPreferences.js +++ b/intl/locale/tests/unit/test_osPreferences.js @@ -11,13 +11,13 @@ function run_test() 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 = [ diff --git a/toolkit/modules/Troubleshoot.jsm b/toolkit/modules/Troubleshoot.jsm index c359be93b4cd9..e32538bf6639a 100644 --- a/toolkit/modules/Troubleshoot.jsm +++ b/toolkit/modules/Troubleshoot.jsm @@ -625,8 +625,8 @@ var dataProviders = { defaultLocale: Services.locale.defaultLocale, }, osPrefs: { - systemLocales: osPrefs.getSystemLocales(), - regionalPrefsLocales: osPrefs.getRegionalPrefsLocales(), + systemLocales: osPrefs.systemLocales, + regionalPrefsLocales: osPrefs.regionalPrefsLocales, }, }); }, -- GitLab