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/. */
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
)
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");
42 aError
.Throw(NS_ERROR_UNEXPECTED
);
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.
51 if (!jsapi
.Init(xpc::PrivilegedJunkScope())) {
52 aError
.Throw(NS_ERROR_FAILURE
);
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
);
64 JS::Rooted
<JS::Value
> options(cx
);
65 if (!ToJSValue(cx
, aOptions
, &options
)) {
66 aError
.StealExceptionFromJSContext(cx
);
70 // Now call the method.
71 JS::Rooted
<JS::Value
> retVal(cx
);
72 nsresult rv
= mozIntl
->GetDisplayNamesDeprecated(locales
, options
, &retVal
);
78 if (!retVal
.isObject() || !JS_WrapValue(cx
, &retVal
)) {
79 aError
.Throw(NS_ERROR_FAILURE
);
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