From f532bbd7af98d2cb543bf86b93b0e0e9fa1e9b2a Mon Sep 17 00:00:00 2001 From: Moonchild Date: Sat, 21 May 2022 09:18:08 +0000 Subject: Issue #1210 - Simplify the implementation of mozILocaleService. Combine both GetAppLocale methods and Update mozILocaleService.getAppLocales to avoid use of jsapi. --- intl/locale/LocaleService.cpp | 33 ++++++++++----------------------- intl/locale/LocaleService.h | 25 ++++--------------------- intl/locale/mozILocaleService.idl | 31 +++++++++++++++++++++++++++++-- 3 files changed, 43 insertions(+), 46 deletions(-) diff --git a/intl/locale/LocaleService.cpp b/intl/locale/LocaleService.cpp index 2d3429d688..7a84ae2449 100644 --- a/intl/locale/LocaleService.cpp +++ b/intl/locale/LocaleService.cpp @@ -5,7 +5,6 @@ #include "LocaleService.h" -#include "jsapi.h" #include "mozilla/ClearOnShutdown.h" #include "mozilla/Services.h" #include "nsIObserverService.h" @@ -60,15 +59,6 @@ LocaleService::GetAppLocales(nsTArray& aRetVal) aRetVal = mAppLocales; } -void -LocaleService::GetAppLocale(nsACString& aRetVal) -{ - if (mAppLocales.IsEmpty()) { - ReadAppLocales(mAppLocales); - } - aRetVal = mAppLocales[0]; -} - void LocaleService::Refresh() { @@ -88,31 +78,28 @@ LocaleService::Refresh() * mozILocaleService methods */ NS_IMETHODIMP -LocaleService::GetAppLocales(JSContext* aCtx, JS::MutableHandleValue aRetVal) +LocaleService::GetAppLocales(uint32_t* aCount, char*** aOutArray) { if (mAppLocales.IsEmpty()) { ReadAppLocales(mAppLocales); } - uint32_t appLocalesNum = mAppLocales.Length(); - - JS::RootedObject locales(aCtx, JS_NewArrayObject(aCtx, appLocalesNum)); - JS::Rooted value(aCtx); + *aCount = mAppLocales.Length(); + *aOutArray = static_cast(moz_xmalloc(*aCount * sizeof(char*))); - for (size_t i = 0; i < appLocalesNum; i++) { - const nsCString& loc = mAppLocales[i]; - JSString* str = JS_NewStringCopyN(aCtx, loc.get(), loc.Length()); - value.setString(str); - JS_DefineElement(aCtx, locales, i, value, JSPROP_ENUMERATE); + for (uint32_t i = 0; i < *aCount; i++) { + (*aOutArray)[i] = moz_xstrdup(mAppLocales[i].get()); } - aRetVal.setObject(*locales); return NS_OK; } NS_IMETHODIMP -LocaleService::GetAppLocale(JSContext* aCtx, nsACString& aRetVal) +LocaleService::GetAppLocale(nsACString& aRetVal) { - GetAppLocale(aRetVal); + if (mAppLocales.IsEmpty()) { + ReadAppLocales(mAppLocales); + } + aRetVal = mAppLocales[0]; return NS_OK; } \ No newline at end of file diff --git a/intl/locale/LocaleService.h b/intl/locale/LocaleService.h index 42f6c48167..e41e3c8d1e 100644 --- a/intl/locale/LocaleService.h +++ b/intl/locale/LocaleService.h @@ -58,29 +58,12 @@ public: * Example: ["en-US", "de", "pl", "sr-Cyrl", "zh-Hans-HK"] * * Usage: - * nsTArray appLocales; - * LocaleService::GetInstance()->GetAppLocales(appLocales); - */ - void GetAppLocales(nsTArray& aRetVal); - - /** - * Returns the best locale that the application should be localized to. - * - * The result is a valid locale IDs and it should be - * used for all APIs that do not handle language negotiation. + * nsTArray appLocales; + * LocaleService::GetInstance()->GetAppLocales(appLocales); * - * Where possible, GetAppLocales should be preferred over this API and - * all callsites should handle some form of "best effort" language - * negotiation to respect user preferences in case the use case does - * not have data for the first locale in the list. - * - * Example: "zh-Hans-HK" - * - * Usage: - * nsAutoCString appLocale; - * LocaleService::GetInstance()->GetAppLocale(appLocale); + * (See mozILocaleService.idl for a JS-callable version of this.) */ - void GetAppLocale(nsACString& aRetVal); + void GetAppLocales(nsTArray& aRetVal); /** * Triggers a refresh of the language negotiation process. diff --git a/intl/locale/mozILocaleService.idl b/intl/locale/mozILocaleService.idl index 9e517bcb70..297ae7c063 100644 --- a/intl/locale/mozILocaleService.idl +++ b/intl/locale/mozILocaleService.idl @@ -16,6 +16,33 @@ [scriptable, uuid(C27F8983-B48B-4D1A-92D7-FEB8106F212D)] interface mozILocaleService : nsISupports { - [implicit_jscontext] jsval getAppLocales(); - [implicit_jscontext] ACString getAppLocale(); + /** + * Returns a list of locales that the application should be localized to. + * + * The result is a sorted list of valid locale IDs and it should be + * used for all APIs that accept list of locales, like ECMA402 and L10n APIs. + * + * This API always returns at least one locale. + * + * Example: ["en-US", "de", "pl", "sr-Cyrl", "zh-Hans-HK"] + * + * (See LocaleService.h for a more C++-friendly version of this.) + */ + void getAppLocales([optional] out unsigned long aCount, + [retval, array, size_is(aCount)] out string aLocales); + + /** + * Returns the best locale that the application should be localized to. + * + * The result is a valid locale ID and it should be + * used for all APIs that do not handle language negotiation. + * + * Where possible, getAppLocales() should be preferred over this API and + * all callsites should handle some form of "best effort" language + * negotiation to respect user preferences in case the use case does + * not have data for the first locale in the list. + * + * Example: "zh-Hans-HK" + */ + ACString getAppLocale(); }; -- cgit v1.2.3