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.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 CronetEngine#executeRequest CronetEngine.executeRequest( )}. |
| 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 { |
|
mef
2015/09/25 21:32:19
should this be an abstract class?
pauljensen
2015/09/28 14:18:12
Perhaps, but let's leave that for another CL.
| |
| 16 /** | 19 /** |
| 20 * Builder for {@link UrlRequest}s. Allows configuring requests before start ing | |
| 21 * them via {@link CronetEngine#executeRequest}. | |
| 22 */ | |
| 23 public static class Builder { | |
|
mef
2015/09/25 21:32:19
should it also be final?
pauljensen
2015/09/28 14:18:12
Done.
| |
| 24 // All fields are temporary storage of UrlRequest configuration to be | |
| 25 // copied to built UrlRequests. | |
| 26 | |
| 27 // URL to request. | |
| 28 final String mUrl; | |
| 29 // Listener to receive progress callbacks. | |
| 30 final UrlRequestListener mListener; | |
| 31 // Executor to call listener on. | |
| 32 final Executor mExecutor; | |
| 33 // HTTP method (e.g. GET, POST etc). | |
| 34 String mMethod; | |
| 35 // List of request headers, stored as header field name and value pairs. | |
| 36 final ArrayList<Pair<String, String>> mRequestHeaders = | |
|
mef
2015/09/25 21:32:19
I think java people have prefer Map.Entry<String,
pauljensen
2015/09/28 14:18:12
From the API review feedback, I think that is only
| |
| 37 new ArrayList<Pair<String, String>>(); | |
| 38 // Disable the cache for just this request. | |
| 39 boolean mDisableCache; | |
| 40 // Priority of request. | |
| 41 int mPriority = REQUEST_PRIORITY_MEDIUM; | |
| 42 // If request is an upload, this provides the request body data. | |
| 43 UploadDataProvider mUploadDataProvider; | |
| 44 // Executor to call upload data provider back on. | |
| 45 Executor mUploadDataProviderExecutor; | |
| 46 | |
| 47 /** | |
| 48 * Creates a builder for {@link UrlRequest} objects. All callbacks for | |
| 49 * generated {@link UrlRequest} objects will be called on | |
| 50 * {@code executor}'s thread. {@code executor} must not run tasks on the | |
| 51 * current thread to prevent blocking networking operations and causing | |
| 52 * exceptions during shutdown. | |
| 53 * | |
| 54 * @param url {@link java.net.URL} for the generated requests. | |
| 55 * @param listener callback class that gets called on different events. | |
| 56 * @param executor {@link Executor} on which all callbacks will be calle d. | |
| 57 */ | |
| 58 public Builder(String url, UrlRequestListener listener, Executor executo r) { | |
| 59 mUrl = url; | |
| 60 mListener = listener; | |
| 61 mExecutor = executor; | |
| 62 } | |
| 63 | |
| 64 /** | |
| 65 * Sets the HTTP method verb to use for this request. | |
| 66 * | |
| 67 * <p>The default when this method is not called is "GET" if the request has | |
| 68 * no body or "POST" if it does. | |
| 69 * | |
| 70 * @param method "GET", "HEAD", "DELETE", "POST" or "PUT". | |
| 71 */ | |
| 72 public void setHttpMethod(String method) { | |
| 73 if (method == null) { | |
| 74 throw new NullPointerException("Method is required."); | |
| 75 } | |
| 76 mMethod = method; | |
| 77 } | |
| 78 | |
| 79 /** | |
| 80 * Adds a request header. | |
| 81 * | |
| 82 * @param header header name. | |
| 83 * @param value header value. | |
| 84 */ | |
| 85 public void addHeader(String header, String value) { | |
| 86 if (header == null) { | |
| 87 throw new NullPointerException("Invalid header name."); | |
| 88 } | |
| 89 if (value == null) { | |
| 90 throw new NullPointerException("Invalid header value."); | |
| 91 } | |
| 92 mRequestHeaders.add(Pair.create(header, value)); | |
| 93 } | |
| 94 | |
| 95 /** | |
| 96 * Disables cache for the request. If context is not set up to use cache , | |
| 97 * this call has no effect. | |
| 98 */ | |
| 99 public void disableCache() { | |
| 100 mDisableCache = true; | |
| 101 } | |
| 102 | |
| 103 /** | |
| 104 * Lowest request priority. Passed to {@link #setPriority}. | |
| 105 */ | |
| 106 public static final int REQUEST_PRIORITY_IDLE = 0; | |
| 107 /** | |
| 108 * Very low request priority. Passed to {@link #setPriority}. | |
| 109 */ | |
| 110 public static final int REQUEST_PRIORITY_LOWEST = 1; | |
| 111 /** | |
| 112 * Low request priority. Passed to {@link #setPriority}. | |
| 113 */ | |
| 114 public static final int REQUEST_PRIORITY_LOW = 2; | |
| 115 /** | |
| 116 * Medium request priority. Passed to {@link #setPriority}. | |
| 117 */ | |
| 118 public static final int REQUEST_PRIORITY_MEDIUM = 3; | |
| 119 /** | |
| 120 * Highest request priority. Passed to {@link #setPriority}. | |
| 121 */ | |
| 122 public static final int REQUEST_PRIORITY_HIGHEST = 4; | |
| 123 | |
| 124 /** | |
| 125 * Sets priority of the request which should be one of the | |
| 126 * {@link #REQUEST_PRIORITY_IDLE REQUEST_PRIORITY_*} values. | |
| 127 * Defaults to {@link #REQUEST_PRIORITY_MEDIUM} | |
| 128 * | |
| 129 * @param priority priority of the request which should be one of the | |
| 130 * {@link #REQUEST_PRIORITY_IDLE REQUEST_PRIORITY_*} values. | |
| 131 */ | |
| 132 public void setPriority(int priority) { | |
| 133 mPriority = priority; | |
| 134 } | |
| 135 | |
| 136 /** | |
| 137 * Sets upload data provider. Switches method to "POST" if not | |
| 138 * explicitly set. Starting the request will throw an exception if a | |
| 139 * Content-Type header is not set. | |
| 140 * | |
| 141 * @param uploadDataProvider responsible for providing the upload data. | |
| 142 * @param executor All {@code uploadDataProvider} methods will be called | |
| 143 * using this {@code Executor}. May optionally be the same | |
| 144 * {@code Executor} the request itself is using. | |
| 145 */ | |
| 146 public void setUploadDataProvider( | |
| 147 UploadDataProvider uploadDataProvider, Executor executor) { | |
| 148 if (uploadDataProvider == null) { | |
| 149 throw new NullPointerException("Invalid UploadDataProvider."); | |
| 150 } | |
| 151 if (executor == null) { | |
| 152 throw new NullPointerException("Invalid UploadDataProvider Execu tor."); | |
| 153 } | |
| 154 if (mMethod == null) { | |
| 155 mMethod = "POST"; | |
| 156 } | |
| 157 mUploadDataProvider = uploadDataProvider; | |
| 158 mUploadDataProviderExecutor = executor; | |
| 159 } | |
| 160 } | |
| 161 | |
| 162 /** | |
| 17 * Request status values returned by {@link #getStatus}. | 163 * Request status values returned by {@link #getStatus}. |
| 18 */ | 164 */ |
| 19 public static class Status { | 165 public static class Status { |
| 20 /** | 166 /** |
| 21 * This state indicates that the request is completed, canceled, or is n ot | 167 * This state indicates that the request is completed, canceled, or is n ot |
| 22 * started. | 168 * started. |
| 23 */ | 169 */ |
| 24 public static final int INVALID = -1; | 170 public static final int INVALID = -1; |
| 25 /** | 171 /** |
| 26 * This state corresponds to a resource load that has either not yet beg un | 172 * 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 /** | 335 /** |
| 190 * Called on {@link UrlRequest}'s {@link Executor}'s thread when request | 336 * Called on {@link UrlRequest}'s {@link Executor}'s thread when request |
| 191 * status is obtained. | 337 * status is obtained. |
| 192 * @param status integer representing the status of the request. It is | 338 * @param status integer representing the status of the request. It is |
| 193 * one of the values defined in {@link Status}. | 339 * one of the values defined in {@link Status}. |
| 194 */ | 340 */ |
| 195 public abstract void onStatus(int status); | 341 public abstract void onStatus(int status); |
| 196 } | 342 } |
| 197 | 343 |
| 198 /** | 344 /** |
| 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 | 345 * Sets the HTTP method verb to use for this request. Must be done before |
| 230 * request has started. | 346 * request has started. |
| 231 * | 347 * |
| 232 * <p>The default when this method is not called is "GET" if the request has | 348 * <p>The default when this method is not called is "GET" if the request has |
| 233 * no body or "POST" if it does. | 349 * no body or "POST" if it does. |
| 234 * | 350 * |
| 235 * @param method "GET", "HEAD", "DELETE", "POST" or "PUT". | 351 * @param method "GET", "HEAD", "DELETE", "POST" or "PUT". |
| 352 * @deprecated Use {@link Builder#setHttpMethod}. | |
| 236 */ | 353 */ |
| 237 public void setHttpMethod(String method); | 354 @Deprecated public void setHttpMethod(String method); |
| 238 | 355 |
| 239 /** | 356 /** |
| 240 * Adds a request header. Must be done before request has started. | 357 * Adds a request header. Must be done before request has started. |
| 241 * | 358 * |
| 242 * @param header header name. | 359 * @param header header name. |
| 243 * @param value header value. | 360 * @param value header value. |
| 361 * @deprecated Use {@link Builder#setPriority}. | |
| 244 */ | 362 */ |
| 245 public void addHeader(String header, String value); | 363 @Deprecated public void addHeader(String header, String value); |
| 246 | 364 |
| 247 /** | 365 /** |
| 248 * Sets upload data provider. Must be done before request has started. May o nly be | 366 * 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 | 367 * invoked once per request. Switches method to "POST" if not explicitly |
| 250 * set. Starting the request will throw an exception if a Content-Type | 368 * set. Starting the request will throw an exception if a Content-Type |
| 251 * header is not set. | 369 * header is not set. |
| 252 * | 370 * |
| 253 * @param uploadDataProvider responsible for providing the upload data. | 371 * @param uploadDataProvider responsible for providing the upload data. |
| 254 * @param executor All {@code uploadDataProvider} methods will be called | 372 * @param executor All {@code uploadDataProvider} methods will be called |
| 255 * using this {@code Executor}. May optionally be the same | 373 * using this {@code Executor}. May optionally be the same |
| 256 * {@code Executor} the request itself is using. | 374 * {@code Executor} the request itself is using. |
| 375 * @deprecated Use {@link Builder#setUploadDataProvider}. | |
| 257 */ | 376 */ |
| 377 @Deprecated | |
| 258 public void setUploadDataProvider(UploadDataProvider uploadDataProvider, Exe cutor executor); | 378 public void setUploadDataProvider(UploadDataProvider uploadDataProvider, Exe cutor executor); |
| 259 | 379 |
| 260 /** | 380 /** |
| 261 * Starts the request, all callbacks go to listener. May only be called | 381 * Starts the request, all callbacks go to listener. May only be called |
| 262 * once. May not be called if {@link #cancel} has been called. | 382 * once. May not be called if {@link #cancel} has been called. |
| 383 * @deprecated Use {@link CronetEngine#executeRequest}. | |
| 263 */ | 384 */ |
| 264 public void start(); | 385 @Deprecated public void start(); |
| 265 | 386 |
| 266 /** | 387 /** |
| 267 * Follows a pending redirect. Must only be called at most once for each | 388 * Follows a pending redirect. Must only be called at most once for each |
| 268 * invocation of {@link UrlRequestListener#onReceivedRedirect | 389 * invocation of {@link UrlRequestListener#onReceivedRedirect |
| 269 * UrlRequestListener.onReceivedRedirect()}. | 390 * UrlRequestListener.onReceivedRedirect()}. |
| 270 */ | 391 */ |
| 271 public void followRedirect(); | 392 public void followRedirect(); |
| 272 | 393 |
| 273 /** | 394 /** |
| 274 * Attempts to read part of the response body into the provided buffer. | 395 * Attempts to read part of the response body into the provided buffer. |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 339 * Returns {@code true} if the request was successfully started and is now | 460 * Returns {@code true} if the request was successfully started and is now |
| 340 * done (completed, canceled, or failed). | 461 * done (completed, canceled, or failed). |
| 341 * @return {@code true} if the request was successfully started and is now | 462 * @return {@code true} if the request was successfully started and is now |
| 342 * done (completed, canceled, or failed). | 463 * done (completed, canceled, or failed). |
| 343 */ | 464 */ |
| 344 public boolean isDone(); | 465 public boolean isDone(); |
| 345 | 466 |
| 346 /** | 467 /** |
| 347 * Disables cache for the request. If context is not set up to use cache, | 468 * Disables cache for the request. If context is not set up to use cache, |
| 348 * this call has no effect. | 469 * this call has no effect. |
| 470 * @deprecated Use {@link Builder#disableCache}. | |
| 349 */ | 471 */ |
| 350 public void disableCache(); | 472 @Deprecated public void disableCache(); |
| 351 | 473 |
| 352 /** | 474 /** |
| 353 * Queries the status of the request. | 475 * Queries the status of the request. |
| 354 * @param listener a {@link StatusListener} that will be called back with | 476 * @param listener a {@link StatusListener} that will be called back with |
| 355 * the request's current status. {@code listener} will be called | 477 * the request's current status. {@code listener} will be called |
| 356 * back on the {@link Executor} passed in when the request was | 478 * back on the {@link Executor} passed in when the request was |
| 357 * created. | 479 * created. |
| 358 */ | 480 */ |
| 359 public void getStatus(final StatusListener listener); | 481 public void getStatus(final StatusListener listener); |
| 360 | 482 |
| 361 // Note: There are deliberately no accessors for the results of the request | 483 // Note: There are deliberately no accessors for the results of the request |
| 362 // here. Having none removes any ambiguity over when they are populated, | 484 // here. Having none removes any ambiguity over when they are populated, |
| 363 // particularly in the redirect case. | 485 // particularly in the redirect case. |
| 364 } | 486 } |
| 365 | 487 |
| OLD | NEW |