Skip to content

Commit c2eccca

Browse files
committed
cronet: Add internal API to specify Network
cl/661194496
1 parent fd8734f commit c2eccca

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

cronet/src/main/java/io/grpc/cronet/CronetChannelBuilder.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020
import static com.google.common.base.Preconditions.checkNotNull;
2121
import static io.grpc.internal.GrpcUtil.DEFAULT_MAX_MESSAGE_SIZE;
2222

23+
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;
108112

109113
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
}
192196

197+
/** 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;
297314

298315
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
}
310329

311330
@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
}

cronet/src/main/java/io/grpc/cronet/InternalCronetChannelBuilder.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
package io.grpc.cronet;
1818

19+
import android.net.Network;
1920
import io.grpc.Internal;
21+
import org.checkerframework.checker.nullness.qual.Nullable;
2022

2123
/**
2224
* Internal {@link CronetChannelBuilder} accessor. This is intended for usage internal to the gRPC
@@ -58,4 +60,9 @@ public static void setTrafficStatsTag(CronetChannelBuilder builder, int tag) {
5860
public static void setTrafficStatsUid(CronetChannelBuilder builder, int uid) {
5961
builder.setTrafficStatsUid(uid);
6062
}
63+
64+
/** Sets the network {@link android.net.Network} to use when relying traffic by this channel. */
65+
public static void bindToNetwork(CronetChannelBuilder builder, @Nullable Network network) {
66+
builder.bindToNetwork(network);
67+
}
6168
}

0 commit comments

Comments
 (0)