Bug 1336281 - Expose mozILocaleService. draft
authorZibi Braniecki <gandalf@mozilla.com>
Thu, 02 Feb 2017 18:41:37 -0800
changeset 469980 5ff8bb68a185b9d5eca520d7ddeb9349d5888512
parent 469935 1951fbecf24d991d204c0bf00de3336878886df1
child 544354 dd1d6510738fd749896c965e47edb96965331485
push id43898
push userzbraniecki@mozilla.com
push dateFri, 03 Feb 2017 02:42:05 +0000
bugs1336281
milestone54.0a1
Bug 1336281 - Expose mozILocaleService. MozReview-Commit-ID: JEsOysvuny5
intl/build/nsI18nModule.cpp
intl/locale/moz.build
intl/locale/mozILocaleService.idl
intl/locale/mozLocaleService.cpp
intl/locale/mozLocaleService.h
intl/locale/nsLocaleConstructors.h
--- a/intl/build/nsI18nModule.cpp
+++ b/intl/build/nsI18nModule.cpp
@@ -39,16 +39,17 @@ NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsSt
 
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsCaseConversionImp2)
 NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsCategoryImp,
                                          nsCategoryImp::GetInstance)
 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);
 NS_DEFINE_NAMED_CID(NS_UNICHARUTIL_CID);
 NS_DEFINE_NAMED_CID(NS_UNICHARCATEGORY_CID);
 NS_DEFINE_NAMED_CID(NS_ENTITYCONVERTER_CID);
 NS_DEFINE_NAMED_CID(NS_SAVEASCHARSET_CID);
 NS_DEFINE_NAMED_CID(NS_UNICODE_NORMALIZER_CID);
@@ -65,16 +66,17 @@ NS_DEFINE_NAMED_CID(NS_COLLATION_CID);
 #ifdef USE_UNIX_LOCALE
 NS_DEFINE_NAMED_CID(NS_COLLATION_CID);
 #endif
 #ifdef USE_MAC_LOCALE
 NS_DEFINE_NAMED_CID(NS_COLLATION_CID);
 #endif
 
 static const mozilla::Module::CIDEntry kIntlCIDs[] = {
+    { &kMOZ_LOCALESERVICE_CID, false, nullptr, mozLocaleServiceConstructor },
     { &kNS_LBRK_CID, false, nullptr, nsJISx4051LineBreakerConstructor },
     { &kNS_WBRK_CID, false, nullptr, nsSampleWordBreakerConstructor },
     { &kNS_SEMANTICUNITSCANNER_CID, false, nullptr, nsSemanticUnitScannerConstructor },
     { &kNS_UNICHARUTIL_CID, false, nullptr, nsCaseConversionImp2Constructor },
     { &kNS_UNICHARCATEGORY_CID, false, nullptr, nsCategoryImpConstructor },
     { &kNS_ENTITYCONVERTER_CID, false, nullptr, nsEntityConverterConstructor },
     { &kNS_SAVEASCHARSET_CID, false, nullptr, nsSaveAsCharsetConstructor },
     { &kNS_UNICODE_NORMALIZER_CID, false, nullptr, nsUnicodeNormalizerConstructor },
@@ -93,16 +95,17 @@ static const mozilla::Module::CIDEntry k
 #endif
 #ifdef USE_MAC_LOCALE
     { &kNS_COLLATION_CID, false, nullptr, nsCollationMacUCConstructor },
 #endif
     { nullptr }
 };
 
 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 },
     { NS_UNICHARUTIL_CONTRACTID, &kNS_UNICHARUTIL_CID },
     { NS_UNICHARCATEGORY_CONTRACTID, &kNS_UNICHARCATEGORY_CID },
     { NS_ENTITYCONVERTER_CONTRACTID, &kNS_ENTITYCONVERTER_CID },
     { NS_SAVEASCHARSET_CONTRACTID, &kNS_SAVEASCHARSET_CID },
     { NS_UNICODE_NORMALIZER_CONTRACTID, &kNS_UNICODE_NORMALIZER_CID },
--- a/intl/locale/moz.build
+++ b/intl/locale/moz.build
@@ -11,16 +11,17 @@ toolkit = CONFIG['MOZ_WIDGET_TOOLKIT']
 if toolkit == 'windows':
     DIRS += ['windows']
 elif toolkit == 'cocoa':
     DIRS += ['mac']
 else:
     DIRS += ['unix']
 
 XPIDL_SOURCES += [
+    'mozILocaleService.idl',
     'nsICollation.idl',
     'nsILocale.idl',
     'nsILocaleService.idl',
     'nsIScriptableDateFormat.idl',
 ]
 
 XPIDL_MODULE = 'locale'
 
