Change URL formatter for short URLs in PageInfo

UrlFormatter.formatUrlForSecurityDisplay is too strict and displays
some valid URLs in punycode.
Add new url formatter with the difference that non-dangerous non-ascii
characters are displayed correctly and www subdomains are omitted

Bug: 1077766
Change-Id: I896fbf0a4d89df36c5fc882afaf09d1366f5423b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2532294
Reviewed-by: Chris Thompson <cthomp@chromium.org>
Reviewed-by: Tommy Li <tommycli@chromium.org>
Commit-Queue: Christian Dullweber <dullweber@chromium.org>
Cr-Commit-Position: refs/heads/master@{#826697}
diff --git a/components/page_info/android/java/src/org/chromium/components/page_info/PageInfoController.java b/components/page_info/android/java/src/org/chromium/components/page_info/PageInfoController.java
index 7263c6673..c5dc483 100644
--- a/components/page_info/android/java/src/org/chromium/components/page_info/PageInfoController.java
+++ b/components/page_info/android/java/src/org/chromium/components/page_info/PageInfoController.java
@@ -42,7 +42,6 @@
 import org.chromium.components.page_info.PageInfoView.PageInfoViewParams;
 import org.chromium.components.security_state.ConnectionSecurityLevel;
 import org.chromium.components.security_state.SecurityStateModel;
-import org.chromium.components.url_formatter.SchemeDisplay;
 import org.chromium.components.url_formatter.UrlFormatter;
 import org.chromium.content_public.browser.WebContents;
 import org.chromium.content_public.browser.WebContentsObserver;
@@ -257,8 +256,8 @@
             PageInfoContainer.Params containerParams = new PageInfoContainer.Params();
             containerParams.url = viewParams.url;
             containerParams.urlOriginLength = viewParams.urlOriginLength;
-            containerParams.truncatedUrl = UrlFormatter.formatUrlForSecurityDisplay(
-                    url, SchemeDisplay.OMIT_HTTP_AND_HTTPS);
+            containerParams.truncatedUrl =
+                    UrlFormatter.formatUrlForDisplayOmitSchemePathAndTrivialSubdomains(url);
             containerParams.backButtonClickCallback = this::exitSubpage;
             containerParams.urlTitleClickCallback = mContainer::toggleUrlTruncation;
             containerParams.urlTitleLongClickCallback = viewParams.urlTitleLongClickCallback;
diff --git a/components/url_formatter/android/BUILD.gn b/components/url_formatter/android/BUILD.gn
index 287b881c..fcef32c 100644
--- a/components/url_formatter/android/BUILD.gn
+++ b/components/url_formatter/android/BUILD.gn
@@ -37,6 +37,7 @@
     "//third_party/android_deps:androidx_test_runner_java",
     "//third_party/android_support_test_runner:runner_java",
     "//third_party/junit",
+    "//url:gurl_android_test_helper_java",
     "//url:gurl_java",
   ]
 }
diff --git a/components/url_formatter/android/java/src/org/chromium/components/url_formatter/UrlFormatter.java b/components/url_formatter/android/java/src/org/chromium/components/url_formatter/UrlFormatter.java
index 3ad37094..df324c5d 100644
--- a/components/url_formatter/android/java/src/org/chromium/components/url_formatter/UrlFormatter.java
+++ b/components/url_formatter/android/java/src/org/chromium/components/url_formatter/UrlFormatter.java
@@ -75,13 +75,33 @@
 
     /**
      * Builds a String representation of <code>uri</code> suitable for display to the user,
-     * omitting the HTTP scheme, the username and password, trailing slash on a bare hostname,
+     * omitting the HTTP/HTTPS scheme, the username and password, trailing slash on a bare hostname,
      * converting %20 to spaces, and removing trivial subdomains.
      *
      * The IDN hostname is turned to Unicode if the Unicode representation is deemed safe.
      * For more information, see <code>url_formatter::FormatUrl(const GURL&)</code>.
      *
      * Example:
+     *  - "http://user:password@example.com/%20test" -> "example.com/ test"
+     *  - "http://user:password@example.com/" -> "example.com"
+     *  - "http://www.xn--frgbolaget-q5a.se" -> "färgbolaget.se"
+     *
+     * @param uri URI to format.
+     * @return Formatted URL.
+     */
+    public static String formatUrlForDisplayOmitSchemeOmitTrivialSubdomains(String uri) {
+        return UrlFormatterJni.get().formatUrlForDisplayOmitSchemeOmitTrivialSubdomains(uri);
+    }
+
+    /**
+     * Builds a String representation of <code>uri</code> suitable for display to the user,
+     * omitting the HTTP/HTTPS scheme, the username and password, the path and removing trivial
+     * subdomains.
+     *
+     * The IDN hostname is turned to Unicode if the Unicode representation is deemed safe.
+     * For more information, see <code>url_formatter::FormatUrl(const GURL&)</code>.
+     *
+     * Example:
      *  - "http://user:password@example.com/%20test" -> "example.com"
      *  - "http://user:password@example.com/" -> "example.com"
      *  - "http://www.xn--frgbolaget-q5a.se" -> "färgbolaget.se"
@@ -89,9 +109,10 @@
      * @param uri URI to format.
      * @return Formatted URL.
      */
