60fb941c87e1fee6dda31b0d2709d780404ed1d5
[WebKit-https.git] / Source / WebKit / WebProcess / InjectedBundle / API / gtk / DOM / WebKitDOMHTMLHtmlElement.cpp
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 "WebKitDOMHTMLHtmlElement.h"
22
23 #include <WebCore/CSSImportRule.h>
24 #include "DOMObjectCache.h"
25 #include <WebCore/Document.h>
26 #include <WebCore/ExceptionCode.h>
27 #include <WebCore/ExceptionCodeDescription.h>
28 #include "GObjectEventListener.h"
29 #include <WebCore/HTMLNames.h>
30 #include <WebCore/JSMainThreadExecState.h>
31 #include "WebKitDOMEventPrivate.h"
32 #include "WebKitDOMEventTarget.h"
33 #include "WebKitDOMHTMLHtmlElementPrivate.h"
34 #include "WebKitDOMNodePrivate.h"
35 #include "WebKitDOMPrivate.h"
36 #include "ConvertToUTF8String.h"
37 #include <wtf/GetPtr.h>
38 #include <wtf/RefPtr.h>
39
40 namespace WebKit {
41
42 WebKitDOMHTMLHtmlElement* kit(WebCore::HTMLHtmlElement* obj)
43 {
44     return WEBKIT_DOM_HTML_HTML_ELEMENT(kit(static_cast<WebCore::Node*>(obj)));
45 }
46
47 WebCore::HTMLHtmlElement* core(WebKitDOMHTMLHtmlElement* request)
48 {
49     return request ? static_cast<WebCore::HTMLHtmlElement*>(WEBKIT_DOM_OBJECT(request)->coreObject) : 0;
50 }
51
52 WebKitDOMHTMLHtmlElement* wrapHTMLHtmlElement(WebCore::HTMLHtmlElement* coreObject)
53 {
54     ASSERT(coreObject);
55     return WEBKIT_DOM_HTML_HTML_ELEMENT(g_object_new(WEBKIT_DOM_TYPE_HTML_HTML_ELEMENT, "core-object", coreObject, nullptr));
56 }
57
58 } // namespace WebKit
59
60 static gboolean webkit_dom_html_html_element_dispatch_event(WebKitDOMEventTarget* target, WebKitDOMEvent* event, GError** error)
61 {
62     WebCore::Event* coreEvent = WebKit::core(event);
63     if (!coreEvent)
64         return false;
65     WebCore::HTMLHtmlElement* coreTarget = static_cast<WebCore::HTMLHtmlElement*>(WEBKIT_DOM_OBJECT(target)->coreObject);
66
67     auto result = coreTarget->dispatchEventForBindings(*coreEvent);
68     if (result.hasException()) {
69         WebCore::ExceptionCodeDescription description(result.releaseException().code());
70         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.code, description.name);
71         return false;
72     }
73     return result.releaseReturnValue();
74 }
75
76 static gboolean webkit_dom_html_html_element_add_event_listener(WebKitDOMEventTarget* target, const char* eventName, GClosure* handler, gboolean useCapture)
77 {
78     WebCore::HTMLHtmlElement* coreTarget = static_cast<WebCore::HTMLHtmlElement*>(WEBKIT_DOM_OBJECT(target)->coreObject);
79     return WebKit::GObjectEventListener::addEventListener(G_OBJECT(target), coreTarget, eventName, handler, useCapture);
80 }
81
82 static gboolean webkit_dom_html_html_element_remove_event_listener(WebKitDOMEventTarget* target, const char* eventName, GClosure* handler, gboolean useCapture)
83 {
84     WebCore::HTMLHtmlElement* coreTarget = static_cast<WebCore::HTMLHtmlElement*>(WEBKIT_DOM_OBJECT(target)->coreObject);
85     return WebKit::GObjectEventListener::removeEventListener(G_OBJECT(target), coreTarget, eventName, handler, useCapture);
86 }
87
88 static void webkit_dom_event_target_init(WebKitDOMEventTargetIface* iface)
89 {
90     iface->dispatch_event = webkit_dom_html_html_element_dispatch_event;
91     iface->add_event_listener = webkit_dom_html_html_element_add_event_listener;
92     iface->remove_event_listener = webkit_dom_html_html_element_remove_event_listener;
93 }
94
95 G_DEFINE_TYPE_WITH_CODE(WebKitDOMHTMLHtmlElement, webkit_dom_html_html_element, WEBKIT_DOM_TYPE_HTML_ELEMENT, G_IMPLEMENT_INTERFACE(WEBKIT_DOM_TYPE_EVENT_TARGET, webkit_dom_event_target_init))
96
97 enum {
98     PROP_0,
99     PROP_VERSION,
100 };
101
102 static void webkit_dom_html_html_element_set_property(GObject* object, guint propertyId, const GValue* value, GParamSpec* pspec)
103 {
104     WebKitDOMHTMLHtmlElement* self = WEBKIT_DOM_HTML_HTML_ELEMENT(object);
105
106     switch (propertyId) {
107     case PROP_VERSION:
108         webkit_dom_html_html_element_set_version(self, g_value_get_string(value));
109         break;
110     default:
111         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
112         break;
113     }
114 }
115
116 static void webkit_dom_html_html_element_get_property(GObject* object, guint propertyId, GValue* value, GParamSpec* pspec)
117 {
118     WebKitDOMHTMLHtmlElement* self = WEBKIT_DOM_HTML_HTML_ELEMENT(object);
119
120     switch (propertyId) {
121     case PROP_VERSION:
122         g_value_take_string(value, webkit_dom_html_html_element_get_version(self));
123         break;
124     default:
125         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
126         break;
127     }
128 }
129
130 static void webkit_dom_html_html_element_class_init(WebKitDOMHTMLHtmlElementClass* requestClass)
131 {
132     GObjectClass* gobjectClass = G_OBJECT_CLASS(requestClass);
133     gobjectClass->set_property = webkit_dom_html_html_element_set_property;
134     gobjectClass->get_property = webkit_dom_html_html_element_get_property;
135
136     g_object_class_install_property(
137         gobjectClass,
138         PROP_VERSION,
139         g_param_spec_string(
140             "version",
141             "HTMLHtmlElement:version",
142             "read-write gchar* HTMLHtmlElement:version",
143             "",
144             WEBKIT_PARAM_READWRITE));
145 }
146
147 static void webkit_dom_html_html_element_init(WebKitDOMHTMLHtmlElement* request)
148 {
149     UNUSED_PARAM(request);
150 }
151
152 gchar* webkit_dom_html_html_element_get_version(WebKitDOMHTMLHtmlElement* self)
153 {
154     WebCore::JSMainThreadNullState state;
155     g_return_val_if_fail(WEBKIT_DOM_IS_HTML_HTML_ELEMENT(self), 0);
156     WebCore::HTMLHtmlElement* item = WebKit::core(self);
157     gchar* result = convertToUTF8String(item->attributeWithoutSynchronization(WebCore::HTMLNames::versionAttr));
158     return result;
159 }
160
161 void webkit_dom_html_html_element_set_version(WebKitDOMHTMLHtmlElement* self, const gchar* value)
162 {
163     WebCore::JSMainThreadNullState state;
164     g_return_if_fail(WEBKIT_DOM_IS_HTML_HTML_ELEMENT(self));
165     g_return_if_fail(value);
166     WebCore::HTMLHtmlElement* item = WebKit::core(self);
167     WTF::String convertedValue = WTF::String::fromUTF8(value);
168     item->setAttributeWithoutSynchronization(WebCore::HTMLNames::versionAttr, convertedValue);
169 }