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 #pragma once
18 
19 #include "../Entry.h"
20 #include "../InputTarget.h"
21 #include "EventTrackerInterface.h"
22 
23 namespace 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  */
41 class InputTracerInterface {
42 public:
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;
55 
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 
64     /**
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&,
85                                          nsecs_t processingTimestamp) = 0;
86 
87     /**
88      * Trace an input event that is derived from another event. This is used in cases where an event
89      * is modified from the original, such as when a touch is split across multiple windows, or
90      * when a HOVER_MOVE event is modified to be a HOVER_EXIT, etc. The original event's tracker
91      * must be provided, and a new EventTracker is returned that should be used to track the event's
92      * lifecycle.
93      *
94      * NOTE: The derived tracker cannot be used to change the targets of the original event, meaning
95      * it cannot be used with {@link #dispatchToTargetHint} or {@link eventProcessingComplete}.
96      */
97     virtual std::unique_ptr<EventTrackerInterface> traceDerivedEvent(
98             const EventEntry&, const EventTrackerInterface& originalEventTracker) = 0;
99 
100     /**
101      * Trace an input event being successfully dispatched to a window. The dispatched event may
102      * be a previously traced inbound event, or it may be a synthesized event. All dispatched events
103      * must have been previously traced, so the trace tracker associated with the event must be
104      * provided.
105      */
106     virtual void traceEventDispatch(const DispatchEntry&, const EventTrackerInterface&) = 0;
107 
108     /**
109      * Notify that the state of the input method connection changed.
110      */
111     virtual void setInputMethodConnectionIsActive(bool isActive) = 0;
112 };
113 
114 } // namespace android::inputdispatcher::trace
115