-    public static String formatUrlForDisplayOmitSchemeOmitTrivialSubdomains(String uri) {
-        return UrlFormatterJni.get().formatUrlForDisplayOmitSchemeOmitTrivialSubdomains(uri);
+    public static String formatUrlForDisplayOmitSchemePathAndTrivialSubdomains(GURL uri) {
+        return UrlFormatterJni.get().formatUrlForDisplayOmitSchemePathAndTrivialSubdomains(uri);
     }
+
     /**
      * Builds a String representation of <code>uri</code> suitable for display to the user,
      * omitting the username and password and trailing slash on a bare hostname.
@@ -163,6 +184,7 @@
         String formatUrlForDisplayOmitScheme(String url);
         String formatUrlForDisplayOmitHTTPScheme(String url);
         String formatUrlForDisplayOmitSchemeOmitTrivialSubdomains(String url);
+        String formatUrlForDisplayOmitSchemePathAndTrivialSubdomains(GURL url);
         String formatUrlForDisplayOmitUsernamePassword(String url);
         String formatUrlForCopy(String url);
         String formatUrlForSecurityDisplay(GURL url, @SchemeDisplay int schemeDisplay);
diff --git a/components/url_formatter/android/javatests/src/org/chromium/components/url_formatter/UrlFormatterUnitTest.java b/components/url_formatter/android/javatests/src/org/chromium/components/url_formatter/UrlFormatterUnitTest.java
index 6696a1c..3040b326 100644
--- a/components/url_formatter/android/javatests/src/org/chromium/components/url_formatter/UrlFormatterUnitTest.java
+++ b/components/url_formatter/android/javatests/src/org/chromium/components/url_formatter/UrlFormatterUnitTest.java
@@ -4,6 +4,8 @@
 
 package org.chromium.components.url_formatter;
 
+import static org.junit.Assert.assertEquals;
+
 import androidx.test.filters.SmallTest;
 
 import org.junit.Assert;
@@ -11,9 +13,12 @@
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
+import org.chromium.base.Function;
 import org.chromium.base.test.BaseJUnit4ClassRunner;
 import org.chromium.base.test.util.Batch;
 import org.chromium.content_public.browser.test.NativeLibraryTestUtils;
+import org.chromium.url.GURL;
+import org.chromium.url.GURLJavaTestHelper;
 
 /**
  * Unit tests for {@link UrlFormatter}.
@@ -27,14 +32,15 @@
     @Before
     public void setUp() {
         NativeLibraryTestUtils.loadNativeLibraryNoBrowserProcess();
+        GURLJavaTestHelper.nativeInitializeICU();
     }
 
     @Test
     @SmallTest
     public void testFixupUrl() {
-        Assert.assertEquals("http://google.com/", UrlFormatter.fixupUrl("google.com").getSpec());
-        Assert.assertEquals("chrome://version/", UrlFormatter.fixupUrl("about:").getSpec());
-        Assert.assertEquals("file:///mail.google.com:/",
+        assertEquals("http://google.com/", UrlFormatter.fixupUrl("google.com").getSpec());
+        assertEquals("chrome://version/", UrlFormatter.fixupUrl("about:").getSpec());
+        assertEquals("file:///mail.google.com:/",
                 UrlFormatter.fixupUrl("//mail.google.com:/").getSpec());
         Assert.assertFalse(UrlFormatter.fixupUrl("0x100.0").isValid());
     }
@@ -42,12 +48,25 @@
     @Test
     @SmallTest
     public void testFormatUrlForDisplayOmitUsernamePassword() {
-        Assert.assertEquals("http://google.com/path",
+        assertEquals("http://google.com/path",
                 UrlFormatter.formatUrlForDisplayOmitUsernamePassword("http://google.com/path"));
-        Assert.assertEquals("http://google.com",
+        assertEquals("http://google.com",
                 UrlFormatter.formatUrlForDisplayOmitUsernamePassword(
                         "http://user:pass@google.com"));
-        Assert.assertEquals("http://google.com",
+        assertEquals("http://google.com",
                 UrlFormatter.formatUrlForDisplayOmitUsernamePassword("http://user@google.com"));
     }
+
+    @Test
+    @SmallTest
+    public void testFormatUrlForDisplayOmitSchemePathAndTrivialSubdomains() {
+        Function<GURL, String> f =
+                UrlFormatter::formatUrlForDisplayOmitSchemePathAndTrivialSubdomains;
+
+        assertEquals("google.com", f.apply(new GURL("http://user:pass@google.com/path")));
+        assertEquals("chrome://version", f.apply(new GURL("chrome://version")));
+        assertEquals("äää.de", f.apply(new GURL("https://äää.de")));
+        assertEquals("xn--4caaa.com", f.apply(new GURL("https://äää.com")));
+        assertEquals("مثال.إختبار", f.apply(new GURL("https://xn--mgbh0fb.xn--kgbechtv/")));
+    }
 }
diff --git a/components/url_formatter/url_formatter_android.cc b/components/url_formatter/url_formatter_android.cc
index 5c394a6..9a93a0245 100644
--- a/components/url_formatter/url_formatter_android.cc
+++ b/components/url_formatter/url_formatter_android.cc
@@ -118,6 +118,22 @@
                net::UnescapeRule::SPACES, nullptr, nullptr, nullptr));
 }
 
+static ScopedJavaLocalRef<jstring>
+JNI_UrlFormatter_FormatUrlForDisplayOmitSchemePathAndTrivialSubdomains(
+    JNIEnv* env,
+    const JavaParamRef<jobject>& j_gurl) {
+  DCHECK(j_gurl);
+  std::unique_ptr<GURL> gurl = url::GURLAndroid::ToNativeGURL(env, j_gurl);
+  return base::android::ConvertUTF16ToJavaString(
+      env, url_formatter::FormatUrl(
+               *gurl,
+               url_formatter::kFormatUrlOmitDefaults |
+                   url_formatter::kFormatUrlTrimAfterHost |
+                   url_formatter::kFormatUrlOmitHTTPS |
+                   url_formatter::kFormatUrlOmitTrivialSubdomains,
+               net::UnescapeRule::SPACES, nullptr, nullptr, nullptr));
+}
+
 }  // namespace android
 
 }  // namespace url_formatter