File: android_input_helper.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (190 lines) | stat: -rw-r--r-- 7,186 bytes parent folder | download | duplicates (5)
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
183
184
185
186
187
188
189
190
// 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/android_input_helper.h"

#include "base/metrics/histogram_macros.h"
#include "components/input/events_helper.h"
#include "components/input/render_input_router.h"
#include "components/input/render_widget_host_input_event_router.h"
#include "components/input/switches.h"
#include "ui/events/blink/blink_event_util.h"
#include "ui/events/blink/web_input_event_traits.h"
#include "ui/events/event_utils.h"

namespace input {

AndroidInputHelper::~AndroidInputHelper() = default;

AndroidInputHelper::AndroidInputHelper(RenderWidgetHostViewInput* view,
                                       Delegate* delegate)
    : view_(*view), delegate_(*delegate) {}

void AndroidInputHelper::RouteOrForwardTouchEvent(
    blink::WebTouchEvent& web_event) {
  RenderInputRouter* rir = view_->GetViewRenderInputRouter();
  CHECK(rir);
  ui::LatencyInfo latency_info;
  latency_info.AddLatencyNumber(ui::INPUT_EVENT_LATENCY_UI_COMPONENT);
  if (ShouldRouteEvents()) {
    rir->delegate()->GetInputEventRouter()->RouteTouchEvent(&*view_, &web_event,
                                                            latency_info);
  } else {
    rir->ForwardTouchEventWithLatencyInfo(web_event, latency_info);
  }
}

void AndroidInputHelper::RouteOrForwardGestureEvent(
    const blink::WebGestureEvent& event) {
  RenderInputRouter* rir = view_->GetViewRenderInputRouter();
  CHECK(rir);

  ui::LatencyInfo latency_info;
  if (ShouldRouteEvents()) {
    blink::WebGestureEvent gesture_event(event);
    rir->delegate()->GetInputEventRouter()->RouteGestureEvent(
        &*view_, &gesture_event, latency_info);
  } else {
    rir->ForwardGestureEventWithLatencyInfo(event, latency_info);
  }
}

bool AndroidInputHelper::ShouldRouteEvents() const {
  CHECK(view_->GetViewRenderInputRouter());
  return view_->GetViewRenderInputRouter()->delegate() &&
         view_->GetViewRenderInputRouter()->delegate()->GetInputEventRouter();
}

void AndroidInputHelper::ResetGestureDetection() {
  ui::FilteredGestureProvider& gesture_provider =
      delegate_->GetGestureProvider();

  const ui::MotionEvent* current_down_event =
      gesture_provider.GetCurrentDownEvent();
  if (!current_down_event) {
    // A hard reset ensures prevention of any timer-based events that might fire
    // after a touch sequence has ended.
    gesture_provider.ResetDetection();
    return;
  }

  std::unique_ptr<ui::MotionEvent> cancel_event = current_down_event->Cancel();
  if (gesture_provider.OnTouchEvent(*cancel_event).succeeded) {
    blink::WebTouchEvent web_event = ui::CreateWebTouchEventFromMotionEvent(
        *cancel_event, false /* may_cause_scrolling */, false /* hovering */);
    RouteOrForwardTouchEvent(web_event);
  }
}

bool AndroidInputHelper::RequiresDoubleTapGestureEvents() const {
  return true;
}

void AndroidInputHelper::OnGestureEvent(const ui::GestureEventData& gesture) {
  if ((gesture.type() == ui::EventType::kGesturePinchBegin ||
       gesture.type() == ui::EventType::kGesturePinchUpdate ||
       gesture.type() == ui::EventType::kGesturePinchEnd) &&
      !switches::IsPinchToZoomEnabled()) {
    return;
  }

  blink::WebGestureEvent web_gesture =
      ui::CreateWebGestureEventFromGestureEventData(gesture);
  // TODO(jdduke): Remove this workaround after Android fixes UiAutomator to
  // stop providing shift meta values to synthetic MotionEvents. This prevents
  // unintended shift+click interpretation of all accessibility clicks.
  // See crbug.com/443247.
  if (web_gesture.GetType() == blink::WebInputEvent::Type::kGestureTap &&
      web_gesture.GetModifiers() == blink::WebInputEvent::kShiftKey) {
    web_gesture.SetModifiers(blink::WebInputEvent::kNoModifiers);
  }
  delegate_->SendGestureEvent(web_gesture);
}

void AndroidInputHelper::ProcessAckedTouchEvent(
    const input::TouchEventWithLatencyInfo& touch,
    blink::mojom::InputEventResultState ack_result) {
  const bool event_consumed =
      ack_result == blink::mojom::InputEventResultState::kConsumed;
  // |is_source_touch_event_set_non_blocking| defines a blocking behaviour of
  // the future inputs.
  const bool is_source_touch_event_set_non_blocking =
      input::InputEventResultStateIsSetBlocking(ack_result);
  // |was_touch_blocked| indicates whether the current event was dispatched
  // blocking to the Renderer.
  const bool was_touch_blocked =
      ui::WebInputEventTraits::ShouldBlockEventStream(touch.event);
  delegate_->GetGestureProvider().OnTouchEventAck(
      touch.event.unique_touch_event_id, event_consumed,
      is_source_touch_event_set_non_blocking,
      was_touch_blocked
          ? std::make_optional(touch.event.GetEventLatencyMetadata())
          : std::nullopt);
  if (touch.event.touch_start_or_first_touch_move && event_consumed &&
      view_->GetViewRenderInputRouter()->delegate() &&
      view_->GetViewRenderInputRouter()->delegate()->GetInputEventRouter()) {
    view_->GetViewRenderInputRouter()
        ->delegate()
        ->GetInputEventRouter()
        ->OnHandledTouchStartOrFirstTouchMove(
            touch.event.unique_touch_event_id);
  }
}

bool AndroidInputHelper::TransformPointToCoordSpaceForView(
    const gfx::PointF& point,
    input::RenderWidgetHostViewInput* target_view,
    gfx::PointF* transformed_point) {
  if (target_view == &*view_) {
    *transformed_point = point;
    return true;
  }

  if (!view_->GetFrameSinkId().is_valid()) {
    return false;
  }

  // In TransformPointToLocalCoordSpace() there is a Point-to-Pixel
  // conversion, but it is not necessary here because the final target view
  // is responsible for converting before computing the final transform.
  return target_view->TransformPointToLocalCoordSpace(
      point, view_->GetFrameSinkId(), transformed_point);
}

void AndroidInputHelper::RecordToolTypeForActionDown(
    const ui::MotionEventAndroid& event) {
  ui::MotionEventAndroid::Action action = event.GetAction();
  if (action == ui::MotionEventAndroid::Action::DOWN ||
      action == ui::MotionEventAndroid::Action::POINTER_DOWN ||
      action == ui::MotionEventAndroid::Action::BUTTON_PRESS) {
    UMA_HISTOGRAM_ENUMERATION(
        "Event.AndroidActionDown.ToolType",
        static_cast<int>(event.GetToolType(0)),
        static_cast<int>(ui::MotionEventAndroid::ToolType::LAST) + 1);
  }
}

void AndroidInputHelper::ComputeEventLatencyOSTouchHistograms(
    const ui::MotionEvent& event,
    const base::TimeTicks& processing_time) {
  ui::EventType event_type;
  switch (event.GetAction()) {
    case ui::MotionEvent::Action::DOWN:
    case ui::MotionEvent::Action::POINTER_DOWN:
      event_type = ui::EventType::kTouchPressed;
      break;
    case ui::MotionEvent::Action::MOVE:
      event_type = ui::EventType::kTouchMoved;
      break;
    case ui::MotionEvent::Action::UP:
    case ui::MotionEvent::Action::POINTER_UP:
      event_type = ui::EventType::kTouchReleased;
      break;
    default:
      return;
  }
  ui::ComputeEventLatencyOS(event_type, event.GetEventTime(), processing_time);
}

}  // namespace input