Commit dbdbd176 authored by Michael Thiessen's avatar Michael Thiessen Committed by Commit Bot

Migrate NTP from URI to GURL

Replaces usages of java.net.URI with GURL.

This change is slightly complicated as GURL and URI handle about: URLs
differently, in that GURL considers everything after the about: scheme
as a path instead of a host/path/etc.

This is usually handled in the native code by running FixupUrl on URLs
with the about: scheme to convert them into the chrome: scheme (unless
they're about:blank or about:srcdoc), and this is how navigation to
about:newtab works (by converting it to chrome://newtab).

So in order to know if something will resolve to the NTP, we need to
first fixup the URL, and then check its host/validity.

Bug: 783819
Change-Id: Idf6ad46fa6ef9793a969f3a8ee721c9bb05b97a5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2031710
Commit-Queue: Michael Thiessen <mthiesse@chromium.org>
Reviewed-by: default avatarYaron Friedman <yfriedman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#738273}
parent f69f152e
......@@ -11,6 +11,7 @@ import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
......@@ -65,12 +66,12 @@ import org.chromium.chrome.browser.util.UrlConstants;
import org.chromium.chrome.browser.util.UrlUtilities;
import org.chromium.chrome.browser.vr.VrModuleProvider;
import org.chromium.components.search_engines.TemplateUrlService.TemplateUrlServiceObserver;
import org.chromium.components.url_formatter.UrlFormatter;
import org.chromium.content_public.browser.NavigationController;
import org.chromium.content_public.browser.NavigationEntry;
import org.chromium.ui.mojom.WindowOpenDisposition;
import org.chromium.url.GURL;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
/**
......@@ -160,23 +161,12 @@ public class NewTabPage implements NativePage, InvalidationAwareThumbnailProvide
public static boolean isNTPUrl(String url) {
// Also handle the legacy chrome://newtab and about:newtab URLs since they will redirect to
// chrome-native://newtab natively.
if (url == null) return false;
try {
// URL().getProtocol() throws MalformedURLException if the scheme is "invalid",
// including common ones like "about:", so it's not usable for isInternalScheme().
URI uri = new URI(url);
if (!UrlUtilities.isInternalScheme(uri)) return false;
String host = uri.getHost();
if (host == null) {
// "about:newtab" would lead to null host.
uri = new URI(uri.getScheme() + "://" + uri.getSchemeSpecificPart());
host = uri.getHost();
}
return UrlConstants.NTP_HOST.equals(host);
} catch (URISyntaxException e) {
return false;
}
if (TextUtils.isEmpty(url)) return false;
// We need to fixup the URL to handle about: schemes and transform them into the equivalent
// chrome:// scheme so that GURL parses the host correctly.
GURL gurl = UrlFormatter.fixupUrl(url);
if (!gurl.isValid() || !UrlUtilities.isInternalScheme(gurl)) return false;
return UrlConstants.NTP_HOST.equals(gurl.getHost());
}
protected class NewTabPageManagerImpl
......
......@@ -25,6 +25,7 @@ public class PartnerBrowserCustomizationsUnitTest {
Assert.assertTrue(PartnerBrowserCustomizations.isValidHomepage("chrome-native://newtab/"));
Assert.assertTrue(PartnerBrowserCustomizations.isValidHomepage("chrome-native://newtab"));
Assert.assertTrue(PartnerBrowserCustomizations.isValidHomepage("chrome://newtab"));
Assert.assertTrue(PartnerBrowserCustomizations.isValidHomepage("chrome:newtab"));
Assert.assertTrue(PartnerBrowserCustomizations.isValidHomepage("about://newtab"));
Assert.assertTrue(PartnerBrowserCustomizations.isValidHomepage("about:newtab"));
Assert.assertTrue(
......
......@@ -27,6 +27,7 @@ android_library("java") {
"//third_party/android_deps:com_android_support_collections_java",
"//third_party/android_deps:com_android_support_support_compat_java",
"//third_party/android_deps:com_android_support_support_core_utils_java",
"//url:gurl_java",
]
annotation_processor_deps = [ "//base/android/jni_generator:jni_processor" ]
}
......
......@@ -14,6 +14,7 @@ import org.chromium.base.CollectionUtil;
import org.chromium.base.Log;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.content_public.common.ContentUrlConstants;
import org.chromium.url.GURL;
import java.io.UnsupportedEncodingException;
import java.net.URI;
......@@ -107,6 +108,15 @@ public class UrlUtilities {
return INTERNAL_SCHEMES.contains(uri.getScheme());
}
/**
* @param gurl A GURL.
*
* @return Whether the URL's scheme is for a internal chrome page.
*/
public static boolean isInternalScheme(GURL gurl) {
return INTERNAL_SCHEMES.contains(gurl.getScheme());
}
/**
* @param url A URL.
*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment