1/*
2 * Copyright (C) 2014 Igalia S.L.
3 *
4 *  This library is free software; you can redistribute it and/or
5 *  modify it under the terms of the GNU Lesser 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 *  Lesser General Public License for more details.
13 *
14 *  You should have received a copy of the GNU Lesser General Public
15 *  License along with this library; if not, write to the Free Software
16 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19#include "config.h"
20#include "WebKitDOMXPathNSResolver.h"
21
22#include "DOMObjectCache.h"
23#include "GObjectXPathNSResolver.h"
24#include "JSMainThreadExecState.h"
25#include "WebKitDOMObject.h"
26#include "WebKitDOMXPathNSResolverPrivate.h"
27#include "gobject/ConvertToUTF8String.h"
28#include <wtf/GetPtr.h>
29#include <wtf/RefPtr.h>
30
31typedef WebKitDOMXPathNSResolverIface WebKitDOMXPathNSResolverInterface;
32
33G_DEFINE_INTERFACE(WebKitDOMXPathNSResolver, webkit_dom_xpath_ns_resolver, G_TYPE_OBJECT)
34
35static void webkit_dom_xpath_ns_resolver_default_init(WebKitDOMXPathNSResolverIface*)
36{
37}
38
39char* webkit_dom_xpath_ns_resolver_lookup_namespace_uri(WebKitDOMXPathNSResolver* resolver, const char* prefix)
40{
41    g_return_val_if_fail(WEBKIT_DOM_IS_XPATH_NS_RESOLVER(resolver), nullptr);
42    g_return_val_if_fail(prefix, nullptr);
43
44    return WEBKIT_DOM_XPATH_NS_RESOLVER_GET_IFACE(resolver)->lookup_namespace_uri(resolver, prefix);
45}
46
47// WebKitDOMNativeXPathNSResolver.
48struct _WebKitDOMNativeXPathNSResolver {
49    WebKitDOMObject parent;
50};
51
52struct _WebKitDOMNativeXPathNSResolverClass {
53    WebKitDOMObjectClass parentClass;
54};
55
56typedef struct _WebKitDOMNativeXPathNSResolverPrivate {
57    RefPtr<WebCore::XPathNSResolver> coreObject;
58} WebKitDOMNativeXPathNSResolverPrivate;
59
60#define WEBKIT_DOM_NATIVE_XPATH_NS_RESOLVER_GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE(obj, WEBKIT_DOM_TYPE_NATIVE_XPATH_NS_RESOLVER, WebKitDOMNativeXPathNSResolverPrivate)
61
62static void webkitDOMXPathNSResolverIfaceInit(WebKitDOMXPathNSResolverIface*);
63
64G_DEFINE_TYPE_WITH_CODE(WebKitDOMNativeXPathNSResolver, webkit_dom_native_xpath_ns_resolver, WEBKIT_DOM_TYPE_OBJECT, G_IMPLEMENT_INTERFACE(WEBKIT_DOM_TYPE_XPATH_NS_RESOLVER, webkitDOMXPathNSResolverIfaceInit))
65
66static void webkitDOMNativeXPathNSResolverFinalize(GObject* object)
67{
68    WebKitDOMNativeXPathNSResolverPrivate* priv = WEBKIT_DOM_NATIVE_XPATH_NS_RESOLVER_GET_PRIVATE(object);
69    priv->~WebKitDOMNativeXPathNSResolverPrivate();
70    G_OBJECT_CLASS(webkit_dom_native_xpath_ns_resolver_parent_class)->finalize(object);
71}
72
73static GObject* webkitDOMNativeXPathNSResolverConstructor(GType type, guint constructPropertiesCount, GObjectConstructParam* constructProperties)
74{
75    GObject* object = G_OBJECT_CLASS(webkit_dom_native_xpath_ns_resolver_parent_class)->constructor(type, constructPropertiesCount, constructProperties);
76    WebKitDOMNativeXPathNSResolverPrivate* priv = WEBKIT_DOM_NATIVE_XPATH_NS_RESOLVER_GET_PRIVATE(object);
77    priv->coreObject = static_cast<WebCore::XPathNSResolver*>(WEBKIT_DOM_OBJECT(object)->coreObject);
78    WebKit::DOMObjectCache::put(priv->coreObject.get(), object);
79    return object;
80}
81
82static void webkit_dom_native_xpath_ns_resolver_init(WebKitDOMNativeXPathNSResolver* resolver)
83{
84    WebKitDOMNativeXPathNSResolverPrivate* priv = WEBKIT_DOM_NATIVE_XPATH_NS_RESOLVER_GET_PRIVATE(resolver);
85    new (priv) WebKitDOMNativeXPathNSResolverPrivate();
86}
87
88static void webkit_dom_native_xpath_ns_resolver_class_init(WebKitDOMNativeXPathNSResolverClass* klass)
89{
90    GObjectClass* gobjectClass = G_OBJECT_CLASS(klass);
91    g_type_class_add_private(gobjectClass, sizeof(WebKitDOMNativeXPathNSResolverPrivate));
92    gobjectClass->constructor = webkitDOMNativeXPathNSResolverConstructor;
93    gobjectClass->finalize = webkitDOMNativeXPathNSResolverFinalize;
94}
95
96static char* webkitDOMNativeXPathNSResolverLookupNamespaceURI(WebKitDOMXPathNSResolver* resolver, const char* prefix)
97{
98    WebCore::JSMainThreadNullState state;
99    g_return_val_if_fail(WEBKIT_DOM_IS_NATIVE_XPATH_NS_RESOLVER(resolver), nullptr);
100
101    return convertToUTF8String(WebKit::core(resolver)->lookupNamespaceURI(WTF::String::fromUTF8(prefix)));
102}
103
104static void webkitDOMXPathNSResolverIfaceInit(WebKitDOMXPathNSResolverIface* iface)
105{
106    iface->lookup_namespace_uri = webkitDOMNativeXPathNSResolverLookupNamespaceURI;
107}
108
109namespace WebKit {
110
111PassRefPtr<WebCore::XPathNSResolver> core(WebKitDOMXPathNSResolver* xPathNSResolver)
112{
113    if (!xPathNSResolver)
114        return nullptr;
115
116    RefPtr<WebCore::XPathNSResolver> coreResolver;
117    if (WEBKIT_DOM_IS_NATIVE_XPATH_NS_RESOLVER(xPathNSResolver))
118        coreResolver = core(WEBKIT_DOM_NATIVE_XPATH_NS_RESOLVER(xPathNSResolver));
119    else
120        coreResolver = WebCore::GObjectXPathNSResolver::create(xPathNSResolver);
121    return coreResolver.release();
122}
123
124WebKitDOMXPathNSResolver* kit(WebCore::XPathNSResolver* coreXPathNSResolver)
125{
126    if (!coreXPathNSResolver)
127        return nullptr;
128
129    if (gpointer ret = DOMObjectCache::get(coreXPathNSResolver))
130        return WEBKIT_DOM_XPATH_NS_RESOLVER(ret);
131
132    return WEBKIT_DOM_XPATH_NS_RESOLVER(g_object_new(WEBKIT_DOM_TYPE_NATIVE_XPATH_NS_RESOLVER, "core-object", coreXPathNSResolver, nullptr));
133}
134
135WebCore::XPathNSResolver* core(WebKitDOMNativeXPathNSResolver* xPathNSResolver)
136{
137    return xPathNSResolver ? static_cast<WebCore::XPathNSResolver*>(WEBKIT_DOM_OBJECT(xPathNSResolver)->coreObject) : nullptr;
138}
139
140} // namespace WebKit
141