cronet: Add internal API to specify Network · grpc/grpc-java@c2eccca

@@ -20,9 +20,12 @@

2020

import static com.google.common.base.Preconditions.checkNotNull;

2121

import static io.grpc.internal.GrpcUtil.DEFAULT_MAX_MESSAGE_SIZE;

222223+

import android.net.Network;

24+

import android.os.Build;

2325

import com.google.common.annotations.VisibleForTesting;

2426

import com.google.common.base.Preconditions;

2527

import com.google.common.util.concurrent.MoreExecutors;

28+

import com.google.errorprone.annotations.CanIgnoreReturnValue;

2629

import com.google.errorprone.annotations.DoNotCall;

2730

import io.grpc.ChannelCredentials;

2831

import io.grpc.ChannelLogger;

@@ -105,6 +108,7 @@ public static CronetChannelBuilder forAddress(String name, int port) {

105108

private int trafficStatsTag;

106109

private boolean trafficStatsUidSet;

107110

private int trafficStatsUid;

111+

private Network network;

108112109113

private CronetChannelBuilder(String host, int port, CronetEngine cronetEngine) {

110114

final class CronetChannelTransportFactoryBuilder implements ClientTransportFactoryBuilder {

@@ -190,6 +194,13 @@ CronetChannelBuilder setTrafficStatsUid(int uid) {

190194

return this;

191195

}

192196197+

/** Sets the network ID to use for this channel traffic. */

198+

@CanIgnoreReturnValue

199+

CronetChannelBuilder bindToNetwork(@Nullable Network network) {

200+

this.network = network;

201+

return this;

202+

}

203+193204

/**

194205

* Provides a custom scheduled executor service.

195206

*

@@ -210,7 +221,12 @@ public CronetChannelBuilder scheduledExecutorService(

210221

ClientTransportFactory buildTransportFactory() {

211222

return new CronetTransportFactory(

212223

new TaggingStreamFactory(

213-

cronetEngine, trafficStatsTagSet, trafficStatsTag, trafficStatsUidSet, trafficStatsUid),

224+

cronetEngine,

225+

trafficStatsTagSet,

226+

trafficStatsTag,

227+

trafficStatsUidSet,

228+

trafficStatsUid,

229+

network),

214230

MoreExecutors.directExecutor(),

215231

scheduledExecutorService,

216232

maxMessageSize,

@@ -294,18 +310,21 @@ private static class TaggingStreamFactory extends StreamBuilderFactory {

294310

private final int trafficStatsTag;

295311

private final boolean trafficStatsUidSet;

296312

private final int trafficStatsUid;

313+

private final Network network;

297314298315

TaggingStreamFactory(

299316

CronetEngine cronetEngine,

300317

boolean trafficStatsTagSet,

301318

int trafficStatsTag,

302319

boolean trafficStatsUidSet,

303-

int trafficStatsUid) {

320+

int trafficStatsUid,

321+

Network network) {

304322

this.cronetEngine = cronetEngine;

305323

this.trafficStatsTagSet = trafficStatsTagSet;

306324

this.trafficStatsTag = trafficStatsTag;

307325

this.trafficStatsUidSet = trafficStatsUidSet;

308326

this.trafficStatsUid = trafficStatsUid;

327+

this.network = network;

309328

}

310329311330

@Override

@@ -320,6 +339,11 @@ public BidirectionalStream.Builder newBidirectionalStreamBuilder(

320339

if (trafficStatsUidSet) {

321340

builder.setTrafficStatsUid(trafficStatsUid);

322341

}

342+

if (network != null) {

343+

if (Build.VERSION.SDK_INT >= 23) {

344+

builder.bindToNetwork(network.getNetworkHandle());

345+

}

346+

}

323347

return builder;

324348

}

325349

}