tizen beta release
[framework/web/webkit-efl.git] / Source / WebCore / bindings / cpp / WebDOMEventTarget.cpp
1 /*
2  * Copyright (C) Research In Motion Limited 2010. All rights reserved.
3  * Copyright (C) 2008 Apple Inc. All Rights Reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #include "config.h"
22 #include "WebDOMEventTarget.h"
23
24 #include "DOMApplicationCache.h"
25 #include "DOMWindow.h"
26 #include "DedicatedWorkerContext.h"
27 #include "EventSource.h"
28 #include "MessagePort.h"
29 #include "Node.h"
30 #include "Notification.h"
31 #include "SharedWorker.h"
32 #include "SharedWorkerContext.h"
33 #include "ThreadCheck.h"
34 #include "WebDOMDOMApplicationCache.h"
35 #include "WebDOMDOMWindow.h"
36 #include "WebDOMEventSource.h"
37 #include "WebDOMMessagePort.h"
38 #include "WebDOMNode.h"
39 #include "WebDOMXMLHttpRequest.h"
40 #include "WebDOMXMLHttpRequestUpload.h"
41 #include "WebExceptionHandler.h"
42 #include "WebSocket.h"
43 #include "Worker.h"
44 #include "XMLHttpRequest.h"
45 #include "XMLHttpRequestUpload.h"
46
47 #include <wtf/RefPtr.h>
48
49 #if ENABLE(WORKERS)
50 #include "WebDOMDedicatedWorkerContext.h"
51 #include "WebDOMWorker.h"
52 #endif
53
54 #if ENABLE(SHARED_WORKERS)
55 #include "WebDOMSharedWorker.h"
56 #include "WebDOMSharedWorkerContext.h"
57 #endif
58
59 #if ENABLE(NOTIFICATIONS)
60 #include "WebDOMNotification.h"
61 #endif
62
63 #if ENABLE(WEB_SOCKETS)
64 #include "WebDOMWebSocket.h"
65 #endif
66
67 struct WebDOMEventTarget::WebDOMEventTargetPrivate {
68     WebDOMEventTargetPrivate(WebCore::EventTarget* object = 0)
69         : impl(object)
70     {
71     }
72
73     RefPtr<WebCore::EventTarget> impl;
74 };
75
76 WebDOMEventTarget::WebDOMEventTarget()
77     : WebDOMObject()
78     , m_impl(0)
79 {
80 }
81
82 WebDOMEventTarget::WebDOMEventTarget(WebCore::EventTarget* impl)
83     : WebDOMObject()
84     , m_impl(new WebDOMEventTargetPrivate(impl))
85 {
86 }
87
88 WebDOMEventTarget::WebDOMEventTarget(const WebDOMEventTarget& copy)
89     : WebDOMObject()
90 {
91     m_impl = copy.impl() ? new WebDOMEventTargetPrivate(copy.impl()) : 0;
92 }
93
94 WebDOMEventTarget::~WebDOMEventTarget()
95 {
96     delete m_impl;
97     m_impl = 0;
98 }
99
100 WebCore::EventTarget* WebDOMEventTarget::impl() const
101 {
102     return m_impl ? m_impl->impl.get() : 0;
103 }
104
105 #define ConvertTo(type) \
106 WebDOM##type WebDOMEventTarget::to##type() \
107 { \
108     WebCore::EventTarget* target = impl(); \
109     return WebDOM##type(target ? target->to##type() : 0); \
110 }
111
112 ConvertTo(Node)
113 ConvertTo(DOMWindow)
114
115 #if ENABLE(WORKERS)
116 ConvertTo(Worker)
117 ConvertTo(DedicatedWorkerContext)
118 #endif
119
120 #if ENABLE(SHARED_WORKERS)
121 ConvertTo(SharedWorker)
122 ConvertTo(SharedWorkerContext)
123 #endif
124
125 #if ENABLE(NOTIFICATIONS)
126 ConvertTo(Notification)
127 #endif
128
129 #if ENABLE(WEB_SOCKETS)
130 ConvertTo(WebSocket)
131 #endif
132
133 WebCore::EventTarget* toWebCore(const WebDOMEventTarget& wrapper)
134 {
135     return wrapper.impl();
136 }
137
138 WebDOMEventTarget toWebKit(WebCore::EventTarget* value)
139 {
140     if (WebCore::Node* node = value->toNode())
141         return toWebKit(node);
142
143     if (WebCore::DOMWindow* window = value->toDOMWindow())
144         return toWebKit(window);
145
146 #if ENABLE(SVG) && 0
147     // FIXME: Enable once SVG bindings are generated.
148     // SVGElementInstance supports both toSVGElementInstance and toNode since so much mouse handling code depends on toNode returning a valid node.
149     if (WebCore::SVGElementInstance* instance = value->toSVGElementInstance())
150         return toWebKit(instance);
151 #endif
152
153 #if ENABLE(WORKERS)
154     if (WebCore::Worker* worker = value->toWorker())
155         return toWebKit(worker);
156
157     if (WebCore::DedicatedWorkerContext* workerContext = value->toDedicatedWorkerContext())
158         return toWebKit(workerContext);
159 #endif
160
161 #if ENABLE(SHARED_WORKERS)
162     if (WebCore::SharedWorker* sharedWorker = value->toSharedWorker())
163         return toWebKit(sharedWorker);
164
165     if (WebCore::SharedWorkerContext* workerContext = value->toSharedWorkerContext())
166         return toWebKit(workerContext);
167 #endif
168
169 #if ENABLE(NOTIFICATIONS)
170     if (WebCore::Notification* notification = value->toNotification())
171         return toWebKit(notification);
172 #endif
173
174 #if ENABLE(WEB_SOCKETS)
175     if (WebCore::WebSocket* webSocket = value->toWebSocket())
176         return toWebKit(webSocket);
177 #endif
178
179     ASSERT_NOT_REACHED();
180     return WebDOMEventTarget();
181 }
182
183 WebDOMEventTarget& WebDOMEventTarget::operator=(const WebDOMEventTarget& copy)
184 {
185     delete m_impl;
186     m_impl = copy.impl() ? new WebDOMEventTargetPrivate(copy.impl()) : 0;
187     return *this;
188 }