blob: af6eefb1dbcc5d24d17099ac9112d181ff740265 [file] [log] [blame]
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00001/*
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#pragma once
18
19#include "../Entry.h"
20#include "../InputTarget.h"
21#include "EventTrackerInterface.h"
22
23namespace android::inputdispatcher::trace {
24
25/**
26 * InputTracerInterface is the tracing interface for InputDispatcher.
27 *
28 * The tracer is responsible for tracing information about input events and where they are
29 * dispatched. The trace is logged to the backend using the InputTracingBackendInterface.
30 *
31 * A normal traced event should have the following lifecycle:
32 * - The EventTracker is obtained from traceInboundEvent(), after which point the event
33 * should not change.
34 * - While the event is being processed, dispatchToTargetHint() is called for each target that
35 * the event will be eventually sent to.
36 * - Once all targets have been determined, eventProcessingComplete() is called, at which point
37 * the tracer will have enough information to commit the event to the trace.
38 * - For each event that is dispatched to the client, traceEventDispatch() is called, and the
39 * tracer will record that the event was sent to the client.
40 */
41class InputTracerInterface {
42public:
43 InputTracerInterface() = default;
44 virtual ~InputTracerInterface() = default;
45 InputTracerInterface(const InputTracerInterface&) = delete;
46 InputTracerInterface& operator=(const InputTracerInterface&) = delete;
47
48 /**
49 * Trace an input event that is being processed by InputDispatcher. The event must not be
50 * modified after it is traced to keep the traced event consistent with the event that is
51 * eventually dispatched. An EventTracker is returned for each traced event that should be used
52 * to track the event's lifecycle inside InputDispatcher.
53 */
54 virtual std::unique_ptr<EventTrackerInterface> traceInboundEvent(const EventEntry&) = 0;
Prabir Pradhand6b2b052024-02-21 23:25:15 +000055
56 /**
57 * Create a trace tracker for a synthetic event that does not stem from an inbound input event.
58 * This includes things like generating cancellations or down events for various reasons,
59 * such as ANR, pilfering, transfer touch, etc. Any key or motion events generated for this
60 * synthetic event should be traced as a derived event using {@link #traceDerivedEvent}.
61 */
62 virtual std::unique_ptr<EventTrackerInterface> createTrackerForSyntheticEvent() = 0;
63
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +000064 /**
65 * Notify the tracer that the traced event will be sent to the given InputTarget.
66 * The tracer may change how the event is logged depending on the target. For example,
67 * events targeting certain UIDs may be logged as sensitive events.
68 * This may be called 0 or more times for each tracked event before event processing is
69 * completed.
70 */
71 virtual void dispatchToTargetHint(const EventTrackerInterface&, const InputTarget&) = 0;
72
73 /**
74 * Notify the tracer that the event processing is complete. This may be called at most once
75 * for each traced event. If a tracked event is dropped before it can be processed, it is
76 * possible that this is never called before the EventTracker is destroyed.
77 *
78 * This is used to commit the event to the trace in a timely manner, rather than always
79 * waiting for the event to go out of scope (and thus for the EventTracker to be destroyed)
80 * before committing. The point at which the event is destroyed can depend on several factors
81 * outside of our control, such as how long apps take to respond, so we don't want to depend on
82 * that.
83 */
84 virtual void eventProcessingComplete(const EventTrackerInterface&) = 0;
85
86 /**
Prabir Pradhana67623c2024-02-21 06:57:36 +000087 * Trace an input event that is derived from another event. This is used in cases where an event
88 * is modified from the original, such as when a touch is split across multiple windows, or
89 * when a HOVER_MOVE event is modified to be a HOVER_EXIT, etc. The original event's tracker
90 * must be provided, and a new EventTracker is returned that should be used to track the event's
91 * lifecycle.
92 *
93 * NOTE: The derived tracker cannot be used to change the targets of the original event, meaning
94 * it cannot be used with {@link #dispatchToTargetHint} or {@link eventProcessingComplete}.
95 */
96 virtual std::unique_ptr<EventTrackerInterface> traceDerivedEvent(
97 const EventEntry&, const EventTrackerInterface& originalEventTracker) = 0;
98
99 /**
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +0000100 * Trace an input event being successfully dispatched to a window. The dispatched event may
Prabir Pradhand6b2b052024-02-21 23:25:15 +0000101 * be a previously traced inbound event, or it may be a synthesized event. All dispatched events
102 * must have been previously traced, so the trace tracker associated with the event must be
103 * provided.
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +0000104 */
Prabir Pradhand6b2b052024-02-21 23:25:15 +0000105 virtual void traceEventDispatch(const DispatchEntry&, const EventTrackerInterface&) = 0;
Prabir Pradhan04a66422024-03-15 22:45:27 +0000106
107 /**
108 * Notify that the state of the input method connection changed.
109 */
110 virtual void setInputMethodConnectionIsActive(bool isActive) = 0;
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +0000111};
112
113} // namespace android::inputdispatcher::trace