1 // Copyright 2016 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 package org.chromium.net.impl; 5 6 import androidx.annotation.IntDef; 7 import androidx.annotation.Nullable; 8 9 import android.net.http.BidirectionalStream; 10 import android.net.http.ExperimentalBidirectionalStream; 11 import android.net.http.ExperimentalHttpEngine; 12 import android.net.http.ExperimentalUrlRequest; 13 import android.net.http.HeaderBlock; 14 import android.net.http.RequestFinishedInfo; 15 import android.net.http.UrlRequest; 16 17 import java.lang.annotation.Retention; 18 import java.lang.annotation.RetentionPolicy; 19 import java.net.URL; 20 import java.util.Collection; 21 import java.util.List; 22 import java.util.Map; 23 import java.util.concurrent.Executor; 24 25 /** 26 * Base class of {@link CronetUrlRequestContext}. 27 */ 28 public abstract class CronetEngineBase extends ExperimentalHttpEngine { 29 /* 30 * Network handle representing the default network. To be used when a network has not been 31 * explicitly set. 32 */ 33 protected static final long DEFAULT_NETWORK_HANDLE = -1; 34 35 /** 36 * Creates a {@link UrlRequest} object. All callbacks will 37 * be called on {@code executor}'s thread. {@code executor} must not run 38 * tasks on the current thread to prevent blocking networking operations 39 * and causing exceptions during shutdown. 40 * 41 * @param url {@link URL} for the request. 42 * @param callback callback object that gets invoked on different events. 43 * @param executor {@link Executor} on which all callbacks will be invoked. 44 * @param priority priority of the request which should be one of the 45 * {@link UrlRequest.Builder#REQUEST_PRIORITY_IDLE REQUEST_PRIORITY_*} 46 * values. 47 * @param requestAnnotations Objects to pass on to 48 * {@link RequestFinishedInfo.Listener}. 49 * @param disableCache disables cache for the request. 50 * If context is not set up to use cache this param has no effect. 51 * @param disableConnectionMigration disables connection migration for this 52 * request if it is enabled for the session. 53 * @param allowDirectExecutor whether executors used by this request are permitted 54 * to execute submitted tasks inline. 55 * @param trafficStatsTagSet {@code true} if {@code trafficStatsTag} represents a TrafficStats 56 * tag to apply to sockets used to perform this request. 57 * @param trafficStatsTag TrafficStats tag to apply to sockets used to perform this request. 58 * @param trafficStatsUidSet {@code true} if {@code trafficStatsUid} represents a UID to 59 * attribute traffic used to perform this request. 60 * @param trafficStatsUid UID to attribute traffic used to perform this request. 61 * @param requestFinishedListener callback to get invoked with metrics when request is finished. 62 * Set to {@code null} if not used. 63 * @param idempotency idempotency of the request which should be one of the 64 * {@link ExperimentalUrlRequest.Builder#DEFAULT_IDEMPOTENCY IDEMPOTENT NOT_IDEMPOTENT} 65 * values. 66 * @param network network to be used to send this request. Set to {@code null} if not specified. 67 * @return new request. 68 */ createRequest(String url, UrlRequest.Callback callback, Executor executor, @RequestPriority int priority, Collection<Object> requestAnnotations, boolean disableCache, boolean disableConnectionMigration, boolean allowDirectExecutor, boolean trafficStatsTagSet, int trafficStatsTag, boolean trafficStatsUidSet, int trafficStatsUid, @Nullable RequestFinishedInfo.Listener requestFinishedListener, @Idempotency int idempotency, long networkHandle, HeaderBlock headerBlock)69 protected abstract UrlRequestBase createRequest(String url, UrlRequest.Callback callback, 70 Executor executor, @RequestPriority int priority, Collection<Object> requestAnnotations, 71 boolean disableCache, boolean disableConnectionMigration, boolean allowDirectExecutor, 72 boolean trafficStatsTagSet, int trafficStatsTag, boolean trafficStatsUidSet, 73 int trafficStatsUid, @Nullable RequestFinishedInfo.Listener requestFinishedListener, 74 @Idempotency int idempotency, long networkHandle, HeaderBlock headerBlock); 75 76 /** 77 * Creates a {@link BidirectionalStream} object. {@code callback} methods will 78 * be invoked on {@code executor}. {@code executor} must not run 79 * tasks on the current thread to prevent blocking networking operations 80 * and causing exceptions during shutdown. 81 * 82 * @param url the URL for the stream 83 * @param callback the object whose methods get invoked upon different events 84 * @param executor the {@link Executor} on which all callbacks will be called 85 * @param httpMethod the HTTP method to use for the stream 86 * @param requestHeaders the list of request headers 87 * @param priority priority of the stream which should be one of the 88 * {@link BidirectionalStream.Builder#STREAM_PRIORITY_IDLE STREAM_PRIORITY_*} 89 * values. 90 * @param delayRequestHeadersUntilFirstFlush whether to delay sending request 91 * headers until flush() is called, and try to combine them 92 * with the next data frame. 93 * @param requestAnnotations Objects to pass on to 94 * {@link RequestFinishedInfo.Listener}. 95 * @param trafficStatsTagSet {@code true} if {@code trafficStatsTag} represents a TrafficStats 96 * tag to apply to sockets used to perform this request. 97 * @param trafficStatsTag TrafficStats tag to apply to sockets used to perform this request. 98 * @param trafficStatsUidSet {@code true} if {@code trafficStatsUid} represents a UID to 99 * attribute traffic used to perform this request. 100 * @param trafficStatsUid UID to attribute traffic used to perform this request. 101 * @param network network to be used to send this request. Set to {@code null} if not specified. 102 * @return a new stream. 103 */ createBidirectionalStream(String url, BidirectionalStream.Callback callback, Executor executor, String httpMethod, List<Map.Entry<String, String>> requestHeaders, @StreamPriority int priority, boolean delayRequestHeadersUntilFirstFlush, Collection<Object> requestAnnotations, boolean trafficStatsTagSet, int trafficStatsTag, boolean trafficStatsUidSet, int trafficStatsUid, long networkHandle)104 protected abstract ExperimentalBidirectionalStream createBidirectionalStream(String url, 105 BidirectionalStream.Callback callback, Executor executor, String httpMethod, 106 List<Map.Entry<String, String>> requestHeaders, @StreamPriority int priority, 107 boolean delayRequestHeadersUntilFirstFlush, Collection<Object> requestAnnotations, 108 boolean trafficStatsTagSet, int trafficStatsTag, boolean trafficStatsUidSet, 109 int trafficStatsUid, long networkHandle); 110 111 @Override newUrlRequestBuilder( String url, Executor executor, UrlRequest.Callback callback)112 public ExperimentalUrlRequest.Builder newUrlRequestBuilder( 113 String url, Executor executor, UrlRequest.Callback callback) { 114 return new UrlRequestBuilderImpl(url, callback, executor, this); 115 } 116 117 @IntDef({UrlRequest.REQUEST_PRIORITY_IDLE, UrlRequest.REQUEST_PRIORITY_LOWEST, 118 UrlRequest.REQUEST_PRIORITY_LOW, UrlRequest.REQUEST_PRIORITY_MEDIUM, 119 UrlRequest.REQUEST_PRIORITY_HIGHEST}) 120 @Retention(RetentionPolicy.SOURCE) 121 public @interface RequestPriority {} 122 123 @IntDef({ 124 BidirectionalStream.STREAM_PRIORITY_IDLE, 125 BidirectionalStream.STREAM_PRIORITY_LOWEST, 126 BidirectionalStream.STREAM_PRIORITY_LOW, 127 BidirectionalStream.STREAM_PRIORITY_MEDIUM, 128 BidirectionalStream.STREAM_PRIORITY_HIGHEST, 129 }) 130 @Retention(RetentionPolicy.SOURCE) 131 public @interface StreamPriority {} 132 133 @IntDef({ExperimentalUrlRequest.Builder.DEFAULT_IDEMPOTENCY, 134 ExperimentalUrlRequest.Builder.IDEMPOTENT, 135 ExperimentalUrlRequest.Builder.NOT_IDEMPOTENT}) 136 @Retention(RetentionPolicy.SOURCE) 137 public @interface Idempotency {} 138 } 139