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

Unified Diff: components/cronet/android/java/src/org/chromium/net/ChromiumUrlRequest.java

Issue 474573003: Catch and report exceptions in CalledByNative java methods. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add test asserts. Created 6 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: components/cronet/android/java/src/org/chromium/net/ChromiumUrlRequest.java
diff --git a/components/cronet/android/java/src/org/chromium/net/ChromiumUrlRequest.java b/components/cronet/android/java/src/org/chromium/net/ChromiumUrlRequest.java
index 02cd530e3e381e5daa0b21cd3b1381018d741ba7..895432c8a5dc1a013c3af07218abb791fd35f9c0 100644
--- a/components/cronet/android/java/src/org/chromium/net/ChromiumUrlRequest.java
+++ b/components/cronet/android/java/src/org/chromium/net/ChromiumUrlRequest.java
@@ -4,6 +4,8 @@
package org.chromium.net;
+import android.util.Log;
+
import org.apache.http.conn.ConnectTimeoutException;
import org.chromium.base.CalledByNative;
import org.chromium.base.JNINamespace;
@@ -404,39 +406,61 @@ public class ChromiumUrlRequest implements HttpUrlRequest {
// Private methods called by native library.
/**
+ * If @CalledByNative method throws an exception, request gets cancelled
+ * and exception could be retrieved from using getException().
+ */
+ private void onCalledByNativeException(Exception e) {
+ mSinkException = new IOException(
+ "CalledByNative method has thrown an exception", e);
mmenke 2014/08/13 21:15:27 Should these continuation lines all use 8-space in
mef 2014/08/13 21:34:40 Done.
+ Log.e(ChromiumUrlRequestContext.LOG_TAG,
+ "Exception in CalledByNative method", e);
+ try {
+ cancel();
mmenke 2014/08/13 21:15:27 Do we tell the consumer when this happens?
mef 2014/08/13 21:34:40 Consumer can check whether request isCanceled() an
mmenke 2014/08/13 22:18:09 Right, but the consumer isn't notified there was a
mef 2014/08/15 14:58:19 Listener's onRequestComplete is getting called whe
+ } catch (Exception cancel_exception) {
+ Log.e(ChromiumUrlRequestContext.LOG_TAG,
+ "Exception trying to cancel request", cancel_exception);
Charles 2014/08/13 21:17:09 I'm not super comfortable with swallowing all exce
mef 2014/08/13 21:34:40 Um, you do check results of request, right? Would
+ }
+ }
+
+ /**
* A callback invoked when the first chunk of the response has arrived.
*/
@CalledByNative
private void onResponseStarted() {
- mContentType = nativeGetContentType(mUrlRequestAdapter);
- mContentLength = nativeGetContentLength(mUrlRequestAdapter);
- mHeadersAvailable = true;
-
- if (mContentLengthLimit > 0 && mContentLength > mContentLengthLimit
- && mCancelIfContentLengthOverLimit) {
- onContentLengthOverLimit();
- return;
- }
+ try {
+ mContentType = nativeGetContentType(mUrlRequestAdapter);
+ mContentLength = nativeGetContentLength(mUrlRequestAdapter);
+ mHeadersAvailable = true;
+
+ if (mContentLengthLimit > 0 &&
+ mContentLength > mContentLengthLimit &&
+ mCancelIfContentLengthOverLimit) {
+ onContentLengthOverLimit();
+ return;
+ }
- if (mBufferFullResponse && mContentLength != -1
- && !mContentLengthOverLimit) {
- ((ChunkedWritableByteChannel)getSink()).setCapacity(
- (int)mContentLength);
- }
+ if (mBufferFullResponse && mContentLength != -1
+ && !mContentLengthOverLimit) {
+ ((ChunkedWritableByteChannel)getSink()).setCapacity(
+ (int)mContentLength);
+ }
- if (mOffset != 0) {
- // The server may ignore the request for a byte range.
- if (getHttpStatusCode() == 200) {
- // TODO(mef): Revisit this logic.
- if (mContentLength != -1) {
- mContentLength -= mOffset;
+ if (mOffset != 0) {
+ // The server may ignore the request for a byte range.
+ if (getHttpStatusCode() == 200) {
+ // TODO(mef): Revisit this logic.
+ if (mContentLength != -1) {
+ mContentLength -= mOffset;
+ }
+ mSkippingToOffset = true;
+ } else {
+ mSize = mOffset;
}
- mSkippingToOffset = true;
- } else {
- mSize = mOffset;
}
+ mListener.onResponseStarted(this);
+ } catch (Exception e) {
+ onCalledByNativeException(e);
}
- mListener.onResponseStarted(this);
}
/**
@@ -448,37 +472,41 @@ public class ChromiumUrlRequest implements HttpUrlRequest {
*/
@CalledByNative
private void onBytesRead(ByteBuffer buffer) {
- if (mContentLengthOverLimit) {
- return;
- }
-
- int size = buffer.remaining();
- mSize += size;
- if (mSkippingToOffset) {
- if (mSize <= mOffset) {
+ try {
+ if (mContentLengthOverLimit) {
return;
- } else {
- mSkippingToOffset = false;
- buffer.position((int)(mOffset - (mSize - size)));
}
- }
- boolean contentLengthOverLimit =
- (mContentLengthLimit != 0 && mSize > mContentLengthLimit);
- if (contentLengthOverLimit) {
- buffer.limit(size - (int)(mSize - mContentLengthLimit));
- }
+ int size = buffer.remaining();
+ mSize += size;
+ if (mSkippingToOffset) {
+ if (mSize <= mOffset) {
+ return;
+ } else {
+ mSkippingToOffset = false;
+ buffer.position((int)(mOffset - (mSize - size)));
+ }
+ }
- try {
- while (buffer.hasRemaining()) {
- mSink.write(buffer);
+ boolean contentLengthOverLimit =
+ (mContentLengthLimit != 0 && mSize > mContentLengthLimit);
+ if (contentLengthOverLimit) {
+ buffer.limit(size - (int)(mSize - mContentLengthLimit));
}
- } catch (IOException e) {
- mSinkException = e;
- cancel();
- }
- if (contentLengthOverLimit) {
- onContentLengthOverLimit();
+
+ try {
+ while (buffer.hasRemaining()) {
+ mSink.write(buffer);
+ }
+ } catch (IOException e) {
+ mSinkException = e;
+ cancel();
+ }
+ if (contentLengthOverLimit) {
+ onContentLengthOverLimit();
+ }
+ } catch (Exception e) {
+ onCalledByNativeException(e);
}
}
@@ -488,21 +516,25 @@ public class ChromiumUrlRequest implements HttpUrlRequest {
@SuppressWarnings("unused")
@CalledByNative
private void finish() {
- synchronized (mLock) {
- mFinished = true;
+ try {
+ synchronized (mLock) {
+ mFinished = true;
- if (mRecycled) {
- return;
- }
- try {
- mSink.close();
- } catch (IOException e) {
- // Ignore
+ if (mRecycled) {
+ return;
+ }
+ try {
+ mSink.close();
+ } catch (IOException e) {
+ // Ignore
+ }
+ onRequestComplete();
+ nativeDestroyRequestAdapter(mUrlRequestAdapter);
+ mUrlRequestAdapter = 0;
+ mRecycled = true;
}
- onRequestComplete();
- nativeDestroyRequestAdapter(mUrlRequestAdapter);
- mUrlRequestAdapter = 0;
- mRecycled = true;
+ } catch (Exception e) {
+ onCalledByNativeException(e);
}
}
@@ -513,10 +545,14 @@ public class ChromiumUrlRequest implements HttpUrlRequest {
@CalledByNative
private void onAppendResponseHeader(ResponseHeadersMap headersMap,
String name, String value) {
- if (!headersMap.containsKey(name)) {
- headersMap.put(name, new ArrayList<String>());
+ try {
+ if (!headersMap.containsKey(name)) {
+ headersMap.put(name, new ArrayList<String>());
+ }
+ headersMap.get(name).add(value);
+ } catch (Exception e) {
+ onCalledByNativeException(e);
}
- headersMap.get(name).add(value);
}
/**
@@ -528,25 +564,29 @@ public class ChromiumUrlRequest implements HttpUrlRequest {
@SuppressWarnings("unused")
@CalledByNative
private int readFromUploadChannel(ByteBuffer dest) {
- if (mUploadChannel == null || !mUploadChannel.isOpen())
- return -1;
try {
- int result = mUploadChannel.read(dest);
- if (result < 0) {
- mUploadChannel.close();
- return 0;
- }
- return result;
- } catch (IOException e) {
- mSinkException = e;
+ if (mUploadChannel == null || !mUploadChannel.isOpen())
+ return -1;
try {
- mUploadChannel.close();
- } catch (IOException ignored) {
- // Ignore this exception.
+ int result = mUploadChannel.read(dest);
+ if (result < 0) {
+ mUploadChannel.close();
+ return 0;
+ }
+ return result;
+ } catch (IOException e) {
+ mSinkException = e;
+ try {
+ mUploadChannel.close();
+ } catch (IOException ignored) {
+ // Ignore this exception.
+ }
+ cancel();
}
- cancel();
- return -1;
+ } catch (Exception e) {
+ onCalledByNativeException(e);
}
+ return -1;
}
// Native methods are implemented in chromium_url_request.cc.

Powered by Google App Engine
This is Rietveld 408576698