Bug 1340396 - Add window.getAppLocales() for ChromeOrXBL. r=smaug
authorJessica Jong <jjong>
Sun, 19 Feb 2017 22:11:00 -0500
changeset 343879 711456564e1e73cc1e5c101e55a8f28bfb43a108
parent 343878 f305383515a1f74e0c34edd81f0590d3f44c45c1
child 343880 3a8c78a6146fa9fb3fa9f73d0547b6520e26a470
push id87212
push userryanvm@gmail.com
push dateMon, 20 Feb 2017 19:42:07 +0000
treeherdermozilla-inbound@3a8c78a6146f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1340396
milestone54.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 1340396 - Add window.getAppLocales() for ChromeOrXBL. r=smaug
dom/base/nsGlobalWindow.cpp
dom/base/nsGlobalWindow.h
dom/tests/mochitest/chrome/chrome.ini
dom/tests/mochitest/chrome/test_window_getAppLocales.html
dom/webidl/Window.webidl
--- a/dom/base/nsGlobalWindow.cpp
+++ b/dom/base/nsGlobalWindow.cpp
@@ -49,16 +49,17 @@
 #include "nsScriptNameSpaceManager.h"
 #include "nsISlowScriptDebug.h"
 #include "nsWindowMemoryReporter.h"
 #include "WindowNamedPropertiesHandler.h"
 #include "nsFrameSelection.h"
 #include "nsNetUtil.h"
 #include "nsVariant.h"
 #include "nsPrintfCString.h"
+#include "mozilla/intl/LocaleService.h"
 
 // Helper Classes
 #include "nsJSUtils.h"
 #include "jsapi.h"              // for JSAutoRequest
 #include "jswrapper.h"
 #include "nsCharSeparatedTokenizer.h"
 #include "nsReadableUtils.h"
 #include "nsDOMClassInfo.h"
@@ -14912,11 +14913,22 @@ nsGlobalWindow::GetPaintWorklet(ErrorRes
     }
 
     mPaintWorklet = new Worklet(AsInner(), principal, Worklet::ePaintWorklet);
   }
 
   return mPaintWorklet;
 }
 
+void
+nsGlobalWindow::GetAppLocales(nsTArray<nsString>& aLocales)
+{
+  nsTArray<nsCString> appLocales;
+  mozilla::intl::LocaleService::GetInstance()->GetAppLocales(appLocales);
+
+  for (uint32_t i = 0; i < appLocales.Length(); i++) {
+    aLocales.AppendElement(NS_ConvertUTF8toUTF16(appLocales[i]));
+  }
+}
+
 template class nsPIDOMWindow<mozIDOMWindowProxy>;
 template class nsPIDOMWindow<mozIDOMWindow>;
 template class nsPIDOMWindow<nsISupports>;
--- a/dom/base/nsGlobalWindow.h
+++ b/dom/base/nsGlobalWindow.h
@@ -942,16 +942,19 @@ public:
   ConvertDialogOptions(const nsAString& aOptions, nsAString& aResult);
 
   mozilla::dom::Worklet*
   GetAudioWorklet(mozilla::ErrorResult& aRv);
 
   mozilla::dom::Worklet*
   GetPaintWorklet(mozilla::ErrorResult& aRv);
 
+  void
+  GetAppLocales(nsTArray<nsString>& aLocales);
+
 protected:
   bool AlertOrConfirm(bool aAlert, const nsAString& aMessage,
                       nsIPrincipal& aSubjectPrincipal,
                       mozilla::ErrorResult& aError);
 
 public:
   void Alert(nsIPrincipal& aSubjectPrincipal,
              mozilla::ErrorResult& aError);
--- a/dom/tests/mochitest/chrome/chrome.ini
+++ b/dom/tests/mochitest/chrome/chrome.ini
@@ -82,8 +82,9 @@ skip-if = os == 'linux'
 [test_sandbox_postMessage.html]
 [test_selectAtPoint.html]
 [test_sizemode_attribute.xul]
 tags = fullscreen
 skip-if = os != 'win'
 [test_subscript_bindings.xul]
 [test_xray_event_constructor.xul]
 [test_clipboard_events_chrome.html]
+[test_window_getAppLocales.html]
new file mode 100644
--- /dev/null
+++ b/dom/tests/mochitest/chrome/test_window_getAppLocales.html
@@ -0,0 +1,23 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=1337234
+-->
+<head>
+  <title>Test for Bug 1337234</title>
+  <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1337234">Mozilla Bug 1337234</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+<script>
+
+let appLocales = window.getAppLocales();
+ok(appLocales.length > 0, "getAppLocales returns at least one locale.");
+is(appLocales[0], "en-US", "The first app locale should be en-US.");
+
+</script>
+</body>
+</html>
--- a/dom/webidl/Window.webidl
+++ b/dom/webidl/Window.webidl
@@ -514,8 +514,23 @@ callback IdleRequestCallback = void (Idl
  * WARNING: Do not use this unless you are familiar with the issues that
  * taking opener state into account is designed to address (or else you may
  * introduce security issues).  If in doubt, use |isSecureContext|.  In
  * particular do not use this to gate access to JavaScript APIs.
  */
 partial interface Window {
   [ChromeOnly] readonly attribute boolean isSecureContextIfOpenerIgnored;
 };
+
+partial interface Window {
+/**
+  * 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
+  * used for all APIs that accept list of locales, like ECMA402 and L10n APIs.
+  *
+  * This API always returns at least one locale.
+  *
+  * Example: ["en-US", "de", "pl", "sr-Cyrl", "zh-Hans-HK"]
+  */
+  [Func="IsChromeOrXBL"]
+  sequence<DOMString> getAppLocales();
+};