File: utils.cc

package info (click to toggle)
chromium 146.0.7680.153-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,057,156 kB
  • sloc: cpp: 36,426,539; ansic: 7,626,206; javascript: 3,599,825; python: 1,658,592; xml: 842,302; asm: 722,011; pascal: 186,153; sh: 88,976; perl: 88,684; objc: 79,984; sql: 60,492; cs: 42,470; fortran: 24,101; makefile: 21,141; tcl: 15,277; php: 14,022; yacc: 9,154; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (182 lines) | stat: -rw-r--r-- 7,817 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/input/utils.h"

#include <string>

#include "base/time/time.h"
#include "build/build_config.h"
#include "third_party/blink/public/mojom/input/input_event_result.mojom-shared.h"

#if BUILDFLAG(IS_ANDROID)
#include "base/android/android_info.h"
#include "base/android/jni_android.h"
#include "components/input/android/jni_headers/InputUtils_jni.h"
#include "components/input/features.h"
#endif

namespace input {

using blink::WebInputEvent;
using blink::mojom::InputEventResultState;
using perfetto::protos::pbzero::ChromeLatencyInfo2;

#if BUILDFLAG(IS_ANDROID)
static bool JNI_InputUtils_IsTransferInputToVizSupported(JNIEnv* env) {
  return InputUtils::IsTransferInputToVizSupported();
}
#endif

// static
bool InputUtils::IsTransferInputToVizSupported() {
#if BUILDFLAG(IS_ANDROID)
  if (base::android::android_info::sdk_int() <
      base::android::android_info::SdkVersion::SDK_VERSION_V) {
    // InputOnViz does not work on < Android V, since the touch transfer APIs
    // were introduced in Android V.
    return false;
  }

  if (base::android::android_info::sdk_int() ==
      base::android::android_info::SdkVersion::SDK_VERSION_V) {
    // Allow enabling on userdebug builds to have test coverage on older Android
    // 15 bots.
    return base::android::android_info::is_debug_android() &&
           base::FeatureList::IsEnabled(input::features::kInputOnViz);
  }

  // Android 16+.
  return base::FeatureList::IsEnabled(input::features::kInputOnViz);
#else
  return false;
#endif
}

#if BUILDFLAG(IS_ANDROID)

void InputUtils::RunGarbageCollection() {
  Java_InputUtils_runGarbageCollection(base::android::AttachCurrentThread());
}

#endif

ChromeLatencyInfo2::InputType InputEventTypeToProto(
    blink::WebInputEvent::Type event_type) {
  switch (event_type) {
    case WebInputEvent::Type::kUndefined:
      return ChromeLatencyInfo2::InputType::UNDEFINED_EVENT;
    case WebInputEvent::Type::kMouseDown:
      return ChromeLatencyInfo2::InputType::MOUSE_DOWN_EVENT;
    case WebInputEvent::Type::kMouseUp:
      return ChromeLatencyInfo2::InputType::MOUSE_UP_EVENT;
    case WebInputEvent::Type::kMouseMove:
      return ChromeLatencyInfo2::InputType::MOUSE_MOVE_EVENT;
    case WebInputEvent::Type::kMouseEnter:
      return ChromeLatencyInfo2::InputType::MOUSE_ENTER_EVENT;
    case WebInputEvent::Type::kMouseLeave:
      return ChromeLatencyInfo2::InputType::MOUSE_LEAVE_EVENT;
    case WebInputEvent::Type::kContextMenu:
      return ChromeLatencyInfo2::InputType::CONTEXT_MENU_EVENT;
    case WebInputEvent::Type::kMouseWheel:
      return ChromeLatencyInfo2::InputType::MOUSE_WHEEL_EVENT;
    case WebInputEvent::Type::kRawKeyDown:
      return ChromeLatencyInfo2::InputType::RAW_KEY_DOWN_EVENT;
    case WebInputEvent::Type::kKeyDown:
      return ChromeLatencyInfo2::InputType::KEY_DOWN_EVENT;
    case WebInputEvent::Type::kKeyUp:
      return ChromeLatencyInfo2::InputType::KEY_UP_EVENT;
    case WebInputEvent::Type::kChar:
      return ChromeLatencyInfo2::InputType::CHAR_EVENT;
    case WebInputEvent::Type::kGestureScrollBegin:
      return ChromeLatencyInfo2::InputType::GESTURE_SCROLL_BEGIN_EVENT;
    case WebInputEvent::Type::kGestureScrollEnd:
      return ChromeLatencyInfo2::InputType::GESTURE_SCROLL_END_EVENT;
    case WebInputEvent::Type::kGestureScrollUpdate:
      return ChromeLatencyInfo2::InputType::GESTURE_SCROLL_UPDATE_EVENT;
    case WebInputEvent::Type::kGestureFlingStart:
      return ChromeLatencyInfo2::InputType::GESTURE_FLING_START_EVENT;
    case WebInputEvent::Type::kGestureFlingCancel:
      return ChromeLatencyInfo2::InputType::GESTURE_FLING_CANCEL_EVENT;
    case WebInputEvent::Type::kGesturePinchBegin:
      return ChromeLatencyInfo2::InputType::GESTURE_PINCH_BEGIN_EVENT;
    case WebInputEvent::Type::kGesturePinchEnd:
      return ChromeLatencyInfo2::InputType::GESTURE_PINCH_END_EVENT;
    case WebInputEvent::Type::kGesturePinchUpdate:
      return ChromeLatencyInfo2::InputType::GESTURE_PINCH_UPDATE_EVENT;
    case WebInputEvent::Type::kGestureBegin:
      return ChromeLatencyInfo2::InputType::GESTURE_BEGIN_EVENT;
    case WebInputEvent::Type::kGestureTapDown:
      return ChromeLatencyInfo2::InputType::GESTURE_TAP_DOWN_EVENT;
    case WebInputEvent::Type::kGestureShowPress:
      return ChromeLatencyInfo2::InputType::GESTURE_SHOW_PRESS_EVENT;
    case WebInputEvent::Type::kGestureTap:
      return ChromeLatencyInfo2::InputType::GESTURE_TAP_EVENT;
    case WebInputEvent::Type::kGestureTapCancel:
      return ChromeLatencyInfo2::InputType::GESTURE_TAP_CANCEL_EVENT;
    case WebInputEvent::Type::kGestureShortPress:
      return ChromeLatencyInfo2::InputType::GESTURE_SHORT_PRESS_EVENT;
    case WebInputEvent::Type::kGestureLongPress:
      return ChromeLatencyInfo2::InputType::GESTURE_LONG_PRESS_EVENT;
    case WebInputEvent::Type::kGestureLongTap:
      return ChromeLatencyInfo2::InputType::GESTURE_LONG_TAP_EVENT;
    case WebInputEvent::Type::kGestureTwoFingerTap:
      return ChromeLatencyInfo2::InputType::GESTURE_TWO_FINGER_TAP_EVENT;
    case WebInputEvent::Type::kGestureTapUnconfirmed:
      return ChromeLatencyInfo2::InputType::GESTURE_TAP_UNCONFIRMED_EVENT;
    case WebInputEvent::Type::kGestureDoubleTap:
      return ChromeLatencyInfo2::InputType::GESTURE_DOUBLE_TAP_EVENT;
    case WebInputEvent::Type::kGestureEnd:
      return ChromeLatencyInfo2::InputType::GESTURE_END_EVENT;
    case WebInputEvent::Type::kTouchStart:
      return ChromeLatencyInfo2::InputType::TOUCH_START_EVENT;
    case WebInputEvent::Type::kTouchMove:
      return ChromeLatencyInfo2::InputType::TOUCH_MOVE_EVENT;
    case WebInputEvent::Type::kTouchEnd:
      return ChromeLatencyInfo2::InputType::TOUCH_END_EVENT;
    case WebInputEvent::Type::kTouchCancel:
      return ChromeLatencyInfo2::InputType::TOUCH_CANCEL_EVENT;
    case WebInputEvent::Type::kTouchScrollStarted:
      return ChromeLatencyInfo2::InputType::TOUCH_SCROLL_STARTED_EVENT;
    case WebInputEvent::Type::kPointerDown:
      return ChromeLatencyInfo2::InputType::POINTER_DOWN_EVENT;
    case WebInputEvent::Type::kPointerUp:
      return ChromeLatencyInfo2::InputType::POINTER_UP_EVENT;
    case WebInputEvent::Type::kPointerMove:
      return ChromeLatencyInfo2::InputType::POINTER_MOVE_EVENT;
    case WebInputEvent::Type::kPointerRawUpdate:
      return ChromeLatencyInfo2::InputType::POINTER_RAW_UPDATE_EVENT;
    case WebInputEvent::Type::kPointerCancel:
      return ChromeLatencyInfo2::InputType::POINTER_CANCEL_EVENT;
    case WebInputEvent::Type::kPointerCausedUaAction:
      return ChromeLatencyInfo2::InputType::POINTER_CAUSED_UA_ACTION_EVENT;
  }
}

ChromeLatencyInfo2::InputResultState InputEventResultStateToProto(
    InputEventResultState result_state) {
  switch (result_state) {
    case InputEventResultState::kUnknown:
      return ChromeLatencyInfo2::InputResultState::UNKNOWN;
    case InputEventResultState::kConsumed:
      return ChromeLatencyInfo2::InputResultState::CONSUMED;
    case InputEventResultState::kNotConsumed:
      return ChromeLatencyInfo2::InputResultState::NOT_CONSUMED;
    case InputEventResultState::kNoConsumerExists:
      return ChromeLatencyInfo2::InputResultState::NO_CONSUMER_EXISTS;
    case InputEventResultState::kIgnored:
      return ChromeLatencyInfo2::InputResultState::IGNORED;
    case InputEventResultState::kSetNonBlocking:
      return ChromeLatencyInfo2::InputResultState::SET_NON_BLOCKING;
    case InputEventResultState::kSetNonBlockingDueToFling:
      return ChromeLatencyInfo2::InputResultState::
          SET_NON_BLOCKING_DUE_TO_FLING;
  }
}

}  // namespace input

#if BUILDFLAG(IS_ANDROID)
DEFINE_JNI(InputUtils)
#endif