Drop ExceptionCodeDescription class
[WebKit-https.git] / Source / WebKit / WebProcess / InjectedBundle / API / gtk / DOM / WebKitDOMCharacterData.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 "WebKitDOMCharacterData.h"
22
23 #include <WebCore/CSSImportRule.h>
24 #include "DOMObjectCache.h"
25 #include <WebCore/DOMException.h>
26 #include <WebCore/Document.h>
27 #include "GObjectEventListener.h"
28 #include <WebCore/JSMainThreadExecState.h>
29 #include "WebKitDOMCharacterDataPrivate.h"
30 #include "WebKitDOMElementPrivate.h"
31 #include "WebKitDOMEventPrivate.h"
32 #include "WebKitDOMEventTarget.h"
33 #include "WebKitDOMNodePrivate.h"
34 #include "WebKitDOMPrivate.h"
35 #include "ConvertToUTF8String.h"
36 #include <wtf/GetPtr.h>
37 #include <wtf/RefPtr.h>
38
39 namespace WebKit {
40
41 WebKitDOMCharacterData* kit(WebCore::CharacterData* obj)
42 {
43     return WEBKIT_DOM_CHARACTER_DATA(kit(static_cast<WebCore::Node*>(obj)));
44 }
45
46 WebCore::CharacterData* core(WebKitDOMCharacterData* request)
47 {
48     return request ? static_cast<WebCore::CharacterData*>(WEBKIT_DOM_OBJECT(request)->coreObject) : 0;
49 }
50
51 WebKitDOMCharacterData* wrapCharacterData(WebCore::CharacterData* coreObject)
52 {
53     ASSERT(coreObject);
54     return WEBKIT_DOM_CHARACTER_DATA(g_object_new(WEBKIT_DOM_TYPE_CHARACTER_DATA, "core-object", coreObject, nullptr));
55 }
56
57 } // namespace WebKit
58
59 static gboolean webkit_dom_character_data_dispatch_event(WebKitDOMEventTarget* target, WebKitDOMEvent* event, GError** error)
60 {
61     WebCore::Event* coreEvent = WebKit::core(event);
62     if (!coreEvent)
63         return false;
64     WebCore::CharacterData* coreTarget = static_cast<WebCore::CharacterData*>(WEBKIT_DOM_OBJECT(target)->coreObject);
65
66     auto result = coreTarget->dispatchEventForBindings(*coreEvent);
67     if (result.hasException()) {
68         auto description = WebCore::DOMException::description(result.releaseException().code());
69         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
70         return false;
71     }
72     return result.releaseReturnValue();
73 }
74
75 static gboolean webkit_dom_character_data_add_event_listener(WebKitDOMEventTarget* target, const char* eventName, GClosure* handler, gboolean useCapture)
76 {
77     WebCore::CharacterData* coreTarget = static_cast<WebCore::CharacterData*>(WEBKIT_DOM_OBJECT(target)->coreObject);
78     return WebKit::GObjectEventListener::addEventListener(G_OBJECT(target), coreTarget, eventName, handler, useCapture);
79 }
80
81 static gboolean webkit_dom_character_data_remove_event_listener(WebKitDOMEventTarget* target, const char* eventName, GClosure* handler, gboolean useCapture)
82 {
83     WebCore::CharacterData* coreTarget = static_cast<WebCore::CharacterData*>(WEBKIT_DOM_OBJECT(target)->coreObject);
84     return WebKit::GObjectEventListener::removeEventListener(G_OBJECT(target), coreTarget, eventName, handler, useCapture);
85 }
86
87 static void webkit_dom_event_target_init(WebKitDOMEventTargetIface* iface)
88 {
89     iface->dispatch_event = webkit_dom_character_data_dispatch_event;
90     iface->add_event_listener = webkit_dom_character_data_add_event_listener;
91     iface->remove_event_listener = webkit_dom_character_data_remove_event_listener;
92 }
93
94 G_DEFINE_TYPE_WITH_CODE(WebKitDOMCharacterData, webkit_dom_character_data, WEBKIT_DOM_TYPE_NODE, G_IMPLEMENT_INTERFACE(WEBKIT_DOM_TYPE_EVENT_TARGET, webkit_dom_event_target_init))
95
96 enum {
97     PROP_0,
98     PROP_DATA,
99     PROP_LENGTH,
100 };
101
102 static void webkit_dom_character_data_set_property(GObject* object, guint propertyId, const GValue* value, GParamSpec* pspec)
103 {
104     WebKitDOMCharacterData* self = WEBKIT_DOM_CHARACTER_DATA(object);
105
106     switch (propertyId) {
107     case PROP_DATA:
108         webkit_dom_character_data_set_data(self, g_value_get_string(value), nullptr);
109         break;
110     default:
111         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
112         break;
113     }
114 }
115
116 static void webkit_dom_character_data_get_property(GObject* object, guint propertyId, GValue* value, GParamSpec* pspec)
117 {
118     WebKitDOMCharacterData* self = WEBKIT_DOM_CHARACTER_DATA(object);
119
120     switch (propertyId) {
121     case PROP_DATA:
122         g_value_take_string(value, webkit_dom_character_data_get_data(self));
123         break;
124     case PROP_LENGTH:
125         g_value_set_ulong(value, webkit_dom_character_data_get_length(self));
126         break;
127     default:
128         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
129         break;
130     }
131 }
132
133 static void webkit_dom_character_data_class_init(WebKitDOMCharacterDataClass* requestClass)
134 {
135     GObjectClass* gobjectClass = G_OBJECT_CLASS(requestClass);
136     gobjectClass->set_property = webkit_dom_character_data_set_property;
137     gobjectClass->get_property = webkit_dom_character_data_get_property;
138
139     g_object_class_install_property(
140         gobjectClass,
141         PROP_DATA,
142         g_param_spec_string(
143             "data",
144             "CharacterData:data",
145             "read-write gchar* CharacterData:data",
146             "",
147             WEBKIT_PARAM_READWRITE));
148
149     g_object_class_install_property(
150         gobjectClass,
151         PROP_LENGTH,
152         g_param_spec_ulong(
153             "length",
154             "CharacterData:length",
155             "read-only gulong CharacterData:length",
156             0, G_MAXULONG, 0,
157             WEBKIT_PARAM_READABLE));
158 }
159
160 static void webkit_dom_character_data_init(WebKitDOMCharacterData* request)
161 {
162     UNUSED_PARAM(request);
163 }
164
165 gchar* webkit_dom_character_data_substring_data(WebKitDOMCharacterData* self, gulong offset, gulong length, GError** error)
166 {
167     WebCore::JSMainThreadNullState state;
168     g_return_val_if_fail(WEBKIT_DOM_IS_CHARACTER_DATA(self), 0);
169     g_return_val_if_fail(!error || !*error, 0);
170     WebCore::CharacterData* item = WebKit::core(self);
171     auto result = item->substringData(offset, length);
172     if (result.hasException())
173         return nullptr;
174     return convertToUTF8String(result.releaseReturnValue());
175 }
176
177 void webkit_dom_character_data_append_data(WebKitDOMCharacterData* self, const gchar* data, GError** error)
178 {
179     WebCore::JSMainThreadNullState state;
180     g_return_if_fail(WEBKIT_DOM_IS_CHARACTER_DATA(self));
181     g_return_if_fail(data);
182     UNUSED_PARAM(error);
183     WebCore::CharacterData* item = WebKit::core(self);
184     WTF::String convertedData = WTF::String::fromUTF8(data);
185     item->appendData(convertedData);
186 }
187
188 void webkit_dom_character_data_insert_data(WebKitDOMCharacterData* self, gulong offset, const gchar* data, GError** error)
189 {
190     WebCore::JSMainThreadNullState state;
191     g_return_if_fail(WEBKIT_DOM_IS_CHARACTER_DATA(self));
192     g_return_if_fail(data);
193     g_return_if_fail(!error || !*error);
194     WebCore::CharacterData* item = WebKit::core(self);
195     WTF::String convertedData = WTF::String::fromUTF8(data);
196     auto result = item->insertData(offset, convertedData);
197     if (result.hasException()) {
198         auto description = WebCore::DOMException::description(result.releaseException().code());
199         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
200     }
201 }
202
203 void webkit_dom_character_data_delete_data(WebKitDOMCharacterData* self, gulong offset, gulong length, GError** error)
204 {
205     WebCore::JSMainThreadNullState state;
206     g_return_if_fail(WEBKIT_DOM_IS_CHARACTER_DATA(self));
207     g_return_if_fail(!error || !*error);
208     WebCore::CharacterData* item = WebKit::core(self);
209     auto result = item->deleteData(offset, length);
210     if (result.hasException()) {
211         auto description = WebCore::DOMException::description(result.releaseException().code());
212         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
213     }
214 }
215
216 void webkit_dom_character_data_replace_data(WebKitDOMCharacterData* self, gulong offset, gulong length, const gchar* data, GError** error)
217 {
218     WebCore::JSMainThreadNullState state;
219     g_return_if_fail(WEBKIT_DOM_IS_CHARACTER_DATA(self));
220     g_return_if_fail(data);
221     g_return_if_fail(!error || !*error);
222     WebCore::CharacterData* item = WebKit::core(self);
223     WTF::String convertedData = WTF::String::fromUTF8(data);
224     auto result = item->replaceData(offset, length, convertedData);
225     if (result.hasException()) {
226         auto description = WebCore::DOMException::description(result.releaseException().code());
227         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
228     }
229 }
230
231 gchar* webkit_dom_character_data_get_data(WebKitDOMCharacterData* self)
232 {
233     WebCore::JSMainThreadNullState state;
234     g_return_val_if_fail(WEBKIT_DOM_IS_CHARACTER_DATA(self), 0);
235     WebCore::CharacterData* item = WebKit::core(self);
236     gchar* result = convertToUTF8String(item->data());
237     return result;
238 }
239
240 void webkit_dom_character_data_set_data(WebKitDOMCharacterData* self, const gchar* value, GError** error)
241 {
242     WebCore::JSMainThreadNullState state;
243     g_return_if_fail(WEBKIT_DOM_IS_CHARACTER_DATA(self));
244     g_return_if_fail(value);
245     UNUSED_PARAM(error);
246     WebCore::CharacterData* item = WebKit::core(self);
247     WTF::String convertedValue = WTF::String::fromUTF8(value);
248     item->setData(convertedValue);
249 }
250
251 gulong webkit_dom_character_data_get_length(WebKitDOMCharacterData* self)
252 {
253     WebCore::JSMainThreadNullState state;
254     g_return_val_if_fail(WEBKIT_DOM_IS_CHARACTER_DATA(self), 0);
255     WebCore::CharacterData* item = WebKit::core(self);
256     gulong result = item->length();
257     return result;
258 }