/external/webkit/Source/WebCore/bindings/js/JSEventTarget.cpp

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk · C++ · 243 lines · 168 code · 50 blank · 25 comment · 23 complexity · e39cb21827a0199d320251a00dccc127 MD5 · raw file

  1. /*
  2. * Copyright (C) 2008 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. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "JSEventTarget.h"
  27. #include "DOMWindow.h"
  28. #include "Document.h"
  29. #include "JSDOMWindow.h"
  30. #include "JSDOMWindowShell.h"
  31. #include "JSEventListener.h"
  32. #include "JSMessagePort.h"
  33. #include "JSNode.h"
  34. #if ENABLE(SHARED_WORKERS)
  35. #include "JSSharedWorker.h"
  36. #include "JSSharedWorkerContext.h"
  37. #endif
  38. #include "JSXMLHttpRequest.h"
  39. #include "JSXMLHttpRequestUpload.h"
  40. #include "MessagePort.h"
  41. #if ENABLE(SHARED_WORKERS)
  42. #include "SharedWorker.h"
  43. #include "SharedWorkerContext.h"
  44. #endif
  45. #include "XMLHttpRequest.h"
  46. #include "XMLHttpRequestUpload.h"
  47. #if ENABLE(EVENTSOURCE)
  48. #include "EventSource.h"
  49. #include "JSEventSource.h"
  50. #endif
  51. #if ENABLE(OFFLINE_WEB_APPLICATIONS)
  52. #include "DOMApplicationCache.h"
  53. #include "JSDOMApplicationCache.h"
  54. #endif
  55. #if ENABLE(SVG)
  56. #include "SVGElementInstance.h"
  57. #include "JSSVGElementInstance.h"
  58. #endif
  59. #if ENABLE(WORKERS)
  60. #include "DedicatedWorkerContext.h"
  61. #include "JSDedicatedWorkerContext.h"
  62. #include "JSWorker.h"
  63. #include "Worker.h"
  64. #endif
  65. #if ENABLE(NOTIFICATIONS)
  66. #include "JSNotification.h"
  67. #include "Notification.h"
  68. #endif
  69. #if ENABLE(INDEXED_DATABASE)
  70. #include "IDBRequest.h"
  71. #include "JSIDBRequest.h"
  72. #endif
  73. #if ENABLE(WEB_AUDIO)
  74. #include "AudioContext.h"
  75. #include "JSAudioContext.h"
  76. #include "JSJavaScriptAudioNode.h"
  77. #include "JavaScriptAudioNode.h"
  78. #endif
  79. #if ENABLE(WEB_SOCKETS)
  80. #include "JSWebSocket.h"
  81. #include "WebSocket.h"
  82. #endif
  83. #if ENABLE(BLOB)
  84. #include "JSFileReader.h"
  85. #include "FileReader.h"
  86. #endif
  87. using namespace JSC;
  88. namespace WebCore {
  89. JSValue toJS(ExecState* exec, JSDOMGlobalObject* globalObject, EventTarget* target)
  90. {
  91. if (!target)
  92. return jsNull();
  93. #if ENABLE(EVENTSOURCE)
  94. if (EventSource* eventSource = target->toEventSource())
  95. return toJS(exec, globalObject, eventSource);
  96. #endif
  97. #if ENABLE(SVG)
  98. // SVGElementInstance supports both toSVGElementInstance and toNode since so much mouse handling code depends on toNode returning a valid node.
  99. if (SVGElementInstance* instance = target->toSVGElementInstance())
  100. return toJS(exec, globalObject, instance);
  101. #endif
  102. if (Node* node = target->toNode())
  103. return toJS(exec, globalObject, node);
  104. if (DOMWindow* domWindow = target->toDOMWindow())
  105. return toJS(exec, globalObject, domWindow);
  106. if (XMLHttpRequest* xhr = target->toXMLHttpRequest())
  107. return toJS(exec, globalObject, xhr);
  108. if (XMLHttpRequestUpload* upload = target->toXMLHttpRequestUpload())
  109. return toJS(exec, globalObject, upload);
  110. #if ENABLE(OFFLINE_WEB_APPLICATIONS)
  111. if (DOMApplicationCache* cache = target->toDOMApplicationCache())
  112. return toJS(exec, globalObject, cache);
  113. #endif
  114. if (MessagePort* messagePort = target->toMessagePort())
  115. return toJS(exec, globalObject, messagePort);
  116. #if ENABLE(WORKERS)
  117. if (Worker* worker = target->toWorker())
  118. return toJS(exec, globalObject, worker);
  119. if (DedicatedWorkerContext* workerContext = target->toDedicatedWorkerContext())
  120. return toJSDOMGlobalObject(workerContext, exec);
  121. #endif
  122. #if ENABLE(SHARED_WORKERS)
  123. if (SharedWorker* sharedWorker = target->toSharedWorker())
  124. return toJS(exec, globalObject, sharedWorker);
  125. if (SharedWorkerContext* workerContext = target->toSharedWorkerContext())
  126. return toJSDOMGlobalObject(workerContext, exec);
  127. #endif
  128. #if ENABLE(NOTIFICATIONS)
  129. if (Notification* notification = target->toNotification())
  130. return toJS(exec, notification);
  131. #endif
  132. #if ENABLE(INDEXED_DATABASE)
  133. if (IDBDatabase* idbDatabase = target->toIDBDatabase())
  134. return toJS(exec, idbDatabase);
  135. if (IDBRequest* idbRequest = target->toIDBRequest())
  136. return toJS(exec, idbRequest);
  137. if (IDBTransaction* idbTransaction = target->toIDBTransaction())
  138. return toJS(exec, idbTransaction);
  139. #endif
  140. #if ENABLE(WEB_AUDIO)
  141. if (JavaScriptAudioNode* jsAudioNode = target->toJavaScriptAudioNode())
  142. return toJS(exec, globalObject, jsAudioNode);
  143. if (AudioContext* audioContext = target->toAudioContext())
  144. return toJS(exec, globalObject, audioContext);
  145. #endif
  146. #if ENABLE(WEB_SOCKETS)
  147. if (WebSocket* webSocket = target->toWebSocket())
  148. return toJS(exec, webSocket);
  149. #endif
  150. #if ENABLE(BLOB)
  151. if (FileReader* fileReader = target->toFileReader())
  152. return toJS(exec, globalObject, fileReader);
  153. #endif
  154. ASSERT_NOT_REACHED();
  155. return jsNull();
  156. }
  157. EventTarget* toEventTarget(JSC::JSValue value)
  158. {
  159. #define CONVERT_TO_EVENT_TARGET(type) \
  160. if (value.inherits(&JS##type::s_info)) \
  161. return static_cast<JS##type*>(asObject(value))->impl();
  162. CONVERT_TO_EVENT_TARGET(Node)
  163. CONVERT_TO_EVENT_TARGET(XMLHttpRequest)
  164. CONVERT_TO_EVENT_TARGET(XMLHttpRequestUpload)
  165. CONVERT_TO_EVENT_TARGET(MessagePort)
  166. if (value.inherits(&JSDOMWindowShell::s_info))
  167. return static_cast<JSDOMWindowShell*>(asObject(value))->impl();
  168. #if ENABLE(EVENTSOURCE)
  169. CONVERT_TO_EVENT_TARGET(EventSource)
  170. #endif
  171. #if ENABLE(OFFLINE_WEB_APPLICATIONS)
  172. CONVERT_TO_EVENT_TARGET(DOMApplicationCache)
  173. #endif
  174. #if ENABLE(SVG)
  175. CONVERT_TO_EVENT_TARGET(SVGElementInstance)
  176. #endif
  177. #if ENABLE(WORKERS)
  178. CONVERT_TO_EVENT_TARGET(Worker)
  179. CONVERT_TO_EVENT_TARGET(DedicatedWorkerContext)
  180. #endif
  181. #if ENABLE(SHARED_WORKERS)
  182. CONVERT_TO_EVENT_TARGET(SharedWorker)
  183. CONVERT_TO_EVENT_TARGET(SharedWorkerContext)
  184. #endif
  185. #if ENABLE(NOTIFICATIONS)
  186. CONVERT_TO_EVENT_TARGET(Notification)
  187. #endif
  188. #if ENABLE(WEB_SOCKETS)
  189. CONVERT_TO_EVENT_TARGET(WebSocket)
  190. #endif
  191. return 0;
  192. }
  193. } // namespace WebCore