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