cr: Add 'gn' command
[chromium-blink-merge.git] / mojo / services / html_viewer / touch_handler.cc
blob6261dab93b7ae8cc6d265a5fdcd198f6d71e5849
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "mojo/services/html_viewer/touch_handler.h"
7 #include "third_party/WebKit/public/web/WebInputEvent.h"
8 #include "third_party/WebKit/public/web/WebView.h"
9 #include "third_party/mojo_services/src/input_events/public/interfaces/input_events.mojom.h"
10 #include "ui/events/blink/blink_event_util.h"
11 #include "ui/events/gesture_detection/gesture_provider_config_helper.h"
12 #include "ui/events/gesture_detection/motion_event_generic.h"
14 namespace html_viewer {
15 namespace {
17 void SetPropertiesFromEvent(const mojo::Event& event,
18 ui::PointerProperties* properties) {
19 properties->id = event.pointer_data->pointer_id;
20 properties->x = event.pointer_data->x;
21 properties->y = event.pointer_data->y;
22 properties->raw_x = event.pointer_data->screen_x;
23 properties->raw_y = event.pointer_data->screen_y;
24 properties->pressure = event.pointer_data->pressure;
25 properties->touch_major = event.pointer_data->radius_major;
26 properties->touch_minor = event.pointer_data->radius_minor;
27 properties->orientation = event.pointer_data->orientation;
28 // TODO(sky): Add support for tool_type.
31 } // namespace
33 TouchHandler::TouchHandler(blink::WebView* web_view)
34 : web_view_(web_view),
35 gesture_provider_(ui::GetGestureProviderConfig(
36 ui::GestureProviderConfigType::CURRENT_PLATFORM),
37 this) {
40 TouchHandler::~TouchHandler() {
43 void TouchHandler::OnTouchEvent(const mojo::Event& event) {
44 if (!UpdateMotionEvent(event))
45 return;
47 SendMotionEventToGestureProvider();
49 PostProcessMotionEvent(event);
52 void TouchHandler::OnGestureEvent(const ui::GestureEventData& gesture) {
53 blink::WebGestureEvent web_gesture =
54 CreateWebGestureEventFromGestureEventData(gesture);
55 // TODO(jdduke): Remove this workaround after Android fixes UiAutomator to
56 // stop providing shift meta values to synthetic MotionEvents. This prevents
57 // unintended shift+click interpretation of all accessibility clicks.
58 // See crbug.com/443247.
59 if (web_gesture.type == blink::WebInputEvent::GestureTap &&
60 web_gesture.modifiers == blink::WebInputEvent::ShiftKey) {
61 web_gesture.modifiers = 0;
63 web_view_->handleInputEvent(web_gesture);
66 bool TouchHandler::UpdateMotionEvent(const mojo::Event& event) {
67 ui::PointerProperties properties;
68 SetPropertiesFromEvent(event, &properties);
70 const base::TimeTicks timestamp(
71 base::TimeTicks::FromInternalValue(event.time_stamp));
72 if (current_motion_event_.get()) {
73 current_motion_event_->set_id(current_motion_event_->GetId() + 1);
74 current_motion_event_->set_event_time(timestamp);
77 switch (event.action) {
78 case mojo::EVENT_TYPE_POINTER_DOWN:
79 if (!current_motion_event_.get()) {
80 current_motion_event_.reset(new ui::MotionEventGeneric(
81 ui::MotionEvent::ACTION_DOWN, timestamp, properties));
82 } else {
83 const int index =
84 current_motion_event_->FindPointerIndexOfId(properties.id);
85 if (index != -1) {
86 DVLOG(1) << "pointer down and pointer already down id="
87 << properties.id;
88 return false;
90 current_motion_event_->PushPointer(properties);
91 current_motion_event_->set_action(ui::MotionEvent::ACTION_POINTER_DOWN);
92 current_motion_event_->set_action_index(static_cast<int>(index));
94 return true;
96 case mojo::EVENT_TYPE_POINTER_UP: {
97 if (!current_motion_event_.get()) {
98 DVLOG(1) << "pointer up with no event, id=" << properties.id;
99 return false;
101 const int index =
102 current_motion_event_->FindPointerIndexOfId(properties.id);
103 if (index == -1) {
104 DVLOG(1) << "pointer up and pointer not down id=" << properties.id;
105 return false;
107 current_motion_event_->pointer(index) = properties;
108 current_motion_event_->set_action(
109 current_motion_event_->GetPointerCount() == 1
110 ? ui::MotionEvent::ACTION_UP
111 : ui::MotionEvent::ACTION_POINTER_UP);
112 current_motion_event_->set_action_index(static_cast<int>(index));
113 return true;
116 case mojo::EVENT_TYPE_POINTER_MOVE: {
117 if (!current_motion_event_.get()) {
118 DVLOG(1) << "pointer move with no event, id=" << properties.id;
119 return false;
121 const int index =
122 current_motion_event_->FindPointerIndexOfId(properties.id);
123 if (index == -1) {
124 DVLOG(1) << "pointer move and pointer not down id=" << properties.id;
125 return false;
127 current_motion_event_->pointer(index) = properties;
128 current_motion_event_->set_action(ui::MotionEvent::ACTION_MOVE);
129 current_motion_event_->set_action_index(static_cast<int>(index));
130 return true;
133 case mojo::EVENT_TYPE_POINTER_CANCEL: {
134 if (!current_motion_event_.get()) {
135 DVLOG(1) << "canel with no event, id=" << properties.id;
136 return false;
138 const int index =
139 current_motion_event_->FindPointerIndexOfId(properties.id);
140 if (index == -1) {
141 DVLOG(1) << "cancel and pointer not down id=" << properties.id;
142 return false;
144 current_motion_event_->pointer(index) = properties;
145 current_motion_event_->set_action(ui::MotionEvent::ACTION_CANCEL);
146 current_motion_event_->set_action_index(0);
147 return true;
150 default:
151 NOTREACHED();
153 return false;
156 void TouchHandler::SendMotionEventToGestureProvider() {
157 ui::FilteredGestureProvider::TouchHandlingResult result =
158 gesture_provider_.OnTouchEvent(*current_motion_event_);
159 if (!result.succeeded)
160 return;
162 blink::WebTouchEvent web_event = ui::CreateWebTouchEventFromMotionEvent(
163 *current_motion_event_, result.did_generate_scroll);
164 gesture_provider_.OnSyncTouchEventAck(web_view_->handleInputEvent(web_event));
167 void TouchHandler::PostProcessMotionEvent(const mojo::Event& event) {
168 switch (event.action) {
169 case mojo::EVENT_TYPE_POINTER_UP: {
170 const int index = current_motion_event_->FindPointerIndexOfId(
171 event.pointer_data->pointer_id);
172 current_motion_event_->RemovePointerAt(index);
173 if (current_motion_event_->GetPointerCount() == 0)
174 current_motion_event_.reset();
175 break;
178 case mojo::EVENT_TYPE_POINTER_CANCEL:
179 current_motion_event_.reset();
180 break;
182 default:
183 break;
187 } // namespace html_viewer