Bug 867501 - Pref allows JS locale to be set to US English/C. r=khuey
authorArthur Edelstein <arthuredelstein@gmail.com>
Tue, 29 Dec 2015 13:56:00 +0100 (2015-12-29)
changeset 277905 52d635f2b33da982930f586f070c9fee58544f17
parent 277904 cb7ddeee6364905a0184e7ceccfab9d1dfea25f9
child 277906 9debeca68327498c74d80604a2aa8ee38d083219
push id29838
push userkwierso@gmail.com
push dateThu, 31 Dec 2015 01:36:02 +0000 (2015-12-31)
treeherdermozilla-central@22f51211915b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskhuey
bugs867501
milestone46.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 867501 - Pref allows JS locale to be set to US English/C. r=khuey
js/xpconnect/src/XPCLocale.cpp
toolkit/xre/nsAppRunner.cpp
toolkit/xre/nsAppRunner.h
toolkit/xre/nsEmbedFunctions.cpp
--- a/js/xpconnect/src/XPCLocale.cpp
+++ b/js/xpconnect/src/XPCLocale.cpp
@@ -12,16 +12,17 @@
 #include "nsJSUtils.h"
 #include "nsIPlatformCharset.h"
 #include "nsILocaleService.h"
 #include "nsICollation.h"
 #include "nsUnicharUtils.h"
 #include "nsComponentManagerUtils.h"
 #include "nsServiceManagerUtils.h"
 #include "mozilla/dom/EncodingUtils.h"
+#include "mozilla/Preferences.h"
 #include "nsIUnicodeDecoder.h"
 
 #include "xpcpublic.h"
 
 using namespace JS;
 using mozilla::dom::EncodingUtils;
 
 /**
@@ -247,16 +248,25 @@ private:
 };
 
 bool
 xpc_LocalizeRuntime(JSRuntime* rt)
 {
   JS_SetLocaleCallbacks(rt, new XPCLocaleCallbacks());
 
   // Set the default locale.
+
+  // Check a pref to see if we should use US English locale regardless
+  // of the system locale.
+  if (Preferences::GetBool("javascript.use_us_english_locale", false)) {
+    return JS_SetDefaultLocale(rt, "en-US");
+  }
+
+  // No pref has been found, so get the default locale from the
+  // application's locale.
   nsCOMPtr<nsILocaleService> localeService =
     do_GetService(NS_LOCALESERVICE_CONTRACTID);
   if (!localeService)
     return false;
 
   nsCOMPtr<nsILocale> appLocale;
   nsresult rv = localeService->GetApplicationLocale(getter_AddRefs(appLocale));
   if (NS_FAILED(rv))
--- a/toolkit/xre/nsAppRunner.cpp
+++ b/toolkit/xre/nsAppRunner.cpp
@@ -140,16 +140,17 @@
 #include "nsToolkitCompsCID.h"
 
 #include "nsINIParser.h"
 #include "mozilla/Omnijar.h"
 #include "mozilla/StartupTimeline.h"
 #include "mozilla/LateWriteChecks.h"
 
 #include <stdlib.h>
+#include <locale.h>
 
 #ifdef XP_UNIX
 #include <sys/stat.h>
 #include <unistd.h>
 #include <pwd.h>
 #endif
 
 #ifdef XP_WIN
@@ -4145,16 +4146,18 @@ XREMain::XRE_mainRun()
       // Set the new profile as the default after we're done cleaning up the old default.
       rv = SetCurrentProfileAsDefault(mProfileSvc, mProfD);
       if (NS_FAILED(rv)) NS_WARNING("Could not set current profile as the default");
     }
   }
 
   mDirProvider.DoStartup();
 
+  OverrideDefaultLocaleIfNeeded();
+
 #ifdef MOZ_CRASHREPORTER
   nsCString userAgentLocale;
   // Try a localized string first. This pref is always a localized string in
   // Fennec, and might be elsewhere, too.
   if (NS_SUCCEEDED(Preferences::GetLocalizedCString("general.useragent.locale", &userAgentLocale))) {
     CrashReporter::AnnotateCrashReport(NS_LITERAL_CSTRING("useragent_locale"), userAgentLocale);
   } else if (NS_SUCCEEDED(Preferences::GetCString("general.useragent.locale", &userAgentLocale))) {
     CrashReporter::AnnotateCrashReport(NS_LITERAL_CSTRING("useragent_locale"), userAgentLocale);
@@ -4803,8 +4806,20 @@ SetupErrorHandling(const char* progname)
   _CrtSetReportHook(MSCRTReportHook);
 #endif
 
   InstallSignalHandlers(progname);
 
   // Unbuffer stdout, needed for tinderbox tests.
   setbuf(stdout, 0);
 }
+
+void
+OverrideDefaultLocaleIfNeeded() {
+  // Read pref to decide whether to override default locale with US English.
+  if (mozilla::Preferences::GetBool("javascript.use_us_english_locale", false)) {
+    // Set the application-wide C-locale. Needed to resist fingerprinting
+    // of Date.toLocaleFormat(). We use the locale to "C.UTF-8" if possible,
+    // to avoid interfering with non-ASCII keyboard input on some Linux desktops.
+    // Otherwise fall back to the "C" locale, which is available on all platforms.
+    setlocale(LC_ALL, "C.UTF-8") || setlocale(LC_ALL, "C");
+  }
+}
--- a/toolkit/xre/nsAppRunner.h
+++ b/toolkit/xre/nsAppRunner.h
@@ -88,16 +88,19 @@ NS_NewToolkitProfileFactory(nsIFactory* 
  */
 nsresult
 NS_LockProfilePath(nsIFile* aPath, nsIFile* aTempPath,
                    nsIProfileUnlocker* *aUnlocker, nsIProfileLock* *aResult);
 
 void
 WriteConsoleLog();
 
+void
+OverrideDefaultLocaleIfNeeded();
+
 #ifdef XP_WIN
 void
 UseParentConsole();
 
 BOOL
 WinLaunchChild(const wchar_t *exePath, int argc,
                char **argv, HANDLE userToken = nullptr,
                HANDLE *hProcess = nullptr);
--- a/toolkit/xre/nsEmbedFunctions.cpp
+++ b/toolkit/xre/nsEmbedFunctions.cpp
@@ -614,16 +614,18 @@ XRE_InitChildProcess(int aArgc,
 #endif
 
 #if defined(MOZ_SANDBOX) && defined(XP_WIN)
       // We need to do this after the process has been initialised, as
       // InitLoggingIfRequired may need access to prefs.
       mozilla::sandboxing::InitLoggingIfRequired();
 #endif
 
+      OverrideDefaultLocaleIfNeeded();
+
       // Run the UI event loop on the main thread.
       uiMessageLoop.MessageLoop::Run();
 
       // Allow ProcessChild to clean up after itself before going out of
       // scope and being deleted
       process->CleanUp();
       mozilla::Omnijar::CleanUp();
     }