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

Bug 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 7bda8189
No related branches found
No related tags found
No related merge requests found
......@@ -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;
......@@ -566,6 +567,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.
*
......@@ -659,6 +670,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.
......@@ -710,6 +722,7 @@ public final class GeckoRuntimeSettings extends RuntimeSettings {
mRequestedLocales = settings.mRequestedLocales;
mConfigFilePath = settings.mConfigFilePath;
mExperimentDelegate = settings.mExperimentDelegate;
mSupportedLocales = settings.mSupportedLocales;
}
/* package */ void commit() {
......@@ -1036,24 +1049,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 register or to comment