Commit 3dc9c3c3 authored by Colin Blundell's avatar Colin Blundell Committed by Commit Bot

[Android] Move queryIntentActivities() into ExternalNavigationHandler

As the start of unforking as much as possible of the //weblayer and
//chrome implementations of the ExternalNavigationDelegate interface,
this CL moves their common implementations of queryIntentActivities()
into ExternalNavigationHandler.java itself. We leave the implementation
in the test delegate used in ExternalNavigationHandlerTest (no longer
overriding the public interface), as this is used by TestPackageManager
in those tests to mock out this functionality. This test setup could
likely be streamlined, but we will leave such restructuring of the
ExternalNavigationHandler test for a followup phase after the dust has
completely settled on the unforking of the two delegate
implementations in order to separate concerns and avoid the risk of
introducing significant noise by continually re-refactoring the test
in different ways as the unforking proceeds.

Bug: 1071390
Change-Id: Ibceedd3a952a721e2d6fa53f50c11d60d7bf6bd3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2152448Reviewed-by: default avatarMichael Thiessen <mthiesse@chromium.org>
Commit-Queue: Colin Blundell <blundell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#759650}
parent 70b96fe8
...@@ -204,12 +204,6 @@ public class ExternalNavigationDelegateImpl implements ExternalNavigationDelegat ...@@ -204,12 +204,6 @@ public class ExternalNavigationDelegateImpl implements ExternalNavigationDelegat
return info != null && info.activityInfo.packageName.equals(context.getPackageName()); return info != null && info.activityInfo.packageName.equals(context.getPackageName());
} }
@Override
public List<ResolveInfo> queryIntentActivities(Intent intent) {
return PackageManagerUtils.queryIntentActivities(
intent, PackageManager.GET_RESOLVED_FILTER);
}
@Override @Override
public boolean willChromeHandleIntent(Intent intent) { public boolean willChromeHandleIntent(Intent intent) {
return willChromeHandleIntent(intent, false); return willChromeHandleIntent(intent, false);
......
...@@ -1675,7 +1675,6 @@ public class ExternalNavigationHandlerTest { ...@@ -1675,7 +1675,6 @@ public class ExternalNavigationHandlerTest {
} }
private static class TestExternalNavigationDelegate implements ExternalNavigationDelegate { private static class TestExternalNavigationDelegate implements ExternalNavigationDelegate {
@Override
public List<ResolveInfo> queryIntentActivities(Intent intent) { public List<ResolveInfo> queryIntentActivities(Intent intent) {
List<ResolveInfo> list = new ArrayList<>(); List<ResolveInfo> list = new ArrayList<>();
String dataString = intent.getDataString(); String dataString = intent.getDataString();
...@@ -2112,9 +2111,9 @@ public class ExternalNavigationHandlerTest { ...@@ -2112,9 +2111,9 @@ public class ExternalNavigationHandlerTest {
} }
private static class TestPackageManager extends MockPackageManager { private static class TestPackageManager extends MockPackageManager {
private ExternalNavigationDelegate mDelegate; private TestExternalNavigationDelegate mDelegate;
public TestPackageManager(ExternalNavigationDelegate delegate) { public TestPackageManager(TestExternalNavigationDelegate delegate) {
mDelegate = delegate; mDelegate = delegate;
} }
...@@ -2127,7 +2126,7 @@ public class ExternalNavigationHandlerTest { ...@@ -2127,7 +2126,7 @@ public class ExternalNavigationHandlerTest {
private static class TestContext extends ContextWrapper { private static class TestContext extends ContextWrapper {
private PackageManager mPackageManager; private PackageManager mPackageManager;
public TestContext(Context baseContext, ExternalNavigationDelegate delegate) { public TestContext(Context baseContext, TestExternalNavigationDelegate delegate) {
super(baseContext); super(baseContext);
mPackageManager = new TestPackageManager(delegate); mPackageManager = new TestPackageManager(delegate);
} }
......
...@@ -9,7 +9,6 @@ import android.content.pm.ResolveInfo; ...@@ -9,7 +9,6 @@ import android.content.pm.ResolveInfo;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import org.chromium.base.PackageManagerUtils;
import org.chromium.components.external_intents.ExternalNavigationHandler.OverrideUrlLoadingResult; import org.chromium.components.external_intents.ExternalNavigationHandler.OverrideUrlLoadingResult;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -20,12 +19,6 @@ import java.util.List; ...@@ -20,12 +19,6 @@ import java.util.List;
* by {@link ExternalNavigationHandler}. * by {@link ExternalNavigationHandler}.
*/ */
public interface ExternalNavigationDelegate { public interface ExternalNavigationDelegate {
/**
* See {@link PackageManagerUtils#queryIntentActivities(Intent, int)}
*/
@NonNull
List<ResolveInfo> queryIntentActivities(Intent intent);
/** /**
* Determine if Chrome is the default or only handler for a given intent. If true, Chrome * Determine if Chrome is the default or only handler for a given intent. If true, Chrome
* will handle the intent when started. * will handle the intent when started.
......
...@@ -8,6 +8,7 @@ import android.content.ActivityNotFoundException; ...@@ -8,6 +8,7 @@ import android.content.ActivityNotFoundException;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Intent; import android.content.Intent;
import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo; import android.content.pm.ResolveInfo;
import android.net.Uri; import android.net.Uri;
import android.os.SystemClock; import android.os.SystemClock;
...@@ -17,6 +18,7 @@ import android.util.Pair; ...@@ -17,6 +18,7 @@ import android.util.Pair;
import android.webkit.WebView; import android.webkit.WebView;
import androidx.annotation.IntDef; import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
...@@ -24,6 +26,7 @@ import org.chromium.base.CommandLine; ...@@ -24,6 +26,7 @@ import org.chromium.base.CommandLine;
import org.chromium.base.ContextUtils; import org.chromium.base.ContextUtils;
import org.chromium.base.IntentUtils; import org.chromium.base.IntentUtils;
import org.chromium.base.Log; import org.chromium.base.Log;
import org.chromium.base.PackageManagerUtils;
import org.chromium.base.metrics.RecordHistogram; import org.chromium.base.metrics.RecordHistogram;
import org.chromium.base.metrics.RecordUserAction; import org.chromium.base.metrics.RecordUserAction;
import org.chromium.components.embedder_support.util.UrlConstants; import org.chromium.components.embedder_support.util.UrlConstants;
...@@ -220,7 +223,7 @@ public class ExternalNavigationHandler { ...@@ -220,7 +223,7 @@ public class ExternalNavigationHandler {
try { try {
Intent intent = Intent.parseUri(browserFallbackUrl, Intent.URI_INTENT_SCHEME); Intent intent = Intent.parseUri(browserFallbackUrl, Intent.URI_INTENT_SCHEME);
sanitizeQueryIntentActivitiesIntent(intent); sanitizeQueryIntentActivitiesIntent(intent);
List<ResolveInfo> resolvingInfos = mDelegate.queryIntentActivities(intent); List<ResolveInfo> resolvingInfos = queryIntentActivities(intent);
if (!isAlreadyInTargetWebApk(resolvingInfos, params) if (!isAlreadyInTargetWebApk(resolvingInfos, params)
&& launchWebApkIfSoleIntentHandler(resolvingInfos, intent)) { && launchWebApkIfSoleIntentHandler(resolvingInfos, intent)) {
return OverrideUrlLoadingResult.OVERRIDE_WITH_EXTERNAL_INTENT; return OverrideUrlLoadingResult.OVERRIDE_WITH_EXTERNAL_INTENT;
...@@ -547,7 +550,7 @@ public class ExternalNavigationHandler { ...@@ -547,7 +550,7 @@ public class ExternalNavigationHandler {
final Uri uri = targetIntent.getData(); final Uri uri = targetIntent.getData();
if (targetIntent.getPackage() == null && uri != null if (targetIntent.getPackage() == null && uri != null
&& UrlConstants.SMS_SCHEME.equals(uri.getScheme())) { && UrlConstants.SMS_SCHEME.equals(uri.getScheme())) {
List<ResolveInfo> resolvingInfos = mDelegate.queryIntentActivities(targetIntent); List<ResolveInfo> resolvingInfos = queryIntentActivities(targetIntent);
targetIntent.setPackage(getDefaultSmsPackageName(resolvingInfos)); targetIntent.setPackage(getDefaultSmsPackageName(resolvingInfos));
return true; return true;
} }
...@@ -640,8 +643,7 @@ public class ExternalNavigationHandler { ...@@ -640,8 +643,7 @@ public class ExternalNavigationHandler {
} }
if (previousIntent != null if (previousIntent != null
&& resolversSubsetOf( && resolversSubsetOf(resolvingInfos, queryIntentActivities(previousIntent))) {
resolvingInfos, mDelegate.queryIntentActivities(previousIntent))) {
if (DEBUG) Log.i(TAG, "Same host, no new resolvers"); if (DEBUG) Log.i(TAG, "Same host, no new resolvers");
return true; return true;
} }
...@@ -867,7 +869,7 @@ public class ExternalNavigationHandler { ...@@ -867,7 +869,7 @@ public class ExternalNavigationHandler {
if (hasIntentScheme) recordIntentActionMetrics(targetIntent); if (hasIntentScheme) recordIntentActionMetrics(targetIntent);
Intent debugIntent = new Intent(targetIntent); Intent debugIntent = new Intent(targetIntent);
List<ResolveInfo> resolvingInfos = mDelegate.queryIntentActivities(targetIntent); List<ResolveInfo> resolvingInfos = queryIntentActivities(targetIntent);
if (resolvingInfos.isEmpty()) { if (resolvingInfos.isEmpty()) {
return handleUnresolvableIntent(params, targetIntent, browserFallbackUrl); return handleUnresolvableIntent(params, targetIntent, browserFallbackUrl);
} }
...@@ -925,7 +927,7 @@ public class ExternalNavigationHandler { ...@@ -925,7 +927,7 @@ public class ExternalNavigationHandler {
} }
/** /**
* Sanitize intent to be passed to {@link ExternalNavigationDelegate#queryIntentActivities()} * Sanitize intent to be passed to {@link queryIntentActivities()}
* ensuring that web pages cannot bypass browser security. * ensuring that web pages cannot bypass browser security.
*/ */
private void sanitizeQueryIntentActivitiesIntent(Intent intent) { private void sanitizeQueryIntentActivitiesIntent(Intent intent) {
...@@ -1050,7 +1052,7 @@ public class ExternalNavigationHandler { ...@@ -1050,7 +1052,7 @@ public class ExternalNavigationHandler {
} }
if (intent.getPackage() != null) return true; if (intent.getPackage() != null) return true;
List<ResolveInfo> resolvingInfos = mDelegate.queryIntentActivities(intent); List<ResolveInfo> resolvingInfos = queryIntentActivities(intent);
return resolvingInfos != null && !resolvingInfos.isEmpty(); return resolvingInfos != null && !resolvingInfos.isEmpty();
} }
...@@ -1098,10 +1100,19 @@ public class ExternalNavigationHandler { ...@@ -1098,10 +1100,19 @@ public class ExternalNavigationHandler {
* Returns whether or not there's an activity available to handle the intent. * Returns whether or not there's an activity available to handle the intent.
*/ */
private boolean deviceCanHandleIntent(Intent intent) { private boolean deviceCanHandleIntent(Intent intent) {
List<ResolveInfo> resolveInfos = mDelegate.queryIntentActivities(intent); List<ResolveInfo> resolveInfos = queryIntentActivities(intent);
return resolveInfos != null && !resolveInfos.isEmpty(); return resolveInfos != null && !resolveInfos.isEmpty();
} }
/**
* See {@link PackageManagerUtils#queryIntentActivities(Intent, int)}
*/
@NonNull
private List<ResolveInfo> queryIntentActivities(Intent intent) {
return PackageManagerUtils.queryIntentActivities(
intent, PackageManager.GET_RESOLVED_FILTER);
}
private static boolean intentResolutionMatches(Intent intent, Intent other) { private static boolean intentResolutionMatches(Intent intent, Intent other) {
return intent.filterEquals(other) return intent.filterEquals(other)
&& (intent.getSelector() == other.getSelector() && (intent.getSelector() == other.getSelector()
......
...@@ -158,12 +158,6 @@ public class ExternalNavigationDelegateImpl implements ExternalNavigationDelegat ...@@ -158,12 +158,6 @@ public class ExternalNavigationDelegateImpl implements ExternalNavigationDelegat
|| PDF_MIME.equals(intent.getType()); || PDF_MIME.equals(intent.getType());
} }
@Override
public List<ResolveInfo> queryIntentActivities(Intent intent) {
return PackageManagerUtils.queryIntentActivities(
intent, PackageManager.GET_RESOLVED_FILTER);
}
@Override @Override
public boolean willChromeHandleIntent(Intent intent) { public boolean willChromeHandleIntent(Intent intent) {
return false; return false;
......
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