summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMoonchild <moonchild@palemoon.org>2022-05-20 01:02:52 +0000
committerMoonchild <moonchild@palemoon.org>2022-05-20 01:02:52 +0000
commite4bf79f9121fff05a76711228003f79b70b4666d (patch)
tree92ee15b8213fec7082a4a66c7a639912055a467f
parente1f95393a1ac94e15707e97717841e432a0b5269 (diff)
downloaduxp-e4bf79f9121fff05a76711228003f79b70b4666d.tar.gz
Issue #1210 - Expose mozILocaleService Cc interface
-rw-r--r--intl/build/nsI18nModule.cpp3
-rw-r--r--intl/locale/LocaleService.cpp44
-rw-r--r--intl/locale/LocaleService.h27
-rw-r--r--intl/locale/moz.build1
-rw-r--r--intl/locale/mozILocaleService.idl21
-rw-r--r--intl/locale/nsLocaleConstructors.h8
6 files changed, 99 insertions, 5 deletions
diff --git a/intl/build/nsI18nModule.cpp b/intl/build/nsI18nModule.cpp
index 9959d3d6fb..62ac7e47df 100644
--- a/intl/build/nsI18nModule.cpp
+++ b/intl/build/nsI18nModule.cpp
@@ -44,6 +44,7 @@ NS_GENERIC_FACTORY_CONSTRUCTOR(nsEntityConverter)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsSaveAsCharset)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsUnicodeNormalizer)
+NS_DEFINE_NAMED_CID(MOZ_LOCALESERVICE_CID);
NS_DEFINE_NAMED_CID(NS_LBRK_CID);
NS_DEFINE_NAMED_CID(NS_WBRK_CID);
NS_DEFINE_NAMED_CID(NS_SEMANTICUNITSCANNER_CID);
@@ -73,6 +74,7 @@ NS_DEFINE_NAMED_CID(NS_DATETIMEFORMAT_CID);
#endif
static const mozilla::Module::CIDEntry kIntlCIDs[] = {
+ { &kMOZ_LOCALESERVICE_CID, false, nullptr, mozilla::intl::LocaleServiceConstructor },
{ &kNS_LBRK_CID, false, nullptr, nsJISx4051LineBreakerConstructor },
{ &kNS_WBRK_CID, false, nullptr, nsSampleWordBreakerConstructor },
{ &kNS_SEMANTICUNITSCANNER_CID, false, nullptr, nsSemanticUnitScannerConstructor },
@@ -104,6 +106,7 @@ static const mozilla::Module::CIDEntry kIntlCIDs[] = {
};
static const mozilla::Module::ContractIDEntry kIntlContracts[] = {
+ { MOZ_LOCALESERVICE_CONTRACTID, &kMOZ_LOCALESERVICE_CID },
{ NS_LBRK_CONTRACTID, &kNS_LBRK_CID },
{ NS_WBRK_CONTRACTID, &kNS_WBRK_CID },
{ NS_SEMANTICUNITSCANNER_CONTRACTID, &kNS_SEMANTICUNITSCANNER_CID },
diff --git a/intl/locale/LocaleService.cpp b/intl/locale/LocaleService.cpp
index 55542dc1a9..2d3429d688 100644
--- a/intl/locale/LocaleService.cpp
+++ b/intl/locale/LocaleService.cpp
@@ -4,6 +4,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "LocaleService.h"
+
+#include "jsapi.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/Services.h"
#include "nsIObserverService.h"
@@ -11,6 +13,10 @@
using namespace mozilla::intl;
+NS_IMPL_ISUPPORTS(LocaleService, mozILocaleService)
+
+mozilla::StaticRefPtr<LocaleService> LocaleService::sInstance;
+
/**
* This function performs the actual language negotiation for the API.
*
@@ -35,9 +41,8 @@ ReadAppLocales(nsTArray<nsCString>& aRetVal)
}
}
-mozilla::StaticAutoPtr<LocaleService> LocaleService::sInstance;
-
-LocaleService* LocaleService::GetInstance()
+LocaleService*
+LocaleService::GetInstance()
{
if (!sInstance) {
sInstance = new LocaleService();
@@ -77,4 +82,37 @@ LocaleService::Refresh()
obs->NotifyObservers(nullptr, "intl:app-locales-changed", nullptr);
}
}
+}
+
+/**
+ * mozILocaleService methods
+ */
+NS_IMETHODIMP
+LocaleService::GetAppLocales(JSContext* aCtx, JS::MutableHandleValue aRetVal)
+{
+ if (mAppLocales.IsEmpty()) {
+ ReadAppLocales(mAppLocales);
+ }
+
+ uint32_t appLocalesNum = mAppLocales.Length();
+
+ JS::RootedObject locales(aCtx, JS_NewArrayObject(aCtx, appLocalesNum));
+ JS::Rooted<JS::Value> value(aCtx);
+
+ 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);
+ }
+
+ aRetVal.setObject(*locales);
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+LocaleService::GetAppLocale(JSContext* aCtx, nsACString& aRetVal)
+{
+ GetAppLocale(aRetVal);
+ return NS_OK;
} \ No newline at end of file
diff --git a/intl/locale/LocaleService.h b/intl/locale/LocaleService.h
index e5228418c4..42f6c48167 100644
--- a/intl/locale/LocaleService.h
+++ b/intl/locale/LocaleService.h
@@ -10,6 +10,8 @@
#include "nsString.h"
#include "nsTArray.h"
+#include "mozILocaleService.h"
+
namespace mozilla {
namespace intl {
@@ -21,12 +23,31 @@ namespace intl {
* requested languages and negotiating them to produce a fallback
* chain of locales for the application.
*/
-class LocaleService
+class LocaleService : public mozILocaleService
{
public:
+ NS_DECL_ISUPPORTS
+ NS_DECL_MOZILOCALESERVICE
+
+ /**
+ * Create (if necessary) and return a raw pointer to the singleton instance.
+ * Use this accessor in C++ code that just wants to call a method on the
+ * instance, but does not need to hold a reference, as in
+ * nsAutoCString str;
+ * LocaleService::GetInstance()->GetAppLocale(str);
+ */
static LocaleService* GetInstance();
/**
+ * Return an addRef'd pointer to the singleton instance. This is used by the
+ * XPCOM constructor that exists to support usage from JS.
+ */
+ static already_AddRefed<LocaleService> GetInstanceAddRefed()
+ {
+ return RefPtr<LocaleService>(GetInstance()).forget();
+ }
+
+ /**
* 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
@@ -73,7 +94,9 @@ protected:
nsTArray<nsCString> mAppLocales;
private:
- static StaticAutoPtr<LocaleService> sInstance;
+ virtual ~LocaleService() {};
+
+ static StaticRefPtr<LocaleService> sInstance;
};
} // intl
diff --git a/intl/locale/moz.build b/intl/locale/moz.build
index 2ba1c6679a..1f6d44253e 100644
--- a/intl/locale/moz.build
+++ b/intl/locale/moz.build
@@ -15,6 +15,7 @@ else:
DIRS += ['unix']
XPIDL_SOURCES += [
+ 'mozILocaleService.idl',
'nsICollation.idl',
'nsILocale.idl',
'nsILocaleService.idl',
diff --git a/intl/locale/mozILocaleService.idl b/intl/locale/mozILocaleService.idl
new file mode 100644
index 0000000000..9e517bcb70
--- /dev/null
+++ b/intl/locale/mozILocaleService.idl
@@ -0,0 +1,21 @@
+/* -*- Mode: IDL; 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 "nsISupports.idl"
+
+%{C++
+// Define Contractid and CID
+#define MOZ_LOCALESERVICE_CID \
+ { 0x92735ff4, 0x6384, 0x4ad6, { 0x85, 0x08, 0x75, 0x70, 0x10, 0xe1, 0x49, 0xee } }
+
+#define MOZ_LOCALESERVICE_CONTRACTID "@mozilla.org/intl/localeservice;1"
+%}
+
+[scriptable, uuid(C27F8983-B48B-4D1A-92D7-FEB8106F212D)]
+interface mozILocaleService : nsISupports
+{
+ [implicit_jscontext] jsval getAppLocales();
+ [implicit_jscontext] ACString getAppLocale();
+};
diff --git a/intl/locale/nsLocaleConstructors.h b/intl/locale/nsLocaleConstructors.h
index 2d2e579740..8bf4ad6fe4 100644
--- a/intl/locale/nsLocaleConstructors.h
+++ b/intl/locale/nsLocaleConstructors.h
@@ -14,6 +14,7 @@
#include "nsIServiceManager.h"
#include "nsLanguageAtomService.h"
#include "nsPlatformCharset.h"
+#include "LocaleService.h"
#if defined(XP_MACOSX)
#define USE_MAC_LOCALE
@@ -61,6 +62,13 @@ NS_GENERIC_FACTORY_CONSTRUCTOR(nsCollationFactory)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsLanguageAtomService)
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsPlatformCharset, Init)
+namespace mozilla {
+namespace intl {
+NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(LocaleService,
+ LocaleService::GetInstanceAddRefed)
+}
+}
+
#ifdef XP_WIN
NS_GENERIC_FACTORY_CONSTRUCTOR(nsCollationWin)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsDateTimeFormatWin)