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 "CSSParserMode.h"
26 #include <wtf/HashMap.h>
27 #include <wtf/ListHashSet.h>
28 #include <wtf/RefCounted.h>
29 #include <wtf/Vector.h>
30 #include <wtf/text/AtomicStringHash.h>
35 class CachedCSSStyleSheet;
37 class MemoryObjectInfo;
41 class StyleRuleImport;
43 class StyleSheetContents : public RefCounted<StyleSheetContents> {
45 static PassRefPtr<StyleSheetContents> create(const CSSParserContext& context = CSSParserContext(CSSStrictMode))
47 return adoptRef(new StyleSheetContents(0, String(), KURL(), context));
49 static PassRefPtr<StyleSheetContents> create(const String& originalURL, const KURL& finalURL, const CSSParserContext& context)
51 return adoptRef(new StyleSheetContents(0, originalURL, finalURL, context));
53 static PassRefPtr<StyleSheetContents> create(StyleRuleImport* ownerRule, const String& originalURL, const KURL& finalURL, const CSSParserContext& context)
55 return adoptRef(new StyleSheetContents(ownerRule, originalURL, finalURL, context));
58 ~StyleSheetContents();
60 const CSSParserContext& parserContext() const { return m_parserContext; }
62 const AtomicString& determineNamespace(const AtomicString& prefix);
64 void parseAuthorStyleSheet(const CachedCSSStyleSheet*, const SecurityOrigin*);
65 bool parseString(const String&);
66 bool parseStringAtLine(const String&, int startLineNumber);
68 bool isCacheable() const;
70 bool isLoading() const;
73 void startLoadingDynamicSheet();
75 StyleSheetContents* rootStyleSheet() const;
76 Node* singleOwnerNode() const;
77 Document* singleOwnerDocument() const;
79 const String& charset() const { return m_parserContext.charset; }
81 bool loadCompleted() const { return m_loadCompleted; }
83 KURL completeURL(const String& url) const;
84 void addSubresourceStyleURLs(ListHashSet<KURL>&);
86 void setIsUserStyleSheet(bool b) { m_isUserStyleSheet = b; }
87 bool isUserStyleSheet() const { return m_isUserStyleSheet; }
88 void setHasSyntacticallyValidCSSHeader(bool b) { m_hasSyntacticallyValidCSSHeader = b; }
89 bool hasSyntacticallyValidCSSHeader() const { return m_hasSyntacticallyValidCSSHeader; }
91 void parserAddNamespace(const AtomicString& prefix, const AtomicString& uri);
92 void parserAppendRule(PassRefPtr<StyleRuleBase>);
93 void parserSetEncodingFromCharsetRule(const String& encoding);
94 void parserSetUsesRemUnits(bool b) { m_usesRemUnits = b; }
98 bool hasCharsetRule() const { return !m_encodingFromCharsetRule.isNull(); }
99 String encodingFromCharsetRule() const { return m_encodingFromCharsetRule; }
100 // Rules other than @charset and @import.
101 const Vector<RefPtr<StyleRuleBase> >& childRules() const { return m_childRules; }
102 const Vector<RefPtr<StyleRuleImport> >& importRules() const { return m_importRules; }
104 void notifyLoadedSheet(const CachedCSSStyleSheet*);
106 StyleSheetContents* parentStyleSheet() const;
107 StyleRuleImport* ownerRule() const { return m_ownerRule; }
108 void clearOwnerRule() { m_ownerRule = 0; }
110 // Note that href is the URL that started the redirect chain that led to
111 // this style sheet. This property probably isn't useful for much except
112 // the JavaScript binding (which needs to use this value for security).
113 String originalURL() const { return m_originalURL; }
115 const KURL& finalURL() const { return m_finalURL; }
116 const KURL& baseURL() const { return m_parserContext.baseURL; }
118 unsigned ruleCount() const;
119 StyleRuleBase* ruleAt(unsigned index) const;
121 bool usesRemUnits() const { return m_usesRemUnits; }
123 unsigned estimatedSizeInBytes() const;
125 bool wrapperInsertRule(PassRefPtr<StyleRuleBase>, unsigned index);
126 void wrapperDeleteRule(unsigned index);
128 PassRefPtr<StyleSheetContents> copy() const { return adoptRef(new StyleSheetContents(*this)); }
130 void registerClient(CSSStyleSheet*);
131 void unregisterClient(CSSStyleSheet*);
132 bool hasOneClient() { return m_clients.size() == 1; }
134 bool isMutable() const { return m_isMutable; }
135 void setMutable() { m_isMutable = true; }
137 bool isInMemoryCache() const { return m_isInMemoryCache; }
138 void addedToMemoryCache();
139 void removedFromMemoryCache();
141 void reportMemoryUsage(MemoryObjectInfo*) const;
144 StyleSheetContents(StyleRuleImport* ownerRule, const String& originalURL, const KURL& baseURL, const CSSParserContext&);
145 StyleSheetContents(const StyleSheetContents&);
147 void clearCharsetRule();
149 StyleRuleImport* m_ownerRule;
151 String m_originalURL;
154 String m_encodingFromCharsetRule;
155 Vector<RefPtr<StyleRuleImport> > m_importRules;
156 Vector<RefPtr<StyleRuleBase> > m_childRules;
157 typedef HashMap<AtomicString, AtomicString> PrefixNamespaceURIMap;
158 PrefixNamespaceURIMap m_namespaces;
160 bool m_loadCompleted : 1;
161 bool m_isUserStyleSheet : 1;
162 bool m_hasSyntacticallyValidCSSHeader : 1;
163 bool m_didLoadErrorOccur : 1;
164 bool m_usesRemUnits : 1;
165 bool m_isMutable : 1;
166 bool m_isInMemoryCache : 1;
168 CSSParserContext m_parserContext;
170 Vector<CSSStyleSheet*> m_clients;