gecko-dev/dom/base/IntlUtils.cpp
André Bargull 5680bb810a Bug 1639515 - Part 2: Add mozIntl.DisplayNames. r=platform-i18n-reviewers,gregtatum
Expose `Intl.DisplayNames` to `mozIntl`. The exposed `Intl.DisplayNames` version
supports date-time types, which have been removed from the spec proposal, but
may be re-enabled at some point in the future.

The existing `getDisplayNames` WebIDL function had to be renamed, because WebIDL
doesn't allow specify an attribute named `DisplayNames` and a separate function
called `getDisplayNames`.

Drive-by change:
- Removed an out-dated comment in "mozIMozIntlHelper.idl" which mentioned calling
  `addDateTimeFormatConstructor` twice throws an error.

Differential Revision: https://phabricator.services.mozilla.com/D76114
2021-09-14 09:20:31 +00:00

96 lines
2.8 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "IntlUtils.h"
#include "mozilla/dom/ToJSValue.h"
#include "mozilla/intl/LocaleService.h"
#include "mozIMozIntl.h"
#include "nsContentUtils.h"
namespace mozilla::dom {
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(IntlUtils, mWindow)
NS_IMPL_CYCLE_COLLECTING_ADDREF(IntlUtils)
NS_IMPL_CYCLE_COLLECTING_RELEASE(IntlUtils)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(IntlUtils)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
IntlUtils::IntlUtils(nsPIDOMWindowInner* aWindow) : mWindow(aWindow) {}
IntlUtils::~IntlUtils() = default;
JSObject* IntlUtils::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return IntlUtils_Binding::Wrap(aCx, this, aGivenProto);
}
void IntlUtils::GetDisplayNames(const Sequence<nsString>& aLocales,
const DisplayNameOptions& aOptions,
DisplayNameResult& aResult,
ErrorResult& aError) {
MOZ_ASSERT(nsContentUtils::IsCallerChrome() ||
nsContentUtils::IsCallerUAWidget());
nsCOMPtr<mozIMozIntl> mozIntl = do_GetService("@mozilla.org/mozintl;1");
if (!mozIntl) {
aError.Throw(NS_ERROR_UNEXPECTED);
return;
}
aError.MightThrowJSException();
// Need to enter privileged junk scope since mozIntl implementation is in
// chrome JS and passing XBL JS there shouldn't work.
AutoJSAPI jsapi;
if (!jsapi.Init(xpc::PrivilegedJunkScope())) {
aError.Throw(NS_ERROR_FAILURE);
return;
}
JSContext* cx = jsapi.cx();
// Prepare parameter for getDisplayNames().
JS::Rooted<JS::Value> locales(cx);
if (!ToJSValue(cx, aLocales, &locales)) {
aError.StealExceptionFromJSContext(cx);
return;
}
JS::Rooted<JS::Value> options(cx);
if (!ToJSValue(cx, aOptions, &options)) {
aError.StealExceptionFromJSContext(cx);
return;
}
// Now call the method.
JS::Rooted<JS::Value> retVal(cx);
nsresult rv = mozIntl->GetDisplayNamesDeprecated(locales, options, &retVal);
if (NS_FAILED(rv)) {
aError.Throw(rv);
return;
}
if (!retVal.isObject() || !JS_WrapValue(cx, &retVal)) {
aError.Throw(NS_ERROR_FAILURE);
return;
}
// Return the result as DisplayNameResult.
if (!aResult.Init(cx, retVal)) {
aError.Throw(NS_ERROR_FAILURE);
}
}
bool IntlUtils::IsAppLocaleRTL() {
MOZ_ASSERT(nsContentUtils::IsCallerChrome() ||
nsContentUtils::IsCallerUAWidget());
return intl::LocaleService::GetInstance()->IsAppLocaleRTL();
}
} // namespace mozilla::dom