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

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

Issue 2339223002: Cronet API Refactoring (Closed)
Patch Set: Addressed Paul's comments + rebase Created 4 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
(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.
pauljensen 2016/10/03 15:22:38 why not just say ExperimentalUrlRequest.Builder, r
kapishnikov 2016/10/03 23:49:28 Done.
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
pauljensen 2016/10/03 15:22:38 UrlRequest->ExperimentalUrlRequest
kapishnikov 2016/10/03 23:49:28 Done.
27 // copied to built UrlRequests.
pauljensen 2016/10/03 15:22:38 ditto
kapishnikov 2016/10/03 23:49:28 Done.
28
29 // CronetEngine to execute request.
pauljensen 2016/10/03 15:22:38 CronetEngine->CronetEngineBase
kapishnikov 2016/10/03 23:49:28 Done.
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.
pauljensen 2016/10/03 15:22:38 remove incorrect @link tag
kapishnikov 2016/10/03 23:49:28 Done.
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 @Override
91 public ExperimentalUrlRequest.Builder setHttpMethod(String method) {
92 if (method == null) {
93 throw new NullPointerException("Method is required.");
94 }
95 mMethod = method;
96 return this;
97 }
98
99 @Override
100 public UrlRequestBuilderImpl addHeader(String header, String value) {
101 if (header == null) {
102 throw new NullPointerException("Invalid header name.");
103 }
104 if (value == null) {
105 throw new NullPointerException("Invalid header value.");
106 }
107 if (ACCEPT_ENCODING.equalsIgnoreCase(header)) {
108 Log.w(TAG, "It's not necessary to set Accept-Encoding on requests - cronet will do"
109 + " this automatically for you, and setting it yours elf has no "
110 + "effect. See https://crbug.com/581399 for details. ",
111 new Exception());
112 return this;
113 }
114 mRequestHeaders.add(Pair.create(header, value));
115 return this;
116 }
117
118 @Override
119 public UrlRequestBuilderImpl disableCache() {
120 mDisableCache = true;
121 return this;
122 }
123
124 @Override
125 public UrlRequestBuilderImpl disableConnectionMigration() {
126 mDisableConnectionMigration = true;
127 return this;
128 }
129
130 @Override
131 public UrlRequestBuilderImpl setPriority(@CronetEngineBase.RequestPriority i nt priority) {
132 mPriority = priority;
133 return this;
134 }
135
136 @Override
137 public UrlRequestBuilderImpl setUploadDataProvider(
138 UploadDataProvider uploadDataProvider, Executor executor) {
139 if (uploadDataProvider == null) {
140 throw new NullPointerException("Invalid UploadDataProvider.");
141 }
142 if (executor == null) {
143 throw new NullPointerException("Invalid UploadDataProvider Executor. ");
144 }
145 if (mMethod == null) {
146 mMethod = "POST";
147 }
148 mUploadDataProvider = uploadDataProvider;
149 mUploadDataProviderExecutor = executor;
150 return this;
151 }
152
153 @Override
154 public UrlRequestBuilderImpl allowDirectExecutor() {
155 mAllowDirectExecutor = true;
156 return this;
157 }
158
159 @Override
160 public UrlRequestBuilderImpl addRequestAnnotation(Object annotation) {
161 if (annotation == null) {
162 throw new NullPointerException("Invalid metrics annotation.");
163 }
164 if (mRequestAnnotations.isEmpty()) {
165 mRequestAnnotations = new ArrayList<>();
166 }
167 mRequestAnnotations.add(annotation);
168 return this;
169 }
170
171 @Override
172 public UrlRequestBase build() {
173 final UrlRequestBase request = mCronetEngine.createRequest(mUrl, mCallba ck, mExecutor,
174 mPriority, mRequestAnnotations, mDisableCache, mDisableConnectio nMigration,
175 mAllowDirectExecutor);
176 if (mMethod != null) {
177 request.setHttpMethod(mMethod);
178 }
179 for (Pair<String, String> header : mRequestHeaders) {
180 request.addHeader(header.first, header.second);
181 }
182 if (mUploadDataProvider != null) {
183 request.setUploadDataProvider(mUploadDataProvider, mUploadDataProvid erExecutor);
184 }
185 return request;
186 }
187 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698