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