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

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

Issue 2339223002: Cronet API Refactoring (Closed)
Patch Set: Rebase & Conflict Resolution Created 4 years, 1 month 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.annotation.SuppressLint;
7 import android.util.Log;
8 import android.util.Pair;
9
10 import org.chromium.net.CronetEngine;
11 import org.chromium.net.ExperimentalUrlRequest;
12 import org.chromium.net.UploadDataProvider;
13 import org.chromium.net.UrlRequest;
14
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.concurrent.Executor;
19
20 /**
21 * Implements {@link org.chromium.net.ExperimentalUrlRequest.Builder}.
22 */
23 public class UrlRequestBuilderImpl extends ExperimentalUrlRequest.Builder {
24 private static final String ACCEPT_ENCODING = "Accept-Encoding";
25 private static final String TAG = "UrlRequestBuilder";
26
27 // All fields are temporary storage of ExperimentalUrlRequest configuration to be
28 // copied to built ExperimentalUrlRequest.
29
30 // CronetEngineBase to execute request.
31 private final CronetEngineBase mCronetEngine;
32 // URL to request.
33 private final String mUrl;
34 // Callback to receive progress callbacks.
35 private final UrlRequest.Callback mCallback;
36 // Executor to invoke callback on.
37 private final Executor mExecutor;
38 // HTTP method (e.g. GET, POST etc).
39 private String mMethod;
40
41 // List of request headers, stored as header field name and value pairs.
42 private final ArrayList<Pair<String, String>> mRequestHeaders = new ArrayLis t<>();
43 // Disable the cache for just this request.
44 private boolean mDisableCache;
45 // Disable connection migration for just this request.
46 private boolean mDisableConnectionMigration;
47 // Priority of request. Default is medium.
48 @CronetEngineBase.RequestPriority
49 private int mPriority = REQUEST_PRIORITY_MEDIUM;
50 // Request reporting annotations. Avoid extra object creation if no annotati ons added.
51 private Collection<Object> mRequestAnnotations = Collections.emptyList();
52 // If request is an upload, this provides the request body data.
53 private UploadDataProvider mUploadDataProvider;
54 // Executor to call upload data provider back on.
55 private Executor mUploadDataProviderExecutor;
56 private boolean mAllowDirectExecutor = false;
57
58 /**
59 * Creates a builder for {@link UrlRequest} objects. All callbacks for
60 * generated {@link UrlRequest} objects will be invoked on
61 * {@code executor}'s thread. {@code executor} must not run tasks on the
62 * current thread to prevent blocking networking operations and causing
63 * exceptions during shutdown.
64 *
65 * @param url URL for the generated requests.
66 * @param callback callback object that gets invoked on different events.
67 * @param executor {@link Executor} on which all callbacks will be invoked.
68 * @param cronetEngine {@link CronetEngine} used to execute this request.
69 */
70 UrlRequestBuilderImpl(String url, UrlRequest.Callback callback, Executor exe cutor,
71 CronetEngineBase cronetEngine) {
72 super();
73 if (url == null) {
74 throw new NullPointerException("URL is required.");
75 }
76 if (callback == null) {
77 throw new NullPointerException("Callback is required.");
78 }
79 if (executor == null) {
80 throw new NullPointerException("Executor is required.");
81 }
82 if (cronetEngine == null) {
83 throw new NullPointerException("CronetEngine is required.");
84 }
85 mUrl = url;
86 mCallback = callback;
87 mExecutor = executor;
88 mCronetEngine = cronetEngine;
89 }
90
91 @Override
92 public ExperimentalUrlRequest.Builder setHttpMethod(String method) {
93 if (method == null) {
94 throw new NullPointerException("Method is required.");
95 }
96 mMethod = method;
97 return this;
98 }
99
100 @Override
101 public UrlRequestBuilderImpl addHeader(String header, String value) {
102 if (header == null) {
103 throw new NullPointerException("Invalid header name.");
104 }
105 if (value == null) {
106 throw new NullPointerException("Invalid header value.");
107 }
108 if (ACCEPT_ENCODING.equalsIgnoreCase(header)) {
109 Log.w(TAG, "It's not necessary to set Accept-Encoding on requests - cronet will do"
110 + " this automatically for you, and setting it yours elf has no "
111 + "effect. See https://crbug.com/581399 for details. ",
112 new Exception());
113 return this;
114 }
115 mRequestHeaders.add(Pair.create(header, value));
116 return this;
117 }
118
119 @Override
120 public UrlRequestBuilderImpl disableCache() {
121 mDisableCache = true;
122 return this;
123 }
124
125 @Override
126 public UrlRequestBuilderImpl disableConnectionMigration() {
127 mDisableConnectionMigration = true;
128 return this;
129 }
130
131 @Override
132 public UrlRequestBuilderImpl setPriority(@CronetEngineBase.RequestPriority i nt priority) {
133 mPriority = priority;
134 return this;
135 }
136
137 @Override
138 public UrlRequestBuilderImpl setUploadDataProvider(
139 UploadDataProvider uploadDataProvider, Executor executor) {
140 if (uploadDataProvider == null) {
141 throw new NullPointerException("Invalid UploadDataProvider.");
142 }
143 if (executor == null) {
144 throw new NullPointerException("Invalid UploadDataProvider Executor. ");
145 }
146 if (mMethod == null) {
147 mMethod = "POST";
148 }
149 mUploadDataProvider = uploadDataProvider;
150 mUploadDataProviderExecutor = executor;
151 return this;
152 }
153
154 @Override
155 public UrlRequestBuilderImpl allowDirectExecutor() {
156 mAllowDirectExecutor = true;
157 return this;
158 }
159
160 @Override
161 public UrlRequestBuilderImpl addRequestAnnotation(Object annotation) {
162 if (annotation == null) {
163 throw new NullPointerException("Invalid metrics annotation.");
164 }
165 if (mRequestAnnotations.isEmpty()) {
166 mRequestAnnotations = new ArrayList<>();
167 }
168 mRequestAnnotations.add(annotation);
169 return this;
170 }
171
172 @Override
173 public UrlRequestBase build() {
174 @SuppressLint("WrongConstant") // TODO(jbudorick): Remove this after rol ling to the N SDK.
175 final UrlRequestBase request = mCronetEngine.createRequest(mUrl, mCallba ck, mExecutor,
176 mPriority, mRequestAnnotations, mDisableCache, mDisableConnectio nMigration,
177 mAllowDirectExecutor);
178 if (mMethod != null) {
179 request.setHttpMethod(mMethod);
180 }
181 for (Pair<String, String> header : mRequestHeaders) {
182 request.addHeader(header.first, header.second);
183 }
184 if (mUploadDataProvider != null) {
185 request.setUploadDataProvider(mUploadDataProvider, mUploadDataProvid erExecutor);
186 }
187 return request;
188 }
189 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698