Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / css / CSSStyleSheet.h
1 /*
2  * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3  * Copyright (C) 2004, 2006, 2007, 2008, 2009, 2010, 2012 Apple Inc. All rights reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #ifndef CSSStyleSheet_h
22 #define CSSStyleSheet_h
23
24 #include "core/css/CSSRule.h"
25 #include "core/css/StyleSheet.h"
26 #include "platform/heap/Handle.h"
27 #include "wtf/Noncopyable.h"
28 #include "wtf/text/TextPosition.h"
29
30 namespace blink {
31
32 class CSSCharsetRule;
33 class CSSImportRule;
34 class BisonCSSParser;
35 class CSSRule;
36 class CSSRuleList;
37 class CSSStyleSheet;
38 class CSSStyleSheetResource;
39 class Document;
40 class ExceptionState;
41 class MediaQuerySet;
42 class SecurityOrigin;
43 class StyleSheetContents;
44
45 enum StyleSheetUpdateType {
46     PartialRuleUpdate,
47     EntireStyleSheetUpdate
48 };
49
50 class CSSStyleSheet FINAL : public StyleSheet {
51 public:
52     static PassRefPtrWillBeRawPtr<CSSStyleSheet> create(PassRefPtrWillBeRawPtr<StyleSheetContents>, CSSImportRule* ownerRule = 0);
53     static PassRefPtrWillBeRawPtr<CSSStyleSheet> create(PassRefPtrWillBeRawPtr<StyleSheetContents>, Node* ownerNode);
54     static PassRefPtrWillBeRawPtr<CSSStyleSheet> createInline(Node*, const KURL&, const TextPosition& startPosition = TextPosition::minimumPosition(), const String& encoding = String());
55     static PassRefPtrWillBeRawPtr<CSSStyleSheet> createInline(PassRefPtrWillBeRawPtr<StyleSheetContents>, Node* ownerNode, const TextPosition& startPosition = TextPosition::minimumPosition());
56
57     virtual ~CSSStyleSheet();
58
59     virtual CSSStyleSheet* parentStyleSheet() const OVERRIDE;
60     virtual Node* ownerNode() const OVERRIDE { return m_ownerNode; }
61     virtual MediaList* media() const OVERRIDE;
62     virtual String href() const OVERRIDE;
63     virtual String title() const OVERRIDE { return m_title; }
64     virtual bool disabled() const OVERRIDE { return m_isDisabled; }
65     virtual void setDisabled(bool) OVERRIDE;
66
67     PassRefPtrWillBeRawPtr<CSSRuleList> cssRules();
68     unsigned insertRule(const String& rule, unsigned index, ExceptionState&);
69     unsigned insertRule(const String& rule, ExceptionState&); // Deprecated.
70     void deleteRule(unsigned index, ExceptionState&);
71
72     // IE Extensions
73     PassRefPtrWillBeRawPtr<CSSRuleList> rules();
74     int addRule(const String& selector, const String& style, int index, ExceptionState&);
75     int addRule(const String& selector, const String& style, ExceptionState&);
76     void removeRule(unsigned index, ExceptionState& exceptionState) { deleteRule(index, exceptionState); }
77
78     // For CSSRuleList.
79     unsigned length() const;
80     CSSRule* item(unsigned index);
81
82     virtual void clearOwnerNode() OVERRIDE;
83
84     virtual CSSRule* ownerRule() const OVERRIDE { return m_ownerRule; }
85     virtual KURL baseURL() const OVERRIDE;
86     virtual bool isLoading() const OVERRIDE;
87
88     void clearOwnerRule() { m_ownerRule = nullptr; }
89     Document* ownerDocument() const;
90     MediaQuerySet* mediaQueries() const { return m_mediaQueries.get(); }
91     void setMediaQueries(PassRefPtrWillBeRawPtr<MediaQuerySet>);
92     void setTitle(const String& title) { m_title = title; }
93
94     class RuleMutationScope {
95         WTF_MAKE_NONCOPYABLE(RuleMutationScope);
96         STACK_ALLOCATED();
97     public:
98         explicit RuleMutationScope(CSSStyleSheet*);
99         explicit RuleMutationScope(CSSRule*);
100         ~RuleMutationScope();
101
102     private:
103         RawPtrWillBeMember<CSSStyleSheet> m_styleSheet;
104     };
105
106     void willMutateRules();
107     void didMutateRules();
108     void didMutate(StyleSheetUpdateType = PartialRuleUpdate);
109
110     void clearChildRuleCSSOMWrappers();
111
112     StyleSheetContents* contents() const { return m_contents.get(); }
113
114     bool isInline() const { return m_isInlineStylesheet; }
115     TextPosition startPositionInSource() const { return m_startPosition; }
116
117     bool sheetLoaded();
118     bool loadCompleted() const { return m_loadCompleted; }
119     void startLoadingDynamicSheet();
120
121     virtual void trace(Visitor*) OVERRIDE;
122
123 private:
124     CSSStyleSheet(PassRefPtrWillBeRawPtr<StyleSheetContents>, CSSImportRule* ownerRule);
125     CSSStyleSheet(PassRefPtrWillBeRawPtr<StyleSheetContents>, Node* ownerNode, bool isInlineStylesheet, const TextPosition& startPosition);
126
127     virtual bool isCSSStyleSheet() const OVERRIDE { return true; }
128     virtual String type() const OVERRIDE { return "text/css"; }
129
130     void reattachChildRuleCSSOMWrappers();
131
132     bool canAccessRules() const;
133
134     void setLoadCompleted(bool);
135
136     RefPtrWillBeMember<StyleSheetContents> m_contents;
137     bool m_isInlineStylesheet;
138     bool m_isDisabled;
139     String m_title;
140     RefPtrWillBeMember<MediaQuerySet> m_mediaQueries;
141
142     RawPtrWillBeMember<Node> m_ownerNode;
143     RawPtrWillBeMember<CSSRule> m_ownerRule;
144
145     TextPosition m_startPosition;
146     bool m_loadCompleted;
147     mutable RefPtrWillBeMember<MediaList> m_mediaCSSOMWrapper;
148     mutable WillBeHeapVector<RefPtrWillBeMember<CSSRule> > m_childRuleCSSOMWrappers;
149     mutable OwnPtrWillBeMember<CSSRuleList> m_ruleListCSSOMWrapper;
150 };
151
152 inline CSSStyleSheet::RuleMutationScope::RuleMutationScope(CSSStyleSheet* sheet)
153     : m_styleSheet(sheet)
154 {
155     if (m_styleSheet)
156         m_styleSheet->willMutateRules();
157 }
158
159 inline CSSStyleSheet::RuleMutationScope::RuleMutationScope(CSSRule* rule)
160     : m_styleSheet(rule ? rule->parentStyleSheet() : 0)
161 {
162     if (m_styleSheet)
163         m_styleSheet->willMutateRules();
164 }
165
166 inline CSSStyleSheet::RuleMutationScope::~RuleMutationScope()
167 {
168     if (m_styleSheet)
169         m_styleSheet->didMutateRules();
170 }
171
172 DEFINE_TYPE_CASTS(CSSStyleSheet, StyleSheet, sheet, sheet->isCSSStyleSheet(), sheet.isCSSStyleSheet());
173
174 } // namespace
175
176 #endif