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

Commit 9b632f25 authored by Arthur Ishiguro's avatar Arthur Ishiguro
Browse files

Remove HAL endpoint callback thread

The extra thread can be removed because the HAL callback interface is oneway. This CL also modifies the notePermission call to be done with a clean binder identity to avoid permission issues.

Bug: 388607517
Flag: android.chre.flags.offload_implementation
Test: CHQTS endpoint test pass

Change-Id: I20d98a6337c80f0e9e6c4ac58a42907191736ee9
parent ae5d014b
Loading
Loading
Loading
Loading
+35 −18
Original line number Diff line number Diff line
@@ -508,24 +508,21 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
            }
            remote = mSessionInfoMap.get(sessionId).getRemoteEndpointInfo();
        }
        if (!ContextHubServiceUtil.notePermissions(
                mAppOpsManager,
                mUid,
                mPackageName,
                mAttributionTag,
                remote.getRequiredPermissions(),
                RECEIVE_MSG_NOTE
                        + "-0x"
                        + Long.toHexString(remote.getIdentifier().getHub())
                        + "-0x"
                        + Long.toHexString(remote.getIdentifier().getEndpoint()))) {
            Log.e(
                    TAG,

        try {
            Binder.withCleanCallingIdentity(
                    () -> {
                        if (!notePermissions(remote)) {
                            throw new RuntimeException(
                                    "Dropping message from "
                                            + remote
                                            + ". "
                                            + mPackageName
                                            + " doesn't have permission");
                        }
                    });
        } catch (RuntimeException e) {
            Log.e(TAG, e.getMessage());
            return ErrorCode.PERMISSION_DENIED;
        }

@@ -657,4 +654,24 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
            return mIsRegistered;
        }
    }

    /**
     * Utility to call notePermissions for e.g. when processing a message from a given endpoint for
     * this broker.
     *
     * @param endpoint The endpoint to check permissions for this broker.
     */
    private boolean notePermissions(HubEndpointInfo endpoint) {
        return ContextHubServiceUtil.notePermissions(
                mAppOpsManager,
                mUid,
                mPackageName,
                mAttributionTag,
                endpoint.getRequiredPermissions(),
                RECEIVE_MSG_NOTE
                        + "-0x"
                        + Long.toHexString(endpoint.getIdentifier().getHub())
                        + "-0x"
                        + Long.toHexString(endpoint.getIdentifier().getEndpoint()));
    }
}
+11 −26
Original line number Diff line number Diff line
@@ -21,9 +21,6 @@ import android.hardware.contexthub.HubMessage;
import android.hardware.contexthub.IEndpointCallback;
import android.hardware.contexthub.Message;
import android.hardware.contexthub.MessageDeliveryStatus;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Process;
import android.os.RemoteException;

/** IEndpointCallback implementation. */
@@ -32,11 +29,6 @@ public class ContextHubHalEndpointCallback
    private final IEndpointLifecycleCallback mEndpointLifecycleCallback;
    private final IEndpointSessionCallback mEndpointSessionCallback;

    // Use this thread in case where the execution requires to be on an async service thread.
    private final HandlerThread mHandlerThread =
            new HandlerThread("Context Hub endpoint callback", Process.THREAD_PRIORITY_BACKGROUND);
    private Handler mHandler;

    /** Interface for listening for endpoint start and stop events. */
    public interface IEndpointLifecycleCallback {
        /** Called when a batch of endpoints started. */
@@ -73,9 +65,6 @@ public class ContextHubHalEndpointCallback
            IEndpointSessionCallback endpointSessionCallback) {
        mEndpointLifecycleCallback = endpointLifecycleCallback;
        mEndpointSessionCallback = endpointSessionCallback;

        mHandlerThread.start();
        mHandler = new Handler(mHandlerThread.getLooper());
    }

    @Override
@@ -88,7 +77,7 @@ public class ContextHubHalEndpointCallback
        for (int i = 0; i < halEndpointInfos.length; i++) {
            endpointInfos[i] = new HubEndpointInfo(halEndpointInfos[i]);
        }
        mHandler.post(() -> mEndpointLifecycleCallback.onEndpointStarted(endpointInfos));
        mEndpointLifecycleCallback.onEndpointStarted(endpointInfos);
    }

    @Override
@@ -98,7 +87,7 @@ public class ContextHubHalEndpointCallback
        for (int i = 0; i < halEndpointIds.length; i++) {
            endpointIds[i] = new HubEndpointInfo.HubEndpointIdentifier(halEndpointIds[i]);
        }
        mHandler.post(() -> mEndpointLifecycleCallback.onEndpointStopped(endpointIds, reason));
        mEndpointLifecycleCallback.onEndpointStopped(endpointIds, reason);
    }

    @Override
@@ -109,37 +98,33 @@ public class ContextHubHalEndpointCallback
                new HubEndpointInfo.HubEndpointIdentifier(destination.hubId, destination.id);
        HubEndpointInfo.HubEndpointIdentifier initiatorId =
                new HubEndpointInfo.HubEndpointIdentifier(initiator.hubId, initiator.id);
        mHandler.post(
                () ->
        mEndpointSessionCallback.onEndpointSessionOpenRequest(
                                sessionId, destinationId, initiatorId, serviceDescriptor));
                sessionId, destinationId, initiatorId, serviceDescriptor);
    }

    @Override
    public void onCloseEndpointSession(int sessionId, byte reason) throws RemoteException {
        mHandler.post(() -> mEndpointSessionCallback.onCloseEndpointSession(sessionId, reason));
        mEndpointSessionCallback.onCloseEndpointSession(sessionId, reason);
    }

    @Override
    public void onEndpointSessionOpenComplete(int sessionId) throws RemoteException {
        mHandler.post(() -> mEndpointSessionCallback.onEndpointSessionOpenComplete(sessionId));
        mEndpointSessionCallback.onEndpointSessionOpenComplete(sessionId);
    }

    @Override
    public void onMessageReceived(int sessionId, Message message) throws RemoteException {
        HubMessage hubMessage = ContextHubServiceUtil.createHubMessage(message);
        mHandler.post(() -> mEndpointSessionCallback.onMessageReceived(sessionId, hubMessage));
        mEndpointSessionCallback.onMessageReceived(sessionId, hubMessage);
    }

    @Override
    public void onMessageDeliveryStatusReceived(
            int sessionId, MessageDeliveryStatus messageDeliveryStatus) throws RemoteException {
        mHandler.post(
                () ->
        mEndpointSessionCallback.onMessageDeliveryStatusReceived(
                sessionId,
                messageDeliveryStatus.messageSequenceNumber,
                                messageDeliveryStatus.errorCode));
                messageDeliveryStatus.errorCode);
    }

    @Override