Commit 3171580e authored by Pier Angelo Vendrame's avatar Pier Angelo Vendrame 🎃
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 72d13d42
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -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
@@ -109,6 +110,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()
    }
}
+41 −12
Original line number Diff line number Diff line
@@ -25,6 +25,8 @@ import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Collection;
import java.util.HashMap;
import java.util.Locale;
import org.mozilla.gecko.EventDispatcher;
import org.mozilla.gecko.GeckoSystemStateListener;
@@ -602,6 +604,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;
    }

    /**
     * Set this flag to disable low-memory detection. Set this when running tests to avoid
     * unpredictable behavior at runtime.
@@ -742,6 +754,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.
@@ -794,6 +807,7 @@ public final class GeckoRuntimeSettings extends RuntimeSettings {
    mRequestedLocales = settings.mRequestedLocales;
    mConfigFilePath = settings.mConfigFilePath;
    mExperimentDelegate = settings.mExperimentDelegate;
    mSupportedLocales = settings.mSupportedLocales;
  }

  /* package */ void commit() {
@@ -1317,24 +1331,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() {