Pre-factor out the guts of the BinderClientTransport handshake. · grpc/grpc-java@9313e87 (original) (raw)

`@@ -25,6 +25,8 @@

`

25

25

`import android.os.IBinder;

`

26

26

`import android.os.Parcel;

`

27

27

`import android.os.Process;

`

``

28

`+

import androidx.annotation.BinderThread;

`

``

29

`+

import androidx.annotation.MainThread;

`

28

30

`import com.google.common.base.Ticker;

`

29

31

`import com.google.common.util.concurrent.FutureCallback;

`

30

32

`import com.google.common.util.concurrent.Futures;

`

`@@ -72,6 +74,9 @@ public final class BinderClientTransport extends BinderTransport

`

72

74

`private final SecurityPolicy securityPolicy;

`

73

75

`private final Bindable serviceBinding;

`

74

76

``

``

77

`+

@GuardedBy("this")

`

``

78

`+

private final ClientHandshake handshake;

`

``

79

+

75

80

`/** Number of ongoing calls which keep this transport "in-use". */

`

76

81

`private final AtomicInteger numInUseStreams;

`

77

82

``

`@@ -114,6 +119,7 @@ public BinderClientTransport(

`

114

119

`Boolean preAuthServerOverride = options.getEagAttributes().get(PRE_AUTH_SERVER_OVERRIDE);

`

115

120

`this.preAuthorizeServer =

`

116

121

`preAuthServerOverride != null ? preAuthServerOverride : factory.preAuthorizeServers;

`

``

122

`+

this.handshake = new LegacyClientHandshake();

`

117

123

`numInUseStreams = new AtomicInteger();

`

118

124

`pingTracker = new PingTracker(Ticker.systemTicker(), (id) -> sendPing(id));

`

119

125

``

`@@ -136,7 +142,7 @@ void releaseExecutors() {

`

136

142

``

137

143

`@Override

`

138

144

`public synchronized void onBound(IBinder binder) {

`

139

``

`-

sendSetupTransaction(binderDecorator.decorate(OneWayBinderProxy.wrap(binder, offloadExecutor)));

`

``

145

`+

handshake.onBound(binderDecorator.decorate(OneWayBinderProxy.wrap(binder, offloadExecutor)));

`

140

146

` }

`

141

147

``

142

148

`@Override

`

`@@ -340,10 +346,11 @@ protected void handleSetupTransport(Parcel parcel) {

`

340

346

`Status.UNAVAILABLE.withDescription("Failed to observe outgoing binder"), true);

`

341

347

`return;

`

342

348

` }

`

``

349

`+

handshake.handleSetupTransport();

`

``

350

`+

}

`

343

351

``

344

``

`-

int remoteUid = Binder.getCallingUid();

`

345

``

`-

restrictIncomingBinderToCallsFrom(remoteUid);

`

346

``

`-

attributes = setSecurityAttrs(attributes, remoteUid);

`

``

352

`+

@GuardedBy("this")

`

``

353

`+

private void checkServerAuthorization(int remoteUid) {

`

347

354

`ListenableFuture authResultFuture = register(checkServerAuthorizationAsync(remoteUid));

`

348

355

`Futures.addCallback(

`

349

356

`authResultFuture,

`

`@@ -376,7 +383,11 @@ private synchronized void handleAuthResult(Status authorization) {

`

376

383

`shutdownInternal(authorization, true);

`

377

384

`return;

`

378

385

` }

`

``

386

`+

handshake.onServerAuthorizationOk();

`

``

387

`+

}

`

379

388

``

``

389

`+

@GuardedBy("this")

`

``

390

`+

private void onHandshakeComplete() {

`

380

391

`setState(TransportState.READY);

`

381

392

`attributes = clientTransportListener.filterTransport(attributes);

`

382

393

`clientTransportListener.transportReady();

`

`@@ -397,6 +408,52 @@ protected void handlePingResponse(Parcel parcel) {

`

397

408

`pingTracker.onPingResponse(parcel.readInt());

`

398

409

` }

`

399

410

``

``

411

`+

/**

`

``

412

`+

`

``

413

`+

`

``

414

`+

`

``

415

`+

*/

`

``

416

`+

private interface ClientHandshake {

`

``

417

`+

/**

`

``

418

`+

`

``

419

`+

`

``

420

`+

*/

`

``

421

`+

@MainThread

`

``

422

`+

void onBound(OneWayBinderProxy endpointBinder);

`

``

423

+

``

424

`+

/** Notifies the implementation that we've received a valid SETUP_TRANSPORT transaction. */

`

``

425

`+

@BinderThread

`

``

426

`+

void handleSetupTransport();

`

``

427

+

``

428

`+

/** Notifies the implementation that the SecurityPolicy check of the server succeeded. */

`

``

429

`+

void onServerAuthorizationOk();

`

``

430

`+

}

`

``

431

+

``

432

`+

private final class LegacyClientHandshake implements ClientHandshake {

`

``

433

`+

@Override

`

``

434

`+

@MainThread

`

``

435

`` +

@GuardedBy("BinderClientTransport.this") // By way of @GuardedBy("this") handshake member.

``

``

436

`+

public void onBound(OneWayBinderProxy binder) {

`

``

437

`+

sendSetupTransaction(binder);

`

``

438

`+

}

`

``

439

+

``

440

`+

@Override

`

``

441

`+

@BinderThread

`

``

442

`` +

@GuardedBy("BinderClientTransport.this") // By way of @GuardedBy("this") handshake member.

``

``

443

`+

public void handleSetupTransport() {

`

``

444

`+

int remoteUid = Binder.getCallingUid();

`

``

445

`+

restrictIncomingBinderToCallsFrom(remoteUid);

`

``

446

`+

attributes = setSecurityAttrs(attributes, remoteUid);

`

``

447

`+

checkServerAuthorization(remoteUid);

`

``

448

`+

}

`

``

449

+

``

450

`+

@Override

`

``

451

`` +

@GuardedBy("BinderClientTransport.this") // By way of @GuardedBy("this") handshake member.

``

``

452

`+

public void onServerAuthorizationOk() {

`

``

453

`+

onHandshakeComplete();

`

``

454

`+

}

`

``

455

`+

}

`

``

456

+

400

457

`private static ClientStream newFailingClientStream(

`

401

458

`Status failure, Attributes attributes, Metadata headers, ClientStreamTracer[] tracers) {

`

402

459

`StatsTraceContext statsTraceContext =

`