Drop ExceptionCodeDescription class
[WebKit-https.git] / Source / WebKit / WebProcess / InjectedBundle / API / gtk / DOM / WebKitDOMCSSStyleSheet.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 "WebKitDOMCSSStyleSheet.h"
22
23 #include <WebCore/CSSImportRule.h>
24 #include "DOMObjectCache.h"
25 #include <WebCore/DOMException.h>
26 #include <WebCore/Document.h>
27 #include <WebCore/JSMainThreadExecState.h>
28 #include "WebKitDOMCSSRuleListPrivate.h"
29 #include "WebKitDOMCSSRulePrivate.h"
30 #include "WebKitDOMCSSStyleSheetPrivate.h"
31 #include "WebKitDOMPrivate.h"
32 #include "WebKitDOMStyleSheetPrivate.h"
33 #include "ConvertToUTF8String.h"
34 #include <wtf/GetPtr.h>
35 #include <wtf/RefPtr.h>
36
37 namespace WebKit {
38
39 WebKitDOMCSSStyleSheet* kit(WebCore::CSSStyleSheet* obj)
40 {
41     return WEBKIT_DOM_CSS_STYLE_SHEET(kit(static_cast<WebCore::StyleSheet*>(obj)));
42 }
43
44 WebCore::CSSStyleSheet* core(WebKitDOMCSSStyleSheet* request)
45 {
46     return request ? static_cast<WebCore::CSSStyleSheet*>(WEBKIT_DOM_OBJECT(request)->coreObject) : 0;
47 }
48
49 WebKitDOMCSSStyleSheet* wrapCSSStyleSheet(WebCore::CSSStyleSheet* coreObject)
50 {
51     ASSERT(coreObject);
52     return WEBKIT_DOM_CSS_STYLE_SHEET(g_object_new(WEBKIT_DOM_TYPE_CSS_STYLE_SHEET, "core-object", coreObject, nullptr));
53 }
54
55 } // namespace WebKit
56
57 G_DEFINE_TYPE(WebKitDOMCSSStyleSheet, webkit_dom_css_style_sheet, WEBKIT_DOM_TYPE_STYLE_SHEET)
58
59 enum {
60     PROP_0,
61     PROP_OWNER_RULE,
62     PROP_CSS_RULES,
63     PROP_RULES,
64 };
65
66 static void webkit_dom_css_style_sheet_get_property(GObject* object, guint propertyId, GValue* value, GParamSpec* pspec)
67 {
68     WebKitDOMCSSStyleSheet* self = WEBKIT_DOM_CSS_STYLE_SHEET(object);
69
70     switch (propertyId) {
71     case PROP_OWNER_RULE:
72         g_value_set_object(value, webkit_dom_css_style_sheet_get_owner_rule(self));
73         break;
74     case PROP_CSS_RULES:
75         g_value_set_object(value, webkit_dom_css_style_sheet_get_css_rules(self));
76         break;
77     case PROP_RULES:
78         g_value_set_object(value, webkit_dom_css_style_sheet_get_rules(self));
79         break;
80     default:
81         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
82         break;
83     }
84 }
85
86 static void webkit_dom_css_style_sheet_class_init(WebKitDOMCSSStyleSheetClass* requestClass)
87 {
88     GObjectClass* gobjectClass = G_OBJECT_CLASS(requestClass);
89     gobjectClass->get_property = webkit_dom_css_style_sheet_get_property;
90
91     g_object_class_install_property(
92         gobjectClass,
93         PROP_OWNER_RULE,
94         g_param_spec_object(
95             "owner-rule",
96             "CSSStyleSheet:owner-rule",
97             "read-only WebKitDOMCSSRule* CSSStyleSheet:owner-rule",
98             WEBKIT_DOM_TYPE_CSS_RULE,
99             WEBKIT_PARAM_READABLE));
100
101     g_object_class_install_property(
102         gobjectClass,
103         PROP_CSS_RULES,
104         g_param_spec_object(
105             "css-rules",
106             "CSSStyleSheet:css-rules",
107             "read-only WebKitDOMCSSRuleList* CSSStyleSheet:css-rules",
108             WEBKIT_DOM_TYPE_CSS_RULE_LIST,
109             WEBKIT_PARAM_READABLE));
110
111     g_object_class_install_property(
112         gobjectClass,
113         PROP_RULES,
114         g_param_spec_object(
115             "rules",
116             "CSSStyleSheet:rules",
117             "read-only WebKitDOMCSSRuleList* CSSStyleSheet:rules",
118             WEBKIT_DOM_TYPE_CSS_RULE_LIST,
119             WEBKIT_PARAM_READABLE));
120
121 }
122
123 static void webkit_dom_css_style_sheet_init(WebKitDOMCSSStyleSheet* request)
124 {
125     UNUSED_PARAM(request);
126 }
127
128 gulong webkit_dom_css_style_sheet_insert_rule(WebKitDOMCSSStyleSheet* self, const gchar* rule, gulong index, GError** error)
129 {
130     WebCore::JSMainThreadNullState state;
131     g_return_val_if_fail(WEBKIT_DOM_IS_CSS_STYLE_SHEET(self), 0);
132     g_return_val_if_fail(rule, 0);
133     g_return_val_if_fail(!error || !*error, 0);
134     WebCore::CSSStyleSheet* item = WebKit::core(self);
135     WTF::String convertedRule = WTF::String::fromUTF8(rule);
136     auto result = item->insertRule(convertedRule, index);
137     if (result.hasException()) {
138         auto description = WebCore::DOMException::description(result.releaseException().code());
139         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
140         return 0;
141     }
142     return result.releaseReturnValue();
143 }
144
145 void webkit_dom_css_style_sheet_delete_rule(WebKitDOMCSSStyleSheet* self, gulong index, GError** error)
146 {
147     WebCore::JSMainThreadNullState state;
148     g_return_if_fail(WEBKIT_DOM_IS_CSS_STYLE_SHEET(self));
149     g_return_if_fail(!error || !*error);
150     WebCore::CSSStyleSheet* item = WebKit::core(self);
151     auto result = item->deleteRule(index);
152     if (result.hasException()) {
153         auto description = WebCore::DOMException::description(result.releaseException().code());
154         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
155     }
156 }
157
158 glong webkit_dom_css_style_sheet_add_rule(WebKitDOMCSSStyleSheet* self, const gchar* selector, const gchar* style, gulong index, GError** error)
159 {
160     WebCore::JSMainThreadNullState state;
161     g_return_val_if_fail(WEBKIT_DOM_IS_CSS_STYLE_SHEET(self), 0);
162     g_return_val_if_fail(selector, 0);
163     g_return_val_if_fail(style, 0);
164     g_return_val_if_fail(!error || !*error, 0);
165     WebCore::CSSStyleSheet* item = WebKit::core(self);
166     WTF::String convertedSelector = WTF::String::fromUTF8(selector);
167     WTF::String convertedStyle = WTF::String::fromUTF8(style);
168     auto result = item->addRule(convertedSelector, convertedStyle, index);
169     if (result.hasException()) {
170         auto description = WebCore::DOMException::description(result.releaseException().code());
171         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
172         return -1;
173     }
174     return result.releaseReturnValue();
175 }
176
177 void webkit_dom_css_style_sheet_remove_rule(WebKitDOMCSSStyleSheet* self, gulong index, GError** error)
178 {
179     WebCore::JSMainThreadNullState state;
180     g_return_if_fail(WEBKIT_DOM_IS_CSS_STYLE_SHEET(self));
181     g_return_if_fail(!error || !*error);
182     WebCore::CSSStyleSheet* item = WebKit::core(self);
183     auto result = item->removeRule(index);
184     if (result.hasException()) {
185         auto description = WebCore::DOMException::description(result.releaseException().code());
186         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
187     }
188 }
189
190 WebKitDOMCSSRule* webkit_dom_css_style_sheet_get_owner_rule(WebKitDOMCSSStyleSheet* self)
191 {
192     WebCore::JSMainThreadNullState state;
193     g_return_val_if_fail(WEBKIT_DOM_IS_CSS_STYLE_SHEET(self), 0);
194     WebCore::CSSStyleSheet* item = WebKit::core(self);
195     RefPtr<WebCore::CSSRule> gobjectResult = WTF::getPtr(item->ownerRule());
196     return WebKit::kit(gobjectResult.get());
197 }
198
199 WebKitDOMCSSRuleList* webkit_dom_css_style_sheet_get_css_rules(WebKitDOMCSSStyleSheet* self)
200 {
201     WebCore::JSMainThreadNullState state;
202     g_return_val_if_fail(WEBKIT_DOM_IS_CSS_STYLE_SHEET(self), 0);
203     WebCore::CSSStyleSheet* item = WebKit::core(self);
204     RefPtr<WebCore::CSSRuleList> gobjectResult = WTF::getPtr(item->cssRules());
205     return WebKit::kit(gobjectResult.get());
206 }
207
208 WebKitDOMCSSRuleList* webkit_dom_css_style_sheet_get_rules(WebKitDOMCSSStyleSheet* self)
209 {
210     WebCore::JSMainThreadNullState state;
211     g_return_val_if_fail(WEBKIT_DOM_IS_CSS_STYLE_SHEET(self), 0);
212     WebCore::CSSStyleSheet* item = WebKit::core(self);
213     RefPtr<WebCore::CSSRuleList> gobjectResult = WTF::getPtr(item->rules());
214     return WebKit::kit(gobjectResult.get());
215 }
216