| 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 "WebKitDOMCSSRule.h" |
| 22 | |
| 23 | #include <WebCore/CSSImportRule.h> |
| 24 | #include "DOMObjectCache.h" |
| 25 | #include <WebCore/DOMException.h> |
| 26 | #include <WebCore/Document.h> |
| 27 | #include <WebCore/JSExecState.h> |
| 28 | #include "WebKitDOMCSSRulePrivate.h" |
| 29 | #include "WebKitDOMCSSStyleSheetPrivate.h" |
| 30 | #include "WebKitDOMPrivate.h" |
| 31 | #include "ConvertToUTF8String.h" |
| 32 | #include <wtf/GetPtr.h> |
| 33 | #include <wtf/RefPtr.h> |
| 34 | |
| 35 | #define WEBKIT_DOM_CSS_RULE_GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE(obj, WEBKIT_DOM_TYPE_CSS_RULE, WebKitDOMCSSRulePrivate) |
| 36 | |
| 37 | typedef struct _WebKitDOMCSSRulePrivate { |
| 38 | RefPtr<WebCore::CSSRule> coreObject; |
| 39 | } WebKitDOMCSSRulePrivate; |
| 40 | |
| 41 | G_GNUC_BEGIN_IGNORE_DEPRECATIONS; |
| 42 | |
| 43 | namespace WebKit { |
| 44 | |
| 45 | WebKitDOMCSSRule* kit(WebCore::CSSRule* obj) |
| 46 | { |
| 47 | if (!obj) |
| 48 | return 0; |
| 49 | |
| 50 | if (gpointer ret = DOMObjectCache::get(obj)) |
| 51 | return WEBKIT_DOM_CSS_RULE(ret); |
| 52 | |
| 53 | return wrapCSSRule(obj); |
| 54 | } |
| 55 | |
| 56 | WebCore::CSSRule* core(WebKitDOMCSSRule* request) |
| 57 | { |
| 58 | return request ? static_cast<WebCore::CSSRule*>(WEBKIT_DOM_OBJECT(request)->coreObject) : 0; |
| 59 | } |
| 60 | |
| 61 | WebKitDOMCSSRule* wrapCSSRule(WebCore::CSSRule* coreObject) |
| 62 | { |
| 63 | ASSERT(coreObject); |
| 64 | return WEBKIT_DOM_CSS_RULE(g_object_new(WEBKIT_DOM_TYPE_CSS_RULE, "core-object" , coreObject, nullptr)); |
| 65 | } |
| 66 | |
| 67 | } // namespace WebKit |
| 68 | |
| 69 | G_DEFINE_TYPE(WebKitDOMCSSRule, webkit_dom_css_rule, WEBKIT_DOM_TYPE_OBJECT) |
| 70 | |
| 71 | enum { |
| 72 | DOM_CSS_RULE_PROP_0, |
| 73 | DOM_CSS_RULE_PROP_TYPE, |
| 74 | DOM_CSS_RULE_PROP_CSS_TEXT, |
| 75 | DOM_CSS_RULE_PROP_PARENT_STYLE_SHEET, |
| 76 | DOM_CSS_RULE_PROP_PARENT_RULE, |
| 77 | }; |
| 78 | |
| 79 | static void webkit_dom_css_rule_finalize(GObject* object) |
| 80 | { |
| 81 | WebKitDOMCSSRulePrivate* priv = WEBKIT_DOM_CSS_RULE_GET_PRIVATE(object); |
| 82 | |
| 83 | WebKit::DOMObjectCache::forget(priv->coreObject.get()); |
| 84 | |
| 85 | priv->~WebKitDOMCSSRulePrivate(); |
| 86 | G_OBJECT_CLASS(webkit_dom_css_rule_parent_class)->finalize(object); |
| 87 | } |
| 88 | |
| 89 | static void webkit_dom_css_rule_set_property(GObject* object, guint propertyId, const GValue* value, GParamSpec* pspec) |
| 90 | { |
| 91 | WebKitDOMCSSRule* self = WEBKIT_DOM_CSS_RULE(object); |
| 92 | |
| 93 | switch (propertyId) { |
| 94 | case DOM_CSS_RULE_PROP_CSS_TEXT: |
| 95 | webkit_dom_css_rule_set_css_text(self, g_value_get_string(value), nullptr); |
| 96 | break; |
| 97 | default: |
| 98 | G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec); |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | static void webkit_dom_css_rule_get_property(GObject* object, guint propertyId, GValue* value, GParamSpec* pspec) |
| 104 | { |
| 105 | WebKitDOMCSSRule* self = WEBKIT_DOM_CSS_RULE(object); |
| 106 | |
| 107 | switch (propertyId) { |
| 108 | case DOM_CSS_RULE_PROP_TYPE: |
| 109 | g_value_set_uint(value, webkit_dom_css_rule_get_rule_type(self)); |
| 110 | break; |
| 111 | case DOM_CSS_RULE_PROP_CSS_TEXT: |
| 112 | g_value_take_string(value, webkit_dom_css_rule_get_css_text(self)); |
| 113 | break; |
| 114 | case DOM_CSS_RULE_PROP_PARENT_STYLE_SHEET: |
| 115 | g_value_set_object(value, webkit_dom_css_rule_get_parent_style_sheet(self)); |
| 116 | break; |
| 117 | case DOM_CSS_RULE_PROP_PARENT_RULE: |
| 118 | g_value_set_object(value, webkit_dom_css_rule_get_parent_rule(self)); |
| 119 | break; |
| 120 | default: |
| 121 | G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec); |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | static GObject* webkit_dom_css_rule_constructor(GType type, guint constructPropertiesCount, GObjectConstructParam* constructProperties) |
| 127 | { |
| 128 | GObject* object = G_OBJECT_CLASS(webkit_dom_css_rule_parent_class)->constructor(type, constructPropertiesCount, constructProperties); |
| 129 | |
| 130 | WebKitDOMCSSRulePrivate* priv = WEBKIT_DOM_CSS_RULE_GET_PRIVATE(object); |
| 131 | priv->coreObject = static_cast<WebCore::CSSRule*>(WEBKIT_DOM_OBJECT(object)->coreObject); |
| 132 | WebKit::DOMObjectCache::put(priv->coreObject.get(), object); |
| 133 | |
| 134 | return object; |
| 135 | } |
| 136 | |
| 137 | static void webkit_dom_css_rule_class_init(WebKitDOMCSSRuleClass* requestClass) |
| 138 | { |
| 139 | GObjectClass* gobjectClass = G_OBJECT_CLASS(requestClass); |
| 140 | g_type_class_add_private(gobjectClass, sizeof(WebKitDOMCSSRulePrivate)); |
| 141 | gobjectClass->constructor = webkit_dom_css_rule_constructor; |
| 142 | gobjectClass->finalize = webkit_dom_css_rule_finalize; |
| 143 | gobjectClass->set_property = webkit_dom_css_rule_set_property; |
| 144 | gobjectClass->get_property = webkit_dom_css_rule_get_property; |
| 145 | |
| 146 | g_object_class_install_property( |
| 147 | gobjectClass, |
| 148 | DOM_CSS_RULE_PROP_TYPE, |
| 149 | g_param_spec_uint( |
| 150 | "type" , |
| 151 | "CSSRule:type" , |
| 152 | "read-only gushort CSSRule:type" , |
| 153 | 0, G_MAXUINT, 0, |
| 154 | WEBKIT_PARAM_READABLE)); |
| 155 | |
| 156 | g_object_class_install_property( |
| 157 | gobjectClass, |
| 158 | DOM_CSS_RULE_PROP_CSS_TEXT, |
| 159 | g_param_spec_string( |
| 160 | "css-text" , |
| 161 | "CSSRule:css-text" , |
| 162 | "read-write gchar* CSSRule:css-text" , |
| 163 | "" , |
| 164 | WEBKIT_PARAM_READWRITE)); |
| 165 | |
| 166 | g_object_class_install_property( |
| 167 | gobjectClass, |
| 168 | DOM_CSS_RULE_PROP_PARENT_STYLE_SHEET, |
| 169 | g_param_spec_object( |
| 170 | "parent-style-sheet" , |
| 171 | "CSSRule:parent-style-sheet" , |
| 172 | "read-only WebKitDOMCSSStyleSheet* CSSRule:parent-style-sheet" , |
| 173 | WEBKIT_DOM_TYPE_CSS_STYLE_SHEET, |
| 174 | WEBKIT_PARAM_READABLE)); |
| 175 | |
| 176 | g_object_class_install_property( |
| 177 | gobjectClass, |
| 178 | DOM_CSS_RULE_PROP_PARENT_RULE, |
| 179 | g_param_spec_object( |
| 180 | "parent-rule" , |
| 181 | "CSSRule:parent-rule" , |
| 182 | "read-only WebKitDOMCSSRule* CSSRule:parent-rule" , |
| 183 | WEBKIT_DOM_TYPE_CSS_RULE, |
| 184 | WEBKIT_PARAM_READABLE)); |
| 185 | |
| 186 | } |
| 187 | |
| 188 | static void webkit_dom_css_rule_init(WebKitDOMCSSRule* request) |
| 189 | { |
| 190 | WebKitDOMCSSRulePrivate* priv = WEBKIT_DOM_CSS_RULE_GET_PRIVATE(request); |
| 191 | new (priv) WebKitDOMCSSRulePrivate(); |
| 192 | } |
| 193 | |
| 194 | gushort webkit_dom_css_rule_get_rule_type(WebKitDOMCSSRule* self) |
| 195 | { |
| 196 | WebCore::JSMainThreadNullState state; |
| 197 | g_return_val_if_fail(WEBKIT_DOM_IS_CSS_RULE(self), 0); |
| 198 | WebCore::CSSRule* item = WebKit::core(self); |
| 199 | gushort result = item->type(); |
| 200 | return result; |
| 201 | } |
| 202 | |
| 203 | gchar* webkit_dom_css_rule_get_css_text(WebKitDOMCSSRule* self) |
| 204 | { |
| 205 | WebCore::JSMainThreadNullState state; |
| 206 | g_return_val_if_fail(WEBKIT_DOM_IS_CSS_RULE(self), 0); |
| 207 | WebCore::CSSRule* item = WebKit::core(self); |
| 208 | gchar* result = convertToUTF8String(item->cssText()); |
| 209 | return result; |
| 210 | } |
| 211 | |
| 212 | void webkit_dom_css_rule_set_css_text(WebKitDOMCSSRule* self, const gchar* value, GError** error) |
| 213 | { |
| 214 | WebCore::JSMainThreadNullState state; |
| 215 | g_return_if_fail(WEBKIT_DOM_IS_CSS_RULE(self)); |
| 216 | g_return_if_fail(value); |
| 217 | g_return_if_fail(!error || !*error); |
| 218 | WebCore::CSSRule* item = WebKit::core(self); |
| 219 | WTF::String convertedValue = WTF::String::fromUTF8(value); |
| 220 | auto result = item->setCssText(convertedValue); |
| 221 | if (result.hasException()) { |
| 222 | auto description = WebCore::DOMException::description(result.releaseException().code()); |
| 223 | g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM" ), description.legacyCode, description.name); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | WebKitDOMCSSStyleSheet* webkit_dom_css_rule_get_parent_style_sheet(WebKitDOMCSSRule* self) |
| 228 | { |
| 229 | WebCore::JSMainThreadNullState state; |
| 230 | g_return_val_if_fail(WEBKIT_DOM_IS_CSS_RULE(self), 0); |
| 231 | WebCore::CSSRule* item = WebKit::core(self); |
| 232 | RefPtr<WebCore::CSSStyleSheet> gobjectResult = WTF::getPtr(item->parentStyleSheet()); |
| 233 | return WebKit::kit(gobjectResult.get()); |
| 234 | } |
| 235 | |
| 236 | WebKitDOMCSSRule* webkit_dom_css_rule_get_parent_rule(WebKitDOMCSSRule* self) |
| 237 | { |
| 238 | WebCore::JSMainThreadNullState state; |
| 239 | g_return_val_if_fail(WEBKIT_DOM_IS_CSS_RULE(self), 0); |
| 240 | WebCore::CSSRule* item = WebKit::core(self); |
| 241 | RefPtr<WebCore::CSSRule> gobjectResult = WTF::getPtr(item->parentRule()); |
| 242 | return WebKit::kit(gobjectResult.get()); |
| 243 | } |
| 244 | |
| 245 | G_GNUC_END_IGNORE_DEPRECATIONS; |
| 246 | |