Skip to content
Snippets Groups Projects
Commit 986e02b3 authored by Pier Angelo Vendrame's avatar Pier Angelo Vendrame :jack_o_lantern:
Browse files

BB 42562: Normalized the Accepted Languages on Android.

The OS language might be outside the list of actually supported
languages and it might leak the user's region.
Therefore, we force the locale reported in Accept-Language to match one
we support with translations, even when it means using a not exact
region tag.
parent e3d0c65f
No related branches found
No related tags found
1 merge request!1500TB 43415: Rebased onto 134.0a1
......@@ -15,6 +15,7 @@ import mozilla.components.experiment.NimbusExperimentDelegate
import mozilla.components.lib.crash.handler.CrashHandlerService
import mozilla.components.service.sync.autofill.GeckoCreditCardsAddressesStorageDelegate
import mozilla.components.service.sync.logins.GeckoLoginStorageDelegate
import org.mozilla.fenix.BuildConfig
import org.mozilla.fenix.Config
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.settings
......@@ -140,6 +141,7 @@ object GeckoProvider {
.translationsOfferPopup(context.settings().offerTranslation)
.disableShip(FxNimbus.features.ship.value().disabled)
.fissionEnabled(FxNimbus.features.fission.value().enabled)
.supportedLocales(BuildConfig.SUPPORTED_LOCALE_ARRAY.toList())
.build()
}
}
......@@ -22,7 +22,8 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.LinkedHashMap;
import java.util.Collection;
import java.util.HashMap;
import java.util.Locale;
import org.mozilla.gecko.EventDispatcher;
import org.mozilla.gecko.GeckoSystemStateListener;
......@@ -589,6 +590,16 @@ public final class GeckoRuntimeSettings extends RuntimeSettings {
return this;
}
public @NonNull Builder supportedLocales(final Collection<String> locales) {
getSettings().mSupportedLocales.clear();
for (String tag : locales) {
Locale locale = Locale.forLanguageTag(tag);
getSettings().mSupportedLocales.put(locale, locale);
getSettings().mSupportedLocales.put(new Locale(locale.getLanguage()), locale);
}
return this;
}
/**
* Sets whether we should spoof locale to English for webpages.
*
......@@ -706,6 +717,7 @@ public final class GeckoRuntimeSettings extends RuntimeSettings {
/* package */ Class<? extends Service> mCrashHandler;
/* package */ String[] mRequestedLocales;
/* package */ ExperimentDelegate mExperimentDelegate;
/* package */ HashMap<Locale, Locale> mSupportedLocales = new HashMap<>();
/**
* Attach and commit the settings to the given runtime.
......@@ -757,6 +769,7 @@ public final class GeckoRuntimeSettings extends RuntimeSettings {
mRequestedLocales = settings.mRequestedLocales;
mConfigFilePath = settings.mConfigFilePath;
mExperimentDelegate = settings.mExperimentDelegate;
mSupportedLocales = settings.mSupportedLocales;
}
/* package */ void commit() {
......@@ -1248,24 +1261,39 @@ public final class GeckoRuntimeSettings extends RuntimeSettings {
EventDispatcher.getInstance().dispatch("GeckoView:SetLocale", data);
}
private String computeAcceptLanguages() {
final LinkedHashMap<String, String> locales = new LinkedHashMap<>();
private Locale getLocaleIfSupported(String tag) {
Locale exact = Locale.forLanguageTag(tag);
if (mSupportedLocales.containsKey(exact)) {
return exact;
}
Locale fallback = new Locale(exact.getLanguage());
return mSupportedLocales.get(fallback);
}
// Explicitly-set app prefs come first:
private String computeAcceptLanguages() {
Locale locale = null;
if (mRequestedLocales != null) {
for (final String locale : mRequestedLocales) {
locales.put(locale.toLowerCase(Locale.ROOT), locale);
for (String tag : mRequestedLocales) {
locale = getLocaleIfSupported(tag);
if (locale != null) {
break;
}
}
// OS prefs come second:
for (final String locale : getSystemLocalesForAcceptLanguage()) {
final String localeLowerCase = locale.toLowerCase(Locale.ROOT);
if (!locales.containsKey(localeLowerCase)) {
locales.put(localeLowerCase, locale);
}
if (locale == null) {
for (final String tag : getSystemLocalesForAcceptLanguage()) {
locale = getLocaleIfSupported(tag);
if (locale != null) {
break;
}
return TextUtils.join(",", locales.values());
}
}
String acceptLanguages = locale != null ? locale.toLanguageTag().replace('_', '-') : "en-US";
if (acceptLanguages.equals("en-US")) {
// For consistency with spoof English.
acceptLanguages += ", en";
}
return acceptLanguages;
}
private static String[] getSystemLocalesForAcceptLanguage() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment