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

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

Issue 849903002: [Cronet] Upload support for async APIs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Combined with Matt's CL Created 5 years, 10 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.Log; 7 import android.util.Log;
8 import android.util.Pair; 8 import android.util.Pair;
9 9
10 import org.chromium.base.CalledByNative; 10 import org.chromium.base.CalledByNative;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 * decided a redirect will be followed. 49 * decided a redirect will be followed.
50 */ 50 */
51 private final List<String> mUrlChain = new ArrayList<String>(); 51 private final List<String> mUrlChain = new ArrayList<String>();
52 52
53 private final UrlRequestListener mListener; 53 private final UrlRequestListener mListener;
54 private final String mInitialUrl; 54 private final String mInitialUrl;
55 private final int mPriority; 55 private final int mPriority;
56 private String mInitialMethod; 56 private String mInitialMethod;
57 private final HeadersList mRequestHeaders = new HeadersList(); 57 private final HeadersList mRequestHeaders = new HeadersList();
58 58
59 private CronetUploadDataStream mUploadDataStream;
60
59 private NativeResponseInfo mResponseInfo; 61 private NativeResponseInfo mResponseInfo;
60 62
61 /* 63 /*
62 * Listener callback is repeatedly called when data is received, so it is 64 * Listener callback is repeatedly called when data is received, so it is
63 * cached as member variable. 65 * cached as member variable.
64 */ 66 */
65 private OnDataReceivedRunnable mOnDataReceivedTask; 67 private OnDataReceivedRunnable mOnDataReceivedTask;
66 68
67 static final class HeadersList extends ArrayList<Pair<String, String>> { 69 static final class HeadersList extends ArrayList<Pair<String, String>> {
68 } 70 }
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 if (header == null) { 252 if (header == null) {
251 throw new NullPointerException("Invalid header name."); 253 throw new NullPointerException("Invalid header name.");
252 } 254 }
253 if (value == null) { 255 if (value == null) {
254 throw new NullPointerException("Invalid header value."); 256 throw new NullPointerException("Invalid header value.");
255 } 257 }
256 mRequestHeaders.add(Pair.create(header, value)); 258 mRequestHeaders.add(Pair.create(header, value));
257 } 259 }
258 260
259 @Override 261 @Override
262 public void setUploadDataProvider(UploadDataProvider uploadDataProvider, Exe cutor executor) {
263 if (uploadDataProvider == null) {
264 throw new NullPointerException("Invalid UploadDataProvider.");
265 }
266 if (mInitialMethod == null) {
267 mInitialMethod = "POST";
268 }
269 mUploadDataStream = new CronetUploadDataStream(uploadDataProvider, execu tor);
270 }
271
272 @Override
260 public void start() { 273 public void start() {
261 synchronized (mUrlRequestAdapterLock) { 274 synchronized (mUrlRequestAdapterLock) {
262 checkNotStarted(); 275 checkNotStarted();
263 mUrlRequestAdapter = nativeCreateRequestAdapter( 276
264 mRequestContext.getUrlRequestContextAdapter(), 277 try {
265 mInitialUrl, 278 mUrlRequestAdapter = nativeCreateRequestAdapter(
266 mPriority); 279 mRequestContext.getUrlRequestContextAdapter(), mInitialU rl, mPriority);
267 mRequestContext.onRequestStarted(this); 280 mRequestContext.onRequestStarted(this);
268 if (mInitialMethod != null) { 281 if (mInitialMethod != null) {
269 if (!nativeSetHttpMethod(mUrlRequestAdapter, mInitialMethod)) { 282 if (!nativeSetHttpMethod(mUrlRequestAdapter, mInitialMethod) ) {
270 destroyRequestAdapter(); 283 throw new IllegalArgumentException("Invalid http method " + mInitialMethod);
271 throw new IllegalArgumentException("Invalid http method " 284 }
272 + mInitialMethod);
273 } 285 }
286
287 boolean hasContentType = false;
288 for (Pair<String, String> header : mRequestHeaders) {
289 if (header.first == "Content-Type" && !header.second.isEmpty ()) {
pauljensen 2015/02/02 17:40:29 Should use equalsIgnoreCase() to compare header na
xunjieli 2015/02/02 18:25:41 Done.
290 hasContentType = true;
291 }
292 if (!nativeAddHeader(mUrlRequestAdapter, header.first, heade r.second)) {
293 destroyRequestAdapter();
294 throw new IllegalArgumentException(
295 "Invalid header " + header.first + "=" + header. second);
296 }
297 }
298 if (mUploadDataStream != null) {
mef 2015/02/02 17:45:11 Would it make sense to make Content-Type an argume
mmenke 2015/02/02 18:26:12 The problem with this is if the header is also set
299 if (!hasContentType) {
300 throw new IllegalArgumentException(
301 "Requests with body must have a Content-Type.");
pauljensen 2015/02/02 17:40:29 Certain HTTP stacks (e.g. OkHttp) default to appli
xunjieli 2015/02/02 18:25:41 Thanks for the heads up! Will keep this in mind. A
302 }
303 mUploadDataStream.attachToRequest(this, mUrlRequestAdapter);
304 }
305 } catch (RuntimeException e) {
mef 2015/02/02 17:45:11 should this be just Exception?
mmenke 2015/02/02 18:26:12 All other exceptions have to be declared, no? Sin
306 // If there's an exception, cleanup and then throw the
307 // exception to the caller.
308 destroyRequestAdapter();
309 throw e;
274 } 310 }
275 for (Pair<String, String> header : mRequestHeaders) { 311
276 if (!nativeAddHeader(mUrlRequestAdapter, header.first,
277 header.second)) {
278 destroyRequestAdapter();
279 throw new IllegalArgumentException("Invalid header "
280 + header.first + "=" + header.second);
281 }
282 }
283 mStarted = true; 312 mStarted = true;
284 nativeStart(mUrlRequestAdapter); 313 nativeStart(mUrlRequestAdapter);
285 } 314 }
286 } 315 }
287 316
288 @Override 317 @Override
289 public void cancel() { 318 public void cancel() {
290 synchronized (mUrlRequestAdapterLock) { 319 synchronized (mUrlRequestAdapterLock) {
291 if (mCanceled || !mStarted) { 320 if (mCanceled || !mStarted) {
292 return; 321 return;
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 cancel(); 437 cancel();
409 } 438 }
410 try { 439 try {
411 mListener.onFailed(this, mResponseInfo, requestError); 440 mListener.onFailed(this, mResponseInfo, requestError);
412 } catch (Exception failException) { 441 } catch (Exception failException) {
413 Log.e(CronetUrlRequestContext.LOG_TAG, 442 Log.e(CronetUrlRequestContext.LOG_TAG,
414 "Exception notifying of failed request", failException); 443 "Exception notifying of failed request", failException);
415 } 444 }
416 } 445 }
417 446
447 /**
mef 2015/02/02 17:45:11 empty comment?
xunjieli 2015/02/02 18:25:41 Done.
448 */
449 void onUploadException(Exception e) {
450 UrlRequestException uploadError =
451 new UrlRequestException("Exception received from UploadDataProvi der", e);
452 Log.e(CronetUrlRequestContext.LOG_TAG, "Exception in upload method", e);
453 // Do not call into listener if request is canceled.
454 synchronized (mUrlRequestAdapterLock) {
455 if (isCanceled()) {
456 return;
457 }
458 cancel();
459 }
460 try {
461 mListener.onFailed(this, mResponseInfo, uploadError);
462 } catch (Exception failException) {
463 Log.e(CronetUrlRequestContext.LOG_TAG, "Exception notifying of faile d upload",
464 failException);
465 }
466 }
467
418 //////////////////////////////////////////////// 468 ////////////////////////////////////////////////
419 // Private methods called by the native code. 469 // Private methods called by the native code.
420 // Always called on network thread. 470 // Always called on network thread.
421 //////////////////////////////////////////////// 471 ////////////////////////////////////////////////
422 472
423 /** 473 /**
424 * Called before following redirects. The redirect will automatically be 474 * Called before following redirects. The redirect will automatically be
425 * followed, unless the request is paused or canceled during this 475 * followed, unless the request is paused or canceled during this
426 * callback. If the redirect response has a body, it will be ignored. 476 * callback. If the redirect response has a body, it will be ignored.
427 * This will only be called between start and onResponseStarted. 477 * This will only be called between start and onResponseStarted.
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
614 HeadersList headers); 664 HeadersList headers);
615 665
616 private native String nativeGetNegotiatedProtocol(long urlRequestAdapter); 666 private native String nativeGetNegotiatedProtocol(long urlRequestAdapter);
617 667
618 private native String nativeGetHttpStatusText(long urlRequestAdapter); 668 private native String nativeGetHttpStatusText(long urlRequestAdapter);
619 669
620 private native boolean nativeGetWasCached(long urlRequestAdapter); 670 private native boolean nativeGetWasCached(long urlRequestAdapter);
621 671
622 private native long nativeGetTotalReceivedBytes(long urlRequestAdapter); 672 private native long nativeGetTotalReceivedBytes(long urlRequestAdapter);
623 } 673 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698