Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e7f48d99 authored by Dmitri Plotnikov's avatar Dmitri Plotnikov
Browse files

Use ParcelableException to put exception in bundle

Test: atest FrameworksCoreTests:android.content.ContentResolverTest
Bug: 149184281
Change-Id: Ice6e1207ace7a0cc6b4b8dc1b99da22b13b04031
parent b529a3f1
Loading
Loading
Loading
Loading
+5 −19
Original line number Diff line number Diff line
@@ -47,8 +47,8 @@ import android.os.Bundle;
import android.os.CancellationSignal;
import android.os.IBinder;
import android.os.ICancellationSignal;
import android.os.Parcel;
import android.os.ParcelFileDescriptor;
import android.os.ParcelableException;
import android.os.Process;
import android.os.RemoteCallback;
import android.os.RemoteException;
@@ -307,7 +307,8 @@ public abstract class ContentProvider implements ContentInterface, ComponentCall
            try {
                result.putString(ContentResolver.REMOTE_CALLBACK_RESULT, getType(uri));
            } catch (Exception e) {
                putExceptionInBundle(result, ContentResolver.REMOTE_CALLBACK_ERROR, e);
                result.putParcelable(ContentResolver.REMOTE_CALLBACK_ERROR,
                        new ParcelableException(e));
            }
            callback.sendResult(result);
        }
@@ -594,7 +595,8 @@ public abstract class ContentProvider implements ContentInterface, ComponentCall
                result.putParcelable(ContentResolver.REMOTE_CALLBACK_RESULT,
                        canonicalize(callingPkg, attributionTag, uri));
            } catch (Exception e) {
                putExceptionInBundle(result, ContentResolver.REMOTE_CALLBACK_ERROR, e);
                result.putParcelable(ContentResolver.REMOTE_CALLBACK_ERROR,
                        new ParcelableException(e));
            }
            callback.sendResult(result);
        }
@@ -709,22 +711,6 @@ 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) {
+10 −23
Original line number Diff line number Diff line
@@ -55,8 +55,8 @@ import android.os.DeadObjectException;
import android.os.IBinder;
import android.os.ICancellationSignal;
import android.os.OperationCanceledException;
import android.os.Parcel;
import android.os.ParcelFileDescriptor;
import android.os.ParcelableException;
import android.os.RemoteCallback;
import android.os.RemoteException;
import android.os.ServiceManager;
@@ -931,8 +931,15 @@ public abstract class ContentResolver implements ContentInterface {
        @Override
        public void onResult(Bundle result) {
            synchronized (this) {
                this.exception = getExceptionFromBundle(result);
                if (this.exception == null) {
                ParcelableException e = result.getParcelable(REMOTE_CALLBACK_ERROR);
                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);
                }
                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);

        public void waitForResult(long timeout) {