tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

IntlUtils.cpp (2901B)


      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/. */
      6 
      7 #include "IntlUtils.h"
      8 
      9 #include "mozIMozIntl.h"
     10 #include "mozilla/dom/ToJSValue.h"
     11 #include "mozilla/intl/LocaleService.h"
     12 #include "nsContentUtils.h"
     13 
     14 namespace mozilla::dom {
     15 
     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
     23 
     24 IntlUtils::IntlUtils(nsPIDOMWindowInner* aWindow) : mWindow(aWindow) {}
     25 
     26 IntlUtils::~IntlUtils() = default;
     27 
     28 JSObject* IntlUtils::WrapObject(JSContext* aCx,
     29                                JS::Handle<JSObject*> aGivenProto) {
     30  return IntlUtils_Binding::Wrap(aCx, this, aGivenProto);
     31 }
     32 
     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());
     39 
     40  nsCOMPtr<mozIMozIntl> mozIntl = do_GetService("@mozilla.org/mozintl;1");
     41  if (!mozIntl) {
     42    aError.Throw(NS_ERROR_UNEXPECTED);
     43    return;
     44  }
     45 
     46  aError.MightThrowJSException();
     47 
     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;
     54  }
     55  JSContext* cx = jsapi.cx();
     56 
     57  // Prepare parameter for getDisplayNames().
     58  JS::Rooted<JS::Value> locales(cx);
     59  if (!ToJSValue(cx, aLocales, &locales)) {
     60    aError.StealExceptionFromJSContext(cx);
     61    return;
     62  }
     63 
     64  JS::Rooted<JS::Value> options(cx);
     65  if (!ToJSValue(cx, aOptions, &options)) {
     66    aError.StealExceptionFromJSContext(cx);
     67    return;
     68  }
     69 
     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;
     76  }
     77 
     78  if (!retVal.isObject() || !JS_WrapValue(cx, &retVal)) {
     79    aError.Throw(NS_ERROR_FAILURE);
     80    return;
     81  }
     82 
     83  // Return the result as DisplayNameResult.
     84  if (!aResult.Init(cx, retVal)) {
     85    aError.Throw(NS_ERROR_FAILURE);
     86  }
     87 }
     88 
     89 bool IntlUtils::IsAppLocaleRTL() {
     90  MOZ_ASSERT(nsContentUtils::IsCallerChrome() ||
     91             nsContentUtils::IsCallerUAWidget());
     92  return intl::LocaleService::GetInstance()->IsAppLocaleRTL();
     93 }
     94 
     95 }  // namespace mozilla::dom