| 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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 Loading... |
| 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.equalsIgnoreCase("Content-Type") |
| 290 && !header.second.isEmpty()) { |
| 291 hasContentType = true; |
| 292 } |
| 293 if (!nativeAddHeader(mUrlRequestAdapter, header.first, heade
r.second)) { |
| 294 destroyRequestAdapter(); |
| 295 throw new IllegalArgumentException( |
| 296 "Invalid header " + header.first + "=" + header.
second); |
| 297 } |
| 298 } |
| 299 if (mUploadDataStream != null) { |
| 300 if (!hasContentType) { |
| 301 throw new IllegalArgumentException( |
| 302 "Requests with body must have a Content-Type."); |
| 303 } |
| 304 mUploadDataStream.attachToRequest(this, mUrlRequestAdapter); |
| 305 } |
| 306 } catch (RuntimeException e) { |
| 307 // If there's an exception, cleanup and then throw the |
| 308 // exception to the caller. |
| 309 destroyRequestAdapter(); |
| 310 throw e; |
| 274 } | 311 } |
| 275 for (Pair<String, String> header : mRequestHeaders) { | 312 |
| 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; | 313 mStarted = true; |
| 284 nativeStart(mUrlRequestAdapter); | 314 nativeStart(mUrlRequestAdapter); |
| 285 } | 315 } |
| 286 } | 316 } |
| 287 | 317 |
| 288 @Override | 318 @Override |
| 289 public void cancel() { | 319 public void cancel() { |
| 290 synchronized (mUrlRequestAdapterLock) { | 320 synchronized (mUrlRequestAdapterLock) { |
| 291 if (mCanceled || !mStarted) { | 321 if (mCanceled || !mStarted) { |
| 292 return; | 322 return; |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 cancel(); | 438 cancel(); |
| 409 } | 439 } |
| 410 try { | 440 try { |
| 411 mListener.onFailed(this, mResponseInfo, requestError); | 441 mListener.onFailed(this, mResponseInfo, requestError); |
| 412 } catch (Exception failException) { | 442 } catch (Exception failException) { |
| 413 Log.e(CronetUrlRequestContext.LOG_TAG, | 443 Log.e(CronetUrlRequestContext.LOG_TAG, |
| 414 "Exception notifying of failed request", failException); | 444 "Exception notifying of failed request", failException); |
| 415 } | 445 } |
| 416 } | 446 } |
| 417 | 447 |
| 448 /** |
| 449 * Called when UploadDataProvider encounters an error. |
| 450 */ |
| 451 void onUploadException(Exception e) { |
| 452 UrlRequestException uploadError = |
| 453 new UrlRequestException("Exception received from UploadDataProvi
der", e); |
| 454 Log.e(CronetUrlRequestContext.LOG_TAG, "Exception in upload method", e); |
| 455 // Do not call into listener if request is canceled. |
| 456 synchronized (mUrlRequestAdapterLock) { |
| 457 if (isCanceled()) { |
| 458 return; |
| 459 } |
| 460 cancel(); |
| 461 } |
| 462 try { |
| 463 mListener.onFailed(this, mResponseInfo, uploadError); |
| 464 } catch (Exception failException) { |
| 465 Log.e(CronetUrlRequestContext.LOG_TAG, "Exception notifying of faile
d upload", |
| 466 failException); |
| 467 } |
| 468 } |
| 469 |
| 418 //////////////////////////////////////////////// | 470 //////////////////////////////////////////////// |
| 419 // Private methods called by the native code. | 471 // Private methods called by the native code. |
| 420 // Always called on network thread. | 472 // Always called on network thread. |
| 421 //////////////////////////////////////////////// | 473 //////////////////////////////////////////////// |
| 422 | 474 |
| 423 /** | 475 /** |
| 424 * Called before following redirects. The redirect will automatically be | 476 * Called before following redirects. The redirect will automatically be |
| 425 * followed, unless the request is paused or canceled during this | 477 * followed, unless the request is paused or canceled during this |
| 426 * callback. If the redirect response has a body, it will be ignored. | 478 * callback. If the redirect response has a body, it will be ignored. |
| 427 * This will only be called between start and onResponseStarted. | 479 * This will only be called between start and onResponseStarted. |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 614 HeadersList headers); | 666 HeadersList headers); |
| 615 | 667 |
| 616 private native String nativeGetNegotiatedProtocol(long urlRequestAdapter); | 668 private native String nativeGetNegotiatedProtocol(long urlRequestAdapter); |
| 617 | 669 |
| 618 private native String nativeGetHttpStatusText(long urlRequestAdapter); | 670 private native String nativeGetHttpStatusText(long urlRequestAdapter); |
| 619 | 671 |
| 620 private native boolean nativeGetWasCached(long urlRequestAdapter); | 672 private native boolean nativeGetWasCached(long urlRequestAdapter); |
| 621 | 673 |
| 622 private native long nativeGetTotalReceivedBytes(long urlRequestAdapter); | 674 private native long nativeGetTotalReceivedBytes(long urlRequestAdapter); |
| 623 } | 675 } |
| OLD | NEW |