aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Scott <scottjonathan@google.com>2021-10-15 15:25:56 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2021-10-15 15:25:56 +0000
commit6c3f1daaeef291674599a5b870c5b83c24b25ad4 (patch)
tree7dada9629976efaca4537893d3c6841bdee6f692
parentd4bd75578d3b42a84730e20670e36ccefd36c6a5 (diff)
parent265a3eca1141c83f1e7b58c781b3588d91a7c270 (diff)
downloadconnectedappssdk-6c3f1daaeef291674599a5b870c5b83c24b25ad4.tar.gz
Properly handle Errors in IPCs. am: 7a49de334f am: 265a3eca11
Original change: https://googleplex-android-review.googlesource.com/c/platform/external/connectedappssdk/+/16047810 Change-Id: I74cf70bf2a5f36a3284f386a82be731dd03ef149
-rw-r--r--processor/src/main/java/com/google/android/enterprise/connectedapps/processor/DispatcherGenerator.java15
-rw-r--r--processor/src/main/java/com/google/android/enterprise/connectedapps/processor/FakeOtherGenerator.java6
-rw-r--r--sdk/src/main/java/com/google/android/enterprise/connectedapps/exceptions/ProfileRuntimeException.java4
-rw-r--r--sdk/src/main/java/com/google/android/enterprise/connectedapps/internal/BackgroundExceptionThrower.java22
4 files changed, 41 insertions, 6 deletions
diff --git a/processor/src/main/java/com/google/android/enterprise/connectedapps/processor/DispatcherGenerator.java b/processor/src/main/java/com/google/android/enterprise/connectedapps/processor/DispatcherGenerator.java
index f5264b0..a4f8ef6 100644
--- a/processor/src/main/java/com/google/android/enterprise/connectedapps/processor/DispatcherGenerator.java
+++ b/processor/src/main/java/com/google/android/enterprise/connectedapps/processor/DispatcherGenerator.java
@@ -187,6 +187,21 @@ final class DispatcherGenerator {
methodCode.addStatement("$T.throwInBackground(e)", BACKGROUND_EXCEPTION_THROWER_CLASSNAME);
methodCode.addStatement("return throwableBytes");
+ methodCode.nextControlFlow("catch ($T e)", Error.class);
+
+ // parcel is recycled in this method
+ methodCode.addStatement("$1T throwableParcel = $1T.obtain()", PARCEL_CLASSNAME);
+ methodCode.add("throwableParcel.writeInt(1); //errors\n");
+ methodCode.addStatement(
+ "$T.writeThrowableToParcel(throwableParcel, e)", PARCEL_UTILITIES_CLASSNAME);
+ methodCode.addStatement(
+ "$1T throwableBytes = parcelCallReceiver.prepareResponse(callId, throwableParcel)",
+ ArrayTypeName.of(byte.class));
+ methodCode.addStatement("throwableParcel.recycle()");
+
+ methodCode.addStatement("$T.throwInBackground(e)", BACKGROUND_EXCEPTION_THROWER_CLASSNAME);
+
+ methodCode.addStatement("return throwableBytes");
methodCode.endControlFlow();
MethodSpec callMethod =
diff --git a/processor/src/main/java/com/google/android/enterprise/connectedapps/processor/FakeOtherGenerator.java b/processor/src/main/java/com/google/android/enterprise/connectedapps/processor/FakeOtherGenerator.java
index 20bf0fb..53e5c72 100644
--- a/processor/src/main/java/com/google/android/enterprise/connectedapps/processor/FakeOtherGenerator.java
+++ b/processor/src/main/java/com/google/android/enterprise/connectedapps/processor/FakeOtherGenerator.java
@@ -215,6 +215,8 @@ final class FakeOtherGenerator {
methodBuilder.addStatement(methodCall);
methodBuilder.nextControlFlow("catch ($T e)", RuntimeException.class);
methodBuilder.addStatement("throw new $T(e)", PROFILE_RUNTIME_EXCEPTION_CLASSNAME);
+ methodBuilder.nextControlFlow("catch ($T e)", Error.class);
+ methodBuilder.addStatement("throw new $T(e)", PROFILE_RUNTIME_EXCEPTION_CLASSNAME);
methodBuilder.endControlFlow();
classBuilder.addMethod(methodBuilder.build());
}
@@ -267,6 +269,8 @@ final class FakeOtherGenerator {
methodBuilder.addStatement(methodCall);
methodBuilder.nextControlFlow("catch ($T e)", RuntimeException.class);
methodBuilder.addStatement("throw new $T(e)", PROFILE_RUNTIME_EXCEPTION_CLASSNAME);
+ methodBuilder.nextControlFlow("catch ($T e)", Error.class);
+ methodBuilder.addStatement("throw new $T(e)", PROFILE_RUNTIME_EXCEPTION_CLASSNAME);
methodBuilder.endControlFlow();
classBuilder.addMethod(methodBuilder.build());
@@ -332,6 +336,8 @@ final class FakeOtherGenerator {
}
methodBuilder.nextControlFlow("catch ($T e)", RuntimeException.class);
methodBuilder.addStatement("throw new $T(e)", PROFILE_RUNTIME_EXCEPTION_CLASSNAME);
+ methodBuilder.nextControlFlow("catch ($T e)", Error.class);
+ methodBuilder.addStatement("throw new $T(e)", PROFILE_RUNTIME_EXCEPTION_CLASSNAME);
methodBuilder.endControlFlow();
classBuilder.addMethod(methodBuilder.build());
diff --git a/sdk/src/main/java/com/google/android/enterprise/connectedapps/exceptions/ProfileRuntimeException.java b/sdk/src/main/java/com/google/android/enterprise/connectedapps/exceptions/ProfileRuntimeException.java
index 6ea9005..511a77a 100644
--- a/sdk/src/main/java/com/google/android/enterprise/connectedapps/exceptions/ProfileRuntimeException.java
+++ b/sdk/src/main/java/com/google/android/enterprise/connectedapps/exceptions/ProfileRuntimeException.java
@@ -16,12 +16,12 @@
package com.google.android.enterprise.connectedapps.exceptions;
/**
- * Thrown when a {@link RuntimeException} is thrown during a cross-profile call.
+ * Thrown when a {@link Throwable} is thrown during a cross-profile call.
*
* <p>To get the original exception, call {@link #getCause()}.
*/
public class ProfileRuntimeException extends RuntimeException {
- public ProfileRuntimeException(RuntimeException cause) {
+ public ProfileRuntimeException(Throwable cause) {
super(cause);
}
}
diff --git a/sdk/src/main/java/com/google/android/enterprise/connectedapps/internal/BackgroundExceptionThrower.java b/sdk/src/main/java/com/google/android/enterprise/connectedapps/internal/BackgroundExceptionThrower.java
index 0999a35..9511e19 100644
--- a/sdk/src/main/java/com/google/android/enterprise/connectedapps/internal/BackgroundExceptionThrower.java
+++ b/sdk/src/main/java/com/google/android/enterprise/connectedapps/internal/BackgroundExceptionThrower.java
@@ -24,15 +24,23 @@ public final class BackgroundExceptionThrower {
private BackgroundExceptionThrower() {}
private static class ThrowingRunnable implements Runnable {
- RuntimeException throwable;
+ RuntimeException runtimeException;
+ Error error;
- ThrowingRunnable(RuntimeException throwable) {
- this.throwable = throwable;
+ ThrowingRunnable(RuntimeException runtimeException) {
+ this.runtimeException = runtimeException;
+ }
+
+ ThrowingRunnable(Error error) {
+ this.error = error;
}
@Override
public void run() {
- throw throwable;
+ if (error != null) {
+ throw error;
+ }
+ throw runtimeException;
}
}
@@ -41,4 +49,10 @@ public final class BackgroundExceptionThrower {
// We add a small delay to ensure that the return can be completed before crashing
new Handler(Looper.getMainLooper()).postDelayed(new ThrowingRunnable(throwable), 1000);
}
+
+ /** Throw the given {@link Error} after a delay on the main looper. */
+ public static void throwInBackground(Error throwable) {
+ // We add a small delay to ensure that the return can be completed before crashing
+ new Handler(Looper.getMainLooper()).postDelayed(new ThrowingRunnable(throwable), 1000);
+ }
}