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

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

Issue 470443005: Cronet modifications to support AGSA. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkgr
Patch Set: Small changes. Created 6 years, 4 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.os.ConditionVariable;
8
7 import org.apache.http.conn.ConnectTimeoutException; 9 import org.apache.http.conn.ConnectTimeoutException;
8 import org.chromium.base.CalledByNative; 10 import org.chromium.base.CalledByNative;
9 import org.chromium.base.JNINamespace; 11 import org.chromium.base.JNINamespace;
10 12
11 import java.io.IOException; 13 import java.io.IOException;
12 import java.net.MalformedURLException; 14 import java.net.MalformedURLException;
13 import java.net.URL; 15 import java.net.URL;
14 import java.net.UnknownHostException; 16 import java.net.UnknownHostException;
15 import java.nio.ByteBuffer; 17 import java.nio.ByteBuffer;
16 import java.nio.channels.ReadableByteChannel; 18 import java.nio.channels.ReadableByteChannel;
(...skipping 15 matching lines...) Expand all
32 private final UrlRequestContext mRequestContext; 34 private final UrlRequestContext mRequestContext;
33 private final String mUrl; 35 private final String mUrl;
34 private final int mPriority; 36 private final int mPriority;
35 private final Map<String, String> mHeaders; 37 private final Map<String, String> mHeaders;
36 private final WritableByteChannel mSink; 38 private final WritableByteChannel mSink;
37 private Map<String, String> mAdditionalHeaders; 39 private Map<String, String> mAdditionalHeaders;
38 private String mUploadContentType; 40 private String mUploadContentType;
39 private String mMethod; 41 private String mMethod;
40 private byte[] mUploadData; 42 private byte[] mUploadData;
41 private ReadableByteChannel mUploadChannel; 43 private ReadableByteChannel mUploadChannel;
42 private WritableByteChannel mOutputChannel;
43 private IOException mSinkException; 44 private IOException mSinkException;
44 private volatile boolean mStarted; 45 private volatile boolean mStarted;
45 private volatile boolean mCanceled; 46 private volatile boolean mCanceled;
46 private volatile boolean mRecycled; 47 private volatile boolean mRecycled;
47 private volatile boolean mFinished; 48 private volatile boolean mFinished;
48 private boolean mHeadersAvailable; 49 private boolean mHeadersAvailable;
49 private String mContentType; 50 private String mContentType;
50 private long mContentLength; 51 private long mContentLength;
51 private long mUploadContentLength; 52 private long mUploadContentLength;
53 private ConditionVariable mAppendChunkCondition;
52 private final ContextLock mLock; 54 private final ContextLock mLock;
53 55
54 /** 56 /**
55 * Native adapter object, owned by UrlRequest. 57 * Native adapter object, owned by UrlRequest.
56 */ 58 */
57 private long mUrlRequestAdapter; 59 private long mUrlRequestAdapter;
58 60
59 /** 61 /**
60 * Constructor. 62 * Constructor.
61 * 63 *
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 * POST. 105 * POST.
104 * @param data The content that needs to be uploaded if this is a POST 106 * @param data The content that needs to be uploaded if this is a POST
105 * request. 107 * request.
106 */ 108 */
107 public void setUploadData(String contentType, byte[] data) { 109 public void setUploadData(String contentType, byte[] data) {
108 synchronized (mLock) { 110 synchronized (mLock) {
109 validateNotStarted(); 111 validateNotStarted();
110 mUploadContentType = contentType; 112 mUploadContentType = contentType;
111 mUploadData = data; 113 mUploadData = data;
112 mUploadChannel = null; 114 mUploadChannel = null;
115 mAppendChunkCondition = null;
113 } 116 }
114 } 117 }
115 118
116 /** 119 /**
117 * Sets a readable byte channel to upload as part of a POST request. 120 * Sets a readable byte channel to upload as part of a POST request.
118 * 121 *
119 * @param contentType MIME type of the post content or null if this is not a 122 * @param contentType MIME type of the post content or null if this is not a
120 * POST request. 123 * POST request.
121 * @param channel The channel to read to read upload data from if this is a 124 * @param channel The channel to read to read upload data from if this is a
122 * POST request. 125 * POST request.
123 * @param contentLength The length of data to upload. 126 * @param contentLength The length of data to upload.
124 */ 127 */
125 public void setUploadChannel(String contentType, 128 public void setUploadChannel(String contentType,
126 ReadableByteChannel channel, long contentLength) { 129 ReadableByteChannel channel, long contentLength) {
127 synchronized (mLock) { 130 synchronized (mLock) {
128 validateNotStarted(); 131 validateNotStarted();
129 mUploadContentType = contentType; 132 mUploadContentType = contentType;
130 mUploadChannel = channel; 133 mUploadChannel = channel;
131 mUploadContentLength = contentLength; 134 mUploadContentLength = contentLength;
132 mUploadData = null; 135 mUploadData = null;
136 mAppendChunkCondition = null;
133 } 137 }
134 } 138 }
135 139
140 /**
141 * Sets this request up for chunked uploading. To upload data call
142 * {@link #appendChunkBlocking(ByteBuffer, boolean)} after {@link #start()}.
143 *
144 * @param contentType MIME type of the post content or null if this is not a
145 * POST request.
146 */
147 public void setChunkedUpload(String contentType) {
148 synchronized (mLock) {
149 validateNotStarted();
150 mUploadContentType = contentType;
151 mAppendChunkCondition = new ConditionVariable();
152 mUploadData = null;
153 mUploadChannel = null;
154 }
155 }
156
157 /**
158 * Uploads a new chunk. Must have called {@link #setChunkedUpload(String)}
159 * and {@link #start()}.
160 *
161 * @param chunk The data, which will not be modified. It must not be empty
162 * and its current position must be zero.
163 * @param isLastChunk Whether this chunk is the last one.
164 */
165 public void appendChunkBlocking(ByteBuffer chunk, boolean isLastChunk)
166 throws IOException {
167 if (!chunk.hasRemaining()) {
168 throw new IllegalArgumentException(
169 "Attempted to write empty buffer.");
170 }
171 if (chunk.position() != 0) {
172 throw new IllegalArgumentException("The position must be zero.");
173 }
174 synchronized (mLock) {
175 if (!mStarted) {
176 throw new IllegalStateException("Request not yet started.");
177 }
178 if (mAppendChunkCondition == null) {
179 throw new IllegalStateException(
180 "Request not set for chunked uploadind.");
181 }
182 if (mUrlRequestAdapter == 0) {
183 throw new IOException("Native peer destroyed.");
184 }
185 mAppendChunkCondition.close();
186 nativeAppendChunk(mUrlRequestAdapter, chunk, chunk.limit(),
187 isLastChunk);
188 }
189 // Wait for the data to be actually consumed. Outside mLock to avoid
190 // deadlock.
191 mAppendChunkCondition.block();
192 }
193
136 public void setHttpMethod(String method) { 194 public void setHttpMethod(String method) {
137 validateNotStarted(); 195 validateNotStarted();
138 if (!("PUT".equals(method) || "POST".equals(method))) { 196 if (!("PUT".equals(method) || "POST".equals(method))) {
139 throw new IllegalArgumentException("Only PUT or POST are allowed."); 197 throw new IllegalArgumentException("Only PUT or POST are allowed.");
140 } 198 }
141 mMethod = method; 199 mMethod = method;
142 } 200 }
143 201
144 public WritableByteChannel getSink() { 202 public WritableByteChannel getSink() {
145 return mSink; 203 return mSink;
146 } 204 }
147 205
148 public void start() { 206 public void start() {
149 synchronized (mLock) { 207 synchronized (mLock) {
150 if (mCanceled) { 208 if (mCanceled) {
151 return; 209 return;
152 } 210 }
153 211
154 validateNotStarted(); 212 validateNotStarted();
155 validateNotRecycled(); 213 validateNotRecycled();
156 214
157 mStarted = true; 215 mStarted = true;
158 216
159 String method = mMethod; 217 String method = mMethod;
160 if (method == null && 218 if (method == null &&
161 ((mUploadData != null && mUploadData.length > 0) || 219 ((mUploadData != null && mUploadData.length > 0) ||
162 mUploadChannel != null)) { 220 mUploadChannel != null || mAppendChunkCondition != null)) {
163 // Default to POST if there is data to upload but no method was 221 // Default to POST if there is data to upload but no method was
164 // specified. 222 // specified.
165 method = "POST"; 223 method = "POST";
166 } 224 }
167 225
168 if (method != null) { 226 if (method != null) {
169 nativeSetMethod(mUrlRequestAdapter, method); 227 nativeSetMethod(mUrlRequestAdapter, method);
170 } 228 }
171 229
172 if (mHeaders != null && !mHeaders.isEmpty()) { 230 if (mHeaders != null && !mHeaders.isEmpty()) {
(...skipping 10 matching lines...) Expand all
183 entry.getValue()); 241 entry.getValue());
184 } 242 }
185 } 243 }
186 244
187 if (mUploadData != null && mUploadData.length > 0) { 245 if (mUploadData != null && mUploadData.length > 0) {
188 nativeSetUploadData(mUrlRequestAdapter, mUploadContentType, 246 nativeSetUploadData(mUrlRequestAdapter, mUploadContentType,
189 mUploadData); 247 mUploadData);
190 } else if (mUploadChannel != null) { 248 } else if (mUploadChannel != null) {
191 nativeSetUploadChannel(mUrlRequestAdapter, mUploadContentType, 249 nativeSetUploadChannel(mUrlRequestAdapter, mUploadContentType,
192 mUploadContentLength); 250 mUploadContentLength);
251 } else if (mAppendChunkCondition != null) {
252 nativeEnableChunkedUpload(mUrlRequestAdapter,
253 mUploadContentType);
193 } 254 }
194 255
195 nativeStart(mUrlRequestAdapter); 256 nativeStart(mUrlRequestAdapter);
196 } 257 }
197 } 258 }
198 259
199 public void cancel() { 260 public void cancel() {
200 synchronized (mLock) { 261 synchronized (mLock) {
201 if (mCanceled) { 262 if (mCanceled) {
202 return; 263 return;
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 342
282 // All response headers. 343 // All response headers.
283 public Map<String, List<String>> getAllHeaders() { 344 public Map<String, List<String>> getAllHeaders() {
284 validateHeadersAvailable(); 345 validateHeadersAvailable();
285 ResponseHeadersMap result = new ResponseHeadersMap(); 346 ResponseHeadersMap result = new ResponseHeadersMap();
286 nativeGetAllHeaders(mUrlRequestAdapter, result); 347 nativeGetAllHeaders(mUrlRequestAdapter, result);
287 return result; 348 return result;
288 } 349 }
289 350
290 /** 351 /**
352 * A callback invoked when appending a chunk to the request has completed.
353 */
354 @CalledByNative
355 protected void onAppendChunkCompleted() {
356 mAppendChunkCondition.open();
357 }
358
359 /**
291 * A callback invoked when the first chunk of the response has arrived. 360 * A callback invoked when the first chunk of the response has arrived.
292 */ 361 */
293 @CalledByNative 362 @CalledByNative
294 protected void onResponseStarted() { 363 protected void onResponseStarted() {
295 mContentType = nativeGetContentType(mUrlRequestAdapter); 364 mContentType = nativeGetContentType(mUrlRequestAdapter);
296 mContentLength = nativeGetContentLength(mUrlRequestAdapter); 365 mContentLength = nativeGetContentLength(mUrlRequestAdapter);
297 mHeadersAvailable = true; 366 mHeadersAvailable = true;
298 } 367 }
299 368
300 /** 369 /**
(...skipping 22 matching lines...) Expand all
323 } 392 }
324 393
325 /** 394 /**
326 * Notifies the listener, releases native data structures. 395 * Notifies the listener, releases native data structures.
327 */ 396 */
328 @SuppressWarnings("unused") 397 @SuppressWarnings("unused")
329 @CalledByNative 398 @CalledByNative
330 private void finish() { 399 private void finish() {
331 synchronized (mLock) { 400 synchronized (mLock) {
332 mFinished = true; 401 mFinished = true;
402 if (mAppendChunkCondition != null) {
403 mAppendChunkCondition.open();
404 }
333 405
334 if (mRecycled) { 406 if (mRecycled) {
335 return; 407 return;
336 } 408 }
337 try { 409 try {
338 mSink.close(); 410 mSink.close();
339 } catch (IOException e) { 411 } catch (IOException e) {
340 // Ignore 412 // Ignore
341 } 413 }
342 onRequestComplete(); 414 onRequestComplete();
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
418 String value); 490 String value);
419 491
420 private native void nativeSetMethod(long urlRequestAdapter, String method); 492 private native void nativeSetMethod(long urlRequestAdapter, String method);
421 493
422 private native void nativeSetUploadData(long urlRequestAdapter, 494 private native void nativeSetUploadData(long urlRequestAdapter,
423 String contentType, byte[] content); 495 String contentType, byte[] content);
424 496
425 private native void nativeSetUploadChannel(long urlRequestAdapter, 497 private native void nativeSetUploadChannel(long urlRequestAdapter,
426 String contentType, long contentLength); 498 String contentType, long contentLength);
427 499
500 private native void nativeEnableChunkedUpload(long urlRequestAdapter,
501 String contentType);
502
503 private native void nativeAppendChunk(long urlRequestAdapter,
504 ByteBuffer chunk, int chunkSize, boolean isLastChunk);
505
428 private native void nativeStart(long urlRequestAdapter); 506 private native void nativeStart(long urlRequestAdapter);
429 507
430 private native void nativeCancel(long urlRequestAdapter); 508 private native void nativeCancel(long urlRequestAdapter);
431 509
432 private native void nativeDestroyRequestAdapter(long urlRequestAdapter); 510 private native void nativeDestroyRequestAdapter(long urlRequestAdapter);
433 511
434 private native int nativeGetErrorCode(long urlRequestAdapter); 512 private native int nativeGetErrorCode(long urlRequestAdapter);
435 513
436 private native int nativeGetHttpStatusCode(long urlRequestAdapter); 514 private native int nativeGetHttpStatusCode(long urlRequestAdapter);
437 515
438 private native String nativeGetErrorString(long urlRequestAdapter); 516 private native String nativeGetErrorString(long urlRequestAdapter);
439 517
440 private native String nativeGetContentType(long urlRequestAdapter); 518 private native String nativeGetContentType(long urlRequestAdapter);
441 519
442 private native long nativeGetContentLength(long urlRequestAdapter); 520 private native long nativeGetContentLength(long urlRequestAdapter);
443 521
444 private native String nativeGetHeader(long urlRequestAdapter, String name); 522 private native String nativeGetHeader(long urlRequestAdapter, String name);
445 523
446 private native void nativeGetAllHeaders(long urlRequestAdapter, 524 private native void nativeGetAllHeaders(long urlRequestAdapter,
447 ResponseHeadersMap headers); 525 ResponseHeadersMap headers);
448 526
449 // Explicit class to work around JNI-generator generics confusion. 527 // Explicit class to work around JNI-generator generics confusion.
450 private class ResponseHeadersMap extends HashMap<String, List<String>> { 528 private class ResponseHeadersMap extends HashMap<String, List<String>> {
451 } 529 }
452 } 530 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698