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

Commit 0c8b84e4 authored by Bernardo Rufino's avatar Bernardo Rufino
Browse files

Move {get,set}AncestralSerialNumber() to Trampoline

From BMS.

Test: atest BackupManagerServiceTest TrampolineRoboTest TrampolineTest
Bug: 135661048
Change-Id: I48fd336dce36f39a7b0e7af5f0f9740ee2b4e246
parent 470f8be8
Loading
Loading
Loading
Loading
+0 −53
Original line number Diff line number Diff line
@@ -30,11 +30,9 @@ import android.app.job.JobScheduler;
import android.app.job.JobService;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Binder;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.SparseArray;

import com.android.internal.annotations.VisibleForTesting;
@@ -100,57 +98,6 @@ public class BackupManagerService {
     */
    // TODO (b/118520567): Stop hardcoding system user when we pass in user id as a parameter

    /**
     * Sets the ancestral work profile for the calling user.
     *
     * <p> The ancestral work profile corresponds to the profile that was used to restore to the
     * callers profile.
     */
    public void setAncestralSerialNumber(long ancestralSerialNumber) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(
                        Binder.getCallingUserHandle().getIdentifier(),
                        "setAncestralSerialNumber()");

        if (userBackupManagerService != null) {
            userBackupManagerService.setAncestralSerialNumber(ancestralSerialNumber);
        }
    }

    /**
     * Returns a {@link UserHandle} for the user that has {@code ancestralSerialNumber} as the
     * serial number of the its ancestral work profile or null if there is no {@link
     * UserBackupManagerService} associated with that user.
     *
     * <p> The ancestral work profile is set by {@link #setAncestralSerialNumber(long)}
     * and it corresponds to the profile that was used to restore to the callers profile.
     */
    @Nullable
    public UserHandle getUserForAncestralSerialNumber(long ancestralSerialNumber) {
        int callingUserId = Binder.getCallingUserHandle().getIdentifier();
        long oldId = Binder.clearCallingIdentity();
        final int[] userIds;
        try {
            userIds =
                    mContext
                            .getSystemService(UserManager.class)
                            .getProfileIds(callingUserId, false);
        } finally {
            Binder.restoreCallingIdentity(oldId);
        }

        for (int userId : userIds) {
            UserBackupManagerService userBackupManagerService = mServiceUsers.get(userId);
            if (userBackupManagerService != null) {
                if (userBackupManagerService.getAncestralSerialNumber() == ancestralSerialNumber) {
                    return UserHandle.of(userId);
                }
            }
        }

        return null;
    }

    // ---------------------------------------------
    // SETTINGS OPERATIONS
    // ---------------------------------------------
+48 −4
Original line number Diff line number Diff line
@@ -1155,18 +1155,62 @@ public class Trampoline extends IBackupManager.Stub {
        cancelBackupsForUser(binderGetCallingUserId());
    }

    /**
     * Returns a {@link UserHandle} for the user that has {@code ancestralSerialNumber} as the
     * serial number of the its ancestral work profile or null if there is no {@link
     * UserBackupManagerService} associated with that user.
     *
     * <p> The ancestral work profile is set by {@link #setAncestralSerialNumber(long)}
     * and it corresponds to the profile that was used to restore to the callers profile.
     */
    @Override
    @Nullable public UserHandle getUserForAncestralSerialNumber(long ancestralSerialNumber) {
    @Nullable
    public UserHandle getUserForAncestralSerialNumber(long ancestralSerialNumber) {
        if (mGlobalDisable) {
            return null;
        }
        return mService.getUserForAncestralSerialNumber(ancestralSerialNumber);
        int callingUserId = Binder.getCallingUserHandle().getIdentifier();
        long oldId = Binder.clearCallingIdentity();
        final int[] userIds;
        try {
            userIds =
                    mContext
                            .getSystemService(UserManager.class)
                            .getProfileIds(callingUserId, false);
        } finally {
            Binder.restoreCallingIdentity(oldId);
        }

        for (int userId : userIds) {
            UserBackupManagerService userBackupManagerService = mUserServices.get(userId);
            if (userBackupManagerService != null) {
                if (userBackupManagerService.getAncestralSerialNumber() == ancestralSerialNumber) {
                    return UserHandle.of(userId);
                }
            }
        }

        return null;
    }

    /**
     * Sets the ancestral work profile for the calling user.
     *
     * <p> The ancestral work profile corresponds to the profile that was used to restore to the
     * callers profile.
     */
    @Override
    public void setAncestralSerialNumber(long ancestralSerialNumber) {
        if (!mGlobalDisable) {
            mService.setAncestralSerialNumber(ancestralSerialNumber);
        if (mGlobalDisable) {
            return;
        }
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(
                        Binder.getCallingUserHandle().getIdentifier(),
                        "setAncestralSerialNumber()");

        if (userBackupManagerService != null) {
            userBackupManagerService.setAncestralSerialNumber(ancestralSerialNumber);
        }
    }

+10 −6
Original line number Diff line number Diff line
@@ -16,18 +16,18 @@

package com.android.server.backup;

import static com.google.common.truth.Truth.assertThat;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
@@ -786,17 +786,21 @@ public class TrampolineTest {
    public void testGetUserForAncestralSerialNumber() {
        TrampolineTestable.sBackupDisabled = false;
        Trampoline trampoline = new TrampolineTestable(mContextMock, mUserServices);
        when(mUserBackupManagerService.getAncestralSerialNumber()).thenReturn(11L);

        UserHandle user = trampoline.getUserForAncestralSerialNumber(11L);

        trampoline.getUserForAncestralSerialNumber(0L);
        verify(mBackupManagerServiceMock).getUserForAncestralSerialNumber(anyInt());
        assertThat(user).isEqualTo(UserHandle.of(1));
    }

    public void testGetUserForAncestralSerialNumber_whenDisabled() {
        TrampolineTestable.sBackupDisabled = true;
        Trampoline trampoline = new TrampolineTestable(mContextMock, mUserServices);
        when(mUserBackupManagerService.getAncestralSerialNumber()).thenReturn(11L);

        UserHandle user = trampoline.getUserForAncestralSerialNumber(11L);

        trampoline.getUserForAncestralSerialNumber(0L);
        verify(mBackupManagerServiceMock, never()).getUserForAncestralSerialNumber(anyInt());
        assertThat(user).isNull();
    }

    private static class TrampolineTestable extends Trampoline {