Chromium Code Reviews| 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; | |
| 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 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 394 | 396 |
| 395 private void validateHeadersAvailable() { | 397 private void validateHeadersAvailable() { |
| 396 if (!mHeadersAvailable) { | 398 if (!mHeadersAvailable) { |
| 397 throw new IllegalStateException("Response headers not available"); | 399 throw new IllegalStateException("Response headers not available"); |
| 398 } | 400 } |
| 399 } | 401 } |
| 400 | 402 |
| 401 // Private methods called by native library. | 403 // Private methods called by native library. |
| 402 | 404 |
| 403 /** | 405 /** |
| 406 * If @CalledByNative method throws an exception, request gets cancelled | |
| 407 * and exception could be retrieved from using getException(). | |
| 408 */ | |
| 409 private void onCalledByNativeException(Exception e) { | |
| 410 mSinkException = new IOException( | |
| 411 "CalledByNative method has thrown an exception", e); | |
| 412 Log.e(ChromiumUrlRequestContext.LOG_TAG, | |
| 413 "Exception in CalledByNative method", e); | |
| 414 try { | |
| 415 cancel(); | |
| 416 } catch (Exception cancel_exception) { | |
| 417 Log.e(ChromiumUrlRequestContext.LOG_TAG, | |
| 418 "Exception trying to cancel request", cancel_exception); | |
| 419 } | |
| 420 } | |
| 421 | |
| 422 /** | |
| 404 * A callback invoked when the first chunk of the response has arrived. | 423 * A callback invoked when the first chunk of the response has arrived. |
| 405 */ | 424 */ |
| 406 @CalledByNative | 425 @CalledByNative |
| 407 private void onResponseStarted() { | 426 private void onResponseStarted() { |
| 408 mContentType = nativeGetContentType(mUrlRequestAdapter); | 427 try { |
| 409 mContentLength = nativeGetContentLength(mUrlRequestAdapter); | 428 mContentType = nativeGetContentType(mUrlRequestAdapter); |
| 410 mHeadersAvailable = true; | 429 mContentLength = nativeGetContentLength(mUrlRequestAdapter); |
| 430 mHeadersAvailable = true; | |
| 411 | 431 |
| 412 if (mContentLengthLimit > 0 && mContentLength > mContentLengthLimit | 432 if (mContentLengthLimit > 0 && |
| 413 && mCancelIfContentLengthOverLimit) { | 433 mContentLength > mContentLengthLimit && |
| 414 onContentLengthOverLimit(); | 434 mCancelIfContentLengthOverLimit) { |
| 415 return; | 435 onContentLengthOverLimit(); |
| 436 return; | |
| 437 } | |
| 438 | |
| 439 if (mBufferFullResponse && mContentLength != -1 | |
| 440 && !mContentLengthOverLimit) { | |
| 441 ((ChunkedWritableByteChannel)getSink()).setCapacity( | |
| 442 (int)mContentLength); | |
| 443 } | |
| 444 | |
| 445 if (mOffset != 0) { | |
| 446 // The server may ignore the request for a byte range, in which case | |
| 447 // status code will be 200, instead of 206. Note that we cannot call | |
| 448 // getHttpStatusCode as it rewrites 206 into 200. | |
| 449 if (nativeGetHttpStatusCode(mUrlRequestAdapter) == 200) { | |
| 450 // TODO(mef): Revisit this logic. | |
| 451 if (mContentLength != -1) { | |
| 452 mContentLength -= mOffset; | |
| 453 } | |
| 454 mSkippingToOffset = true; | |
| 455 } else { | |
| 456 mSize = mOffset; | |
| 457 } | |
| 458 } | |
| 459 mListener.onResponseStarted(this); | |
| 460 } catch (Exception e) { | |
| 461 onCalledByNativeException(e); | |
| 416 } | 462 } |
| 417 | |
| 418 if (mBufferFullResponse && mContentLength != -1 | |
| 419 && !mContentLengthOverLimit) { | |
| 420 ((ChunkedWritableByteChannel)getSink()).setCapacity( | |
| 421 (int)mContentLength); | |
| 422 } | |
| 423 | |
| 424 if (mOffset != 0) { | |
| 425 // The server may ignore the request for a byte range, in which case | |
| 426 // status code will be 200, instead of 206. Note that we cannot call | |
| 427 // getHttpStatusCode as it rewrites 206 into 200. | |
| 428 if (nativeGetHttpStatusCode(mUrlRequestAdapter) == 200) { | |
| 429 // TODO(mef): Revisit this logic. | |
| 430 if (mContentLength != -1) { | |
| 431 mContentLength -= mOffset; | |
| 432 } | |
| 433 mSkippingToOffset = true; | |
| 434 } else { | |
| 435 mSize = mOffset; | |
| 436 } | |
| 437 } | |
| 438 mListener.onResponseStarted(this); | |
| 439 } | 463 } |
| 440 | 464 |
| 441 /** | 465 /** |
| 442 * Consumes a portion of the response. | 466 * Consumes a portion of the response. |
| 443 * | 467 * |
| 444 * @param byteBuffer The ByteBuffer to append. Must be a direct buffer, and | 468 * @param byteBuffer The ByteBuffer to append. Must be a direct buffer, and |
| 445 * no references to it may be retained after the method ends, as | 469 * no references to it may be retained after the method ends, as |
| 446 * it wraps code managed on the native heap. | 470 * it wraps code managed on the native heap. |
| 447 */ | 471 */ |
| 448 @CalledByNative | 472 @CalledByNative |
| 449 private void onBytesRead(ByteBuffer buffer) { | 473 private void onBytesRead(ByteBuffer buffer) { |
| 450 if (mContentLengthOverLimit) { | 474 try { |
| 451 return; | 475 if (mContentLengthOverLimit) { |
| 452 } | 476 return; |
| 477 } | |
| 453 | 478 |
| 454 int size = buffer.remaining(); | 479 int size = buffer.remaining(); |
| 455 mSize += size; | 480 mSize += size; |
| 456 if (mSkippingToOffset) { | 481 if (mSkippingToOffset) { |
| 457 if (mSize <= mOffset) { | 482 if (mSize <= mOffset) { |
| 458 return; | 483 return; |
| 459 } else { | 484 } else { |
| 460 mSkippingToOffset = false; | 485 mSkippingToOffset = false; |
| 461 buffer.position((int)(mOffset - (mSize - size))); | 486 buffer.position((int)(mOffset - (mSize - size))); |
| 487 } | |
| 462 } | 488 } |
| 463 } | |
| 464 | 489 |
| 465 boolean contentLengthOverLimit = | 490 boolean contentLengthOverLimit = |
| 466 (mContentLengthLimit != 0 && mSize > mContentLengthLimit); | 491 (mContentLengthLimit != 0 && mSize > mContentLengthLimit); |
| 467 if (contentLengthOverLimit) { | 492 if (contentLengthOverLimit) { |
| 468 buffer.limit(size - (int)(mSize - mContentLengthLimit)); | 493 buffer.limit(size - (int)(mSize - mContentLengthLimit)); |
| 469 } | 494 } |
| 470 | 495 |
| 471 try { | 496 try { |
| 472 while (buffer.hasRemaining()) { | 497 while (buffer.hasRemaining()) { |
| 473 mSink.write(buffer); | 498 mSink.write(buffer); |
| 499 } | |
| 500 } catch (IOException e) { | |
| 501 mSinkException = e; | |
|
mmenke
2014/08/15 15:23:28
I don't think we need to handle these exceptions d
mef
2014/08/15 16:03:35
Done.
| |
| 502 cancel(); | |
| 474 } | 503 } |
| 475 } catch (IOException e) { | 504 if (contentLengthOverLimit) { |
| 476 mSinkException = e; | 505 onContentLengthOverLimit(); |
| 477 cancel(); | 506 } |
| 478 } | 507 } catch (Exception e) { |
| 479 if (contentLengthOverLimit) { | 508 onCalledByNativeException(e); |
| 480 onContentLengthOverLimit(); | |
| 481 } | 509 } |
| 482 } | 510 } |
| 483 | 511 |
| 484 /** | 512 /** |
| 485 * Notifies the listener, releases native data structures. | 513 * Notifies the listener, releases native data structures. |
| 486 */ | 514 */ |
| 487 @SuppressWarnings("unused") | 515 @SuppressWarnings("unused") |
| 488 @CalledByNative | 516 @CalledByNative |
| 489 private void finish() { | 517 private void finish() { |
| 490 synchronized (mLock) { | 518 try { |
| 491 mFinished = true; | 519 synchronized (mLock) { |
| 520 mFinished = true; | |
| 492 | 521 |
| 493 if (mRecycled) { | 522 if (mRecycled) { |
| 494 return; | 523 return; |
| 524 } | |
| 525 try { | |
| 526 mSink.close(); | |
| 527 } catch (IOException e) { | |
| 528 // Ignore | |
| 529 } | |
| 530 onRequestComplete(); | |
| 531 nativeDestroyRequestAdapter(mUrlRequestAdapter); | |
| 532 mUrlRequestAdapter = 0; | |
| 533 mRecycled = true; | |
| 495 } | 534 } |
| 496 try { | 535 } catch (Exception e) { |
| 497 mSink.close(); | 536 onCalledByNativeException(e); |
| 498 } catch (IOException e) { | |
| 499 // Ignore | |
| 500 } | |
| 501 onRequestComplete(); | |
| 502 nativeDestroyRequestAdapter(mUrlRequestAdapter); | |
| 503 mUrlRequestAdapter = 0; | |
| 504 mRecycled = true; | |
| 505 } | 537 } |
| 506 } | 538 } |
| 507 | 539 |
| 508 /** | 540 /** |
| 509 * Appends header |name| with value |value| to |headersMap|. | 541 * Appends header |name| with value |value| to |headersMap|. |
| 510 */ | 542 */ |
| 511 @SuppressWarnings("unused") | 543 @SuppressWarnings("unused") |
| 512 @CalledByNative | 544 @CalledByNative |
| 513 private void onAppendResponseHeader(ResponseHeadersMap headersMap, | 545 private void onAppendResponseHeader(ResponseHeadersMap headersMap, |
| 514 String name, String value) { | 546 String name, String value) { |
| 515 if (!headersMap.containsKey(name)) { | 547 try { |
| 516 headersMap.put(name, new ArrayList<String>()); | 548 if (!headersMap.containsKey(name)) { |
| 549 headersMap.put(name, new ArrayList<String>()); | |
| 550 } | |
| 551 headersMap.get(name).add(value); | |
| 552 } catch (Exception e) { | |
| 553 onCalledByNativeException(e); | |
| 517 } | 554 } |
| 518 headersMap.get(name).add(value); | |
| 519 } | 555 } |
| 520 | 556 |
| 521 /** | 557 /** |
| 522 * Reads a sequence of bytes from upload channel into the given buffer. | 558 * Reads a sequence of bytes from upload channel into the given buffer. |
| 523 * @param dest The buffer into which bytes are to be transferred. | 559 * @param dest The buffer into which bytes are to be transferred. |
| 524 * @return Returns number of bytes read (could be 0) or -1 and closes | 560 * @return Returns number of bytes read (could be 0) or -1 and closes |
| 525 * the channel if error occured. | 561 * the channel if error occured. |
| 526 */ | 562 */ |
| 527 @SuppressWarnings("unused") | 563 @SuppressWarnings("unused") |
| 528 @CalledByNative | 564 @CalledByNative |
| 529 private int readFromUploadChannel(ByteBuffer dest) { | 565 private int readFromUploadChannel(ByteBuffer dest) { |
| 530 if (mUploadChannel == null || !mUploadChannel.isOpen()) | |
| 531 return -1; | |
| 532 try { | 566 try { |
| 533 int result = mUploadChannel.read(dest); | 567 if (mUploadChannel == null || !mUploadChannel.isOpen()) |
| 534 if (result < 0) { | 568 return -1; |
| 535 mUploadChannel.close(); | 569 try { |
| 536 return 0; | 570 int result = mUploadChannel.read(dest); |
| 571 if (result < 0) { | |
| 572 mUploadChannel.close(); | |
| 573 return 0; | |
| 574 } | |
| 575 return result; | |
| 576 } catch (IOException e) { | |
|
mmenke
2014/08/15 15:23:28
We should merge this exception logic, right?
mef
2014/08/15 16:03:35
Done.
| |
| 577 mSinkException = e; | |
| 578 try { | |
| 579 mUploadChannel.close(); | |
| 580 } catch (IOException ignored) { | |
| 581 // Ignore this exception. | |
| 582 } | |
| 583 cancel(); | |
| 537 } | 584 } |
| 538 return result; | 585 } catch (Exception e) { |
| 539 } catch (IOException e) { | 586 onCalledByNativeException(e); |
| 540 mSinkException = e; | |
| 541 try { | |
| 542 mUploadChannel.close(); | |
| 543 } catch (IOException ignored) { | |
| 544 // Ignore this exception. | |
| 545 } | |
| 546 cancel(); | |
| 547 return -1; | |
| 548 } | 587 } |
| 588 return -1; | |
| 549 } | 589 } |
| 550 | 590 |
| 551 // Native methods are implemented in chromium_url_request.cc. | 591 // Native methods are implemented in chromium_url_request.cc. |
| 552 | 592 |
| 553 private native long nativeCreateRequestAdapter( | 593 private native long nativeCreateRequestAdapter( |
| 554 long ChromiumUrlRequestContextAdapter, String url, int priority); | 594 long ChromiumUrlRequestContextAdapter, String url, int priority); |
| 555 | 595 |
| 556 private native void nativeAddHeader(long urlRequestAdapter, String name, | 596 private native void nativeAddHeader(long urlRequestAdapter, String name, |
| 557 String value); | 597 String value); |
| 558 | 598 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 582 | 622 |
| 583 private native String nativeGetHeader(long urlRequestAdapter, String name); | 623 private native String nativeGetHeader(long urlRequestAdapter, String name); |
| 584 | 624 |
| 585 private native void nativeGetAllHeaders(long urlRequestAdapter, | 625 private native void nativeGetAllHeaders(long urlRequestAdapter, |
| 586 ResponseHeadersMap headers); | 626 ResponseHeadersMap headers); |
| 587 | 627 |
| 588 // Explicit class to work around JNI-generator generics confusion. | 628 // Explicit class to work around JNI-generator generics confusion. |
| 589 private class ResponseHeadersMap extends HashMap<String, List<String>> { | 629 private class ResponseHeadersMap extends HashMap<String, List<String>> { |
| 590 } | 630 } |
| 591 } | 631 } |
| OLD | NEW |