Merge "Use ParcelableException to put exception in bundle" into rvc-dev

This commit is contained in:
Dmitri Plotnikov
2020-03-31 18:36:57 +00:00
committed by Android (Google) Code Review
2 changed files with 15 additions and 42 deletions

View File

@@ -47,8 +47,8 @@ import android.os.Bundle;
import android.os.CancellationSignal; import android.os.CancellationSignal;
import android.os.IBinder; import android.os.IBinder;
import android.os.ICancellationSignal; import android.os.ICancellationSignal;
import android.os.Parcel;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.os.ParcelableException;
import android.os.Process; import android.os.Process;
import android.os.RemoteCallback; import android.os.RemoteCallback;
import android.os.RemoteException; import android.os.RemoteException;
@@ -307,7 +307,8 @@ public abstract class ContentProvider implements ContentInterface, ComponentCall
try { try {
result.putString(ContentResolver.REMOTE_CALLBACK_RESULT, getType(uri)); result.putString(ContentResolver.REMOTE_CALLBACK_RESULT, getType(uri));
} catch (Exception e) { } catch (Exception e) {
putExceptionInBundle(result, ContentResolver.REMOTE_CALLBACK_ERROR, e); result.putParcelable(ContentResolver.REMOTE_CALLBACK_ERROR,
new ParcelableException(e));
} }
callback.sendResult(result); callback.sendResult(result);
} }
@@ -594,7 +595,8 @@ public abstract class ContentProvider implements ContentInterface, ComponentCall
result.putParcelable(ContentResolver.REMOTE_CALLBACK_RESULT, result.putParcelable(ContentResolver.REMOTE_CALLBACK_RESULT,
canonicalize(callingPkg, attributionTag, uri)); canonicalize(callingPkg, attributionTag, uri));
} catch (Exception e) { } catch (Exception e) {
putExceptionInBundle(result, ContentResolver.REMOTE_CALLBACK_ERROR, e); result.putParcelable(ContentResolver.REMOTE_CALLBACK_ERROR,
new ParcelableException(e));
} }
callback.sendResult(result); callback.sendResult(result);
} }
@@ -709,22 +711,6 @@ public abstract class ContentProvider implements ContentInterface, ComponentCall
return AppOpsManager.MODE_ALLOWED; return AppOpsManager.MODE_ALLOWED;
} }
private void putExceptionInBundle(Bundle bundle, String key, Exception e) {
Parcel parcel = Parcel.obtain();
try {
try {
parcel.writeException(e);
} catch (Exception ex) {
// getType threw an unparcelable exception. Wrap the message into
// a parcelable exception type
parcel.writeException(new IllegalStateException(e.getMessage()));
}
bundle.putByteArray(key, parcel.marshall());
} finally {
parcel.recycle();
}
}
} }
boolean checkUser(int pid, int uid, Context context) { boolean checkUser(int pid, int uid, Context context) {

View File

@@ -55,8 +55,8 @@ import android.os.DeadObjectException;
import android.os.IBinder; import android.os.IBinder;
import android.os.ICancellationSignal; import android.os.ICancellationSignal;
import android.os.OperationCanceledException; import android.os.OperationCanceledException;
import android.os.Parcel;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.os.ParcelableException;
import android.os.RemoteCallback; import android.os.RemoteCallback;
import android.os.RemoteException; import android.os.RemoteException;
import android.os.ServiceManager; import android.os.ServiceManager;
@@ -931,8 +931,15 @@ public abstract class ContentResolver implements ContentInterface {
@Override @Override
public void onResult(Bundle result) { public void onResult(Bundle result) {
synchronized (this) { synchronized (this) {
this.exception = getExceptionFromBundle(result); ParcelableException e = result.getParcelable(REMOTE_CALLBACK_ERROR);
if (this.exception == null) { if (e != null) {
Throwable t = e.getCause();
if (t instanceof RuntimeException) {
this.exception = (RuntimeException) t;
} else {
this.exception = new RuntimeException(t);
}
} else {
this.result = getResultFromBundle(result); this.result = getResultFromBundle(result);
} }
done = true; done = true;
@@ -940,26 +947,6 @@ public abstract class ContentResolver implements ContentInterface {
} }
} }
private RuntimeException getExceptionFromBundle(Bundle result) {
byte[] bytes = result.getByteArray(REMOTE_CALLBACK_ERROR);
if (bytes == null) {
return null;
}
Parcel parcel = Parcel.obtain();
try {
parcel.unmarshall(bytes, 0, bytes.length);
parcel.setDataPosition(0);
parcel.readException();
} catch (RuntimeException ex) {
return ex;
} finally {
parcel.recycle();
}
return null;
}
protected abstract T getResultFromBundle(Bundle result); protected abstract T getResultFromBundle(Bundle result);
public void waitForResult(long timeout) { public void waitForResult(long timeout) {