Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.annotation.TargetApi; | 7 import android.annotation.TargetApi; |
| 8 import android.net.TrafficStats; | 8 import android.net.TrafficStats; |
| 9 import android.os.Build; | 9 import android.os.Build; |
| 10 import android.util.Log; | 10 import android.util.Log; |
| (...skipping 638 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 649 } | 649 } |
| 650 })); | 650 })); |
| 651 } | 651 } |
| 652 }); | 652 }); |
| 653 } | 653 } |
| 654 | 654 |
| 655 private void processReadResult(int read, final ByteBuffer buffer) throws IOE xception { | 655 private void processReadResult(int read, final ByteBuffer buffer) throws IOE xception { |
| 656 if (read != -1) { | 656 if (read != -1) { |
| 657 mCallbackAsync.onReadCompleted(mUrlResponseInfo, buffer); | 657 mCallbackAsync.onReadCompleted(mUrlResponseInfo, buffer); |
| 658 } else { | 658 } else { |
| 659 mResponseChannel.close(); | 659 mResponseChannel.close(); |
|
mef
2016/08/26 16:14:11
Do we need to closeResponseChannel() here as well?
Charles
2016/08/26 18:15:51
No, processReadResult is always called on mExecuto
| |
| 660 if (mState.compareAndSet(State.READING, State.COMPLETE)) { | 660 if (mState.compareAndSet(State.READING, State.COMPLETE)) { |
| 661 fireDisconnect(); | 661 fireDisconnect(); |
| 662 mCallbackAsync.onSucceeded(mUrlResponseInfo); | 662 mCallbackAsync.onSucceeded(mUrlResponseInfo); |
| 663 } | 663 } |
| 664 } | 664 } |
| 665 } | 665 } |
| 666 | 666 |
| 667 private void fireDisconnect() { | 667 private void fireDisconnect() { |
| 668 final HttpURLConnection connection = mCurrentUrlConnection.getAndSet(nul l); | 668 final HttpURLConnection connection = mCurrentUrlConnection.getAndSet(nul l); |
| 669 if (connection != null) { | 669 if (connection != null) { |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 792 @Override | 792 @Override |
| 793 public void run() throws Exception { | 793 public void run() throws Exception { |
| 794 if (mState.compareAndSet(State.READING, State.AWAITING_READ) ) { | 794 if (mState.compareAndSet(State.READING, State.AWAITING_READ) ) { |
| 795 mCallback.onReadCompleted(JavaUrlRequest.this, info, byt eBuffer); | 795 mCallback.onReadCompleted(JavaUrlRequest.this, info, byt eBuffer); |
| 796 } | 796 } |
| 797 } | 797 } |
| 798 }); | 798 }); |
| 799 } | 799 } |
| 800 | 800 |
| 801 void onCanceled(final UrlResponseInfo info) { | 801 void onCanceled(final UrlResponseInfo info) { |
| 802 closeQuietly(mResponseChannel); | 802 closeResponseChannel(); |
|
mef
2016/08/25 22:23:54
would it make sense to just call closeQuietly from
Charles
2016/08/26 15:58:19
No, because then we'd be doing I/O on the user's e
mef
2016/08/26 16:14:11
Oh, I see. I've missed that those are different ex
| |
| 803 mUserExecutor.execute(new Runnable() { | 803 mUserExecutor.execute(new Runnable() { |
| 804 @Override | 804 @Override |
| 805 public void run() { | 805 public void run() { |
| 806 try { | 806 try { |
| 807 mCallback.onCanceled(JavaUrlRequest.this, info); | 807 mCallback.onCanceled(JavaUrlRequest.this, info); |
| 808 } catch (Exception exception) { | 808 } catch (Exception exception) { |
| 809 Log.e(TAG, "Exception in onCanceled method", exception); | 809 Log.e(TAG, "Exception in onCanceled method", exception); |
| 810 } | 810 } |
| 811 } | 811 } |
| 812 }); | 812 }); |
| 813 } | 813 } |
| 814 | 814 |
| 815 void onSucceeded(final UrlResponseInfo info) { | 815 void onSucceeded(final UrlResponseInfo info) { |
| 816 mUserExecutor.execute(new Runnable() { | 816 mUserExecutor.execute(new Runnable() { |
| 817 @Override | 817 @Override |
| 818 public void run() { | 818 public void run() { |
| 819 try { | 819 try { |
| 820 mCallback.onSucceeded(JavaUrlRequest.this, info); | 820 mCallback.onSucceeded(JavaUrlRequest.this, info); |
| 821 } catch (Exception exception) { | 821 } catch (Exception exception) { |
| 822 Log.e(TAG, "Exception in onSucceeded method", exception) ; | 822 Log.e(TAG, "Exception in onSucceeded method", exception) ; |
| 823 } | 823 } |
| 824 } | 824 } |
| 825 }); | 825 }); |
| 826 } | 826 } |
| 827 | 827 |
| 828 void onFailed(final UrlResponseInfo urlResponseInfo, final UrlRequestExc eption e) { | 828 void onFailed(final UrlResponseInfo urlResponseInfo, final UrlRequestExc eption e) { |
| 829 closeQuietly(mResponseChannel); | 829 closeResponseChannel(); |
| 830 mUserExecutor.execute(new Runnable() { | 830 mUserExecutor.execute(new Runnable() { |
| 831 @Override | 831 @Override |
| 832 public void run() { | 832 public void run() { |
| 833 try { | 833 try { |
| 834 mCallback.onFailed(JavaUrlRequest.this, urlResponseInfo, e); | 834 mCallback.onFailed(JavaUrlRequest.this, urlResponseInfo, e); |
| 835 } catch (Exception exception) { | 835 } catch (Exception exception) { |
| 836 Log.e(TAG, "Exception in onFailed method", exception); | 836 Log.e(TAG, "Exception in onFailed method", exception); |
| 837 } | 837 } |
| 838 } | 838 } |
| 839 }); | 839 }); |
| 840 } | 840 } |
| 841 } | 841 } |
| 842 | 842 |
| 843 private static void closeQuietly(Closeable closeable) { | 843 private void closeResponseChannel() { |
| 844 final Closeable closeable = mResponseChannel; | |
| 844 if (closeable == null) { | 845 if (closeable == null) { |
| 845 return; | 846 return; |
| 846 } | 847 } |
| 847 try { | 848 mResponseChannel = null; |
| 848 closeable.close(); | 849 mExecutor.execute(new Runnable() { |
|
mef
2016/08/26 16:14:11
what if mExecutor rejects execution?
Charles
2016/08/26 18:15:51
It can't, we use an unbounded thread pool that's n
| |
| 849 } catch (IOException e) { | 850 @Override |
| 850 e.printStackTrace(); | 851 public void run() { |
| 851 } | 852 try { |
| 853 closeable.close(); | |
| 854 } catch (IOException e) { | |
| 855 e.printStackTrace(); | |
| 856 } | |
| 857 } | |
| 858 }); | |
| 852 } | 859 } |
| 853 } | 860 } |
| OLD | NEW |