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