Loading services/backup/java/com/android/server/backup/UserBackupManagerService.java +28 −17 Original line number Diff line number Diff line Loading @@ -2359,23 +2359,10 @@ public class UserBackupManagerService { } } while (headBusy); if (!runBackup) { if (DEBUG_SCHEDULING) { Slog.i( TAG, addUserIdToLogMessage( mUserId, "Nothing pending full backup; rescheduling +" + latency)); } final long deferTime = latency; // pin for the closure FullBackupJob.schedule(mUserId, mContext, deferTime, mConstants); return false; } // Okay, the top thing is ready for backup now. Do it. mFullBackupQueue.remove(0); if (runBackup) { CountDownLatch latch = new CountDownLatch(1); String[] pkg = new String[]{entry.packageName}; try { mRunningFullBackupTask = PerformFullTransportBackupTask.newWithCurrentTransport( this, mOperationStorage, Loading @@ -2389,6 +2376,28 @@ public class UserBackupManagerService { /* userInitiated */ false, "BMS.beginFullBackup()", getEligibilityRulesForOperation(OperationType.BACKUP)); } catch (IllegalStateException e) { Slog.w(TAG, "Failed to start backup", e); runBackup = false; } } if (!runBackup) { if (DEBUG_SCHEDULING) { Slog.i( TAG, addUserIdToLogMessage( mUserId, "Nothing pending full backup or failed to start the " + "operation; rescheduling +" + latency)); } final long deferTime = latency; // pin for the closure FullBackupJob.schedule(mUserId, mContext, deferTime, mConstants); return false; } // Okay, the top thing is ready for backup now. Do it. mFullBackupQueue.remove(0); // Acquiring wakelock for PerformFullTransportBackupTask before its start. mWakelock.acquire(); (new Thread(mRunningFullBackupTask)).start(); Loading Loading @@ -2884,7 +2893,6 @@ public class UserBackupManagerService { public void fullTransportBackup(String[] pkgNames) { mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "fullTransportBackup"); final int callingUserHandle = UserHandle.getCallingUserId(); // TODO: http://b/22388012 if (callingUserHandle != UserHandle.USER_SYSTEM) { Loading Loading @@ -2936,6 +2944,9 @@ public class UserBackupManagerService { for (String pkg : pkgNames) { enqueueFullBackup(pkg, now); } } catch (IllegalStateException e) { Slog.w(TAG, "Failed to start backup: ", e); return; } finally { Binder.restoreCallingIdentity(oldId); } Loading services/backup/java/com/android/server/backup/fullbackup/PerformFullTransportBackupTask.java +6 −0 Original line number Diff line number Diff line Loading @@ -99,6 +99,9 @@ import java.util.concurrent.atomic.AtomicLong; * mBackupRunner.getBackupResultBlocking(). */ public class PerformFullTransportBackupTask extends FullBackupTask implements BackupRestoreTask { /** * @throws IllegalStateException if there's no transport available. */ public static PerformFullTransportBackupTask newWithCurrentTransport( UserBackupManagerService backupManagerService, OperationStorage operationStorage, Loading @@ -115,6 +118,9 @@ public class PerformFullTransportBackupTask extends FullBackupTask implements Ba TransportManager transportManager = backupManagerService.getTransportManager(); TransportConnection transportConnection = transportManager.getCurrentTransportClient( caller); if (transportConnection == null) { throw new IllegalStateException("No TransportConnection available"); } OnTaskFinishedListener listener = listenerCaller -> transportManager.disposeOfTransportClient(transportConnection, Loading services/tests/servicestests/src/com/android/server/backup/fullbackup/PerformFullTransportBackupTaskTest.java 0 → 100644 +73 −0 Original line number Diff line number Diff line /* * Copyright (C) 2022 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.server.backup.fullbackup; import static org.junit.Assert.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; import android.platform.test.annotations.Presubmit; import androidx.test.runner.AndroidJUnit4; import com.android.server.backup.TransportManager; import com.android.server.backup.UserBackupManagerService; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @Presubmit @RunWith(AndroidJUnit4.class) public class PerformFullTransportBackupTaskTest { @Mock UserBackupManagerService mBackupManagerService; @Mock TransportManager mTransportManager; @Before public void setUp() { MockitoAnnotations.initMocks(this); when(mBackupManagerService.getTransportManager()).thenReturn(mTransportManager); } @Test public void testNewWithCurrentTransport_noTransportConnection_throws() { when(mTransportManager.getCurrentTransportClient(any())).thenReturn(null); assertThrows(IllegalStateException.class, () -> { PerformFullTransportBackupTask task = PerformFullTransportBackupTask .newWithCurrentTransport( mBackupManagerService, /* operationStorage */ null, /* observer */ null, /* whichPackages */ null, /* updateSchedule */ false, /* runningJob */ null, /* latch */ null, /* backupObserver */ null, /* monitor */ null, /* userInitiated */ false, /* caller */ null, /* backupEligibilityRules */ null); }); } } Loading
services/backup/java/com/android/server/backup/UserBackupManagerService.java +28 −17 Original line number Diff line number Diff line Loading @@ -2359,23 +2359,10 @@ public class UserBackupManagerService { } } while (headBusy); if (!runBackup) { if (DEBUG_SCHEDULING) { Slog.i( TAG, addUserIdToLogMessage( mUserId, "Nothing pending full backup; rescheduling +" + latency)); } final long deferTime = latency; // pin for the closure FullBackupJob.schedule(mUserId, mContext, deferTime, mConstants); return false; } // Okay, the top thing is ready for backup now. Do it. mFullBackupQueue.remove(0); if (runBackup) { CountDownLatch latch = new CountDownLatch(1); String[] pkg = new String[]{entry.packageName}; try { mRunningFullBackupTask = PerformFullTransportBackupTask.newWithCurrentTransport( this, mOperationStorage, Loading @@ -2389,6 +2376,28 @@ public class UserBackupManagerService { /* userInitiated */ false, "BMS.beginFullBackup()", getEligibilityRulesForOperation(OperationType.BACKUP)); } catch (IllegalStateException e) { Slog.w(TAG, "Failed to start backup", e); runBackup = false; } } if (!runBackup) { if (DEBUG_SCHEDULING) { Slog.i( TAG, addUserIdToLogMessage( mUserId, "Nothing pending full backup or failed to start the " + "operation; rescheduling +" + latency)); } final long deferTime = latency; // pin for the closure FullBackupJob.schedule(mUserId, mContext, deferTime, mConstants); return false; } // Okay, the top thing is ready for backup now. Do it. mFullBackupQueue.remove(0); // Acquiring wakelock for PerformFullTransportBackupTask before its start. mWakelock.acquire(); (new Thread(mRunningFullBackupTask)).start(); Loading Loading @@ -2884,7 +2893,6 @@ public class UserBackupManagerService { public void fullTransportBackup(String[] pkgNames) { mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "fullTransportBackup"); final int callingUserHandle = UserHandle.getCallingUserId(); // TODO: http://b/22388012 if (callingUserHandle != UserHandle.USER_SYSTEM) { Loading Loading @@ -2936,6 +2944,9 @@ public class UserBackupManagerService { for (String pkg : pkgNames) { enqueueFullBackup(pkg, now); } } catch (IllegalStateException e) { Slog.w(TAG, "Failed to start backup: ", e); return; } finally { Binder.restoreCallingIdentity(oldId); } Loading
services/backup/java/com/android/server/backup/fullbackup/PerformFullTransportBackupTask.java +6 −0 Original line number Diff line number Diff line Loading @@ -99,6 +99,9 @@ import java.util.concurrent.atomic.AtomicLong; * mBackupRunner.getBackupResultBlocking(). */ public class PerformFullTransportBackupTask extends FullBackupTask implements BackupRestoreTask { /** * @throws IllegalStateException if there's no transport available. */ public static PerformFullTransportBackupTask newWithCurrentTransport( UserBackupManagerService backupManagerService, OperationStorage operationStorage, Loading @@ -115,6 +118,9 @@ public class PerformFullTransportBackupTask extends FullBackupTask implements Ba TransportManager transportManager = backupManagerService.getTransportManager(); TransportConnection transportConnection = transportManager.getCurrentTransportClient( caller); if (transportConnection == null) { throw new IllegalStateException("No TransportConnection available"); } OnTaskFinishedListener listener = listenerCaller -> transportManager.disposeOfTransportClient(transportConnection, Loading
services/tests/servicestests/src/com/android/server/backup/fullbackup/PerformFullTransportBackupTaskTest.java 0 → 100644 +73 −0 Original line number Diff line number Diff line /* * Copyright (C) 2022 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.server.backup.fullbackup; import static org.junit.Assert.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; import android.platform.test.annotations.Presubmit; import androidx.test.runner.AndroidJUnit4; import com.android.server.backup.TransportManager; import com.android.server.backup.UserBackupManagerService; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @Presubmit @RunWith(AndroidJUnit4.class) public class PerformFullTransportBackupTaskTest { @Mock UserBackupManagerService mBackupManagerService; @Mock TransportManager mTransportManager; @Before public void setUp() { MockitoAnnotations.initMocks(this); when(mBackupManagerService.getTransportManager()).thenReturn(mTransportManager); } @Test public void testNewWithCurrentTransport_noTransportConnection_throws() { when(mTransportManager.getCurrentTransportClient(any())).thenReturn(null); assertThrows(IllegalStateException.class, () -> { PerformFullTransportBackupTask task = PerformFullTransportBackupTask .newWithCurrentTransport( mBackupManagerService, /* operationStorage */ null, /* observer */ null, /* whichPackages */ null, /* updateSchedule */ false, /* runningJob */ null, /* latch */ null, /* backupObserver */ null, /* monitor */ null, /* userInitiated */ false, /* caller */ null, /* backupEligibilityRules */ null); }); } }