@@ -36,16 +37,17 @@ EXPORTS += [
 ]
 
 EXPORTS.mozilla.intl += [
     'LocaleService.h'
 ]
 
 UNIFIED_SOURCES += [
     'LocaleService.cpp',
+    'mozLocaleService.cpp',
     'nsCollation.cpp',
     'nsLanguageAtomService.cpp',
     'nsLocale.cpp',
     'nsLocaleService.cpp',
     'nsScriptableDateFormat.cpp',
     'nsUConvPropertySearch.cpp',
 ]
 
new file mode 100644
--- /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(92735ff4-6384-4ad6-8508-757010e149ee)]
+interface mozILocaleService : nsISupports
+{
+  [implicit_jscontext] jsval GetAppLocales();
+  [implicit_jscontext] ACString GetAppLocale();
+};
new file mode 100644
--- /dev/null
+++ b/intl/locale/mozLocaleService.cpp
@@ -0,0 +1,53 @@
+/* -*- 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 "mozLocaleService.h"
+#include "LocaleService.h"
+#include "jsapi.h"
+
+using namespace mozilla::intl;
+
+NS_IMPL_ISUPPORTS(mozLocaleService, mozILocaleService)
+
+mozLocaleService::mozLocaleService()
+{
+}
+
+mozLocaleService::~mozLocaleService()
+{
+}
+
+NS_IMETHODIMP
+mozLocaleService::GetAppLocales(JSContext* cx, JS::MutableHandleValue retval)
+{
+  nsTArray<nsCString> appLocales;
+  LocaleService::GetInstance()->GetAppLocales(appLocales);
+
+  PRUint32 appLocalesNum = appLocales.Length();
+
+  JS::RootedObject locales(cx, JS_NewArrayObject(cx, appLocalesNum));
+  JS::Rooted<JS::Value> value(cx);
+
+  JSString* str;
+
+  for (size_t i = 0; i < appLocalesNum; i++) {
+    str = JS_NewStringCopyZ(cx, appLocales[i].get());
+    value.setString(str);
+    JS_DefineElement(cx, locales, i, value, JSPROP_ENUMERATE);
+  }
+  retval.setObject(*locales);
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+mozLocaleService::GetAppLocale(JSContext* cx, nsACString& retval)
+{
+  nsAutoCString appLocale;
+  LocaleService::GetInstance()->GetAppLocale(appLocale);
+
+  retval = appLocale;
+
+  return NS_OK;
+}
new file mode 100644
--- /dev/null
+++ b/intl/locale/mozLocaleService.h
@@ -0,0 +1,24 @@
+/* -*- 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/. */
+
+#ifndef mozilla_intl_mozILocaleService_h__
+#define mozilla_intl_mozILocaleService_h__
+
+#include "mozILocaleService.h"
+
+class mozLocaleService : public mozILocaleService
+{
+public:
+  NS_DECL_ISUPPORTS
+  NS_DECL_MOZILOCALESERVICE
+
+  mozLocaleService();
+
+private:
+  virtual ~mozLocaleService();
+};
+
+#endif  /* mozilla_intl_mozILocaleService_h__ */
--- a/intl/locale/nsLocaleConstructors.h
+++ b/intl/locale/nsLocaleConstructors.h
@@ -3,16 +3,17 @@
  * 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/. */
 
 #ifndef nsLocaleConstructors_h__
 #define nsLocaleConstructors_h__
 
 #include "nsCollationCID.h"
 #include "mozilla/ModuleUtils.h"
+#include "mozLocaleService.h"
 #include "nsILocaleService.h"
 #include "nsIScriptableDateFormat.h"
 #include "nsIServiceManager.h"
 #include "nsLanguageAtomService.h"
 #include "nsPlatformCharset.h"
 
 #if defined(XP_MACOSX)
 #define USE_MAC_LOCALE
@@ -51,16 +52,17 @@ ctor_(nsISupports* aOuter, REFNSIID aIID
 }
 
 
 NSLOCALE_MAKE_CTOR(CreateLocaleService, nsILocaleService, NS_NewLocaleService)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsCollationFactory)
 //NS_GENERIC_FACTORY_CONSTRUCTOR(nsScriptableDateTimeFormat)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsLanguageAtomService)
 NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsPlatformCharset, Init)
+NS_GENERIC_FACTORY_CONSTRUCTOR(mozLocaleService)
 
 #ifdef XP_WIN
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsCollationWin)
 #endif
 
 #ifdef USE_UNIX_LOCALE
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsCollationUnix)
 #endif