Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.net; | 5 package org.chromium.net; |
| 6 | 6 |
| 7 import android.support.annotation.IntDef; | |
| 8 import android.util.Log; | |
| 9 import android.util.Pair; | |
| 10 | |
| 11 import java.lang.annotation.Retention; | |
| 12 import java.lang.annotation.RetentionPolicy; | |
| 13 import java.nio.ByteBuffer; | 7 import java.nio.ByteBuffer; |
| 14 import java.util.ArrayList; | |
| 15 import java.util.Collection; | |
| 16 import java.util.Collections; | |
| 17 import java.util.concurrent.Executor; | 8 import java.util.concurrent.Executor; |
| 18 | 9 |
| 19 /** | 10 /** |
| 20 * Controls an HTTP request (GET, PUT, POST etc). | 11 * Controls an HTTP request (GET, PUT, POST etc). |
| 21 * Created using {@link UrlRequest.Builder}. | 12 * Created using {@link CronetEngine#newUrlRequestBuilder(String, Callback, Exec utor)}. |
|
pauljensen
2016/09/26 14:51:21
you replaced a type with a method that returns tha
kapishnikov
2016/09/27 18:38:26
Done.
| |
| 22 * Note: All methods must be called on the {@link Executor} passed in during cre ation. | 13 * Note: All methods must be called on the {@link Executor} passed in during cre ation. |
|
pauljensen
2016/09/26 14:51:21
in during creation->to {@link CronetEngine#newUrlR
kapishnikov
2016/09/27 18:38:26
Done.
| |
| 23 */ | 14 */ |
| 24 public interface UrlRequest { | 15 public abstract class UrlRequest { |
| 25 /** | 16 /** |
| 26 * Builder for {@link UrlRequest}s. Allows configuring requests before const ructing them | 17 * Builder for {@link UrlRequest}s. Allows configuring requests before const ructing them |
| 27 * with {@link Builder#build}. | 18 * with {@link Builder#build}. |
|
pauljensen
2016/09/27 12:52:32
We need a sentence explaining how to create a UrlR
kapishnikov
2016/09/27 18:38:26
Done.
| |
| 28 */ | 19 */ |
| 29 public static final class Builder { | 20 public abstract static class Builder { |
| 30 private static final String ACCEPT_ENCODING = "Accept-Encoding"; | |
| 31 // All fields are temporary storage of UrlRequest configuration to be | |
| 32 // copied to built UrlRequests. | |
| 33 | |
| 34 // CronetEngine to execute request. | |
| 35 final CronetEngine mCronetEngine; | |
| 36 // URL to request. | |
| 37 final String mUrl; | |
| 38 // Callback to receive progress callbacks. | |
| 39 final Callback mCallback; | |
| 40 // Executor to invoke callback on. | |
| 41 final Executor mExecutor; | |
| 42 // HTTP method (e.g. GET, POST etc). | |
| 43 String mMethod; | |
| 44 // List of request headers, stored as header field name and value pairs. | |
| 45 final ArrayList<Pair<String, String>> mRequestHeaders = | |
| 46 new ArrayList<Pair<String, String>>(); | |
| 47 // Disable the cache for just this request. | |
| 48 boolean mDisableCache; | |
| 49 // Disable connection migration for just this request. | |
| 50 boolean mDisableConnectionMigration; | |
| 51 // Priority of request. Default is medium. | |
| 52 @RequestPriority int mPriority = REQUEST_PRIORITY_MEDIUM; | |
| 53 // Request reporting annotations. Avoid extra object creation if no anno tations added. | |
| 54 Collection<Object> mRequestAnnotations = Collections.emptyList(); | |
| 55 // If request is an upload, this provides the request body data. | |
| 56 UploadDataProvider mUploadDataProvider; | |
| 57 // Executor to call upload data provider back on. | |
| 58 Executor mUploadDataProviderExecutor; | |
| 59 private boolean mAllowDirectExecutor = false; | |
| 60 | |
| 61 /** | |
| 62 * Creates a builder for {@link UrlRequest} objects. All callbacks for | |
| 63 * generated {@link UrlRequest} objects will be invoked on | |
| 64 * {@code executor}'s thread. {@code executor} must not run tasks on the | |
| 65 * current thread to prevent blocking networking operations and causing | |
| 66 * exceptions during shutdown. | |
| 67 * | |
| 68 * @param url {@link java.net.URL} for the generated requests. | |
| 69 * @param callback callback object that gets invoked on different events . | |
| 70 * @param executor {@link Executor} on which all callbacks will be invok ed. | |
| 71 * @param cronetEngine {@link CronetEngine} used to execute this request . | |
| 72 */ | |
| 73 public Builder( | |
| 74 String url, Callback callback, Executor executor, CronetEngine c ronetEngine) { | |
| 75 if (url == null) { | |
| 76 throw new NullPointerException("URL is required."); | |
| 77 } | |
| 78 if (callback == null) { | |
| 79 throw new NullPointerException("Callback is required."); | |
| 80 } | |
| 81 if (executor == null) { | |
| 82 throw new NullPointerException("Executor is required."); | |
| 83 } | |
| 84 if (cronetEngine == null) { | |
| 85 throw new NullPointerException("CronetEngine is required."); | |
| 86 } | |
| 87 mUrl = url; | |
| 88 mCallback = callback; | |
| 89 mExecutor = executor; | |
| 90 mCronetEngine = cronetEngine; | |
| 91 } | |
| 92 | |
| 93 /** | 21 /** |
| 94 * Sets the HTTP method verb to use for this request. | 22 * Sets the HTTP method verb to use for this request. |
| 95 * | 23 * |
| 96 * <p>The default when this method is not called is "GET" if the request has | 24 * <p>The default when this method is not called is "GET" if the request has |
| 97 * no body or "POST" if it does. | 25 * no body or "POST" if it does. |
| 98 * | 26 * |
| 99 * @param method "GET", "HEAD", "DELETE", "POST" or "PUT". | 27 * @param method "GET", "HEAD", "DELETE", "POST" or "PUT". |
| 100 * @return the builder to facilitate chaining. | 28 * @return the builder to facilitate chaining. |
| 101 */ | 29 */ |
| 102 public Builder setHttpMethod(String method) { | 30 public abstract Builder setHttpMethod(String method); |
| 103 if (method == null) { | |
| 104 throw new NullPointerException("Method is required."); | |
| 105 } | |
| 106 mMethod = method; | |
| 107 return this; | |
| 108 } | |
| 109 | 31 |
| 110 /** | 32 /** |
| 111 * Adds a request header. | 33 * Adds a request header. |
| 112 * | 34 * |
| 113 * @param header header name. | 35 * @param header header name. |
| 114 * @param value header value. | 36 * @param value header value. |
| 115 * @return the builder to facilitate chaining. | 37 * @return the builder to facilitate chaining. |
| 116 */ | 38 */ |
| 117 public Builder addHeader(String header, String value) { | 39 public abstract Builder addHeader(String header, String value); |
| 118 if (header == null) { | |
| 119 throw new NullPointerException("Invalid header name."); | |
| 120 } | |
| 121 if (value == null) { | |
| 122 throw new NullPointerException("Invalid header value."); | |
| 123 } | |
| 124 if (ACCEPT_ENCODING.equalsIgnoreCase(header)) { | |
| 125 Log.w("cronet", | |
| 126 "It's not necessary to set Accept-Encoding on requests - cronet will do" | |
| 127 + " this automatically for you, and setting it y ourself has no " | |
| 128 + "effect. See https://crbug.com/581399 for deta ils.", | |
| 129 new Exception()); | |
| 130 return this; | |
| 131 } | |
| 132 mRequestHeaders.add(Pair.create(header, value)); | |
| 133 return this; | |
| 134 } | |
| 135 | 40 |
| 136 /** | 41 /** |
| 137 * Disables cache for the request. If context is not set up to use cache , | 42 * Disables cache for the request. If context is not set up to use cache , |
| 138 * this call has no effect. | 43 * this call has no effect. |
| 139 * @return the builder to facilitate chaining. | 44 * @return the builder to facilitate chaining. |
| 140 */ | 45 */ |
| 141 public Builder disableCache() { | 46 public abstract Builder disableCache(); |
| 142 mDisableCache = true; | |
| 143 return this; | |
| 144 } | |
| 145 | |
| 146 /** | |
| 147 * Disables connection migration for the request if enabled for | |
| 148 * the session. | |
| 149 * @return the builder to facilitate chaining. | |
| 150 * | |
| 151 * @hide as experimental. | |
| 152 */ | |
| 153 public Builder disableConnectionMigration() { | |
| 154 mDisableConnectionMigration = true; | |
| 155 return this; | |
| 156 } | |
| 157 | |
| 158 /** @hide */ | |
| 159 @IntDef({ | |
| 160 REQUEST_PRIORITY_IDLE, REQUEST_PRIORITY_LOWEST, REQUEST_PRIORITY _LOW, | |
| 161 REQUEST_PRIORITY_MEDIUM, REQUEST_PRIORITY_HIGHEST, | |
| 162 }) | |
| 163 @Retention(RetentionPolicy.SOURCE) | |
| 164 public @interface RequestPriority {} | |
|
pauljensen
2016/09/26 14:51:21
how come you removed this?
kapishnikov
2016/09/27 18:38:26
I moved it to CronetEngineBase since it is used th
| |
| 165 | 47 |
| 166 /** | 48 /** |
| 167 * Lowest request priority. Passed to {@link #setPriority}. | 49 * Lowest request priority. Passed to {@link #setPriority}. |
| 168 */ | 50 */ |
| 169 public static final int REQUEST_PRIORITY_IDLE = 0; | 51 public static final int REQUEST_PRIORITY_IDLE = 0; |
| 170 /** | 52 /** |
| 171 * Very low request priority. Passed to {@link #setPriority}. | 53 * Very low request priority. Passed to {@link #setPriority}. |
| 172 */ | 54 */ |
| 173 public static final int REQUEST_PRIORITY_LOWEST = 1; | 55 public static final int REQUEST_PRIORITY_LOWEST = 1; |
| 174 /** | 56 /** |
| 175 * Low request priority. Passed to {@link #setPriority}. | 57 * Low request priority. Passed to {@link #setPriority}. |
| 176 */ | 58 */ |
| 177 public static final int REQUEST_PRIORITY_LOW = 2; | 59 public static final int REQUEST_PRIORITY_LOW = 2; |
| 178 /** | 60 /** |
| 179 * Medium request priority. Passed to {@link #setPriority}. This is the | 61 * Medium request priority. Passed to {@link #setPriority}. This is the |
| 180 * default priority given to the request. | 62 * default priority given to the request. |
| 181 */ | 63 */ |
| 182 public static final int REQUEST_PRIORITY_MEDIUM = 3; | 64 public static final int REQUEST_PRIORITY_MEDIUM = 3; |
| 183 /** | 65 /** |
| 184 * Highest request priority. Passed to {@link #setPriority}. | 66 * Highest request priority. Passed to {@link #setPriority}. |
| 185 */ | 67 */ |
| 186 public static final int REQUEST_PRIORITY_HIGHEST = 4; | 68 public static final int REQUEST_PRIORITY_HIGHEST = 4; |
| 187 | 69 |
| 188 /** | 70 /** |
| 189 * Sets priority of the request which should be one of the | 71 * Sets priority of the request which should be one of the |
| 190 * {@link #REQUEST_PRIORITY_IDLE REQUEST_PRIORITY_*} values. | 72 * {@link #REQUEST_PRIORITY_IDLE REQUEST_PRIORITY_*} values. |
| 191 * The request is given {@link #REQUEST_PRIORITY_MEDIUM} priority if {@l ink | 73 * The request is given {@link #REQUEST_PRIORITY_MEDIUM} priority by def ault. |
| 192 * #setPriority} is not called. | |
| 193 * | 74 * |
| 194 * @param priority priority of the request which should be one of the | 75 * @param priority priority of the request which should be one of the |
| 195 * {@link #REQUEST_PRIORITY_IDLE REQUEST_PRIORITY_*} values. | 76 * {@link #REQUEST_PRIORITY_IDLE REQUEST_PRIORITY_*} values. |
| 196 * @return the builder to facilitate chaining. | 77 * @return the builder to facilitate chaining. |
| 197 */ | 78 */ |
| 198 public Builder setPriority(@RequestPriority int priority) { | 79 public abstract Builder setPriority(int priority); |
| 199 mPriority = priority; | |
| 200 return this; | |
| 201 } | |
| 202 | 80 |
| 203 /** | 81 /** |
| 204 * Sets upload data provider. Switches method to "POST" if not | 82 * Sets upload data provider. Switches method to "POST" if not |
| 205 * explicitly set. Starting the request will throw an exception if a | 83 * explicitly set. Starting the request will throw an exception if a |
| 206 * Content-Type header is not set. | 84 * Content-Type header is not set. |
| 207 * | 85 * |
| 208 * @param uploadDataProvider responsible for providing the upload data. | 86 * @param uploadDataProvider responsible for providing the upload data. |
| 209 * @param executor All {@code uploadDataProvider} methods will be invoke d | 87 * @param executor All {@code uploadDataProvider} methods will be invoke d |
| 210 * using this {@code Executor}. May optionally be the same | 88 * using this {@code Executor}. May optionally be the same |
| 211 * {@code Executor} the request itself is using. | 89 * {@code Executor} the request itself is using. |
| 212 * @return the builder to facilitate chaining. | 90 * @return the builder to facilitate chaining. |
| 213 */ | 91 */ |
| 214 public Builder setUploadDataProvider( | 92 public abstract Builder setUploadDataProvider( |
| 215 UploadDataProvider uploadDataProvider, Executor executor) { | 93 UploadDataProvider uploadDataProvider, Executor executor); |
| 216 if (uploadDataProvider == null) { | |
| 217 throw new NullPointerException("Invalid UploadDataProvider."); | |
| 218 } | |
| 219 if (executor == null) { | |
| 220 throw new NullPointerException("Invalid UploadDataProvider Execu tor."); | |
| 221 } | |
| 222 if (mMethod == null) { | |
| 223 mMethod = "POST"; | |
| 224 } | |
| 225 mUploadDataProvider = uploadDataProvider; | |
| 226 mUploadDataProviderExecutor = executor; | |
| 227 return this; | |
| 228 } | |
| 229 | 94 |
| 230 /** | 95 /** |
| 231 * Marks that the executors this request will use to notify callbacks (f or | 96 * Marks that the executors this request will use to notify callbacks (f or |
| 232 * {@code UploadDataProvider}s and {@code UrlRequest.Callback}s) is inte ntionally performing | 97 * {@code UploadDataProvider}s and {@code UrlRequest.Callback}s) is inte ntionally performing |
| 233 * inline execution, like Guava's directExecutor or | 98 * inline execution, like Guava's directExecutor or |
| 234 * {@link java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy}. | 99 * {@link java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy}. |
| 235 * | 100 * |
| 236 * <p><b>Warning:</b> This option makes it easy to accidentally block th e network thread. | 101 * <p><b>Warning:</b> This option makes it easy to accidentally block th e network thread. |
| 237 * It should not be used if your callbacks perform disk I/O, acquire loc ks, or call into | 102 * It should not be used if your callbacks perform disk I/O, acquire loc ks, or call into |
| 238 * other code you don't carefully control and audit. | 103 * other code you don't carefully control and audit. |
| 239 */ | 104 */ |
| 240 public Builder allowDirectExecutor() { | 105 public abstract Builder allowDirectExecutor(); |
| 241 mAllowDirectExecutor = true; | |
| 242 return this; | |
| 243 } | |
| 244 | |
| 245 /** | |
| 246 * Associates the annotation object with this request. May add more than one. | |
| 247 * Passed through to a {@link RequestFinishedInfo.Listener}, | |
| 248 * see {@link RequestFinishedInfo#getAnnotations}. | |
| 249 * | |
| 250 * @param annotation an object to pass on to the {@link RequestFinishedI nfo.Listener} with a | |
| 251 * {@link RequestFinishedInfo}. | |
| 252 * @return the builder to facilitate chaining. | |
| 253 * | |
| 254 * @hide as it's a prototype. | |
| 255 */ | |
| 256 public Builder addRequestAnnotation(Object annotation) { | |
| 257 if (annotation == null) { | |
| 258 throw new NullPointerException("Invalid metrics annotation."); | |
| 259 } | |
| 260 if (mRequestAnnotations.isEmpty()) { | |
| 261 mRequestAnnotations = new ArrayList<Object>(); | |
| 262 } | |
| 263 mRequestAnnotations.add(annotation); | |
| 264 return this; | |
| 265 } | |
| 266 | 106 |
| 267 /** | 107 /** |
| 268 * Creates a {@link UrlRequest} using configuration within this | 108 * Creates a {@link UrlRequest} using configuration within this |
| 269 * {@link Builder}. The returned {@code UrlRequest} can then be started | 109 * {@link Builder}. The returned {@code UrlRequest} can then be started |
| 270 * by calling {@link UrlRequest#start}. | 110 * by calling {@link UrlRequest#start}. |
| 271 * | 111 * |
| 272 * @return constructed {@link UrlRequest} using configuration within | 112 * @return constructed {@link UrlRequest} using configuration within |
| 273 * this {@link Builder}. | 113 * this {@link Builder}. |
| 274 */ | 114 */ |
| 275 public UrlRequest build() { | 115 public abstract UrlRequest build(); |
| 276 final UrlRequest request = mCronetEngine.createRequest(mUrl, mCallba ck, mExecutor, | |
| 277 mPriority, mRequestAnnotations, mDisableCache, mDisableConne ctionMigration, | |
| 278 mAllowDirectExecutor); | |
| 279 if (mMethod != null) { | |
| 280 request.setHttpMethod(mMethod); | |
| 281 } | |
| 282 for (Pair<String, String> header : mRequestHeaders) { | |
| 283 request.addHeader(header.first, header.second); | |
| 284 } | |
| 285 if (mUploadDataProvider != null) { | |
| 286 request.setUploadDataProvider(mUploadDataProvider, mUploadDataPr oviderExecutor); | |
| 287 } | |
| 288 return request; | |
| 289 } | |
| 290 } | 116 } |
| 291 | 117 |
| 292 /** | 118 /** |
| 293 * Users of Cronet extend this class to receive callbacks indicating the | 119 * Users of Cronet extend this class to receive callbacks indicating the |
| 294 * progress of a {@link UrlRequest} being processed. An instance of this cla ss | 120 * progress of a {@link UrlRequest} being processed. An instance of this cla ss |
| 295 * is passed in to {@link UrlRequest.Builder}'s constructor when | 121 * is passed in to {@link UrlRequest.Builder}'s constructor when |
| 296 * constructing the {@code UrlRequest}. | 122 * constructing the {@code UrlRequest}. |
| 297 * <p> | 123 * <p> |
| 298 * Note: All methods will be invoked on the thread of the | 124 * Note: All methods will be invoked on the thread of the |
| 299 * {@link java.util.concurrent.Executor} used during construction of the | 125 * {@link java.util.concurrent.Executor} used during construction of the |
| 300 * {@code UrlRequest}. | 126 * {@code UrlRequest}. |
| 301 */ | 127 */ |
| 302 public abstract class Callback { | 128 public abstract static class Callback { |
| 303 /** | 129 /** |
| 304 * Invoked whenever a redirect is encountered. This will only be invoked | 130 * Invoked whenever a redirect is encountered. This will only be invoked |
| 305 * between the call to {@link UrlRequest#start} and | 131 * between the call to {@link UrlRequest#start} and |
| 306 * {@link Callback#onResponseStarted onResponseStarted()}. | 132 * {@link Callback#onResponseStarted onResponseStarted()}. |
| 307 * The body of the redirect response, if it has one, will be ignored. | 133 * The body of the redirect response, if it has one, will be ignored. |
| 308 * | 134 * |
| 309 * The redirect will not be followed until the URLRequest's | 135 * The redirect will not be followed until the URLRequest's |
| 310 * {@link UrlRequest#followRedirect} method is called, either | 136 * {@link UrlRequest#followRedirect} method is called, either |
| 311 * synchronously or asynchronously. | 137 * synchronously or asynchronously. |
| 312 * | 138 * |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 396 * @param info Response information. May be {@code null} if no response was | 222 * @param info Response information. May be {@code null} if no response was |
| 397 * received. | 223 * received. |
| 398 */ | 224 */ |
| 399 public void onCanceled(UrlRequest request, UrlResponseInfo info) {} | 225 public void onCanceled(UrlRequest request, UrlResponseInfo info) {} |
| 400 } | 226 } |
| 401 | 227 |
| 402 /** | 228 /** |
| 403 * Request status values returned by {@link #getStatus}. | 229 * Request status values returned by {@link #getStatus}. |
| 404 */ | 230 */ |
| 405 public static class Status { | 231 public static class Status { |
| 406 /** @hide */ | |
| 407 @IntDef({ | |
| 408 INVALID, IDLE, WAITING_FOR_STALLED_SOCKET_POOL, WAITING_FOR_AVAI LABLE_SOCKET, | |
| 409 WAITING_FOR_DELEGATE, WAITING_FOR_CACHE, DOWNLOADING_PROXY_SCRIP T, | |
| 410 RESOLVING_PROXY_FOR_URL, RESOLVING_HOST_IN_PROXY_SCRIPT, ESTABLI SHING_PROXY_TUNNEL, | |
| 411 RESOLVING_HOST, CONNECTING, SSL_HANDSHAKE, SENDING_REQUEST, WAIT ING_FOR_RESPONSE, | |
| 412 READING_RESPONSE, | |
| 413 }) | |
| 414 @Retention(RetentionPolicy.SOURCE) | |
| 415 public @interface StatusValues {} | |
|
pauljensen
2016/09/26 14:51:21
ditto?
| |
| 416 | 232 |
| 417 /** | 233 /** |
| 418 * This state indicates that the request is completed, canceled, or is n ot | 234 * This state indicates that the request is completed, canceled, or is n ot |
| 419 * started. | 235 * started. |
| 420 */ | 236 */ |
| 421 public static final int INVALID = -1; | 237 public static final int INVALID = -1; |
| 422 /** | 238 /** |
| 423 * This state corresponds to a resource load that has either not yet beg un | 239 * This state corresponds to a resource load that has either not yet beg un |
| 424 * or is idle waiting for the consumer to do something to move things al ong | 240 * or is idle waiting for the consumer to do something to move things al ong |
| 425 * (e.g. when the consumer of a {@link UrlRequest} has not called | 241 * (e.g. when the consumer of a {@link UrlRequest} has not called |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 509 /** | 325 /** |
| 510 * This state corresponds to a resource load that is blocked waiting for a | 326 * This state corresponds to a resource load that is blocked waiting for a |
| 511 * read to complete. In the case of a HTTP transaction, this corresponds to | 327 * read to complete. In the case of a HTTP transaction, this corresponds to |
| 512 * the period after the response headers have been received and before a ll | 328 * the period after the response headers have been received and before a ll |
| 513 * of the response body has been downloaded. (NOTE: This state only appl ies | 329 * of the response body has been downloaded. (NOTE: This state only appl ies |
| 514 * for an {@link UrlRequest} while there is an outstanding | 330 * for an {@link UrlRequest} while there is an outstanding |
| 515 * {@link UrlRequest#read read()} operation.) | 331 * {@link UrlRequest#read read()} operation.) |
| 516 */ | 332 */ |
| 517 public static final int READING_RESPONSE = 14; | 333 public static final int READING_RESPONSE = 14; |
| 518 | 334 |
| 519 private Status() {} | 335 protected Status() {} |
|
pauljensen
2016/09/26 14:51:21
why the transition to protected?
kapishnikov
2016/09/27 18:38:26
The status is defined in the public API (org.chrom
pauljensen
2016/09/27 19:08:09
Maybe just not extend Status in the impl.
kapishnikov
2016/09/28 00:20:49
Done.
| |
| 520 | |
| 521 /** | |
| 522 * Convert a LoadState int to one of values listed above. | |
| 523 * @param loadState a LoadState to convert. | |
| 524 * @return static int Status. | |
| 525 * @hide only used by internal implementation. | |
| 526 */ | |
| 527 @StatusValues | |
| 528 public static int convertLoadState(int loadState) { | |
| 529 assert loadState >= LoadState.IDLE && loadState <= LoadState.READING _RESPONSE; | |
| 530 switch (loadState) { | |
| 531 case (LoadState.IDLE): | |
| 532 return IDLE; | |
| 533 | |
| 534 case (LoadState.WAITING_FOR_STALLED_SOCKET_POOL): | |
| 535 return WAITING_FOR_STALLED_SOCKET_POOL; | |
| 536 | |
| 537 case (LoadState.WAITING_FOR_AVAILABLE_SOCKET): | |
| 538 return WAITING_FOR_AVAILABLE_SOCKET; | |
| 539 | |
| 540 case (LoadState.WAITING_FOR_DELEGATE): | |
| 541 return WAITING_FOR_DELEGATE; | |
| 542 | |
| 543 case (LoadState.WAITING_FOR_CACHE): | |
| 544 return WAITING_FOR_CACHE; | |
| 545 | |
| 546 case (LoadState.DOWNLOADING_PROXY_SCRIPT): | |
| 547 return DOWNLOADING_PROXY_SCRIPT; | |
| 548 | |
| 549 case (LoadState.RESOLVING_PROXY_FOR_URL): | |
| 550 return RESOLVING_PROXY_FOR_URL; | |
| 551 | |
| 552 case (LoadState.RESOLVING_HOST_IN_PROXY_SCRIPT): | |
| 553 return RESOLVING_HOST_IN_PROXY_SCRIPT; | |
| 554 | |
| 555 case (LoadState.ESTABLISHING_PROXY_TUNNEL): | |
| 556 return ESTABLISHING_PROXY_TUNNEL; | |
| 557 | |
| 558 case (LoadState.RESOLVING_HOST): | |
| 559 return RESOLVING_HOST; | |
| 560 | |
| 561 case (LoadState.CONNECTING): | |
| 562 return CONNECTING; | |
| 563 | |
| 564 case (LoadState.SSL_HANDSHAKE): | |
| 565 return SSL_HANDSHAKE; | |
| 566 | |
| 567 case (LoadState.SENDING_REQUEST): | |
| 568 return SENDING_REQUEST; | |
| 569 | |
| 570 case (LoadState.WAITING_FOR_RESPONSE): | |
| 571 return WAITING_FOR_RESPONSE; | |
| 572 | |
| 573 case (LoadState.READING_RESPONSE): | |
| 574 return READING_RESPONSE; | |
| 575 | |
| 576 default: | |
| 577 // A load state is retrieved but there is no corresponding | |
| 578 // request status. This most likely means that the mapping i s | |
| 579 // incorrect. | |
| 580 throw new IllegalArgumentException("No request status found. "); | |
| 581 } | |
| 582 } | |
| 583 } | 336 } |
| 584 | 337 |
| 585 /** | 338 /** |
| 586 * Listener class used with {@link #getStatus} to receive the status of a | 339 * Listener class used with {@link #getStatus} to receive the status of a |
| 587 * {@link UrlRequest}. | 340 * {@link UrlRequest}. |
| 588 */ | 341 */ |
| 589 public abstract class StatusListener { | 342 public abstract static class StatusListener { |
| 590 /** | 343 /** |
| 591 * Invoked on {@link UrlRequest}'s {@link Executor}'s thread when reques t | 344 * Invoked on {@link UrlRequest}'s {@link Executor}'s thread when reques t |
| 592 * status is obtained. | 345 * status is obtained. |
| 593 * @param status integer representing the status of the request. It is | 346 * @param status integer representing the status of the request. It is |
| 594 * one of the values defined in {@link Status}. | 347 * one of the values defined in {@link Status}. |
| 595 */ | 348 */ |
| 596 public abstract void onStatus(@Status.StatusValues int status); | 349 public abstract void onStatus(int status); |
| 597 } | 350 } |
| 598 | 351 |
| 599 /** | 352 /** |
| 600 * Sets the HTTP method verb to use for this request. Must be done before | |
| 601 * request has started. | |
| 602 * | |
| 603 * <p>The default when this method is not called is "GET" if the request has | |
| 604 * no body or "POST" if it does. | |
| 605 * | |
| 606 * @param method "GET", "HEAD", "DELETE", "POST" or "PUT". | |
| 607 * @deprecated Use {@link Builder#setHttpMethod}. | |
| 608 * @hide | |
| 609 */ | |
| 610 @Deprecated public void setHttpMethod(String method); | |
| 611 | |
| 612 /** | |
| 613 * Adds a request header. Must be done before request has started. | |
| 614 * | |
| 615 * @param header header name. | |
| 616 * @param value header value. | |
| 617 * @deprecated Use {@link Builder#setPriority}. | |
| 618 * @hide | |
| 619 */ | |
| 620 @Deprecated public void addHeader(String header, String value); | |
| 621 | |
| 622 /** | |
| 623 * Sets upload data provider. Must be done before request has started. May o nly be | |
| 624 * invoked once per request. Switches method to "POST" if not explicitly | |
| 625 * set. Starting the request will throw an exception if a Content-Type | |
| 626 * header is not set. | |
| 627 * | |
| 628 * @param uploadDataProvider responsible for providing the upload data. | |
| 629 * @param executor All {@code uploadDataProvider} methods will be invoked | |
| 630 * using this {@code Executor}. May optionally be the same | |
| 631 * {@code Executor} the request itself is using. | |
| 632 * @deprecated Use {@link Builder#setUploadDataProvider}. | |
| 633 * @hide | |
| 634 */ | |
| 635 @Deprecated | |
| 636 public void setUploadDataProvider(UploadDataProvider uploadDataProvider, Exe cutor executor); | |
| 637 | |
| 638 /** | |
| 639 * Starts the request, all callbacks go to {@link Callback}. May only be cal led | 353 * Starts the request, all callbacks go to {@link Callback}. May only be cal led |
| 640 * once. May not be called if {@link #cancel} has been called. | 354 * once. May not be called if {@link #cancel} has been called. |
| 641 */ | 355 */ |
| 642 public void start(); | 356 public abstract void start(); |
| 643 | 357 |
| 644 /** | 358 /** |
| 645 * Follows a pending redirect. Must only be called at most once for each | 359 * Follows a pending redirect. Must only be called at most once for each |
| 646 * invocation of {@link Callback#onRedirectReceived | 360 * invocation of {@link Callback#onRedirectReceived |
| 647 * onRedirectReceived()}. | 361 * onRedirectReceived()}. |
| 648 */ | 362 */ |
| 649 public void followRedirect(); | 363 public abstract void followRedirect(); |
| 650 | 364 |
| 651 /** | 365 /** |
| 652 * Attempts to read part of the response body into the provided buffer. | 366 * Attempts to read part of the response body into the provided buffer. |
| 653 * Must only be called at most once in response to each invocation of the | 367 * Must only be called at most once in response to each invocation of the |
| 654 * {@link Callback#onResponseStarted onResponseStarted()} and {@link | 368 * {@link Callback#onResponseStarted onResponseStarted()} and {@link |
| 655 * Callback#onReadCompleted onReadCompleted()} methods of the {@link | 369 * Callback#onReadCompleted onReadCompleted()} methods of the {@link |
| 656 * Callback}. Each call will result in an asynchronous call to | 370 * Callback}. Each call will result in an asynchronous call to |
| 657 * either the {@link Callback Callback's} | 371 * either the {@link Callback Callback's} |
| 658 * {@link Callback#onReadCompleted onReadCompleted()} method if data | 372 * {@link Callback#onReadCompleted onReadCompleted()} method if data |
| 659 * is read, its {@link Callback#onSucceeded onSucceeded()} method if | 373 * is read, its {@link Callback#onSucceeded onSucceeded()} method if |
| 660 * there's no more data to read, or its {@link Callback#onFailed | 374 * there's no more data to read, or its {@link Callback#onFailed |
| 661 * onFailed()} method if there's an error. | 375 * onFailed()} method if there's an error. |
| 662 * | 376 * |
| 663 * @param buffer {@link ByteBuffer} to write response body to. Must be a | 377 * @param buffer {@link ByteBuffer} to write response body to. Must be a |
| 664 * direct ByteBuffer. The embedder must not read or modify buffer's | 378 * direct ByteBuffer. The embedder must not read or modify buffer's |
| 665 * position, limit, or data between its position and limit until the | 379 * position, limit, or data between its position and limit until the |
| 666 * request calls back into the {@link Callback}. | 380 * request calls back into the {@link Callback}. |
| 667 */ | 381 */ |
| 668 public void read(ByteBuffer buffer); | 382 public abstract void read(ByteBuffer buffer); |
| 669 | 383 |
| 670 /** | 384 /** |
| 671 * Cancels the request. Can be called at any time. | 385 * Cancels the request. Can be called at any time. |
| 672 * {@link Callback#onCanceled onCanceled()} will be invoked when cancellatio n | 386 * {@link Callback#onCanceled onCanceled()} will be invoked when cancellatio n |
| 673 * is complete and no further callback methods will be invoked. If the | 387 * is complete and no further callback methods will be invoked. If the |
| 674 * request has completed or has not started, calling {@code cancel()} has no | 388 * request has completed or has not started, calling {@code cancel()} has no |
| 675 * effect and {@code onCanceled()} will not be invoked. If the | 389 * effect and {@code onCanceled()} will not be invoked. If the |
| 676 * {@link Executor} passed in during {@code UrlRequest} construction runs | 390 * {@link Executor} passed in during {@code UrlRequest} construction runs |
| 677 * tasks on a single thread, and {@code cancel()} is called on that thread, | 391 * tasks on a single thread, and {@code cancel()} is called on that thread, |
| 678 * no callback methods (besides {@code onCanceled()}) will be invoked after | 392 * no callback methods (besides {@code onCanceled()}) will be invoked after |
| 679 * {@code cancel()} is called. Otherwise, at most one callback method may be | 393 * {@code cancel()} is called. Otherwise, at most one callback method may be |
| 680 * invoked after {@code cancel()} has completed. | 394 * invoked after {@code cancel()} has completed. |
| 681 */ | 395 */ |
| 682 public void cancel(); | 396 public abstract void cancel(); |
| 683 | 397 |
| 684 /** | 398 /** |
| 685 * Returns {@code true} if the request was successfully started and is now | 399 * Returns {@code true} if the request was successfully started and is now |
| 686 * finished (completed, canceled, or failed). | 400 * finished (completed, canceled, or failed). |
| 687 * @return {@code true} if the request was successfully started and is now | 401 * @return {@code true} if the request was successfully started and is now |
| 688 * finished (completed, canceled, or failed). | 402 * finished (completed, canceled, or failed). |
| 689 */ | 403 */ |
| 690 public boolean isDone(); | 404 public abstract boolean isDone(); |
| 691 | 405 |
| 692 /** | 406 /** |
| 693 * Queries the status of the request. | 407 * Queries the status of the request. |
| 694 * @param listener a {@link StatusListener} that will be invoked with | 408 * @param listener a {@link StatusListener} that will be invoked with |
| 695 * the request's current status. {@code listener} will be invoked | 409 * the request's current status. {@code listener} will be invoked |
| 696 * back on the {@link Executor} passed in when the request was | 410 * back on the {@link Executor} passed in when the request was |
| 697 * created. | 411 * created. |
| 698 */ | 412 */ |
| 699 public void getStatus(final StatusListener listener); | 413 public abstract void getStatus(final StatusListener listener); |
| 700 | 414 |
| 701 // Note: There are deliberately no accessors for the results of the request | 415 // Note: There are deliberately no accessors for the results of the request |
| 702 // here. Having none removes any ambiguity over when they are populated, | 416 // here. Having none removes any ambiguity over when they are populated, |
| 703 // particularly in the redirect case. | 417 // particularly in the redirect case. |
| 704 } | 418 } |
| OLD | NEW |