| OLD | NEW |
| 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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 * decided a redirect will be followed. | 50 * decided a redirect will be followed. |
| 51 */ | 51 */ |
| 52 private final List<String> mUrlChain = new ArrayList<String>(); | 52 private final List<String> mUrlChain = new ArrayList<String>(); |
| 53 | 53 |
| 54 private final UrlRequestListener mListener; | 54 private final UrlRequestListener mListener; |
| 55 private final String mInitialUrl; | 55 private final String mInitialUrl; |
| 56 private final int mPriority; | 56 private final int mPriority; |
| 57 private String mInitialMethod; | 57 private String mInitialMethod; |
| 58 private final HeadersList mRequestHeaders = new HeadersList(); | 58 private final HeadersList mRequestHeaders = new HeadersList(); |
| 59 | 59 |
| 60 private CronetUploadDataStream mUploadDataStream; |
| 61 |
| 60 private NativeResponseInfo mResponseInfo; | 62 private NativeResponseInfo mResponseInfo; |
| 61 | 63 |
| 62 /* | 64 /* |
| 63 * Listener callback is repeatedly called when data is received, so it is | 65 * Listener callback is repeatedly called when data is received, so it is |
| 64 * cached as member variable. | 66 * cached as member variable. |
| 65 */ | 67 */ |
| 66 private OnDataReceivedRunnable mOnDataReceivedTask; | 68 private OnDataReceivedRunnable mOnDataReceivedTask; |
| 67 | 69 |
| 68 static final class HeadersList extends ArrayList<Pair<String, String>> { | 70 static final class HeadersList extends ArrayList<Pair<String, String>> { |
| 69 } | 71 } |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 if (header == null) { | 253 if (header == null) { |
| 252 throw new NullPointerException("Invalid header name."); | 254 throw new NullPointerException("Invalid header name."); |
| 253 } | 255 } |
| 254 if (value == null) { | 256 if (value == null) { |
| 255 throw new NullPointerException("Invalid header value."); | 257 throw new NullPointerException("Invalid header value."); |
| 256 } | 258 } |
| 257 mRequestHeaders.add(Pair.create(header, value)); | 259 mRequestHeaders.add(Pair.create(header, value)); |
| 258 } | 260 } |
| 259 | 261 |
| 260 @Override | 262 @Override |
| 263 public void setUploadDataProvider(UploadDataProvider uploadDataProvider, Exe
cutor executor) { |
| 264 if (uploadDataProvider == null) { |
| 265 throw new NullPointerException("Invalid UploadDataProvider."); |
| 266 } |
| 267 if (mInitialMethod == null) { |
| 268 mInitialMethod = "POST"; |
| 269 } |
| 270 mUploadDataStream = new CronetUploadDataStream(uploadDataProvider, execu
tor); |
| 271 } |
| 272 |
| 273 @Override |
| 261 public void start() { | 274 public void start() { |
| 262 synchronized (mUrlRequestAdapterLock) { | 275 synchronized (mUrlRequestAdapterLock) { |
| 263 checkNotStarted(); | 276 checkNotStarted(); |
| 264 mUrlRequestAdapter = nativeCreateRequestAdapter( | 277 |
| 265 mRequestContext.getUrlRequestContextAdapter(), | 278 try { |
| 266 mInitialUrl, | 279 mUrlRequestAdapter = nativeCreateRequestAdapter( |
| 267 mPriority); | 280 mRequestContext.getUrlRequestContextAdapter(), mInitialU
rl, mPriority); |
| 268 mRequestContext.onRequestStarted(this); | 281 mRequestContext.onRequestStarted(this); |
| 269 if (mInitialMethod != null) { | 282 if (mInitialMethod != null) { |
| 270 if (!nativeSetHttpMethod(mUrlRequestAdapter, mInitialMethod)) { | 283 if (!nativeSetHttpMethod(mUrlRequestAdapter, mInitialMethod)
) { |
| 271 destroyRequestAdapter(); | 284 throw new IllegalArgumentException("Invalid http method
" + mInitialMethod); |
| 272 throw new IllegalArgumentException("Invalid http method " | 285 } |
| 273 + mInitialMethod); | |
| 274 } | 286 } |
| 275 } | 287 |
| 276 for (Pair<String, String> header : mRequestHeaders) { | 288 boolean hasContentType = false; |
| 277 if (!nativeAddHeader(mUrlRequestAdapter, header.first, | 289 for (Pair<String, String> header : mRequestHeaders) { |
| 278 header.second)) { | 290 if (header.first.equalsIgnoreCase("Content-Type") |
| 279 destroyRequestAdapter(); | 291 && !header.second.isEmpty()) { |
| 280 throw new IllegalArgumentException("Invalid header " | 292 hasContentType = true; |
| 281 + header.first + "=" + header.second); | 293 } |
| 294 if (!nativeAddHeader(mUrlRequestAdapter, header.first, heade
r.second)) { |
| 295 destroyRequestAdapter(); |
| 296 throw new IllegalArgumentException( |
| 297 "Invalid header " + header.first + "=" + header.
second); |
| 298 } |
| 282 } | 299 } |
| 300 if (mUploadDataStream != null) { |
| 301 if (!hasContentType) { |
| 302 throw new IllegalArgumentException( |
| 303 "Requests with upload data must have a Content-T
ype."); |
| 304 } |
| 305 mUploadDataStream.attachToRequest(this, mUrlRequestAdapter); |
| 306 } |
| 307 } catch (RuntimeException e) { |
| 308 // If there's an exception, cleanup and then throw the |
| 309 // exception to the caller. |
| 310 destroyRequestAdapter(); |
| 311 throw e; |
| 283 } | 312 } |
| 284 if (mDisableCache) { | 313 if (mDisableCache) { |
| 285 nativeDisableCache(mUrlRequestAdapter); | 314 nativeDisableCache(mUrlRequestAdapter); |
| 286 } | 315 } |
| 287 mStarted = true; | 316 mStarted = true; |
| 288 nativeStart(mUrlRequestAdapter); | 317 nativeStart(mUrlRequestAdapter); |
| 289 } | 318 } |
| 290 } | 319 } |
| 291 | 320 |
| 292 @Override | 321 @Override |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 cancel(); | 447 cancel(); |
| 419 } | 448 } |
| 420 try { | 449 try { |
| 421 mListener.onFailed(this, mResponseInfo, requestError); | 450 mListener.onFailed(this, mResponseInfo, requestError); |
| 422 } catch (Exception failException) { | 451 } catch (Exception failException) { |
| 423 Log.e(CronetUrlRequestContext.LOG_TAG, | 452 Log.e(CronetUrlRequestContext.LOG_TAG, |
| 424 "Exception notifying of failed request", failException); | 453 "Exception notifying of failed request", failException); |
| 425 } | 454 } |
| 426 } | 455 } |
| 427 | 456 |
| 457 /** |
| 458 * Called when UploadDataProvider encounters an error. |
| 459 */ |
| 460 void onUploadException(Exception e) { |
| 461 UrlRequestException uploadError = |
| 462 new UrlRequestException("Exception received from UploadDataProvi
der", e); |
| 463 Log.e(CronetUrlRequestContext.LOG_TAG, "Exception in upload method", e); |
| 464 // Do not call into listener if request is canceled. |
| 465 synchronized (mUrlRequestAdapterLock) { |
| 466 if (isCanceled()) { |
| 467 return; |
| 468 } |
| 469 cancel(); |
| 470 } |
| 471 try { |
| 472 mListener.onFailed(this, mResponseInfo, uploadError); |
| 473 } catch (Exception failException) { |
| 474 Log.e(CronetUrlRequestContext.LOG_TAG, "Exception notifying of faile
d upload", |
| 475 failException); |
| 476 } |
| 477 } |
| 478 |
| 428 //////////////////////////////////////////////// | 479 //////////////////////////////////////////////// |
| 429 // Private methods called by the native code. | 480 // Private methods called by the native code. |
| 430 // Always called on network thread. | 481 // Always called on network thread. |
| 431 //////////////////////////////////////////////// | 482 //////////////////////////////////////////////// |
| 432 | 483 |
| 433 /** | 484 /** |
| 434 * Called before following redirects. The redirect will automatically be | 485 * Called before following redirects. The redirect will automatically be |
| 435 * followed, unless the request is paused or canceled during this | 486 * followed, unless the request is paused or canceled during this |
| 436 * callback. If the redirect response has a body, it will be ignored. | 487 * callback. If the redirect response has a body, it will be ignored. |
| 437 * This will only be called between start and onResponseStarted. | 488 * This will only be called between start and onResponseStarted. |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 626 private native String nativeGetNegotiatedProtocol(long urlRequestAdapter); | 677 private native String nativeGetNegotiatedProtocol(long urlRequestAdapter); |
| 627 | 678 |
| 628 private native String nativeGetHttpStatusText(long urlRequestAdapter); | 679 private native String nativeGetHttpStatusText(long urlRequestAdapter); |
| 629 | 680 |
| 630 private native boolean nativeGetWasCached(long urlRequestAdapter); | 681 private native boolean nativeGetWasCached(long urlRequestAdapter); |
| 631 | 682 |
| 632 private native long nativeGetTotalReceivedBytes(long urlRequestAdapter); | 683 private native long nativeGetTotalReceivedBytes(long urlRequestAdapter); |
| 633 | 684 |
| 634 private native void nativeDisableCache(long urlRequestAdapter); | 685 private native void nativeDisableCache(long urlRequestAdapter); |
| 635 } | 686 } |
| OLD | NEW |