Commit 1f173f12 authored by Xi Han's avatar Xi Han Committed by Commit Bot

Rewrite URL to indicate a request are from Maps lite PWA.

If |loggedIntentUrlParam| is in the AndroidManifest, we want to rewrite the URL
to |start_url| appended with the original URL as a query parameter defined in
|loggedIntentUrlParam|. The site for the WebAPK will know that the request is
comming from a WebAPK, and doesn't redirect the url to a url which is out of the
scope of the requested WebAPK. Otherwise, the url will be loaded in a CCT on top
of the WebApkActivity.

Bug: 775605
Change-Id: I6f24ea2c13c239812d2ba1d51d4898c1cd191856
Reviewed-on: https://chromium-review.googlesource.com/723846
Commit-Queue: Xi Han <hanxi@chromium.org>
Commit-Queue: Peter Kotwicz <pkotwicz@chromium.org>
Reviewed-by: default avatarPeter Kotwicz <pkotwicz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#510122}
parent dd789ac6
......@@ -96,14 +96,46 @@ public final class MainActivityTest {
}
/**
* Test that the intent URL is not rewritten if it is inside the scope specified in the Android
* Manifest.
* Tests that the intent URL is rewritten if |LoggedIntentUrlParam| is set, even though the
* intent URL is inside the scope specified in the Android Manifest.
*/
@Test
public void testNotRewriteStartUrlInsideScope() {
public void testRewriteStartUrlInsideScope() {
final String intentStartUrl = "https://www.google.com/maps/address?A=a";
final String manifestStartUrl = "https://www.google.com/maps/startUrl";
final String manifestScope = "https://www.google.com/maps";
final String expectedStartUrl =
"https://www.google.com/maps/startUrl?originalUrl=https%3A%2F%2Fwww.google.com%2Fmaps%2Faddress%3FA%3Da";
final String browserPackageName = "com.android.chrome";
Bundle bundle = new Bundle();
bundle.putString(WebApkMetaDataKeys.START_URL, manifestStartUrl);
bundle.putString(WebApkMetaDataKeys.SCOPE, manifestScope);
bundle.putString(WebApkMetaDataKeys.RUNTIME_HOST, browserPackageName);
bundle.putString(WebApkMetaDataKeys.LOGGED_INTENT_URL_PARAM, "originalUrl");
WebApkTestHelper.registerWebApkWithMetaData(WebApkUtilsTest.WEBAPK_PACKAGE_NAME, bundle);
installBrowser(browserPackageName);
Intent launchIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(intentStartUrl));
Robolectric.buildActivity(MainActivity.class).withIntent(launchIntent).create();
Intent startActivityIntent = ShadowApplication.getInstance().getNextStartedActivity();
Assert.assertEquals(MainActivity.ACTION_START_WEBAPK, startActivityIntent.getAction());
Assert.assertEquals(
expectedStartUrl, startActivityIntent.getStringExtra(WebApkConstants.EXTRA_URL));
}
/**
* Tests that the intent URL is not rewritten again if the query parameter to append is part of
* the intent URL when |LoggedIntentUrlParam| is set.
*/
@Test
public void testNotRewriteStartUrlWhenContainsTheQueryParameterToAppend() {
final String intentStartUrl =
"https://www.google.com/maps/startUrl?originalUrl=https%3A%2F%2Fwww.google.com%2Fmaps%2Faddress%3FA%3Da";
final String manifestStartUrl = "https://www.google.com/maps/startUrl";
final String manifestScope = "https://www.google.com/maps";
final String browserPackageName = "com.android.chrome";
Bundle bundle = new Bundle();
......
......@@ -86,7 +86,7 @@ public class WebApkUtilsTest {
* manifest meta data.
*/
@Test
public void testLoggedIntentUrlParamWhenRewrite() {
public void testLoggedIntentUrlParamWhenRewriteOutOfScope() {
final String intentStartUrl = "https://maps.google.com/page?a=A";
final String manifestStartUrl = "https://www.google.com/maps";
final String manifestScope = "https://www.google.com";
......@@ -104,6 +104,31 @@ public class WebApkUtilsTest {
WebApkUtils.rewriteIntentUrlIfNecessary(intentStartUrl, bundle));
}
/**
* Test that MainActivity appends the start URL as a paramater if |loggedIntentUrlParam| in
* WebAPK metadata is set and {@link intentStartUrl} is in the scope specified in the manifest
* meta data.
*/
@Test
public void testLoggedIntentUrlParamWhenRewriteInScope() {
final String intentStartUrl = "https://www.google.com/maps/search/A";
final String manifestStartUrl = "https://www.google.com/maps?force=qVTs2FOxxTmHHo79-pwa";
final String manifestScope = "https://www.google.com";
final String expectedRewrittenStartUrl =
"https://www.google.com/maps?force=qVTs2FOxxTmHHo79-pwa&intent="
+ "https%3A%2F%2Fwww.google.com%2Fmaps%2Fsearch%2FA";
final String browserPackageName = "browser.support.webapks";
Bundle bundle = new Bundle();
bundle.putString(WebApkMetaDataKeys.START_URL, manifestStartUrl);
bundle.putString(WebApkMetaDataKeys.SCOPE, manifestScope);
bundle.putString(WebApkMetaDataKeys.RUNTIME_HOST, browserPackageName);
bundle.putString(WebApkMetaDataKeys.LOGGED_INTENT_URL_PARAM, "intent");
Assert.assertEquals(expectedRewrittenStartUrl,
WebApkUtils.rewriteIntentUrlIfNecessary(intentStartUrl, bundle));
}
/**
* This is a test for the WebAPK WITH a runtime host specified in its AndroidManifest.xml.
* Tests that the package name of the host browser specified in the AndroidManifest.xml will be
......
......@@ -6,7 +6,7 @@
# (including AndroidManifest.xml) is updated. This version should be incremented
# prior to uploading a new ShellAPK to the WebAPK Minting Server.
# Does not affect Chrome.apk
template_shell_apk_version = 29
template_shell_apk_version = 30
# The ShellAPK version expected by Chrome. Chrome will try to update the WebAPK
# if the WebAPK's ShellAPK version is less than |expected_shell_apk_version|.
......
......@@ -130,37 +130,26 @@ public class WebApkUtils {
}
/**
* Returns the new intent url, rewrite if necessary.
* The WebAPK may have been launched as a result of an intent filter for a different
* scheme or top level domain. Rewrite the scheme and host name to the scope's
* scheme and host name in this case, and append orginal intent url if |loggedIntentUrlParam| is
* set.
* Returns the new intent url, rewrite if |loggedIntentUrlParam| is set. A query parameter with
* the original URL is appended to the URL. Note: if the intent url has been rewritten before,
* we don't rewrite it again.
*/
public static String rewriteIntentUrlIfNecessary(String intentStartUrl, Bundle metadata) {
String scopeUrl = metadata.getString(WebApkMetaDataKeys.SCOPE);
String startUrl = metadata.getString(WebApkMetaDataKeys.START_URL);
String loggedIntentUrlParam =
metadata.getString(WebApkMetaDataKeys.LOGGED_INTENT_URL_PARAM);
if (TextUtils.isEmpty(scopeUrl) || TextUtils.isEmpty(loggedIntentUrlParam)) {
return intentStartUrl;
}
if (TextUtils.isEmpty(loggedIntentUrlParam)) return intentStartUrl;
if (!isUrlInScope(intentStartUrl, scopeUrl)) {
Uri.Builder returnUrlBuilder = Uri.parse(startUrl).buildUpon();
returnUrlBuilder.appendQueryParameter(loggedIntentUrlParam, intentStartUrl);
return returnUrlBuilder.toString();
if (intentStartUrl.startsWith(startUrl)
&& !TextUtils.isEmpty(
Uri.parse(intentStartUrl).getQueryParameter(loggedIntentUrlParam))) {
return intentStartUrl;
}
return intentStartUrl;
}
private static boolean isUrlInScope(String url, String scopeUrl) {
Uri parsedUrl = Uri.parse(url);
Uri parsedScopeUrl = Uri.parse(scopeUrl);
return parsedUrl.getScheme().equals(parsedScopeUrl.getScheme())
&& parsedUrl.getHost().equals(parsedScopeUrl.getHost())
&& parsedUrl.getPath().startsWith(parsedScopeUrl.getPath());
Uri.Builder returnUrlBuilder = Uri.parse(startUrl).buildUpon();
returnUrlBuilder.appendQueryParameter(loggedIntentUrlParam, intentStartUrl);
return returnUrlBuilder.toString();
}
/**
......
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