Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / content / common / input / synthetic_web_input_event_builders.cc
blob36fc28f21b920bc173f6b783c7565607b171c126
1 // Copyright 2013 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 "content/common/input/synthetic_web_input_event_builders.h"
7 #include "base/logging.h"
8 #include "content/common/input/web_touch_event_traits.h"
9 #include "ui/events/keycodes/keyboard_codes.h"
11 namespace content {
13 using blink::WebInputEvent;
14 using blink::WebKeyboardEvent;
15 using blink::WebGestureEvent;
16 using blink::WebMouseEvent;
17 using blink::WebMouseWheelEvent;
18 using blink::WebTouchEvent;
19 using blink::WebTouchPoint;
21 WebMouseEvent SyntheticWebMouseEventBuilder::Build(
22 blink::WebInputEvent::Type type) {
23 WebMouseEvent result;
24 result.type = type;
25 return result;
28 WebMouseEvent SyntheticWebMouseEventBuilder::Build(
29 blink::WebInputEvent::Type type,
30 int window_x,
31 int window_y,
32 int modifiers) {
33 DCHECK(WebInputEvent::isMouseEventType(type));
34 WebMouseEvent result = Build(type);
35 result.x = window_x;
36 result.y = window_y;
37 result.windowX = window_x;
38 result.windowY = window_y;
39 result.modifiers = modifiers;
41 if (type == WebInputEvent::MouseDown || type == WebInputEvent::MouseUp)
42 result.button = WebMouseEvent::ButtonLeft;
43 else
44 result.button = WebMouseEvent::ButtonNone;
46 return result;
49 WebMouseWheelEvent SyntheticWebMouseWheelEventBuilder::Build(
50 WebMouseWheelEvent::Phase phase) {
51 WebMouseWheelEvent result;
52 result.type = WebInputEvent::MouseWheel;
53 result.phase = phase;
54 return result;
57 WebMouseWheelEvent SyntheticWebMouseWheelEventBuilder::Build(float dx,
58 float dy,
59 int modifiers,
60 bool precise) {
61 WebMouseWheelEvent result;
62 result.type = WebInputEvent::MouseWheel;
63 result.deltaX = dx;
64 result.deltaY = dy;
65 if (dx)
66 result.wheelTicksX = dx > 0.0f ? 1.0f : -1.0f;
67 if (dy)
68 result.wheelTicksY = dy > 0.0f ? 1.0f : -1.0f;
69 result.modifiers = modifiers;
70 result.hasPreciseScrollingDeltas = precise;
71 result.canScroll = true;
72 return result;
75 WebKeyboardEvent SyntheticWebKeyboardEventBuilder::Build(
76 WebInputEvent::Type type) {
77 DCHECK(WebInputEvent::isKeyboardEventType(type));
78 WebKeyboardEvent result;
79 result.type = type;
80 result.windowsKeyCode = ui::VKEY_L; // non-null made up value.
81 return result;
84 WebGestureEvent SyntheticWebGestureEventBuilder::Build(
85 WebInputEvent::Type type,
86 blink::WebGestureDevice source_device) {
87 DCHECK(WebInputEvent::isGestureEventType(type));
88 WebGestureEvent result;
89 result.type = type;
90 result.sourceDevice = source_device;
91 if (type == WebInputEvent::GestureTap ||
92 type == WebInputEvent::GestureTapUnconfirmed ||
93 type == WebInputEvent::GestureDoubleTap) {
94 result.data.tap.tapCount = 1;
95 result.data.tap.width = 10;
96 result.data.tap.height = 10;
98 return result;
101 WebGestureEvent SyntheticWebGestureEventBuilder::BuildScrollBegin(
102 float dx_hint,
103 float dy_hint) {
104 WebGestureEvent result = Build(WebInputEvent::GestureScrollBegin,
105 blink::WebGestureDeviceTouchscreen);
106 result.data.scrollBegin.deltaXHint = dx_hint;
107 result.data.scrollBegin.deltaYHint = dy_hint;
108 return result;
111 WebGestureEvent SyntheticWebGestureEventBuilder::BuildScrollUpdate(
112 float dx,
113 float dy,
114 int modifiers,
115 blink::WebGestureDevice source_device) {
116 WebGestureEvent result =
117 Build(WebInputEvent::GestureScrollUpdate, source_device);
118 result.data.scrollUpdate.deltaX = dx;
119 result.data.scrollUpdate.deltaY = dy;
120 result.modifiers = modifiers;
121 return result;
124 WebGestureEvent SyntheticWebGestureEventBuilder::BuildPinchUpdate(
125 float scale,
126 float anchor_x,
127 float anchor_y,
128 int modifiers,
129 blink::WebGestureDevice source_device) {
130 WebGestureEvent result =
131 Build(WebInputEvent::GesturePinchUpdate, source_device);
132 result.data.pinchUpdate.scale = scale;
133 result.x = anchor_x;
134 result.y = anchor_y;
135 result.globalX = anchor_x;
136 result.globalY = anchor_y;
137 result.modifiers = modifiers;
138 return result;
141 WebGestureEvent SyntheticWebGestureEventBuilder::BuildFling(
142 float velocity_x,
143 float velocity_y,
144 blink::WebGestureDevice source_device) {
145 WebGestureEvent result = Build(WebInputEvent::GestureFlingStart,
146 source_device);
147 result.data.flingStart.velocityX = velocity_x;
148 result.data.flingStart.velocityY = velocity_y;
149 return result;
152 SyntheticWebTouchEvent::SyntheticWebTouchEvent() : WebTouchEvent() {
153 SetTimestamp(base::TimeTicks::Now() - base::TimeTicks());
156 void SyntheticWebTouchEvent::ResetPoints() {
157 int point = 0;
158 for (unsigned int i = 0; i < touchesLength; ++i) {
159 if (touches[i].state == WebTouchPoint::StateReleased)
160 continue;
162 touches[point] = touches[i];
163 touches[point].state = WebTouchPoint::StateStationary;
164 ++point;
166 touchesLength = point;
167 type = WebInputEvent::Undefined;
168 causesScrollingIfUncanceled = false;
171 int SyntheticWebTouchEvent::PressPoint(float x, float y) {
172 if (touchesLength == touchesLengthCap)
173 return -1;
174 WebTouchPoint& point = touches[touchesLength];
175 point.id = touchesLength;
176 point.position.x = point.screenPosition.x = x;
177 point.position.y = point.screenPosition.y = y;
178 point.state = WebTouchPoint::StatePressed;
179 point.radiusX = point.radiusY = 1.f;
180 point.rotationAngle = 1.f;
181 point.force = 1.f;
182 ++touchesLength;
183 WebTouchEventTraits::ResetType(
184 WebInputEvent::TouchStart, timeStampSeconds, this);
185 return point.id;
188 void SyntheticWebTouchEvent::MovePoint(int index, float x, float y) {
189 CHECK_GE(index, 0);
190 CHECK_LT(index, touchesLengthCap);
191 // Always set this bit to avoid otherwise unexpected touchmove suppression.
192 // The caller can opt-out explicitly, if necessary.
193 causesScrollingIfUncanceled = true;
194 WebTouchPoint& point = touches[index];
195 point.position.x = point.screenPosition.x = x;
196 point.position.y = point.screenPosition.y = y;
197 touches[index].state = WebTouchPoint::StateMoved;
198 WebTouchEventTraits::ResetType(
199 WebInputEvent::TouchMove, timeStampSeconds, this);
202 void SyntheticWebTouchEvent::ReleasePoint(int index) {
203 CHECK_GE(index, 0);
204 CHECK_LT(index, touchesLengthCap);
205 touches[index].state = WebTouchPoint::StateReleased;
206 WebTouchEventTraits::ResetType(
207 WebInputEvent::TouchEnd, timeStampSeconds, this);
210 void SyntheticWebTouchEvent::CancelPoint(int index) {
211 CHECK_GE(index, 0);
212 CHECK_LT(index, touchesLengthCap);
213 touches[index].state = WebTouchPoint::StateCancelled;
214 WebTouchEventTraits::ResetType(
215 WebInputEvent::TouchCancel, timeStampSeconds, this);
218 void SyntheticWebTouchEvent::SetTimestamp(base::TimeDelta timestamp) {
219 timeStampSeconds = timestamp.InSecondsF();
222 } // namespace content