2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2006, 2007, 2008, 2009, 2010, 2012 Apple Inc. All rights reserved.
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.
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.
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.
21 #ifndef StyleSheetContents_h
22 #define StyleSheetContents_h
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"
39 class CSSStyleSheetResource;
44 class StyleRuleFontFace;
45 class StyleRuleImport;
47 class StyleSheetContents : public RefCountedWillBeRefCountedGarbageCollected<StyleSheetContents> {
50 static PassRefPtrWillBeRawPtr<StyleSheetContents> create(const CSSParserContext& context)
52 return adoptRefCountedWillBeRefCountedGarbageCollected(new StyleSheetContents(0, String(), context));
54 static PassRefPtrWillBeRawPtr<StyleSheetContents> create(const String& originalURL, const CSSParserContext& context)
56 return adoptRefCountedWillBeRefCountedGarbageCollected(new StyleSheetContents(0, originalURL, context));
58 static PassRefPtrWillBeRawPtr<StyleSheetContents> create(StyleRuleImport* ownerRule, const String& originalURL, const CSSParserContext& context)
60 return adoptRefCountedWillBeRefCountedGarbageCollected(new StyleSheetContents(ownerRule, originalURL, context));
63 ~StyleSheetContents();
65 const CSSParserContext& parserContext() const { return m_parserContext; }
67 const AtomicString& determineNamespace(const AtomicString& prefix);
69 void parseAuthorStyleSheet(const CSSStyleSheetResource*, const SecurityOrigin*);
70 bool parseString(const String&);
71 bool parseStringAtPosition(const String&, const TextPosition&, bool);
73 bool isCacheable() const;
74 bool maybeCacheable() const;
76 bool isLoading() const;
79 void startLoadingDynamicSheet();
81 StyleSheetContents* rootStyleSheet() const;
82 bool hasSingleOwnerNode() const;
83 Node* singleOwnerNode() const;
84 Document* singleOwnerDocument() const;
86 const String& charset() const { return m_parserContext.charset(); }
88 bool loadCompleted() const;
89 bool hasFailedOrCanceledSubresources() const;
91 KURL completeURL(const String& url) const;
93 void setHasSyntacticallyValidCSSHeader(bool isValidCss);
94 bool hasSyntacticallyValidCSSHeader() const { return m_hasSyntacticallyValidCSSHeader; }
96 void setHasFontFaceRule(bool b) { m_hasFontFaceRule = b; }
97 bool hasFontFaceRule() const { return m_hasFontFaceRule; }
98 void findFontFaceRules(Vector<const StyleRuleFontFace*>& fontFaceRules);
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; }
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; }
113 void notifyLoadedSheet(const CSSStyleSheetResource*);
115 StyleSheetContents* parentStyleSheet() const;
116 StyleRuleImport* ownerRule() const { return m_ownerRule; }
117 void clearOwnerRule() { m_ownerRule = 0; }
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(); }
125 unsigned ruleCount() const;
126 StyleRuleBase* ruleAt(unsigned index) const;
128 bool usesRemUnits() const { return m_usesRemUnits; }
130 unsigned estimatedSizeInBytes() const;
132 bool wrapperInsertRule(PassRefPtr<StyleRuleBase>, unsigned index);
133 void wrapperDeleteRule(unsigned index);
135 PassRefPtr<StyleSheetContents> copy() const { return adoptRef(new StyleSheetContents(*this)); }
137 void registerClient(CSSStyleSheet*);
138 void unregisterClient(CSSStyleSheet*);
139 bool hasOneClient() { return m_clients.size() == 1; }
141 bool isMutable() const { return m_isMutable; }
142 void setMutable() { m_isMutable = true; }
144 bool isInMemoryCache() const { return m_isInMemoryCache; }
145 void addedToMemoryCache();
146 void removedFromMemoryCache();
148 void setHasMediaQueries();
149 bool hasMediaQueries() { return m_hasMediaQueries; }
152 RuleSet& ruleSet() { ASSERT(m_ruleSet); return *m_ruleSet.get(); }
153 RuleSet& ensureRuleSet(const MediaQueryEvaluator&, AddRuleFlags);
156 void trace(Visitor*);
159 StyleSheetContents(StyleRuleImport* ownerRule, const String& originalURL, const CSSParserContext&);
160 StyleSheetContents(const StyleSheetContents&);
161 void notifyRemoveFontFaceRule(const StyleRuleFontFace*);
163 void clearCharsetRule();
165 StyleRuleImport* m_ownerRule;
167 String m_originalURL;
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;
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;
183 CSSParserContext m_parserContext;
185 Vector<CSSStyleSheet*> m_clients;
186 OwnPtr<RuleSet> m_ruleSet;