Skip to content

Commit

Permalink
Bug 1347306 - Hand over language negotiation from ChromeRegistry to L…
Browse files Browse the repository at this point in the history
…ocaleService. r=jfkthame

MozReview-Commit-ID: RIPZUHN4LW

--HG--
extra : rebase_source : 3011d3c9f1887c9943d6636ea959bb643644bd3e
  • Loading branch information
Zibi Braniecki committed Mar 14, 2017
1 parent 6109edd commit ca25902
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 251 deletions.
1 change: 0 additions & 1 deletion chrome/nsChromeRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,6 @@ nsChromeRegistry::FlushAllCaches()
NS_IMETHODIMP
nsChromeRegistry::ReloadChrome()
{
UpdateSelectedLocale();
FlushAllCaches();
// Do a reload of all top level windows.
nsresult rv = NS_OK;
Expand Down
6 changes: 0 additions & 6 deletions chrome/nsChromeRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ class nsChromeRegistry : public nsIToolkitChromeRegistry,
void FlushSkinCaches();
void FlushAllCaches();

// Update the selected locale used by the chrome registry, and fire a
// notification about this change
virtual nsresult UpdateSelectedLocale() = 0;

static void LogMessage(const char* aMsg, ...)
MOZ_FORMAT_PRINTF(1, 2);
static void LogMessageWithContext(nsIURI* aURL, uint32_t aLineNumber, uint32_t flags,
Expand All @@ -95,8 +91,6 @@ class nsChromeRegistry : public nsIToolkitChromeRegistry,
virtual nsresult GetFlagsFromPackage(const nsCString& aPackage,
uint32_t* aFlags) = 0;

nsresult SelectLocaleFromPref(nsIPrefBranch* prefs);

static nsresult RefreshWindow(nsPIDOMWindowOuter* aWindow);
static nsresult GetProviderAndPath(nsIURL* aChromeURL,
nsACString& aProvider, nsACString& aPath);
Expand Down
155 changes: 47 additions & 108 deletions chrome/nsChromeRegistryChrome.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
#include "mozilla/Unused.h"
#include "mozilla/intl/LocaleService.h"

#include "nsICommandLine.h"
#include "nsILocaleService.h"
#include "nsIObserverService.h"
#include "nsIPrefBranch.h"
#include "nsIPrefService.h"
Expand All @@ -39,16 +37,13 @@
#include "nsIXPConnect.h"
#include "nsIXULRuntime.h"

#define UILOCALE_CMD_LINE_ARG "UILocale"

#define MATCH_OS_LOCALE_PREF "intl.locale.matchOS"
#define SELECTED_LOCALE_PREF "general.useragent.locale"
#define SELECTED_SKIN_PREF "general.skins.selectedSkin"
#define PACKAGE_OVERRIDE_BRANCH "chrome.override_package."

using namespace mozilla;
using mozilla::dom::ContentParent;
using mozilla::dom::PContentParent;
using mozilla::intl::LocaleService;

// We use a "best-fit" algorithm for matching locales and themes.
// 1) the exact selected locale/theme
Expand Down Expand Up @@ -113,7 +108,6 @@ nsChromeRegistryChrome::Init()
if (NS_FAILED(rv))
return rv;

mSelectedLocale = NS_LITERAL_CSTRING("en-US");
mSelectedSkin = NS_LITERAL_CSTRING("classic/1.0");

bool safeMode = false;
Expand All @@ -140,17 +134,13 @@ nsChromeRegistryChrome::Init()
if (NS_SUCCEEDED(rv))
mSelectedSkin = provider;

SelectLocaleFromPref(prefs);

rv = prefs->AddObserver(MATCH_OS_LOCALE_PREF, this, true);
rv = prefs->AddObserver(SELECTED_LOCALE_PREF, this, true);
rv = prefs->AddObserver(SELECTED_SKIN_PREF, this, true);
}

nsCOMPtr<nsIObserverService> obsService = mozilla::services::GetObserverService();
if (obsService) {
obsService->AddObserver(this, "command-line-startup", true);
obsService->AddObserver(this, "profile-initial-state", true);
obsService->AddObserver(this, "intl:app-locales-changed", true);
}

return NS_OK;
Expand Down Expand Up @@ -203,22 +193,6 @@ nsChromeRegistryChrome::GetLocalesForPackage(const nsACString& aPackage,
return rv;
}

static nsresult
getUILangCountry(nsACString& aUILang)
{
nsresult rv;

nsCOMPtr<nsILocaleService> localeService = do_GetService(NS_LOCALESERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);

nsAutoString uiLang;
rv = localeService->GetLocaleComponentForUserAgent(uiLang);
NS_ENSURE_SUCCESS(rv, rv);

CopyUTF16toUTF8(uiLang, aUILang);
return NS_OK;
}

NS_IMETHODIMP
nsChromeRegistryChrome::IsLocaleRTL(const nsACString& package, bool *aResult)
{
Expand All @@ -233,11 +207,27 @@ nsChromeRegistryChrome::IsLocaleRTL(const nsACString& package, bool *aResult)
return NS_OK;
}

/**
* This method negotiates only between the app locale and the available
* chrome packages.
*
* If you want to get the current application's UI locale, please use
* LocaleService::GetAppLocaleAsLangTag.
*/
nsresult
nsChromeRegistryChrome::GetSelectedLocale(const nsACString& aPackage,
bool aAsBCP47,
nsACString& aLocale)
{
nsAutoCString reqLocale;
if (aPackage.Equals("global")) {
LocaleService::GetInstance()->GetAppLocaleAsLangTag(reqLocale);
} else {
AutoTArray<nsCString, 10> requestedLocales;
LocaleService::GetInstance()->GetRequestedLocales(requestedLocales);
reqLocale.Assign(requestedLocales[0]);
}

nsCString realpackage;
nsresult rv = OverrideLocalePackage(aPackage, realpackage);
if (NS_FAILED(rv))
Expand All @@ -246,7 +236,7 @@ nsChromeRegistryChrome::GetSelectedLocale(const nsACString& aPackage,
if (!mPackagesHash.Get(realpackage, &entry))
return NS_ERROR_FILE_NOT_FOUND;

aLocale = entry->locales.GetSelected(mSelectedLocale, nsProviderArray::LOCALE);
aLocale = entry->locales.GetSelected(reqLocale, nsProviderArray::LOCALE);
if (aLocale.IsEmpty())
return NS_ERROR_FAILURE;

Expand All @@ -272,34 +262,6 @@ nsChromeRegistryChrome::OverrideLocalePackage(const nsACString& aPackage,
return NS_OK;
}

nsresult
nsChromeRegistryChrome::SelectLocaleFromPref(nsIPrefBranch* prefs)
{
nsresult rv;
bool matchOSLocale = false;
rv = prefs->GetBoolPref(MATCH_OS_LOCALE_PREF, &matchOSLocale);

if (NS_SUCCEEDED(rv) && matchOSLocale) {
// compute lang and region code only when needed!
nsAutoCString uiLocale;
rv = getUILangCountry(uiLocale);
if (NS_SUCCEEDED(rv))
mSelectedLocale = uiLocale;
}
else {
nsXPIDLCString provider;
rv = prefs->GetCharPref(SELECTED_LOCALE_PREF, getter_Copies(provider));
if (NS_SUCCEEDED(rv)) {
mSelectedLocale = provider;
}
}

if (NS_FAILED(rv))
NS_ERROR("Couldn't select locale from pref!");

return rv;
}

NS_IMETHODIMP
nsChromeRegistryChrome::Observe(nsISupports *aSubject, const char *aTopic,
const char16_t *someData)
Expand All @@ -312,13 +274,7 @@ nsChromeRegistryChrome::Observe(nsISupports *aSubject, const char *aTopic,

NS_ConvertUTF16toUTF8 pref(someData);

if (pref.EqualsLiteral(MATCH_OS_LOCALE_PREF) ||
pref.EqualsLiteral(SELECTED_LOCALE_PREF)) {
rv = UpdateSelectedLocale();
if (NS_SUCCEEDED(rv) && mProfileLoaded)
FlushAllCaches();
}
else if (pref.EqualsLiteral(SELECTED_SKIN_PREF)) {
if (pref.EqualsLiteral(SELECTED_SKIN_PREF)) {
nsXPIDLCString provider;
rv = prefs->GetCharPref(pref.get(), getter_Copies(provider));
if (NS_FAILED(rv)) {
Expand All @@ -332,24 +288,14 @@ nsChromeRegistryChrome::Observe(nsISupports *aSubject, const char *aTopic,
NS_ERROR("Unexpected pref!");
}
}
else if (!strcmp("command-line-startup", aTopic)) {
nsCOMPtr<nsICommandLine> cmdLine (do_QueryInterface(aSubject));
if (cmdLine) {
nsAutoString uiLocale;
rv = cmdLine->HandleFlagWithParam(NS_LITERAL_STRING(UILOCALE_CMD_LINE_ARG),
false, uiLocale);
if (NS_SUCCEEDED(rv) && !uiLocale.IsEmpty()) {
CopyUTF16toUTF8(uiLocale, mSelectedLocale);
nsCOMPtr<nsIPrefBranch> prefs (do_GetService(NS_PREFSERVICE_CONTRACTID));
if (prefs) {
prefs->RemoveObserver(SELECTED_LOCALE_PREF, this);
}
}
}
}
else if (!strcmp("profile-initial-state", aTopic)) {
mProfileLoaded = true;
}
else if (!strcmp("intl:app-locales-changed", aTopic)) {
if (mProfileLoaded) {
FlushAllCaches();
}
}
else {
NS_ERROR("Unexpected observer topic!");
}
Expand All @@ -375,25 +321,6 @@ nsChromeRegistryChrome::CheckForNewChrome()
return NS_OK;
}

nsresult nsChromeRegistryChrome::UpdateSelectedLocale()
{
nsresult rv = NS_OK;
nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
if (prefs) {
rv = SelectLocaleFromPref(prefs);
if (NS_SUCCEEDED(rv)) {
nsCOMPtr<nsIObserverService> obsSvc =
mozilla::services::GetObserverService();
NS_ASSERTION(obsSvc, "Couldn't get observer service.");
obsSvc->NotifyObservers((nsIChromeRegistry*) this,
"selected-locale-has-changed", nullptr);
mozilla::intl::LocaleService::GetInstance()->Refresh();
}
}

return rv;
}

static void
SerializeURI(nsIURI* aURI,
SerializedURI& aSerializedURI)
Expand All @@ -416,7 +343,7 @@ nsChromeRegistryChrome::SendRegisteredChrome(
for (auto iter = mPackagesHash.Iter(); !iter.Done(); iter.Next()) {
ChromePackage chromePackage;
ChromePackageFromPackageEntry(iter.Key(), iter.UserData(), &chromePackage,
mSelectedLocale, mSelectedSkin);
mSelectedSkin);
packages.AppendElement(chromePackage);
}

Expand Down Expand Up @@ -447,9 +374,12 @@ nsChromeRegistryChrome::SendRegisteredChrome(
overrides.AppendElement(override);
}

nsAutoCString appLocale;
LocaleService::GetInstance()->GetAppLocaleAsLangTag(appLocale);

if (aParent) {
bool success = aParent->SendRegisterChrome(packages, resources, overrides,
mSelectedLocale, false);
appLocale, false);
NS_ENSURE_TRUE_VOID(success);
} else {
nsTArray<ContentParent*> parents;
Expand All @@ -460,7 +390,7 @@ nsChromeRegistryChrome::SendRegisteredChrome(
for (uint32_t i = 0; i < parents.Length(); i++) {
DebugOnly<bool> success =
parents[i]->SendRegisterChrome(packages, resources, overrides,
mSelectedLocale, true);
appLocale, true);
NS_WARNING_ASSERTION(success,
"couldn't reset a child's registered chrome");
}
Expand All @@ -471,12 +401,13 @@ nsChromeRegistryChrome::SendRegisteredChrome(
nsChromeRegistryChrome::ChromePackageFromPackageEntry(const nsACString& aPackageName,
PackageEntry* aPackage,
ChromePackage* aChromePackage,
const nsCString& aSelectedLocale,
const nsCString& aSelectedSkin)
{
nsAutoCString appLocale;
LocaleService::GetInstance()->GetAppLocaleAsLangTag(appLocale);

SerializeURI(aPackage->baseURI, aChromePackage->contentBaseURI);
SerializeURI(aPackage->locales.GetBase(aSelectedLocale,
nsProviderArray::LOCALE),
SerializeURI(aPackage->locales.GetBase(appLocale, nsProviderArray::LOCALE),
aChromePackage->localeBaseURI);
SerializeURI(aPackage->skins.GetBase(aSelectedSkin, nsProviderArray::ANY),
aChromePackage->skinBaseURI);
Expand Down Expand Up @@ -511,7 +442,9 @@ nsChromeRegistryChrome::GetBaseURIFromPackage(const nsCString& aPackage,
}

if (aProvider.EqualsLiteral("locale")) {
return entry->locales.GetBase(mSelectedLocale, nsProviderArray::LOCALE);
nsAutoCString appLocale;
LocaleService::GetInstance()->GetAppLocaleAsLangTag(appLocale);
return entry->locales.GetBase(appLocale, nsProviderArray::LOCALE);
}
else if (aProvider.EqualsLiteral("skin")) {
return entry->skins.GetBase(mSelectedSkin, nsProviderArray::ANY);
Expand Down Expand Up @@ -764,7 +697,7 @@ nsChromeRegistryChrome::ManifestContent(ManifestProcessingContext& cx, int linen
if (mDynamicRegistration) {
ChromePackage chromePackage;
ChromePackageFromPackageEntry(packageName, entry, &chromePackage,
mSelectedLocale, mSelectedSkin);
mSelectedSkin);
SendManifestEntry(chromePackage);
}
}
Expand Down Expand Up @@ -800,9 +733,15 @@ nsChromeRegistryChrome::ManifestLocale(ManifestProcessingContext& cx, int lineno
if (mDynamicRegistration) {
ChromePackage chromePackage;
ChromePackageFromPackageEntry(packageName, entry, &chromePackage,
mSelectedLocale, mSelectedSkin);
mSelectedSkin);
SendManifestEntry(chromePackage);
}

if (strcmp(package, "global") == 0) {
// We should refresh the LocaleService, since the available
// locales changed.
LocaleService::GetInstance()->Refresh();
}
}

void
Expand Down Expand Up @@ -836,7 +775,7 @@ nsChromeRegistryChrome::ManifestSkin(ManifestProcessingContext& cx, int lineno,
if (mDynamicRegistration) {
ChromePackage chromePackage;
ChromePackageFromPackageEntry(packageName, entry, &chromePackage,
mSelectedLocale, mSelectedSkin);
mSelectedSkin);
SendManifestEntry(chromePackage);
}
}
Expand Down
4 changes: 0 additions & 4 deletions chrome/nsChromeRegistryChrome.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,10 @@ class nsChromeRegistryChrome : public nsChromeRegistry
static void ChromePackageFromPackageEntry(const nsACString& aPackageName,
PackageEntry* aPackage,
ChromePackage* aChromePackage,
const nsCString& aSelectedLocale,
const nsCString& aSelectedSkin);

nsresult OverrideLocalePackage(const nsACString& aPackage,
nsACString& aOverride);
nsresult SelectLocaleFromPref(nsIPrefBranch* prefs);
nsresult UpdateSelectedLocale() override;
nsIURI* GetBaseURIFromPackage(const nsCString& aPackage,
const nsCString& aProvider,
const nsCString& aPath) override;
Expand Down Expand Up @@ -157,7 +154,6 @@ class nsChromeRegistryChrome : public nsChromeRegistry
bool mProfileLoaded;
bool mDynamicRegistration;

nsCString mSelectedLocale;
nsCString mSelectedSkin;

// Hash of package names ("global") to PackageEntry objects
Expand Down
5 changes: 0 additions & 5 deletions chrome/nsChromeRegistryContent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,6 @@ nsChromeRegistryContent::GetXULOverlays(nsIURI *aChromeURL,
CONTENT_NOT_IMPLEMENTED();
}

nsresult nsChromeRegistryContent::UpdateSelectedLocale()
{
CONTENT_NOT_IMPLEMENTED();
}

void
nsChromeRegistryContent::ManifestContent(ManifestProcessingContext& cx,
int lineno, char *const * argv,
Expand Down
1 change: 0 additions & 1 deletion chrome/nsChromeRegistryContent.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class nsChromeRegistryContent : public nsChromeRegistry
uint32_t flags;
};

nsresult UpdateSelectedLocale() override;
nsIURI* GetBaseURIFromPackage(const nsCString& aPackage,
const nsCString& aProvider,
const nsCString& aPath) override;
Expand Down
Loading

0 comments on commit ca25902

Please sign in to comment.