[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [Git][tpo/applications/tor-browser][tor-browser-115.11.0esr-13.5-1] 2 commits: fixup! Bug 40199: Avoid using system locale for intl.accept_languages in GeckoView



Title: GitLab

Pier Angelo Vendrame pushed to branch tor-browser-115.11.0esr-13.5-1 at The Tor Project / Applications / Tor Browser

Commits:

  • 83a3762f
    by Pier Angelo Vendrame at 2024-05-09T18:04:04+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.
    
  • e6f7f151
    by Pier Angelo Vendrame at 2024-05-09T18:04:07+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:

Changes:

  • mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntimeSettings.java
    ... ... @@ -22,7 +22,8 @@ import androidx.annotation.NonNull;
    22 22
     import androidx.annotation.Nullable;
    
    23 23
     import java.lang.annotation.Retention;
    
    24 24
     import java.lang.annotation.RetentionPolicy;
    
    25
    -import java.util.ArrayList;
    
    25
    +import java.util.Collection;
    
    26
    +import java.util.HashMap;
    
    26 27
     import java.util.Locale;
    
    27 28
     import org.mozilla.gecko.EventDispatcher;
    
    28 29
     import org.mozilla.gecko.GeckoSystemStateListener;
    
    ... ... @@ -455,6 +456,16 @@ public final class GeckoRuntimeSettings extends RuntimeSettings {
    455 456
           return this;
    
    456 457
         }
    
    457 458
     
    
    459
    +    public @NonNull Builder supportedLocales(final Collection<String> locales) {
    
    460
    +      getSettings().mSupportedLocales.clear();
    
    461
    +      for (String tag : locales) {
    
    462
    +        Locale locale = Locale.forLanguageTag(tag);
    
    463
    +        getSettings().mSupportedLocales.put(locale, locale);
    
    464
    +        getSettings().mSupportedLocales.put(new Locale(locale.getLanguage()), locale);
    
    465
    +      }
    
    466
    +      return this;
    
    467
    +    }
    
    468
    +
    
    458 469
         /**
    
    459 470
          * Sets whether we should spoof locale to English for webpages.
    
    460 471
          *
    
    ... ... @@ -546,6 +557,7 @@ public final class GeckoRuntimeSettings extends RuntimeSettings {
    546 557
       /* package */ int mScreenHeightOverride;
    
    547 558
       /* package */ Class<? extends Service> mCrashHandler;
    
    548 559
       /* package */ String[] mRequestedLocales;
    
    560
    +  /* package */ HashMap<Locale, Locale> mSupportedLocales = new HashMap<>();
    
    549 561
       /* package */ RuntimeTelemetry.Proxy mTelemetryProxy;
    
    550 562
     
    
    551 563
       /**
    
    ... ... @@ -602,6 +614,7 @@ public final class GeckoRuntimeSettings extends RuntimeSettings {
    602 614
         mRequestedLocales = settings.mRequestedLocales;
    
    603 615
         mConfigFilePath = settings.mConfigFilePath;
    
    604 616
         mTelemetryProxy = settings.mTelemetryProxy;
    
    617
    +    mSupportedLocales = settings.mSupportedLocales;
    
    605 618
       }
    
    606 619
     
    
    607 620
       /* package */ void commit() {
    
    ... ... @@ -810,30 +823,39 @@ public final class GeckoRuntimeSettings extends RuntimeSettings {
    810 823
         EventDispatcher.getInstance().dispatch("GeckoView:SetLocale", data);
    
    811 824
       }
    
    812 825
     
    
    826
    +  private Locale getLocaleIfSupported(String tag) {
    
    827
    +    Locale exact = Locale.forLanguageTag(tag);
    
    828
    +    if (mSupportedLocales.containsKey(exact)) {
    
    829
    +      return exact;
    
    830
    +    }
    
    831
    +    Locale fallback = new Locale(exact.getLanguage());
    
    832
    +    return mSupportedLocales.get(fallback);
    
    833
    +  }
    
    834
    +
    
    813 835
       private String computeAcceptLanguages() {
    
    814
    -    final ArrayList<String> locales = new ArrayList<String>();
    
    815
    -
    
    816
    -    // In Desktop, these are defined in the `intl.accept_languages` localized property.
    
    817
    -    // At some point we should probably use the same values here, but for now we use a simple
    
    818
    -    // strategy which will hopefully result in reasonable acceptLanguage values.
    
    819
    -    if (mRequestedLocales != null && mRequestedLocales.length > 0) {
    
    820
    -      String locale = mRequestedLocales[0].toLowerCase(Locale.ROOT);
    
    821
    -      // No need to include `en-us` twice.
    
    822
    -      if (!locale.equals("en-us")) {
    
    823
    -        locales.add(locale);
    
    824
    -        if (locale.contains("-")) {
    
    825
    -          String lang = locale.split("-")[0];
    
    826
    -          // No need to include `en` twice.
    
    827
    -          if (!lang.equals("en")) {
    
    828
    -            locales.add(lang);
    
    829
    -          }
    
    836
    +    Locale locale = null;
    
    837
    +    if (mRequestedLocales != null) {
    
    838
    +      for (String tag : mRequestedLocales) {
    
    839
    +        locale = getLocaleIfSupported(tag);
    
    840
    +        if (locale != null) {
    
    841
    +          break;
    
    830 842
             }
    
    831 843
           }
    
    832 844
         }
    
    833
    -    locales.add("en-us");
    
    834
    -    locales.add("en");
    
    835
    -
    
    836
    -    return TextUtils.join(",", locales);
    
    845
    +    if (locale == null) {
    
    846
    +      for (final String tag : getDefaultLocales()) {
    
    847
    +        locale = getLocaleIfSupported(tag);
    
    848
    +        if (locale != null) {
    
    849
    +          break;
    
    850
    +        }
    
    851
    +      }
    
    852
    +    }
    
    853
    +    String acceptLanguages = locale != null ? locale.toString().replace('_', '-') : "en-US";
    
    854
    +    if (acceptLanguages.equals("en-US")) {
    
    855
    +      // For consistency with spoof English.
    
    856
    +      acceptLanguages += ", en";
    
    857
    +    }
    
    858
    +    return acceptLanguages;
    
    837 859
       }
    
    838 860
     
    
    839 861
       private static String[] getDefaultLocales() {
    

  • _______________________________________________
    tor-commits mailing list
    tor-commits@xxxxxxxxxxxxxxxxxxxx
    https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits