Commit 99de6a6c authored by Peter E Conn's avatar Peter E Conn Committed by Commit Bot

🤝 Implement Origin logic in Java.

This Origin class is better than nothing however I feel more tests will
need to be added.

Bug: 821795
Change-Id: I069cf3556af12268f5ae97b8cda0634800f3112c
Reviewed-on: https://chromium-review.googlesource.com/963325Reviewed-by: default avatarBernhard Bauer <bauerb@chromium.org>
Commit-Queue: Peter Conn <peconn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#543395}
parent 3af065b9
...@@ -6,34 +6,62 @@ package org.chromium.chrome.browser.browserservices; ...@@ -6,34 +6,62 @@ package org.chromium.chrome.browser.browserservices;
import android.net.Uri; import android.net.Uri;
import org.chromium.net.GURLUtils;
/** /**
* A class to canonically represent a web origin in Java. It requires the native library to be * A class to canonically represent a web origin in Java. It intends to mirror the behaviour of
* loaded as it uses {@link GURLUtils#getOrigin}. * GURLUtils.getOrigin, but needs to work without native being loaded.
*/ */
public class Origin { public class Origin {
private final String mOrigin; private static final int HTTP_DEFAULT_PORT = 80;
private static final int HTTPS_DEFAULT_PORT = 443;
private final Uri mOrigin;
/** /**
* Constructs a canonical Origin from a String. * Constructs a canonical Origin from a String.
*/ */
public Origin(String uri) { public Origin(String uri) {
mOrigin = GURLUtils.getOrigin(uri); this(Uri.parse(uri));
} }
/** /**
* Constructs a canonical Origin from an Uri. * Constructs a canonical Origin from an Uri.
*/ */
public Origin(Uri uri) { public Origin(Uri uri) {
this(uri.toString()); if (uri.getScheme() == null || uri.getAuthority() == null) {
mOrigin = Uri.EMPTY;
return;
}
// Make explicit ports implicit and remove any user:password.
int port = uri.getPort();
if (uri.getScheme().equals("http") && port == HTTP_DEFAULT_PORT) port = -1;
if (uri.getScheme().equals("https") && port == HTTPS_DEFAULT_PORT) port = -1;
String authority = uri.getHost();
if (port != -1) authority += ":" + port;
Uri origin;
try {
origin = uri.normalizeScheme()
.buildUpon()
.opaquePart("")
.fragment("")
.path("/")
.encodedAuthority(authority)
.clearQuery()
.build();
} catch (UnsupportedOperationException e) {
origin = Uri.EMPTY;
}
mOrigin = origin;
} }
/** /**
* Returns a Uri representing this Origin. * Returns a Uri representing the Origin.
*/ */
public Uri uri() { public Uri uri() {
return Uri.parse(mOrigin); return mOrigin;
} }
@Override @Override
...@@ -41,9 +69,12 @@ public class Origin { ...@@ -41,9 +69,12 @@ public class Origin {
return mOrigin.hashCode(); return mOrigin.hashCode();
} }
/**
* Returns a String representing the Origin.
*/
@Override @Override
public String toString() { public String toString() {
return mOrigin; return mOrigin.toString();
} }
@Override @Override
......
...@@ -29,11 +29,48 @@ public class OriginTest { ...@@ -29,11 +29,48 @@ public class OriginTest {
mNativeLibraryTestRule.loadNativeLibraryNoBrowserProcess(); mNativeLibraryTestRule.loadNativeLibraryNoBrowserProcess();
} }
/** @Test
* The actual conversion from a free form URL to an Origin is done in native and that is @SmallTest
* thoroughly tested there. Here we only check that the transformation is performed, not that public void testTransformation() {
* it is complete and correct. Assert.assertEquals(Uri.parse("http://example.com:123/").getPort(), 123);
*/
// Unlike origin.cc, the returned Uri has a port of -1 if it is the default port for the
// scheme.
check("http://192.168.9.1/", "http", "192.168.9.1", -1);
// Test cases for origin.cc that do *not* work with Origin.java:
// check("http://[2001:db8::1]/", "http", "[2001:db8::1]", 80);
// This is because Uri cannot deal with IPv6 URLS, eg:
// Uri.parse("http://[2001:db8::1]/").getHost() returns "[2001"
// check("http://☃.net/", "http", "xn--n3h.net", 80);
// check("blob:http://☃.net/", "http", "xn--n3h.net", 80);
// We don't perform punycode substitution so the string is unchanged.
check("http://example.com/", "http", "example.com", -1);
check("http://example.com:123/", "http", "example.com", 123);
check("https://example.com/", "https", "example.com", -1);
check("https://example.com:123/", "https", "example.com", 123);
check("http://user:pass@example.com/", "http", "example.com", -1);
check("http://example.com:123/?query", "http", "example.com", 123);
check("https://example.com/#1234", "https", "example.com", -1);
check("https://u:p@example.com:123/?query#1234", "https", "example.com", 123);
}
private static void check(String url, String scheme, String host, int port) {
Origin origin = new Origin(url);
Assert.assertEquals(scheme, origin.uri().getScheme());
Assert.assertEquals(host, origin.uri().getHost());
Assert.assertEquals(port, origin.uri().getPort());
if (port == -1) {
Assert.assertEquals(origin.toString(), scheme + "://" + host + "/");
} else {
Assert.assertEquals(origin.toString(), scheme + "://" + host + ":" + port + "/");
}
}
@Test @Test
@SmallTest @SmallTest
public void testConstruction() { public void testConstruction() {
......
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