Drop ExceptionCodeDescription class
[WebKit-https.git] / Source / WebKit / WebProcess / InjectedBundle / API / gtk / DOM / WebKitDOMTreeWalker.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 "WebKitDOMTreeWalker.h"
22
23 #include <WebCore/CSSImportRule.h>
24 #include "DOMObjectCache.h"
25 #include <WebCore/Document.h>
26 #include <WebCore/ExceptionCode.h>
27 #include <WebCore/JSMainThreadExecState.h>
28 #include "WebKitDOMNodeFilterPrivate.h"
29 #include "WebKitDOMNodePrivate.h"
30 #include "WebKitDOMPrivate.h"
31 #include "WebKitDOMTreeWalkerPrivate.h"
32 #include "ConvertToUTF8String.h"
33 #include <wtf/GetPtr.h>
34 #include <wtf/RefPtr.h>
35
36 #define WEBKIT_DOM_TREE_WALKER_GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE(obj, WEBKIT_DOM_TYPE_TREE_WALKER, WebKitDOMTreeWalkerPrivate)
37
38 typedef struct _WebKitDOMTreeWalkerPrivate {
39     RefPtr<WebCore::TreeWalker> coreObject;
40 } WebKitDOMTreeWalkerPrivate;
41
42 namespace WebKit {
43
44 WebKitDOMTreeWalker* kit(WebCore::TreeWalker* obj)
45 {
46     if (!obj)
47         return 0;
48
49     if (gpointer ret = DOMObjectCache::get(obj))
50         return WEBKIT_DOM_TREE_WALKER(ret);
51
52     return wrapTreeWalker(obj);
53 }
54
55 WebCore::TreeWalker* core(WebKitDOMTreeWalker* request)
56 {
57     return request ? static_cast<WebCore::TreeWalker*>(WEBKIT_DOM_OBJECT(request)->coreObject) : 0;
58 }
59
60 WebKitDOMTreeWalker* wrapTreeWalker(WebCore::TreeWalker* coreObject)
61 {
62     ASSERT(coreObject);
63     return WEBKIT_DOM_TREE_WALKER(g_object_new(WEBKIT_DOM_TYPE_TREE_WALKER, "core-object", coreObject, nullptr));
64 }
65
66 } // namespace WebKit
67
68 G_DEFINE_TYPE(WebKitDOMTreeWalker, webkit_dom_tree_walker, WEBKIT_DOM_TYPE_OBJECT)
69
70 enum {
71     PROP_0,
72     PROP_ROOT,
73     PROP_WHAT_TO_SHOW,
74     PROP_FILTER,
75     PROP_CURRENT_NODE,
76 };
77
78 static void webkit_dom_tree_walker_finalize(GObject* object)
79 {
80     WebKitDOMTreeWalkerPrivate* priv = WEBKIT_DOM_TREE_WALKER_GET_PRIVATE(object);
81
82     WebKit::DOMObjectCache::forget(priv->coreObject.get());
83
84     priv->~WebKitDOMTreeWalkerPrivate();
85     G_OBJECT_CLASS(webkit_dom_tree_walker_parent_class)->finalize(object);
86 }
87
88 static void webkit_dom_tree_walker_get_property(GObject* object, guint propertyId, GValue* value, GParamSpec* pspec)
89 {
90     WebKitDOMTreeWalker* self = WEBKIT_DOM_TREE_WALKER(object);
91
92     switch (propertyId) {
93     case PROP_ROOT:
94         g_value_set_object(value, webkit_dom_tree_walker_get_root(self));
95         break;
96     case PROP_WHAT_TO_SHOW:
97         g_value_set_ulong(value, webkit_dom_tree_walker_get_what_to_show(self));
98         break;
99     case PROP_FILTER:
100         g_value_set_object(value, webkit_dom_tree_walker_get_filter(self));
101         break;
102     case PROP_CURRENT_NODE:
103         g_value_set_object(value, webkit_dom_tree_walker_get_current_node(self));
104         break;
105     default:
106         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
107         break;
108     }
109 }
110
111 static GObject* webkit_dom_tree_walker_constructor(GType type, guint constructPropertiesCount, GObjectConstructParam* constructProperties)
112 {
113     GObject* object = G_OBJECT_CLASS(webkit_dom_tree_walker_parent_class)->constructor(type, constructPropertiesCount, constructProperties);
114
115     WebKitDOMTreeWalkerPrivate* priv = WEBKIT_DOM_TREE_WALKER_GET_PRIVATE(object);
116     priv->coreObject = static_cast<WebCore::TreeWalker*>(WEBKIT_DOM_OBJECT(object)->coreObject);
117     WebKit::DOMObjectCache::put(priv->coreObject.get(), object);
118
119     return object;
120 }
121
122 static void webkit_dom_tree_walker_class_init(WebKitDOMTreeWalkerClass* requestClass)
123 {
124     GObjectClass* gobjectClass = G_OBJECT_CLASS(requestClass);
125     g_type_class_add_private(gobjectClass, sizeof(WebKitDOMTreeWalkerPrivate));
126     gobjectClass->constructor = webkit_dom_tree_walker_constructor;
127     gobjectClass->finalize = webkit_dom_tree_walker_finalize;
128     gobjectClass->get_property = webkit_dom_tree_walker_get_property;
129
130     g_object_class_install_property(
131         gobjectClass,
132         PROP_ROOT,
133         g_param_spec_object(
134             "root",
135             "TreeWalker:root",
136             "read-only WebKitDOMNode* TreeWalker:root",
137             WEBKIT_DOM_TYPE_NODE,
138             WEBKIT_PARAM_READABLE));
139
140     g_object_class_install_property(
141         gobjectClass,
142         PROP_WHAT_TO_SHOW,
143         g_param_spec_ulong(
144             "what-to-show",
145             "TreeWalker:what-to-show",
146             "read-only gulong TreeWalker:what-to-show",
147             0, G_MAXULONG, 0,
148             WEBKIT_PARAM_READABLE));
149
150     g_object_class_install_property(
151         gobjectClass,
152         PROP_FILTER,
153         g_param_spec_object(
154             "filter",
155             "TreeWalker:filter",
156             "read-only WebKitDOMNodeFilter* TreeWalker:filter",
157             WEBKIT_DOM_TYPE_NODE_FILTER,
158             WEBKIT_PARAM_READABLE));
159
160     g_object_class_install_property(
161         gobjectClass,
162         PROP_CURRENT_NODE,
163         g_param_spec_object(
164             "current-node",
165             "TreeWalker:current-node",
166             "read-only WebKitDOMNode* TreeWalker:current-node",
167             WEBKIT_DOM_TYPE_NODE,
168             WEBKIT_PARAM_READABLE));
169
170 }
171
172 static void webkit_dom_tree_walker_init(WebKitDOMTreeWalker* request)
173 {
174     WebKitDOMTreeWalkerPrivate* priv = WEBKIT_DOM_TREE_WALKER_GET_PRIVATE(request);
175     new (priv) WebKitDOMTreeWalkerPrivate();
176 }
177
178 WebKitDOMNode* webkit_dom_tree_walker_parent_node(WebKitDOMTreeWalker* self)
179 {
180     WebCore::JSMainThreadNullState state;
181     g_return_val_if_fail(WEBKIT_DOM_IS_TREE_WALKER(self), 0);
182     WebCore::TreeWalker* item = WebKit::core(self);
183
184     auto result = item->parentNode();
185     if (result.hasException())
186         return nullptr;
187
188     RefPtr<WebCore::Node> gobjectResult = WTF::getPtr(result.releaseReturnValue());
189     return WebKit::kit(gobjectResult.get());
190 }
191
192 WebKitDOMNode* webkit_dom_tree_walker_first_child(WebKitDOMTreeWalker* self)
193 {
194     WebCore::JSMainThreadNullState state;
195     g_return_val_if_fail(WEBKIT_DOM_IS_TREE_WALKER(self), 0);
196     WebCore::TreeWalker* item = WebKit::core(self);
197
198     auto result = item->firstChild();
199     if (result.hasException())
200         return nullptr;
201
202     RefPtr<WebCore::Node> gobjectResult = WTF::getPtr(result.releaseReturnValue());
203     return WebKit::kit(gobjectResult.get());
204 }
205
206 WebKitDOMNode* webkit_dom_tree_walker_last_child(WebKitDOMTreeWalker* self)
207 {
208     WebCore::JSMainThreadNullState state;
209     g_return_val_if_fail(WEBKIT_DOM_IS_TREE_WALKER(self), 0);
210     WebCore::TreeWalker* item = WebKit::core(self);
211
212     auto result = item->lastChild();
213     if (result.hasException())
214         return nullptr;
215
216     RefPtr<WebCore::Node> gobjectResult = WTF::getPtr(result.releaseReturnValue());
217     return WebKit::kit(gobjectResult.get());
218 }
219
220 WebKitDOMNode* webkit_dom_tree_walker_previous_sibling(WebKitDOMTreeWalker* self)
221 {
222     WebCore::JSMainThreadNullState state;
223     g_return_val_if_fail(WEBKIT_DOM_IS_TREE_WALKER(self), 0);
224     WebCore::TreeWalker* item = WebKit::core(self);
225
226     auto result = item->previousSibling();
227     if (result.hasException())
228         return nullptr;
229
230     RefPtr<WebCore::Node> gobjectResult = WTF::getPtr(result.releaseReturnValue());
231     return WebKit::kit(gobjectResult.get());
232 }
233
234 WebKitDOMNode* webkit_dom_tree_walker_next_sibling(WebKitDOMTreeWalker* self)
235 {
236     WebCore::JSMainThreadNullState state;
237     g_return_val_if_fail(WEBKIT_DOM_IS_TREE_WALKER(self), 0);
238     WebCore::TreeWalker* item = WebKit::core(self);
239
240     auto result = item->nextSibling();
241     if (result.hasException())
242         return nullptr;
243
244     RefPtr<WebCore::Node> gobjectResult = WTF::getPtr(result.releaseReturnValue());
245     return WebKit::kit(gobjectResult.get());
246 }
247
248 WebKitDOMNode* webkit_dom_tree_walker_previous_node(WebKitDOMTreeWalker* self)
249 {
250     WebCore::JSMainThreadNullState state;
251     g_return_val_if_fail(WEBKIT_DOM_IS_TREE_WALKER(self), 0);
252     WebCore::TreeWalker* item = WebKit::core(self);
253
254     auto result = item->previousNode();
255     if (result.hasException())
256         return nullptr;
257
258     RefPtr<WebCore::Node> gobjectResult = WTF::getPtr(result.releaseReturnValue());
259     return WebKit::kit(gobjectResult.get());
260 }
261
262 WebKitDOMNode* webkit_dom_tree_walker_next_node(WebKitDOMTreeWalker* self)
263 {
264     WebCore::JSMainThreadNullState state;
265     g_return_val_if_fail(WEBKIT_DOM_IS_TREE_WALKER(self), 0);
266     WebCore::TreeWalker* item = WebKit::core(self);
267
268     auto result = item->nextNode();
269     if (result.hasException())
270         return nullptr;
271
272     RefPtr<WebCore::Node> gobjectResult = WTF::getPtr(result.releaseReturnValue());
273     return WebKit::kit(gobjectResult.get());
274 }
275
276 WebKitDOMNode* webkit_dom_tree_walker_get_root(WebKitDOMTreeWalker* self)
277 {
278     WebCore::JSMainThreadNullState state;
279     g_return_val_if_fail(WEBKIT_DOM_IS_TREE_WALKER(self), 0);
280     WebCore::TreeWalker* item = WebKit::core(self);
281     RefPtr<WebCore::Node> gobjectResult = WTF::getPtr(item->root());
282     return WebKit::kit(gobjectResult.get());
283 }
284
285 gulong webkit_dom_tree_walker_get_what_to_show(WebKitDOMTreeWalker* self)
286 {
287     WebCore::JSMainThreadNullState state;
288     g_return_val_if_fail(WEBKIT_DOM_IS_TREE_WALKER(self), 0);
289     WebCore::TreeWalker* item = WebKit::core(self);
290     gulong result = item->whatToShow();
291     return result;
292 }
293
294 WebKitDOMNodeFilter* webkit_dom_tree_walker_get_filter(WebKitDOMTreeWalker* self)
295 {
296     WebCore::JSMainThreadNullState state;
297     g_return_val_if_fail(WEBKIT_DOM_IS_TREE_WALKER(self), 0);
298     WebCore::TreeWalker* item = WebKit::core(self);
299     RefPtr<WebCore::NodeFilter> gobjectResult = WTF::getPtr(item->filter());
300     return WebKit::kit(gobjectResult.get());
301 }
302
303 WebKitDOMNode* webkit_dom_tree_walker_get_current_node(WebKitDOMTreeWalker* self)
304 {
305     WebCore::JSMainThreadNullState state;
306     g_return_val_if_fail(WEBKIT_DOM_IS_TREE_WALKER(self), 0);
307     WebCore::TreeWalker* item = WebKit::core(self);
308     RefPtr<WebCore::Node> gobjectResult = WTF::getPtr(item->currentNode());
309     return WebKit::kit(gobjectResult.get());
310 }
311
312 void webkit_dom_tree_walker_set_current_node(WebKitDOMTreeWalker* self, WebKitDOMNode* value, GError** error)
313 {
314     WebCore::JSMainThreadNullState state;
315     g_return_if_fail(WEBKIT_DOM_IS_TREE_WALKER(self));
316     g_return_if_fail(WEBKIT_DOM_IS_NODE(value));
317     UNUSED_PARAM(error);
318     WebCore::TreeWalker* item = WebKit::core(self);
319     WebCore::Node* convertedValue = WebKit::core(value);
320     item->setCurrentNode(*convertedValue);
321 }
322