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

Commit 93e38c95 authored by Hai Zhang's avatar Hai Zhang
Browse files

Move or refactor PermissionManagerInternal APIs.

Make the three backup related methods in PermissionManagerInternal
ready for system API. PermissionManagerInternal is currently used in
framework so it can't be removed without other changes yet. The other
listener methods are only used by PermissionPolicyService and will
become module internal.

Finally turn PermissionManagerInternal and
PermissionManagerServiceInternal into an interface from an abstract
class, and remove redundant modifiers after this.

Also refactors onUserCreated/Removed() to be system API ready.

Bug: 158736025
Test: presubmit
Change-Id: I335a7a37b737f6fa0faf0a2c34634d44199aee97
parent 14197c21
Loading
Loading
Loading
Loading
+19 −50
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ package android.permission;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.os.UserHandle;

/**
 * Internal interfaces to be used by other components within the system server.
@@ -28,69 +27,39 @@ import android.os.UserHandle;
 *
 * @hide
 */
public abstract class PermissionManagerInternal {

    /**
     * Listener for package permission state (permissions or flags) changes.
     */
    public interface OnRuntimePermissionStateChangedListener {

        /**
         * Called when the runtime permission state (permissions or flags) changed.
         *
         * @param packageName The package for which the change happened.
         * @param userId the user id for which the change happened.
         */
        @Nullable
        void onRuntimePermissionStateChanged(@NonNull String packageName,
                @UserIdInt int userId);
    }

public interface PermissionManagerInternal {
    /**
     * Get the state of the runtime permissions as xml file.
     * Get the state of the runtime permissions as a blob.
     *
     * @param user The user the data should be extracted for
     * @param userId The user ID the data should be extracted for
     *
     * @return The state as a xml file
     * @return the state as a blob
     */
    public abstract @Nullable byte[] backupRuntimePermissions(@NonNull UserHandle user);
    //@SystemApi(client = SystemApi.Client.SYSTEM_SERVER)
    @Nullable
    byte[] backupRuntimePermissions(@UserIdInt int userId);

    /**
     * Restore a permission state previously backed up via {@link #backupRuntimePermissions}.
     * <p>
     * If not all state can be restored, the un-restorable state will be delayed and can be
     * retried via {@link #restoreDelayedRuntimePermissions}.
     *
     * <p>If not all state can be restored, the un-restoreable state will be delayed and can be
     * re-tried via {@link #restoreDelayedRuntimePermissions}.
     *
     * @param backup The state as an xml file
     * @param user The user the data should be restored for
     * @param backup the state as a blob
     * @param userId the user ID the data should be restored for
     */
    public abstract void restoreRuntimePermissions(@NonNull byte[] backup,
            @NonNull UserHandle user);
    //@SystemApi(client = SystemApi.Client.SYSTEM_SERVER)
    void restoreRuntimePermissions(@NonNull byte[] backup, @UserIdInt int userId);

    /**
     * Try to apply permission backup of a package that was previously not applied.
     *
     * @param packageName The package that is newly installed
     * @param user The user the package is installed for
     * @param packageName the package that is newly installed
     * @param userId the user ID the package is installed for
     *
     * @see #restoreRuntimePermissions
     */
    public abstract void restoreDelayedRuntimePermissions(@NonNull String packageName,
            @NonNull UserHandle user);

    /**
     * Adds a listener for runtime permission state (permissions or flags) changes.
     *
     * @param listener The listener.
     */
    public abstract void addOnRuntimePermissionStateChangedListener(
            @NonNull OnRuntimePermissionStateChangedListener listener);

