Skip to content

Commit 9ec685b

Browse files
committed
resolve() throws
1 parent bde880f commit 9ec685b

3 files changed

Lines changed: 24 additions & 15 deletions

File tree

binder/src/main/java/io/grpc/binder/internal/Bindable.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import android.os.IBinder;
2121
import androidx.annotation.AnyThread;
2222
import androidx.annotation.MainThread;
23+
import com.google.common.util.concurrent.ListenableFuture;
2324
import io.grpc.Status;
25+
import io.grpc.StatusException;
2426

2527
/** An interface for managing a {@code Binder} connection. */
2628
interface Bindable {
@@ -48,7 +50,7 @@ interface Observer {
4850

4951
/** Fetches details about the remote service from PackageManager *before* binding to it. */
5052
@AnyThread
51-
ServiceInfo resolve();
53+
ServiceInfo resolve() throws StatusException;
5254

5355
/**
5456
* Attempt to bind with the remote service.

binder/src/main/java/io/grpc/binder/internal/BinderTransport.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -670,22 +670,22 @@ public synchronized Runnable start(ManagedClientTransport.Listener clientTranspo
670670

671671
@GuardedBy("this")
672672
private void preAuthorizeServer() {
673-
ServiceInfo serviceInfo = serviceBinding.resolve();
673+
ServiceInfo serviceInfo;
674+
try {
675+
serviceInfo = serviceBinding.resolve();
676+
} catch (StatusException e) {
677+
shutdownInternal(e.getStatus(), true);
678+
return;
679+
}
674680

675-
// It's unlikely, but in theory the server identity/existence represented by this ServiceInfo
676-
// could change by the time we actually bind/connect. It doesn't matter though, because:
681+
// It's unlikely, but the server identity/existence of this Service could change by the time
682+
// we actually connect. It doesn't matter though, because:
677683
// - If pre-auth fails (but would succeed for the new identity), grpc-core will retry
678684
// against the replacement server using a new instance of BinderClientTransport.
679685
// - If pre-auth succeeds (but would fail for the new identity), we might incorrectly bind
680-
// to an unauthorized server, but we'll notice when we the SecurityPolicy again as part of the
681-
// usual handshake.
682-
if (serviceInfo == null) {
683-
preAuthResultFuture =
684-
Futures.immediateFuture(
685-
Status.UNIMPLEMENTED.withDescription("resolveService() was null"));
686-
} else {
687-
preAuthResultFuture = checkServerAuthorizationAsync(serviceInfo.applicationInfo.uid);
688-
}
686+
// to an unauthorized server, but we'll notice when we check SecurityPolicy again as part of
687+
// the usual handshake.
688+
preAuthResultFuture = checkServerAuthorizationAsync(serviceInfo.applicationInfo.uid);
689689
Futures.addCallback(
690690
preAuthResultFuture,
691691
new FutureCallback<Status>() {

binder/src/main/java/io/grpc/binder/internal/ServiceBinding.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.google.common.annotations.VisibleForTesting;
3434
import com.google.errorprone.annotations.concurrent.GuardedBy;
3535
import io.grpc.Status;
36+
import io.grpc.StatusException;
3637
import io.grpc.binder.BinderChannelCredentials;
3738
import java.lang.reflect.Method;
3839
import java.util.concurrent.Executor;
@@ -275,12 +276,18 @@ private static ResolveInfo resolveServiceAsUser(
275276
}
276277

277278
@AnyThread
278-
public ServiceInfo resolve() {
279+
public ServiceInfo resolve() throws StatusException {
279280
checkState(sourceContext != null);
280281
try {
281282
ResolveInfo resolveInfo =
282283
resolveServiceAsUser(sourceContext.getPackageManager(), bindIntent, 0, targetUserHandle);
283-
return resolveInfo != null ? resolveInfo.serviceInfo : null;
284+
if (resolveInfo == null) {
285+
// Same code as when bindService() returns false.
286+
throw Status.UNIMPLEMENTED
287+
.withDescription("resolveService(" + bindIntent + ") returned null")
288+
.asException();
289+
}
290+
return resolveInfo.serviceInfo;
284291
} catch (ReflectiveOperationException e) {
285292
throw Status.fromThrowable(e).asRuntimeException();
286293
}

0 commit comments

Comments
 (0)