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 <WebCore/JSExecState.h> |
25 | #include "WebKitDOMObject.h" |
26 | #include "WebKitDOMXPathNSResolverPrivate.h" |
27 | #include "ConvertToUTF8String.h" |
28 | #include <wtf/GetPtr.h> |
29 | #include <wtf/RefPtr.h> |
30 | |
31 | G_GNUC_BEGIN_IGNORE_DEPRECATIONS; |
32 | |
33 | typedef WebKitDOMXPathNSResolverIface WebKitDOMXPathNSResolverInterface; |
34 | |
35 | G_DEFINE_INTERFACE(WebKitDOMXPathNSResolver, webkit_dom_xpath_ns_resolver, G_TYPE_OBJECT) |
36 | |
37 | static void webkit_dom_xpath_ns_resolver_default_init(WebKitDOMXPathNSResolverIface*) |
38 | { |
39 | } |
40 | |
41 | char* webkit_dom_xpath_ns_resolver_lookup_namespace_uri(WebKitDOMXPathNSResolver* resolver, const char* prefix) |
42 | { |
43 | g_return_val_if_fail(WEBKIT_DOM_IS_XPATH_NS_RESOLVER(resolver), nullptr); |
44 | g_return_val_if_fail(prefix, nullptr); |
45 | |
46 | return WEBKIT_DOM_XPATH_NS_RESOLVER_GET_IFACE(resolver)->lookup_namespace_uri(resolver, prefix); |
47 | } |
48 | |
49 | // WebKitDOMNativeXPathNSResolver. |
50 | struct _WebKitDOMNativeXPathNSResolver { |
51 | WebKitDOMObject parent; |
52 | }; |
53 | |
54 | struct _WebKitDOMNativeXPathNSResolverClass { |
55 | WebKitDOMObjectClass parentClass; |
56 | }; |
57 | |
58 | typedef struct _WebKitDOMNativeXPathNSResolverPrivate { |
59 | RefPtr<WebCore::XPathNSResolver> coreObject; |
60 | } WebKitDOMNativeXPathNSResolverPrivate; |
61 | |
62 | #define WEBKIT_DOM_NATIVE_XPATH_NS_RESOLVER_GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE(obj, WEBKIT_DOM_TYPE_NATIVE_XPATH_NS_RESOLVER, WebKitDOMNativeXPathNSResolverPrivate) |
63 | |
64 | static void webkitDOMXPathNSResolverIfaceInit(WebKitDOMXPathNSResolverIface*); |
65 | |
66 | G_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)) |
67 | |
68 | static void webkitDOMNativeXPathNSResolverFinalize(GObject* object) |
69 | { |
70 | WebKitDOMNativeXPathNSResolverPrivate* priv = WEBKIT_DOM_NATIVE_XPATH_NS_RESOLVER_GET_PRIVATE(object); |
71 | priv->~WebKitDOMNativeXPathNSResolverPrivate(); |
72 | G_OBJECT_CLASS(webkit_dom_native_xpath_ns_resolver_parent_class)->finalize(object); |
73 | } |
74 | |
75 | static GObject* webkitDOMNativeXPathNSResolverConstructor(GType type, guint constructPropertiesCount, GObjectConstructParam* constructProperties) |
76 | { |
77 | GObject* object = G_OBJECT_CLASS(webkit_dom_native_xpath_ns_resolver_parent_class)->constructor(type, constructPropertiesCount, constructProperties); |
78 | WebKitDOMNativeXPathNSResolverPrivate* priv = WEBKIT_DOM_NATIVE_XPATH_NS_RESOLVER_GET_PRIVATE(object); |
79 | priv->coreObject = static_cast<WebCore::XPathNSResolver*>(WEBKIT_DOM_OBJECT(object)->coreObject); |
80 | WebKit::DOMObjectCache::put(priv->coreObject.get(), object); |
81 | return object; |
82 | } |
83 | |
84 | static void webkit_dom_native_xpath_ns_resolver_init(WebKitDOMNativeXPathNSResolver* resolver) |
85 | { |
86 | WebKitDOMNativeXPathNSResolverPrivate* priv = WEBKIT_DOM_NATIVE_XPATH_NS_RESOLVER_GET_PRIVATE(resolver); |
87 | new (priv) WebKitDOMNativeXPathNSResolverPrivate(); |
88 | } |
89 | |
90 | static void webkit_dom_native_xpath_ns_resolver_class_init(WebKitDOMNativeXPathNSResolverClass* klass) |
91 | { |
92 | GObjectClass* gobjectClass = G_OBJECT_CLASS(klass); |
93 | g_type_class_add_private(gobjectClass, sizeof(WebKitDOMNativeXPathNSResolverPrivate)); |
94 | gobjectClass->constructor = webkitDOMNativeXPathNSResolverConstructor; |
95 | gobjectClass->finalize = webkitDOMNativeXPathNSResolverFinalize; |
96 | } |
97 | |
98 | static char* webkitDOMNativeXPathNSResolverLookupNamespaceURI(WebKitDOMXPathNSResolver* resolver, const char* prefix) |
99 | { |
100 | WebCore::JSMainThreadNullState state; |
101 | g_return_val_if_fail(WEBKIT_DOM_IS_NATIVE_XPATH_NS_RESOLVER(resolver), nullptr); |
102 | |
103 | return convertToUTF8String(WebKit::core(resolver)->lookupNamespaceURI(WTF::String::fromUTF8(prefix))); |
104 | } |
105 | |
106 | static void webkitDOMXPathNSResolverIfaceInit(WebKitDOMXPathNSResolverIface* iface) |
107 | { |
108 | iface->lookup_namespace_uri = webkitDOMNativeXPathNSResolverLookupNamespaceURI; |
109 | } |
110 | |
111 | namespace WebKit { |
112 | |
113 | RefPtr<WebCore::XPathNSResolver> core(WebKitDOMXPathNSResolver* xPathNSResolver) |
114 | { |
115 | if (!xPathNSResolver) |
116 | return nullptr; |
117 | |
118 | RefPtr<WebCore::XPathNSResolver> coreResolver; |
119 | if (WEBKIT_DOM_IS_NATIVE_XPATH_NS_RESOLVER(xPathNSResolver)) |
120 | coreResolver = core(WEBKIT_DOM_NATIVE_XPATH_NS_RESOLVER(xPathNSResolver)); |
121 | else |
122 | coreResolver = WebKit::GObjectXPathNSResolver::create(xPathNSResolver); |
123 | return coreResolver; |
124 | } |
125 | |
126 | WebKitDOMXPathNSResolver* kit(WebCore::XPathNSResolver* coreXPathNSResolver) |
127 | { |
128 | if (!coreXPathNSResolver) |
129 | return nullptr; |
130 | |
131 | if (gpointer ret = DOMObjectCache::get(coreXPathNSResolver)) |
132 | return WEBKIT_DOM_XPATH_NS_RESOLVER(ret); |
133 | |
134 | return WEBKIT_DOM_XPATH_NS_RESOLVER(g_object_new(WEBKIT_DOM_TYPE_NATIVE_XPATH_NS_RESOLVER, "core-object" , coreXPathNSResolver, nullptr)); |
135 | } |
136 | |
137 | WebCore::XPathNSResolver* core(WebKitDOMNativeXPathNSResolver* xPathNSResolver) |
138 | { |
139 | return xPathNSResolver ? static_cast<WebCore::XPathNSResolver*>(WEBKIT_DOM_OBJECT(xPathNSResolver)->coreObject) : nullptr; |
140 | } |
141 | |
142 | } // namespace WebKit |
143 | G_GNUC_END_IGNORE_DEPRECATIONS; |
144 | |