Wrap IBackupTransport usages with BackupTransportClient

Bug: 202716271
Change-Id: I4899fea3342f9e913fa1afa14ed7b67481f02908
This commit is contained in:
Ruslan Tkhakokhov
2021-12-04 11:12:20 +00:00
parent 32cb668a40
commit 622b968123
16 changed files with 144 additions and 125 deletions

View File

@@ -39,6 +39,7 @@ import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.backup.IBackupTransport;
import com.android.internal.util.Preconditions;
import com.android.server.backup.transport.BackupTransportClient;
import com.android.server.backup.transport.OnTransportRegisteredListener;
import com.android.server.backup.transport.TransportConnection;
import com.android.server.backup.transport.TransportConnectionManager;
@@ -641,7 +642,7 @@ public class TransportManager {
TransportConnection transportConnection =
mTransportConnectionManager.getTransportClient(
transportComponent, extras, callerLogString);
final IBackupTransport transport;
final BackupTransportClient transport;
try {
transport = transportConnection.connectOrThrow(callerLogString);
} catch (TransportNotAvailableException e) {
@@ -653,10 +654,6 @@ public class TransportManager {
int result;
try {
// This is a temporary fix to allow blocking calls.
// TODO: b/147702043. Redesign IBackupTransport so as to make the calls non-blocking.
Binder.allowBlocking(transport.asBinder());
String transportName = transport.name();
String transportDirName = transport.transportDirName();
registerTransport(transportComponent, transport);
@@ -674,8 +671,8 @@ public class TransportManager {
}
/** If {@link RemoteException} is thrown the transport is guaranteed to not be registered. */
private void registerTransport(ComponentName transportComponent, IBackupTransport transport)
throws RemoteException {
private void registerTransport(ComponentName transportComponent,
BackupTransportClient transport) throws RemoteException {
checkCanUseTransport();
TransportDescription description =

View File

@@ -21,6 +21,7 @@ import android.app.backup.RestoreDescription;
import android.app.backup.RestoreSet;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.os.Binder;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
@@ -35,6 +36,10 @@ public class BackupTransportClient {
BackupTransportClient(IBackupTransport transportBinder) {
mTransportBinder = transportBinder;
// This is a temporary fix to allow blocking calls.
// TODO: b/147702043. Redesign IBackupTransport so as to make the calls non-blocking.
Binder.allowBlocking(mTransportBinder.asBinder());
}
/**

View File

@@ -59,7 +59,7 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
/**
* A {@link TransportConnection} manages the connection to an {@link IBackupTransport} service,
* A {@link TransportConnection} manages the connection to a {@link BackupTransportClient},
* obtained via the {@param bindIntent} parameter provided in the constructor. A
* {@link TransportConnection} is responsible for only one connection to the transport service,
* not more.
@@ -67,9 +67,9 @@ import java.util.concurrent.ExecutionException;
* <p>After retrieved using {@link TransportManager#getTransportClient(String, String)}, you can
* call either {@link #connect(String)}, if you can block your thread, or {@link
* #connectAsync(TransportConnectionListener, String)}, otherwise, to obtain a {@link
* IBackupTransport} instance. It's meant to be passed around as a token to a connected transport.
* When the connection is not needed anymore you should call {@link #unbind(String)} or indirectly
* via {@link TransportManager#disposeOfTransportClient(TransportConnection, String)}.
* BackupTransportClient} instance. It's meant to be passed around as a token to a connected
* transport. When the connection is not needed anymore you should call {@link #unbind(String)} or
* indirectly via {@link TransportManager#disposeOfTransportClient(TransportConnection, String)}.
*
* <p>DO NOT forget to unbind otherwise there will be dangling connections floating around.
*
@@ -106,7 +106,7 @@ public class TransportConnection {
private int mState = State.IDLE;
@GuardedBy("mStateLock")
private volatile IBackupTransport mTransport;
private volatile BackupTransportClient mTransport;
TransportConnection(
@UserIdInt int userId,
@@ -174,10 +174,12 @@ public class TransportConnection {
* trigger another one, just piggyback on the original request.
*
* <p>It's guaranteed that you are going to get a call back to {@param listener} after this
* call. However, the {@param IBackupTransport} parameter, the transport binder, is not
* guaranteed to be non-null, or if it's non-null it's not guaranteed to be usable - i.e. it can
* throw {@link DeadObjectException}s on method calls. You should check for both in your code.
* The reasons for a null transport binder are:
* call. However, the {@link BackupTransportClient} parameter in
* {@link TransportConnectionListener#onTransportConnectionResult(BackupTransportClient,
* TransportConnection)}, the transport client, is not guaranteed to be non-null, or if it's
* non-null it's not guaranteed to be usable - i.e. it can throw {@link DeadObjectException}s
* on method calls. You should check for both in your code. The reasons for a null transport
* client are:
*
* <ul>
* <li>Some code called {@link #unbind(String)} before you got a callback.
@@ -193,7 +195,7 @@ public class TransportConnection {
* For unusable transport binders check {@link DeadObjectException}.
*
* @param listener The listener that will be called with the (possibly null or unusable) {@link
* IBackupTransport} instance and this {@link TransportConnection} object.
* BackupTransportClient} instance and this {@link TransportConnection} object.
* @param caller A {@link String} identifying the caller for logging/debugging purposes. This
* should be a human-readable short string that is easily identifiable in the logs. Ideally
* TAG.methodName(), where TAG is the one used in logcat. In cases where this is is not very
@@ -293,8 +295,8 @@ public class TransportConnection {
*
* <p>Synchronous version of {@link #connectAsync(TransportConnectionListener, String)}. The
* same observations about state are valid here. Also, what was said about the {@link
* IBackupTransport} parameter of {@link TransportConnectionListener} now apply to the return
* value of this method.
* BackupTransportClient} parameter of {@link TransportConnectionListener} now apply to the
* return value of this method.
*
* <p>This is a potentially blocking operation, so be sure to call this carefully on the correct
* threads. You can't call this from the process main-thread (it throws an exception if you do
@@ -305,18 +307,18 @@ public class TransportConnection {
*
* @param caller A {@link String} identifying the caller for logging/debugging purposes. Check
* {@link #connectAsync(TransportConnectionListener, String)} for more details.
* @return A {@link IBackupTransport} transport binder instance or null. If it's non-null it can
* still be unusable - throws {@link DeadObjectException} on method calls
* @return A {@link BackupTransportClient} transport client instance or null. If it's non-null
* it can still be unusable - throws {@link DeadObjectException} on method calls
*/
@WorkerThread
@Nullable
public IBackupTransport connect(String caller) {
public BackupTransportClient connect(String caller) {
// If called on the main-thread this could deadlock waiting because calls to
// ServiceConnection are on the main-thread as well
Preconditions.checkState(
!Looper.getMainLooper().isCurrentThread(), "Can't call connect() on main thread");
IBackupTransport transport = mTransport;
BackupTransportClient transport = mTransport;
if (transport != null) {
log(Priority.DEBUG, caller, "Sync connect: reusing transport");
return transport;
@@ -330,7 +332,7 @@ public class TransportConnection {
}
}
CompletableFuture<IBackupTransport> transportFuture = new CompletableFuture<>();
CompletableFuture<BackupTransportClient> transportFuture = new CompletableFuture<>();
TransportConnectionListener requestListener =
(requestedTransport, transportClient) ->
transportFuture.complete(requestedTransport);
@@ -359,13 +361,14 @@ public class TransportConnection {
*
* @param caller A {@link String} identifying the caller for logging/debugging purposes. Check
* {@link #connectAsync(TransportConnectionListener, String)} for more details.
* @return A {@link IBackupTransport} transport binder instance.
* @return A {@link BackupTransportClient} transport binder instance.
* @see #connect(String)
* @throws TransportNotAvailableException if connection attempt fails.
*/
@WorkerThread
public IBackupTransport connectOrThrow(String caller) throws TransportNotAvailableException {
IBackupTransport transport = connect(caller);
public BackupTransportClient connectOrThrow(String caller)
throws TransportNotAvailableException {
BackupTransportClient transport = connect(caller);
if (transport == null) {
log(Priority.ERROR, caller, "Transport connection failed");
throw new TransportNotAvailableException();
@@ -379,12 +382,12 @@ public class TransportConnection {
*
* @param caller A {@link String} identifying the caller for logging/debugging purposes. Check
* {@link #connectAsync(TransportConnectionListener, String)} for more details.
* @return A {@link IBackupTransport} transport binder instance.
* @return A {@link BackupTransportClient} transport client instance.
* @throws TransportNotAvailableException if not connected.
*/
public IBackupTransport getConnectedTransport(String caller)
public BackupTransportClient getConnectedTransport(String caller)
throws TransportNotAvailableException {
IBackupTransport transport = mTransport;
BackupTransportClient transport = mTransport;
if (transport == null) {
log(Priority.ERROR, caller, "Transport not connected");
throw new TransportNotAvailableException();
@@ -425,7 +428,8 @@ public class TransportConnection {
}
private void onServiceConnected(IBinder binder) {
IBackupTransport transport = IBackupTransport.Stub.asInterface(binder);
IBackupTransport transportBinder = IBackupTransport.Stub.asInterface(binder);
BackupTransportClient transport = new BackupTransportClient(transportBinder);
synchronized (mStateLock) {
checkStateIntegrityLocked();
@@ -492,15 +496,15 @@ public class TransportConnection {
private void notifyListener(
TransportConnectionListener listener,
@Nullable IBackupTransport transport,
@Nullable BackupTransportClient transport,
String caller) {
String transportString = (transport != null) ? "IBackupTransport" : "null";
String transportString = (transport != null) ? "BackupTransportClient" : "null";
log(Priority.INFO, "Notifying [" + caller + "] transport = " + transportString);
mListenerHandler.post(() -> listener.onTransportConnectionResult(transport, this));
}
@GuardedBy("mStateLock")
private void notifyListenersAndClearLocked(@Nullable IBackupTransport transport) {
private void notifyListenersAndClearLocked(@Nullable BackupTransportClient transport) {
for (Map.Entry<TransportConnectionListener, String> entry : mListeners.entrySet()) {
TransportConnectionListener listener = entry.getKey();
String caller = entry.getValue();
@@ -510,7 +514,7 @@ public class TransportConnection {
}
@GuardedBy("mStateLock")
private void setStateLocked(@State int state, @Nullable IBackupTransport transport) {
private void setStateLocked(@State int state, @Nullable BackupTransportClient transport) {
log(Priority.VERBOSE, "State: " + stateToString(mState) + " => " + stateToString(state));
onStateTransition(mState, state);
mState = state;

View File

@@ -18,7 +18,7 @@ package com.android.server.backup.transport;
import android.annotation.Nullable;
import com.android.internal.backup.IBackupTransport;
import com.android.server.backup.transport.BackupTransportClient;
/**
* Listener to be called by {@link TransportConnection#connectAsync(TransportConnectionListener,
@@ -26,13 +26,14 @@ import com.android.internal.backup.IBackupTransport;
*/
public interface TransportConnectionListener {
/**
* Called when {@link TransportConnection} has a transport binder available or that it decided
* Called when {@link TransportConnection} has a transport client available or that it decided
* it couldn't obtain one, in which case {@param transport} is null.
*
* @param transport A {@link IBackupTransport} transport binder or null.
* @param transportClient A {@link BackupTransportClient} transport or null.
* @param transportConnection The {@link TransportConnection} used to retrieve this transport
* binder.
* client.
*/
void onTransportConnectionResult(
@Nullable IBackupTransport transport, TransportConnection transportConnection);
@Nullable BackupTransportClient transportClient,
TransportConnection transportConnection);
}

View File

@@ -103,7 +103,6 @@ import android.util.SparseArray;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.backup.IBackupTransport;
import com.android.internal.util.Preconditions;
import com.android.server.AppWidgetBackupBridge;
import com.android.server.EventLogTags;
@@ -127,6 +126,7 @@ import com.android.server.backup.params.ClearRetryParams;
import com.android.server.backup.params.RestoreParams;
import com.android.server.backup.restore.ActiveRestoreSession;
import com.android.server.backup.restore.PerformUnifiedRestoreTask;
import com.android.server.backup.transport.BackupTransportClient;
import com.android.server.backup.transport.TransportConnection;
import com.android.server.backup.transport.TransportNotAvailableException;
import com.android.server.backup.transport.TransportNotRegisteredException;
@@ -3719,7 +3719,8 @@ public class UserBackupManagerService {
mTransportManager.getTransportClient(newTransportName, callerLogString);
if (transportConnection != null) {
try {
IBackupTransport transport = transportConnection.connectOrThrow(callerLogString);
BackupTransportClient transport = transportConnection.connectOrThrow(
callerLogString);
mCurrentToken = transport.getCurrentRestoreSet();
} catch (Exception e) {
// Oops. We can't know the current dataset token, so reset and figure it out
@@ -4371,7 +4372,7 @@ public class UserBackupManagerService {
final long oldCallingId = Binder.clearCallingIdentity();
try {
IBackupTransport transport = transportConnection.connectOrThrow(
BackupTransportClient transport = transportConnection.connectOrThrow(
/* caller */ "BMS.getOperationTypeFromTransport");
if ((transport.getTransportFlags() & BackupAgent.FLAG_DEVICE_TO_DEVICE_TRANSFER) != 0) {
return OperationType.MIGRATION;

View File

@@ -41,7 +41,6 @@ import android.util.EventLog;
import android.util.Log;
import android.util.Slog;
import com.android.internal.backup.IBackupTransport;
import com.android.server.EventLogTags;
import com.android.server.backup.BackupAgentTimeoutParameters;
import com.android.server.backup.BackupRestoreTask;
@@ -51,6 +50,7 @@ import com.android.server.backup.UserBackupManagerService;
import com.android.server.backup.internal.OnTaskFinishedListener;
import com.android.server.backup.internal.Operation;
import com.android.server.backup.remote.RemoteCall;
import com.android.server.backup.transport.BackupTransportClient;
import com.android.server.backup.transport.TransportConnection;
import com.android.server.backup.transport.TransportNotAvailableException;
import com.android.server.backup.utils.BackupEligibilityRules;
@@ -300,7 +300,7 @@ public class PerformFullTransportBackupTask extends FullBackupTask implements Ba
mUserBackupManagerService.handleCancel(mBackupRunnerOpToken, cancelAll);
try {
// If we're running a backup we should be connected to a transport
IBackupTransport transport =
BackupTransportClient transport =
mTransportConnection.getConnectedTransport("PFTBT.handleCancel()");
transport.cancelFullBackup();
} catch (RemoteException | TransportNotAvailableException e) {
@@ -353,7 +353,7 @@ public class PerformFullTransportBackupTask extends FullBackupTask implements Ba
return;
}
IBackupTransport transport = mTransportConnection.connect("PFTBT.run()");
BackupTransportClient transport = mTransportConnection.connect("PFTBT.run()");
if (transport == null) {
Slog.w(TAG, "Transport not present; full data backup not performed");
backupRunStatus = BackupManager.ERROR_TRANSPORT_ABORTED;
@@ -745,7 +745,7 @@ public class PerformFullTransportBackupTask extends FullBackupTask implements Ba
Slog.v(TAG, "Got preflight response; size=" + totalSize);
}
IBackupTransport transport =
BackupTransportClient transport =
mTransportConnection.connectOrThrow("PFTBT$SPBP.preflightFullBackup()");
result = transport.checkFullBackupSize(totalSize);
if (result == BackupTransport.TRANSPORT_QUOTA_EXCEEDED) {

View File

@@ -31,7 +31,6 @@ import android.util.Pair;
import android.util.Slog;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.backup.IBackupTransport;
import com.android.server.EventLogTags;
import com.android.server.backup.BackupAgentTimeoutParameters;
import com.android.server.backup.BackupRestoreTask;
@@ -51,6 +50,7 @@ import com.android.server.backup.params.RestoreGetSetsParams;
import com.android.server.backup.params.RestoreParams;
import com.android.server.backup.restore.PerformAdbRestoreTask;
import com.android.server.backup.restore.PerformUnifiedRestoreTask;
import com.android.server.backup.transport.BackupTransportClient;
import com.android.server.backup.transport.TransportConnection;
import java.util.ArrayList;
@@ -149,7 +149,7 @@ public class BackupHandler extends Handler {
String callerLogString = "BH/MSG_RUN_BACKUP";
TransportConnection transportConnection =
transportManager.getCurrentTransportClient(callerLogString);
IBackupTransport transport =
BackupTransportClient transport =
transportConnection != null
? transportConnection.connect(callerLogString)
: null;
@@ -364,7 +364,7 @@ public class BackupHandler extends Handler {
RestoreGetSetsParams params = (RestoreGetSetsParams) msg.obj;
String callerLogString = "BH/MSG_RUN_GET_RESTORE_SETS";
try {
IBackupTransport transport =
BackupTransportClient transport =
params.mTransportConnection.connectOrThrow(callerLogString);
sets = transport.getAvailableRestoreSets();
// cache the result in the active session

View File

@@ -21,9 +21,9 @@ import static com.android.server.backup.BackupManagerService.TAG;
import android.content.pm.PackageInfo;
import android.util.Slog;
import com.android.internal.backup.IBackupTransport;
import com.android.server.backup.TransportManager;
import com.android.server.backup.UserBackupManagerService;
import com.android.server.backup.transport.BackupTransportClient;
import com.android.server.backup.transport.TransportConnection;
import java.io.File;
@@ -47,7 +47,7 @@ public class PerformClearTask implements Runnable {
public void run() {
String callerLogString = "PerformClearTask.run()";
IBackupTransport transport = null;
BackupTransportClient transport = null;
try {
// Clear the on-device backup state to ensure a full backup next time
String transportDirName =

View File

@@ -28,10 +28,10 @@ import android.util.EventLog;
import android.util.Slog;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.backup.IBackupTransport;
import com.android.server.EventLogTags;
import com.android.server.backup.TransportManager;
import com.android.server.backup.UserBackupManagerService;
import com.android.server.backup.transport.BackupTransportClient;
import com.android.server.backup.transport.TransportConnection;
import java.io.File;
@@ -128,7 +128,8 @@ public class PerformInitializeTask implements Runnable {
EventLog.writeEvent(EventLogTags.BACKUP_START, transportDirName);
long startRealtime = SystemClock.elapsedRealtime();
IBackupTransport transport = transportConnection.connectOrThrow(callerLogString);
BackupTransportClient transport = transportConnection.connectOrThrow(
callerLogString);
int status = transport.initializeDevice();
if (status != BackupTransport.TRANSPORT_OK) {
Slog.e(TAG, "Transport error in initializeDevice()");

View File

@@ -51,7 +51,6 @@ import android.util.Log;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.backup.IBackupTransport;
import com.android.internal.util.Preconditions;
import com.android.server.AppWidgetBackupBridge;
import com.android.server.backup.BackupAgentTimeoutParameters;
@@ -65,6 +64,7 @@ import com.android.server.backup.internal.Operation;
import com.android.server.backup.remote.RemoteCall;
import com.android.server.backup.remote.RemoteCallable;
import com.android.server.backup.remote.RemoteResult;
import com.android.server.backup.transport.BackupTransportClient;
import com.android.server.backup.transport.TransportConnection;
import com.android.server.backup.transport.TransportNotAvailableException;
import com.android.server.backup.utils.BackupEligibilityRules;
@@ -111,7 +111,7 @@ import java.util.concurrent.atomic.AtomicInteger;
* </ul>
*
* If there is no PackageManager (PM) pseudo-package state file in the state directory, the
* specified transport will be initialized with {@link IBackupTransport#initializeDevice()}.
* specified transport will be initialized with {@link BackupTransportClient#initializeDevice()}.
*
* <p>The PM pseudo-package is the first package to be backed-up and sent to the transport in case
* of incremental choice. If non-incremental, PM will only be backed-up if specified in the queue,
@@ -141,8 +141,8 @@ import java.util.concurrent.atomic.AtomicInteger;
* </ul>
* <li>Unbind the agent.
* <li>Assuming agent response, send the staged data that the agent wrote to disk to the transport
* via {@link IBackupTransport#performBackup(PackageInfo, ParcelFileDescriptor, int)}.
* <li>Call {@link IBackupTransport#finishBackup()} if previous call was successful.
* via {@link BackupTransportClient#performBackup(PackageInfo, ParcelFileDescriptor, int)}.
* <li>Call {@link BackupTransportClient#finishBackup()} if previous call was successful.
* <li>Save the new state in the state file. During the agent call it was being written to
* &lt;state file&gt;.new, here we rename it and replace the old one.
* <li>Delete the stage file.
@@ -155,7 +155,7 @@ import java.util.concurrent.atomic.AtomicInteger;
* <li>Delete the {@link DataChangedJournal} provided. Note that this should not be the current
* journal.
* <li>Set {@link UserBackupManagerService} current token as {@link
* IBackupTransport#getCurrentRestoreSet()}, if applicable.
* BackupTransportClient#getCurrentRestoreSet()}, if applicable.
* <li>Add the transport to the list of transports pending initialization ({@link
* UserBackupManagerService#getPendingInits()}) and kick-off initialization if the transport
* ever returned {@link BackupTransport#TRANSPORT_NOT_INITIALIZED}.
@@ -194,7 +194,7 @@ public class KeyValueBackupTask implements BackupRestoreTask, Runnable {
* @param backupManagerService The {@link UserBackupManagerService} instance.
* @param transportConnection The {@link TransportConnection} that contains the transport used
* for the operation.
* @param transportDirName The value of {@link IBackupTransport#transportDirName()} for the
* @param transportDirName The value of {@link BackupTransportClient#transportDirName()} for the
* transport whose {@link TransportConnection} was provided above.
* @param queue The list of package names that will be backed-up.
* @param dataChangedJournal The old data-changed journal file that will be deleted when the
@@ -417,7 +417,7 @@ public class KeyValueBackupTask implements BackupRestoreTask, Runnable {
boolean noDataPackageEncountered = false;
try {
IBackupTransport transport =
BackupTransportClient transport =
mTransportConnection.connectOrThrow("KVBT.informTransportOfEmptyBackups()");
for (String packageName : succeedingPackages) {
@@ -467,8 +467,8 @@ public class KeyValueBackupTask implements BackupRestoreTask, Runnable {
}
/** Send the "no data changed" message to a transport for a specific package */
private void sendNoDataChangedTo(IBackupTransport transport, PackageInfo packageInfo, int flags)
throws RemoteException {
private void sendNoDataChangedTo(BackupTransportClient transport, PackageInfo packageInfo,
int flags) throws RemoteException {
ParcelFileDescriptor pfd;
try {
pfd = ParcelFileDescriptor.open(mBlankStateFile, MODE_READ_ONLY | MODE_CREATE);
@@ -608,7 +608,8 @@ public class KeyValueBackupTask implements BackupRestoreTask, Runnable {
mReporter.onQueueReady(mQueue);
File pmState = new File(mStateDirectory, PM_PACKAGE);
try {
IBackupTransport transport = mTransportConnection.connectOrThrow("KVBT.startTask()");
BackupTransportClient transport = mTransportConnection.connectOrThrow(
"KVBT.startTask()");
String transportName = transport.name();
if (transportName.contains("EncryptedLocalTransport")) {
// Temporary code for EiTF POC. Only supports non-incremental backups.
@@ -764,7 +765,8 @@ public class KeyValueBackupTask implements BackupRestoreTask, Runnable {
long currentToken = mBackupManagerService.getCurrentToken();
if (mHasDataToBackup && (status == BackupTransport.TRANSPORT_OK) && (currentToken == 0)) {
try {
IBackupTransport transport = mTransportConnection.connectOrThrow(callerLogString);
BackupTransportClient transport = mTransportConnection.connectOrThrow(
callerLogString);
transportName = transport.name();
mBackupManagerService.setCurrentToken(transport.getCurrentRestoreSet());
mBackupManagerService.writeRestoreTokens();
@@ -835,7 +837,7 @@ public class KeyValueBackupTask implements BackupRestoreTask, Runnable {
@GuardedBy("mQueueLock")
private void triggerTransportInitializationLocked() throws Exception {
IBackupTransport transport =
BackupTransportClient transport =
mTransportConnection.connectOrThrow("KVBT.triggerTransportInitializationLocked");
mBackupManagerService.getPendingInits().add(transport.name());
deletePmStateFile();
@@ -919,7 +921,7 @@ public class KeyValueBackupTask implements BackupRestoreTask, Runnable {
}
}
IBackupTransport transport = mTransportConnection.connectOrThrow(
BackupTransportClient transport = mTransportConnection.connectOrThrow(
"KVBT.extractAgentData()");
long quota = transport.getBackupQuota(packageName, /* isFullBackup */ false);
int transportFlags = transport.getTransportFlags();
@@ -1078,7 +1080,7 @@ public class KeyValueBackupTask implements BackupRestoreTask, Runnable {
int status;
try (ParcelFileDescriptor backupData =
ParcelFileDescriptor.open(backupDataFile, MODE_READ_ONLY)) {
IBackupTransport transport =
BackupTransportClient transport =
mTransportConnection.connectOrThrow("KVBT.transportPerformBackup()");
mReporter.onTransportPerformBackup(packageName);
int flags = getPerformBackupFlags(mUserInitiated, nonIncremental);
@@ -1131,7 +1133,7 @@ public class KeyValueBackupTask implements BackupRestoreTask, Runnable {
private void agentDoQuotaExceeded(@Nullable IBackupAgent agent, String packageName, long size) {
if (agent != null) {
try {
IBackupTransport transport =
BackupTransportClient transport =
mTransportConnection.connectOrThrow("KVBT.agentDoQuotaExceeded()");
long quota = transport.getBackupQuota(packageName, false);
remoteCall(
@@ -1227,7 +1229,7 @@ public class KeyValueBackupTask implements BackupRestoreTask, Runnable {
mReporter.onRevertTask();
long delay;
try {
IBackupTransport transport =
BackupTransportClient transport =
mTransportConnection.connectOrThrow("KVBT.revertTask()");
delay = transport.requestBackupTime();
} catch (Exception e) {

View File

@@ -54,7 +54,6 @@ import android.util.EventLog;
import android.util.Slog;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.backup.IBackupTransport;
import com.android.server.AppWidgetBackupBridge;
import com.android.server.EventLogTags;
import com.android.server.LocalServices;
@@ -66,6 +65,7 @@ import com.android.server.backup.PackageManagerBackupAgent.Metadata;
import com.android.server.backup.TransportManager;
import com.android.server.backup.UserBackupManagerService;
import com.android.server.backup.internal.OnTaskFinishedListener;
import com.android.server.backup.transport.BackupTransportClient;
import com.android.server.backup.transport.TransportConnection;
import com.android.server.backup.utils.BackupEligibilityRules;
import com.android.server.backup.utils.BackupManagerMonitorUtils;
@@ -397,7 +397,7 @@ public class PerformUnifiedRestoreTask implements BackupRestoreTask {
PackageInfo[] packages = mAcceptSet.toArray(new PackageInfo[0]);
IBackupTransport transport =
BackupTransportClient transport =
mTransportConnection.connectOrThrow("PerformUnifiedRestoreTask.startRestore()");
mStatus = transport.startRestore(mToken, packages);
@@ -495,7 +495,7 @@ public class PerformUnifiedRestoreTask implements BackupRestoreTask {
private void dispatchNextRestore() {
UnifiedRestoreState nextState = UnifiedRestoreState.FINAL;
try {
IBackupTransport transport =
BackupTransportClient transport =
mTransportConnection.connectOrThrow(
"PerformUnifiedRestoreTask.dispatchNextRestore()");
mRestoreDescription = transport.nextRestorePackage();
@@ -709,7 +709,7 @@ public class PerformUnifiedRestoreTask implements BackupRestoreTask {
boolean startedAgentRestore = false;
try {
IBackupTransport transport =
BackupTransportClient transport =
mTransportConnection.connectOrThrow(
"PerformUnifiedRestoreTask.initiateOneRestore()");
@@ -940,7 +940,8 @@ public class PerformUnifiedRestoreTask implements BackupRestoreTask {
String callerLogString = "PerformUnifiedRestoreTask$StreamFeederThread.run()";
try {
IBackupTransport transport = mTransportConnection.connectOrThrow(callerLogString);
BackupTransportClient transport = mTransportConnection.connectOrThrow(
callerLogString);
while (status == BackupTransport.TRANSPORT_OK) {
// have the transport write some of the restoring data to us
int result = transport.getNextFullRestoreDataChunk(tWriteEnd);
@@ -1032,7 +1033,7 @@ public class PerformUnifiedRestoreTask implements BackupRestoreTask {
// Something went wrong somewhere. Whether it was at the transport
// level is immaterial; we need to tell the transport to bail
try {
IBackupTransport transport =
BackupTransportClient transport =
mTransportConnection.connectOrThrow(callerLogString);
transport.abortFullRestore();
} catch (Exception e) {
@@ -1095,7 +1096,7 @@ public class PerformUnifiedRestoreTask implements BackupRestoreTask {
String callerLogString = "PerformUnifiedRestoreTask.finalizeRestore()";
try {
IBackupTransport transport =
BackupTransportClient transport =
mTransportConnection.connectOrThrow(callerLogString);
transport.finishRestore();
} catch (Exception e) {

View File

@@ -40,8 +40,8 @@ import android.os.UserHandle;
import android.util.Slog;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.backup.IBackupTransport;
import com.android.internal.util.ArrayUtils;
import com.android.server.backup.transport.BackupTransportClient;
import com.android.server.backup.transport.TransportConnection;
import com.google.android.collect.Sets;
@@ -237,7 +237,7 @@ public class BackupEligibilityRules {
}
if (transportConnection != null) {
try {
IBackupTransport transport =
BackupTransportClient transport =
transportConnection.connectOrThrow(
"AppBackupUtils.appIsRunningAndEligibleForBackupWithTransport");
return transport.isAppEligibleForBackup(

View File

@@ -43,13 +43,13 @@ import android.os.DeadObjectException;
import android.platform.test.annotations.Presubmit;
import android.util.Log;
import com.android.internal.backup.IBackupTransport;
import com.android.server.backup.BackupManagerService;
import com.android.server.backup.TransportManager;
import com.android.server.backup.UserBackupManagerService;
import com.android.server.backup.testing.TransportData;
import com.android.server.backup.testing.TransportTestUtils;
import com.android.server.backup.testing.TransportTestUtils.TransportMock;
import com.android.server.backup.transport.BackupTransportClient;
import com.android.server.backup.transport.TransportConnection;
import com.android.server.testing.shadows.ShadowSlog;
@@ -75,7 +75,7 @@ public class PerformInitializeTaskTest {
@Mock private UserBackupManagerService mBackupManagerService;
@Mock private TransportManager mTransportManager;
@Mock private OnTaskFinishedListener mListener;
@Mock private IBackupTransport mTransportBinder;
@Mock private BackupTransportClient mTransportClient;
@Mock private IBackupObserver mObserver;
@Mock private AlarmManager mAlarmManager;
@Mock private PendingIntent mRunInitIntent;
@@ -101,19 +101,19 @@ public class PerformInitializeTaskTest {
@Test
public void testRun_callsTransportCorrectly() throws Exception {
setUpTransport(mTransport);
configureTransport(mTransportBinder, TRANSPORT_OK, TRANSPORT_OK);
configureTransport(mTransportClient, TRANSPORT_OK, TRANSPORT_OK);
PerformInitializeTask performInitializeTask = createPerformInitializeTask(mTransportName);
performInitializeTask.run();
verify(mTransportBinder).initializeDevice();
verify(mTransportBinder).finishBackup();
verify(mTransportClient).initializeDevice();
verify(mTransportClient).finishBackup();
}
@Test
public void testRun_callsBackupManagerCorrectly() throws Exception {
setUpTransport(mTransport);
configureTransport(mTransportBinder, TRANSPORT_OK, TRANSPORT_OK);
configureTransport(mTransportClient, TRANSPORT_OK, TRANSPORT_OK);
PerformInitializeTask performInitializeTask = createPerformInitializeTask(mTransportName);
performInitializeTask.run();
@@ -127,7 +127,7 @@ public class PerformInitializeTaskTest {
@Test
public void testRun_callsObserverAndListenerCorrectly() throws Exception {
setUpTransport(mTransport);
configureTransport(mTransportBinder, TRANSPORT_OK, TRANSPORT_OK);
configureTransport(mTransportClient, TRANSPORT_OK, TRANSPORT_OK);
PerformInitializeTask performInitializeTask = createPerformInitializeTask(mTransportName);
performInitializeTask.run();
@@ -140,13 +140,13 @@ public class PerformInitializeTaskTest {
@Test
public void testRun_whenInitializeDeviceFails() throws Exception {
setUpTransport(mTransport);
configureTransport(mTransportBinder, TRANSPORT_ERROR, 0);
configureTransport(mTransportClient, TRANSPORT_ERROR, 0);
PerformInitializeTask performInitializeTask = createPerformInitializeTask(mTransportName);
performInitializeTask.run();
verify(mTransportBinder).initializeDevice();
verify(mTransportBinder, never()).finishBackup();
verify(mTransportClient).initializeDevice();
verify(mTransportClient, never()).finishBackup();
verify(mBackupManagerService)
.recordInitPending(true, mTransportName, mTransport.transportDirName);
}
@@ -155,7 +155,7 @@ public class PerformInitializeTaskTest {
public void testRun_whenInitializeDeviceFails_callsObserverAndListenerCorrectly()
throws Exception {
setUpTransport(mTransport);
configureTransport(mTransportBinder, TRANSPORT_ERROR, 0);
configureTransport(mTransportClient, TRANSPORT_ERROR, 0);
PerformInitializeTask performInitializeTask = createPerformInitializeTask(mTransportName);
performInitializeTask.run();
@@ -168,7 +168,7 @@ public class PerformInitializeTaskTest {
@Test
public void testRun_whenInitializeDeviceFails_schedulesAlarm() throws Exception {
setUpTransport(mTransport);
configureTransport(mTransportBinder, TRANSPORT_ERROR, 0);
configureTransport(mTransportClient, TRANSPORT_ERROR, 0);
PerformInitializeTask performInitializeTask = createPerformInitializeTask(mTransportName);
performInitializeTask.run();
@@ -179,13 +179,13 @@ public class PerformInitializeTaskTest {
@Test
public void testRun_whenFinishBackupFails() throws Exception {
setUpTransport(mTransport);
configureTransport(mTransportBinder, TRANSPORT_OK, TRANSPORT_ERROR);
configureTransport(mTransportClient, TRANSPORT_OK, TRANSPORT_ERROR);
PerformInitializeTask performInitializeTask = createPerformInitializeTask(mTransportName);
performInitializeTask.run();
verify(mTransportBinder).initializeDevice();
verify(mTransportBinder).finishBackup();
verify(mTransportClient).initializeDevice();
verify(mTransportClient).finishBackup();
verify(mBackupManagerService)
.recordInitPending(true, mTransportName, mTransport.transportDirName);
}
@@ -193,7 +193,7 @@ public class PerformInitializeTaskTest {
@Test
public void testRun_whenFinishBackupFails_callsObserverAndListenerCorrectly() throws Exception {
setUpTransport(mTransport);
configureTransport(mTransportBinder, TRANSPORT_OK, TRANSPORT_ERROR);
configureTransport(mTransportClient, TRANSPORT_OK, TRANSPORT_ERROR);
PerformInitializeTask performInitializeTask = createPerformInitializeTask(mTransportName);
performInitializeTask.run();
@@ -206,7 +206,7 @@ public class PerformInitializeTaskTest {
@Test
public void testRun_whenFinishBackupFails_logs() throws Exception {
setUpTransport(mTransport);
configureTransport(mTransportBinder, TRANSPORT_OK, TRANSPORT_ERROR);
configureTransport(mTransportClient, TRANSPORT_OK, TRANSPORT_ERROR);
PerformInitializeTask performInitializeTask = createPerformInitializeTask(mTransportName);
performInitializeTask.run();
@@ -219,7 +219,7 @@ public class PerformInitializeTaskTest {
@Test
public void testRun_whenInitializeDeviceFails_logs() throws Exception {
setUpTransport(mTransport);
configureTransport(mTransportBinder, TRANSPORT_ERROR, 0);
configureTransport(mTransportClient, TRANSPORT_ERROR, 0);
PerformInitializeTask performInitializeTask = createPerformInitializeTask(mTransportName);
performInitializeTask.run();
@@ -232,7 +232,7 @@ public class PerformInitializeTaskTest {
@Test
public void testRun_whenFinishBackupFails_schedulesAlarm() throws Exception {
setUpTransport(mTransport);
configureTransport(mTransportBinder, TRANSPORT_OK, TRANSPORT_ERROR);
configureTransport(mTransportClient, TRANSPORT_OK, TRANSPORT_ERROR);
PerformInitializeTask performInitializeTask = createPerformInitializeTask(mTransportName);
performInitializeTask.run();
@@ -327,7 +327,7 @@ public class PerformInitializeTaskTest {
List<TransportMock> transportMocks =
setUpTransports(mTransportManager, transport1, transport2);
String registeredTransportName = transport2.transportName;
IBackupTransport registeredTransport = transportMocks.get(1).transport;
BackupTransportClient registeredTransport = transportMocks.get(1).transport;
TransportConnection
registeredTransportConnection = transportMocks.get(1).mTransportConnection;
PerformInitializeTask performInitializeTask =
@@ -357,7 +357,7 @@ public class PerformInitializeTaskTest {
@Test
public void testRun_whenTransportThrowsDeadObjectException() throws Exception {
TransportMock transportMock = setUpTransport(mTransport);
IBackupTransport transport = transportMock.transport;
BackupTransportClient transport = transportMock.transport;
TransportConnection transportConnection = transportMock.mTransportConnection;
when(transport.initializeDevice()).thenThrow(DeadObjectException.class);
PerformInitializeTask performInitializeTask = createPerformInitializeTask(mTransportName);
@@ -380,7 +380,7 @@ public class PerformInitializeTaskTest {
}
private void configureTransport(
IBackupTransport transportMock, int initializeDeviceStatus, int finishBackupStatus)
BackupTransportClient transportMock, int initializeDeviceStatus, int finishBackupStatus)
throws Exception {
when(transportMock.initializeDevice()).thenReturn(initializeDeviceStatus);
when(transportMock.finishBackup()).thenReturn(finishBackupStatus);
@@ -389,7 +389,7 @@ public class PerformInitializeTaskTest {
private TransportMock setUpTransport(TransportData transport) throws Exception {
TransportMock transportMock =
TransportTestUtils.setUpTransport(mTransportManager, transport);
mTransportBinder = transportMock.transport;
mTransportClient = transportMock.transport;
return transportMock;
}
}

View File

@@ -34,8 +34,8 @@ import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.os.RemoteException;
import com.android.internal.backup.IBackupTransport;
import com.android.server.backup.TransportManager;
import com.android.server.backup.transport.BackupTransportClient;
import com.android.server.backup.transport.TransportConnection;
import com.android.server.backup.transport.TransportNotAvailableException;
import com.android.server.backup.transport.TransportNotRegisteredException;
@@ -160,7 +160,7 @@ public class TransportTestUtils {
when(transportConnectionMock.getTransportComponent()).thenReturn(transportComponent);
if (status == TransportStatus.REGISTERED_AVAILABLE) {
// Transport registered and available
IBackupTransport transportMock = mockTransportBinder(transport);
BackupTransportClient transportMock = mockTransportBinder(transport);
when(transportConnectionMock.connectOrThrow(any())).thenReturn(transportMock);
when(transportConnectionMock.connect(any())).thenReturn(transportMock);
@@ -179,8 +179,9 @@ public class TransportTestUtils {
}
}
private static IBackupTransport mockTransportBinder(TransportData transport) throws Exception {
IBackupTransport transportBinder = mock(IBackupTransport.class);
private static BackupTransportClient mockTransportBinder(TransportData transport)
throws Exception {
BackupTransportClient transportBinder = mock(BackupTransportClient.class);
try {
when(transportBinder.name()).thenReturn(transport.transportName);
when(transportBinder.transportDirName()).thenReturn(transport.transportDirName);
@@ -199,12 +200,12 @@ public class TransportTestUtils {
public static class TransportMock {
public final TransportData transportData;
@Nullable public final TransportConnection mTransportConnection;
@Nullable public final IBackupTransport transport;
@Nullable public final BackupTransportClient transport;
private TransportMock(
TransportData transportData,
@Nullable TransportConnection transportConnection,
@Nullable IBackupTransport transport) {
@Nullable BackupTransportClient transport) {
this.transportData = transportData;
this.mTransportConnection = transportConnection;
this.transport = transport;

View File

@@ -84,9 +84,11 @@ public class TransportConnectionTest {
@Mock private TransportConnectionListener mTransportConnectionListener;
@Mock private TransportConnectionListener mTransportConnectionListener2;
@Mock private IBackupTransport.Stub mTransportBinder;
@UserIdInt private int mUserId;
private TransportStats mTransportStats;
private TransportConnection mTransportConnection;
private BackupTransportClient mTransportClient;
private ComponentName mTransportComponent;
private String mTransportString;
private Intent mBindIntent;
@@ -116,6 +118,7 @@ public class TransportConnectionTest {
"1",
"caller",
new Handler(mainLooper));
mTransportClient = new BackupTransportClient(mTransportBinder);
when(mContext.bindServiceAsUser(
eq(mBindIntent),
@@ -156,7 +159,8 @@ public class TransportConnectionTest {
mShadowMainLooper.runToEndOfTasks();
verify(mTransportConnectionListener)
.onTransportConnectionResult(any(IBackupTransport.class), eq(mTransportConnection));
.onTransportConnectionResult(any(BackupTransportClient.class),
eq(mTransportConnection));
}
@Test
@@ -169,9 +173,11 @@ public class TransportConnectionTest {
connection.onServiceConnected(mTransportComponent, mTransportBinder);
mShadowMainLooper.runToEndOfTasks();
verify(mTransportConnectionListener)
.onTransportConnectionResult(any(IBackupTransport.class), eq(mTransportConnection));
.onTransportConnectionResult(any(BackupTransportClient.class),
eq(mTransportConnection));
verify(mTransportConnectionListener2)
.onTransportConnectionResult(any(IBackupTransport.class), eq(mTransportConnection));
.onTransportConnectionResult(any(BackupTransportClient.class),
eq(mTransportConnection));
}
@Test
@@ -184,7 +190,8 @@ public class TransportConnectionTest {
mShadowMainLooper.runToEndOfTasks();
verify(mTransportConnectionListener2)
.onTransportConnectionResult(any(IBackupTransport.class), eq(mTransportConnection));
.onTransportConnectionResult(any(BackupTransportClient.class),
eq(mTransportConnection));
}
@Test
@@ -312,10 +319,10 @@ public class TransportConnectionTest {
ServiceConnection connection = verifyBindServiceAsUserAndCaptureServiceConnection(mContext);
connection.onServiceConnected(mTransportComponent, mTransportBinder);
IBackupTransport transportBinder =
BackupTransportClient transportClient =
runInWorkerThread(() -> mTransportConnection.connect("caller2"));
assertThat(transportBinder).isNotNull();
assertThat(transportClient).isNotNull();
}
@Test
@@ -325,10 +332,10 @@ public class TransportConnectionTest {
connection.onServiceConnected(mTransportComponent, mTransportBinder);
connection.onServiceDisconnected(mTransportComponent);
IBackupTransport transportBinder =
BackupTransportClient transportClient =
runInWorkerThread(() -> mTransportConnection.connect("caller2"));
assertThat(transportBinder).isNull();
assertThat(transportClient).isNull();
}
@Test
@@ -337,10 +344,10 @@ public class TransportConnectionTest {
ServiceConnection connection = verifyBindServiceAsUserAndCaptureServiceConnection(mContext);
connection.onBindingDied(mTransportComponent);
IBackupTransport transportBinder =
BackupTransportClient transportClient =
runInWorkerThread(() -> mTransportConnection.connect("caller2"));
assertThat(transportBinder).isNull();
assertThat(transportClient).isNull();
}
@Test
@@ -354,17 +361,17 @@ public class TransportConnectionTest {
doAnswer(
invocation -> {
TransportConnectionListener listener = invocation.getArgument(0);
listener.onTransportConnectionResult(mTransportBinder,
listener.onTransportConnectionResult(mTransportClient,
transportConnection);
return null;
})
.when(transportConnection)
.connectAsync(any(), any());
IBackupTransport transportBinder =
BackupTransportClient transportClient =
runInWorkerThread(() -> transportConnection.connect("caller"));
assertThat(transportBinder).isNotNull();
assertThat(transportClient).isNotNull();
}
@Test

View File

@@ -31,13 +31,13 @@ import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import com.android.internal.backup.IBackupTransport;
import android.platform.test.annotations.Presubmit;
import androidx.test.runner.AndroidJUnit4;
import com.android.server.backup.internal.OnTaskFinishedListener;
import com.android.server.backup.params.BackupParams;
import com.android.server.backup.transport.BackupTransportClient;
import com.android.server.backup.transport.TransportConnection;
import com.android.server.backup.utils.BackupEligibilityRules;
@@ -57,9 +57,8 @@ public class UserBackupManagerServiceTest {
@Mock IBackupManagerMonitor mBackupManagerMonitor;
@Mock IBackupObserver mBackupObserver;
@Mock PackageManager mPackageManager;
@Mock
TransportConnection mTransportConnection;
@Mock IBackupTransport mBackupTransport;
@Mock TransportConnection mTransportConnection;
@Mock BackupTransportClient mBackupTransport;
@Mock BackupEligibilityRules mBackupEligibilityRules;