Drop ExceptionCodeDescription class
[WebKit-https.git] / Source / WebKit / WebProcess / InjectedBundle / API / gtk / DOM / WebKitDOMXPathResult.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 "WebKitDOMXPathResult.h"
22
23 #include <WebCore/CSSImportRule.h>
24 #include "DOMObjectCache.h"
25 #include <WebCore/DOMException.h>
26 #include <WebCore/Document.h>
27 #include <WebCore/JSMainThreadExecState.h>
28 #include "WebKitDOMNodePrivate.h"
29 #include "WebKitDOMPrivate.h"
30 #include "WebKitDOMXPathResultPrivate.h"
31 #include "ConvertToUTF8String.h"
32 #include <wtf/GetPtr.h>
33 #include <wtf/RefPtr.h>
34
35 #define WEBKIT_DOM_XPATH_RESULT_GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE(obj, WEBKIT_DOM_TYPE_XPATH_RESULT, WebKitDOMXPathResultPrivate)
36
37 typedef struct _WebKitDOMXPathResultPrivate {
38     RefPtr<WebCore::XPathResult> coreObject;
39 } WebKitDOMXPathResultPrivate;
40
41 namespace WebKit {
42
43 WebKitDOMXPathResult* kit(WebCore::XPathResult* obj)
44 {
45     if (!obj)
46         return 0;
47
48     if (gpointer ret = DOMObjectCache::get(obj))
49         return WEBKIT_DOM_XPATH_RESULT(ret);
50
51     return wrapXPathResult(obj);
52 }
53
54 WebCore::XPathResult* core(WebKitDOMXPathResult* request)
55 {
56     return request ? static_cast<WebCore::XPathResult*>(WEBKIT_DOM_OBJECT(request)->coreObject) : 0;
57 }
58
59 WebKitDOMXPathResult* wrapXPathResult(WebCore::XPathResult* coreObject)
60 {
61     ASSERT(coreObject);
62     return WEBKIT_DOM_XPATH_RESULT(g_object_new(WEBKIT_DOM_TYPE_XPATH_RESULT, "core-object", coreObject, nullptr));
63 }
64
65 } // namespace WebKit
66
67 G_DEFINE_TYPE(WebKitDOMXPathResult, webkit_dom_xpath_result, WEBKIT_DOM_TYPE_OBJECT)
68
69 enum {
70     PROP_0,
71     PROP_RESULT_TYPE,
72     PROP_NUMBER_VALUE,
73     PROP_STRING_VALUE,
74     PROP_BOOLEAN_VALUE,
75     PROP_SINGLE_NODE_VALUE,
76     PROP_INVALID_ITERATOR_STATE,
77     PROP_SNAPSHOT_LENGTH,
78 };
79
80 static void webkit_dom_xpath_result_finalize(GObject* object)
81 {
82     WebKitDOMXPathResultPrivate* priv = WEBKIT_DOM_XPATH_RESULT_GET_PRIVATE(object);
83
84     WebKit::DOMObjectCache::forget(priv->coreObject.get());
85
86     priv->~WebKitDOMXPathResultPrivate();
87     G_OBJECT_CLASS(webkit_dom_xpath_result_parent_class)->finalize(object);
88 }
89
90 static void webkit_dom_xpath_result_get_property(GObject* object, guint propertyId, GValue* value, GParamSpec* pspec)
91 {
92     WebKitDOMXPathResult* self = WEBKIT_DOM_XPATH_RESULT(object);
93
94     switch (propertyId) {
95     case PROP_RESULT_TYPE:
96         g_value_set_uint(value, webkit_dom_xpath_result_get_result_type(self));
97         break;
98     case PROP_NUMBER_VALUE:
99         g_value_set_double(value, webkit_dom_xpath_result_get_number_value(self, nullptr));
100         break;
101     case PROP_STRING_VALUE:
102         g_value_take_string(value, webkit_dom_xpath_result_get_string_value(self, nullptr));
103         break;
104     case PROP_BOOLEAN_VALUE:
105         g_value_set_boolean(value, webkit_dom_xpath_result_get_boolean_value(self, nullptr));
106         break;
107     case PROP_SINGLE_NODE_VALUE:
108         g_value_set_object(value, webkit_dom_xpath_result_get_single_node_value(self, nullptr));
109         break;
110     case PROP_INVALID_ITERATOR_STATE:
111         g_value_set_boolean(value, webkit_dom_xpath_result_get_invalid_iterator_state(self));
112         break;
113     case PROP_SNAPSHOT_LENGTH:
114         g_value_set_ulong(value, webkit_dom_xpath_result_get_snapshot_length(self, nullptr));
115         break;
116     default:
117         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
118         break;
119     }
120 }
121
122 static GObject* webkit_dom_xpath_result_constructor(GType type, guint constructPropertiesCount, GObjectConstructParam* constructProperties)
123 {
124     GObject* object = G_OBJECT_CLASS(webkit_dom_xpath_result_parent_class)->constructor(type, constructPropertiesCount, constructProperties);
125
126     WebKitDOMXPathResultPrivate* priv = WEBKIT_DOM_XPATH_RESULT_GET_PRIVATE(object);
127     priv->coreObject = static_cast<WebCore::XPathResult*>(WEBKIT_DOM_OBJECT(object)->coreObject);
128     WebKit::DOMObjectCache::put(priv->coreObject.get(), object);
129
130     return object;
131 }
132
133 static void webkit_dom_xpath_result_class_init(WebKitDOMXPathResultClass* requestClass)
134 {
135     GObjectClass* gobjectClass = G_OBJECT_CLASS(requestClass);
136     g_type_class_add_private(gobjectClass, sizeof(WebKitDOMXPathResultPrivate));
137     gobjectClass->constructor = webkit_dom_xpath_result_constructor;
138     gobjectClass->finalize = webkit_dom_xpath_result_finalize;
139     gobjectClass->get_property = webkit_dom_xpath_result_get_property;
140
141     g_object_class_install_property(
142         gobjectClass,
143         PROP_RESULT_TYPE,
144         g_param_spec_uint(
145             "result-type",
146             "XPathResult:result-type",
147             "read-only gushort XPathResult:result-type",
148             0, G_MAXUINT, 0,
149             WEBKIT_PARAM_READABLE));
150
151     g_object_class_install_property(
152         gobjectClass,
153         PROP_NUMBER_VALUE,
154         g_param_spec_double(
155             "number-value",
156             "XPathResult:number-value",
157             "read-only gdouble XPathResult:number-value",
158             -G_MAXDOUBLE, G_MAXDOUBLE, 0,
159             WEBKIT_PARAM_READABLE));
160
161     g_object_class_install_property(
162         gobjectClass,
163         PROP_STRING_VALUE,
164         g_param_spec_string(
165             "string-value",
166             "XPathResult:string-value",
167             "read-only gchar* XPathResult:string-value",
168             "",
169             WEBKIT_PARAM_READABLE));
170
171     g_object_class_install_property(
172         gobjectClass,
173         PROP_BOOLEAN_VALUE,
174         g_param_spec_boolean(
175             "boolean-value",
176             "XPathResult:boolean-value",
177             "read-only gboolean XPathResult:boolean-value",
178             FALSE,
179             WEBKIT_PARAM_READABLE));
180
181     g_object_class_install_property(
182         gobjectClass,
183         PROP_SINGLE_NODE_VALUE,
184         g_param_spec_object(
185             "single-node-value",
186             "XPathResult:single-node-value",
187             "read-only WebKitDOMNode* XPathResult:single-node-value",
188             WEBKIT_DOM_TYPE_NODE,
189             WEBKIT_PARAM_READABLE));
190
191     g_object_class_install_property(
192         gobjectClass,
193         PROP_INVALID_ITERATOR_STATE,
194         g_param_spec_boolean(
195             "invalid-iterator-state",
196             "XPathResult:invalid-iterator-state",
197             "read-only gboolean XPathResult:invalid-iterator-state",
198             FALSE,
199             WEBKIT_PARAM_READABLE));
200
201     g_object_class_install_property(
202         gobjectClass,
203         PROP_SNAPSHOT_LENGTH,
204         g_param_spec_ulong(
205             "snapshot-length",
206             "XPathResult:snapshot-length",
207             "read-only gulong XPathResult:snapshot-length",
208             0, G_MAXULONG, 0,
209             WEBKIT_PARAM_READABLE));
210
211 }
212
213 static void webkit_dom_xpath_result_init(WebKitDOMXPathResult* request)
214 {
215     WebKitDOMXPathResultPrivate* priv = WEBKIT_DOM_XPATH_RESULT_GET_PRIVATE(request);
216     new (priv) WebKitDOMXPathResultPrivate();
217 }
218
219 WebKitDOMNode* webkit_dom_xpath_result_iterate_next(WebKitDOMXPathResult* self, GError** error)
220 {
221     WebCore::JSMainThreadNullState state;
222     g_return_val_if_fail(WEBKIT_DOM_IS_XPATH_RESULT(self), 0);
223     g_return_val_if_fail(!error || !*error, 0);
224     WebCore::XPathResult* item = WebKit::core(self);
225     auto result = item->iterateNext();
226     if (result.hasException()) {
227         auto description = WebCore::DOMException::description(result.releaseException().code());
228         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
229         return nullptr;
230     }
231     return WebKit::kit(result.releaseReturnValue());
232 }
233
234 WebKitDOMNode* webkit_dom_xpath_result_snapshot_item(WebKitDOMXPathResult* self, gulong index, GError** error)
235 {
236     WebCore::JSMainThreadNullState state;
237     g_return_val_if_fail(WEBKIT_DOM_IS_XPATH_RESULT(self), 0);
238     g_return_val_if_fail(!error || !*error, 0);
239     WebCore::XPathResult* item = WebKit::core(self);
240     auto result = item->snapshotItem(index);
241     if (result.hasException()) {
242         auto description = WebCore::DOMException::description(result.releaseException().code());
243         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
244         return nullptr;
245     }
246     return WebKit::kit(result.releaseReturnValue());
247 }
248
249 gushort webkit_dom_xpath_result_get_result_type(WebKitDOMXPathResult* self)
250 {
251     WebCore::JSMainThreadNullState state;
252     g_return_val_if_fail(WEBKIT_DOM_IS_XPATH_RESULT(self), 0);
253     WebCore::XPathResult* item = WebKit::core(self);
254     gushort result = item->resultType();
255     return result;
256 }
257
258 gdouble webkit_dom_xpath_result_get_number_value(WebKitDOMXPathResult* self, GError** error)
259 {
260     WebCore::JSMainThreadNullState state;
261     g_return_val_if_fail(WEBKIT_DOM_IS_XPATH_RESULT(self), 0);
262     g_return_val_if_fail(!error || !*error, 0);
263     WebCore::XPathResult* item = WebKit::core(self);
264     auto result = item->numberValue();
265     if (result.hasException()) {
266         auto description = WebCore::DOMException::description(result.releaseException().code());
267         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
268         return 0;
269     }
270     return result.releaseReturnValue();
271 }
272
273 gchar* webkit_dom_xpath_result_get_string_value(WebKitDOMXPathResult* self, GError** error)
274 {
275     WebCore::JSMainThreadNullState state;
276     g_return_val_if_fail(WEBKIT_DOM_IS_XPATH_RESULT(self), 0);
277     g_return_val_if_fail(!error || !*error, 0);
278     WebCore::XPathResult* item = WebKit::core(self);
279     auto result = item->stringValue();
280     if (result.hasException())
281         return nullptr;
282     return convertToUTF8String(result.releaseReturnValue());
283 }
284
285 gboolean webkit_dom_xpath_result_get_boolean_value(WebKitDOMXPathResult* self, GError** error)
286 {
287     WebCore::JSMainThreadNullState state;
288     g_return_val_if_fail(WEBKIT_DOM_IS_XPATH_RESULT(self), FALSE);
289     g_return_val_if_fail(!error || !*error, FALSE);
290     WebCore::XPathResult* item = WebKit::core(self);
291     auto result = item->booleanValue();
292     if (result.hasException()) {
293         auto description = WebCore::DOMException::description(result.releaseException().code());
294         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
295         return false;
296     }
297     return result.releaseReturnValue();
298 }
299
300 WebKitDOMNode* webkit_dom_xpath_result_get_single_node_value(WebKitDOMXPathResult* self, GError** error)
301 {
302     WebCore::JSMainThreadNullState state;
303     g_return_val_if_fail(WEBKIT_DOM_IS_XPATH_RESULT(self), 0);
304     g_return_val_if_fail(!error || !*error, 0);
305     WebCore::XPathResult* item = WebKit::core(self);
306     auto result = item->singleNodeValue();
307     if (result.hasException()) {
308         auto description = WebCore::DOMException::description(result.releaseException().code());
309         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
310         return nullptr;
311     }
312     return WebKit::kit(result.releaseReturnValue());
313 }
314
315 gboolean webkit_dom_xpath_result_get_invalid_iterator_state(WebKitDOMXPathResult* self)
316 {
317     WebCore::JSMainThreadNullState state;
318     g_return_val_if_fail(WEBKIT_DOM_IS_XPATH_RESULT(self), FALSE);
319     WebCore::XPathResult* item = WebKit::core(self);
320     gboolean result = item->invalidIteratorState();
321     return result;
322 }
323
324 gulong webkit_dom_xpath_result_get_snapshot_length(WebKitDOMXPathResult* self, GError** error)
325 {
326     WebCore::JSMainThreadNullState state;
327     g_return_val_if_fail(WEBKIT_DOM_IS_XPATH_RESULT(self), 0);
328     g_return_val_if_fail(!error || !*error, 0);
329     WebCore::XPathResult* item = WebKit::core(self);
330     auto result = item->snapshotLength();
331     if (result.hasException()) {
332         auto description = WebCore::DOMException::description(result.releaseException().code());
333         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
334     }
335     return result.releaseReturnValue();
336 }
337