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

Commit ecc38b7f authored by Prabir Pradhan's avatar Prabir Pradhan Committed by Android (Google) Code Review
Browse files

Merge changes from topics "android-input-event-data-source",...

Merge changes from topics "android-input-event-data-source", "input-trace-get-package-uid" into main

* changes:
  InputTracer: Adjust traced event sensitivity based on allow-list
  InputTracer: Distinguish sensitive events
  InputTracer: Track inbound events
  InputTracer: Read trace configuration from perfetto
parents 774f6866 bf3c8322
Loading
Loading
Loading
Loading
+6 −3
Original line number Original line Diff line number Diff line
@@ -88,12 +88,13 @@ bool isInputTracingEnabled() {
}
}


// Create the input tracing backend that writes to perfetto from a single thread.
// Create the input tracing backend that writes to perfetto from a single thread.
std::unique_ptr<trace::InputTracingBackendInterface> createInputTracingBackendIfEnabled() {
std::unique_ptr<trace::InputTracingBackendInterface> createInputTracingBackendIfEnabled(
        trace::impl::PerfettoBackend::GetPackageUid getPackageUid) {
    if (!isInputTracingEnabled()) {
    if (!isInputTracingEnabled()) {
        return nullptr;
        return nullptr;
    }
    }
    return std::make_unique<trace::impl::ThreadedBackend<trace::impl::PerfettoBackend>>(
    return std::make_unique<trace::impl::ThreadedBackend<trace::impl::PerfettoBackend>>(
            trace::impl::PerfettoBackend());
            trace::impl::PerfettoBackend(getPackageUid));
}
}


template <class Entry>
template <class Entry>
@@ -903,7 +904,9 @@ private:
// --- InputDispatcher ---
// --- InputDispatcher ---


InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy)
InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy)
      : InputDispatcher(policy, createInputTracingBackendIfEnabled()) {}
      : InputDispatcher(policy, createInputTracingBackendIfEnabled([&policy](std::string pkg) {
                            return policy.getPackageUid(pkg);
                        })) {}


InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy,
InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy,
                                 std::unique_ptr<trace::InputTracingBackendInterface> traceBackend)
                                 std::unique_ptr<trace::InputTracingBackendInterface> traceBackend)
+3 −0
Original line number Original line Diff line number Diff line
@@ -163,6 +163,9 @@ public:
    virtual void notifyDeviceInteraction(DeviceId deviceId, nsecs_t timestamp,
    virtual void notifyDeviceInteraction(DeviceId deviceId, nsecs_t timestamp,
                                         const std::set<gui::Uid>& uids) = 0;
                                         const std::set<gui::Uid>& uids) = 0;


    /* Get the UID associated with the given package. */
    virtual gui::Uid getPackageUid(std::string package) = 0;

private:
private:
    // Additional key latency in case a connection is still processing some motion events.
    // Additional key latency in case a connection is still processing some motion events.
    // This will help with the case when a user touched a button that opens a new window,
    // This will help with the case when a user touched a button that opens a new window,
+103 −9
Original line number Original line Diff line number Diff line
@@ -21,8 +21,26 @@


