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 "WebKitDOMXPathExpression.h"
22
23#include <WebCore/CSSImportRule.h>
24#include "DOMObjectCache.h"
25#include <WebCore/DOMException.h>
26#include <WebCore/Document.h>
27#include <WebCore/JSExecState.h>
28#include "WebKitDOMNodePrivate.h"
29#include "WebKitDOMPrivate.h"
30#include "WebKitDOMXPathExpressionPrivate.h"
31#include "WebKitDOMXPathResultPrivate.h"
32#include "ConvertToUTF8String.h"
33#include <wtf/GetPtr.h>
34#include <wtf/RefPtr.h>
35
36#define WEBKIT_DOM_XPATH_EXPRESSION_GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE(obj, WEBKIT_DOM_TYPE_XPATH_EXPRESSION, WebKitDOMXPathExpressionPrivate)
37
38typedef struct _WebKitDOMXPathExpressionPrivate {
39 RefPtr<WebCore::XPathExpression> coreObject;
40} WebKitDOMXPathExpressionPrivate;
41
42G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
43
44namespace WebKit {
45
46WebKitDOMXPathExpression* kit(WebCore::XPathExpression* obj)
47{
48 if (!obj)
49 return 0;
50
51 if (gpointer ret = DOMObjectCache::get(obj))
52 return WEBKIT_DOM_XPATH_EXPRESSION(ret);
53
54 return wrapXPathExpression(obj);
55}
56
57WebCore::XPathExpression* core(WebKitDOMXPathExpression* request)
58{
59 return request ? static_cast<WebCore::XPathExpression*>(WEBKIT_DOM_OBJECT(request)->coreObject) : 0;
60}
61
62WebKitDOMXPathExpression* wrapXPathExpression(WebCore::XPathExpression* coreObject)
63{
64 ASSERT(coreObject);
65 return WEBKIT_DOM_XPATH_EXPRESSION(g_object_new(WEBKIT_DOM_TYPE_XPATH_EXPRESSION, "core-object", coreObject, nullptr));
66}
67
68} // namespace WebKit
69
70G_DEFINE_TYPE(WebKitDOMXPathExpression, webkit_dom_xpath_expression, WEBKIT_DOM_TYPE_OBJECT)
71
72static void webkit_dom_xpath_expression_finalize(GObject* object)
73{
74 WebKitDOMXPathExpressionPrivate* priv = WEBKIT_DOM_XPATH_EXPRESSION_GET_PRIVATE(object);
75
76 WebKit::DOMObjectCache::forget(priv->coreObject.get());
77
78 priv->~WebKitDOMXPathExpressionPrivate();
79 G_OBJECT_CLASS(webkit_dom_xpath_expression_parent_class)->finalize(object);
80}
81
82static GObject* webkit_dom_xpath_expression_constructor(GType type, guint constructPropertiesCount, GObjectConstructParam* constructProperties)
83{
84 GObject* object = G_OBJECT_CLASS(webkit_dom_xpath_expression_parent_class)->constructor(type, constructPropertiesCount, constructProperties);
85
86 WebKitDOMXPathExpressionPrivate* priv = WEBKIT_DOM_XPATH_EXPRESSION_GET_PRIVATE(object);
87 priv->coreObject = static_cast<WebCore::XPathExpression*>(WEBKIT_DOM_OBJECT(object)->coreObject);
88 WebKit::DOMObjectCache::put(priv->coreObject.get(), object);
89
90 return object;
91}
92
93static void webkit_dom_xpath_expression_class_init(WebKitDOMXPathExpressionClass* requestClass)
94{
95 GObjectClass* gobjectClass = G_OBJECT_CLASS(requestClass);
96 g_type_class_add_private(gobjectClass, sizeof(WebKitDOMXPathExpressionPrivate));
97 gobjectClass->constructor = webkit_dom_xpath_expression_constructor;
98 gobjectClass->finalize = webkit_dom_xpath_expression_finalize;
99}
100
101static void webkit_dom_xpath_expression_init(WebKitDOMXPathExpression* request)
102{
103 WebKitDOMXPathExpressionPrivate* priv = WEBKIT_DOM_XPATH_EXPRESSION_GET_PRIVATE(request);
104 new (priv) WebKitDOMXPathExpressionPrivate();
105}
106
107WebKitDOMXPathResult* webkit_dom_xpath_expression_evaluate(WebKitDOMXPathExpression* self, WebKitDOMNode* contextNode, gushort type, WebKitDOMXPathResult* inResult, GError** error)
108{
109 WebCore::JSMainThreadNullState state;
110 g_return_val_if_fail(WEBKIT_DOM_IS_XPATH_EXPRESSION(self), 0);
111 g_return_val_if_fail(WEBKIT_DOM_IS_NODE(contextNode), 0);
112 g_return_val_if_fail(WEBKIT_DOM_IS_XPATH_RESULT(inResult), 0);
113 g_return_val_if_fail(!error || !*error, 0);
114 WebCore::XPathExpression* item = WebKit::core(self);
115 WebCore::Node* convertedContextNode = WebKit::core(contextNode);
116 WebCore::XPathResult* convertedInResult = WebKit::core(inResult);
117 auto result = item->evaluate(convertedContextNode, type, convertedInResult);
118 if (result.hasException()) {
119 auto description = WebCore::DOMException::description(result.releaseException().code());
120 g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
121 return nullptr;
122 }
123 return WebKit::kit(result.releaseReturnValue().ptr());
124}
125
126G_GNUC_END_IGNORE_DEPRECATIONS;
127