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 "WebKitDOMDOMImplementation.h"
22
23#include "ConvertToUTF8String.h"
24#include "DOMObjectCache.h"
25#include "WebKitDOMCSSStyleSheetPrivate.h"
26#include "WebKitDOMDOMImplementationPrivate.h"
27#include "WebKitDOMDocumentPrivate.h"
28#include "WebKitDOMDocumentTypePrivate.h"
29#include "WebKitDOMHTMLDocumentPrivate.h"
30#include "WebKitDOMPrivate.h"
31#include <WebCore/CSSImportRule.h>
32#include <WebCore/DOMException.h>
33#include <WebCore/Document.h>
34#include <WebCore/JSExecState.h>
35#include <WebCore/SVGTests.h>
36#include <wtf/GetPtr.h>
37#include <wtf/RefPtr.h>
38
39#define WEBKIT_DOM_DOM_IMPLEMENTATION_GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE(obj, WEBKIT_DOM_TYPE_DOM_IMPLEMENTATION, WebKitDOMDOMImplementationPrivate)
40
41typedef struct _WebKitDOMDOMImplementationPrivate {
42 RefPtr<WebCore::DOMImplementation> coreObject;
43} WebKitDOMDOMImplementationPrivate;
44
45G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
46
47namespace WebKit {
48
49WebKitDOMDOMImplementation* kit(WebCore::DOMImplementation* obj)
50{
51 if (!obj)
52 return 0;
53
54 if (gpointer ret = DOMObjectCache::get(obj))
55 return WEBKIT_DOM_DOM_IMPLEMENTATION(ret);
56
57 return wrapDOMImplementation(obj);
58}
59
60WebCore::DOMImplementation* core(WebKitDOMDOMImplementation* request)
61{
62 return request ? static_cast<WebCore::DOMImplementation*>(WEBKIT_DOM_OBJECT(request)->coreObject) : 0;
63}
64
65WebKitDOMDOMImplementation* wrapDOMImplementation(WebCore::DOMImplementation* coreObject)
66{
67 ASSERT(coreObject);
68 return WEBKIT_DOM_DOM_IMPLEMENTATION(g_object_new(WEBKIT_DOM_TYPE_DOM_IMPLEMENTATION, "core-object", coreObject, nullptr));
69}
70
71} // namespace WebKit
72
73G_DEFINE_TYPE(WebKitDOMDOMImplementation, webkit_dom_dom_implementation, WEBKIT_DOM_TYPE_OBJECT)
74
75static void webkit_dom_dom_implementation_finalize(GObject* object)
76{
77 WebKitDOMDOMImplementationPrivate* priv = WEBKIT_DOM_DOM_IMPLEMENTATION_GET_PRIVATE(object);
78
79 WebKit::DOMObjectCache::forget(priv->coreObject.get());
80
81 priv->~WebKitDOMDOMImplementationPrivate();
82 G_OBJECT_CLASS(webkit_dom_dom_implementation_parent_class)->finalize(object);
83}
84
85static GObject* webkit_dom_dom_implementation_constructor(GType type, guint constructPropertiesCount, GObjectConstructParam* constructProperties)
86{
87 GObject* object = G_OBJECT_CLASS(webkit_dom_dom_implementation_parent_class)->constructor(type, constructPropertiesCount, constructProperties);
88
89 WebKitDOMDOMImplementationPrivate* priv = WEBKIT_DOM_DOM_IMPLEMENTATION_GET_PRIVATE(object);
90 priv->coreObject = static_cast<WebCore::DOMImplementation*>(WEBKIT_DOM_OBJECT(object)->coreObject);
91 WebKit::DOMObjectCache::put(priv->coreObject.get(), object);
92
93 return object;
94}
95
96static void webkit_dom_dom_implementation_class_init(WebKitDOMDOMImplementationClass* requestClass)
97{
98 GObjectClass* gobjectClass = G_OBJECT_CLASS(requestClass);
99 g_type_class_add_private(gobjectClass, sizeof(WebKitDOMDOMImplementationPrivate));
100 gobjectClass->constructor = webkit_dom_dom_implementation_constructor;
101 gobjectClass->finalize = webkit_dom_dom_implementation_finalize;
102}
103
104static void webkit_dom_dom_implementation_init(WebKitDOMDOMImplementation* request)
105{
106 WebKitDOMDOMImplementationPrivate* priv = WEBKIT_DOM_DOM_IMPLEMENTATION_GET_PRIVATE(request);
107 new (priv) WebKitDOMDOMImplementationPrivate();
108}
109
110gboolean webkit_dom_dom_implementation_has_feature(WebKitDOMDOMImplementation* self, const gchar* feature, const gchar* version)
111{
112 g_return_val_if_fail(WEBKIT_DOM_IS_DOM_IMPLEMENTATION(self), FALSE);
113 g_return_val_if_fail(feature, FALSE);
114 g_return_val_if_fail(version, FALSE);
115 WTF::String convertedFeature = WTF::String::fromUTF8(feature);
116 WTF::String convertedVersion = WTF::String::fromUTF8(version);
117 return WebCore::SVGTests::hasFeatureForLegacyBindings(convertedFeature, convertedVersion);
118}
119
120WebKitDOMDocumentType* webkit_dom_dom_implementation_create_document_type(WebKitDOMDOMImplementation* self, const gchar* qualifiedName, const gchar* publicId, const gchar* systemId, GError** error)
121{
122 WebCore::JSMainThreadNullState state;
123 g_return_val_if_fail(WEBKIT_DOM_IS_DOM_IMPLEMENTATION(self), 0);
124 g_return_val_if_fail(qualifiedName, 0);
125 g_return_val_if_fail(publicId, 0);
126 g_return_val_if_fail(systemId, 0);
127 g_return_val_if_fail(!error || !*error, 0);
128 WebCore::DOMImplementation* item = WebKit::core(self);
129 WTF::String convertedQualifiedName = WTF::String::fromUTF8(qualifiedName);
130 WTF::String convertedPublicId = WTF::String::fromUTF8(publicId);
131 WTF::String convertedSystemId = WTF::String::fromUTF8(systemId);
132 auto result = item->createDocumentType(convertedQualifiedName, convertedPublicId, convertedSystemId);
133 if (result.hasException()) {
134 auto description = WebCore::DOMException::description(result.releaseException().code());
135 g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
136 return nullptr;
137 }
138 return WebKit::kit(result.releaseReturnValue().ptr());
139}
140
141WebKitDOMDocument* webkit_dom_dom_implementation_create_document(WebKitDOMDOMImplementation* self, const gchar* namespaceURI, const gchar* qualifiedName, WebKitDOMDocumentType* doctype, GError** error)
142{
143 WebCore::JSMainThreadNullState state;
144 g_return_val_if_fail(WEBKIT_DOM_IS_DOM_IMPLEMENTATION(self), 0);
145 g_return_val_if_fail(qualifiedName, 0);
146 g_return_val_if_fail(!doctype || WEBKIT_DOM_IS_DOCUMENT_TYPE(doctype), 0);
147 g_return_val_if_fail(!error || !*error, 0);
148 WebCore::DOMImplementation* item = WebKit::core(self);
149 WTF::String convertedNamespaceURI = WTF::String::fromUTF8(namespaceURI);
150 WTF::String convertedQualifiedName = WTF::String::fromUTF8(qualifiedName);
151 WebCore::DocumentType* convertedDoctype = WebKit::core(doctype);
152 auto result = item->createDocument(convertedNamespaceURI, convertedQualifiedName, convertedDoctype);
153 if (result.hasException()) {
154 auto description = WebCore::DOMException::description(result.releaseException().code());
155 g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
156 return nullptr;
157 }
158 return WebKit::kit(result.releaseReturnValue().ptr());
159}
160
161WebKitDOMCSSStyleSheet* webkit_dom_dom_implementation_create_css_style_sheet(WebKitDOMDOMImplementation* self, const gchar* title, const gchar* media, GError** error)
162{
163 WebCore::JSMainThreadNullState state;
164 g_return_val_if_fail(WEBKIT_DOM_IS_DOM_IMPLEMENTATION(self), 0);
165 g_return_val_if_fail(title, 0);
166 g_return_val_if_fail(media, 0);
167 g_return_val_if_fail(!error || !*error, 0);
168 WebCore::DOMImplementation* item = WebKit::core(self);
169 WTF::String convertedTitle = WTF::String::fromUTF8(title);
170 WTF::String convertedMedia = WTF::String::fromUTF8(media);
171 RefPtr<WebCore::CSSStyleSheet> gobjectResult = WTF::getPtr(item->createCSSStyleSheet(convertedTitle, convertedMedia));
172 return WebKit::kit(gobjectResult.get());
173}
174
175WebKitDOMHTMLDocument* webkit_dom_dom_implementation_create_html_document(WebKitDOMDOMImplementation* self, const gchar* title)
176{
177 WebCore::JSMainThreadNullState state;
178 g_return_val_if_fail(WEBKIT_DOM_IS_DOM_IMPLEMENTATION(self), 0);
179 g_return_val_if_fail(title, 0);
180 WebCore::DOMImplementation* item = WebKit::core(self);
181 WTF::String convertedTitle = WTF::String::fromUTF8(title);
182 RefPtr<WebCore::HTMLDocument> gobjectResult = WTF::getPtr(item->createHTMLDocument(convertedTitle));
183 return WebKit::kit(gobjectResult.get());
184}
185
186G_GNUC_END_IGNORE_DEPRECATIONS;
187