Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / inspector / InspectorInputAgent.cpp
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "core/inspector/InspectorInputAgent.h"
33
34 #include "core/frame/FrameView.h"
35 #include "core/frame/LocalFrame.h"
36 #include "core/inspector/InspectorClient.h"
37 #include "core/page/Chrome.h"
38 #include "core/page/EventHandler.h"
39 #include "core/page/Page.h"
40 #include "platform/JSONValues.h"
41 #include "platform/PlatformKeyboardEvent.h"
42 #include "platform/PlatformMouseEvent.h"
43 #include "platform/PlatformTouchEvent.h"
44 #include "platform/PlatformTouchPoint.h"
45 #include "platform/geometry/FloatSize.h"
46 #include "platform/geometry/IntPoint.h"
47 #include "platform/geometry/IntRect.h"
48 #include "platform/geometry/IntSize.h"
49 #include "wtf/CurrentTime.h"
50
51 namespace {
52
53 class SyntheticInspectorTouchPoint : public blink::PlatformTouchPoint {
54 public:
55     SyntheticInspectorTouchPoint(unsigned id, State state, const blink::IntPoint& screenPos, const blink::IntPoint& pos, int radiusX, int radiusY, double rotationAngle, double force)
56     {
57         m_id = id;
58         m_screenPos = screenPos;
59         m_pos = pos;
60         m_state = state;
61         m_radius = blink::FloatSize(radiusX, radiusY);
62         m_rotationAngle = rotationAngle;
63         m_force = force;
64     }
65 };
66
67 class SyntheticInspectorTouchEvent : public blink::PlatformTouchEvent {
68 public:
69     SyntheticInspectorTouchEvent(const blink::PlatformEvent::Type type, unsigned modifiers, double timestamp)
70     {
71         m_type = type;
72         m_modifiers = modifiers;
73         m_timestamp = timestamp;
74     }
75
76     void append(const blink::PlatformTouchPoint& point)
77     {
78         m_touchPoints.append(point);
79     }
80 };
81
82 void ConvertInspectorPoint(blink::Page* page, const blink::IntPoint& point, blink::IntPoint* convertedPoint, blink::IntPoint* globalPoint)
83 {
84     *convertedPoint = page->deprecatedLocalMainFrame()->view()->convertToContainingWindow(point);
85     *globalPoint = page->chrome().rootViewToScreen(blink::IntRect(point, blink::IntSize(0, 0))).location();
86 }
87
88 } // namespace
89
90 namespace blink {
91
92 InspectorInputAgent::InspectorInputAgent(Page* page, InspectorClient* client)
93     : InspectorBaseAgent<InspectorInputAgent>("Input")
94     , m_page(page), m_client(client)
95 {
96 }
97
98 InspectorInputAgent::~InspectorInputAgent()
99 {
100 }
101
102 void InspectorInputAgent::dispatchKeyEvent(ErrorString* error, const String& type, const int* modifiers, const double* timestamp, const String* text, const String* unmodifiedText, const String* keyIdentifier, const int* windowsVirtualKeyCode, const int* nativeVirtualKeyCode, const bool* autoRepeat, const bool* isKeypad, const bool* isSystemKey)
103 {
104     PlatformEvent::Type convertedType;
105     if (type == "keyDown")
106         convertedType = PlatformEvent::KeyDown;
107     else if (type == "keyUp")
108         convertedType = PlatformEvent::KeyUp;
109     else if (type == "char")
110         convertedType = PlatformEvent::Char;
111     else if (type == "rawKeyDown")
112         convertedType = PlatformEvent::RawKeyDown;
113     else {
114         *error = "Unrecognized type: " + type;
115         return;
116     }
117
118     PlatformKeyboardEvent event(
119         convertedType,
120         text ? *text : "",
121         unmodifiedText ? *unmodifiedText : "",
122         keyIdentifier ? *keyIdentifier : "",
123         windowsVirtualKeyCode ? *windowsVirtualKeyCode : 0,
124         nativeVirtualKeyCode ? *nativeVirtualKeyCode : 0,
125         asBool(autoRepeat),
126         asBool(isKeypad),
127         asBool(isSystemKey),
128         static_cast<PlatformEvent::Modifiers>(modifiers ? *modifiers : 0),
129         timestamp ? *timestamp : currentTime());
130     m_client->dispatchKeyEvent(event);
131 }
132
133 void InspectorInputAgent::dispatchMouseEvent(ErrorString* error, const String& type, int x, int y, const int* modifiers, const double* timestamp, const String* button, const int* clickCount)
134 {
135     PlatformEvent::Type convertedType;
136     if (type == "mousePressed")
137         convertedType = PlatformEvent::MousePressed;
138     else if (type == "mouseReleased")
139         convertedType = PlatformEvent::MouseReleased;
140     else if (type == "mouseMoved")
141         convertedType = PlatformEvent::MouseMoved;
142     else {
143         *error = "Unrecognized type: " + type;
144         return;
145     }
146
147     int convertedModifiers = modifiers ? *modifiers : 0;
148
149     MouseButton convertedButton = NoButton;
150     if (button) {
151         if (*button == "left")
152             convertedButton = LeftButton;
153         else if (*button == "middle")
154             convertedButton = MiddleButton;
155         else if (*button == "right")
156             convertedButton = RightButton;
157         else if (*button != "none") {
158             *error = "Unrecognized button: " + *button;
159             return;
160         }
161     }
162
163     // Some platforms may have flipped coordinate systems, but the given coordinates
164     // assume the origin is in the top-left of the window. Convert.
165     IntPoint convertedPoint, globalPoint;
166     ConvertInspectorPoint(m_page, IntPoint(x, y), &convertedPoint, &globalPoint);
167
168     PlatformMouseEvent event(
169         convertedPoint,
170         globalPoint,
171         convertedButton,
172         convertedType,
173         clickCount ? *clickCount : 0,
174         convertedModifiers & PlatformEvent::ShiftKey,
175         convertedModifiers & PlatformEvent::CtrlKey,
176         convertedModifiers & PlatformEvent::AltKey,
177         convertedModifiers & PlatformEvent::MetaKey,
178         timestamp ? *timestamp : currentTime());
179
180     m_client->dispatchMouseEvent(event);
181 }
182
183 void InspectorInputAgent::dispatchTouchEvent(ErrorString* error, const String& type, const RefPtr<JSONArray>& touchPoints, const int* modifiers, const double* timestamp)
184 {
185     PlatformEvent::Type convertedType;
186     if (type == "touchStart") {
187         convertedType = PlatformEvent::TouchStart;
188     } else if (type == "touchEnd") {
189         convertedType = PlatformEvent::TouchEnd;
190     } else if (type == "touchMove") {
191         convertedType = PlatformEvent::TouchMove;
192     } else {
193         *error = "Unrecognized type: " + type;
194         return;
195     }
196
197     unsigned convertedModifiers = modifiers ? *modifiers : 0;
198
199     SyntheticInspectorTouchEvent event(convertedType, convertedModifiers, timestamp ? *timestamp : currentTime());
200
201     int autoId = 0;
202     JSONArrayBase::iterator iter;
203     for (iter = touchPoints->begin(); iter != touchPoints->end(); ++iter) {
204         RefPtr<JSONObject> pointObj;
205         String state;
206         int x, y, radiusX, radiusY, id;
207         double rotationAngle, force;
208         (*iter)->asObject(&pointObj);
209         if (!pointObj->getString("state", &state)) {
210             *error = "TouchPoint missing 'state'";
211             return;
212         }
213         if (!pointObj->getNumber("x", &x)) {
214             *error = "TouchPoint missing 'x' coordinate";
215             return;
216         }
217         if (!pointObj->getNumber("y", &y)) {
218             *error = "TouchPoint missing 'y' coordinate";
219             return;
220         }
221         if (!pointObj->getNumber("radiusX", &radiusX))
222             radiusX = 1;
223         if (!pointObj->getNumber("radiusY", &radiusY))
224             radiusY = 1;
225         if (!pointObj->getNumber("rotationAngle", &rotationAngle))
226             rotationAngle = 0.0f;
227         if (!pointObj->getNumber("force", &force))
228             force = 1.0f;
229         if (pointObj->getNumber("id", &id)) {
230             if (autoId > 0)
231                 id = -1;
232             autoId = -1;
233         } else {
234             id = autoId++;
235         }
236         if (id < 0) {
237             *error = "All or none of the provided TouchPoints must supply positive integer ids.";
238             return;
239         }
240
241         PlatformTouchPoint::State convertedState;
242         if (state == "touchPressed") {
243             convertedState = PlatformTouchPoint::TouchPressed;
244         } else if (state == "touchReleased") {
245             convertedState = PlatformTouchPoint::TouchReleased;
246         } else if (state == "touchMoved") {
247             convertedState = PlatformTouchPoint::TouchMoved;
248         } else if (state == "touchStationary") {
249             convertedState = PlatformTouchPoint::TouchStationary;
250         } else if (state == "touchCancelled") {
251             convertedState = PlatformTouchPoint::TouchCancelled;
252         } else {
253             *error = "Unrecognized state: " + state;
254             return;
255         }
256
257         // Some platforms may have flipped coordinate systems, but the given coordinates
258         // assume the origin is in the top-left of the window. Convert.
259         IntPoint convertedPoint, globalPoint;
260         ConvertInspectorPoint(m_page, IntPoint(x, y), &convertedPoint, &globalPoint);
261
262         SyntheticInspectorTouchPoint point(id++, convertedState, globalPoint, convertedPoint, radiusX, radiusY, rotationAngle, force);
263         event.append(point);
264     }
265
266     m_page->deprecatedLocalMainFrame()->eventHandler().handleTouchEvent(event);
267 }
268
269 void InspectorInputAgent::trace(Visitor* visitor)
270 {
271     visitor->trace(m_page);
272     InspectorBaseAgent::trace(visitor);
273 }
274
275 } // namespace blink
276