Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / css / StyleSheetContents.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 StyleSheetContents_h
22 #define StyleSheetContents_h
23
24 #include "core/css/CSSParserMode.h"
25 #include "core/css/RuleSet.h"
26 #include "platform/weborigin/KURL.h"
27 #include "wtf/HashMap.h"
28 #include "wtf/ListHashSet.h"
29 #include "wtf/RefCounted.h"
30 #include "wtf/Vector.h"
31 #include "wtf/text/AtomicStringHash.h"
32 #include "wtf/text/StringHash.h"
33 #include "wtf/text/TextPosition.h"
34
35
36 namespace WebCore {
37
38 class CSSStyleSheet;
39 class CSSStyleSheetResource;
40 class Document;
41 class Node;
42 class SecurityOrigin;
43 class StyleRuleBase;
44 class StyleRuleFontFace;
45 class StyleRuleImport;
46
47 class StyleSheetContents : public RefCountedWillBeRefCountedGarbageCollected<StyleSheetContents> {
48     DECLARE_GC_INFO
49 public:
50     static PassRefPtrWillBeRawPtr<StyleSheetContents> create(const CSSParserContext& context)
51     {
52         return adoptRefCountedWillBeRefCountedGarbageCollected(new StyleSheetContents(0, String(), context));
53     }
54     static PassRefPtrWillBeRawPtr<StyleSheetContents> create(const String& originalURL, const CSSParserContext& context)
55     {
56         return adoptRefCountedWillBeRefCountedGarbageCollected(new StyleSheetContents(0, originalURL, context));
57     }
58     static PassRefPtrWillBeRawPtr<StyleSheetContents> create(StyleRuleImport* ownerRule, const String& originalURL, const CSSParserContext& context)
59     {
60         return adoptRefCountedWillBeRefCountedGarbageCollected(new StyleSheetContents(ownerRule, originalURL, context));
61     }
62
63     ~StyleSheetContents();
64
65     const CSSParserContext& parserContext() const { return m_parserContext; }
66
67     const AtomicString& determineNamespace(const AtomicString& prefix);
68
69     void parseAuthorStyleSheet(const CSSStyleSheetResource*, const SecurityOrigin*);
70     bool parseString(const String&);
71     bool parseStringAtPosition(const String&, const TextPosition&, bool);
72
73     bool isCacheable() const;
74     bool maybeCacheable() const;
75
76     bool isLoading() const;
77
78     void checkLoaded();
79     void startLoadingDynamicSheet();
80
81     StyleSheetContents* rootStyleSheet() const;
82     bool hasSingleOwnerNode() const;
83     Node* singleOwnerNode() const;
84     Document* singleOwnerDocument() const;
85
86     const String& charset() const { return m_parserContext.charset(); }
87
88     bool loadCompleted() const;
89     bool hasFailedOrCanceledSubresources() const;
90
91     KURL completeURL(const String& url) const;
92
93     void setHasSyntacticallyValidCSSHeader(bool isValidCss);
94     bool hasSyntacticallyValidCSSHeader() const { return m_hasSyntacticallyValidCSSHeader; }
95
96     void setHasFontFaceRule(bool b) { m_hasFontFaceRule = b; }
97     bool hasFontFaceRule() const { return m_hasFontFaceRule; }
98     void findFontFaceRules(Vector<const StyleRuleFontFace*>& fontFaceRules);
99
100     void parserAddNamespace(const AtomicString& prefix, const AtomicString& uri);
101     void parserAppendRule(PassRefPtr<StyleRuleBase>);
102     void parserSetEncodingFromCharsetRule(const String& encoding);
103     void parserSetUsesRemUnits(bool b) { m_usesRemUnits = b; }
104
105     void clearRules();
106
107     bool hasCharsetRule() const { return !m_encodingFromCharsetRule.isNull(); }
108     String encodingFromCharsetRule() const { return m_encodingFromCharsetRule; }
109     // Rules other than @charset and @import.
110     const Vector<RefPtr<StyleRuleBase> >& childRules() const { return m_childRules; }
111     const Vector<RefPtr<StyleRuleImport> >& importRules() const { return m_importRules; }
112
113     void notifyLoadedSheet(const CSSStyleSheetResource*);
114
115     StyleSheetContents* parentStyleSheet() const;
116     StyleRuleImport* ownerRule() const { return m_ownerRule; }
117     void clearOwnerRule() { m_ownerRule = 0; }
118
119     // Note that href is the URL that started the redirect chain that led to
120     // this style sheet. This property probably isn't useful for much except
121     // the JavaScript binding (which needs to use this value for security).
122     String originalURL() const { return m_originalURL; }
123     const KURL& baseURL() const { return m_parserContext.baseURL(); }
124
125     unsigned ruleCount() const;
126     StyleRuleBase* ruleAt(unsigned index) const;
127
128     bool usesRemUnits() const { return m_usesRemUnits; }
129
130     unsigned estimatedSizeInBytes() const;
131
132     bool wrapperInsertRule(PassRefPtr<StyleRuleBase>, unsigned index);
133     void wrapperDeleteRule(unsigned index);
134
135     PassRefPtr<StyleSheetContents> copy() const { return adoptRef(new StyleSheetContents(*this)); }
136
137     void registerClient(CSSStyleSheet*);
138     void unregisterClient(CSSStyleSheet*);
139     bool hasOneClient() { return m_clients.size() == 1; }
140
141     bool isMutable() const { return m_isMutable; }
142     void setMutable() { m_isMutable = true; }
143
144     bool isInMemoryCache() const { return m_isInMemoryCache; }
145     void addedToMemoryCache();
146     void removedFromMemoryCache();
147
148     void setHasMediaQueries();
149     bool hasMediaQueries() { return m_hasMediaQueries; }
150
151     void shrinkToFit();
152     RuleSet& ruleSet() { ASSERT(m_ruleSet); return *m_ruleSet.get(); }
153     RuleSet& ensureRuleSet(const MediaQueryEvaluator&, AddRuleFlags);
154     void clearRuleSet();
155
156     void trace(Visitor*);
157
158 private:
159     StyleSheetContents(StyleRuleImport* ownerRule, const String& originalURL, const CSSParserContext&);
160     StyleSheetContents(const StyleSheetContents&);
161     void notifyRemoveFontFaceRule(const StyleRuleFontFace*);
162
163     void clearCharsetRule();
164
165     StyleRuleImport* m_ownerRule;
166
167     String m_originalURL;
168
169     String m_encodingFromCharsetRule;
170     Vector<RefPtr<StyleRuleImport> > m_importRules;
171     Vector<RefPtr<StyleRuleBase> > m_childRules;
172     typedef HashMap<AtomicString, AtomicString> PrefixNamespaceURIMap;
173     PrefixNamespaceURIMap m_namespaces;
174
175     bool m_hasSyntacticallyValidCSSHeader : 1;
176     bool m_didLoadErrorOccur : 1;
177     bool m_usesRemUnits : 1;
178     bool m_isMutable : 1;
179     bool m_isInMemoryCache : 1;
180     bool m_hasFontFaceRule : 1;
181     bool m_hasMediaQueries : 1;
182
183     CSSParserContext m_parserContext;
184
185     Vector<CSSStyleSheet*> m_clients;
186     OwnPtr<RuleSet> m_ruleSet;
187 };
188
189 } // namespace
190
191 #endif