Commit bf86f344 authored by Jeremy Roman's avatar Jeremy Roman Committed by Commit Bot

[remoteobjects] Indicate to the caller when the target method threw an exception.

If so, the exception stack trace is printed (this is identical to JNI
ExceptionDescribe, which also looks up and calls printStackTrace). Exceptions
which indicate a bug in the browser-side implementation are rethrown as runtime
exceptions.

Bug: 794320
Change-Id: Ib33a17aae5e5a77cf45c24e055bfb6a097094324
Reviewed-on: https://chromium-review.googlesource.com/893542Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarBo <boliu@chromium.org>
Commit-Queue: Jeremy Roman <jbroman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#533074}
parent d5bd9807
...@@ -12,6 +12,7 @@ import org.chromium.mojo.system.MojoException; ...@@ -12,6 +12,7 @@ import org.chromium.mojo.system.MojoException;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -133,9 +134,25 @@ class RemoteObjectImpl implements RemoteObject { ...@@ -133,9 +134,25 @@ class RemoteObjectImpl implements RemoteObject {
Object result = null; Object result = null;
try { try {
result = method.invoke(target, args); result = method.invoke(target, args);
} catch (Exception e) { } catch (IllegalAccessException | IllegalArgumentException | NullPointerException e) {
// TODO(jbroman): Handle this. (IllegalAccessException and ones thrown by the method // These should never happen.
// internally.) //
// IllegalAccessException:
// java.lang.Class#getMethods returns only public members, so |mMethods| should never
// contain any method for which IllegalAccessException would be thrown.
//
// IllegalArgumentException:
// Argument coercion logic is responsible for creating objects of a suitable Java
// type.
// TODO(jbroman): Actually write said coercion logic.
//
// NullPointerException:
// A user of this class is responsible for ensuring that the target is not collected.
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
e.getCause().printStackTrace();
callback.call(makeErrorResult(RemoteInvocationError.EXCEPTION_THROWN));
return;
} }
RemoteInvocationResult mojoResult = convertResult(result); RemoteInvocationResult mojoResult = convertResult(result);
......
...@@ -273,6 +273,23 @@ public final class RemoteObjectImplTest { ...@@ -273,6 +273,23 @@ public final class RemoteObjectImplTest {
verify(auditor, never()).onObjectGetClassInvocationAttempt(); verify(auditor, never()).onObjectGetClassInvocationAttempt();
} }
@Test
public void testInvocationTargetException() {
Object target = new Object() {
@TestJavascriptInterface
public void exceptionThrowingMethod() throws Exception {
throw new Exception("This exception is expected during test. Do not be alarmed.");
}
};
RemoteObject remoteObject = new RemoteObjectImpl(target, TestJavascriptInterface.class);
RemoteObject.InvokeMethodResponse response = mock(RemoteObject.InvokeMethodResponse.class);
remoteObject.invokeMethod(
"exceptionThrowingMethod", new RemoteInvocationArgument[] {}, response);
verify(response).call(resultHasError(RemoteInvocationError.EXCEPTION_THROWN));
}
private RemoteInvocationResult resultHasError(final int error) { private RemoteInvocationResult resultHasError(final int error) {
return ArgumentMatchers.argThat(result -> result.error == error); return ArgumentMatchers.argThat(result -> result.error == error);
} }
......
...@@ -25,6 +25,7 @@ enum RemoteInvocationError { ...@@ -25,6 +25,7 @@ enum RemoteInvocationError {
OK = 0, OK = 0,
METHOD_NOT_FOUND, METHOD_NOT_FOUND,
OBJECT_GET_CLASS_BLOCKED, OBJECT_GET_CLASS_BLOCKED,
EXCEPTION_THROWN,
}; };
struct RemoteInvocationResult { struct RemoteInvocationResult {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment