[WK2][WKTR] EventSender needs to implement scheduleAsynchronousClick
[WebKit-https.git] / Tools / WebKitTestRunner / InjectedBundle / EventSendingController.cpp
1 /*
2  * Copyright (C) 2010, 2011 Apple 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
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "EventSendingController.h"
28
29 #include "InjectedBundle.h"
30 #include "InjectedBundlePage.h"
31 #include "JSEventSendingController.h"
32 #include "StringFunctions.h"
33 #include <WebKit2/WKBundle.h>
34 #include <WebKit2/WKBundleFrame.h>
35 #include <WebKit2/WKBundlePagePrivate.h>
36 #include <WebKit2/WKBundlePrivate.h>
37 #include <WebKit2/WKMutableDictionary.h>
38 #include <WebKit2/WKNumber.h>
39
40 namespace WTR {
41
42 static const float ZoomMultiplierRatio = 1.2f;
43
44 static WKEventModifiers parseModifier(JSStringRef modifier)
45 {
46     if (JSStringIsEqualToUTF8CString(modifier, "ctrlKey"))
47         return kWKEventModifiersControlKey;
48     if (JSStringIsEqualToUTF8CString(modifier, "shiftKey") || JSStringIsEqualToUTF8CString(modifier, "rangeSelectionKey"))
49         return kWKEventModifiersShiftKey;
50     if (JSStringIsEqualToUTF8CString(modifier, "altKey"))
51         return kWKEventModifiersAltKey;
52     if (JSStringIsEqualToUTF8CString(modifier, "metaKey"))
53         return kWKEventModifiersMetaKey;
54     if (JSStringIsEqualToUTF8CString(modifier, "addSelectionKey")) {
55 #if OS(MAC_OS_X)
56         return kWKEventModifiersMetaKey;
57 #else
58         return kWKEventModifiersControlKey;
59 #endif
60     }
61     return 0;
62 }
63
64 static unsigned arrayLength(JSContextRef context, JSObjectRef array)
65 {
66     JSRetainPtr<JSStringRef> lengthString(Adopt, JSStringCreateWithUTF8CString("length"));
67     JSValueRef lengthValue = JSObjectGetProperty(context, array, lengthString.get(), 0);
68     if (!lengthValue)
69         return 0;
70     return static_cast<unsigned>(JSValueToNumber(context, lengthValue, 0));
71 }
72
73 static WKEventModifiers parseModifierArray(JSContextRef context, JSValueRef arrayValue)
74 {
75     if (!arrayValue)
76         return 0;
77
78     // The value may either be a string with a single modifier or an array of modifiers.
79     if (JSValueIsString(context, arrayValue)) {
80         JSRetainPtr<JSStringRef> string(Adopt, JSValueToStringCopy(context, arrayValue, 0));
81         return parseModifier(string.get());
82     }
83
84     if (!JSValueIsObject(context, arrayValue))
85         return 0;
86     JSObjectRef array = const_cast<JSObjectRef>(arrayValue);
87     unsigned length = arrayLength(context, array);
88     WKEventModifiers modifiers = 0;
89     for (unsigned i = 0; i < length; i++) {
90         JSValueRef exception = 0;
91         JSValueRef value = JSObjectGetPropertyAtIndex(context, array, i, &exception);
92         if (exception)
93             continue;
94         JSRetainPtr<JSStringRef> string(Adopt, JSValueToStringCopy(context, value, &exception));
95         if (exception)
96             continue;
97         modifiers |= parseModifier(string.get());
98     }
99     return modifiers;
100 }
101
102 PassRefPtr<EventSendingController> EventSendingController::create()
103 {
104     return adoptRef(new EventSendingController);
105 }
106
107 EventSendingController::EventSendingController()
108 #ifdef USE_WEBPROCESS_EVENT_SIMULATION
109     : m_time(0)
110     , m_position()
111     , m_clickCount(0)
112     , m_clickTime(0)
113     , m_clickPosition()
114     , m_clickButton(kWKEventMouseButtonNoButton)
115 #endif
116 {
117 }
118
119 EventSendingController::~EventSendingController()
120 {
121 }
122
123 JSClassRef EventSendingController::wrapperClass()
124 {
125     return JSEventSendingController::eventSendingControllerClass();
126 }
127
128 enum MouseState {
129     MouseUp,
130     MouseDown
131 };
132
133 static WKMutableDictionaryRef createMouseMessageBody(MouseState state, int button, WKEventModifiers modifiers)
134 {
135     WKMutableDictionaryRef EventSenderMessageBody = WKMutableDictionaryCreate();
136
137     WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
138     WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString(state == MouseUp ? "MouseUp" : "MouseDown"));
139     WKDictionaryAddItem(EventSenderMessageBody, subMessageKey.get(), subMessageName.get());
140
141     WKRetainPtr<WKStringRef> buttonKey = adoptWK(WKStringCreateWithUTF8CString("Button"));
142     WKRetainPtr<WKUInt64Ref> buttonRef = adoptWK(WKUInt64Create(button));
143     WKDictionaryAddItem(EventSenderMessageBody, buttonKey.get(), buttonRef.get());
144
145     WKRetainPtr<WKStringRef> modifiersKey = adoptWK(WKStringCreateWithUTF8CString("Modifiers"));
146     WKRetainPtr<WKUInt64Ref> modifiersRef = adoptWK(WKUInt64Create(modifiers));
147     WKDictionaryAddItem(EventSenderMessageBody, modifiersKey.get(), modifiersRef.get());
148
149     return EventSenderMessageBody;
150 }
151
152 void EventSendingController::mouseDown(int button, JSValueRef modifierArray) 
153 {
154     WKBundlePageRef page = InjectedBundle::shared().page()->page();
155     WKBundleFrameRef frame = WKBundlePageGetMainFrame(page);
156     JSContextRef context = WKBundleFrameGetJavaScriptContext(frame);
157     WKEventModifiers modifiers = parseModifierArray(context, modifierArray);
158
159 #ifdef USE_WEBPROCESS_EVENT_SIMULATION
160     updateClickCount(button);
161     WKBundlePageSimulateMouseDown(page, button, m_position, m_clickCount, modifiers, m_time);
162 #else
163     WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
164     WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, createMouseMessageBody(MouseDown, button, modifiers));
165
166     WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
167 #endif
168 }
169
170 void EventSendingController::mouseUp(int button, JSValueRef modifierArray)
171 {
172     WKBundlePageRef page = InjectedBundle::shared().page()->page();
173     WKBundleFrameRef frame = WKBundlePageGetMainFrame(page);
174     JSContextRef context = WKBundleFrameGetJavaScriptContext(frame);
175     WKEventModifiers modifiers = parseModifierArray(context, modifierArray);
176
177 #ifdef USE_WEBPROCESS_EVENT_SIMULATION
178     updateClickCount(button);
179     WKBundlePageSimulateMouseUp(page, button, m_position, m_clickCount, modifiers, m_time);
180 #else
181     WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
182     WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, createMouseMessageBody(MouseUp, button, modifiers));
183
184     WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
185 #endif
186 }
187
188 void EventSendingController::mouseMoveTo(int x, int y)
189 {
190 #ifdef USE_WEBPROCESS_EVENT_SIMULATION
191     m_position.x = x;
192     m_position.y = y;
193     WKBundlePageSimulateMouseMotion(InjectedBundle::shared().page()->page(), m_position, m_time);
194 #else
195     WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
196     WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
197
198     WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
199     WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("MouseMoveTo"));
200     WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
201
202     WKRetainPtr<WKStringRef> xKey(AdoptWK, WKStringCreateWithUTF8CString("X"));
203     WKRetainPtr<WKDoubleRef> xRef(AdoptWK, WKDoubleCreate(x));
204     WKDictionaryAddItem(EventSenderMessageBody.get(), xKey.get(), xRef.get());
205
206     WKRetainPtr<WKStringRef> yKey(AdoptWK, WKStringCreateWithUTF8CString("Y"));
207     WKRetainPtr<WKDoubleRef> yRef(AdoptWK, WKDoubleCreate(y));
208     WKDictionaryAddItem(EventSenderMessageBody.get(), yKey.get(), yRef.get());
209
210     WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
211 #endif
212 }
213
214 void EventSendingController::leapForward(int milliseconds)
215 {
216 #ifdef USE_WEBPROCESS_EVENT_SIMULATION
217     m_time += milliseconds / 1000.0;
218 #else
219     WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
220     WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
221
222     WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
223     WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("LeapForward"));
224     WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
225
226     WKRetainPtr<WKStringRef> timeKey(AdoptWK, WKStringCreateWithUTF8CString("TimeInMilliseconds"));
227     WKRetainPtr<WKUInt64Ref> timeRef(AdoptWK, WKUInt64Create(milliseconds));
228     WKDictionaryAddItem(EventSenderMessageBody.get(), timeKey.get(), timeRef.get());
229
230     WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
231 #endif
232 }
233
234 void EventSendingController::scheduleAsynchronousClick()
235 {
236     WKEventModifiers modifiers = 0;
237     int button = 0;
238
239     WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
240
241     // Asynchronous mouse down.
242     WKRetainPtr<WKMutableDictionaryRef> mouseDownMessageBody(AdoptWK, createMouseMessageBody(MouseDown, button, modifiers));
243     WKBundlePostMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), mouseDownMessageBody.get());
244
245     // Asynchronous mouse up.
246     WKRetainPtr<WKMutableDictionaryRef> mouseUpMessageBody(AdoptWK, createMouseMessageBody(MouseUp, button, modifiers));
247     WKBundlePostMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), mouseUpMessageBody.get());
248 }
249
250 void EventSendingController::keyDown(JSStringRef key, JSValueRef modifierArray, int location)
251 {
252     WKBundlePageRef page = InjectedBundle::shared().page()->page();
253     WKBundleFrameRef frame = WKBundlePageGetMainFrame(page);
254     JSContextRef context = WKBundleFrameGetJavaScriptContext(frame);
255     WKEventModifiers modifiers = parseModifierArray(context, modifierArray);
256
257     WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
258     WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
259
260     WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
261     WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("KeyDown"));
262     WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
263
264     WKRetainPtr<WKStringRef> keyKey(AdoptWK, WKStringCreateWithUTF8CString("Key"));
265     WKDictionaryAddItem(EventSenderMessageBody.get(), keyKey.get(), toWK(key).get());
266
267     WKRetainPtr<WKStringRef> modifiersKey(AdoptWK, WKStringCreateWithUTF8CString("Modifiers"));
268     WKRetainPtr<WKUInt64Ref> modifiersRef(AdoptWK, WKUInt64Create(modifiers));
269     WKDictionaryAddItem(EventSenderMessageBody.get(), modifiersKey.get(), modifiersRef.get());
270
271     WKRetainPtr<WKStringRef> locationKey(AdoptWK, WKStringCreateWithUTF8CString("Location"));
272     WKRetainPtr<WKUInt64Ref> locationRef(AdoptWK, WKUInt64Create(location));
273     WKDictionaryAddItem(EventSenderMessageBody.get(), locationKey.get(), locationRef.get());
274
275     WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
276 }
277
278 void EventSendingController::mouseScrollBy(int x, int y)
279 {
280     WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
281     WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
282
283     WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
284     WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("MouseScrollBy"));
285     WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
286
287     WKRetainPtr<WKStringRef> xKey(AdoptWK, WKStringCreateWithUTF8CString("X"));
288     WKRetainPtr<WKDoubleRef> xRef(AdoptWK, WKDoubleCreate(x));
289     WKDictionaryAddItem(EventSenderMessageBody.get(), xKey.get(), xRef.get());
290
291     WKRetainPtr<WKStringRef> yKey(AdoptWK, WKStringCreateWithUTF8CString("Y"));
292     WKRetainPtr<WKDoubleRef> yRef(AdoptWK, WKDoubleCreate(y));
293     WKDictionaryAddItem(EventSenderMessageBody.get(), yKey.get(), yRef.get());
294
295     WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
296 }
297
298 void EventSendingController::continuousMouseScrollBy(int x, int y, bool paged)
299 {
300     WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
301     WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
302
303     WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
304     WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("ContinuousMouseScrollBy"));
305     WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
306
307     WKRetainPtr<WKStringRef> xKey(AdoptWK, WKStringCreateWithUTF8CString("X"));
308     WKRetainPtr<WKDoubleRef> xRef(AdoptWK, WKDoubleCreate(x));
309     WKDictionaryAddItem(EventSenderMessageBody.get(), xKey.get(), xRef.get());
310
311     WKRetainPtr<WKStringRef> yKey(AdoptWK, WKStringCreateWithUTF8CString("Y"));
312     WKRetainPtr<WKDoubleRef> yRef(AdoptWK, WKDoubleCreate(y));
313     WKDictionaryAddItem(EventSenderMessageBody.get(), yKey.get(), yRef.get());
314
315     WKRetainPtr<WKStringRef> pagedKey(AdoptWK, WKStringCreateWithUTF8CString("Paged"));
316     WKRetainPtr<WKUInt64Ref> pagedRef(AdoptWK, WKUInt64Create(paged));
317     WKDictionaryAddItem(EventSenderMessageBody.get(), pagedKey.get(), pagedRef.get());
318
319     WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
320 }
321
322 #ifdef USE_WEBPROCESS_EVENT_SIMULATION
323 void EventSendingController::updateClickCount(WKEventMouseButton button)
324 {
325     if (m_time - m_clickTime < 1 && m_position == m_clickPosition && button == m_clickButton) {
326         ++m_clickCount;
327         m_clickTime = m_time;
328         return;
329     }
330
331     m_clickCount = 1;
332     m_clickTime = m_time;
333     m_clickPosition = m_position;
334     m_clickButton = button;
335 }
336 #endif
337
338 void EventSendingController::textZoomIn()
339 {
340     // Ensure page zoom is reset.
341     WKBundlePageSetPageZoomFactor(InjectedBundle::shared().page()->page(), 1);
342
343     double zoomFactor = WKBundlePageGetTextZoomFactor(InjectedBundle::shared().page()->page());
344     WKBundlePageSetTextZoomFactor(InjectedBundle::shared().page()->page(), zoomFactor * ZoomMultiplierRatio);
345 }
346
347 void EventSendingController::textZoomOut()
348 {
349     // Ensure page zoom is reset.
350     WKBundlePageSetPageZoomFactor(InjectedBundle::shared().page()->page(), 1);
351
352     double zoomFactor = WKBundlePageGetTextZoomFactor(InjectedBundle::shared().page()->page());
353     WKBundlePageSetTextZoomFactor(InjectedBundle::shared().page()->page(), zoomFactor / ZoomMultiplierRatio);
354 }
355
356 void EventSendingController::zoomPageIn()
357 {
358     // Ensure text zoom is reset.
359     WKBundlePageSetTextZoomFactor(InjectedBundle::shared().page()->page(), 1);
360
361     double zoomFactor = WKBundlePageGetPageZoomFactor(InjectedBundle::shared().page()->page());
362     WKBundlePageSetPageZoomFactor(InjectedBundle::shared().page()->page(), zoomFactor * ZoomMultiplierRatio);
363 }
364
365 void EventSendingController::zoomPageOut()
366 {
367     // Ensure text zoom is reset.
368     WKBundlePageSetTextZoomFactor(InjectedBundle::shared().page()->page(), 1);
369
370     double zoomFactor = WKBundlePageGetPageZoomFactor(InjectedBundle::shared().page()->page());
371     WKBundlePageSetPageZoomFactor(InjectedBundle::shared().page()->page(), zoomFactor / ZoomMultiplierRatio);
372 }
373
374 void EventSendingController::scalePageBy(double scale, double x, double y)
375 {
376     WKPoint origin = { x, y };
377     WKBundlePageSetScaleAtOrigin(InjectedBundle::shared().page()->page(), scale, origin);
378 }
379
380 #if ENABLE(TOUCH_EVENTS)
381 void EventSendingController::addTouchPoint(int x, int y)
382 {
383     WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
384     WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
385
386     WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
387     WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("AddTouchPoint"));
388     WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
389
390     WKRetainPtr<WKStringRef> xKey(AdoptWK, WKStringCreateWithUTF8CString("X"));
391     WKRetainPtr<WKUInt64Ref> xRef(AdoptWK, WKUInt64Create(x));
392     WKDictionaryAddItem(EventSenderMessageBody.get(), xKey.get(), xRef.get());
393
394     WKRetainPtr<WKStringRef> yKey(AdoptWK, WKStringCreateWithUTF8CString("Y"));
395     WKRetainPtr<WKUInt64Ref> yRef(AdoptWK, WKUInt64Create(y));
396     WKDictionaryAddItem(EventSenderMessageBody.get(), yKey.get(), yRef.get());
397
398     WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
399 }
400
401 void EventSendingController::updateTouchPoint(int index, int x, int y)
402 {
403     WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
404     WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
405
406     WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
407     WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("UpdateTouchPoint"));
408     WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
409
410     WKRetainPtr<WKStringRef> indexKey(AdoptWK, WKStringCreateWithUTF8CString("Index"));
411     WKRetainPtr<WKUInt64Ref> indexRef(AdoptWK, WKUInt64Create(index));
412     WKDictionaryAddItem(EventSenderMessageBody.get(), indexKey.get(), indexRef.get());
413
414     WKRetainPtr<WKStringRef> xKey(AdoptWK, WKStringCreateWithUTF8CString("X"));
415     WKRetainPtr<WKUInt64Ref> xRef(AdoptWK, WKUInt64Create(x));
416     WKDictionaryAddItem(EventSenderMessageBody.get(), xKey.get(), xRef.get());
417
418     WKRetainPtr<WKStringRef> yKey(AdoptWK, WKStringCreateWithUTF8CString("Y"));
419     WKRetainPtr<WKUInt64Ref> yRef(AdoptWK, WKUInt64Create(y));
420     WKDictionaryAddItem(EventSenderMessageBody.get(), yKey.get(), yRef.get());
421
422     WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
423 }
424
425 void EventSendingController::setTouchModifier(const JSStringRef &modifier, bool enable)
426 {
427     WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
428     WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
429
430     WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
431     WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("SetTouchModifier"));
432     WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
433
434     WKEventModifiers mod = 0;
435     if (JSStringIsEqualToUTF8CString(modifier, "ctrl"))
436         mod = kWKEventModifiersControlKey;
437     if (JSStringIsEqualToUTF8CString(modifier, "shift"))
438         mod = kWKEventModifiersShiftKey;
439     if (JSStringIsEqualToUTF8CString(modifier, "alt"))
440         mod = kWKEventModifiersAltKey;
441     if (JSStringIsEqualToUTF8CString(modifier, "meta"))
442         mod = kWKEventModifiersMetaKey;
443
444     WKRetainPtr<WKStringRef> modifierKey(AdoptWK, WKStringCreateWithUTF8CString("Modifier"));
445     WKRetainPtr<WKUInt64Ref> modifierRef(AdoptWK, WKUInt64Create(mod));
446     WKDictionaryAddItem(EventSenderMessageBody.get(), modifierKey.get(), modifierRef.get());
447
448     WKRetainPtr<WKStringRef> enableKey(AdoptWK, WKStringCreateWithUTF8CString("Enable"));
449     WKRetainPtr<WKUInt64Ref> enableRef(AdoptWK, WKUInt64Create(enable));
450     WKDictionaryAddItem(EventSenderMessageBody.get(), enableKey.get(), enableRef.get());
451
452     WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
453 }
454
455
456 void EventSendingController::setTouchPointRadius(int radiusX, int radiusY)
457 {
458     WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
459     WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
460
461     WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
462     WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("SetTouchPointRadius"));
463     WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
464
465     WKRetainPtr<WKStringRef> xKey(AdoptWK, WKStringCreateWithUTF8CString("RadiusX"));
466     WKRetainPtr<WKUInt64Ref> xRef(AdoptWK, WKUInt64Create(radiusX));
467     WKDictionaryAddItem(EventSenderMessageBody.get(), xKey.get(), xRef.get());
468
469     WKRetainPtr<WKStringRef> yKey(AdoptWK, WKStringCreateWithUTF8CString("RadiusY"));
470     WKRetainPtr<WKUInt64Ref> yRef(AdoptWK, WKUInt64Create(radiusY));
471     WKDictionaryAddItem(EventSenderMessageBody.get(), yKey.get(), yRef.get());
472
473     WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
474 }
475
476 void EventSendingController::touchStart()
477 {
478     WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
479     WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
480
481     WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
482     WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("TouchStart"));
483     WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
484
485     WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
486 }
487
488 void EventSendingController::touchMove()
489 {
490     WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
491     WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
492
493     WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
494     WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("TouchMove"));
495     WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
496
497     WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
498 }
499
500 void EventSendingController::touchEnd()
501 {
502     WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
503     WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
504
505     WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
506     WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("TouchEnd"));
507     WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
508
509     WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
510 }
511
512 void EventSendingController::touchCancel()
513 {
514     WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
515     WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
516
517     WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
518     WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("TouchCancel"));
519     WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
520
521     WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
522 }
523
524 void EventSendingController::clearTouchPoints()
525 {
526     WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
527     WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
528
529     WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
530     WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("ClearTouchPoints"));
531     WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
532
533     WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
534 }
535
536 void EventSendingController::releaseTouchPoint(int index)
537 {
538     WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
539     WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
540
541     WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
542     WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("ReleaseTouchPoint"));
543     WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
544
545     WKRetainPtr<WKStringRef> indexKey(AdoptWK, WKStringCreateWithUTF8CString("Index"));
546     WKRetainPtr<WKUInt64Ref> indexRef(AdoptWK, WKUInt64Create(index));
547     WKDictionaryAddItem(EventSenderMessageBody.get(), indexKey.get(), indexRef.get());
548
549     WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
550 }
551
552 void EventSendingController::cancelTouchPoint(int index)
553 {
554     WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
555     WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
556
557     WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
558     WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("CancelTouchPoint"));
559     WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
560
561     WKRetainPtr<WKStringRef> indexKey(AdoptWK, WKStringCreateWithUTF8CString("Index"));
562     WKRetainPtr<WKUInt64Ref> indexRef(AdoptWK, WKUInt64Create(index));
563     WKDictionaryAddItem(EventSenderMessageBody.get(), indexKey.get(), indexRef.get());
564
565     WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
566 }
567 #endif
568
569 // Object Creation
570
571 void EventSendingController::makeWindowObject(JSContextRef context, JSObjectRef windowObject, JSValueRef* exception)
572 {
573     setProperty(context, windowObject, "eventSender", this, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, exception);
574 }
575
576 } // namespace WTR