Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / base / IntlUtils.cpp
blob7292b9d7797b62c149a174b6c299c047e197452c
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "IntlUtils.h"
9 #include "mozilla/dom/ToJSValue.h"
10 #include "mozilla/intl/LocaleService.h"
11 #include "mozIMozIntl.h"
12 #include "nsContentUtils.h"
14 namespace mozilla::dom {
16 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(IntlUtils, mWindow)
17 NS_IMPL_CYCLE_COLLECTING_ADDREF(IntlUtils)
18 NS_IMPL_CYCLE_COLLECTING_RELEASE(IntlUtils)
19 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(IntlUtils)
20 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
21 NS_INTERFACE_MAP_ENTRY(nsISupports)
22 NS_INTERFACE_MAP_END
24 IntlUtils::IntlUtils(nsPIDOMWindowInner* aWindow) : mWindow(aWindow) {}
26 IntlUtils::~IntlUtils() = default;
28 JSObject* IntlUtils::WrapObject(JSContext* aCx,
29 JS::Handle<JSObject*> aGivenProto) {
30 return IntlUtils_Binding::Wrap(aCx, this, aGivenProto);
33 void IntlUtils::GetDisplayNames(const Sequence<nsString>& aLocales,
34 const DisplayNameOptions& aOptions,
35 DisplayNameResult& aResult,
36 ErrorResult& aError) {
37 MOZ_ASSERT(nsContentUtils::IsCallerChrome() ||
38 nsContentUtils::IsCallerUAWidget());
40 nsCOMPtr<mozIMozIntl> mozIntl = do_GetService("@mozilla.org/mozintl;1");
41 if (!mozIntl) {
42 aError.Throw(NS_ERROR_UNEXPECTED);
43 return;
46 aError.MightThrowJSException();
48 // Need to enter privileged junk scope since mozIntl implementation is in
49 // chrome JS and passing XBL JS there shouldn't work.
50 AutoJSAPI jsapi;
51 if (!jsapi.Init(xpc::PrivilegedJunkScope())) {
52 aError.Throw(NS_ERROR_FAILURE);
53 return;
55 JSContext* cx = jsapi.cx();
57 // Prepare parameter for getDisplayNames().
58 JS::Rooted<JS::Value> locales(cx);
59 if (!ToJSValue(cx, aLocales, &locales)) {
60 aError.StealExceptionFromJSContext(cx);
61 return;
64 JS::Rooted<JS::Value> options(cx);
65 if (!ToJSValue(cx, aOptions, &options)) {
66 aError.StealExceptionFromJSContext(cx);
67 return;
70 // Now call the method.
71 JS::Rooted<JS::Value> retVal(cx);
72 nsresult rv = mozIntl->GetDisplayNamesDeprecated(locales, options, &retVal);
73 if (NS_FAILED(rv)) {
74 aError.Throw(rv);
75 return;
78 if (!retVal.isObject() || !JS_WrapValue(cx, &retVal)) {
79 aError.Throw(NS_ERROR_FAILURE);
80 return;
83 // Return the result as DisplayNameResult.
84 if (!aResult.Init(cx, retVal)) {
85 aError.Throw(NS_ERROR_FAILURE);
89 bool IntlUtils::IsAppLocaleRTL() {
90 MOZ_ASSERT(nsContentUtils::IsCallerChrome() ||
91 nsContentUtils::IsCallerUAWidget());
92 return intl::LocaleService::GetInstance()->IsAppLocaleRTL();
95 } // namespace mozilla::dom