1/*
2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * (C) 2002-2003 Dirk Mueller (mueller@kde.org)
4 * Copyright (C) 2002, 2006, 2008, 2012, 2013 Apple Inc. All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB.  If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22#ifndef StyleRule_h
23#define StyleRule_h
24
25#include "core/css/CSSSelectorList.h"
26#include "core/css/MediaList.h"
27#include "platform/heap/Handle.h"
28#include "wtf/RefPtr.h"
29
30namespace blink {
31
32class CSSRule;
33class CSSStyleSheet;
34class MutableStylePropertySet;
35class StylePropertySet;
36
37class StyleRuleBase : public RefCountedWillBeGarbageCollectedFinalized<StyleRuleBase> {
38    WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
39public:
40    enum Type {
41        Unknown, // Not used.
42        Style,
43        Charset, // Not used. These are internally strings owned by the style sheet.
44        Import,
45        Media,
46        FontFace,
47        Page,
48        Keyframes,
49        Keyframe, // Not used. These are internally non-rule StyleKeyframe objects.
50        Supports = 12,
51        Viewport = 15,
52        Filter = 17
53    };
54
55    Type type() const { return static_cast<Type>(m_type); }
56
57    bool isCharsetRule() const { return type() == Charset; }
58    bool isFontFaceRule() const { return type() == FontFace; }
59    bool isKeyframesRule() const { return type() == Keyframes; }
60    bool isMediaRule() const { return type() == Media; }
61    bool isPageRule() const { return type() == Page; }
62    bool isStyleRule() const { return type() == Style; }
63    bool isSupportsRule() const { return type() == Supports; }
64    bool isViewportRule() const { return type() == Viewport; }
65    bool isImportRule() const { return type() == Import; }
66    bool isFilterRule() const { return type() == Filter; }
67
68    PassRefPtrWillBeRawPtr<StyleRuleBase> copy() const;
69
70#if !ENABLE(OILPAN)
71    void deref()
72    {
73        if (derefBase())
74            destroy();
75    }
76#endif // !ENABLE(OILPAN)
77
78    // FIXME: There shouldn't be any need for the null parent version.
79    PassRefPtrWillBeRawPtr<CSSRule> createCSSOMWrapper(CSSStyleSheet* parentSheet = 0) const;
80    PassRefPtrWillBeRawPtr<CSSRule> createCSSOMWrapper(CSSRule* parentRule) const;
81
82    void trace(Visitor*);
83    void traceAfterDispatch(Visitor*) { };
84    void finalizeGarbageCollectedObject();
85
86protected:
87    StyleRuleBase(Type type) : m_type(type) { }
88    StyleRuleBase(const StyleRuleBase& o) : m_type(o.m_type) { }
89
90    ~StyleRuleBase() { }
91
92private:
93    void destroy();
94
95    PassRefPtrWillBeRawPtr<CSSRule> createCSSOMWrapper(CSSStyleSheet* parentSheet, CSSRule* parentRule) const;
96
97    unsigned m_type : 5;
98};
99
100class StyleRule : public StyleRuleBase {
101    WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
102public:
103    static PassRefPtrWillBeRawPtr<StyleRule> create() { return adoptRefWillBeNoop(new StyleRule()); }
104
105    ~StyleRule();
106
107    const CSSSelectorList& selectorList() const { return m_selectorList; }
108    const StylePropertySet& properties() const { return *m_properties; }
109    MutableStylePropertySet& mutableProperties();
110
111    void parserAdoptSelectorVector(Vector<OwnPtr<CSSParserSelector> >& selectors) { m_selectorList.adoptSelectorVector(selectors); }
112    void wrapperAdoptSelectorList(CSSSelectorList& selectors) { m_selectorList.adopt(selectors); }
113    void setProperties(PassRefPtrWillBeRawPtr<StylePropertySet>);
114
115    PassRefPtrWillBeRawPtr<StyleRule> copy() const { return adoptRefWillBeNoop(new StyleRule(*this)); }
116
117    static unsigned averageSizeInBytes();
118
119    void traceAfterDispatch(Visitor*);
120
121private:
122    StyleRule();
123    StyleRule(const StyleRule&);
124
125    RefPtrWillBeMember<StylePropertySet> m_properties; // Cannot be null.
126    CSSSelectorList m_selectorList;
127};
128
129class StyleRuleFontFace : public StyleRuleBase {
130public:
131    static PassRefPtrWillBeRawPtr<StyleRuleFontFace> create() { return adoptRefWillBeNoop(new StyleRuleFontFace); }
132
133    ~StyleRuleFontFace();
134
135    const StylePropertySet& properties() const { return *m_properties; }
136    MutableStylePropertySet& mutableProperties();
137
138    void setProperties(PassRefPtrWillBeRawPtr<StylePropertySet>);
139
140    PassRefPtrWillBeRawPtr<StyleRuleFontFace> copy() const { return adoptRefWillBeNoop(new StyleRuleFontFace(*this)); }
141
142    void traceAfterDispatch(Visitor*);
143
144private:
145    StyleRuleFontFace();
146    StyleRuleFontFace(const StyleRuleFontFace&);
147
148    RefPtrWillBeMember<StylePropertySet> m_properties; // Cannot be null.
149};
150
151class StyleRulePage : public StyleRuleBase {
152public:
153    static PassRefPtrWillBeRawPtr<StyleRulePage> create() { return adoptRefWillBeNoop(new StyleRulePage); }
154
155    ~StyleRulePage();
156
157    const CSSSelector* selector() const { return m_selectorList.first(); }
158    const StylePropertySet& properties() const { return *m_properties; }
159    MutableStylePropertySet& mutableProperties();
160
161    void parserAdoptSelectorVector(Vector<OwnPtr<CSSParserSelector> >& selectors) { m_selectorList.adoptSelectorVector(selectors); }
162    void wrapperAdoptSelectorList(CSSSelectorList& selectors) { m_selectorList.adopt(selectors); }
163    void setProperties(PassRefPtrWillBeRawPtr<StylePropertySet>);
164
165    PassRefPtrWillBeRawPtr<StyleRulePage> copy() const { return adoptRefWillBeNoop(new StyleRulePage(*this)); }
166
167    void traceAfterDispatch(Visitor*);
168
169private:
170    StyleRulePage();
171    StyleRulePage(const StyleRulePage&);
172
173    RefPtrWillBeMember<StylePropertySet> m_properties; // Cannot be null.
174    CSSSelectorList m_selectorList;
175};
176
177class StyleRuleGroup : public StyleRuleBase {
178public:
179    const WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> >& childRules() const { return m_childRules; }
180
181    void wrapperInsertRule(unsigned, PassRefPtrWillBeRawPtr<StyleRuleBase>);
182    void wrapperRemoveRule(unsigned);
183
184    void traceAfterDispatch(Visitor*);
185
186protected:
187    StyleRuleGroup(Type, WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> >& adoptRule);
188    StyleRuleGroup(const StyleRuleGroup&);
189
190private:
191    WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> > m_childRules;
192};
193
194class StyleRuleMedia : public StyleRuleGroup {
195public:
196    static PassRefPtrWillBeRawPtr<StyleRuleMedia> create(PassRefPtrWillBeRawPtr<MediaQuerySet> media, WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> >& adoptRules)
197    {
198        return adoptRefWillBeNoop(new StyleRuleMedia(media, adoptRules));
199    }
200
201    MediaQuerySet* mediaQueries() const { return m_mediaQueries.get(); }
202
203    PassRefPtrWillBeRawPtr<StyleRuleMedia> copy() const { return adoptRefWillBeNoop(new StyleRuleMedia(*this)); }
204
205    void traceAfterDispatch(Visitor*);
206
207private:
208    StyleRuleMedia(PassRefPtrWillBeRawPtr<MediaQuerySet>, WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> >& adoptRules);
209    StyleRuleMedia(const StyleRuleMedia&);
210
211    RefPtrWillBeMember<MediaQuerySet> m_mediaQueries;
212};
213
214class StyleRuleSupports : public StyleRuleGroup {
215public:
216    static PassRefPtrWillBeRawPtr<StyleRuleSupports> create(const String& conditionText, bool conditionIsSupported, WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> >& adoptRules)
217    {
218        return adoptRefWillBeNoop(new StyleRuleSupports(conditionText, conditionIsSupported, adoptRules));
219    }
220
221    String conditionText() const { return m_conditionText; }
222    bool conditionIsSupported() const { return m_conditionIsSupported; }
223    PassRefPtrWillBeRawPtr<StyleRuleSupports> copy() const { return adoptRefWillBeNoop(new StyleRuleSupports(*this)); }
224
225    void traceAfterDispatch(Visitor* visitor) { StyleRuleGroup::traceAfterDispatch(visitor); }
226
227private:
228    StyleRuleSupports(const String& conditionText, bool conditionIsSupported, WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> >& adoptRules);
229    StyleRuleSupports(const StyleRuleSupports&);
230
231    String m_conditionText;
232    bool m_conditionIsSupported;
233};
234
235class StyleRuleViewport : public StyleRuleBase {
236public:
237    static PassRefPtrWillBeRawPtr<StyleRuleViewport> create() { return adoptRefWillBeNoop(new StyleRuleViewport); }
238
239    ~StyleRuleViewport();
240
241    const StylePropertySet& properties() const { return *m_properties; }
242    MutableStylePropertySet& mutableProperties();
243
244    void setProperties(PassRefPtrWillBeRawPtr<StylePropertySet>);
245
246    PassRefPtrWillBeRawPtr<StyleRuleViewport> copy() const { return adoptRefWillBeNoop(new StyleRuleViewport(*this)); }
247
248    void traceAfterDispatch(Visitor*);
249
250private:
251    StyleRuleViewport();
252    StyleRuleViewport(const StyleRuleViewport&);
253
254    RefPtrWillBeMember<StylePropertySet> m_properties; // Cannot be null
255};
256
257class StyleRuleFilter : public StyleRuleBase {
258public:
259    static PassRefPtrWillBeRawPtr<StyleRuleFilter> create(const String& filterName) { return adoptRefWillBeNoop(new StyleRuleFilter(filterName)); }
260
261    ~StyleRuleFilter();
262
263    const String& filterName() const { return m_filterName; }
264
265    const StylePropertySet& properties() const { return *m_properties; }
266    MutableStylePropertySet& mutableProperties();
267
268    void setProperties(PassRefPtrWillBeRawPtr<StylePropertySet>);
269
270    PassRefPtrWillBeRawPtr<StyleRuleFilter> copy() const { return adoptRefWillBeNoop(new StyleRuleFilter(*this)); }
271
272    void traceAfterDispatch(Visitor*);
273
274private:
275    StyleRuleFilter(const String&);
276    StyleRuleFilter(const StyleRuleFilter&);
277
278    String m_filterName;
279    RefPtrWillBeMember<StylePropertySet> m_properties;
280};
281
282#define DEFINE_STYLE_RULE_TYPE_CASTS(Type) \
283    DEFINE_TYPE_CASTS(StyleRule##Type, StyleRuleBase, rule, rule->is##Type##Rule(), rule.is##Type##Rule())
284
285DEFINE_TYPE_CASTS(StyleRule, StyleRuleBase, rule, rule->isStyleRule(), rule.isStyleRule());
286DEFINE_STYLE_RULE_TYPE_CASTS(FontFace);
287DEFINE_STYLE_RULE_TYPE_CASTS(Page);
288DEFINE_STYLE_RULE_TYPE_CASTS(Media);
289DEFINE_STYLE_RULE_TYPE_CASTS(Supports);
290DEFINE_STYLE_RULE_TYPE_CASTS(Viewport);
291DEFINE_STYLE_RULE_TYPE_CASTS(Filter);
292
293} // namespace blink
294
295#endif // StyleRule_h
296