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

Commit ca707311 authored by Winson Chiu's avatar Winson Chiu Committed by Automerger Merge Worker
Browse files

Merge "Implement domain verification backup and restore" into sc-dev am: 1b8610d9 am: ac60f41f

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/14058688

Change-Id: Ibd9b02e4d4c95940ba2b2ddc692a4e28bb337667
parents 883dc6fa ac60f41f
Loading
Loading
Loading
Loading
+2 −2
Original line number Original line Diff line number Diff line
@@ -310,8 +310,8 @@ interface IPackageManager {
    void restorePreferredActivities(in byte[] backup, int userId);
    void restorePreferredActivities(in byte[] backup, int userId);
    byte[] getDefaultAppsBackup(int userId);
    byte[] getDefaultAppsBackup(int userId);
    void restoreDefaultApps(in byte[] backup, int userId);
    void restoreDefaultApps(in byte[] backup, int userId);
    byte[] getIntentFilterVerificationBackup(int userId);
    byte[] getDomainVerificationBackup(int userId);
    void restoreIntentFilterVerification(in byte[] backup, int userId);
    void restoreDomainVerification(in byte[] backup, int userId);


    /**
    /**
     * Report the set of 'Home' activity candidates, plus (if any) which of them
     * Report the set of 'Home' activity candidates, plus (if any) which of them
+50 −17
Original line number Original line Diff line number Diff line
@@ -16,10 +16,12 @@


package com.android.server.backup;
package com.android.server.backup;


import android.annotation.StringDef;
import android.annotation.UserIdInt;
import android.app.AppGlobals;
import android.app.AppGlobals;
import android.app.backup.BlobBackupHelper;
import android.app.backup.BlobBackupHelper;
import android.content.pm.IPackageManager;
import android.content.pm.IPackageManager;
import android.os.UserHandle;
import android.content.pm.verify.domain.DomainVerificationManager;
import android.util.Slog;
import android.util.Slog;


public class PreferredActivityBackupHelper extends BlobBackupHelper {
public class PreferredActivityBackupHelper extends BlobBackupHelper {
@@ -27,7 +29,7 @@ public class PreferredActivityBackupHelper extends BlobBackupHelper {
    private static final boolean DEBUG = false;
    private static final boolean DEBUG = false;


    // current schema of the backup state blob
    // current schema of the backup state blob
    private static final int STATE_VERSION = 3;
    private static final int STATE_VERSION = 4;


    // key under which the preferred-activity state blob is committed to backup
    // key under which the preferred-activity state blob is committed to backup
    private static final String KEY_PREFERRED = "preferred-activity";
    private static final String KEY_PREFERRED = "preferred-activity";
@@ -35,14 +37,41 @@ public class PreferredActivityBackupHelper extends BlobBackupHelper {
    // key for default-browser [etc] state
    // key for default-browser [etc] state
    private static final String KEY_DEFAULT_APPS = "default-apps";
    private static final String KEY_DEFAULT_APPS = "default-apps";


    // intent-filter verification state
    /**
     * Intent-filter verification state
     * @deprecated Replaced by {@link #KEY_DOMAIN_VERIFICATION}, retained to ensure the key is
     * never reused.
     */
    @Deprecated
    private static final String KEY_INTENT_VERIFICATION = "intent-verification";
    private static final String KEY_INTENT_VERIFICATION = "intent-verification";


    public PreferredActivityBackupHelper() {
    /**
        super(STATE_VERSION,
     * State for {@link DomainVerificationManager}.
     */
    private static final String KEY_DOMAIN_VERIFICATION = "domain-verification";

    private static final String[] KEYS = new String[] {
            KEY_PREFERRED,
            KEY_DEFAULT_APPS,
            KEY_INTENT_VERIFICATION,
            KEY_DOMAIN_VERIFICATION
    };

    @StringDef(value = {
            KEY_PREFERRED,
            KEY_PREFERRED,
            KEY_DEFAULT_APPS,
            KEY_DEFAULT_APPS,
                KEY_INTENT_VERIFICATION);
            KEY_INTENT_VERIFICATION,
            KEY_DOMAIN_VERIFICATION
    })
    private @interface Key {
    }

    @UserIdInt
    private final int mUserId;

    public PreferredActivityBackupHelper(@UserIdInt int userId) {
        super(STATE_VERSION, KEYS);
        mUserId = userId;
    }
    }


    @Override
    @Override
