EventSendingController.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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. #include "config.h"
  26. #include "EventSendingController.h"
  27. #include "InjectedBundle.h"
  28. #include "InjectedBundlePage.h"
  29. #include "JSEventSendingController.h"
  30. #include "StringFunctions.h"
  31. #include <WebKit2/WKBundle.h>
  32. #include <WebKit2/WKBundleFrame.h>
  33. #include <WebKit2/WKBundlePagePrivate.h>
  34. #include <WebKit2/WKBundlePrivate.h>
  35. #include <WebKit2/WKMutableDictionary.h>
  36. #include <WebKit2/WKNumber.h>
  37. namespace WTR {
  38. static const float ZoomMultiplierRatio = 1.2f;
  39. static WKEventModifiers parseModifier(JSStringRef modifier)
  40. {
  41. if (JSStringIsEqualToUTF8CString(modifier, "ctrlKey"))
  42. return kWKEventModifiersControlKey;
  43. if (JSStringIsEqualToUTF8CString(modifier, "shiftKey") || JSStringIsEqualToUTF8CString(modifier, "rangeSelectionKey"))
  44. return kWKEventModifiersShiftKey;
  45. if (JSStringIsEqualToUTF8CString(modifier, "altKey"))
  46. return kWKEventModifiersAltKey;
  47. if (JSStringIsEqualToUTF8CString(modifier, "metaKey"))
  48. return kWKEventModifiersMetaKey;
  49. if (JSStringIsEqualToUTF8CString(modifier, "addSelectionKey")) {
  50. #if OS(MAC_OS_X)
  51. return kWKEventModifiersMetaKey;
  52. #else
  53. return kWKEventModifiersControlKey;
  54. #endif
  55. }
  56. return 0;
  57. }
  58. static unsigned arrayLength(JSContextRef context, JSObjectRef array)
  59. {
  60. JSRetainPtr<JSStringRef> lengthString(Adopt, JSStringCreateWithUTF8CString("length"));
  61. JSValueRef lengthValue = JSObjectGetProperty(context, array, lengthString.get(), 0);
  62. if (!lengthValue)
  63. return 0;
  64. return static_cast<unsigned>(JSValueToNumber(context, lengthValue, 0));
  65. }
  66. static WKEventModifiers parseModifierArray(JSContextRef context, JSValueRef arrayValue)
  67. {
  68. if (!arrayValue)
  69. return 0;
  70. // The value may either be a string with a single modifier or an array of modifiers.
  71. if (JSValueIsString(context, arrayValue)) {
  72. JSRetainPtr<JSStringRef> string(Adopt, JSValueToStringCopy(context, arrayValue, 0));
  73. return parseModifier(string.get());
  74. }
  75. if (!JSValueIsObject(context, arrayValue))
  76. return 0;
  77. JSObjectRef array = const_cast<JSObjectRef>(arrayValue);
  78. unsigned length = arrayLength(context, array);
  79. WKEventModifiers modifiers = 0;
  80. for (unsigned i = 0; i < length; i++) {
  81. JSValueRef exception = 0;
  82. JSValueRef value = JSObjectGetPropertyAtIndex(context, array, i, &exception);
  83. if (exception)
  84. continue;
  85. JSRetainPtr<JSStringRef> string(Adopt, JSValueToStringCopy(context, value, &exception));
  86. if (exception)
  87. continue;
  88. modifiers |= parseModifier(string.get());
  89. }
  90. return modifiers;
  91. }
  92. PassRefPtr<EventSendingController> EventSendingController::create()
  93. {
  94. return adoptRef(new EventSendingController);
  95. }
  96. EventSendingController::EventSendingController()
  97. #ifdef USE_WEBPROCESS_EVENT_SIMULATION
  98. : m_time(0)
  99. , m_position()
  100. , m_clickCount(0)
  101. , m_clickTime(0)
  102. , m_clickPosition()
  103. , m_clickButton(kWKEventMouseButtonNoButton)
  104. #endif
  105. {
  106. }
  107. EventSendingController::~EventSendingController()
  108. {
  109. }
  110. JSClassRef EventSendingController::wrapperClass()
  111. {
  112. return JSEventSendingController::eventSendingControllerClass();
  113. }
  114. enum MouseState {
  115. MouseUp,
  116. MouseDown
  117. };
  118. static WKMutableDictionaryRef createMouseMessageBody(MouseState state, int button, WKEventModifiers modifiers)
  119. {
  120. WKMutableDictionaryRef EventSenderMessageBody = WKMutableDictionaryCreate();
  121. WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
  122. WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString(state == MouseUp ? "MouseUp" : "MouseDown"));
  123. WKDictionaryAddItem(EventSenderMessageBody, subMessageKey.get(), subMessageName.get());
  124. WKRetainPtr<WKStringRef> buttonKey = adoptWK(WKStringCreateWithUTF8CString("Button"));
  125. WKRetainPtr<WKUInt64Ref> buttonRef = adoptWK(WKUInt64Create(button));
  126. WKDictionaryAddItem(EventSenderMessageBody, buttonKey.get(), buttonRef.get());
  127. WKRetainPtr<WKStringRef> modifiersKey = adoptWK(WKStringCreateWithUTF8CString("Modifiers"));
  128. WKRetainPtr<WKUInt64Ref> modifiersRef = adoptWK(WKUInt64Create(modifiers));
  129. WKDictionaryAddItem(EventSenderMessageBody, modifiersKey.get(), modifiersRef.get());
  130. return EventSenderMessageBody;
  131. }
  132. void EventSendingController::mouseDown(int button, JSValueRef modifierArray)
  133. {
  134. WKBundlePageRef page = InjectedBundle::shared().page()->page();
  135. WKBundleFrameRef frame = WKBundlePageGetMainFrame(page);
  136. JSContextRef context = WKBundleFrameGetJavaScriptContext(frame);
  137. WKEventModifiers modifiers = parseModifierArray(context, modifierArray);
  138. #ifdef USE_WEBPROCESS_EVENT_SIMULATION
  139. updateClickCount(button);
  140. WKBundlePageSimulateMouseDown(page, button, m_position, m_clickCount, modifiers, m_time);
  141. #else
  142. WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
  143. WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, createMouseMessageBody(MouseDown, button, modifiers));
  144. WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
  145. #endif
  146. }
  147. void EventSendingController::mouseUp(int button, JSValueRef modifierArray)
  148. {
  149. WKBundlePageRef page = InjectedBundle::shared().page()->page();
  150. WKBundleFrameRef frame = WKBundlePageGetMainFrame(page);
  151. JSContextRef context = WKBundleFrameGetJavaScriptContext(frame);
  152. WKEventModifiers modifiers = parseModifierArray(context, modifierArray);
  153. #ifdef USE_WEBPROCESS_EVENT_SIMULATION
  154. updateClickCount(button);
  155. WKBundlePageSimulateMouseUp(page, button, m_position, m_clickCount, modifiers, m_time);
  156. #else
  157. WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
  158. WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, createMouseMessageBody(MouseUp, button, modifiers));
  159. WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
  160. #endif
  161. }
  162. void EventSendingController::mouseMoveTo(int x, int y)
  163. {
  164. #ifdef USE_WEBPROCESS_EVENT_SIMULATION
  165. m_position.x = x;
  166. m_position.y = y;
  167. WKBundlePageSimulateMouseMotion(InjectedBundle::shared().page()->page(), m_position, m_time);
  168. #else
  169. WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
  170. WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
  171. WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
  172. WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("MouseMoveTo"));
  173. WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
  174. WKRetainPtr<WKStringRef> xKey(AdoptWK, WKStringCreateWithUTF8CString("X"));
  175. WKRetainPtr<WKDoubleRef> xRef(AdoptWK, WKDoubleCreate(x));
  176. WKDictionaryAddItem(EventSenderMessageBody.get(), xKey.get(), xRef.get());
  177. WKRetainPtr<WKStringRef> yKey(AdoptWK, WKStringCreateWithUTF8CString("Y"));
  178. WKRetainPtr<WKDoubleRef> yRef(AdoptWK, WKDoubleCreate(y));
  179. WKDictionaryAddItem(EventSenderMessageBody.get(), yKey.get(), yRef.get());
  180. WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
  181. #endif
  182. }
  183. void EventSendingController::leapForward(int milliseconds)
  184. {
  185. #ifdef USE_WEBPROCESS_EVENT_SIMULATION
  186. m_time += milliseconds / 1000.0;
  187. #else
  188. WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
  189. WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
  190. WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
  191. WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("LeapForward"));
  192. WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
  193. WKRetainPtr<WKStringRef> timeKey(AdoptWK, WKStringCreateWithUTF8CString("TimeInMilliseconds"));
  194. WKRetainPtr<WKUInt64Ref> timeRef(AdoptWK, WKUInt64Create(milliseconds));
  195. WKDictionaryAddItem(EventSenderMessageBody.get(), timeKey.get(), timeRef.get());
  196. WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
  197. #endif
  198. }
  199. void EventSendingController::scheduleAsynchronousClick()
  200. {
  201. WKEventModifiers modifiers = 0;
  202. int button = 0;
  203. WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
  204. // Asynchronous mouse down.
  205. WKRetainPtr<WKMutableDictionaryRef> mouseDownMessageBody(AdoptWK, createMouseMessageBody(MouseDown, button, modifiers));
  206. WKBundlePostMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), mouseDownMessageBody.get());
  207. // Asynchronous mouse up.
  208. WKRetainPtr<WKMutableDictionaryRef> mouseUpMessageBody(AdoptWK, createMouseMessageBody(MouseUp, button, modifiers));
  209. WKBundlePostMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), mouseUpMessageBody.get());
  210. }
  211. static WKRetainPtr<WKMutableDictionaryRef> createKeyDownMessageBody(JSStringRef key, WKEventModifiers modifiers, int location)
  212. {
  213. WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
  214. WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
  215. WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("KeyDown"));
  216. WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
  217. WKRetainPtr<WKStringRef> keyKey(AdoptWK, WKStringCreateWithUTF8CString("Key"));
  218. WKDictionaryAddItem(EventSenderMessageBody.get(), keyKey.get(), toWK(key).get());
  219. WKRetainPtr<WKStringRef> modifiersKey(AdoptWK, WKStringCreateWithUTF8CString("Modifiers"));
  220. WKRetainPtr<WKUInt64Ref> modifiersRef(AdoptWK, WKUInt64Create(modifiers));
  221. WKDictionaryAddItem(EventSenderMessageBody.get(), modifiersKey.get(), modifiersRef.get());
  222. WKRetainPtr<WKStringRef> locationKey(AdoptWK, WKStringCreateWithUTF8CString("Location"));
  223. WKRetainPtr<WKUInt64Ref> locationRef(AdoptWK, WKUInt64Create(location));
  224. WKDictionaryAddItem(EventSenderMessageBody.get(), locationKey.get(), locationRef.get());
  225. return EventSenderMessageBody;
  226. }
  227. void EventSendingController::keyDown(JSStringRef key, JSValueRef modifierArray, int location)
  228. {
  229. WKBundlePageRef page = InjectedBundle::shared().page()->page();
  230. WKBundleFrameRef frame = WKBundlePageGetMainFrame(page);
  231. JSContextRef context = WKBundleFrameGetJavaScriptContext(frame);
  232. WKEventModifiers modifiers = parseModifierArray(context, modifierArray);
  233. WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
  234. WKRetainPtr<WKMutableDictionaryRef> keyDownMessageBody = createKeyDownMessageBody(key, modifiers, location);
  235. WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), keyDownMessageBody.get(), 0);
  236. }
  237. void EventSendingController::scheduleAsynchronousKeyDown(JSStringRef key)
  238. {
  239. WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
  240. WKRetainPtr<WKMutableDictionaryRef> keyDownMessageBody = createKeyDownMessageBody(key, 0 /* modifiers */, 0 /* location */);
  241. WKBundlePostMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), keyDownMessageBody.get());
  242. }
  243. void EventSendingController::mouseScrollBy(int x, int y)
  244. {
  245. WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
  246. WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
  247. WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
  248. WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("MouseScrollBy"));
  249. WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
  250. WKRetainPtr<WKStringRef> xKey(AdoptWK, WKStringCreateWithUTF8CString("X"));
  251. WKRetainPtr<WKDoubleRef> xRef(AdoptWK, WKDoubleCreate(x));
  252. WKDictionaryAddItem(EventSenderMessageBody.get(), xKey.get(), xRef.get());
  253. WKRetainPtr<WKStringRef> yKey(AdoptWK, WKStringCreateWithUTF8CString("Y"));
  254. WKRetainPtr<WKDoubleRef> yRef(AdoptWK, WKDoubleCreate(y));
  255. WKDictionaryAddItem(EventSenderMessageBody.get(), yKey.get(), yRef.get());
  256. WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
  257. }
  258. void EventSendingController::continuousMouseScrollBy(int x, int y, bool paged)
  259. {
  260. WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
  261. WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
  262. WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
  263. WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("ContinuousMouseScrollBy"));
  264. WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
  265. WKRetainPtr<WKStringRef> xKey(AdoptWK, WKStringCreateWithUTF8CString("X"));
  266. WKRetainPtr<WKDoubleRef> xRef(AdoptWK, WKDoubleCreate(x));
  267. WKDictionaryAddItem(EventSenderMessageBody.get(), xKey.get(), xRef.get());
  268. WKRetainPtr<WKStringRef> yKey(AdoptWK, WKStringCreateWithUTF8CString("Y"));
  269. WKRetainPtr<WKDoubleRef> yRef(AdoptWK, WKDoubleCreate(y));
  270. WKDictionaryAddItem(EventSenderMessageBody.get(), yKey.get(), yRef.get());
  271. WKRetainPtr<WKStringRef> pagedKey(AdoptWK, WKStringCreateWithUTF8CString("Paged"));
  272. WKRetainPtr<WKUInt64Ref> pagedRef(AdoptWK, WKUInt64Create(paged));
  273. WKDictionaryAddItem(EventSenderMessageBody.get(), pagedKey.get(), pagedRef.get());
  274. WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
  275. }
  276. JSValueRef EventSendingController::contextClick()
  277. {
  278. WKBundlePageRef page = InjectedBundle::shared().page()->page();
  279. WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(page);
  280. JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame);
  281. #if ENABLE(CONTEXT_MENUS)
  282. // Do mouse context click.
  283. mouseDown(2, 0);
  284. mouseUp(2, 0);
  285. WKRetainPtr<WKArrayRef> entriesNames = adoptWK(WKBundlePageCopyContextMenuItemTitles(page));
  286. JSRetainPtr<JSStringRef> jsPropertyName(Adopt, JSStringCreateWithUTF8CString("title"));
  287. size_t entriesSize = WKArrayGetSize(entriesNames.get());
  288. OwnArrayPtr<JSValueRef> jsValuesArray = adoptArrayPtr(new JSValueRef[entriesSize]);
  289. for (size_t i = 0; i < entriesSize; ++i) {
  290. ASSERT(WKGetTypeID(WKArrayGetItemAtIndex(entriesNames.get(), i)) == WKStringGetTypeID());
  291. WKStringRef wkEntryName = static_cast<WKStringRef>(WKArrayGetItemAtIndex(entriesNames.get(), i));
  292. JSObjectRef jsItemObject = JSObjectMake(context, /* JSClassRef */0, /* privData */0);
  293. JSRetainPtr<JSStringRef> jsNameCopy(Adopt, WKStringCopyJSString(wkEntryName));
  294. JSValueRef jsEntryName = JSValueMakeString(context, jsNameCopy.get());
  295. JSObjectSetProperty(context, jsItemObject, jsPropertyName.get(), jsEntryName, kJSPropertyAttributeReadOnly, 0);
  296. jsValuesArray[i] = jsItemObject;
  297. }
  298. return JSObjectMakeArray(context, entriesSize, jsValuesArray.get(), 0);
  299. #else
  300. return JSValueMakeUndefined(context);
  301. #endif
  302. }
  303. #ifdef USE_WEBPROCESS_EVENT_SIMULATION
  304. void EventSendingController::updateClickCount(WKEventMouseButton button)
  305. {
  306. if (m_time - m_clickTime < 1 && m_position == m_clickPosition && button == m_clickButton) {
  307. ++m_clickCount;
  308. m_clickTime = m_time;
  309. return;
  310. }
  311. m_clickCount = 1;
  312. m_clickTime = m_time;
  313. m_clickPosition = m_position;
  314. m_clickButton = button;
  315. }
  316. #endif
  317. void EventSendingController::textZoomIn()
  318. {
  319. // Ensure page zoom is reset.
  320. WKBundlePageSetPageZoomFactor(InjectedBundle::shared().page()->page(), 1);
  321. double zoomFactor = WKBundlePageGetTextZoomFactor(InjectedBundle::shared().page()->page());
  322. WKBundlePageSetTextZoomFactor(InjectedBundle::shared().page()->page(), zoomFactor * ZoomMultiplierRatio);
  323. }
  324. void EventSendingController::textZoomOut()
  325. {
  326. // Ensure page zoom is reset.
  327. WKBundlePageSetPageZoomFactor(InjectedBundle::shared().page()->page(), 1);
  328. double zoomFactor = WKBundlePageGetTextZoomFactor(InjectedBundle::shared().page()->page());
  329. WKBundlePageSetTextZoomFactor(InjectedBundle::shared().page()->page(), zoomFactor / ZoomMultiplierRatio);
  330. }
  331. void EventSendingController::zoomPageIn()
  332. {
  333. // Ensure text zoom is reset.
  334. WKBundlePageSetTextZoomFactor(InjectedBundle::shared().page()->page(), 1);
  335. double zoomFactor = WKBundlePageGetPageZoomFactor(InjectedBundle::shared().page()->page());
  336. WKBundlePageSetPageZoomFactor(InjectedBundle::shared().page()->page(), zoomFactor * ZoomMultiplierRatio);
  337. }
  338. void EventSendingController::zoomPageOut()
  339. {
  340. // Ensure text zoom is reset.
  341. WKBundlePageSetTextZoomFactor(InjectedBundle::shared().page()->page(), 1);
  342. double zoomFactor = WKBundlePageGetPageZoomFactor(InjectedBundle::shared().page()->page());
  343. WKBundlePageSetPageZoomFactor(InjectedBundle::shared().page()->page(), zoomFactor / ZoomMultiplierRatio);
  344. }
  345. void EventSendingController::scalePageBy(double scale, double x, double y)
  346. {
  347. WKPoint origin = { x, y };
  348. WKBundlePageSetScaleAtOrigin(InjectedBundle::shared().page()->page(), scale, origin);
  349. }
  350. #if ENABLE(TOUCH_EVENTS)
  351. void EventSendingController::addTouchPoint(int x, int y)
  352. {
  353. WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
  354. WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
  355. WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
  356. WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("AddTouchPoint"));
  357. WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
  358. WKRetainPtr<WKStringRef> xKey(AdoptWK, WKStringCreateWithUTF8CString("X"));
  359. WKRetainPtr<WKUInt64Ref> xRef(AdoptWK, WKUInt64Create(x));
  360. WKDictionaryAddItem(EventSenderMessageBody.get(), xKey.get(), xRef.get());
  361. WKRetainPtr<WKStringRef> yKey(AdoptWK, WKStringCreateWithUTF8CString("Y"));
  362. WKRetainPtr<WKUInt64Ref> yRef(AdoptWK, WKUInt64Create(y));
  363. WKDictionaryAddItem(EventSenderMessageBody.get(), yKey.get(), yRef.get());
  364. WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
  365. }
  366. void EventSendingController::updateTouchPoint(int index, int x, int y)
  367. {
  368. WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
  369. WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
  370. WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
  371. WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("UpdateTouchPoint"));
  372. WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
  373. WKRetainPtr<WKStringRef> indexKey(AdoptWK, WKStringCreateWithUTF8CString("Index"));
  374. WKRetainPtr<WKUInt64Ref> indexRef(AdoptWK, WKUInt64Create(index));
  375. WKDictionaryAddItem(EventSenderMessageBody.get(), indexKey.get(), indexRef.get());
  376. WKRetainPtr<WKStringRef> xKey(AdoptWK, WKStringCreateWithUTF8CString("X"));
  377. WKRetainPtr<WKUInt64Ref> xRef(AdoptWK, WKUInt64Create(x));
  378. WKDictionaryAddItem(EventSenderMessageBody.get(), xKey.get(), xRef.get());
  379. WKRetainPtr<WKStringRef> yKey(AdoptWK, WKStringCreateWithUTF8CString("Y"));
  380. WKRetainPtr<WKUInt64Ref> yRef(AdoptWK, WKUInt64Create(y));
  381. WKDictionaryAddItem(EventSenderMessageBody.get(), yKey.get(), yRef.get());
  382. WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
  383. }
  384. void EventSendingController::setTouchModifier(const JSStringRef &modifier, bool enable)
  385. {
  386. WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
  387. WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
  388. WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
  389. WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("SetTouchModifier"));
  390. WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
  391. WKEventModifiers mod = 0;
  392. if (JSStringIsEqualToUTF8CString(modifier, "ctrl"))
  393. mod = kWKEventModifiersControlKey;
  394. if (JSStringIsEqualToUTF8CString(modifier, "shift"))
  395. mod = kWKEventModifiersShiftKey;
  396. if (JSStringIsEqualToUTF8CString(modifier, "alt"))
  397. mod = kWKEventModifiersAltKey;
  398. if (JSStringIsEqualToUTF8CString(modifier, "meta"))
  399. mod = kWKEventModifiersMetaKey;
  400. WKRetainPtr<WKStringRef> modifierKey(AdoptWK, WKStringCreateWithUTF8CString("Modifier"));
  401. WKRetainPtr<WKUInt64Ref> modifierRef(AdoptWK, WKUInt64Create(mod));
  402. WKDictionaryAddItem(EventSenderMessageBody.get(), modifierKey.get(), modifierRef.get());
  403. WKRetainPtr<WKStringRef> enableKey(AdoptWK, WKStringCreateWithUTF8CString("Enable"));
  404. WKRetainPtr<WKUInt64Ref> enableRef(AdoptWK, WKUInt64Create(enable));
  405. WKDictionaryAddItem(EventSenderMessageBody.get(), enableKey.get(), enableRef.get());
  406. WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
  407. }
  408. void EventSendingController::setTouchPointRadius(int radiusX, int radiusY)
  409. {
  410. WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
  411. WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
  412. WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
  413. WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("SetTouchPointRadius"));
  414. WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
  415. WKRetainPtr<WKStringRef> xKey(AdoptWK, WKStringCreateWithUTF8CString("RadiusX"));
  416. WKRetainPtr<WKUInt64Ref> xRef(AdoptWK, WKUInt64Create(radiusX));
  417. WKDictionaryAddItem(EventSenderMessageBody.get(), xKey.get(), xRef.get());
  418. WKRetainPtr<WKStringRef> yKey(AdoptWK, WKStringCreateWithUTF8CString("RadiusY"));
  419. WKRetainPtr<WKUInt64Ref> yRef(AdoptWK, WKUInt64Create(radiusY));
  420. WKDictionaryAddItem(EventSenderMessageBody.get(), yKey.get(), yRef.get());
  421. WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
  422. }
  423. void EventSendingController::touchStart()
  424. {
  425. WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
  426. WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
  427. WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
  428. WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("TouchStart"));
  429. WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
  430. WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
  431. }
  432. void EventSendingController::touchMove()
  433. {
  434. WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
  435. WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
  436. WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
  437. WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("TouchMove"));
  438. WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
  439. WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
  440. }
  441. void EventSendingController::touchEnd()
  442. {
  443. WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
  444. WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
  445. WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
  446. WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("TouchEnd"));
  447. WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
  448. WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
  449. }
  450. void EventSendingController::touchCancel()
  451. {
  452. WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
  453. WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
  454. WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
  455. WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("TouchCancel"));
  456. WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
  457. WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
  458. }
  459. void EventSendingController::clearTouchPoints()
  460. {
  461. WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
  462. WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
  463. WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
  464. WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("ClearTouchPoints"));
  465. WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
  466. WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
  467. }
  468. void EventSendingController::releaseTouchPoint(int index)
  469. {
  470. WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
  471. WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
  472. WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
  473. WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("ReleaseTouchPoint"));
  474. WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
  475. WKRetainPtr<WKStringRef> indexKey(AdoptWK, WKStringCreateWithUTF8CString("Index"));
  476. WKRetainPtr<WKUInt64Ref> indexRef(AdoptWK, WKUInt64Create(index));
  477. WKDictionaryAddItem(EventSenderMessageBody.get(), indexKey.get(), indexRef.get());
  478. WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
  479. }
  480. void EventSendingController::cancelTouchPoint(int index)
  481. {
  482. WKRetainPtr<WKStringRef> EventSenderMessageName(AdoptWK, WKStringCreateWithUTF8CString("EventSender"));
  483. WKRetainPtr<WKMutableDictionaryRef> EventSenderMessageBody(AdoptWK, WKMutableDictionaryCreate());
  484. WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
  485. WKRetainPtr<WKStringRef> subMessageName(AdoptWK, WKStringCreateWithUTF8CString("CancelTouchPoint"));
  486. WKDictionaryAddItem(EventSenderMessageBody.get(), subMessageKey.get(), subMessageName.get());
  487. WKRetainPtr<WKStringRef> indexKey(AdoptWK, WKStringCreateWithUTF8CString("Index"));
  488. WKRetainPtr<WKUInt64Ref> indexRef(AdoptWK, WKUInt64Create(index));
  489. WKDictionaryAddItem(EventSenderMessageBody.get(), indexKey.get(), indexRef.get());
  490. WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), EventSenderMessageName.get(), EventSenderMessageBody.get(), 0);
  491. }
  492. #endif
  493. // Object Creation
  494. void EventSendingController::makeWindowObject(JSContextRef context, JSObjectRef windowObject, JSValueRef* exception)
  495. {
  496. setProperty(context, windowObject, "eventSender", this, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, exception);
  497. }
  498. } // namespace WTR