Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: components/cronet/android/java/src/org/chromium/net/UrlRequest.java

Issue 1363723002: [Cronet] Create Builders, rename UrlRequestContext to CronetEngine (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: update sample Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 {
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 */
89 public void setHttpMethod(String method) {
mef 2015/10/02 16:02:27 should [set|add]Xyz methods return |this| to facil
pauljensen 2015/10/02 18:23:15 SGTM, done.
90 if (method == null) {
91 throw new NullPointerException("Method is required.");
92 }
93 mMethod = method;
94 }
95
96 /**
97 * Adds a request header.
98 *
99 * @param header header name.
100 * @param value header value.
101 */
102 public void addHeader(String header, String value) {
103 if (header == null) {
104 throw new NullPointerException("Invalid header name.");
105 }
106 if (value == null) {
107 throw new NullPointerException("Invalid header value.");
108 }
109 mRequestHeaders.add(Pair.create(header, value));
110 }
111
112 /**
113 * Disables cache for the request. If context is not set up to use cache ,
114 * this call has no effect.
115 */
116 public void disableCache() {
117 mDisableCache = true;
118 }
119
120 /**
121 * Lowest request priority. Passed to {@link #setPriority}.
122 */
123 public static final int REQUEST_PRIORITY_IDLE = 0;
124 /**
125 * Very low request priority. Passed to {@link #setPriority}.
126 */
127 public static final int REQUEST_PRIORITY_LOWEST = 1;
128 /**
129 * Low request priority. Passed to {@link #setPriority}.
130 */
131 public static final int REQUEST_PRIORITY_LOW = 2;
132 /**
133 * Medium request priority. Passed to {@link #setPriority}.
134 */
135 public static final int REQUEST_PRIORITY_MEDIUM = 3;
136 /**
137 * Highest request priority. Passed to {@link #setPriority}.
138 */
139 public static final int REQUEST_PRIORITY_HIGHEST = 4;
140
141 /**
142 * Sets priority of the request which should be one of the
143 * {@link #REQUEST_PRIORITY_IDLE REQUEST_PRIORITY_*} values.
144 * Defaults to {@link #REQUEST_PRIORITY_MEDIUM}
145 *
146 * @param priority priority of the request which should be one of the
147 * {@link #REQUEST_PRIORITY_IDLE REQUEST_PRIORITY_*} values.
148 */
149 public void setPriority(int priority) {
150 mPriority = priority;
151 }
152
153 /**
154 * Sets upload data provider. Switches method to "POST" if not
155 * explicitly set. Starting the request will throw an exception if a
156 * Content-Type header is not set.
157 *
158 * @param uploadDataProvider responsible for providing the upload data.
159 * @param executor All {@code uploadDataProvider} methods will be called
160 * using this {@code Executor}. May optionally be the same
161 * {@code Executor} the request itself is using.
162 */
163 public void setUploadDataProvider(
164 UploadDataProvider uploadDataProvider, Executor executor) {
165 if (uploadDataProvider == null) {
166 throw new NullPointerException("Invalid UploadDataProvider.");
167 }
168 if (executor == null) {
169 throw new NullPointerException("Invalid UploadDataProvider Execu tor.");
170 }
171 if (mMethod == null) {
172 mMethod = "POST";
173 }
174 mUploadDataProvider = uploadDataProvider;
175 mUploadDataProviderExecutor = executor;
176 }
177
178 /**
179 * Creates a {@link UrlRequest} using configuration within this
180 * {@link Builder}. The returned {@code UrlRequest} can then be started
181 * by calling {@link UrlRequest#start}.
182 *
183 * @return constructed {@link UrlRequest} using configuration within
184 * this {@link Builder}.
185 */
186 public UrlRequest build() {
187 final UrlRequest request =
188 mCronetEngine.createRequest(mUrl, mListener, mExecutor, mPri ority);
189 if (mMethod != null) {
190 request.setHttpMethod(mMethod);
191 }
192 if (mDisableCache) {
193 request.disableCache();
194 }
195 for (Pair<String, String> header : mRequestHeaders) {
196 request.addHeader(header.first, header.second);
197 }
198 if (mUploadDataProvider != null) {
199 request.setUploadDataProvider(mUploadDataProvider, mUploadDataPr oviderExecutor);
200 }
201 return request;
202 }
203 }
204
205 /**
17 * Request status values returned by {@link #getStatus}. 206 * Request status values returned by {@link #getStatus}.
18 */ 207 */
19 public static class Status { 208 public static class Status {
20 /** 209 /**
21 * This state indicates that the request is completed, canceled, or is n ot 210 * This state indicates that the request is completed, canceled, or is n ot
22 * started. 211 * started.
23 */ 212 */
24 public static final int INVALID = -1; 213 public static final int INVALID = -1;
25 /** 214 /**
26 * This state corresponds to a resource load that has either not yet beg un 215 * 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
189 /** 378 /**
190 * Called on {@link UrlRequest}'s {@link Executor}'s thread when request 379 * Called on {@link UrlRequest}'s {@link Executor}'s thread when request
191 * status is obtained. 380 * status is obtained.
192 * @param status integer representing the status of the request. It is 381 * @param status integer representing the status of the request. It is
193 * one of the values defined in {@link Status}. 382 * one of the values defined in {@link Status}.
194 */ 383 */
195 public abstract void onStatus(int status); 384 public abstract void onStatus(int status);
196 } 385 }
197 386
198 /** 387 /**
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 388 * Sets the HTTP method verb to use for this request. Must be done before
230 * request has started. 389 * request has started.
231 * 390 *
232 * <p>The default when this method is not called is "GET" if the request has 391 * <p>The default when this method is not called is "GET" if the request has
233 * no body or "POST" if it does. 392 * no body or "POST" if it does.
234 * 393 *
235 * @param method "GET", "HEAD", "DELETE", "POST" or "PUT". 394 * @param method "GET", "HEAD", "DELETE", "POST" or "PUT".
395 * @deprecated Use {@link Builder#setHttpMethod}.
236 */ 396 */
237 public void setHttpMethod(String method); 397 @Deprecated public void setHttpMethod(String method);
238 398
239 /** 399 /**
240 * Adds a request header. Must be done before request has started. 400 * Adds a request header. Must be done before request has started.
241 * 401 *
242 * @param header header name. 402 * @param header header name.
243 * @param value header value. 403 * @param value header value.
404 * @deprecated Use {@link Builder#setPriority}.
244 */ 405 */
245 public void addHeader(String header, String value); 406 @Deprecated public void addHeader(String header, String value);
246 407
247 /** 408 /**
248 * Sets upload data provider. Must be done before request has started. May o nly be 409 * 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 410 * invoked once per request. Switches method to "POST" if not explicitly
250 * set. Starting the request will throw an exception if a Content-Type 411 * set. Starting the request will throw an exception if a Content-Type
251 * header is not set. 412 * header is not set.
252 * 413 *
253 * @param uploadDataProvider responsible for providing the upload data. 414 * @param uploadDataProvider responsible for providing the upload data.
254 * @param executor All {@code uploadDataProvider} methods will be called 415 * @param executor All {@code uploadDataProvider} methods will be called
255 * using this {@code Executor}. May optionally be the same 416 * using this {@code Executor}. May optionally be the same
256 * {@code Executor} the request itself is using. 417 * {@code Executor} the request itself is using.
418 * @deprecated Use {@link Builder#setUploadDataProvider}.
257 */ 419 */
420 @Deprecated
258 public void setUploadDataProvider(UploadDataProvider uploadDataProvider, Exe cutor executor); 421 public void setUploadDataProvider(UploadDataProvider uploadDataProvider, Exe cutor executor);
259 422
260 /** 423 /**
261 * Starts the request, all callbacks go to listener. May only be called 424 * Starts the request, all callbacks go to listener. May only be called
262 * once. May not be called if {@link #cancel} has been called. 425 * once. May not be called if {@link #cancel} has been called.
263 */ 426 */
264 public void start(); 427 public void start();
265 428
266 /** 429 /**
267 * Follows a pending redirect. Must only be called at most once for each 430 * 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
339 * Returns {@code true} if the request was successfully started and is now 502 * Returns {@code true} if the request was successfully started and is now
340 * done (completed, canceled, or failed). 503 * done (completed, canceled, or failed).
341 * @return {@code true} if the request was successfully started and is now 504 * @return {@code true} if the request was successfully started and is now
342 * done (completed, canceled, or failed). 505 * done (completed, canceled, or failed).
343 */ 506 */
344 public boolean isDone(); 507 public boolean isDone();
345 508
346 /** 509 /**
347 * Disables cache for the request. If context is not set up to use cache, 510 * Disables cache for the request. If context is not set up to use cache,
348 * this call has no effect. 511 * this call has no effect.
512 * @deprecated Use {@link Builder#disableCache}.
349 */ 513 */
350 public void disableCache(); 514 @Deprecated public void disableCache();
351 515
352 /** 516 /**
353 * Queries the status of the request. 517 * Queries the status of the request.
354 * @param listener a {@link StatusListener} that will be called back with 518 * @param listener a {@link StatusListener} that will be called back with
355 * the request's current status. {@code listener} will be called 519 * the request's current status. {@code listener} will be called
356 * back on the {@link Executor} passed in when the request was 520 * back on the {@link Executor} passed in when the request was
357 * created. 521 * created.
358 */ 522 */
359 public void getStatus(final StatusListener listener); 523 public void getStatus(final StatusListener listener);
360 524
361 // Note: There are deliberately no accessors for the results of the request 525 // Note: There are deliberately no accessors for the results of the request
362 // here. Having none removes any ambiguity over when they are populated, 526 // here. Having none removes any ambiguity over when they are populated,
363 // particularly in the redirect case. 527 // particularly in the redirect case.
364 } 528 }
365 529
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698