Commit 28e0365d 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 98a82f97
Loading
Loading
Loading
Loading
+2 −0
Original line number Original line Diff line number Diff line
@@ -17,6 +17,7 @@ import mozilla.components.lib.crash.handler.CrashHandlerService
import mozilla.components.lib.crash.store.CrashAction
import mozilla.components.lib.crash.store.CrashAction
import mozilla.components.service.sync.autofill.GeckoCreditCardsAddressesStorageDelegate
import mozilla.components.service.sync.autofill.GeckoCreditCardsAddressesStorageDelegate
import mozilla.components.service.sync.logins.GeckoLoginStorageDelegate
import mozilla.components.service.sync.logins.GeckoLoginStorageDelegate
import org.mozilla.fenix.BuildConfig
import org.mozilla.fenix.Config
import org.mozilla.fenix.Config
import org.mozilla.fenix.components.appstate.AppAction
import org.mozilla.fenix.components.appstate.AppAction
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.components
@@ -132,6 +133,7 @@ object GeckoProvider {
                FxNimbus.features.sameDocumentNavigationOverridesLoadType.value().forceDisableUri,
                FxNimbus.features.sameDocumentNavigationOverridesLoadType.value().forceDisableUri,
            )
            )
            .isolatedProcessEnabled(context.settings().isIsolatedProcessEnabled)
            .isolatedProcessEnabled(context.settings().isIsolatedProcessEnabled)
            .supportedLocales(BuildConfig.SUPPORTED_LOCALE_ARRAY.toList())
            .build()
            .build()
    }
    }
}
}
+41 −12
Original line number Original line Diff line number Diff line
@@ -25,6 +25,8 @@ import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.List;
import java.util.Collection;
import java.util.HashMap;
import java.util.Locale;
import java.util.Locale;
import org.mozilla.gecko.EventDispatcher;
import org.mozilla.gecko.EventDispatcher;
import org.mozilla.gecko.GeckoSystemStateListener;
import org.mozilla.gecko.GeckoSystemStateListener;
@@ -631,6 +633,16 @@ public final class GeckoRuntimeSettings extends RuntimeSettings {
      return this;
      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
     * Set this flag to disable low-memory detection. Set this when running tests to avoid
     * unpredictable behavior at runtime.
     * unpredictable behavior at runtime.
@@ -832,6 +844,7 @@ public final class GeckoRuntimeSettings extends RuntimeSettings {
  /* package */ Class<? extends Service> mCrashHandler;
  /* package */ Class<? extends Service> mCrashHandler;
  /* package */ String[] mRequestedLocales;
  /* package */ String[] mRequestedLocales;
  /* package */ ExperimentDelegate mExperimentDelegate;
  /* package */ ExperimentDelegate mExperimentDelegate;
  /* package */ HashMap<Locale, Locale> mSupportedLocales = new HashMap<>();


  /**
  /**
   * Attach and commit the settings to the given runtime.
   * Attach and commit the settings to the given runtime.
@@ -885,6 +898,7 @@ public final class GeckoRuntimeSettings extends RuntimeSettings {
    mRequestedLocales = settings.mRequestedLocales;
    mRequestedLocales = settings.mRequestedLocales;
    mConfigFilePath = settings.mConfigFilePath;
    mConfigFilePath = settings.mConfigFilePath;
    mExperimentDelegate = settings.mExperimentDelegate;
    mExperimentDelegate = settings.mExperimentDelegate;
    mSupportedLocales = settings.mSupportedLocales;
  }
  }


  /* package */ void commit() {
  /* package */ void commit() {
@@ -1471,24 +1485,39 @@ public final class GeckoRuntimeSettings extends RuntimeSettings {
    EventDispatcher.getInstance().dispatch("GeckoView:SetLocale", data);
    EventDispatcher.getInstance().dispatch("GeckoView:SetLocale", data);
  }
  }


  private String computeAcceptLanguages() {
  private Locale getLocaleIfSupported(String tag) {
    final LinkedHashMap<String, String> locales = new LinkedHashMap<>();
    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) {
    if (mRequestedLocales != null) {
      for (final String locale : mRequestedLocales) {
      for (String tag : mRequestedLocales) {
        locales.put(locale.toLowerCase(Locale.ROOT), locale);
        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() {
  private static String[] getSystemLocalesForAcceptLanguage() {