Bug 1344596 - Allow OSPreferences API without ENABLE_INTL_API. r?gandalf draft
authorMakoto Kato <m_kato@ga2.so-net.ne.jp>
Mon, 06 Mar 2017 11:03:46 +0900
changeset 493849 188007ecd0e1a30e274bdeedf1271089a2d6de5c
parent 493721 8d026c60151005ad942e3d4389318fe28a0c8c54
child 547949 fd92565d775d186e3dcfb0e15d1d8d353eb65f77
push id47866
push userm_kato@ga2.so-net.ne.jp
push dateMon, 06 Mar 2017 07:00:24 +0000
reviewersgandalf
bugs1344596
milestone54.0a1
Bug 1344596 - Allow OSPreferences API without ENABLE_INTL_API. r?gandalf Since we have to keep --with-intl-api=no setting, we allow OSPrefrences API wiout ENABLE_INTL_API. MozReview-Commit-ID: 8sQHW6iq9Ss
intl/locale/OSPreferences.cpp
intl/locale/OSPreferences.h
intl/locale/moz.build
intl/locale/tests/unit/xpcshell.ini
intl/locale/unix/OSPreferences_unix.cpp
--- a/intl/locale/OSPreferences.cpp
+++ b/intl/locale/OSPreferences.cpp
@@ -9,18 +9,20 @@
  * platform-specific private methods.
  */
 
 #include "OSPreferences.h"
 
 #include "mozilla/ClearOnShutdown.h"
 #include "mozilla/Services.h"
 #include "nsIObserverService.h"
+#ifdef ENABLE_INTL_API
 #include "unicode/udat.h"
 #include "unicode/udatpg.h"
+#endif
 
 using namespace mozilla::intl;
 
 NS_IMPL_ISUPPORTS(OSPreferences, mozIOSPreferences)
 
 mozilla::StaticRefPtr<OSPreferences> OSPreferences::sInstance;
 
 OSPreferences*
