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

Commit 3c250d9d authored by Guojing Yuan's avatar Guojing Yuan
Browse files

[CDM perm sync] Backup and restore permissions

Main changes:
1. Backup/restore permissions into/from CompanionMessage. ~95% of the
B+R code is copied from PermissionController and minimal changes have
been made to accomodate the CDM perm sync case. The main logical changes are
near the commented blocks in the files.
2. Message pagination

Bug: 228228540

Test: Manually tested on CDM test app, checked the output proto bytes.
Restore hasn't been tested, will test when the test app is done.

Change-Id: I21cdb6106b4237b72ea798ba3f206db5ee136916
parent 89ec54fe
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -928,7 +928,8 @@ public final class CompanionDeviceManager {
    @UserHandleAware
    public void startSystemDataTransfer(int associationId) throws DeviceNotAssociatedException {
        try {
            mService.startSystemDataTransfer(mContext.getUserId(), associationId);
            mService.startSystemDataTransfer(mContext.getOpPackageName(), mContext.getUserId(),
                    associationId);
        } catch (RemoteException e) {
            ExceptionUtils.propagateIfInstanceOf(e.getCause(), DeviceNotAssociatedException.class);
            throw e.rethrowFromSystemServer();
+7 −0
Original line number Diff line number Diff line
@@ -166,6 +166,9 @@ public abstract class CompanionDeviceService extends Service {
     * <p>Calling app must declare uses-permission
     * {@link android.Manifest.permission#DELIVER_COMPANION_MESSAGES}</p>
     *
     * <p>You need to start the service before calling this method, otherwise the system can't
     * get the context and the dispatch would fail.</p>
     *
     * <p>Note 1: messageId was assigned by the system, and sender should send the messageId along
     * with the message to the receiver. messageId will later be used for verification purpose.
     * Misusing the messageId will result in no action.</p>
@@ -184,6 +187,10 @@ public abstract class CompanionDeviceService extends Service {
    public final void dispatchMessageToSystem(int messageId, int associationId,
            @NonNull byte[] message)
            throws DeviceNotAssociatedException {
        if (getBaseContext() == null) {
            Log.e(LOG_TAG, "Dispatch failed. Start your service before calling this method.");
            return;
        }
        CompanionDeviceManager companionDeviceManager =
                getSystemService(CompanionDeviceManager.class);
        if (companionDeviceManager != null) {
+1 −1
Original line number Diff line number Diff line
@@ -75,5 +75,5 @@ interface ICompanionDeviceManager {
    PendingIntent buildPermissionTransferUserConsentIntent(String callingPackage, int userId,
        int associationId);

    void startSystemDataTransfer(int userId, int associationId);
    void startSystemDataTransfer(String packageName, int userId, int associationId);
}
+9 −5
Original line number Diff line number Diff line
@@ -100,6 +100,7 @@ import com.android.internal.util.DumpUtils;
import com.android.server.FgThread;
import com.android.server.LocalServices;
import com.android.server.SystemService;
import com.android.server.companion.datatransfer.CompanionMessageProcessor;
import com.android.server.companion.datatransfer.SystemDataTransferProcessor;
import com.android.server.companion.datatransfer.SystemDataTransferRequestStore;
import com.android.server.companion.presence.CompanionDevicePresenceMonitor;
@@ -137,6 +138,7 @@ public class CompanionDeviceManagerService extends SystemService {
    private final SystemDataTransferRequestStore mSystemDataTransferRequestStore;
    private AssociationRequestsProcessor mAssociationRequestsProcessor;
    private SystemDataTransferProcessor mSystemDataTransferProcessor;
    private CompanionMessageProcessor mCompanionMessageProcessor;
    private CompanionDevicePresenceMonitor mDevicePresenceMonitor;
    private CompanionApplicationController mCompanionAppController;
    private CompanionSecureCommunicationsManager mSecureCommsManager;
@@ -188,12 +190,13 @@ public class CompanionDeviceManagerService extends SystemService {

        mAssociationRequestsProcessor = new AssociationRequestsProcessor(
                /* cdmService */this, mAssociationStore);
        mSystemDataTransferProcessor = new SystemDataTransferProcessor(this, mAssociationStore,
                mSystemDataTransferRequestStore);
        mCompanionAppController = new CompanionApplicationController(
                context, mApplicationControllerCallback);
        mSecureCommsManager = new CompanionSecureCommunicationsManager(
                mAssociationStore, mCompanionAppController);
        mCompanionMessageProcessor = new CompanionMessageProcessor(mSecureCommsManager);
        mSystemDataTransferProcessor = new SystemDataTransferProcessor(this, mAssociationStore,
                mSystemDataTransferRequestStore, mCompanionMessageProcessor);

        // Publish "binder" service.
        final CompanionDeviceManagerImpl impl = new CompanionDeviceManagerImpl();
@@ -619,14 +622,15 @@ public class CompanionDeviceManagerService extends SystemService {

        @Override
        public PendingIntent buildPermissionTransferUserConsentIntent(String packageName,
                int userId, int associationId) throws RemoteException {
                int userId, int associationId) {
            return mSystemDataTransferProcessor.buildPermissionTransferUserConsentIntent(
                    packageName, userId, associationId);
        }

        @Override
        public void startSystemDataTransfer(int userId, int associationId) throws RemoteException {
            // TODO(b/222121838)
        public void startSystemDataTransfer(String packageName, int userId, int associationId) {
            mSystemDataTransferProcessor.startSystemDataTransfer(packageName, userId,
                    associationId);
        }

        @Override
+11 −3
Original line number Diff line number Diff line
@@ -54,7 +54,7 @@ import java.util.Map;
 * {@link Manifest.permission#REQUEST_COMPANION_PROFILE_APP_STREAMING},
 * {@link Manifest.permission#REQUEST_COMPANION_SELF_MANAGED} etc.)
 */
final class PermissionsUtils {
public final class PermissionsUtils {

    private static final Map<String, String> DEVICE_PROFILE_TO_PERMISSION;
    static {
@@ -132,7 +132,11 @@ final class PermissionsUtils {
        return true;
    }

    static void enforceCallerIsSystemOr(@UserIdInt int userId, @NonNull String packageName) {
    /**
     * Check if the calling user id matches the userId, and if the package belongs to
     * the calling uid.
     */
    public static void enforceCallerIsSystemOr(@UserIdInt int userId, @NonNull String packageName) {
        final int callingUid = getCallingUid();
        if (callingUid == SYSTEM_UID) return;

@@ -191,7 +195,11 @@ final class PermissionsUtils {
        return checkCallerCanManageCompanionDevice(context);
    }

    static @Nullable AssociationInfo sanitizeWithCallerChecks(@NonNull Context context,
    /**
     * Check if CDM can trust the context to process the association.
     */
    @Nullable
    public static AssociationInfo sanitizeWithCallerChecks(@NonNull Context context,
            @Nullable AssociationInfo association) {
        if (association == null) return null;

Loading