blob: c8b25c996164807d2f62be4ac2ac3ae8840b7124 [file] [log] [blame]
Prabir Pradhandae52792023-12-15 07:36:40 +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 "InputTracerInterface.h"
20
Prabir Pradhandae52792023-12-15 07:36:40 +000021#include <memory>
Prabir Pradhandae52792023-12-15 07:36:40 +000022
23#include "../Entry.h"
24#include "InputTracingBackendInterface.h"
25
26namespace android::inputdispatcher::trace::impl {
27
28/**
29 * The tracer implementation for InputDispatcher.
30 *
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000031 * InputTracer's responsibility is to keep track of events as they are processed by InputDispatcher,
32 * and to write the events to the tracing backend when enough information is collected. InputTracer
33 * is not thread-safe.
Prabir Pradhandae52792023-12-15 07:36:40 +000034 *
35 * See the documentation in InputTracerInterface for the API surface.
36 */
37class InputTracer : public InputTracerInterface {
38public:
39 explicit InputTracer(std::unique_ptr<InputTracingBackendInterface>);
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000040 ~InputTracer() = default;
Prabir Pradhandae52792023-12-15 07:36:40 +000041 InputTracer(const InputTracer&) = delete;
42 InputTracer& operator=(const InputTracer&) = delete;
43
44 std::unique_ptr<EventTrackerInterface> traceInboundEvent(const EventEntry&) override;
45 void dispatchToTargetHint(const EventTrackerInterface&, const InputTarget&) override;
46 void eventProcessingComplete(const EventTrackerInterface&) override;
47 void traceEventDispatch(const DispatchEntry&, const EventTrackerInterface*) override;
48
49private:
Prabir Pradhandae52792023-12-15 07:36:40 +000050 std::unique_ptr<InputTracingBackendInterface> mBackend;
51
52 // The state of a tracked event.
53 struct EventState {
54 const TracedEvent event;
55 // TODO(b/210460522): Add additional args for tracking event sensitivity and
56 // dispatch target UIDs.
57 };
Prabir Pradhandae52792023-12-15 07:36:40 +000058
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000059 // Get the event state associated with a tracking cookie.
60 std::optional<EventState>& getState(const EventTrackerInterface&);
Prabir Pradhandae52792023-12-15 07:36:40 +000061
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000062 // Implementation of the event tracker cookie. The cookie holds the event state directly for
63 // convenience to avoid the overhead of tracking the state separately in InputTracer.
Prabir Pradhandae52792023-12-15 07:36:40 +000064 class EventTrackerImpl : public EventTrackerInterface {
65 public:
66 explicit EventTrackerImpl(InputTracer&, TracedEvent&& entry);
67 virtual ~EventTrackerImpl() override;
68
69 private:
70 InputTracer& mTracer;
71 // This event tracker cookie will only hold the state as long as it has not been written
72 // to the trace. The state is released when the event is written to the trace.
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000073 mutable std::optional<EventState> mState;
Prabir Pradhandae52792023-12-15 07:36:40 +000074
Prabir Pradhandae52792023-12-15 07:36:40 +000075 friend std::optional<EventState>& InputTracer::getState(const EventTrackerInterface&);
76 };
Prabir Pradhandae52792023-12-15 07:36:40 +000077};
78
79} // namespace android::inputdispatcher::trace::impl