|
19 | 19 | import static com.google.common.base.Preconditions.checkNotNull; |
20 | 20 | import static com.google.common.base.Preconditions.checkState; |
21 | 21 | import static com.google.common.util.concurrent.Futures.immediateFuture; |
| 22 | +import static io.grpc.binder.internal.TransactionUtils.newCallerFilteringHandler; |
22 | 23 |
|
23 | 24 | import android.os.DeadObjectException; |
24 | 25 | import android.os.IBinder; |
|
31 | 32 | import com.google.common.util.concurrent.ListenableFuture; |
32 | 33 | import com.google.errorprone.annotations.concurrent.GuardedBy; |
33 | 34 | import io.grpc.Attributes; |
| 35 | +import io.grpc.Grpc; |
34 | 36 | import io.grpc.Internal; |
| 37 | +import io.grpc.InternalChannelz; |
35 | 38 | import io.grpc.InternalChannelz.SocketStats; |
36 | 39 | import io.grpc.InternalLogId; |
37 | 40 | import io.grpc.Status; |
38 | 41 | import io.grpc.StatusException; |
39 | 42 | import io.grpc.binder.InboundParcelablePolicy; |
| 43 | +import io.grpc.binder.internal.LeakSafeOneWayBinder.TransactionHandler; |
40 | 44 | import io.grpc.internal.ObjectPool; |
41 | 45 | import java.util.ArrayList; |
42 | 46 | import java.util.Iterator; |
@@ -153,6 +157,7 @@ protected enum TransportState { |
153 | 157 | private final ObjectPool<ScheduledExecutorService> executorServicePool; |
154 | 158 | private final ScheduledExecutorService scheduledExecutorService; |
155 | 159 | private final InternalLogId logId; |
| 160 | + @GuardedBy("this") |
156 | 161 | private final LeakSafeOneWayBinder incomingBinder; |
157 | 162 |
|
158 | 163 | protected final ConcurrentHashMap<Integer, Inbound<?>> ongoingCalls; |
@@ -205,7 +210,15 @@ public final ScheduledExecutorService getScheduledExecutorService() { |
205 | 210 |
|
206 | 211 | // Override in child class. |
207 | 212 | public final ListenableFuture<SocketStats> getStats() { |
208 | | - return immediateFuture(null); |
| 213 | + Attributes attributes = getAttributes(); |
| 214 | + return immediateFuture( |
| 215 | + new InternalChannelz.SocketStats( |
| 216 | + /* data= */ null, // TODO: Keep track of these stats with TransportTracer or similar. |
| 217 | + /* local= */ attributes.get(Grpc.TRANSPORT_ATTR_LOCAL_ADDR), |
| 218 | + /* remote= */ attributes.get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR), |
| 219 | + // TODO: SocketOptions are meaningless for binder but we're still forced to provide one. |
| 220 | + new InternalChannelz.SocketOptions.Builder().build(), |
| 221 | + /* security= */ null)); |
209 | 222 | } |
210 | 223 |
|
211 | 224 | // Override in child class. |
@@ -466,6 +479,15 @@ private boolean handleTransactionInternal(int code, Parcel parcel) { |
466 | 479 | } |
467 | 480 | } |
468 | 481 |
|
| 482 | + @BinderThread |
| 483 | + @GuardedBy("this") |
| 484 | + protected void restrictIncomingBinderToCallsFrom(int allowedCallingUid) { |
| 485 | + TransactionHandler currentHandler = incomingBinder.getHandler(); |
| 486 | + if (currentHandler != null) { |
| 487 | + incomingBinder.setHandler(newCallerFilteringHandler(allowedCallingUid, currentHandler)); |
| 488 | + } |
| 489 | + } |
| 490 | + |
469 | 491 | @Nullable |
470 | 492 | @GuardedBy("this") |
471 | 493 | protected Inbound<?> createInbound(int callId) { |
@@ -551,6 +573,11 @@ Map<Integer, Inbound<?>> getOngoingCalls() { |
551 | 573 | return ongoingCalls; |
552 | 574 | } |
553 | 575 |
|
| 576 | + @VisibleForTesting |
| 577 | + LeakSafeOneWayBinder getIncomingBinderForTesting() { |
| 578 | + return this.incomingBinder; |
| 579 | + } |
| 580 | + |
554 | 581 | private static Status statusFromRemoteException(RemoteException e) { |
555 | 582 | if (e instanceof DeadObjectException || e instanceof TransactionTooLargeException) { |
556 | 583 | // These are to be expected from time to time and can simply be retried. |
|
0 commit comments