Pier Angelo Vendrame pushed to branch base-browser-115.11.0esr-13.5-1 at The Tor Project / Applications / Tor Browser
Commits: df723884 by Pier Angelo Vendrame at 2024-05-09T18:12:13+02:00 fixup! Bug 40199: Avoid using system locale for intl.accept_languages in GeckoView
Revert "Bug 40199: Avoid using system locale for intl.accept_languages in GeckoView"
This reverts commit ff97b6fb06850784785e6993c256bef315b2525f.
- - - - - d6987499 by Pier Angelo Vendrame at 2024-05-09T18:12:14+02:00 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.
- - - - -
1 changed file:
- mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntimeSettings.java
Changes:
===================================== mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntimeSettings.java ===================================== @@ -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.ArrayList; +import java.util.Collection; +import java.util.HashMap; import java.util.Locale; import org.mozilla.gecko.EventDispatcher; import org.mozilla.gecko.GeckoSystemStateListener; @@ -455,6 +456,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. * @@ -539,6 +550,7 @@ public final class GeckoRuntimeSettings extends RuntimeSettings { /* package */ int mScreenHeightOverride; /* package */ Class<? extends Service> mCrashHandler; /* package */ String[] mRequestedLocales; + /* package */ HashMap<Locale, Locale> mSupportedLocales = new HashMap<>(); /* package */ RuntimeTelemetry.Proxy mTelemetryProxy;
/** @@ -595,6 +607,7 @@ public final class GeckoRuntimeSettings extends RuntimeSettings { mRequestedLocales = settings.mRequestedLocales; mConfigFilePath = settings.mConfigFilePath; mTelemetryProxy = settings.mTelemetryProxy; + mSupportedLocales = settings.mSupportedLocales; }
/* package */ void commit() { @@ -803,30 +816,39 @@ public final class GeckoRuntimeSettings extends RuntimeSettings { EventDispatcher.getInstance().dispatch("GeckoView:SetLocale", data); }
+ 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); + } + private String computeAcceptLanguages() { - final ArrayList<String> locales = new ArrayList<String>(); - - // In Desktop, these are defined in the `intl.accept_languages` localized property. - // At some point we should probably use the same values here, but for now we use a simple - // strategy which will hopefully result in reasonable acceptLanguage values. - if (mRequestedLocales != null && mRequestedLocales.length > 0) { - String locale = mRequestedLocales[0].toLowerCase(Locale.ROOT); - // No need to include `en-us` twice. - if (!locale.equals("en-us")) { - locales.add(locale); - if (locale.contains("-")) { - String lang = locale.split("-")[0]; - // No need to include `en` twice. - if (!lang.equals("en")) { - locales.add(lang); - } + Locale locale = null; + if (mRequestedLocales != null) { + for (String tag : mRequestedLocales) { + locale = getLocaleIfSupported(tag); + if (locale != null) { + break; } } } - locales.add("en-us"); - locales.add("en"); - - return TextUtils.join(",", locales); + if (locale == null) { + for (final String tag : getDefaultLocales()) { + locale = getLocaleIfSupported(tag); + if (locale != null) { + break; + } + } + } + String acceptLanguages = locale != null ? locale.toString().replace('_', '-') : "en-US"; + if (acceptLanguages.equals("en-US")) { + // For consistency with spoof English. + acceptLanguages += ", en"; + } + return acceptLanguages; }
private static String[] getDefaultLocales() {
View it on GitLab: https://gitlab.torproject.org/tpo/applications/tor-browser/-/compare/e743054...