@@ -66,40 +68,45 @@ OSPreferences::Refresh()
  * It attempts to retrieve as much of the locale ID as possible, cutting
  * out bits that are not understood (non-strict behavior of ICU).
  *
  * It returns true if the canonicalization was successful.
  */
 bool
 OSPreferences::CanonicalizeLanguageTag(nsCString& aLoc)
 {
+#ifdef ENABLE_INTL_API
   char langTag[512];
 
   UErrorCode status = U_ZERO_ERROR;
 
   int32_t langTagLen =
     uloc_toLanguageTag(aLoc.get(), langTag, sizeof(langTag) - 1, false, &status);
 
   if (U_FAILURE(status)) {
     return false;
   }
 
   aLoc.Assign(langTag, langTagLen);
   return true;
+#else
+  return false;
+#endif
 }
 
 /**
  * This method retrieves from ICU the best pattern for a given date/time style.
  */
 bool
 OSPreferences::GetDateTimePatternForStyle(DateTimeFormatStyle aDateStyle,
                                           DateTimeFormatStyle aTimeStyle,
                                           const nsACString& aLocale,
                                           nsAString& aRetVal)
 {
+#ifdef ENABLE_INTL_API
   UDateFormatStyle timeStyle = UDAT_NONE;
   UDateFormatStyle dateStyle = UDAT_NONE;
 
   switch (aTimeStyle) {
     case DateTimeFormatStyle::None:
       timeStyle = UDAT_NONE;
       break;
     case DateTimeFormatStyle::Short:
@@ -153,16 +160,19 @@ OSPreferences::GetDateTimePatternForStyl
 
   int32_t patsize = udat_toPattern(df, false, pattern, kPatternMax, &status);
   udat_close(df);
   if (U_FAILURE(status)) {
     return false;
   }
   aRetVal.Assign((const char16_t*)pattern, patsize);
   return true;
+#else
+  return false;
+#endif
 }
 
 
 /**
  * This method retrieves from ICU the best skeleton for a given date/time style.
  *
  * This is useful for cases where an OS does not provide its own patterns,
  * but provide ability to customize the skeleton, like alter hourCycle setting.
@@ -170,16 +180,17 @@ OSPreferences::GetDateTimePatternForStyl
  * The returned value is a skeleton that matches the styles.
  */
 bool
 OSPreferences::GetDateTimeSkeletonForStyle(DateTimeFormatStyle aDateStyle,
                                            DateTimeFormatStyle aTimeStyle,
                                            const nsACString& aLocale,
                                            nsAString& aRetVal)
 {
+#ifdef ENABLE_INTL_API
   nsAutoString pattern;
   if (!GetDateTimePatternForStyle(aDateStyle, aTimeStyle, aLocale, pattern)) {
     return false;
   }
 
   const int32_t kSkeletonMax = 160;
   UChar skeleton[kSkeletonMax];
 
@@ -189,32 +200,36 @@ OSPreferences::GetDateTimeSkeletonForSty
     skeleton, kSkeletonMax, &status
   );
   if (U_FAILURE(status)) {
     return false;
   }
 
   aRetVal.Assign((const char16_t*)skeleton, skelsize);
   return true;
+#else
+  return false;
+#endif
 }
 
 /**
  * This function is a counterpart to GetDateTimeSkeletonForStyle.
  *
  * It takes a skeleton and returns the best available pattern for a given locale
  * that represents the provided skeleton.
  *
  * For example:
  * "Hm" skeleton for "en-US" will return "H:m"
  */
 bool
 OSPreferences::GetPatternForSkeleton(const nsAString& aSkeleton,
                                      const nsACString& aLocale,
                                      nsAString& aRetVal)
 {
+#ifdef ENABLE_INTL_API
   UErrorCode status = U_ZERO_ERROR;
   UDateTimePatternGenerator* pg = udatpg_open(PromiseFlatCString(aLocale).get(), &status);
   if (U_FAILURE(status)) {
     return false;
   }
 
   int32_t len =
     udatpg_getBestPattern(pg, (const UChar*)aSkeleton.BeginReading(),
@@ -225,43 +240,50 @@ OSPreferences::GetPatternForSkeleton(con
     udatpg_getBestPattern(pg, (const UChar*)aSkeleton.BeginReading(),
                           aSkeleton.Length(), (UChar*)aRetVal.BeginWriting(),
                           len, &status);
   }
 
   udatpg_close(pg);
 
   return U_SUCCESS(status);
+#else
+  return false;
+#endif
 }
 
 /**
  * This function returns a pattern that should be used to join date and time
  * patterns into a single date/time pattern string.
  *
  * It's useful for OSes that do not provide an API to retrieve such combined
  * pattern.
  *
  * An example output is "{1}, {0}".
  */
 bool
 OSPreferences::GetDateTimeConnectorPattern(const nsACString& aLocale,
                                            nsAString& aRetVal)
 {
+#ifdef ENABLE_INTL_API
   UErrorCode status = U_ZERO_ERROR;
   UDateTimePatternGenerator* pg = udatpg_open(PromiseFlatCString(aLocale).get(), &status);
   if (U_FAILURE(status)) {
     return false;
   }
 
   int32_t resultSize;
   const UChar* value = udatpg_getDateTimeFormat(pg, &resultSize);
   MOZ_ASSERT(resultSize >= 0);
 
   aRetVal.Assign((char16_t*)value, resultSize);
   return true;
+#else
+  return false;
+#endif
 }
 
 /**
  * mozIOSPreferences methods
  */
 NS_IMETHODIMP
 OSPreferences::GetSystemLocales(uint32_t* aCount, char*** aOutArray)
 {
--- a/intl/locale/OSPreferences.h
+++ b/intl/locale/OSPreferences.h
@@ -4,17 +4,19 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef mozilla_intl_IntlOSPreferences_h__
 #define mozilla_intl_IntlOSPreferences_h__
 
 #include "mozilla/StaticPtr.h"
 #include "nsString.h"
 #include "nsTArray.h"
+#ifdef ENABLE_INTL_API
 #include "unicode/uloc.h"
+#endif
 
 #include "mozIOSPreferences.h"
 
 namespace mozilla {
 namespace intl {
 
 /**
  * OSPreferences API provides a set of methods for retrieving information from
--- a/intl/locale/moz.build
+++ b/intl/locale/moz.build
@@ -1,22 +1,16 @@
 # -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
 # vim: set filetype=python:
 # 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/.
 
 XPCSHELL_TESTS_MANIFESTS += ['tests/unit/xpcshell.ini']
 
-if CONFIG['ENABLE_INTL_API']:
-    SOURCES += ['OSPreferences.cpp']
-    EXPORTS.mozilla.intl += [
-        'OSPreferences.h',
-    ]
-
 toolkit = CONFIG['MOZ_WIDGET_TOOLKIT']
 
 if toolkit == 'windows':
     DIRS += ['windows']
 elif toolkit == 'cocoa':
     DIRS += ['mac']
 else:
     DIRS += ['unix']
@@ -42,27 +36,29 @@ EXPORTS += [
     'nsIPlatformCharset.h',
     'nsPosixLocale.h',
     'nsUConvPropertySearch.h',
     'nsWin32Locale.h',
 ]
 
 EXPORTS.mozilla.intl += [
     'LocaleService.h',
+    'OSPreferences.h',
 ]
 
 UNIFIED_SOURCES += [
     'DateTimeFormat.cpp',
     'LocaleService.cpp',
     'nsCollation.cpp',
     'nsLanguageAtomService.cpp',
     'nsLocale.cpp',
     'nsLocaleService.cpp',
     'nsScriptableDateFormat.cpp',
     'nsUConvPropertySearch.cpp',
+    'OSPreferences.cpp',
 ]
 
 EXTRA_JS_MODULES += [
     'PluralForm.jsm',
 ]
 
 FINAL_LIBRARY = 'xul'
 
--- a/intl/locale/tests/unit/xpcshell.ini
+++ b/intl/locale/tests/unit/xpcshell.ini
@@ -19,8 +19,9 @@ skip-if = toolkit != "cocoa"
 skip-if = toolkit == "android" # bug 1309447
 
 [test_pluralForm.js]
 [test_pluralForm_english.js]
 [test_pluralForm_makeGetter.js]
 
 [test_localeService.js]
 [test_osPreferences.js]
+skip-if = toolkit == "android" # bug 1344596
--- a/intl/locale/unix/OSPreferences_unix.cpp
+++ b/intl/locale/unix/OSPreferences_unix.cpp
@@ -1,30 +1,41 @@
 /* -*- 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 "nsPosixLocale.h"
 #include "OSPreferences.h"
 
 using namespace mozilla::intl;
 
 bool
 OSPreferences::ReadSystemLocales(nsTArray<nsCString>& aLocaleList)
 {
+#ifdef ENABLE_INTL_API
   MOZ_ASSERT(aLocaleList.IsEmpty());
 
   nsAutoCString defaultLang(uloc_getDefault());
 
   if (CanonicalizeLanguageTag(defaultLang)) {
     aLocaleList.AppendElement(defaultLang);
     return true;
   }
   return false;
+#else
+  nsAutoString locale;
+  nsPosixLocale::GetXPLocale(getenv("LANG"), locale);
+  if (locale.IsEmpty()) {
+    locale.AssignLiteral("en-US");
+  }
+  aLocaleList.AppendElement(NS_LossyConvertUTF16toASCII(locale));
+  return true;
+#endif
 }
 
 bool
 OSPreferences::ReadDateTimePattern(DateTimeFormatStyle aDateStyle,
                                    DateTimeFormatStyle aTimeStyle,
                                    const nsACString& aLocale, nsAString& aRetVal)
 {
   return false;