1/*
2 * This file is part of the WebKit open source project.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "config.h"
21#include "WebKitDOMEvent.h"
22
23#include <WebCore/CSSImportRule.h>
24#include "DOMObjectCache.h"
25#include <WebCore/Document.h>
26#include <WebCore/ExceptionCode.h>
27#include <WebCore/JSExecState.h>
28#include "WebKitDOMEventPrivate.h"
29#include "WebKitDOMEventTargetPrivate.h"
30#include "WebKitDOMPrivate.h"
31#include "ConvertToUTF8String.h"
32#include <wtf/GetPtr.h>
33#include <wtf/RefPtr.h>
34
35#define WEBKIT_DOM_EVENT_GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE(obj, WEBKIT_DOM_TYPE_EVENT, WebKitDOMEventPrivate)
36
37typedef struct _WebKitDOMEventPrivate {
38 RefPtr<WebCore::Event> coreObject;
39} WebKitDOMEventPrivate;
40
41G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
42
43namespace WebKit {
44
45WebKitDOMEvent* kit(WebCore::Event* obj)
46{
47 if (!obj)
48 return 0;
49
50 if (gpointer ret = DOMObjectCache::get(obj))
51 return WEBKIT_DOM_EVENT(ret);
52
53 return wrap(obj);
54}
55
56WebCore::Event* core(WebKitDOMEvent* request)
57{
58 return request ? static_cast<WebCore::Event*>(WEBKIT_DOM_OBJECT(request)->coreObject) : 0;
59}
60
61WebKitDOMEvent* wrapEvent(WebCore::Event* coreObject)
62{
63 ASSERT(coreObject);
64 return WEBKIT_DOM_EVENT(g_object_new(WEBKIT_DOM_TYPE_EVENT, "core-object", coreObject, nullptr));
65}
66
67} // namespace WebKit
68
69G_DEFINE_TYPE(WebKitDOMEvent, webkit_dom_event, WEBKIT_DOM_TYPE_OBJECT)
70
71enum {
72 DOM_EVENT_PROP_0,
73 DOM_EVENT_PROP_TYPE,
74 DOM_EVENT_PROP_TARGET,
75 DOM_EVENT_PROP_CURRENT_TARGET,
76 DOM_EVENT_PROP_EVENT_PHASE,
77 DOM_EVENT_PROP_BUBBLES,
78 DOM_EVENT_PROP_CANCELABLE,
79 DOM_EVENT_PROP_TIME_STAMP,
80 DOM_EVENT_PROP_SRC_ELEMENT,
81 DOM_EVENT_PROP_RETURN_VALUE,
82 DOM_EVENT_PROP_CANCEL_BUBBLE,
83};
84
85static void webkit_dom_event_finalize(GObject* object)
86{
87 WebKitDOMEventPrivate* priv = WEBKIT_DOM_EVENT_GET_PRIVATE(object);
88
89 WebKit::DOMObjectCache::forget(priv->coreObject.get());
90
91 priv->~WebKitDOMEventPrivate();
92 G_OBJECT_CLASS(webkit_dom_event_parent_class)->finalize(object);
93}
94
95static void webkit_dom_event_set_property(GObject* object, guint propertyId, const GValue* value, GParamSpec* pspec)
96{
97 WebKitDOMEvent* self = WEBKIT_DOM_EVENT(object);
98
99 switch (propertyId) {
100 case DOM_EVENT_PROP_RETURN_VALUE:
101 webkit_dom_event_set_return_value(self, g_value_get_boolean(value));
102 break;
103 case DOM_EVENT_PROP_CANCEL_BUBBLE:
104 webkit_dom_event_set_cancel_bubble(self, g_value_get_boolean(value));
105 break;
106 default:
107 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
108 break;
109 }
110}
111
112static void webkit_dom_event_get_property(GObject* object, guint propertyId, GValue* value, GParamSpec* pspec)
113{
114 WebKitDOMEvent* self = WEBKIT_DOM_EVENT(object);
115
116 switch (propertyId) {
117 case DOM_EVENT_PROP_TYPE:
118 g_value_take_string(value, webkit_dom_event_get_event_type(self));
119 break;
120 case DOM_EVENT_PROP_TARGET:
121 g_value_set_object(value, webkit_dom_event_get_target(self));
122 break;
123 case DOM_EVENT_PROP_CURRENT_TARGET:
124 g_value_set_object(value, webkit_dom_event_get_current_target(self));
125 break;
126 case DOM_EVENT_PROP_EVENT_PHASE:
127 g_value_set_uint(value, webkit_dom_event_get_event_phase(self));
128 break;
129 case DOM_EVENT_PROP_BUBBLES:
130 g_value_set_boolean(value, webkit_dom_event_get_bubbles(self));
131 break;
132 case DOM_EVENT_PROP_CANCELABLE:
133 g_value_set_boolean(value, webkit_dom_event_get_cancelable(self));
134 break;
135 case DOM_EVENT_PROP_TIME_STAMP:
136 g_value_set_uint(value, webkit_dom_event_get_time_stamp(self));
137 break;
138 case DOM_EVENT_PROP_SRC_ELEMENT:
139 g_value_set_object(value, webkit_dom_event_get_src_element(self));
140 break;
141 case DOM_EVENT_PROP_RETURN_VALUE:
142 g_value_set_boolean(value, webkit_dom_event_get_return_value(self));
143 break;
144 case DOM_EVENT_PROP_CANCEL_BUBBLE:
145 g_value_set_boolean(value, webkit_dom_event_get_cancel_bubble(self));
146 break;
147 default:
148 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
149 break;
150 }
151}
152
153static GObject* webkit_dom_event_constructor(GType type, guint constructPropertiesCount, GObjectConstructParam* constructProperties)
154{
155 GObject* object = G_OBJECT_CLASS(webkit_dom_event_parent_class)->constructor(type, constructPropertiesCount, constructProperties);
156
157 WebKitDOMEventPrivate* priv = WEBKIT_DOM_EVENT_GET_PRIVATE(object);
158 priv->coreObject = static_cast<WebCore::Event*>(WEBKIT_DOM_OBJECT(object)->coreObject);
159 WebKit::DOMObjectCache::put(priv->coreObject.get(), object);
160
161 return object;
162}
163
164static void webkit_dom_event_class_init(WebKitDOMEventClass* requestClass)
165{
166 GObjectClass* gobjectClass = G_OBJECT_CLASS(requestClass);
167 g_type_class_add_private(gobjectClass, sizeof(WebKitDOMEventPrivate));
168 gobjectClass->constructor = webkit_dom_event_constructor;
169 gobjectClass->finalize = webkit_dom_event_finalize;
170 gobjectClass->set_property = webkit_dom_event_set_property;
171 gobjectClass->get_property = webkit_dom_event_get_property;
172
173 g_object_class_install_property(
174 gobjectClass,
175 DOM_EVENT_PROP_TYPE,
176 g_param_spec_string(
177 "type",
178 "Event:type",
179 "read-only gchar* Event:type",
180 "",
181 WEBKIT_PARAM_READABLE));
182
183 g_object_class_install_property(
184 gobjectClass,
185 DOM_EVENT_PROP_TARGET,
186 g_param_spec_object(
187 "target",
188 "Event:target",
189 "read-only WebKitDOMEventTarget* Event:target",
190 WEBKIT_DOM_TYPE_EVENT_TARGET,
191 WEBKIT_PARAM_READABLE));
192
193 g_object_class_install_property(
194 gobjectClass,
195 DOM_EVENT_PROP_CURRENT_TARGET,
196 g_param_spec_object(
197 "current-target",
198 "Event:current-target",
199 "read-only WebKitDOMEventTarget* Event:current-target",
200 WEBKIT_DOM_TYPE_EVENT_TARGET,
201 WEBKIT_PARAM_READABLE));
202
203 g_object_class_install_property(
204 gobjectClass,
205 DOM_EVENT_PROP_EVENT_PHASE,
206 g_param_spec_uint(
207 "event-phase",
208 "Event:event-phase",
209 "read-only gushort Event:event-phase",
210 0, G_MAXUINT, 0,
211 WEBKIT_PARAM_READABLE));
212
213 g_object_class_install_property(
214 gobjectClass,
215 DOM_EVENT_PROP_BUBBLES,
216 g_param_spec_boolean(
217 "bubbles",
218 "Event:bubbles",
219 "read-only gboolean Event:bubbles",
220 FALSE,
221 WEBKIT_PARAM_READABLE));
222
223 g_object_class_install_property(
224 gobjectClass,
225 DOM_EVENT_PROP_CANCELABLE,
226 g_param_spec_boolean(
227 "cancelable",
228 "Event:cancelable",
229 "read-only gboolean Event:cancelable",
230 FALSE,
231 WEBKIT_PARAM_READABLE));
232
233 g_object_class_install_property(
234 gobjectClass,
235 DOM_EVENT_PROP_TIME_STAMP,
236 g_param_spec_uint(
237 "time-stamp",
238 "Event:time-stamp",
239 "read-only guint32 Event:time-stamp",
240 0, G_MAXUINT, 0,
241 WEBKIT_PARAM_READABLE));
242
243 g_object_class_install_property(
244 gobjectClass,
245 DOM_EVENT_PROP_SRC_ELEMENT,
246 g_param_spec_object(
247 "src-element",
248 "Event:src-element",
249 "read-only WebKitDOMEventTarget* Event:src-element",
250 WEBKIT_DOM_TYPE_EVENT_TARGET,
251 WEBKIT_PARAM_READABLE));
252
253 g_object_class_install_property(
254 gobjectClass,
255 DOM_EVENT_PROP_RETURN_VALUE,
256 g_param_spec_boolean(
257 "return-value",
258 "Event:return-value",
259 "read-write gboolean Event:return-value",
260 FALSE,
261 WEBKIT_PARAM_READWRITE));
262
263 g_object_class_install_property(
264 gobjectClass,
265 DOM_EVENT_PROP_CANCEL_BUBBLE,
266 g_param_spec_boolean(
267 "cancel-bubble",
268 "Event:cancel-bubble",
269 "read-write gboolean Event:cancel-bubble",
270 FALSE,
271 WEBKIT_PARAM_READWRITE));
272
273}
274
275static void webkit_dom_event_init(WebKitDOMEvent* request)
276{
277 WebKitDOMEventPrivate* priv = WEBKIT_DOM_EVENT_GET_PRIVATE(request);
278 new (priv) WebKitDOMEventPrivate();
279}
280
281void webkit_dom_event_stop_propagation(WebKitDOMEvent* self)
282{
283 WebCore::JSMainThreadNullState state;
284 g_return_if_fail(WEBKIT_DOM_IS_EVENT(self));
285 WebCore::Event* item = WebKit::core(self);
286 item->stopPropagation();
287}
288
289void webkit_dom_event_prevent_default(WebKitDOMEvent* self)
290{
291 WebCore::JSMainThreadNullState state;
292 g_return_if_fail(WEBKIT_DOM_IS_EVENT(self));
293 WebCore::Event* item = WebKit::core(self);
294 item->preventDefault();
295}
296
297void webkit_dom_event_init_event(WebKitDOMEvent* self, const gchar* eventTypeArg, gboolean canBubbleArg, gboolean cancelableArg)
298{
299 WebCore::JSMainThreadNullState state;
300 g_return_if_fail(WEBKIT_DOM_IS_EVENT(self));
301 g_return_if_fail(eventTypeArg);
302 WebCore::Event* item = WebKit::core(self);
303 WTF::String convertedEventTypeArg = WTF::String::fromUTF8(eventTypeArg);
304 item->initEvent(convertedEventTypeArg, canBubbleArg, cancelableArg);
305}
306
307gchar* webkit_dom_event_get_event_type(WebKitDOMEvent* self)
308{
309 WebCore::JSMainThreadNullState state;
310 g_return_val_if_fail(WEBKIT_DOM_IS_EVENT(self), 0);
311 WebCore::Event* item = WebKit::core(self);
312 gchar* result = convertToUTF8String(item->type());
313 return result;
314}
315
316WebKitDOMEventTarget* webkit_dom_event_get_target(WebKitDOMEvent* self)
317{
318 WebCore::JSMainThreadNullState state;
319 g_return_val_if_fail(WEBKIT_DOM_IS_EVENT(self), 0);
320 WebCore::Event* item = WebKit::core(self);
321 RefPtr<WebCore::EventTarget> gobjectResult = WTF::getPtr(item->target());
322 return WebKit::kit(gobjectResult.get());
323}
324
325WebKitDOMEventTarget* webkit_dom_event_get_current_target(WebKitDOMEvent* self)
326{
327 WebCore::JSMainThreadNullState state;
328 g_return_val_if_fail(WEBKIT_DOM_IS_EVENT(self), 0);
329 WebCore::Event* item = WebKit::core(self);
330 RefPtr<WebCore::EventTarget> gobjectResult = WTF::getPtr(item->currentTarget());
331 return WebKit::kit(gobjectResult.get());
332}
333
334gushort webkit_dom_event_get_event_phase(WebKitDOMEvent* self)
335{
336 WebCore::JSMainThreadNullState state;
337 g_return_val_if_fail(WEBKIT_DOM_IS_EVENT(self), 0);
338 WebCore::Event* item = WebKit::core(self);
339 gushort result = item->eventPhase();
340 return result;
341}
342
343gboolean webkit_dom_event_get_bubbles(WebKitDOMEvent* self)
344{
345 WebCore::JSMainThreadNullState state;
346 g_return_val_if_fail(WEBKIT_DOM_IS_EVENT(self), FALSE);
347 WebCore::Event* item = WebKit::core(self);
348 gboolean result = item->bubbles();
349 return result;
350}
351
352gboolean webkit_dom_event_get_cancelable(WebKitDOMEvent* self)
353{
354 WebCore::JSMainThreadNullState state;
355 g_return_val_if_fail(WEBKIT_DOM_IS_EVENT(self), FALSE);
356 WebCore::Event* item = WebKit::core(self);
357 gboolean result = item->cancelable();
358 return result;
359}
360
361guint32 webkit_dom_event_get_time_stamp(WebKitDOMEvent* self)
362{
363 WebCore::JSMainThreadNullState state;
364 g_return_val_if_fail(WEBKIT_DOM_IS_EVENT(self), 0);
365 WebCore::Event* item = WebKit::core(self);
366 guint32 result = item->timeStamp().approximateWallTime().secondsSinceEpoch().milliseconds();
367 return result;
368}
369
370WebKitDOMEventTarget* webkit_dom_event_get_src_element(WebKitDOMEvent* self)
371{
372 WebCore::JSMainThreadNullState state;
373 g_return_val_if_fail(WEBKIT_DOM_IS_EVENT(self), 0);
374 WebCore::Event* item = WebKit::core(self);
375 RefPtr<WebCore::EventTarget> gobjectResult = WTF::getPtr(item->target());
376 return WebKit::kit(gobjectResult.get());
377}
378
379gboolean webkit_dom_event_get_return_value(WebKitDOMEvent* self)
380{
381 WebCore::JSMainThreadNullState state;
382 g_return_val_if_fail(WEBKIT_DOM_IS_EVENT(self), FALSE);
383 WebCore::Event* item = WebKit::core(self);
384 gboolean result = item->legacyReturnValue();
385 return result;
386}
387
388void webkit_dom_event_set_return_value(WebKitDOMEvent* self, gboolean value)
389{
390 WebCore::JSMainThreadNullState state;
391 g_return_if_fail(WEBKIT_DOM_IS_EVENT(self));
392 WebCore::Event* item = WebKit::core(self);
393 item->setLegacyReturnValue(value);
394}
395
396gboolean webkit_dom_event_get_cancel_bubble(WebKitDOMEvent* self)
397{
398 WebCore::JSMainThreadNullState state;
399 g_return_val_if_fail(WEBKIT_DOM_IS_EVENT(self), FALSE);
400 WebCore::Event* item = WebKit::core(self);
401 gboolean result = item->cancelBubble();
402 return result;
403}
404
405void webkit_dom_event_set_cancel_bubble(WebKitDOMEvent* self, gboolean value)
406{
407 WebCore::JSMainThreadNullState state;
408 g_return_if_fail(WEBKIT_DOM_IS_EVENT(self));
409 WebCore::Event* item = WebKit::core(self);
410 item->setCancelBubble(value);
411}
412
413G_GNUC_END_IGNORE_DEPRECATIONS;
414