namespace android::inputdispatcher::trace {
namespace android::inputdispatcher::trace {


namespace {

using namespace ftl::flag_operators;

// The trace config to use for maximal tracing.
const impl::TraceConfig CONFIG_TRACE_ALL{
        .flags = impl::TraceFlag::TRACE_DISPATCHER_INPUT_EVENTS |
                impl::TraceFlag::TRACE_DISPATCHER_WINDOW_DISPATCH,
        .rules = {impl::TraceRule{.level = impl::TraceLevel::TRACE_LEVEL_COMPLETE,
                                  .matchAllPackages = {},
                                  .matchAnyPackages = {},
                                  .matchSecure{},
                                  .matchImeConnectionActive = {}}},
};

} // namespace

void AndroidInputEventProtoConverter::toProtoMotionEvent(const TracedMotionEvent& event,
void AndroidInputEventProtoConverter::toProtoMotionEvent(const TracedMotionEvent& event,
                                                         proto::AndroidMotionEvent& outProto) {
                                                         proto::AndroidMotionEvent& outProto,
                                                         bool isRedacted) {
    outProto.set_event_id(event.id);
    outProto.set_event_id(event.id);
    outProto.set_event_time_nanos(event.eventTime);
    outProto.set_event_time_nanos(event.eventTime);
    outProto.set_down_time_nanos(event.downTime);
    outProto.set_down_time_nanos(event.downTime);
@@ -31,11 +49,15 @@ void AndroidInputEventProtoConverter::toProtoMotionEvent(const TracedMotionEvent
    outProto.set_device_id(event.deviceId);
    outProto.set_device_id(event.deviceId);
    outProto.set_display_id(event.displayId);
    outProto.set_display_id(event.displayId);
    outProto.set_classification(static_cast<int32_t>(event.classification));
    outProto.set_classification(static_cast<int32_t>(event.classification));
    outProto.set_cursor_position_x(event.xCursorPosition);
    outProto.set_cursor_position_y(event.yCursorPosition);
    outProto.set_flags(event.flags);
    outProto.set_flags(event.flags);
    outProto.set_policy_flags(event.policyFlags);
    outProto.set_policy_flags(event.policyFlags);


    if (!isRedacted) {
        outProto.set_cursor_position_x(event.xCursorPosition);
        outProto.set_cursor_position_y(event.yCursorPosition);
        outProto.set_meta_state(event.metaState);
    }

    for (uint32_t i = 0; i < event.pointerProperties.size(); i++) {
    for (uint32_t i = 0; i < event.pointerProperties.size(); i++) {
        auto* pointer = outProto.add_pointer();
        auto* pointer = outProto.add_pointer();


@@ -49,13 +71,17 @@ void AndroidInputEventProtoConverter::toProtoMotionEvent(const TracedMotionEvent
            const auto axis = bits.clearFirstMarkedBit();
            const auto axis = bits.clearFirstMarkedBit();
            auto axisEntry = pointer->add_axis_value();
            auto axisEntry = pointer->add_axis_value();
            axisEntry->set_axis(axis);
            axisEntry->set_axis(axis);

            if (!isRedacted) {
                axisEntry->set_value(coords.values[axisIndex]);
                axisEntry->set_value(coords.values[axisIndex]);
            }
            }
        }
        }
    }
    }
}


void AndroidInputEventProtoConverter::toProtoKeyEvent(const TracedKeyEvent& event,
void AndroidInputEventProtoConverter::toProtoKeyEvent(const TracedKeyEvent& event,
                                                      proto::AndroidKeyEvent& outProto) {
                                                      proto::AndroidKeyEvent& outProto,
                                                      bool isRedacted) {
    outProto.set_event_id(event.id);
    outProto.set_event_id(event.id);
    outProto.set_event_time_nanos(event.eventTime);
    outProto.set_event_time_nanos(event.eventTime);
    outProto.set_down_time_nanos(event.downTime);
    outProto.set_down_time_nanos(event.downTime);
@@ -63,21 +89,28 @@ void AndroidInputEventProtoConverter::toProtoKeyEvent(const TracedKeyEvent& even
    outProto.set_action(event.action);
    outProto.set_action(event.action);
    outProto.set_device_id(event.deviceId);
    outProto.set_device_id(event.deviceId);
    outProto.set_display_id(event.displayId);
    outProto.set_display_id(event.displayId);
    outProto.set_key_code(event.keyCode);
    outProto.set_scan_code(event.scanCode);
    outProto.set_meta_state(event.metaState);
    outProto.set_repeat_count(event.repeatCount);
    outProto.set_repeat_count(event.repeatCount);
    outProto.set_flags(event.flags);
    outProto.set_flags(event.flags);
    outProto.set_policy_flags(event.policyFlags);
    outProto.set_policy_flags(event.policyFlags);

    if (!isRedacted) {
        outProto.set_key_code(event.keyCode);
        outProto.set_scan_code(event.scanCode);
        outProto.set_meta_state(event.metaState);
    }
}
}


void AndroidInputEventProtoConverter::toProtoWindowDispatchEvent(
void AndroidInputEventProtoConverter::toProtoWindowDispatchEvent(
        const WindowDispatchArgs& args, proto::AndroidWindowInputDispatchEvent& outProto) {
        const WindowDispatchArgs& args, proto::AndroidWindowInputDispatchEvent& outProto,
        bool isRedacted) {
    std::visit([&](auto entry) { outProto.set_event_id(entry.id); }, args.eventEntry);
    std::visit([&](auto entry) { outProto.set_event_id(entry.id); }, args.eventEntry);
    outProto.set_vsync_id(args.vsyncId);
    outProto.set_vsync_id(args.vsyncId);
    outProto.set_window_id(args.windowId);
    outProto.set_window_id(args.windowId);
    outProto.set_resolved_flags(args.resolvedFlags);
    outProto.set_resolved_flags(args.resolvedFlags);


    if (isRedacted) {
        return;
    }
    if (auto* motion = std::get_if<TracedMotionEvent>(&args.eventEntry); motion != nullptr) {
    if (auto* motion = std::get_if<TracedMotionEvent>(&args.eventEntry); motion != nullptr) {
        for (size_t i = 0; i < motion->pointerProperties.size(); i++) {
        for (size_t i = 0; i < motion->pointerProperties.size(); i++) {
            auto* pointerProto = outProto.add_dispatched_pointer();
            auto* pointerProto = outProto.add_dispatched_pointer();
@@ -105,4 +138,65 @@ void AndroidInputEventProtoConverter::toProtoWindowDispatchEvent(
    }
    }
}
}


impl::TraceConfig AndroidInputEventProtoConverter::parseConfig(
        proto::AndroidInputEventConfig::Decoder& protoConfig) {
    if (protoConfig.has_mode() &&
        protoConfig.mode() == proto::AndroidInputEventConfig::TRACE_MODE_TRACE_ALL) {
        // User has requested the preset for maximal tracing
        return CONFIG_TRACE_ALL;
    }

    impl::TraceConfig config;

    // Parse trace flags
    if (protoConfig.has_trace_dispatcher_input_events() &&
        protoConfig.trace_dispatcher_input_events()) {
        config.flags |= impl::TraceFlag::TRACE_DISPATCHER_INPUT_EVENTS;
    }
    if (protoConfig.has_trace_dispatcher_window_dispatch() &&
        protoConfig.trace_dispatcher_window_dispatch()) {
        config.flags |= impl::TraceFlag::TRACE_DISPATCHER_WINDOW_DISPATCH;
    }

    // Parse trace rules
    auto rulesIt = protoConfig.rules();
    while (rulesIt) {
        proto::AndroidInputEventConfig::TraceRule::Decoder protoRule{rulesIt->as_bytes()};
        config.rules.emplace_back();
        auto& rule = config.rules.back();

        rule.level = protoRule.has_trace_level()
                ? static_cast<impl::TraceLevel>(protoRule.trace_level())
                : impl::TraceLevel::TRACE_LEVEL_NONE;

        if (protoRule.has_match_all_packages()) {
            auto pkgIt = protoRule.match_all_packages();
            while (pkgIt) {
                rule.matchAllPackages.emplace_back(pkgIt->as_std_string());
                pkgIt++;
            }
        }

        if (protoRule.has_match_any_packages()) {
            auto pkgIt = protoRule.match_any_packages();
            while (pkgIt) {
                rule.matchAnyPackages.emplace_back(pkgIt->as_std_string());
                pkgIt++;
            }
        }

        if (protoRule.has_match_secure()) {
            rule.matchSecure = protoRule.match_secure();
        }

        if (protoRule.has_match_ime_connection_active()) {
            rule.matchImeConnectionActive = protoRule.match_ime_connection_active();
        }

        rulesIt++;
    }

    return config;
}

} // namespace android::inputdispatcher::trace
} // namespace android::inputdispatcher::trace
+9 −3
Original line number Original line Diff line number Diff line
@@ -16,9 +16,11 @@


#pragma once
#pragma once


#include <perfetto/config/android/android_input_event_config.pbzero.h>
#include <perfetto/trace/android/android_input_event.pbzero.h>
#include <perfetto/trace/android/android_input_event.pbzero.h>


#include "InputTracingBackendInterface.h"
#include "InputTracingBackendInterface.h"
#include "InputTracingPerfettoBackendConfig.h"


namespace proto = perfetto::protos::pbzero;
namespace proto = perfetto::protos::pbzero;


@@ -30,10 +32,14 @@ namespace android::inputdispatcher::trace {
class AndroidInputEventProtoConverter {
class AndroidInputEventProtoConverter {
public:
public:
    static void toProtoMotionEvent(const TracedMotionEvent& event,
    static void toProtoMotionEvent(const TracedMotionEvent& event,
                                   proto::AndroidMotionEvent& outProto);
                                   proto::AndroidMotionEvent& outProto, bool isRedacted);
    static void toProtoKeyEvent(const TracedKeyEvent& event, proto::AndroidKeyEvent& outProto);
    static void toProtoKeyEvent(const TracedKeyEvent& event, proto::AndroidKeyEvent& outProto,
                                bool isRedacted);
    static void toProtoWindowDispatchEvent(const WindowDispatchArgs&,
    static void toProtoWindowDispatchEvent(const WindowDispatchArgs&,
                                           proto::AndroidWindowInputDispatchEvent& outProto);
                                           proto::AndroidWindowInputDispatchEvent& outProto,
                                           bool isRedacted);

    static impl::TraceConfig parseConfig(proto::AndroidInputEventConfig::Decoder& protoConfig);
};
};


} // namespace android::inputdispatcher::trace
} // namespace android::inputdispatcher::trace
+70 −34
Original line number Original line Diff line number Diff line
@@ -19,6 +19,7 @@
#include "InputTracer.h"
#include "InputTracer.h"


#include <android-base/logging.h>
#include <android-base/logging.h>
#include <private/android_filesystem_config.h>


namespace android::inputdispatcher::trace::impl {
namespace android::inputdispatcher::trace::impl {


@@ -30,7 +31,7 @@ struct Visitor : V... {
    using V::operator()...;
    using V::operator()...;
};
};


TracedEvent createTracedEvent(const MotionEntry& e) {
TracedEvent createTracedEvent(const MotionEntry& e, EventType type) {
    return TracedMotionEvent{e.id,
    return TracedMotionEvent{e.id,
                             e.eventTime,
                             e.eventTime,
                             e.policyFlags,
                             e.policyFlags,
@@ -50,13 +51,14 @@ TracedEvent createTracedEvent(const MotionEntry& e) {
                             e.yCursorPosition,
                             e.yCursorPosition,
                             e.downTime,
                             e.downTime,
                             e.pointerProperties,
                             e.pointerProperties,
                             e.pointerCoords};
                             e.pointerCoords,
                             type};
}
}


TracedEvent createTracedEvent(const KeyEntry& e) {
TracedEvent createTracedEvent(const KeyEntry& e, EventType type) {
    return TracedKeyEvent{e.id,        e.eventTime, e.policyFlags, e.deviceId, e.source,
    return TracedKeyEvent{e.id,        e.eventTime, e.policyFlags, e.deviceId, e.source,
                          e.displayId, e.action,    e.keyCode,     e.scanCode, e.metaState,
                          e.displayId, e.action,    e.keyCode,     e.scanCode, e.metaState,
                          e.downTime,  e.flags,     e.repeatCount};
                          e.downTime,  e.flags,     e.repeatCount, type};
}
}


void writeEventToBackend(const TracedEvent& event, const TracedEventArgs args,
void writeEventToBackend(const TracedEvent& event, const TracedEventArgs args,
@@ -70,6 +72,24 @@ inline auto getId(const trace::TracedEvent& v) {
    return std::visit([](const auto& event) { return event.id; }, v);
    return std::visit([](const auto& event) { return event.id; }, v);
}
}


// Helper class to extract relevant information from InputTarget.
struct InputTargetInfo {
    gui::Uid uid;
    bool isSecureWindow;
};

InputTargetInfo getTargetInfo(const InputTarget& target) {
    if (target.windowHandle == nullptr) {
        if (!target.connection->monitor) {
            LOG(FATAL) << __func__ << ": Window is not set for non-monitor target";
        }
        // This is a global monitor, assume its target is the system.
        return {.uid = gui::Uid{AID_SYSTEM}, .isSecureWindow = false};
    }
    return {target.windowHandle->getInfo()->ownerUid,
            target.windowHandle->getInfo()->layoutParamsFlags.test(gui::WindowInfo::Flag::SECURE)};
}

} // namespace
} // namespace


// --- InputTracer ---
// --- InputTracer ---
@@ -83,10 +103,10 @@ std::unique_ptr<EventTrackerInterface> InputTracer::traceInboundEvent(const Even


    if (entry.type == EventEntry::Type::MOTION) {
    if (entry.type == EventEntry::Type::MOTION) {
        const auto& motion = static_cast<const MotionEntry&>(entry);
        const auto& motion = static_cast<const MotionEntry&>(entry);
        eventState->events.emplace_back(createTracedEvent(motion));
        eventState->events.emplace_back(createTracedEvent(motion, EventType::INBOUND));
    } else if (entry.type == EventEntry::Type::KEY) {
    } else if (entry.type == EventEntry::Type::KEY) {
        const auto& key = static_cast<const KeyEntry&>(entry);
        const auto& key = static_cast<const KeyEntry&>(entry);
        eventState->events.emplace_back(createTracedEvent(key));
        eventState->events.emplace_back(createTracedEvent(key, EventType::INBOUND));
    } else {
    } else {
        LOG(FATAL) << "Cannot trace EventEntry of type: " << ftl::enum_string(entry.type);
        LOG(FATAL) << "Cannot trace EventEntry of type: " << ftl::enum_string(entry.type);
    }
    }
@@ -103,20 +123,26 @@ std::unique_ptr<EventTrackerInterface> InputTracer::createTrackerForSyntheticEve
void InputTracer::dispatchToTargetHint(const EventTrackerInterface& cookie,
void InputTracer::dispatchToTargetHint(const EventTrackerInterface& cookie,
                                       const InputTarget& target) {
                                       const InputTarget& target) {
    auto& eventState = getState(cookie);
    auto& eventState = getState(cookie);
    const InputTargetInfo& targetInfo = getTargetInfo(target);
    if (eventState->isEventProcessingComplete) {
    if (eventState->isEventProcessingComplete) {
        // TODO(b/210460522): Disallow adding new targets after eventProcessingComplete() is called.
        // Disallow adding new targets after eventProcessingComplete() is called.
        if (eventState->targets.find(targetInfo.uid) == eventState->targets.end()) {
            LOG(FATAL) << __func__ << ": Cannot add new target after eventProcessingComplete";
        }
        return;
        return;
    }
    }
    if (isDerivedCookie(cookie)) {
    if (isDerivedCookie(cookie)) {
        // TODO(b/210460522): Disallow adding new targets from a derived cookie.
        // Disallow adding new targets from a derived cookie.
        if (eventState->targets.find(targetInfo.uid) == eventState->targets.end()) {
            LOG(FATAL) << __func__ << ": Cannot add new target from a derived cookie";
        }
        return;
        return;
    }
    }
    if (target.windowHandle != nullptr) {

        eventState->isSecure |= target.windowHandle->getInfo()->layoutParamsFlags.test(
    eventState->targets.emplace(targetInfo.uid);
                gui::WindowInfo::Flag::SECURE);
    eventState->isSecure |= targetInfo.isSecureWindow;
    // TODO(b/210460522): Set events as sensitive when the IME connection is active.
    // TODO(b/210460522): Set events as sensitive when the IME connection is active.
}
}
}


void InputTracer::eventProcessingComplete(const EventTrackerInterface& cookie) {
void InputTracer::eventProcessingComplete(const EventTrackerInterface& cookie) {
    if (isDerivedCookie(cookie)) {
    if (isDerivedCookie(cookie)) {
@@ -138,10 +164,10 @@ std::unique_ptr<EventTrackerInterface> InputTracer::traceDerivedEvent(


    if (entry.type == EventEntry::Type::MOTION) {
    if (entry.type == EventEntry::Type::MOTION) {
        const auto& motion = static_cast<const MotionEntry&>(entry);
        const auto& motion = static_cast<const MotionEntry&>(entry);
        eventState->events.emplace_back(createTracedEvent(motion));
        eventState->events.emplace_back(createTracedEvent(motion, EventType::SYNTHESIZED));
    } else if (entry.type == EventEntry::Type::KEY) {
    } else if (entry.type == EventEntry::Type::KEY) {
        const auto& key = static_cast<const KeyEntry&>(entry);
        const auto& key = static_cast<const KeyEntry&>(entry);
        eventState->events.emplace_back(createTracedEvent(key));
        eventState->events.emplace_back(createTracedEvent(key, EventType::SYNTHESIZED));
    } else {
    } else {
        LOG(FATAL) << "Cannot trace EventEntry of type: " << ftl::enum_string(entry.type);
        LOG(FATAL) << "Cannot trace EventEntry of type: " << ftl::enum_string(entry.type);
    }
    }
@@ -150,8 +176,10 @@ std::unique_ptr<EventTrackerInterface> InputTracer::traceDerivedEvent(
        // It is possible for a derived event to be dispatched some time after the original event
        // It is possible for a derived event to be dispatched some time after the original event
        // is dispatched, such as in the case of key fallback events. To account for these cases,
        // is dispatched, such as in the case of key fallback events. To account for these cases,
        // derived events can be traced after the processing is complete for the original event.
        // derived events can be traced after the processing is complete for the original event.
        const TracedEventArgs traceArgs{.isSecure = eventState->isSecure};
        const auto& event = eventState->events.back();
        writeEventToBackend(eventState->events.back(), traceArgs, *mBackend);
        const TracedEventArgs traceArgs{.isSecure = eventState->isSecure,
                                        .targets = eventState->targets};
        writeEventToBackend(event, std::move(traceArgs), *mBackend);
    }
    }
    return std::make_unique<EventTrackerImpl>(std::move(eventState), /*isDerived=*/true);
    return std::make_unique<EventTrackerImpl>(std::move(eventState), /*isDerived=*/true);
}
}
@@ -160,38 +188,34 @@ void InputTracer::traceEventDispatch(const DispatchEntry& dispatchEntry,
                                     const EventTrackerInterface& cookie) {
                                     const EventTrackerInterface& cookie) {
    auto& eventState = getState(cookie);
    auto& eventState = getState(cookie);
    const EventEntry& entry = *dispatchEntry.eventEntry;
    const EventEntry& entry = *dispatchEntry.eventEntry;
    const int32_t eventId = entry.id;
    // TODO(b/328618922): Remove resolved key repeats after making repeatCount non-mutable.
    // TODO(b/328618922): Remove resolved key repeats after making repeatCount non-mutable.
    // The KeyEntry's repeatCount is mutable and can be modified after an event is initially traced,
    // The KeyEntry's repeatCount is mutable and can be modified after an event is initially traced,
    // so we need to find the repeatCount at the time of dispatching to trace it accurately.
    // so we need to find the repeatCount at the time of dispatching to trace it accurately.
    int32_t resolvedKeyRepeatCount = 0;
    int32_t resolvedKeyRepeatCount = 0;

    if (entry.type == EventEntry::Type::KEY) {
    TracedEvent traced;
        resolvedKeyRepeatCount = static_cast<const KeyEntry&>(entry).repeatCount;
    if (entry.type == EventEntry::Type::MOTION) {
        const auto& motion = static_cast<const MotionEntry&>(entry);
        traced = createTracedEvent(motion);
    } else if (entry.type == EventEntry::Type::KEY) {
        const auto& key = static_cast<const KeyEntry&>(entry);
        resolvedKeyRepeatCount = key.repeatCount;
        traced = createTracedEvent(key);
    } else {
        LOG(FATAL) << "Cannot trace EventEntry of type: " << ftl::enum_string(entry.type);
    }
    }


    auto tracedEventIt =
    auto tracedEventIt =
            std::find_if(eventState->events.begin(), eventState->events.end(),
            std::find_if(eventState->events.begin(), eventState->events.end(),
                         [&traced](const auto& event) { return getId(traced) == getId(event); });
                         [eventId](const auto& event) { return eventId == getId(event); });
    if (tracedEventIt == eventState->events.end()) {
    if (tracedEventIt == eventState->events.end()) {
        LOG(FATAL)
        LOG(FATAL)
                << __func__
                << __func__
                << ": Failed to find a previously traced event that matches the dispatched event";
                << ": Failed to find a previously traced event that matches the dispatched event";
    }
    }


    if (eventState->targets.count(dispatchEntry.targetUid) == 0) {
        LOG(FATAL) << __func__ << ": Event is being dispatched to UID that it is not targeting";
    }

    // The vsyncId only has meaning if the event is targeting a window.
    // The vsyncId only has meaning if the event is targeting a window.
    const int32_t windowId = dispatchEntry.windowId.value_or(0);
    const int32_t windowId = dispatchEntry.windowId.value_or(0);
    const int32_t vsyncId = dispatchEntry.windowId.has_value() ? dispatchEntry.vsyncId : 0;
    const int32_t vsyncId = dispatchEntry.windowId.has_value() ? dispatchEntry.vsyncId : 0;


    // TODO(b/210460522): Pass HMAC into traceEventDispatch.
    // TODO(b/210460522): Pass HMAC into traceEventDispatch.
    const WindowDispatchArgs windowDispatchArgs{std::move(traced),
    const WindowDispatchArgs windowDispatchArgs{*tracedEventIt,
                                                dispatchEntry.deliveryTime,
                                                dispatchEntry.deliveryTime,
                                                dispatchEntry.resolvedFlags,
                                                dispatchEntry.resolvedFlags,
                                                dispatchEntry.targetUid,
                                                dispatchEntry.targetUid,
@@ -202,8 +226,9 @@ void InputTracer::traceEventDispatch(const DispatchEntry& dispatchEntry,
                                                /*hmac=*/{},
                                                /*hmac=*/{},
                                                resolvedKeyRepeatCount};
                                                resolvedKeyRepeatCount};
    if (eventState->isEventProcessingComplete) {
    if (eventState->isEventProcessingComplete) {
        mBackend->traceWindowDispatch(std::move(windowDispatchArgs),
        const TracedEventArgs traceArgs{.isSecure = eventState->isSecure,
                                      TracedEventArgs{.isSecure = eventState->isSecure});
                                        .targets = eventState->targets};
        mBackend->traceWindowDispatch(std::move(windowDispatchArgs), std::move(traceArgs));
    } else {
    } else {
        eventState->pendingDispatchArgs.emplace_back(std::move(windowDispatchArgs));
        eventState->pendingDispatchArgs.emplace_back(std::move(windowDispatchArgs));
    }
    }
@@ -222,13 +247,24 @@ bool InputTracer::isDerivedCookie(const EventTrackerInterface& cookie) {


void InputTracer::EventState::onEventProcessingComplete() {
void InputTracer::EventState::onEventProcessingComplete() {
    // Write all of the events known so far to the trace.
    // Write all of the events known so far to the trace.
    const TracedEventArgs traceArgs{.isSecure = isSecure};
    for (const auto& event : events) {
    for (const auto& event : events) {
        const TracedEventArgs traceArgs{.isSecure = isSecure, .targets = targets};
        writeEventToBackend(event, traceArgs, *tracer.mBackend);
        writeEventToBackend(event, traceArgs, *tracer.mBackend);
    }
    }
    // Write all pending dispatch args to the trace.
    // Write all pending dispatch args to the trace.
    for (const auto& windowDispatchArgs : pendingDispatchArgs) {
    for (const auto& windowDispatchArgs : pendingDispatchArgs) {
        tracer.mBackend->traceWindowDispatch(windowDispatchArgs, traceArgs);
        auto tracedEventIt =
                std::find_if(events.begin(), events.end(),
                             [id = getId(windowDispatchArgs.eventEntry)](const auto& event) {
                                 return id == getId(event);
                             });
        if (tracedEventIt == events.end()) {
            LOG(FATAL) << __func__
                       << ": Failed to find a previously traced event that matches the dispatched "
                          "event";
        }
        const TracedEventArgs traceArgs{.isSecure = isSecure, .targets = targets};
        tracer.mBackend->traceWindowDispatch(windowDispatchArgs, std::move(traceArgs));
    }
    }
    pendingDispatchArgs.clear();
    pendingDispatchArgs.clear();


Loading