Merge "Add a null check when getting TransportConnection in PFTBT" into tm-dev

This commit is contained in:
Ruslan Tkhakokhov
2022-05-27 12:28:44 +00:00
committed by Android (Google) Code Review
3 changed files with 107 additions and 17 deletions

View File

@@ -2359,13 +2359,37 @@ public class UserBackupManagerService {
} }
} while (headBusy); } while (headBusy);
if (runBackup) {
CountDownLatch latch = new CountDownLatch(1);
String[] pkg = new String[]{entry.packageName};
try {
mRunningFullBackupTask = PerformFullTransportBackupTask.newWithCurrentTransport(
this,
mOperationStorage,
/* observer */ null,
pkg,
/* updateSchedule */ true,
scheduledJob,
latch,
/* backupObserver */ null,
/* monitor */ null,
/* userInitiated */ false,
"BMS.beginFullBackup()",
getEligibilityRulesForOperation(OperationType.BACKUP));
} catch (IllegalStateException e) {
Slog.w(TAG, "Failed to start backup", e);
runBackup = false;
}
}
if (!runBackup) { if (!runBackup) {
if (DEBUG_SCHEDULING) { if (DEBUG_SCHEDULING) {
Slog.i( Slog.i(
TAG, TAG,
addUserIdToLogMessage( addUserIdToLogMessage(
mUserId, mUserId,
"Nothing pending full backup; rescheduling +" + latency)); "Nothing pending full backup or failed to start the "
+ "operation; rescheduling +" + latency));
} }
final long deferTime = latency; // pin for the closure final long deferTime = latency; // pin for the closure
FullBackupJob.schedule(mUserId, mContext, deferTime, mConstants); FullBackupJob.schedule(mUserId, mContext, deferTime, mConstants);
@@ -2374,21 +2398,6 @@ public class UserBackupManagerService {
// Okay, the top thing is ready for backup now. Do it. // Okay, the top thing is ready for backup now. Do it.
mFullBackupQueue.remove(0); mFullBackupQueue.remove(0);
CountDownLatch latch = new CountDownLatch(1);
String[] pkg = new String[]{entry.packageName};
mRunningFullBackupTask = PerformFullTransportBackupTask.newWithCurrentTransport(
this,
mOperationStorage,
/* observer */ null,
pkg,
/* updateSchedule */ true,
scheduledJob,
latch,
/* backupObserver */ null,
/* monitor */ null,
/* userInitiated */ false,
"BMS.beginFullBackup()",
getEligibilityRulesForOperation(OperationType.BACKUP));
// Acquiring wakelock for PerformFullTransportBackupTask before its start. // Acquiring wakelock for PerformFullTransportBackupTask before its start.
mWakelock.acquire(); mWakelock.acquire();
(new Thread(mRunningFullBackupTask)).start(); (new Thread(mRunningFullBackupTask)).start();
@@ -2884,7 +2893,6 @@ public class UserBackupManagerService {
public void fullTransportBackup(String[] pkgNames) { public void fullTransportBackup(String[] pkgNames) {
mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, mContext.enforceCallingPermission(android.Manifest.permission.BACKUP,
"fullTransportBackup"); "fullTransportBackup");
final int callingUserHandle = UserHandle.getCallingUserId(); final int callingUserHandle = UserHandle.getCallingUserId();
// TODO: http://b/22388012 // TODO: http://b/22388012
if (callingUserHandle != UserHandle.USER_SYSTEM) { if (callingUserHandle != UserHandle.USER_SYSTEM) {
@@ -2936,6 +2944,9 @@ public class UserBackupManagerService {
for (String pkg : pkgNames) { for (String pkg : pkgNames) {
enqueueFullBackup(pkg, now); enqueueFullBackup(pkg, now);
} }
} catch (IllegalStateException e) {
Slog.w(TAG, "Failed to start backup: ", e);
return;
} finally { } finally {
Binder.restoreCallingIdentity(oldId); Binder.restoreCallingIdentity(oldId);
} }

View File

@@ -99,6 +99,9 @@ import java.util.concurrent.atomic.AtomicLong;
* mBackupRunner.getBackupResultBlocking(). * mBackupRunner.getBackupResultBlocking().
*/ */
public class PerformFullTransportBackupTask extends FullBackupTask implements BackupRestoreTask { public class PerformFullTransportBackupTask extends FullBackupTask implements BackupRestoreTask {
/**
* @throws IllegalStateException if there's no transport available.
*/
public static PerformFullTransportBackupTask newWithCurrentTransport( public static PerformFullTransportBackupTask newWithCurrentTransport(
UserBackupManagerService backupManagerService, UserBackupManagerService backupManagerService,
OperationStorage operationStorage, OperationStorage operationStorage,
@@ -115,6 +118,9 @@ public class PerformFullTransportBackupTask extends FullBackupTask implements Ba
TransportManager transportManager = backupManagerService.getTransportManager(); TransportManager transportManager = backupManagerService.getTransportManager();
TransportConnection transportConnection = transportManager.getCurrentTransportClient( TransportConnection transportConnection = transportManager.getCurrentTransportClient(
caller); caller);
if (transportConnection == null) {
throw new IllegalStateException("No TransportConnection available");
}
OnTaskFinishedListener listener = OnTaskFinishedListener listener =
listenerCaller -> listenerCaller ->
transportManager.disposeOfTransportClient(transportConnection, transportManager.disposeOfTransportClient(transportConnection,

View File

@@ -0,0 +1,73 @@
/*
* 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);
});
}
}