| 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.util.Pair; |
| 8 |
| 7 import java.nio.ByteBuffer; | 9 import java.nio.ByteBuffer; |
| 10 import java.util.ArrayList; |
| 8 import java.util.concurrent.Executor; | 11 import java.util.concurrent.Executor; |
| 9 | 12 |
| 10 /** | 13 /** |
| 11 * Controls an HTTP request (GET, PUT, POST etc). | 14 * Controls an HTTP request (GET, PUT, POST etc). |
| 12 * Created using {@link UrlRequestContext#createRequest UrlRequestContext.create
Request()}. | 15 * Created using {@link UrlRequest.Builder}. |
| 13 * Note: All methods must be called on the {@link Executor} passed in during cre
ation. | 16 * Note: All methods must be called on the {@link Executor} passed in during cre
ation. |
| 14 */ | 17 */ |
| 15 public interface UrlRequest { | 18 public interface UrlRequest { |
| 16 /** | 19 /** |
| 20 * Builder for {@link UrlRequest}s. Allows configuring requests before const
ructing them |
| 21 * with {@link Builder#build}. |
| 22 */ |
| 23 public static final class Builder { |
| 24 // All fields are temporary storage of UrlRequest configuration to be |
| 25 // copied to built UrlRequests. |
| 26 |
| 27 // CronetEngine to execute request. |
| 28 final CronetEngine mCronetEngine; |
| 29 // URL to request. |
| 30 final String mUrl; |
| 31 // Listener to receive progress callbacks. |
| 32 final UrlRequestListener mListener; |
| 33 // Executor to call listener on. |
| 34 final Executor mExecutor; |
| 35 // HTTP method (e.g. GET, POST etc). |
| 36 String mMethod; |
| 37 // List of request headers, stored as header field name and value pairs. |
| 38 final ArrayList<Pair<String, String>> mRequestHeaders = |
| 39 new ArrayList<Pair<String, String>>(); |
| 40 // Disable the cache for just this request. |
| 41 boolean mDisableCache; |
| 42 // Priority of request. |
| 43 int mPriority = REQUEST_PRIORITY_MEDIUM; |
| 44 // If request is an upload, this provides the request body data. |
| 45 UploadDataProvider mUploadDataProvider; |
| 46 // Executor to call upload data provider back on. |
| 47 Executor mUploadDataProviderExecutor; |
| 48 |
| 49 /** |
| 50 * Creates a builder for {@link UrlRequest} objects. All callbacks for |
| 51 * generated {@link UrlRequest} objects will be called on |
| 52 * {@code executor}'s thread. {@code executor} must not run tasks on the |
| 53 * current thread to prevent blocking networking operations and causing |
| 54 * exceptions during shutdown. |
| 55 * |
| 56 * @param url {@link java.net.URL} for the generated requests. |
| 57 * @param listener callback class that gets called on different events. |
| 58 * @param executor {@link Executor} on which all callbacks will be calle
d. |
| 59 * @param cronetEngine {@link CronetEngine} used to execute this request
. |
| 60 */ |
| 61 public Builder(String url, UrlRequestListener listener, Executor executo
r, |
| 62 CronetEngine cronetEngine) { |
| 63 if (url == null) { |
| 64 throw new NullPointerException("URL is required."); |
| 65 } |
| 66 if (listener == null) { |
| 67 throw new NullPointerException("Listener is required."); |
| 68 } |
| 69 if (executor == null) { |
| 70 throw new NullPointerException("Executor is required."); |
| 71 } |
| 72 if (cronetEngine == null) { |
| 73 throw new NullPointerException("CronetEngine is required."); |
| 74 } |
| 75 mUrl = url; |
| 76 mListener = listener; |
| 77 mExecutor = executor; |
| 78 mCronetEngine = cronetEngine; |
| 79 } |
| 80 |
| 81 /** |
| 82 * Sets the HTTP method verb to use for this request. |
| 83 * |
| 84 * <p>The default when this method is not called is "GET" if the request
has |
| 85 * no body or "POST" if it does. |
| 86 * |
| 87 * @param method "GET", "HEAD", "DELETE", "POST" or "PUT". |
| 88 * @return the builder to facilitate chaining. |
| 89 */ |
| 90 public Builder setHttpMethod(String method) { |
| 91 if (method == null) { |
| 92 throw new NullPointerException("Method is required."); |
| 93 } |
| 94 mMethod = method; |
| 95 return this; |
| 96 } |
| 97 |
| 98 /** |
| 99 * Adds a request header. |
| 100 * |
| 101 * @param header header name. |
| 102 * @param value header value. |
| 103 * @return the builder to facilitate chaining. |
| 104 */ |
| 105 public Builder addHeader(String header, String value) { |
| 106 if (header == null) { |
| 107 throw new NullPointerException("Invalid header name."); |
| 108 } |
| 109 if (value == null) { |
| 110 throw new NullPointerException("Invalid header value."); |
| 111 } |
| 112 mRequestHeaders.add(Pair.create(header, value)); |
| 113 return this; |
| 114 } |
| 115 |
| 116 /** |
| 117 * Disables cache for the request. If context is not set up to use cache
, |
| 118 * this call has no effect. |
| 119 * @return the builder to facilitate chaining. |
| 120 */ |
| 121 public Builder disableCache() { |
| 122 mDisableCache = true; |
| 123 return this; |
| 124 } |
| 125 |
| 126 /** |
| 127 * Lowest request priority. Passed to {@link #setPriority}. |
| 128 */ |
| 129 public static final int REQUEST_PRIORITY_IDLE = 0; |
| 130 /** |
| 131 * Very low request priority. Passed to {@link #setPriority}. |
| 132 */ |
| 133 public static final int REQUEST_PRIORITY_LOWEST = 1; |
| 134 /** |
| 135 * Low request priority. Passed to {@link #setPriority}. |
| 136 */ |
| 137 public static final int REQUEST_PRIORITY_LOW = 2; |
| 138 /** |
| 139 * Medium request priority. Passed to {@link #setPriority}. |
| 140 */ |
| 141 public static final int REQUEST_PRIORITY_MEDIUM = 3; |
| 142 /** |
| 143 * Highest request priority. Passed to {@link #setPriority}. |
| 144 */ |
| 145 public static final int REQUEST_PRIORITY_HIGHEST = 4; |
| 146 |
| 147 /** |
| 148 * Sets priority of the request which should be one of the |
| 149 * {@link #REQUEST_PRIORITY_IDLE REQUEST_PRIORITY_*} values. |
| 150 * Defaults to {@link #REQUEST_PRIORITY_MEDIUM} |
| 151 * |
| 152 * @param priority priority of the request which should be one of the |
| 153 * {@link #REQUEST_PRIORITY_IDLE REQUEST_PRIORITY_*} values. |
| 154 * @return the builder to facilitate chaining. |
| 155 */ |
| 156 public Builder setPriority(int priority) { |
| 157 mPriority = priority; |
| 158 return this; |
| 159 } |
| 160 |
| 161 /** |
| 162 * Sets upload data provider. Switches method to "POST" if not |
| 163 * explicitly set. Starting the request will throw an exception if a |
| 164 * Content-Type header is not set. |
| 165 * |
| 166 * @param uploadDataProvider responsible for providing the upload data. |
| 167 * @param executor All {@code uploadDataProvider} methods will be called |
| 168 * using this {@code Executor}. May optionally be the same |
| 169 * {@code Executor} the request itself is using. |
| 170 * @return the builder to facilitate chaining. |
| 171 */ |
| 172 public Builder setUploadDataProvider( |
| 173 UploadDataProvider uploadDataProvider, Executor executor) { |
| 174 if (uploadDataProvider == null) { |
| 175 throw new NullPointerException("Invalid UploadDataProvider."); |
| 176 } |
| 177 if (executor == null) { |
| 178 throw new NullPointerException("Invalid UploadDataProvider Execu
tor."); |
| 179 } |
| 180 if (mMethod == null) { |
| 181 mMethod = "POST"; |
| 182 } |
| 183 mUploadDataProvider = uploadDataProvider; |
| 184 mUploadDataProviderExecutor = executor; |
| 185 return this; |
| 186 } |
| 187 |
| 188 /** |
| 189 * Creates a {@link UrlRequest} using configuration within this |
| 190 * {@link Builder}. The returned {@code UrlRequest} can then be started |
| 191 * by calling {@link UrlRequest#start}. |
| 192 * |
| 193 * @return constructed {@link UrlRequest} using configuration within |
| 194 * this {@link Builder}. |
| 195 */ |
| 196 public UrlRequest build() { |
| 197 final UrlRequest request = |
| 198 mCronetEngine.createRequest(mUrl, mListener, mExecutor, mPri
ority); |
| 199 if (mMethod != null) { |
| 200 request.setHttpMethod(mMethod); |
| 201 } |
| 202 if (mDisableCache) { |
| 203 request.disableCache(); |
| 204 } |
| 205 for (Pair<String, String> header : mRequestHeaders) { |
| 206 request.addHeader(header.first, header.second); |
| 207 } |
| 208 if (mUploadDataProvider != null) { |
| 209 request.setUploadDataProvider(mUploadDataProvider, mUploadDataPr
oviderExecutor); |
| 210 } |
| 211 return request; |
| 212 } |
| 213 } |
| 214 |
| 215 /** |
| 17 * Request status values returned by {@link #getStatus}. | 216 * Request status values returned by {@link #getStatus}. |
| 18 */ | 217 */ |
| 19 public static class Status { | 218 public static class Status { |
| 20 /** | 219 /** |
| 21 * This state indicates that the request is completed, canceled, or is n
ot | 220 * This state indicates that the request is completed, canceled, or is n
ot |
| 22 * started. | 221 * started. |
| 23 */ | 222 */ |
| 24 public static final int INVALID = -1; | 223 public static final int INVALID = -1; |
| 25 /** | 224 /** |
| 26 * This state corresponds to a resource load that has either not yet beg
un | 225 * This state corresponds to a resource load that has either not yet beg
un |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 /** | 388 /** |
| 190 * Called on {@link UrlRequest}'s {@link Executor}'s thread when request | 389 * Called on {@link UrlRequest}'s {@link Executor}'s thread when request |
| 191 * status is obtained. | 390 * status is obtained. |
| 192 * @param status integer representing the status of the request. It is | 391 * @param status integer representing the status of the request. It is |
| 193 * one of the values defined in {@link Status}. | 392 * one of the values defined in {@link Status}. |
| 194 */ | 393 */ |
| 195 public abstract void onStatus(int status); | 394 public abstract void onStatus(int status); |
| 196 } | 395 } |
| 197 | 396 |
| 198 /** | 397 /** |
| 199 * Lowest request priority. Passed to {@link UrlRequestContext#createRequest
(String, | |
| 200 * UrlRequestListener, Executor, int) UrlRequestContext.createRequest()}. | |
| 201 */ | |
| 202 public static final int REQUEST_PRIORITY_IDLE = 0; | |
| 203 /** | |
| 204 * Very low request priority. Passed to {@link UrlRequestContext#createReque
st(String, | |
| 205 * UrlRequestListener, Executor, int) UrlRequestContext.createRequest()}. | |
| 206 */ | |
| 207 public static final int REQUEST_PRIORITY_LOWEST = 1; | |
| 208 /** | |
| 209 * Low request priority. Passed to {@link UrlRequestContext#createRequest(St
ring, | |
| 210 * UrlRequestListener, Executor, int) UrlRequestContext.createRequest()}. | |
| 211 */ | |
| 212 public static final int REQUEST_PRIORITY_LOW = 2; | |
| 213 /** | |
| 214 * Medium request priority. Passed to {@link UrlRequestContext#createRequest
(String, | |
| 215 * UrlRequestListener, Executor, int) UrlRequestContext.createRequest()}. | |
| 216 */ | |
| 217 public static final int REQUEST_PRIORITY_MEDIUM = 3; | |
| 218 /** | |
| 219 * Highest request priority. Passed to {@link UrlRequestContext#createReques
t(String, | |
| 220 * UrlRequestListener, Executor, int) UrlRequestContext.createRequest()}. | |
| 221 */ | |
| 222 public static final int REQUEST_PRIORITY_HIGHEST = 4; | |
| 223 | |
| 224 // More setters go here. They may only be called before start (Maybe | |
| 225 // also allow during redirects). Could optionally instead use arguments | |
| 226 // to URLRequestFactory when creating the request. | |
| 227 | |
| 228 /** | |
| 229 * Sets the HTTP method verb to use for this request. Must be done before | 398 * Sets the HTTP method verb to use for this request. Must be done before |
| 230 * request has started. | 399 * request has started. |
| 231 * | 400 * |
| 232 * <p>The default when this method is not called is "GET" if the request has | 401 * <p>The default when this method is not called is "GET" if the request has |
| 233 * no body or "POST" if it does. | 402 * no body or "POST" if it does. |
| 234 * | 403 * |
| 235 * @param method "GET", "HEAD", "DELETE", "POST" or "PUT". | 404 * @param method "GET", "HEAD", "DELETE", "POST" or "PUT". |
| 405 * @deprecated Use {@link Builder#setHttpMethod}. |
| 236 */ | 406 */ |
| 237 public void setHttpMethod(String method); | 407 @Deprecated public void setHttpMethod(String method); |
| 238 | 408 |
| 239 /** | 409 /** |
| 240 * Adds a request header. Must be done before request has started. | 410 * Adds a request header. Must be done before request has started. |
| 241 * | 411 * |
| 242 * @param header header name. | 412 * @param header header name. |
| 243 * @param value header value. | 413 * @param value header value. |
| 414 * @deprecated Use {@link Builder#setPriority}. |
| 244 */ | 415 */ |
| 245 public void addHeader(String header, String value); | 416 @Deprecated public void addHeader(String header, String value); |
| 246 | 417 |
| 247 /** | 418 /** |
| 248 * Sets upload data provider. Must be done before request has started. May o
nly be | 419 * Sets upload data provider. Must be done before request has started. May o
nly be |
| 249 * invoked once per request. Switches method to "POST" if not explicitly | 420 * invoked once per request. Switches method to "POST" if not explicitly |
| 250 * set. Starting the request will throw an exception if a Content-Type | 421 * set. Starting the request will throw an exception if a Content-Type |
| 251 * header is not set. | 422 * header is not set. |
| 252 * | 423 * |
| 253 * @param uploadDataProvider responsible for providing the upload data. | 424 * @param uploadDataProvider responsible for providing the upload data. |
| 254 * @param executor All {@code uploadDataProvider} methods will be called | 425 * @param executor All {@code uploadDataProvider} methods will be called |
| 255 * using this {@code Executor}. May optionally be the same | 426 * using this {@code Executor}. May optionally be the same |
| 256 * {@code Executor} the request itself is using. | 427 * {@code Executor} the request itself is using. |
| 428 * @deprecated Use {@link Builder#setUploadDataProvider}. |
| 257 */ | 429 */ |
| 430 @Deprecated |
| 258 public void setUploadDataProvider(UploadDataProvider uploadDataProvider, Exe
cutor executor); | 431 public void setUploadDataProvider(UploadDataProvider uploadDataProvider, Exe
cutor executor); |
| 259 | 432 |
| 260 /** | 433 /** |
| 261 * Starts the request, all callbacks go to listener. May only be called | 434 * Starts the request, all callbacks go to listener. May only be called |
| 262 * once. May not be called if {@link #cancel} has been called. | 435 * once. May not be called if {@link #cancel} has been called. |
| 263 */ | 436 */ |
| 264 public void start(); | 437 public void start(); |
| 265 | 438 |
| 266 /** | 439 /** |
| 267 * Follows a pending redirect. Must only be called at most once for each | 440 * Follows a pending redirect. Must only be called at most once for each |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 339 * Returns {@code true} if the request was successfully started and is now | 512 * Returns {@code true} if the request was successfully started and is now |
| 340 * done (completed, canceled, or failed). | 513 * done (completed, canceled, or failed). |
| 341 * @return {@code true} if the request was successfully started and is now | 514 * @return {@code true} if the request was successfully started and is now |
| 342 * done (completed, canceled, or failed). | 515 * done (completed, canceled, or failed). |
| 343 */ | 516 */ |
| 344 public boolean isDone(); | 517 public boolean isDone(); |
| 345 | 518 |
| 346 /** | 519 /** |
| 347 * Disables cache for the request. If context is not set up to use cache, | 520 * Disables cache for the request. If context is not set up to use cache, |
| 348 * this call has no effect. | 521 * this call has no effect. |
| 522 * @deprecated Use {@link Builder#disableCache}. |
| 349 */ | 523 */ |
| 350 public void disableCache(); | 524 @Deprecated public void disableCache(); |
| 351 | 525 |
| 352 /** | 526 /** |
| 353 * Queries the status of the request. | 527 * Queries the status of the request. |
| 354 * @param listener a {@link StatusListener} that will be called back with | 528 * @param listener a {@link StatusListener} that will be called back with |
| 355 * the request's current status. {@code listener} will be called | 529 * the request's current status. {@code listener} will be called |
| 356 * back on the {@link Executor} passed in when the request was | 530 * back on the {@link Executor} passed in when the request was |
| 357 * created. | 531 * created. |
| 358 */ | 532 */ |
| 359 public void getStatus(final StatusListener listener); | 533 public void getStatus(final StatusListener listener); |
| 360 | 534 |
| 361 // Note: There are deliberately no accessors for the results of the request | 535 // Note: There are deliberately no accessors for the results of the request |
| 362 // here. Having none removes any ambiguity over when they are populated, | 536 // here. Having none removes any ambiguity over when they are populated, |
| 363 // particularly in the redirect case. | 537 // particularly in the redirect case. |
| 364 } | 538 } |
| 365 | 539 |
| OLD | NEW |