intl/locale/LocaleService.cpp
author Zibi Braniecki <gandalf@mozilla.com>
Fri, 03 Mar 2017 13:31:26 -0800
changeset 493402 f83d84909c5377ecc330b8d4023df4dacb138c74
parent 487231 3910a1fa01c05b40a024e7d12857d0300004c8ae
child 494792 0a968b1e6e139dc9978dce8dc6ede6269458d1c8
child 494820 b29be242bcdcbe650a3d9fdd9d014fd2ebd5456f
child 494824 8f4c2789782d83f57305d02b052ae40e421df839
child 494830 ef1c87755d542151c8644bac9d62cd0c7feb6a75
child 495347 464ec71d4f4369d6487ba220fa9b11efc5bbf9c4
permissions -rw-r--r--
Bug 1344141 - Do not BCP47 canonicalize languages from ChromeRegistry in LocaleService. r?jfkthame MozReview-Commit-ID: I6xMPhZRUUv

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* 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 "LocaleService.h"

#include "mozilla/ClearOnShutdown.h"
#include "mozilla/Services.h"
#include "nsIObserverService.h"
#include "nsIToolkitChromeRegistry.h"

using namespace mozilla::intl;

NS_IMPL_ISUPPORTS(LocaleService, mozILocaleService)

mozilla::StaticRefPtr<LocaleService> LocaleService::sInstance;

/**
 * This function performs the actual language negotiation for the API.
 *
 * Currently it collects the locale ID used by nsChromeRegistry and
 * adds hardcoded "en-US" locale as a fallback.
 */
static void
ReadAppLocales(nsTArray<nsCString>& aRetVal)
{
  nsAutoCString uaLangTag;
  nsCOMPtr<nsIToolkitChromeRegistry> cr =
    mozilla::services::GetToolkitChromeRegistryService();
  if (cr) {
    // We don't want to canonicalize the locale from ChromeRegistry into
    // it's BCP47 form because we will use it for direct language
    // negotiation and BCP47 changes `ja-JP-mac` into `ja-JP-x-variant-mac`.
    cr->GetSelectedLocale(NS_LITERAL_CSTRING("global"), false, uaLangTag);
  }
  if (!uaLangTag.IsEmpty()) {
    aRetVal.AppendElement(uaLangTag);
  }

  if (!uaLangTag.EqualsLiteral("en-US")) {
    aRetVal.AppendElement(NS_LITERAL_CSTRING("en-US"));
  }
}

LocaleService*
LocaleService::GetInstance()
{
  if (!sInstance) {
    sInstance = new LocaleService();
    ClearOnShutdown(&sInstance);
  }
  return sInstance;
}

void
LocaleService::GetAppLocales(nsTArray<nsCString>& aRetVal)
{
  if (mAppLocales.IsEmpty()) {
    ReadAppLocales(mAppLocales);
  }
  aRetVal = mAppLocales;
}

void
LocaleService::Refresh()
{
  nsTArray<nsCString> newLocales;
  ReadAppLocales(newLocales);

  if (mAppLocales != newLocales) {
    mAppLocales = Move(newLocales);
    nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
    if (obs) {
      obs->NotifyObservers(nullptr, "intl:app-locales-changed", nullptr);
    }
  }
}

/**
 * mozILocaleService methods
 */
NS_IMETHODIMP
LocaleService::GetAppLocales(uint32_t* aCount, char*** aOutArray)
{
  if (mAppLocales.IsEmpty()) {
    ReadAppLocales(mAppLocales);
  }

  *aCount = mAppLocales.Length();
  *aOutArray = static_cast<char**>(moz_xmalloc(*aCount * sizeof(char*)));

  for (uint32_t i = 0; i < *aCount; i++) {
    (*aOutArray)[i] = moz_xstrdup(mAppLocales[i].get());
  }

  return NS_OK;
}

NS_IMETHODIMP
LocaleService::GetAppLocale(nsACString& aRetVal)
{
  if (mAppLocales.IsEmpty()) {
    ReadAppLocales(mAppLocales);
  }
  aRetVal = mAppLocales[0];
  return NS_OK;
}