@@ -52,14 +81,16 @@ public class PreferredActivityBackupHelper extends BlobBackupHelper {
            Slog.d(TAG, "Handling backup of " + key);
            Slog.d(TAG, "Handling backup of " + key);
        }
        }
        try {
        try {
            // TODO: http://b/22388012
            switch (key) {
            switch (key) {
                case KEY_PREFERRED:
                case KEY_PREFERRED:
                    return pm.getPreferredActivityBackup(UserHandle.USER_SYSTEM);
                    return pm.getPreferredActivityBackup(mUserId);
                case KEY_DEFAULT_APPS:
                case KEY_DEFAULT_APPS:
                    return pm.getDefaultAppsBackup(UserHandle.USER_SYSTEM);
                    return pm.getDefaultAppsBackup(mUserId);
                case KEY_INTENT_VERIFICATION:
                case KEY_INTENT_VERIFICATION:
                    return pm.getIntentFilterVerificationBackup(UserHandle.USER_SYSTEM);
                    // Deprecated
                    return null;
                case KEY_DOMAIN_VERIFICATION:
                    return pm.getDomainVerificationBackup(mUserId);
                default:
                default:
                    Slog.w(TAG, "Unexpected backup key " + key);
                    Slog.w(TAG, "Unexpected backup key " + key);
            }
            }
@@ -70,22 +101,24 @@ public class PreferredActivityBackupHelper extends BlobBackupHelper {
    }
    }


    @Override
    @Override
    protected void applyRestoredPayload(String key, byte[] payload) {
    protected void applyRestoredPayload(@Key String key, byte[] payload) {
        IPackageManager pm = AppGlobals.getPackageManager();
        IPackageManager pm = AppGlobals.getPackageManager();
        if (DEBUG) {
        if (DEBUG) {
            Slog.d(TAG, "Handling restore of " + key);
            Slog.d(TAG, "Handling restore of " + key);
        }
        }
        try {
        try {
            // TODO: http://b/22388012
            switch (key) {
            switch (key) {
                case KEY_PREFERRED:
                case KEY_PREFERRED:
                    pm.restorePreferredActivities(payload, UserHandle.USER_SYSTEM);
                    pm.restorePreferredActivities(payload, mUserId);
                    break;
                    break;
                case KEY_DEFAULT_APPS:
                case KEY_DEFAULT_APPS:
                    pm.restoreDefaultApps(payload, UserHandle.USER_SYSTEM);
                    pm.restoreDefaultApps(payload, mUserId);
                    break;
                    break;
                case KEY_INTENT_VERIFICATION:
                case KEY_INTENT_VERIFICATION:
                    pm.restoreIntentFilterVerification(payload, UserHandle.USER_SYSTEM);
                    // Deprecated
                    break;
                case KEY_DOMAIN_VERIFICATION:
                    pm.restoreDomainVerification(payload, mUserId);
                    break;
                    break;
                default:
                default:
                    Slog.w(TAG, "Unexpected restore key " + key);
                    Slog.w(TAG, "Unexpected restore key " + key);
+1 −1
Original line number Original line Diff line number Diff line
@@ -94,7 +94,7 @@ public class SystemBackupAgent extends BackupAgentHelper {
        mUserId = user.getIdentifier();
        mUserId = user.getIdentifier();


        addHelper(SYNC_SETTINGS_HELPER, new AccountSyncSettingsBackupHelper(this, mUserId));
        addHelper(SYNC_SETTINGS_HELPER, new AccountSyncSettingsBackupHelper(this, mUserId));
        addHelper(PREFERRED_HELPER, new PreferredActivityBackupHelper());
        addHelper(PREFERRED_HELPER, new PreferredActivityBackupHelper(mUserId));
        addHelper(NOTIFICATION_HELPER, new NotificationBackupHelper(mUserId));
        addHelper(NOTIFICATION_HELPER, new NotificationBackupHelper(mUserId));
        addHelper(PERMISSION_HELPER, new PermissionBackupHelper(mUserId));
        addHelper(PERMISSION_HELPER, new PermissionBackupHelper(mUserId));
        addHelper(USAGE_STATS_HELPER, new UsageStatsBackupHelper(this));
        addHelper(USAGE_STATS_HELPER, new UsageStatsBackupHelper(this));
+47 −6
Original line number Original line Diff line number Diff line
@@ -1781,6 +1781,24 @@ public class PackageManagerService extends IPackageManager.Stub
            }
            }
        }
        }
        @Override
        public <ExceptionOne extends Exception, ExceptionTwo extends Exception> void
                withPackageSettingsThrowing2(
                        @NonNull Throwing2Consumer<Function<String, PackageSetting>, ExceptionOne,
                                ExceptionTwo> block) throws ExceptionOne, ExceptionTwo {
            final Computer snapshot = snapshotComputer();
            // This method needs to either lock or not lock consistently throughout the method,
            // so if the live computer is returned, force a wrapping sync block.
            if (snapshot == mLiveComputer) {
                synchronized (mLock) {
                    block.accept(snapshot::getPackageSetting);
                }
            } else {
                block.accept(snapshot::getPackageSetting);
            }
        }
        @Override
        @Override
        public <Output, ExceptionType extends Exception> Output
        public <Output, ExceptionType extends Exception> Output
                withPackageSettingsReturningThrowing(@NonNull ThrowingFunction<Function<String,
                withPackageSettingsReturningThrowing(@NonNull ThrowingFunction<Function<String,
@@ -22459,21 +22477,44 @@ public class PackageManagerService extends IPackageManager.Stub
    }
    }
    @Override
    @Override
    public byte[] getIntentFilterVerificationBackup(int userId) {
    public byte[] getDomainVerificationBackup(int userId) {
        if (Binder.getCallingUid() != Process.SYSTEM_UID) {
        if (Binder.getCallingUid() != Process.SYSTEM_UID) {
            throw new SecurityException("Only the system may call getIntentFilterVerificationBackup()");
            throw new SecurityException("Only the system may call getDomainVerificationBackup()");
        }
        }
        // TODO(b/170746586)
        try {
            try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
                TypedXmlSerializer serializer = Xml.resolveSerializer(output);
                mDomainVerificationManager.writeSettings(serializer, true, userId);
                return output.toByteArray();
            }
        } catch (Exception e) {
            if (DEBUG_BACKUP) {
                Slog.e(TAG, "Unable to write domain verification for backup", e);
            }
            return null;
            return null;
        }
        }
    }
    @Override
    @Override
    public void restoreIntentFilterVerification(byte[] backup, int userId) {
    public void restoreDomainVerification(byte[] backup, int userId) {
        if (Binder.getCallingUid() != Process.SYSTEM_UID) {
        if (Binder.getCallingUid() != Process.SYSTEM_UID) {
            throw new SecurityException("Only the system may call restorePreferredActivities()");
            throw new SecurityException("Only the system may call restorePreferredActivities()");
        }
        }
        // TODO(b/170746586)
        try {
            ByteArrayInputStream input = new ByteArrayInputStream(backup);
            TypedXmlPullParser parser = Xml.resolvePullParser(input);
            // User ID input isn't necessary here as it assumes the user integers match and that
            // the only states inside the backup XML are for the target user.
            mDomainVerificationManager.restoreSettings(parser);
            input.close();
        } catch (Exception e) {
            if (DEBUG_BACKUP) {
                Slog.e(TAG, "Exception restoring domain verification: " + e.getMessage());
            }
        }
    }
    }
    @Override
    @Override
+3 −8
Original line number Original line Diff line number Diff line
@@ -2339,7 +2339,8 @@ public final class Settings implements Watchable, Snappable {
                }
                }
            }
            }


            mDomainVerificationManager.writeSettings(serializer);
            mDomainVerificationManager.writeSettings(serializer, false /* includeSignatures */,
                    UserHandle.USER_ALL);


            mKeySetManagerService.writeKeySetManagerServiceLPr(serializer);
            mKeySetManagerService.writeKeySetManagerServiceLPr(serializer);


@@ -2913,13 +2914,7 @@ public final class Settings implements Watchable, Snappable {
            }
            }


            str.close();
            str.close();

        } catch (IOException | XmlPullParserException e) {
        } catch (XmlPullParserException e) {
            mReadMessages.append("Error reading: " + e.toString());
            PackageManagerService.reportSettingsProblem(Log.ERROR, "Error reading settings: " + e);
            Slog.wtf(PackageManagerService.TAG, "Error reading package manager settings", e);

        } catch (java.io.IOException e) {
            mReadMessages.append("Error reading: " + e.toString());
            mReadMessages.append("Error reading: " + e.toString());
            PackageManagerService.reportSettingsProblem(Log.ERROR, "Error reading settings: " + e);
            PackageManagerService.reportSettingsProblem(Log.ERROR, "Error reading settings: " + e);
            Slog.wtf(PackageManagerService.TAG, "Error reading package manager settings", e);
            Slog.wtf(PackageManagerService.TAG, "Error reading package manager settings", e);
Loading