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

Commit 1ea04a33 authored by Prabir Pradhan's avatar Prabir Pradhan
Browse files

InputTracer: Minor readability improvements

Bug: 210460522
Test: atest inputflinger_tests
Change-Id: I8f97648dc7ce926aec9f84423fc1ef603ca57eae
parent 47827688
Loading
Loading
Loading
Loading
+11 −12
Original line number Diff line number Diff line
@@ -84,24 +84,24 @@ std::unique_ptr<EventTrackerInterface> InputTracer::traceInboundEvent(const Even

void InputTracer::dispatchToTargetHint(const EventTrackerInterface& cookie,
                                       const InputTarget& target) {
    auto& cookieState = getState(cookie);
    if (!cookieState) {
    auto& eventState = getState(cookie);
    if (eventState.isEventProcessingComplete) {
        LOG(FATAL) << "dispatchToTargetHint() should not be called after eventProcessingComplete()";
    }
    // TODO(b/210460522): Determine if the event is sensitive based on the target.
}

void InputTracer::eventProcessingComplete(const EventTrackerInterface& cookie) {
    auto& cookieState = getState(cookie);
    if (!cookieState) {
    auto& eventState = getState(cookie);
    if (eventState.isEventProcessingComplete) {
        LOG(FATAL) << "Traced event was already logged. "
                      "eventProcessingComplete() was likely called more than once.";
    }

    std::visit(Visitor{[&](const TracedMotionEvent& e) { mBackend->traceMotionEvent(e); },
                       [&](const TracedKeyEvent& e) { mBackend->traceKeyEvent(e); }},
               cookieState->event);
    cookieState.reset();
               eventState.event);
    eventState.isEventProcessingComplete = true;
}

void InputTracer::traceEventDispatch(const DispatchEntry& dispatchEntry,
@@ -136,7 +136,7 @@ void InputTracer::traceEventDispatch(const DispatchEntry& dispatchEntry,
                                   /*hmac=*/{}});
}

std::optional<InputTracer::EventState>& InputTracer::getState(const EventTrackerInterface& cookie) {
InputTracer::EventState& InputTracer::getState(const EventTrackerInterface& cookie) {
    return static_cast<const EventTrackerImpl&>(cookie).mState;
}

@@ -146,18 +146,17 @@ InputTracer::EventTrackerImpl::EventTrackerImpl(InputTracer& tracer, TracedEvent
      : mTracer(tracer), mState(event) {}

InputTracer::EventTrackerImpl::~EventTrackerImpl() {
    if (!mState) {
    if (mState.isEventProcessingComplete) {
        // This event has already been written to the trace as expected.
        return;
    }
    // We're still holding on to the state, which means it hasn't yet been written to the trace.
    // Write it to the trace now.
    // The event processing was never marked as complete, so do it now.
    // TODO(b/210460522): Determine why/where the event is being destroyed before
    //   eventProcessingComplete() is called.
    std::visit(Visitor{[&](const TracedMotionEvent& e) { mTracer.mBackend->traceMotionEvent(e); },
                       [&](const TracedKeyEvent& e) { mTracer.mBackend->traceKeyEvent(e); }},
               mState->event);
    mState.reset();
               mState.event);
    mState.isEventProcessingComplete = true;
}

} // namespace android::inputdispatcher::trace::impl
+6 −5
Original line number Diff line number Diff line
@@ -51,13 +51,16 @@ private:

    // The state of a tracked event.
    struct EventState {
        explicit inline EventState(TracedEvent event) : event(std::move(event)){};

        const TracedEvent event;
        bool isEventProcessingComplete{false};
        // TODO(b/210460522): Add additional args for tracking event sensitivity and
        //  dispatch target UIDs.
    };

    // Get the event state associated with a tracking cookie.
    std::optional<EventState>& getState(const EventTrackerInterface&);
    EventState& getState(const EventTrackerInterface&);

    // Implementation of the event tracker cookie. The cookie holds the event state directly for
    // convenience to avoid the overhead of tracking the state separately in InputTracer.
@@ -68,11 +71,9 @@ private:

    private:
        InputTracer& mTracer;
        // This event tracker cookie will only hold the state as long as it has not been written
        // to the trace. The state is released when the event is written to the trace.
        mutable std::optional<EventState> mState;
        mutable EventState mState;

        friend std::optional<EventState>& InputTracer::getState(const EventTrackerInterface&);
        friend EventState& InputTracer::getState(const EventTrackerInterface&);
    };
};