    /**
     * Removes a listener for runtime permission state (permissions or flags) changes.
     *
     * @param listener The listener.
     */
    public abstract void removeOnRuntimePermissionStateChangedListener(
            @NonNull OnRuntimePermissionStateChangedListener listener);
    //@SystemApi(client = SystemApi.Client.SYSTEM_SERVER)
    void restoreDelayedRuntimePermissions(@NonNull String packageName,
            @UserIdInt int userId);
}
+5 −5
Original line number Diff line number Diff line
@@ -17,8 +17,8 @@
package com.android.server.backup;

import android.annotation.NonNull;
import android.annotation.UserIdInt;
import android.app.backup.BlobBackupHelper;
import android.os.UserHandle;
import android.permission.PermissionManagerInternal;
import android.util.Slog;

@@ -34,14 +34,14 @@ public class PermissionBackupHelper extends BlobBackupHelper {
    // key under which the permission-grant state blob is committed to backup
    private static final String KEY_PERMISSIONS = "permissions";

    private final @NonNull UserHandle mUser;
    private final @UserIdInt int mUserId;

    private final @NonNull PermissionManagerInternal mPermissionManager;

    public PermissionBackupHelper(int userId) {
        super(STATE_VERSION, KEY_PERMISSIONS);

        mUser = UserHandle.of(userId);
        mUserId = userId;
        mPermissionManager = LocalServices.getService(PermissionManagerInternal.class);
    }

@@ -53,7 +53,7 @@ public class PermissionBackupHelper extends BlobBackupHelper {
        try {
            switch (key) {
                case KEY_PERMISSIONS:
                    return mPermissionManager.backupRuntimePermissions(mUser);
                    return mPermissionManager.backupRuntimePermissions(mUserId);

                default:
                    Slog.w(TAG, "Unexpected backup key " + key);
@@ -72,7 +72,7 @@ public class PermissionBackupHelper extends BlobBackupHelper {
        try {
            switch (key) {
                case KEY_PERMISSIONS:
                    mPermissionManager.restoreRuntimePermissions(payload, mUser);
                    mPermissionManager.restoreRuntimePermissions(payload, mUserId);
                    break;

                default:
+3 −4
Original line number Diff line number Diff line
@@ -20723,8 +20723,7 @@ public class PackageManagerService extends IPackageManager.Stub
        // We may also need to apply pending (restored) runtime permission grants
        // within these users.
        mPermissionManager.restoreDelayedRuntimePermissions(packageName,
                UserHandle.of(userId));
        mPermissionManager.restoreDelayedRuntimePermissions(packageName, userId);
        // Persistent preferred activity might have came into effect due to this
        // install.
@@ -24333,13 +24332,13 @@ public class PackageManagerService extends IPackageManager.Stub
                    + ", convertedFromPreCreated=" + convertedFromPreCreated + ")");
        }
        if (!convertedFromPreCreated) {
            mPermissionManager.onNewUserCreated(userId);
            mPermissionManager.onUserCreated(userId);
            return;
        }
        if (!readPermissionStateForUser(userId)) {
            // Could not read the existing permissions, re-grant them.
            Slog.i(TAG, "re-granting permissions for pre-created user " + userId);
            mPermissionManager.onNewUserCreated(userId);
            mPermissionManager.onUserCreated(userId);
        }
    }
+34 −24
Original line number Diff line number Diff line
@@ -114,7 +114,6 @@ import android.permission.IPermissionManager;
import android.permission.PermissionControllerManager;
import android.permission.PermissionManager;
import android.permission.PermissionManagerInternal;
import android.permission.PermissionManagerInternal.OnRuntimePermissionStateChangedListener;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.ArraySet;
@@ -151,6 +150,7 @@ import com.android.server.pm.UserManagerInternal;
import com.android.server.pm.UserManagerService;
import com.android.server.pm.parsing.PackageInfoUtils;
import com.android.server.pm.parsing.pkg.AndroidPackage;
import com.android.server.pm.permission.PermissionManagerServiceInternal.OnRuntimePermissionStateChangedListener;
import com.android.server.policy.PermissionPolicyInternal;
import com.android.server.policy.SoftRestrictedPermissionPolicy;

@@ -2197,19 +2197,20 @@ public class PermissionManagerService extends IPermissionManager.Stub {
     *
     * <p>Can not be called on main thread.
     *
     * @param user The user the data should be extracted for
     * @param userId The user ID the data should be extracted for
     *
     * @return The state as a xml file
     */
    private @Nullable byte[] backupRuntimePermissions(@NonNull UserHandle user) {
    @Nullable
    private byte[] backupRuntimePermissions(@UserIdInt int userId) {
        CompletableFuture<byte[]> backup = new CompletableFuture<>();
        mPermissionControllerManager.getRuntimePermissionBackup(user, mContext.getMainExecutor(),
                backup::complete);
        mPermissionControllerManager.getRuntimePermissionBackup(UserHandle.of(userId),
                mContext.getMainExecutor(), backup::complete);

        try {
            return backup.get(BACKUP_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
        } catch (InterruptedException | ExecutionException  | TimeoutException e) {
            Slog.e(TAG, "Cannot create permission backup for " + user, e);
            Slog.e(TAG, "Cannot create permission backup for user " + userId, e);
            return null;
        }
    }
@@ -2221,13 +2222,14 @@ public class PermissionManagerService extends IPermissionManager.Stub {
     * applied via {@link #restoreDelayedRuntimePermissions}.
     *
     * @param backup The state as an xml file
     * @param user The user the data should be restored for
     * @param userId The user ID the data should be restored for
     */
    private void restoreRuntimePermissions(@NonNull byte[] backup, @NonNull UserHandle user) {
    private void restoreRuntimePermissions(@NonNull byte[] backup, @UserIdInt int userId) {
        synchronized (mLock) {
            mHasNoDelayedPermBackup.delete(user.getIdentifier());
            mHasNoDelayedPermBackup.delete(userId);
        }
        mPermissionControllerManager.stageAndApplyRuntimePermissionsBackup(backup, user);
        mPermissionControllerManager.stageAndApplyRuntimePermissionsBackup(backup,
                UserHandle.of(userId));
    }

    /**
@@ -2236,24 +2238,24 @@ public class PermissionManagerService extends IPermissionManager.Stub {
     * <p>Can not be called on main thread.
     *
     * @param packageName The package that is newly installed
     * @param user The user the package is installed for
     * @param userId The user ID the package is installed for
     *
     * @see #restoreRuntimePermissions
     */
    private void restoreDelayedRuntimePermissions(@NonNull String packageName,
            @NonNull UserHandle user) {
            @UserIdInt int userId) {
        synchronized (mLock) {
            if (mHasNoDelayedPermBackup.get(user.getIdentifier(), false)) {
            if (mHasNoDelayedPermBackup.get(userId, false)) {
                return;
            }
        }
        mPermissionControllerManager.applyStagedRuntimePermissionBackup(packageName, user,
                mContext.getMainExecutor(), (hasMoreBackup) -> {
        mPermissionControllerManager.applyStagedRuntimePermissionBackup(packageName,
                UserHandle.of(userId), mContext.getMainExecutor(), (hasMoreBackup) -> {
                    if (hasMoreBackup) {
                        return;
                    }
                    synchronized (mLock) {
                        mHasNoDelayedPermBackup.put(user.getIdentifier(), true);
                        mHasNoDelayedPermBackup.put(userId, true);
                    }
                });
    }
@@ -5058,7 +5060,7 @@ public class PermissionManagerService extends IPermissionManager.Stub {
        }
    }

    private class PermissionManagerServiceInternalImpl extends PermissionManagerServiceInternal {
    private class PermissionManagerServiceInternalImpl implements PermissionManagerServiceInternal {
        @Override
        public void systemReady() {
            PermissionManagerService.this.systemReady();
@@ -5083,6 +5085,7 @@ public class PermissionManagerService extends IPermissionManager.Stub {
        }
        @Override
        public void onUserRemoved(@UserIdInt int userId) {
            Preconditions.checkArgumentNonNegative(userId, "userId");
            PermissionManagerService.this.onUserRemoved(userId);
        }
        @NonNull
@@ -5162,20 +5165,26 @@ public class PermissionManagerService extends IPermissionManager.Stub {
            return matchingPermissions;
        }

        @Nullable
        @Override
        public @Nullable byte[] backupRuntimePermissions(@NonNull UserHandle user) {
            return PermissionManagerService.this.backupRuntimePermissions(user);
        public byte[] backupRuntimePermissions(@UserIdInt int userId) {
            Preconditions.checkArgumentNonNegative(userId, "userId");
            return PermissionManagerService.this.backupRuntimePermissions(userId);
        }

        @Override
        public void restoreRuntimePermissions(@NonNull byte[] backup, @NonNull UserHandle user) {
            PermissionManagerService.this.restoreRuntimePermissions(backup, user);
        public void restoreRuntimePermissions(@NonNull byte[] backup, @UserIdInt int userId) {
            Objects.requireNonNull(backup, "backup");
            Preconditions.checkArgumentNonNegative(userId, "userId");
            PermissionManagerService.this.restoreRuntimePermissions(backup, userId);
        }

        @Override
        public void restoreDelayedRuntimePermissions(@NonNull String packageName,
                @NonNull UserHandle user) {
            PermissionManagerService.this.restoreDelayedRuntimePermissions(packageName, user);
                @UserIdInt int userId) {
            Objects.requireNonNull(packageName, "packageName");
            Preconditions.checkArgumentNonNegative(userId, "userId");
            PermissionManagerService.this.restoreDelayedRuntimePermissions(packageName, userId);
        }

        @Override
@@ -5264,7 +5273,8 @@ public class PermissionManagerService extends IPermissionManager.Stub {
        }

        @Override
        public void onNewUserCreated(int userId) {
        public void onUserCreated(@UserIdInt int userId) {
            Preconditions.checkArgumentNonNegative(userId, "userId");
            // NOTE: This adds UPDATE_PERMISSIONS_REPLACE_PKG
            PermissionManagerService.this.updateAllPermissions(StorageManager.UUID_PRIVATE_INTERNAL,
                    true, mDefaultPermissionCallback);
+94 −58

File changed.

Preview size limit exceeded, changes collapsed.