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

Commit 6a93c233 authored by Annie Meng's avatar Annie Meng
Browse files

[Multi-user] Decouple UserBMS and BMS creation

The BMS constructor is now only responsible for setting up the system
service. User registration logic is moved to its own helper that
Trampoline calls during a user unlock callback.

** Logic **
All IBackupManager methods that BMS implements now use bookkeeping to
get the UserBMS instance to act on. Currently the system user instance
is hardcoded in most methods as we don't pass in the user id parameter
(except for a few added in ag/5667585).

If these methods are called on a non-registered user, we log and return
a default value.

** Tests **
Each IBackupManager method now has four corresponding tests:
1) Permission denial test: No INTERACT_ACROSS_USERS_FULL permission +
non-calling user id = security exception (added in ag/5667585, only for
methods that take in an user id param currently).
2) Permission grant test: INTERACT_ACROSS_USERS_FULL permission +
non-calling user id = call forwarded.
3) Registered user test: Passing an user id that has a corresponding
UserBMS instance -> delegate call to that UserBMS instance.
4) Unknown user test: Passing an user id that has no corresponding
UserBMS instance -> no call forwarded.

These tests will be updated when more methods take in an user id param.

Bug: 120212806
Test: 1) atest RunFrameworksServicesRoboTests
2) Unlock system user -> starts service, registers transports
3) adb shell bmgr flows
4) atest TrampolineTest
5) CTS + GTS Backup test cases
6) SUW + Cloud restore; D2D
Change-Id: Ic04f754c75af905ee34c609063c08406e23671d5
parent feb504cb
Loading
Loading
Loading
Loading
+311 −78

File changed.

Preview size limit exceeded, changes collapsed.

+21 −11
Original line number Diff line number Diff line
@@ -116,7 +116,7 @@ public class Trampoline extends IBackupManager.Stub {
        return SystemProperties.getBoolean(BACKUP_DISABLE_PROPERTY, false);
    }

    protected boolean isMultiUserEnabled() {
    private boolean isMultiUserEnabled() {
        return Settings.Global.getInt(
                mContext.getContentResolver(),
                Settings.Global.BACKUP_MULTI_USER_ENABLED,
@@ -145,6 +145,10 @@ public class Trampoline extends IBackupManager.Stub {
        return new BackupManagerService(mContext, this, mHandlerThread);
    }

    protected void postToHandler(Runnable runnable) {
        mHandler.post(runnable);
    }

    /**
     * Initialize {@link BackupManagerService} if the backup service is not disabled. Only the
     * system user can initialize the service.
@@ -174,14 +178,18 @@ public class Trampoline extends IBackupManager.Stub {
     * to initialize {@link BackupManagerService} and set backup state for the system user.
     */
    void initializeServiceAndUnlockSystemUser() {
        mHandler.post(
        postToHandler(
                () -> {
                    // Initialize the backup service.
                    Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backup init");
                    initializeService(UserHandle.USER_SYSTEM);
                    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);

                    // Start the service for the system user.
                    BackupManagerService service = mService;
                    if (service != null) {
                        Slog.i(TAG, "Starting service for system user");
                        service.startServiceForUser(UserHandle.USER_SYSTEM);
                        Slog.i(TAG, "Unlocking system user");
                        service.unlockSystemUser();
                    }
@@ -195,20 +203,21 @@ public class Trampoline extends IBackupManager.Stub {
     */
    // TODO(b/120212806): Consolidate service start for system and non-system users when system
    // user-only logic is removed.
    void startServiceForUser(int userId) {
    void unlockUser(int userId) {
        if (!isMultiUserEnabled()) {
            Slog.i(TAG, "Multi-user disabled, cannot start service for user: " + userId);
            return;
        }

        mHandler.post(
                () -> {
        postToHandler(() -> startServiceForUser(userId));
    }

    private void startServiceForUser(int userId) {
        BackupManagerService service = mService;
        if (service != null) {
            Slog.i(TAG, "Starting service for user: " + userId);
            service.startServiceForUser(userId);
        }
                });
    }

    /**
@@ -242,6 +251,7 @@ public class Trampoline extends IBackupManager.Stub {
            if (makeActive) {
                mService = createBackupManagerService();
                mSuppressFile.delete();
                startServiceForUser(userId);
            } else {
                mService = null;
                try {
+958 −222

File changed.

Preview size limit exceeded, changes collapsed.

+11 −4
Original line number Diff line number Diff line
@@ -132,19 +132,21 @@ public class TrampolineTest {
    }

    @Test
    public void startServiceForUser_whenMultiUserSettingDisabled_isIgnored() {
    public void unlockUser_whenMultiUserSettingDisabled_isIgnoredForNonSystemUser() {
        Settings.Global.putInt(mContentResolver, Settings.Global.BACKUP_MULTI_USER_ENABLED, 0);
        mTrampoline.initializeService(UserHandle.USER_SYSTEM);

        mTrampoline.startServiceForUser(10);
        mTrampoline.unlockUser(10);

        verify(mBackupManagerServiceMock, never()).startServiceForUser(10);
    }

    @Test
    public void startServiceForUser_whenMultiUserSettingEnabled_callsBackupManagerService() {
    public void unlockUser_whenMultiUserSettingEnabled_callsBackupManagerServiceForNonSystemUser() {
        Settings.Global.putInt(mContentResolver, Settings.Global.BACKUP_MULTI_USER_ENABLED, 1);
        mTrampoline.initializeService(UserHandle.USER_SYSTEM);

        mTrampoline.startServiceForUser(10);
        mTrampoline.unlockUser(10);

        verify(mBackupManagerServiceMock).startServiceForUser(10);
    }
@@ -989,6 +991,11 @@ public class TrampolineTest {
            return sBackupManagerServiceMock;
        }

        @Override
        protected void postToHandler(Runnable runnable) {
            runnable.run();
        }

        int getCreateServiceCallsCount() {
            return mCreateServiceCallsCount;
        }