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

Commit e7f7a0e4 authored by Tom Chan's avatar Tom Chan
Browse files

Enforce concurrent connection limit.

Test: atest CtsWearableSensingServiceTestCases after patching ag/29937309
Flag: android.app.wearable.enable_concurrent_wearable_connections
Bug: 358133158
Change-Id: I2bce93627263fb15540e3c82c2198d8942541fa5
parent 3640e558
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -350,9 +350,9 @@ public class WearableSensingManager {
                            statusCallback);
            if (connectionId != CONNECTION_ID_INVALID) {
                mWearableConnectionIdMap.put(wearableConnection, connectionId);
            } else {
                mWearableConnectionIdMap.remove(wearableConnection);
            }
            // For invalid connection IDs, the status callback will remove the connection from
            // mWearableConnectionIdMap
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
+41 −3
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.app.AppGlobals;
import android.app.ambientcontext.AmbientContextEvent;
import android.app.wearable.Flags;
import android.app.wearable.IWearableSensingCallback;
import android.app.wearable.WearableSensingManager;
import android.companion.CompanionDeviceManager;
@@ -311,11 +312,31 @@ final class WearableSensingManagerPerUserService
                return WearableSensingManager.CONNECTION_ID_INVALID;
            }
        }
        boolean isConcurrentConnectionLimitReached = false;
        synchronized (mSecureChannelMap) {
            // Do a pre-check on concurrent connection count. We need another check right before we
            // add the connection into the map to prevent race conditions, but that can only happen
            // after the WearableSensingSecureChannel is created. This check here allows us to
            // reject before creating a new secure channel.
            if (mSecureChannelMap.size() >= mMaxNumberOfConcurrentConnections) {
                isConcurrentConnectionLimitReached = true;
            }
        }
        if (isConcurrentConnectionLimitReached) {
            Slog.i(
                    TAG,
                    "Rejecting connection because max concurrent connections limit has been"
                        + " reached.");
            if (Flags.enableConcurrentWearableConnections()) {
                notifyStatusCallback(
                        statusCallback,
                        WearableSensingManager.STATUS_MAX_CONCURRENT_CONNECTIONS_EXCEEDED);
            }
            return WearableSensingManager.CONNECTION_ID_INVALID;
        }
        int connectionId = mNextConnectionId.getAndIncrement();
        RemoteCallback wrappedStatusCallback =
                wrapCallbackWithSecureChannelMapCleanUp(statusCallback, connectionId);

        // TODO(b/358133158): enforce mMaxNumberOfConcurrentConnections
        WearableSensingSecureChannel secureChannel;
        try {
            secureChannel =
@@ -353,8 +374,25 @@ final class WearableSensingManagerPerUserService
            return WearableSensingManager.CONNECTION_ID_INVALID;
        }
        synchronized (mSecureChannelMap) {
            if (mSecureChannelMap.size() >= mMaxNumberOfConcurrentConnections) {
                isConcurrentConnectionLimitReached = true;
            } else {
                mSecureChannelMap.put(connectionId, secureChannel);
            }
        }
        if (isConcurrentConnectionLimitReached) {
            Slog.i(
                    TAG,
                    "Rejecting connection because max concurrent connections limit has been"
                        + " reached.");
            if (Flags.enableConcurrentWearableConnections()) {
                notifyStatusCallback(
                        statusCallback,
                        WearableSensingManager.STATUS_MAX_CONCURRENT_CONNECTIONS_EXCEEDED);
            }
            secureChannel.close();
            return WearableSensingManager.CONNECTION_ID_INVALID;
        }
        return connectionId;
    }