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

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

Issue 2339223002: Cronet API Refactoring (Closed)
Patch Set: Javadoc + rebase Created 4 years, 3 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
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
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 android.util.Pair;
7
8 import org.chromium.base.Log;
9 import org.chromium.net.CronetEngine;
10 import org.chromium.net.ExperimentalUrlRequest;
11 import org.chromium.net.UploadDataProvider;
12 import org.chromium.net.UrlRequest;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.concurrent.Executor;
18
19 /**
20 * Implements {@link org.chromium.net.UrlRequest.Builder} and all experimental f eatures.
21 */
22 public class UrlRequestBuilderImpl extends ExperimentalUrlRequest.Builder {
23 private static final String ACCEPT_ENCODING = "Accept-Encoding";
24 private static final String TAG = "UrlRequestBuilder";
25
26 // All fields are temporary storage of UrlRequest configuration to be
27 // copied to built UrlRequests.
28
29 // CronetEngine to execute request.
30 private final CronetEngineBase mCronetEngine;
31 // URL to request.
32 private final String mUrl;
33 // Callback to receive progress callbacks.
34 private final UrlRequest.Callback mCallback;
35 // Executor to invoke callback on.
36 private final Executor mExecutor;
37 // HTTP method (e.g. GET, POST etc).
38 private String mMethod;
39
40 // List of request headers, stored as header field name and value pairs.
41 private final ArrayList<Pair<String, String>> mRequestHeaders = new ArrayLis t<>();
42 // Disable the cache for just this request.
43 private boolean mDisableCache;
44 // Disable connection migration for just this request.
45 private boolean mDisableConnectionMigration;
46 // Priority of request. Default is medium.
47 @CronetEngineBase.RequestPriority
48 private int mPriority = REQUEST_PRIORITY_MEDIUM;
49 // Request reporting annotations. Avoid extra object creation if no annotati ons added.
50 private Collection<Object> mRequestAnnotations = Collections.emptyList();
51 // If request is an upload, this provides the request body data.
52 private UploadDataProvider mUploadDataProvider;
53 // Executor to call upload data provider back on.
54 private Executor mUploadDataProviderExecutor;
55 private boolean mAllowDirectExecutor = false;
56
57 /**
58 * Creates a builder for {@link UrlRequest} objects. All callbacks for
59 * generated {@link UrlRequest} objects will be invoked on
60 * {@code executor}'s thread. {@code executor} must not run tasks on the
61 * current thread to prevent blocking networking operations and causing
62 * exceptions during shutdown.
63 *
64 * @param url {@link java.net.URL} for the generated requests.
65 * @param callback callback object that gets invoked on different events.
66 * @param executor {@link Executor} on which all callbacks will be invoked.
67 * @param cronetEngine {@link CronetEngine} used to execute this request.
68 */
69 UrlRequestBuilderImpl(String url, UrlRequest.Callback callback, Executor exe cutor,
70 CronetEngineBase cronetEngine) {
71 super();
72 if (url == null) {
73 throw new NullPointerException("URL is required.");
74 }
75 if (callback == null) {
76 throw new NullPointerException("Callback is required.");
77 }
78 if (executor == null) {
79 throw new NullPointerException("Executor is required.");
80 }
81 if (cronetEngine == null) {
82 throw new NullPointerException("CronetEngine is required.");
83 }
84 mUrl = url;
85 mCallback = callback;
86 mExecutor = executor;
87 mCronetEngine = cronetEngine;
88 }
89
90 /**
91 * Sets the HTTP method verb to use for this request.
92 *
93 * <p>The default when this method is not called is "GET" if the request has
94 * no body or "POST" if it does.
95 *
96 * @param method "GET", "HEAD", "DELETE", "POST" or "PUT".
97 * @return the builder to facilitate chaining.
98 */
pauljensen 2016/09/20 19:01:37 all these comments should be removed and replaced
kapishnikov 2016/09/22 21:32:03 Done. Same for BidirectionalStreamBuilderImpl. Kep
99 public UrlRequest.Builder setHttpMethod(String method) {
100 if (method == null) {
101 throw new NullPointerException("Method is required.");
102 }
103 mMethod = method;
104 return this;
105 }
106
107 /**
108 * Adds a request header.
109 *
110 * @param header header name.
111 * @param value header value.
112 * @return the builder to facilitate chaining.
113 */
114 public UrlRequestBuilderImpl addHeader(String header, String value) {
115 if (header == null) {
116 throw new NullPointerException("Invalid header name.");
117 }
118 if (value == null) {
119 throw new NullPointerException("Invalid header value.");
120 }
121 if (ACCEPT_ENCODING.equalsIgnoreCase(header)) {
122 Log.w(TAG, "It's not necessary to set Accept-Encoding on requests - cronet will do"
123 + " this automatically for you, and setting it yours elf has no "
124 + "effect. See https://crbug.com/581399 for details. ",
125 new Exception());
126 return this;
127 }
128 mRequestHeaders.add(Pair.create(header, value));
129 return this;
130 }
131
132 /**
133 * Disables cache for the request. If context is not set up to use cache,
134 * this call has no effect.
135 * @return the builder to facilitate chaining.
136 */
137 public UrlRequestBuilderImpl disableCache() {
138 mDisableCache = true;
139 return this;
140 }
141
142 /**
143 * Disables connection migration for the request if enabled for
144 * the session.
145 * @return the builder to facilitate chaining.
146 */
147 public UrlRequestBuilderImpl disableConnectionMigration() {
148 mDisableConnectionMigration = true;
149 return this;
150 }
151
152 /**
153 * Sets priority of the request which should be one of the
154 * {@link #REQUEST_PRIORITY_IDLE REQUEST_PRIORITY_*} values.
155 * The request is given {@link #REQUEST_PRIORITY_MEDIUM} priority
156 * if this method is not called.
157 *
158 * @param priority priority of the request which should be one of the
159 * {@link #REQUEST_PRIORITY_IDLE REQUEST_PRIORITY_*} values.
160 * @return the builder to facilitate chaining.
161 */
162 public UrlRequestBuilderImpl setPriority(@CronetEngineBase.RequestPriority i nt priority) {
163 mPriority = priority;
164 return this;
165 }
166
167 /**
168 * Sets upload data provider. Switches method to "POST" if not
169 * explicitly set. Starting the request will throw an exception if a
170 * Content-Type header is not set.
171 *
172 * @param uploadDataProvider responsible for providing the upload data.
173 * @param executor All {@code uploadDataProvider} methods will be invoked
174 * using this {@code Executor}. May optionally be the same
175 * {@code Executor} the request itself is using.
176 * @return the builder to facilitate chaining.
177 */
178 public UrlRequestBuilderImpl setUploadDataProvider(
179 UploadDataProvider uploadDataProvider, Executor executor) {
180 if (uploadDataProvider == null) {
181 throw new NullPointerException("Invalid UploadDataProvider.");
182 }
183 if (executor == null) {
184 throw new NullPointerException("Invalid UploadDataProvider Executor. ");
185 }
186 if (mMethod == null) {
187 mMethod = "POST";
188 }
189 mUploadDataProvider = uploadDataProvider;
190 mUploadDataProviderExecutor = executor;
191 return this;
192 }
193
194 /**
195 * Marks that the executors this request will use to notify callbacks (for
196 * {@code UploadDataProvider}s and {@code UrlRequest.Callback}s) is intentio nally performing
197 * inline execution, like Guava's directExecutor or
198 * {@link java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy}.
199 *
200 * <p><b>Warning:</b> This option makes it easy to accidentally block the ne twork thread.
201 * It should not be used if your callbacks perform disk I/O, acquire locks, or call into
202 * other code you don't carefully control and audit.
203 */
204 public UrlRequestBuilderImpl allowDirectExecutor() {
205 mAllowDirectExecutor = true;
206 return this;
207 }
208
209 /**
210 * Associates the annotation object with this request. May add more than one .
211 * Passed through to a {@link org.chromium.net.RequestFinishedInfo.Listener} ,
212 * see {@link org.chromium.net.RequestFinishedInfo#getAnnotations}.
213 *
214 * @param annotation an object to pass on to the
215 * {@link org.chromium.net.RequestFinishedInfo.Listener} with a
216 * {@link org.chromium.net.RequestFinishedInfo}.
217 * @return the builder to facilitate chaining.
218 */
219 public UrlRequestBuilderImpl addRequestAnnotation(Object annotation) {
220 if (annotation == null) {
221 throw new NullPointerException("Invalid metrics annotation.");
222 }
223 if (mRequestAnnotations.isEmpty()) {
224 mRequestAnnotations = new ArrayList<>();
225 }
226 mRequestAnnotations.add(annotation);
227 return this;
228 }
229
230 /**
231 * Creates a {@link UrlRequest} using configuration within this
232 * builder. The returned {@code UrlRequest} can then be started
233 * by calling {@link UrlRequest#start}.
234 *
235 * @return constructed {@link UrlRequest}.
236 */
237 public UrlRequestBase build() {
238 final UrlRequestBase request = mCronetEngine.createRequest(mUrl, mCallba ck, mExecutor,
239 mPriority, mRequestAnnotations, mDisableCache, mDisableConnectio nMigration,
240 mAllowDirectExecutor);
241 if (mMethod != null) {
242 request.setHttpMethod(mMethod);
243 }
244 for (Pair<String, String> header : mRequestHeaders) {
245 request.addHeader(header.first, header.second);
246 }
247 if (mUploadDataProvider != null) {
248 request.setUploadDataProvider(mUploadDataProvider, mUploadDataProvid erExecutor);
249 }
250 return request;
251 }
252 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698