Merge "Propagate exception thrown by ContentProvider.canonicalize" into rvc-dev am: f7b0b5a373

Change-Id: I940f1bd306c57c62eecf5c9e3e3c724c68cf3f2d
This commit is contained in:
Dmitri Plotnikov
2020-03-26 23:09:36 +00:00
committed by Automerger Merge Worker
3 changed files with 40 additions and 21 deletions

View File

@@ -307,19 +307,7 @@ public abstract class ContentProvider implements ContentInterface, ComponentCall
try {
result.putString(ContentResolver.REMOTE_CALLBACK_RESULT, getType(uri));
} catch (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()));
}
result.putByteArray(ContentResolver.REMOTE_CALLBACK_ERROR, parcel.marshall());
} finally {
parcel.recycle();
}
putExceptionInBundle(result, ContentResolver.REMOTE_CALLBACK_ERROR, e);
}
callback.sendResult(result);
}
@@ -602,8 +590,12 @@ public abstract class ContentProvider implements ContentInterface, ComponentCall
public void canonicalizeAsync(String callingPkg, @Nullable String attributionTag, Uri uri,
RemoteCallback callback) {
final Bundle result = new Bundle();
result.putParcelable(ContentResolver.REMOTE_CALLBACK_RESULT,
canonicalize(callingPkg, attributionTag, uri));
try {
result.putParcelable(ContentResolver.REMOTE_CALLBACK_RESULT,
canonicalize(callingPkg, attributionTag, uri));
} catch (Exception e) {
putExceptionInBundle(result, ContentResolver.REMOTE_CALLBACK_ERROR, e);
}
callback.sendResult(result);
}
@@ -717,6 +709,22 @@ public abstract class ContentProvider implements ContentInterface, ComponentCall
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) {

View File

@@ -238,12 +238,9 @@ public class ContentResolverTest {
@Test
public void testGetType_providerException() {
try {
mResolver.getType(Uri.parse("content://android.content.FakeProviderRemote/error"));
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
// Expected
}
String type =
mResolver.getType(Uri.parse("content://android.content.FakeProviderRemote/error"));
assertThat(type).isNull();
}
@Test
@@ -253,4 +250,15 @@ public class ContentResolverTest {
assertThat(canonical).isEqualTo(
Uri.parse("content://android.content.FakeProviderRemote/canonical"));
}
@Test
public void testCanonicalize_providerException() {
try {
mResolver.canonicalize(
Uri.parse("content://android.content.FakeProviderRemote/error"));
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
// Expected
}
}
}

View File

@@ -60,6 +60,9 @@ public class FakeProviderRemote extends ContentProvider {
@Override
public Uri canonicalize(Uri uri) {
if (uri.getPath() != null && uri.getPath().contains("error")) {
throw new IllegalArgumentException("Expected exception");
}
return new Uri.Builder().scheme(uri.getScheme()).authority(uri.getAuthority())
.appendPath("canonical").build();
}