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

Commit c10e83eb authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Cleanup resources on failure to handle open session request" into main

parents ecadb8d4 17e984cb
Loading
Loading
Loading
Loading
+28 −18
Original line number Diff line number Diff line
@@ -372,25 +372,8 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub

    /* package */ void onEndpointSessionOpenRequest(
            int sessionId, HubEndpointInfo initiator, String serviceDescriptor) {
        if (!hasEndpointPermissions(initiator)) {
            halCloseEndpointSessionNoThrow(sessionId, Reason.PERMISSION_DENIED);
            return;
        }

        synchronized (mOpenSessionLock) {
            if (hasSessionId(sessionId)) {
                Log.e(TAG, "Existing session in onEndpointSessionOpenRequest: id=" + sessionId);
                halCloseEndpointSessionNoThrow(sessionId, Reason.UNSPECIFIED);
                return;
            }
            mSessionInfoMap.put(sessionId, new SessionInfo(initiator, true));
        }

        boolean success =
                invokeCallback(
                        (consumer) ->
                                consumer.onSessionOpenRequest(
                                        sessionId, initiator, serviceDescriptor));
                onEndpointSessionOpenRequestInternal(sessionId, initiator, serviceDescriptor);
        if (!success) {
            cleanupSessionResources(sessionId);
        }
@@ -439,6 +422,33 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
        }
    }

    private boolean onEndpointSessionOpenRequestInternal(
            int sessionId, HubEndpointInfo initiator, String serviceDescriptor) {
        if (!hasEndpointPermissions(initiator)) {
            Log.e(
                    TAG,
                    "onEndpointSessionOpenRequest: "
                            + initiator
                            + " doesn't have permission for "
                            + mEndpointInfo);
            halCloseEndpointSessionNoThrow(sessionId, Reason.PERMISSION_DENIED);
            return false;
        }

        synchronized (mOpenSessionLock) {
            if (hasSessionId(sessionId)) {
                Log.e(TAG, "Existing session in onEndpointSessionOpenRequest: id=" + sessionId);
                halCloseEndpointSessionNoThrow(sessionId, Reason.UNSPECIFIED);
                return false;
            }
            mSessionInfoMap.put(sessionId, new SessionInfo(initiator, true));
        }

        return invokeCallback(
                (consumer) ->
                        consumer.onSessionOpenRequest(sessionId, initiator, serviceDescriptor));
    }

    private byte onMessageReceivedInternal(int sessionId, HubMessage message) {
        HubEndpointInfo remote;
        synchronized (mOpenSessionLock) {