Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2024 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "InputTracer" |
| 18 | |
| 19 | #include "InputTracer.h" |
| 20 | |
| 21 | #include <android-base/logging.h> |
| 22 | |
| 23 | namespace android::inputdispatcher::trace::impl { |
| 24 | |
| 25 | namespace { |
| 26 | |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 27 | // Helper to std::visit with lambdas. |
| 28 | template <typename... V> |
| 29 | struct Visitor : V... { |
| 30 | using V::operator()...; |
| 31 | }; |
| 32 | |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 33 | TracedEvent createTracedEvent(const MotionEntry& e) { |
| 34 | return TracedMotionEvent{e.id, |
| 35 | e.eventTime, |
| 36 | e.policyFlags, |
| 37 | e.deviceId, |
| 38 | e.source, |
| 39 | e.displayId, |
| 40 | e.action, |
| 41 | e.actionButton, |
| 42 | e.flags, |
| 43 | e.metaState, |
| 44 | e.buttonState, |
| 45 | e.classification, |
| 46 | e.edgeFlags, |
| 47 | e.xPrecision, |
| 48 | e.yPrecision, |
| 49 | e.xCursorPosition, |
| 50 | e.yCursorPosition, |
| 51 | e.downTime, |
| 52 | e.pointerProperties, |
| 53 | e.pointerCoords}; |
| 54 | } |
| 55 | |
| 56 | TracedEvent createTracedEvent(const KeyEntry& e) { |
| 57 | return TracedKeyEvent{e.id, e.eventTime, e.policyFlags, e.deviceId, e.source, |
| 58 | e.displayId, e.action, e.keyCode, e.scanCode, e.metaState, |
| 59 | e.downTime, e.flags, e.repeatCount}; |
| 60 | } |
| 61 | |
Prabir Pradhan | 8c3b143 | 2024-02-09 23:34:16 +0000 | [diff] [blame] | 62 | void writeEventToBackend(const TracedEvent& event, const TracedEventArgs args, |
| 63 | InputTracingBackendInterface& backend) { |
| 64 | std::visit(Visitor{[&](const TracedMotionEvent& e) { backend.traceMotionEvent(e, args); }, |
| 65 | [&](const TracedKeyEvent& e) { backend.traceKeyEvent(e, args); }}, |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 66 | event); |
| 67 | } |
| 68 | |
Prabir Pradhan | d6b2b05 | 2024-02-21 23:25:15 +0000 | [diff] [blame] | 69 | inline auto getId(const trace::TracedEvent& v) { |
| 70 | return std::visit([](const auto& event) { return event.id; }, v); |
| 71 | } |
| 72 | |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 73 | } // namespace |
| 74 | |
| 75 | // --- InputTracer --- |
| 76 | |
| 77 | InputTracer::InputTracer(std::unique_ptr<InputTracingBackendInterface> backend) |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 78 | : mBackend(std::move(backend)) {} |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 79 | |
| 80 | std::unique_ptr<EventTrackerInterface> InputTracer::traceInboundEvent(const EventEntry& entry) { |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 81 | // This is a newly traced inbound event. Create a new state to track it and its derived events. |
| 82 | auto eventState = std::make_shared<EventState>(*this); |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 83 | |
| 84 | if (entry.type == EventEntry::Type::MOTION) { |
| 85 | const auto& motion = static_cast<const MotionEntry&>(entry); |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 86 | eventState->events.emplace_back(createTracedEvent(motion)); |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 87 | } else if (entry.type == EventEntry::Type::KEY) { |
| 88 | const auto& key = static_cast<const KeyEntry&>(entry); |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 89 | eventState->events.emplace_back(createTracedEvent(key)); |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 90 | } else { |
| 91 | LOG(FATAL) << "Cannot trace EventEntry of type: " << ftl::enum_string(entry.type); |
| 92 | } |
| 93 | |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 94 | return std::make_unique<EventTrackerImpl>(std::move(eventState), /*isDerived=*/false); |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Prabir Pradhan | d6b2b05 | 2024-02-21 23:25:15 +0000 | [diff] [blame] | 97 | std::unique_ptr<EventTrackerInterface> InputTracer::createTrackerForSyntheticEvent() { |
| 98 | // Create a new EventState to track events derived from this tracker. |
| 99 | return std::make_unique<EventTrackerImpl>(std::make_shared<EventState>(*this), |
| 100 | /*isDerived=*/false); |
| 101 | } |
| 102 | |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 103 | void InputTracer::dispatchToTargetHint(const EventTrackerInterface& cookie, |
| 104 | const InputTarget& target) { |
Prabir Pradhan | 1ea04a3 | 2024-02-10 03:02:59 +0000 | [diff] [blame] | 105 | auto& eventState = getState(cookie); |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 106 | if (eventState->isEventProcessingComplete) { |
| 107 | // TODO(b/210460522): Disallow adding new targets after eventProcessingComplete() is called. |
| 108 | return; |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 109 | } |
Prabir Pradhan | b95d4aa | 2024-03-11 22:48:04 +0000 | [diff] [blame] | 110 | if (isDerivedCookie(cookie)) { |
| 111 | // TODO(b/210460522): Disallow adding new targets from a derived cookie. |
| 112 | return; |
| 113 | } |
Prabir Pradhan | 8c3b143 | 2024-02-09 23:34:16 +0000 | [diff] [blame] | 114 | if (target.windowHandle != nullptr) { |
| 115 | eventState->isSecure |= target.windowHandle->getInfo()->layoutParamsFlags.test( |
| 116 | gui::WindowInfo::Flag::SECURE); |
| 117 | // TODO(b/210460522): Set events as sensitive when the IME connection is active. |
| 118 | } |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void InputTracer::eventProcessingComplete(const EventTrackerInterface& cookie) { |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 122 | if (isDerivedCookie(cookie)) { |
| 123 | LOG(FATAL) << "Event processing cannot be set from a derived cookie."; |
| 124 | } |
Prabir Pradhan | 1ea04a3 | 2024-02-10 03:02:59 +0000 | [diff] [blame] | 125 | auto& eventState = getState(cookie); |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 126 | if (eventState->isEventProcessingComplete) { |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 127 | LOG(FATAL) << "Traced event was already logged. " |
| 128 | "eventProcessingComplete() was likely called more than once."; |
| 129 | } |
Prabir Pradhan | e770164 | 2024-02-22 04:06:53 +0000 | [diff] [blame] | 130 | eventState->onEventProcessingComplete(); |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | std::unique_ptr<EventTrackerInterface> InputTracer::traceDerivedEvent( |
| 134 | const EventEntry& entry, const EventTrackerInterface& originalEventCookie) { |
| 135 | // This is an event derived from an already-established event. Use the same state to track |
| 136 | // this event too. |
| 137 | auto eventState = getState(originalEventCookie); |
| 138 | |
| 139 | if (entry.type == EventEntry::Type::MOTION) { |
| 140 | const auto& motion = static_cast<const MotionEntry&>(entry); |
| 141 | eventState->events.emplace_back(createTracedEvent(motion)); |
| 142 | } else if (entry.type == EventEntry::Type::KEY) { |
| 143 | const auto& key = static_cast<const KeyEntry&>(entry); |
| 144 | eventState->events.emplace_back(createTracedEvent(key)); |
| 145 | } else { |
| 146 | LOG(FATAL) << "Cannot trace EventEntry of type: " << ftl::enum_string(entry.type); |
| 147 | } |
| 148 | |
| 149 | if (eventState->isEventProcessingComplete) { |
| 150 | // It is possible for a derived event to be dispatched some time after the original event |
| 151 | // is dispatched, such as in the case of key fallback events. To account for these cases, |
| 152 | // derived events can be traced after the processing is complete for the original event. |
Prabir Pradhan | 8c3b143 | 2024-02-09 23:34:16 +0000 | [diff] [blame] | 153 | const TracedEventArgs traceArgs{.isSecure = eventState->isSecure}; |
| 154 | writeEventToBackend(eventState->events.back(), traceArgs, *mBackend); |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 155 | } |
| 156 | return std::make_unique<EventTrackerImpl>(std::move(eventState), /*isDerived=*/true); |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void InputTracer::traceEventDispatch(const DispatchEntry& dispatchEntry, |
Prabir Pradhan | d6b2b05 | 2024-02-21 23:25:15 +0000 | [diff] [blame] | 160 | const EventTrackerInterface& cookie) { |
| 161 | auto& eventState = getState(cookie); |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 162 | const EventEntry& entry = *dispatchEntry.eventEntry; |
Prabir Pradhan | 560d0d1 | 2024-03-07 18:08:27 +0000 | [diff] [blame] | 163 | // TODO(b/328618922): Remove resolved key repeats after making repeatCount non-mutable. |
| 164 | // The KeyEntry's repeatCount is mutable and can be modified after an event is initially traced, |
| 165 | // so we need to find the repeatCount at the time of dispatching to trace it accurately. |
| 166 | int32_t resolvedKeyRepeatCount = 0; |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 167 | |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 168 | TracedEvent traced; |
| 169 | if (entry.type == EventEntry::Type::MOTION) { |
| 170 | const auto& motion = static_cast<const MotionEntry&>(entry); |
| 171 | traced = createTracedEvent(motion); |
| 172 | } else if (entry.type == EventEntry::Type::KEY) { |
| 173 | const auto& key = static_cast<const KeyEntry&>(entry); |
Prabir Pradhan | 560d0d1 | 2024-03-07 18:08:27 +0000 | [diff] [blame] | 174 | resolvedKeyRepeatCount = key.repeatCount; |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 175 | traced = createTracedEvent(key); |
| 176 | } else { |
| 177 | LOG(FATAL) << "Cannot trace EventEntry of type: " << ftl::enum_string(entry.type); |
| 178 | } |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 179 | |
Prabir Pradhan | d6b2b05 | 2024-02-21 23:25:15 +0000 | [diff] [blame] | 180 | auto tracedEventIt = |
| 181 | std::find_if(eventState->events.begin(), eventState->events.end(), |
| 182 | [&traced](const auto& event) { return getId(traced) == getId(event); }); |
| 183 | if (tracedEventIt == eventState->events.end()) { |
| 184 | LOG(FATAL) |
| 185 | << __func__ |
| 186 | << ": Failed to find a previously traced event that matches the dispatched event"; |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 187 | } |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 188 | |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 189 | // The vsyncId only has meaning if the event is targeting a window. |
| 190 | const int32_t windowId = dispatchEntry.windowId.value_or(0); |
| 191 | const int32_t vsyncId = dispatchEntry.windowId.has_value() ? dispatchEntry.vsyncId : 0; |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 192 | |
Prabir Pradhan | 8c3b143 | 2024-02-09 23:34:16 +0000 | [diff] [blame] | 193 | // TODO(b/210460522): Pass HMAC into traceEventDispatch. |
Prabir Pradhan | 52ec3ff | 2024-02-22 03:26:38 +0000 | [diff] [blame] | 194 | const WindowDispatchArgs windowDispatchArgs{std::move(traced), |
| 195 | dispatchEntry.deliveryTime, |
| 196 | dispatchEntry.resolvedFlags, |
| 197 | dispatchEntry.targetUid, |
| 198 | vsyncId, |
| 199 | windowId, |
| 200 | dispatchEntry.transform, |
| 201 | dispatchEntry.rawTransform, |
| 202 | /*hmac=*/{}, |
| 203 | resolvedKeyRepeatCount}; |
| 204 | if (eventState->isEventProcessingComplete) { |
Prabir Pradhan | 8c3b143 | 2024-02-09 23:34:16 +0000 | [diff] [blame] | 205 | mBackend->traceWindowDispatch(std::move(windowDispatchArgs), |
| 206 | TracedEventArgs{.isSecure = eventState->isSecure}); |
Prabir Pradhan | 52ec3ff | 2024-02-22 03:26:38 +0000 | [diff] [blame] | 207 | } else { |
| 208 | eventState->pendingDispatchArgs.emplace_back(std::move(windowDispatchArgs)); |
| 209 | } |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 210 | } |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 211 | |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 212 | std::shared_ptr<InputTracer::EventState>& InputTracer::getState( |
| 213 | const EventTrackerInterface& cookie) { |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 214 | return static_cast<const EventTrackerImpl&>(cookie).mState; |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 217 | bool InputTracer::isDerivedCookie(const EventTrackerInterface& cookie) { |
| 218 | return static_cast<const EventTrackerImpl&>(cookie).mIsDerived; |
| 219 | } |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 220 | |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 221 | // --- InputTracer::EventState --- |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 222 | |
Prabir Pradhan | e770164 | 2024-02-22 04:06:53 +0000 | [diff] [blame] | 223 | void InputTracer::EventState::onEventProcessingComplete() { |
| 224 | // Write all of the events known so far to the trace. |
Prabir Pradhan | 8c3b143 | 2024-02-09 23:34:16 +0000 | [diff] [blame] | 225 | const TracedEventArgs traceArgs{.isSecure = isSecure}; |
Prabir Pradhan | e770164 | 2024-02-22 04:06:53 +0000 | [diff] [blame] | 226 | for (const auto& event : events) { |
Prabir Pradhan | 8c3b143 | 2024-02-09 23:34:16 +0000 | [diff] [blame] | 227 | writeEventToBackend(event, traceArgs, *tracer.mBackend); |
Prabir Pradhan | e770164 | 2024-02-22 04:06:53 +0000 | [diff] [blame] | 228 | } |
Prabir Pradhan | 52ec3ff | 2024-02-22 03:26:38 +0000 | [diff] [blame] | 229 | // Write all pending dispatch args to the trace. |
| 230 | for (const auto& windowDispatchArgs : pendingDispatchArgs) { |
Prabir Pradhan | 8c3b143 | 2024-02-09 23:34:16 +0000 | [diff] [blame] | 231 | tracer.mBackend->traceWindowDispatch(windowDispatchArgs, traceArgs); |
Prabir Pradhan | 52ec3ff | 2024-02-22 03:26:38 +0000 | [diff] [blame] | 232 | } |
| 233 | pendingDispatchArgs.clear(); |
| 234 | |
Prabir Pradhan | e770164 | 2024-02-22 04:06:53 +0000 | [diff] [blame] | 235 | isEventProcessingComplete = true; |
| 236 | } |
| 237 | |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 238 | InputTracer::EventState::~EventState() { |
| 239 | if (isEventProcessingComplete) { |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 240 | // This event has already been written to the trace as expected. |
| 241 | return; |
| 242 | } |
Prabir Pradhan | 1ea04a3 | 2024-02-10 03:02:59 +0000 | [diff] [blame] | 243 | // The event processing was never marked as complete, so do it now. |
Prabir Pradhan | e770164 | 2024-02-22 04:06:53 +0000 | [diff] [blame] | 244 | // We should never end up here in normal operation. However, in tests, it's possible that we |
| 245 | // stop and destroy InputDispatcher without waiting for it to finish processing events, at |
| 246 | // which point an event (and thus its EventState) may be destroyed before processing finishes. |
| 247 | onEventProcessingComplete(); |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | } // namespace android::inputdispatcher::trace::impl |