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

Commit f39ab702 authored by Ruslan Tkhakokhov's avatar Ruslan Tkhakokhov
Browse files

Rename TransportClient to TransportConnection

The change is auto-generated through IDE rename function and makes the
following naming changes:

1. TransportClient -> TransportConnection
2. TransportClientManager -> TransportConnectionManager
3. + corresponding test files

TransportConnection is a more appropriate name for the class as it's
exactly what it does - manages the connection to the remote
BackupTransport service implementation.

This is a preparatory change to making the BackupTransport AIDL async.
TransportClient name will later be used more appropriately for a class
that wraps the actual communication with the transport.

Bug: 202716271
Test: m -j
Change-Id: I76a98edc7102c8fcffdb050208e9e65543e6e10c
parent 5169896d
Loading
Loading
Loading
Loading
+39 −37
Original line number Diff line number Diff line
@@ -40,8 +40,8 @@ import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.backup.IBackupTransport;
import com.android.internal.util.Preconditions;
import com.android.server.backup.transport.OnTransportRegisteredListener;
import com.android.server.backup.transport.TransportClient;
import com.android.server.backup.transport.TransportClientManager;
import com.android.server.backup.transport.TransportConnection;
import com.android.server.backup.transport.TransportConnectionManager;
import com.android.server.backup.transport.TransportConnectionListener;
import com.android.server.backup.transport.TransportNotAvailableException;
import com.android.server.backup.transport.TransportNotRegisteredException;
@@ -65,7 +65,7 @@ public class TransportManager {
    private final @UserIdInt int mUserId;
    private final PackageManager mPackageManager;
    private final Set<ComponentName> mTransportWhitelist;
    private final TransportClientManager mTransportClientManager;
    private final TransportConnectionManager mTransportConnectionManager;
    private final TransportStats mTransportStats;
    private OnTransportRegisteredListener mOnTransportRegisteredListener = (c, n) -> {};

@@ -73,8 +73,8 @@ public class TransportManager {
     * Lock for registered transports and currently selected transport.
     *
     * <p><b>Warning:</b> No calls to {@link IBackupTransport} or calls that result in transport
     * code being executed such as {@link TransportClient#connect(String)}} and its variants should
     * be made with this lock held, risk of deadlock.
     * code being executed such as {@link TransportConnection#connect(String)}} and its variants
     * should be made with this lock held, risk of deadlock.
     */
    private final Object mTransportLock = new Object();

@@ -94,7 +94,8 @@ public class TransportManager {
        mTransportWhitelist = Preconditions.checkNotNull(whitelist);
        mCurrentTransportName = selectedTransport;
        mTransportStats = new TransportStats();
        mTransportClientManager = new TransportClientManager(mUserId, context, mTransportStats);
        mTransportConnectionManager = new TransportConnectionManager(mUserId, context,
                mTransportStats);
    }

    @VisibleForTesting
@@ -103,13 +104,13 @@ public class TransportManager {
            Context context,
            Set<ComponentName> whitelist,
            String selectedTransport,
            TransportClientManager transportClientManager) {
            TransportConnectionManager transportConnectionManager) {
        mUserId = userId;
        mPackageManager = context.getPackageManager();
        mTransportWhitelist = Preconditions.checkNotNull(whitelist);
        mCurrentTransportName = selectedTransport;
        mTransportStats = new TransportStats();
        mTransportClientManager = transportClientManager;
        mTransportConnectionManager = transportConnectionManager;
    }

    /* Sets a listener to be called whenever a transport is registered. */
@@ -307,7 +308,7 @@ public class TransportManager {
     * transportConsumer}.
     *
     * <p><b>Warning:</b> Do NOT make any calls to {@link IBackupTransport} or call any variants of
     * {@link TransportClient#connect(String)} here, otherwise you risk deadlock.
     * {@link TransportConnection#connect(String)} here, otherwise you risk deadlock.
     */
    public void forEachRegisteredTransport(Consumer<String> transportConsumer) {
        synchronized (mTransportLock) {
@@ -407,17 +408,17 @@ public class TransportManager {
    }

    /**
     * Returns a {@link TransportClient} for {@code transportName} or {@code null} if not
     * Returns a {@link TransportConnection} for {@code transportName} or {@code null} if not
     * registered.
     *
     * @param transportName The name of the transport.
     * @param caller A {@link String} identifying the caller for logging/debugging purposes. Check
     *     {@link TransportClient#connectAsync(TransportConnectionListener, String)} for more
     *     {@link TransportConnection#connectAsync(TransportConnectionListener, String)} for more
     *     details.
     * @return A {@link TransportClient} or null if not registered.
     * @return A {@link TransportConnection} or null if not registered.
     */
    @Nullable
    public TransportClient getTransportClient(String transportName, String caller) {
    public TransportConnection getTransportClient(String transportName, String caller) {
        try {
            return getTransportClientOrThrow(transportName, caller);
        } catch (TransportNotRegisteredException e) {
@@ -427,38 +428,38 @@ public class TransportManager {
    }

    /**
     * Returns a {@link TransportClient} for {@code transportName} or throws if not registered.
     * Returns a {@link TransportConnection} for {@code transportName} or throws if not registered.
     *
     * @param transportName The name of the transport.
     * @param caller A {@link String} identifying the caller for logging/debugging purposes. Check
     *     {@link TransportClient#connectAsync(TransportConnectionListener, String)} for more
     *     {@link TransportConnection#connectAsync(TransportConnectionListener, String)} for more
     *     details.
     * @return A {@link TransportClient}.
     * @return A {@link TransportConnection}.
     * @throws TransportNotRegisteredException if the transport is not registered.
     */
    public TransportClient getTransportClientOrThrow(String transportName, String caller)
    public TransportConnection getTransportClientOrThrow(String transportName, String caller)
            throws TransportNotRegisteredException {
        synchronized (mTransportLock) {
            ComponentName component = getRegisteredTransportComponentLocked(transportName);
            if (component == null) {
                throw new TransportNotRegisteredException(transportName);
            }
            return mTransportClientManager.getTransportClient(component, caller);
            return mTransportConnectionManager.getTransportClient(component, caller);
        }
    }

    /**
     * Returns a {@link TransportClient} for the current transport or {@code null} if not
     * Returns a {@link TransportConnection} for the current transport or {@code null} if not
     * registered.
     *
     * @param caller A {@link String} identifying the caller for logging/debugging purposes. Check
     *     {@link TransportClient#connectAsync(TransportConnectionListener, String)} for more
     *     {@link TransportConnection#connectAsync(TransportConnectionListener, String)} for more
     *     details.
     * @return A {@link TransportClient} or null if not registered.
     * @return A {@link TransportConnection} or null if not registered.
     * @throws IllegalStateException if no transport is selected.
     */
    @Nullable
    public TransportClient getCurrentTransportClient(String caller) {
    public TransportConnection getCurrentTransportClient(String caller) {
        if (mCurrentTransportName == null) {
            throw new IllegalStateException("No transport selected");
        }
@@ -468,16 +469,16 @@ public class TransportManager {
    }

    /**
     * Returns a {@link TransportClient} for the current transport or throws if not registered.
     * Returns a {@link TransportConnection} for the current transport or throws if not registered.
     *
     * @param caller A {@link String} identifying the caller for logging/debugging purposes. Check
     *     {@link TransportClient#connectAsync(TransportConnectionListener, String)} for more
     *     {@link TransportConnection#connectAsync(TransportConnectionListener, String)} for more
     *     details.
     * @return A {@link TransportClient}.
     * @return A {@link TransportConnection}.
     * @throws TransportNotRegisteredException if the transport is not registered.
     * @throws IllegalStateException if no transport is selected.
     */
    public TransportClient getCurrentTransportClientOrThrow(String caller)
    public TransportConnection getCurrentTransportClientOrThrow(String caller)
            throws TransportNotRegisteredException {
        if (mCurrentTransportName == null) {
            throw new IllegalStateException("No transport selected");
@@ -488,15 +489,15 @@ public class TransportManager {
    }

    /**
     * Disposes of the {@link TransportClient}.
     * Disposes of the {@link TransportConnection}.
     *
     * @param transportClient The {@link TransportClient} to be disposed of.
     * @param transportConnection The {@link TransportConnection} to be disposed of.
     * @param caller A {@link String} identifying the caller for logging/debugging purposes. Check
     *     {@link TransportClient#connectAsync(TransportConnectionListener, String)} for more
     *     {@link TransportConnection#connectAsync(TransportConnectionListener, String)} for more
     *     details.
     */
    public void disposeOfTransportClient(TransportClient transportClient, String caller) {
        mTransportClientManager.disposeOfTransportClient(transportClient, caller);
    public void disposeOfTransportClient(TransportConnection transportConnection, String caller) {
        mTransportConnectionManager.disposeOfTransportClient(transportConnection, caller);
    }

    /**
@@ -637,15 +638,16 @@ public class TransportManager {
        Bundle extras = new Bundle();
        extras.putBoolean(BackupTransport.EXTRA_TRANSPORT_REGISTRATION, true);

        TransportClient transportClient =
                mTransportClientManager.getTransportClient(
        TransportConnection transportConnection =
                mTransportConnectionManager.getTransportClient(
                        transportComponent, extras, callerLogString);
        final IBackupTransport transport;
        try {
            transport = transportClient.connectOrThrow(callerLogString);
            transport = transportConnection.connectOrThrow(callerLogString);
        } catch (TransportNotAvailableException e) {
            Slog.e(TAG, "Couldn't connect to transport " + transportString + " for registration");
            mTransportClientManager.disposeOfTransportClient(transportClient, callerLogString);
            mTransportConnectionManager.disposeOfTransportClient(transportConnection,
                    callerLogString);
            return BackupManager.ERROR_TRANSPORT_UNAVAILABLE;
        }

@@ -667,7 +669,7 @@ public class TransportManager {
            result = BackupManager.ERROR_TRANSPORT_UNAVAILABLE;
        }

        mTransportClientManager.disposeOfTransportClient(transportClient, callerLogString);
        mTransportConnectionManager.disposeOfTransportClient(transportConnection, callerLogString);
        return result;
    }

@@ -695,7 +697,7 @@ public class TransportManager {
    }

    public void dumpTransportClients(PrintWriter pw) {
        mTransportClientManager.dump(pw);
        mTransportConnectionManager.dump(pw);
    }

    public void dumpTransportStats(PrintWriter pw) {
+28 −26
Original line number Diff line number Diff line
@@ -59,16 +59,17 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * A {@link TransportClient} manages the connection to an {@link IBackupTransport} service, obtained
 * via the {@param bindIntent} parameter provided in the constructor. A {@link TransportClient} is
 * responsible for only one connection to the transport service, not more.
 * A {@link TransportConnection} manages the connection to an {@link IBackupTransport} service,
 * 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.
 *
 * <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(TransportClient, String)}.
 * via {@link TransportManager#disposeOfTransportClient(TransportConnection, String)}.
 *
 * <p>DO NOT forget to unbind otherwise there will be dangling connections floating around.
 *
@@ -76,8 +77,8 @@ import java.util.concurrent.ExecutionException;
 *
 * @see TransportManager
 */
public class TransportClient {
    @VisibleForTesting static final String TAG = "TransportClient";
public class TransportConnection {
    @VisibleForTesting static final String TAG = "TransportConnection";
    private static final int LOG_BUFFER_SIZE = 5;

    private final @UserIdInt int mUserId;
@@ -107,7 +108,7 @@ public class TransportClient {
    @GuardedBy("mStateLock")
    private volatile IBackupTransport mTransport;

    TransportClient(
    TransportConnection(
            @UserIdInt int userId,
            Context context,
            TransportStats transportStats,
@@ -127,7 +128,7 @@ public class TransportClient {
    }

    @VisibleForTesting
    TransportClient(
    TransportConnection(
            @UserIdInt int userId,
            Context context,
            TransportStats transportStats,
@@ -144,7 +145,7 @@ public class TransportClient {
        mIdentifier = identifier;
        mCreatorLogString = caller;
        mListenerHandler = listenerHandler;
        mConnection = new TransportConnection(context, this);
        mConnection = new TransportConnectionMonitor(context, this);

        // For logging
        String classNameForLog = mTransportComponent.getShortClassName().replaceFirst(".*\\.", "");
@@ -192,7 +193,7 @@ public class TransportClient {
     * 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 TransportClient} object.
     *     IBackupTransport} 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
@@ -373,8 +374,8 @@ public class TransportClient {
    }

    /**
     * If the {@link TransportClient} is already connected to the transport, returns the transport,
     * otherwise throws {@link TransportNotAvailableException}.
     * If the {@link TransportConnection} is already connected to the transport, returns the
     * transport, otherwise throws {@link TransportNotAvailableException}.
     *
     * @param caller A {@link String} identifying the caller for logging/debugging purposes. Check
     *     {@link #connectAsync(TransportConnectionListener, String)} for more details.
@@ -647,19 +648,20 @@ public class TransportClient {
     * This class is a proxy to TransportClient methods that doesn't hold a strong reference to the
     * TransportClient, allowing it to be GC'ed. If the reference was lost it logs a message.
     */
    private static class TransportConnection implements ServiceConnection {
    private static class TransportConnectionMonitor implements ServiceConnection {
        private final Context mContext;
        private final WeakReference<TransportClient> mTransportClientRef;
        private final WeakReference<TransportConnection> mTransportClientRef;

        private TransportConnection(Context context, TransportClient transportClient) {
        private TransportConnectionMonitor(Context context,
                TransportConnection transportConnection) {
            mContext = context;
            mTransportClientRef = new WeakReference<>(transportClient);
            mTransportClientRef = new WeakReference<>(transportConnection);
        }

        @Override
        public void onServiceConnected(ComponentName transportComponent, IBinder binder) {
            TransportClient transportClient = mTransportClientRef.get();
            if (transportClient == null) {
            TransportConnection transportConnection = mTransportClientRef.get();
            if (transportConnection == null) {
                referenceLost("TransportConnection.onServiceConnected()");
                return;
            }
@@ -667,30 +669,30 @@ public class TransportClient {
            // In short-term, blocking calls are OK as the transports come from the allowlist at
            // {@link SystemConfig#getBackupTransportWhitelist()}
            Binder.allowBlocking(binder);
            transportClient.onServiceConnected(binder);
            transportConnection.onServiceConnected(binder);
        }

        @Override
        public void onServiceDisconnected(ComponentName transportComponent) {
            TransportClient transportClient = mTransportClientRef.get();
            if (transportClient == null) {
            TransportConnection transportConnection = mTransportClientRef.get();
            if (transportConnection == null) {
                referenceLost("TransportConnection.onServiceDisconnected()");
                return;
            }
            transportClient.onServiceDisconnected();
            transportConnection.onServiceDisconnected();
        }

        @Override
        public void onBindingDied(ComponentName transportComponent) {
            TransportClient transportClient = mTransportClientRef.get();
            if (transportClient == null) {
            TransportConnection transportConnection = mTransportClientRef.get();
            if (transportConnection == null) {
                referenceLost("TransportConnection.onBindingDied()");
                return;
            }
            transportClient.onBindingDied();
            transportConnection.onBindingDied();
        }

        /** @see TransportClient#finalize() */
        /** @see TransportConnection#finalize() */
        private void referenceLost(String caller) {
            mContext.unbindService(this);
            TransportUtils.log(
+6 −5
Original line number Diff line number Diff line
@@ -21,17 +21,18 @@ import android.annotation.Nullable;
import com.android.internal.backup.IBackupTransport;

/**
 * Listener to be called by {@link TransportClient#connectAsync(TransportConnectionListener,
 * Listener to be called by {@link TransportConnection#connectAsync(TransportConnectionListener,
 * String)}.
 */
public interface TransportConnectionListener {
    /**
     * Called when {@link TransportClient} has a transport binder available or that it decided it
     * couldn't obtain one, in which case {@param transport} is null.
     * Called when {@link TransportConnection} has a transport binder 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 The {@link TransportClient} used to retrieve this transport binder.
     * @param transportConnection The {@link TransportConnection} used to retrieve this transport
     *                            binder.
     */
    void onTransportConnectionResult(
            @Nullable IBackupTransport transport, TransportClient transportClient);
            @Nullable IBackupTransport transport, TransportConnection transportConnection);
}
+33 −33

File changed and moved.

Preview size limit exceeded, changes collapsed.

+3 −3
Original line number Diff line number Diff line
@@ -22,10 +22,10 @@ import com.android.internal.backup.IBackupTransport;

/**
 * Exception thrown when the {@link IBackupTransport} is not available. This happen when a {@link
 * TransportClient} connection attempt fails. Check {@link
 * TransportClient#connectAsync(TransportConnectionListener, String)} for when that happens.
 * TransportConnection} connection attempt fails. Check {@link
 * TransportConnection#connectAsync(TransportConnectionListener, String)} for when that happens.
 *
 * @see TransportClient#connectAsync(TransportConnectionListener, String)
 * @see TransportConnection#connectAsync(TransportConnectionListener, String)
 */
public class TransportNotAvailableException extends AndroidException {
    TransportNotAvailableException() {
Loading