Ticket #13019: 0001-Bug-5926-Spoof-default-JavaScript-locale-as-en-US.patch

File 0001-Bug-5926-Spoof-default-JavaScript-locale-as-en-US.patch, 6.2 KB (added by arthuredelstein, 3 years ago)
  • js/src/jsapi.cpp

    From ab41c4f58da92f098e8f6a13fc157fdd1a76bebd Mon Sep 17 00:00:00 2001
    From: Arthur Edelstein <arthuredelstein@gmail.com>
    Date: Fri, 5 Sep 2014 16:59:10 -0700
    Subject: [PATCH 1/3] Bug #5926: Spoof default JavaScript locale as "en-US"
    
    ---
     js/src/jsapi.cpp            |   7 +++
     js/src/jsapi.h              |   9 ++++
     xpcom/build/nsXPComInit.cpp | 104 ++++++++++++++++++++++++++++++++++++++++++++
     3 files changed, 120 insertions(+)
    
    diff --git a/js/src/jsapi.cpp b/js/src/jsapi.cpp
    index 7b1c886..d3fea44 100644
    a b JS_SetDefaultLocale(JSRuntime *rt, const char *locale) 
    58795879    return rt->setDefaultLocale(locale);
    58805880}
    58815881
     5882JS_PUBLIC_API(const char*)
     5883JS_GetDefaultLocale(JSRuntime *rt)
     5884{
     5885    AssertHeapIsIdle(rt);
     5886    return JS_strdup(rt, rt->getDefaultLocale());
     5887}
     5888
    58825889JS_PUBLIC_API(void)
    58835890JS_ResetDefaultLocale(JSRuntime *rt)
    58845891{
  • js/src/jsapi.h

    diff --git a/js/src/jsapi.h b/js/src/jsapi.h
    index 5ae1f86..98543e5 100644
    a b extern JS_PUBLIC_API(bool) 
    43254325JS_SetDefaultLocale(JSRuntime *rt, const char *locale);
    43264326
    43274327/*
     4328 * Returns the default locale for the ECMAScript Internationalization API
     4329 * (Intl.Collator, Intl.NumberFormat, Intl.DateTimeFormat).
     4330 * Note that the Internationalization API encourages clients to
     4331 * specify their own locales.
     4332 */
     4333extern JS_PUBLIC_API(const char*)
     4334JS_GetDefaultLocale(JSRuntime *rt);
     4335
     4336/*
    43284337 * Reset the default locale to OS defaults.
    43294338 */
    43304339extern JS_PUBLIC_API(void)
  • xpcom/build/nsXPComInit.cpp

    diff --git a/xpcom/build/nsXPComInit.cpp b/xpcom/build/nsXPComInit.cpp
    index aef309f..26b9a06 100644
    a b  
    88
    99#include "mozilla/Atomics.h"
    1010#include "mozilla/Poison.h"
     11#include "mozilla/Preferences.h"
    1112#include "mozilla/XPCOM.h"
    1213#include "nsXULAppAPI.h"
    1314
     
    4142#include "nsSupportsPrimitives.h"
    4243#include "nsConsoleService.h"
    4344
     45#include "nsIJSRuntimeService.h"
     46
    4447#include "nsComponentManager.h"
    4548#include "nsCategoryManagerUtils.h"
    4649#include "nsIServiceManager.h"
    NS_IMPL_ISUPPORTS(NesteggReporter, nsIMemoryReporter) 
    443446/* static */ template<> Atomic<size_t> CountingAllocatorBase<NesteggReporter>::sAmount(0);
    444447#endif /* MOZ_WEBM */
    445448
     449// Anonymous namespace for customizing the default locale that JavaScript
     450// uses, according to the value of the "javascript.default_locale" pref.
     451// The current default locale can be detected in JavaScript by calling
     452// `Intl.DateTimeFormat().resolvedOptions().locale`
     453namespace {
     454
     455#define DEFAULT_LOCALE_PREF "javascript.default_locale"
     456
     457class DefaultJSLocaleSetter : public nsIObserver
     458{
     459public:
     460  DefaultJSLocaleSetter();
     461  static DefaultJSLocaleSetter* newDefaultJSLocaleSetter();
     462  const char* systemLocale;
     463  const char* jsLocale;
     464  NS_DECL_ISUPPORTS
     465  NS_DECL_NSIOBSERVER
     466  nsresult Run();
     467  ~DefaultJSLocaleSetter();
     468};
     469
     470NS_IMPL_ISUPPORTS(DefaultJSLocaleSetter, nsIObserver);
     471
     472// static
     473nsresult
     474DefaultJSLocaleSetter::Run() {
     475  NS_ASSERTION(NS_IsMainThread(), "DefaultJSLocaleSetter::Observe can only be accessed in main thread");
     476  // Read the pref, which may contain a custom locale to be used in JavaScript.
     477  nsAutoCString prefLocale;
     478  mozilla::Preferences::GetCString(DEFAULT_LOCALE_PREF, &prefLocale);
     479  // Set the application-wide C-locale. Needed for Date.toLocaleFormat().
     480  setlocale(LC_ALL, prefLocale.IsEmpty() ? systemLocale : prefLocale.get());
     481  // Get a reference to the JavaScript Runtime object.
     482  nsresult rv;
     483  nsCOMPtr<nsIJSRuntimeService> rts = do_GetService("@mozilla.org/js/xpc/RuntimeService;1", &rv);
     484  if (NS_FAILED(rv)) return rv;
     485  JSRuntime *rt;
     486  rts->GetRuntime(&rt);
     487  if (!rt) return NS_ERROR_FAILURE;
     488  // Now override the JavaScript Runtime Locale that is used by the Intl API
     489  // as well as Date.toLocaleString, Number.toLocaleString, and String.localeCompare.
     490  JS_SetDefaultLocale(rt, prefLocale.IsEmpty() ? jsLocale : prefLocale.get());
     491  return NS_OK;
     492}
     493
     494NS_IMETHODIMP
     495DefaultJSLocaleSetter::Observe(nsISupports* aSubject,
     496                   const char* aTopic,
     497                   const char16_t* someData) {
     498  // Update the locale according to the latest pref value.
     499  return Run();
     500}
     501
     502DefaultJSLocaleSetter::DefaultJSLocaleSetter() {
     503  // Get the default system locale. To be used if pref is not available.
     504  systemLocale = setlocale(LC_ALL,NULL);
     505  nsresult rv;
     506  nsCOMPtr<nsIJSRuntimeService> rts = do_GetService("@mozilla.org/js/xpc/RuntimeService;1", &rv);
     507  if (NS_FAILED(rv)) return;
     508  JSRuntime *rt;
     509  rts->GetRuntime(&rt);
     510  jsLocale = JS_GetDefaultLocale(rt);
     511  // Now update the locale according to the current pref value.
     512  DefaultJSLocaleSetter::Run();
     513}
     514
     515//static
     516DefaultJSLocaleSetter*
     517DefaultJSLocaleSetter::newDefaultJSLocaleSetter() {
     518  DefaultJSLocaleSetter* setter = new DefaultJSLocaleSetter();
     519  // Get a handle to the prefs service.
     520  nsCOMPtr<nsIPrefBranch> prefs;
     521  prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
     522  // Watch the pref so we can update the locale whenever it changes.
     523  prefs->AddObserver(DEFAULT_LOCALE_PREF, setter, false);
     524  return setter;
     525}
     526
     527DefaultJSLocaleSetter::~DefaultJSLocaleSetter() {
     528  // Get a handle to the prefs service.
     529  nsCOMPtr<nsIPrefBranch> prefs;
     530  prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
     531  // Stop watching the pref.
     532  prefs->RemoveObserver(DEFAULT_LOCALE_PREF, this);
     533}
     534
     535// The customizer is a singleton.
     536static DefaultJSLocaleSetter* sDefaultJSLocaleSetter;
     537
     538} // anonymous namespace
     539
    446540EXPORT_XPCOM_API(nsresult)
    447541NS_InitXPCOM2(nsIServiceManager* *result,
    448542              nsIFile* binDirectory,
    NS_InitXPCOM2(nsIServiceManager* *result, 
    701795    mozilla::eventtracer::Init();
    702796#endif
    703797
     798    // Start watching the javascript.default_locale pref.
     799    if (!sDefaultJSLocaleSetter) {
     800      sDefaultJSLocaleSetter = DefaultJSLocaleSetter::newDefaultJSLocaleSetter();
     801    }
     802
    704803    return NS_OK;
    705804}
    706805
    ShutdownXPCOM(nsIServiceManager* servMgr) 
    757856        NS_RUNTIMEABORT("Shutdown on wrong thread");
    758857    }
    759858
     859    // Stop watching the javascript.default_locale pref
     860    if (sDefaultJSLocaleSetter) {
     861      delete sDefaultJSLocaleSetter;
     862    }
     863
    760864    nsresult rv;
    761865    nsCOMPtr<nsISimpleEnumerator> moduleLoaders;
    762866