Commit f94a7b84 authored by Peter Kotwicz's avatar Peter Kotwicz Committed by Commit Bot

Fix TrustedWebActivityClient#createLaunchIntentForTwa() non-HTTP URL crash

This CL fixes a crash when
TrustedWebActivityClient#createLaunchIntentForTwa() is given a non-HTTPS
URL

BUG=1057524
TEST=TrustedWebActivityClientTest#createLaunchIntentForTwaNonHttpScheme()

Change-Id: Ib16ebd309c2e450ac40183f1ef4f7c7c6bb4876f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2082485Reviewed-by: default avatarPeter Conn <peconn@chromium.org>
Commit-Queue: Peter Kotwicz <pkotwicz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#746674}
parent 5c46f273
......@@ -106,9 +106,10 @@ public class TrustedWebActivityClient {
Resources res = ContextUtils.getApplicationContext().getResources();
String channelDisplayName = res.getString(R.string.notification_category_group_general);
return connectAndExecute(origin.uri(), service ->
callback.onPermissionCheck(service.getComponentName(),
service.areNotificationsEnabled(channelDisplayName)));
return connectAndExecute(origin.uri(),
(originCopy, service)
-> callback.onPermissionCheck(service.getComponentName(),
service.areNotificationsEnabled(channelDisplayName)));
}
/**
......@@ -124,9 +125,8 @@ public class TrustedWebActivityClient {
NotificationBuilderBase builder, NotificationUmaTracker notificationUmaTracker) {
Resources res = ContextUtils.getApplicationContext().getResources();
String channelDisplayName = res.getString(R.string.notification_category_group_general);
Origin origin = Origin.createOrThrow(scope);
connectAndExecute(scope, service -> {
connectAndExecute(scope, (origin, service) -> {
if (!service.areNotificationsEnabled(channelDisplayName)) {
mDelegatesManager.updatePermission(origin,
service.getComponentName().getPackageName(), false);
......@@ -189,15 +189,18 @@ public class TrustedWebActivityClient {
* @param platformId The id of the notification to cancel.
*/
public void cancelNotification(Uri scope, String platformTag, int platformId) {
connectAndExecute(scope, service -> service.cancel(platformTag, platformId));
connectAndExecute(scope, (origin, service) -> service.cancel(platformTag, platformId));
}
private interface ExecutionCallback {
void onConnected(TrustedWebActivityServiceConnection service) throws RemoteException;
void onConnected(Origin origin, TrustedWebActivityServiceConnection service)
throws RemoteException;
}
private boolean connectAndExecute(Uri scope, ExecutionCallback callback) {
Origin origin = Origin.createOrThrow(scope);
Origin origin = Origin.create(scope);
if (origin == null) return false;
Set<Token> possiblePackages = mDelegatesManager.getAllDelegateApps(origin);
if (possiblePackages == null || possiblePackages.isEmpty()) return false;
......@@ -205,7 +208,7 @@ public class TrustedWebActivityClient {
mConnection.connect(scope, possiblePackages, AsyncTask.THREAD_POOL_EXECUTOR);
connection.addListener(() -> {
try {
callback.onConnected(connection.get());
callback.onConnected(origin, connection.get());
} catch (RemoteException | ExecutionException | InterruptedException e) {
Log.w(TAG, "Failed to execute TWA command.");
}
......@@ -231,7 +234,8 @@ public class TrustedWebActivityClient {
private @Nullable Intent createLaunchIntentForTwaInternal(Context appContext, String url,
List<ResolveInfo> resolveInfosForUrl) {
Origin origin = Origin.createOrThrow(url);
Origin origin = Origin.create(url);
if (origin == null) return null;
// Trusted Web Activities only work with https so we can shortcut here.
if (!UrlConstants.HTTPS_SCHEME.equals(origin.uri().getScheme())) return null;
......
......@@ -4,6 +4,7 @@
package org.chromium.chrome.browser.browserservices;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
......@@ -13,10 +14,15 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.ComponentName;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.RemoteException;
import androidx.browser.trusted.Token;
import androidx.browser.trusted.TrustedWebActivityServiceConnection;
import androidx.browser.trusted.TrustedWebActivityServiceConnectionPool;
import com.google.common.util.concurrent.ListenableFuture;
import org.junit.Before;
......@@ -26,6 +32,7 @@ import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.stubbing.Answer;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.chromium.base.test.BaseRobolectricTestRunner;
......@@ -34,14 +41,11 @@ import org.chromium.chrome.browser.notifications.ChromeNotification;
import org.chromium.chrome.browser.notifications.NotificationBuilderBase;
import org.chromium.chrome.browser.notifications.NotificationUmaTracker;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import androidx.browser.trusted.Token;
import androidx.browser.trusted.TrustedWebActivityServiceConnection;
import androidx.browser.trusted.TrustedWebActivityServiceConnectionPool;
/**
* Unit tests for {@link TrustedWebActivityClient}.
*/
......@@ -152,4 +156,10 @@ public class TrustedWebActivityClientTest {
mClient.notifyNotification(uri, "tag", 1, mNotificationBuilder,
mNotificationUmaTracker);
}
@Test
public void createLaunchIntentForTwaNonHttpScheme() {
assertNull(TrustedWebActivityClient.createLaunchIntentForTwa(RuntimeEnvironment.application,
"mailto:miranda@example.com", new ArrayList<ResolveInfo>()));
}
}
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