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