2020import static com .google .common .base .Preconditions .checkNotNull ;
2121import static io .grpc .internal .GrpcUtil .DEFAULT_MAX_MESSAGE_SIZE ;
2222
23- import android .util .Log ;
2423import com .google .common .annotations .VisibleForTesting ;
2524import com .google .common .base .Preconditions ;
2625import com .google .common .util .concurrent .MoreExecutors ;
3837import io .grpc .internal .ManagedChannelImplBuilder .ClientTransportFactoryBuilder ;
3938import io .grpc .internal .SharedResourceHolder ;
4039import io .grpc .internal .TransportTracer ;
41- import java .lang .reflect .InvocationTargetException ;
42- import java .lang .reflect .Method ;
4340import java .net .InetSocketAddress ;
4441import java .net .SocketAddress ;
4542import java .util .Collection ;
4946import javax .annotation .Nullable ;
5047import org .chromium .net .BidirectionalStream ;
5148import org .chromium .net .CronetEngine ;
52- import org .chromium .net .ExperimentalBidirectionalStream ;
53- import org .chromium .net .ExperimentalCronetEngine ;
5449
5550/** Convenience class for building channels with the cronet transport. */
5651@ ExperimentalApi ("There is no plan to make this API stable, given transport API instability" )
5752public final class CronetChannelBuilder extends ForwardingChannelBuilder2 <CronetChannelBuilder > {
5853
59- private static final String LOG_TAG = "CronetChannelBuilder" ;
60-
6154 /** BidirectionalStream.Builder factory used for getting the gRPC BidirectionalStream. */
6255 public static abstract class StreamBuilderFactory {
6356 public abstract BidirectionalStream .Builder newBidirectionalStreamBuilder (
@@ -91,7 +84,7 @@ public static CronetChannelBuilder forAddress(String name, int port) {
9184
9285 private final CronetEngine cronetEngine ;
9386 private final ManagedChannelImplBuilder managedChannelImplBuilder ;
94- private TransportTracer .Factory transportTracerFactory = TransportTracer .getDefaultFactory ();
87+ private final TransportTracer .Factory transportTracerFactory = TransportTracer .getDefaultFactory ();
9588
9689 private boolean alwaysUsePut = false ;
9790
@@ -139,7 +132,7 @@ protected ManagedChannelBuilder<?> delegate() {
139132 * Sets the maximum message size allowed to be received on the channel. If not called,
140133 * defaults to {@link io.grpc.internal.GrpcUtil#DEFAULT_MAX_MESSAGE_SIZE}.
141134 */
142- public final CronetChannelBuilder maxMessageSize (int maxMessageSize ) {
135+ public CronetChannelBuilder maxMessageSize (int maxMessageSize ) {
143136 checkArgument (maxMessageSize >= 0 , "maxMessageSize must be >= 0" );
144137 this .maxMessageSize = maxMessageSize ;
145138 return this ;
@@ -148,7 +141,7 @@ public final CronetChannelBuilder maxMessageSize(int maxMessageSize) {
148141 /**
149142 * Sets the Cronet channel to always use PUT instead of POST. Defaults to false.
150143 */
151- public final CronetChannelBuilder alwaysUsePut (boolean enable ) {
144+ public CronetChannelBuilder alwaysUsePut (boolean enable ) {
152145 this .alwaysUsePut = enable ;
153146 return this ;
154147 }
@@ -170,7 +163,7 @@ public final CronetChannelBuilder alwaysUsePut(boolean enable) {
170163 * application.
171164 * @return the builder to facilitate chaining.
172165 */
173- final CronetChannelBuilder setTrafficStatsTag (int tag ) {
166+ CronetChannelBuilder setTrafficStatsTag (int tag ) {
174167 trafficStatsTagSet = true ;
175168 trafficStatsTag = tag ;
176169 return this ;
@@ -180,7 +173,7 @@ final CronetChannelBuilder setTrafficStatsTag(int tag) {
180173 * Sets specific UID to use when accounting socket traffic caused by this channel. See {@link
181174 * android.net.TrafficStats} for more information. Designed for use when performing an operation
182175 * on behalf of another application. Caller must hold {@link
183- * android.Manifest.permission#MODIFY_NETWORK_ACCOUNTING } permission. By default traffic is
176+ * android.Manifest.permission#UPDATE_DEVICE_STATS } permission. By default traffic is
184177 * attributed to UID of caller.
185178 *
186179 * <p><b>NOTE:</b>Setting a UID disallows sharing of sockets with channels with other UIDs, which
@@ -191,7 +184,7 @@ final CronetChannelBuilder setTrafficStatsTag(int tag) {
191184 * @param uid the UID to attribute socket traffic caused by this channel.
192185 * @return the builder to facilitate chaining.
193186 */
194- final CronetChannelBuilder setTrafficStatsUid (int uid ) {
187+ CronetChannelBuilder setTrafficStatsUid (int uid ) {
195188 trafficStatsUidSet = true ;
196189 trafficStatsUid = uid ;
197190 return this ;
@@ -207,7 +200,7 @@ final CronetChannelBuilder setTrafficStatsUid(int uid) {
207200 *
208201 * @since 1.12.0
209202 */
210- public final CronetChannelBuilder scheduledExecutorService (
203+ public CronetChannelBuilder scheduledExecutorService (
211204 ScheduledExecutorService scheduledExecutorService ) {
212205 this .scheduledExecutorService =
213206 checkNotNull (scheduledExecutorService , "scheduledExecutorService" );
@@ -296,11 +289,6 @@ public Collection<Class<? extends SocketAddress>> getSupportedSocketAddressTypes
296289 * StreamBuilderFactory impl that applies TrafficStats tags to stream builders that are produced.
297290 */
298291 private static class TaggingStreamFactory extends StreamBuilderFactory {
299- private static volatile boolean loadSetTrafficStatsTagAttempted ;
300- private static volatile boolean loadSetTrafficStatsUidAttempted ;
301- private static volatile Method setTrafficStatsTagMethod ;
302- private static volatile Method setTrafficStatsUidMethod ;
303-
304292 private final CronetEngine cronetEngine ;
305293 private final boolean trafficStatsTagSet ;
306294 private final int trafficStatsTag ;
@@ -323,74 +311,16 @@ private static class TaggingStreamFactory extends StreamBuilderFactory {
323311 @ Override
324312 public BidirectionalStream .Builder newBidirectionalStreamBuilder (
325313 String url , BidirectionalStream .Callback callback , Executor executor ) {
326- ExperimentalBidirectionalStream .Builder builder =
327- (( ExperimentalCronetEngine ) cronetEngine )
314+ BidirectionalStream .Builder builder =
315+ cronetEngine
328316 .newBidirectionalStreamBuilder (url , callback , executor );
329317 if (trafficStatsTagSet ) {
330- setTrafficStatsTag (builder , trafficStatsTag );
318+ builder . setTrafficStatsTag (trafficStatsTag );
331319 }
332320 if (trafficStatsUidSet ) {
333- setTrafficStatsUid (builder , trafficStatsUid );
321+ builder . setTrafficStatsUid (trafficStatsUid );
334322 }
335323 return builder ;
336324 }
337-
338- private static void setTrafficStatsTag (ExperimentalBidirectionalStream .Builder builder ,
339- int tag ) {
340- if (!loadSetTrafficStatsTagAttempted ) {
341- synchronized (TaggingStreamFactory .class ) {
342- if (!loadSetTrafficStatsTagAttempted ) {
343- try {
344- setTrafficStatsTagMethod = ExperimentalBidirectionalStream .Builder .class
345- .getMethod ("setTrafficStatsTag" , int .class );
346- } catch (NoSuchMethodException e ) {
347- Log .w (LOG_TAG ,
348- "Failed to load method ExperimentalBidirectionalStream.Builder.setTrafficStatsTag" ,
349- e );
350- } finally {
351- loadSetTrafficStatsTagAttempted = true ;
352- }
353- }
354- }
355- }
356- if (setTrafficStatsTagMethod != null ) {
357- try {
358- setTrafficStatsTagMethod .invoke (builder , tag );
359- } catch (InvocationTargetException e ) {
360- throw new RuntimeException (e .getCause () == null ? e .getTargetException () : e .getCause ());
361- } catch (IllegalAccessException e ) {
362- Log .w (LOG_TAG , "Failed to set traffic stats tag: " + tag , e );
363- }
364- }
365- }
366-
367- private static void setTrafficStatsUid (ExperimentalBidirectionalStream .Builder builder ,
368- int uid ) {
369- if (!loadSetTrafficStatsUidAttempted ) {
370- synchronized (TaggingStreamFactory .class ) {
371- if (!loadSetTrafficStatsUidAttempted ) {
372- try {
373- setTrafficStatsUidMethod = ExperimentalBidirectionalStream .Builder .class
374- .getMethod ("setTrafficStatsUid" , int .class );
375- } catch (NoSuchMethodException e ) {
376- Log .w (LOG_TAG ,
377- "Failed to load method ExperimentalBidirectionalStream.Builder.setTrafficStatsUid" ,
378- e );
379- } finally {
380- loadSetTrafficStatsUidAttempted = true ;
381- }
382- }
383- }
384- }
385- if (setTrafficStatsUidMethod != null ) {
386- try {
387- setTrafficStatsUidMethod .invoke (builder , uid );
388- } catch (InvocationTargetException e ) {
389- throw new RuntimeException (e .getCause () == null ? e .getTargetException () : e .getCause ());
390- } catch (IllegalAccessException e ) {
391- Log .w (LOG_TAG , "Failed to set traffic stats uid: " + uid , e );
392- }
393- }
394- }
395325 }
396326}
0 commit comments