• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/heap/Handle.h"
27 #include "platform/weborigin/KURL.h"
28 #include "wtf/HashMap.h"
29 #include "wtf/ListHashSet.h"
30 #include "wtf/RefCounted.h"
31 #include "wtf/Vector.h"
32 #include "wtf/text/AtomicStringHash.h"
33 #include "wtf/text/StringHash.h"
34 #include "wtf/text/TextPosition.h"
35 
36 
37 namespace WebCore {
38 
39 class CSSStyleSheet;
40 class CSSStyleSheetResource;
41 class Document;
42 class Node;
43 class SecurityOrigin;
44 class StyleRuleBase;
45 class StyleRuleFontFace;
46 class StyleRuleImport;
47 
48 class StyleSheetContents : public RefCountedWillBeGarbageCollectedFinalized<StyleSheetContents> {
49 public:
create(const CSSParserContext & context)50     static PassRefPtrWillBeRawPtr<StyleSheetContents> create(const CSSParserContext& context)
51     {
52         return adoptRefWillBeNoop(new StyleSheetContents(0, String(), context));
53     }
create(const String & originalURL,const CSSParserContext & context)54     static PassRefPtrWillBeRawPtr<StyleSheetContents> create(const String& originalURL, const CSSParserContext& context)
55     {
56         return adoptRefWillBeNoop(new StyleSheetContents(0, originalURL, context));
57     }
create(StyleRuleImport * ownerRule,const String & originalURL,const CSSParserContext & context)58     static PassRefPtrWillBeRawPtr<StyleSheetContents> create(StyleRuleImport* ownerRule, const String& originalURL, const CSSParserContext& context)
59     {
60         return adoptRefWillBeNoop(new StyleSheetContents(ownerRule, originalURL, context));
61     }
62 
63     ~StyleSheetContents();
64 
parserContext()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 
75     bool isLoading() const;
76 
77     void checkLoaded();
78     void startLoadingDynamicSheet();
79 
80     StyleSheetContents* rootStyleSheet() const;
81     bool hasSingleOwnerNode() const;
82     Node* singleOwnerNode() const;
83     Document* singleOwnerDocument() const;
84 
charset()85     const String& charset() const { return m_parserContext.charset(); }
86 
87     bool loadCompleted() const;
88     bool hasFailedOrCanceledSubresources() const;
89 
90     KURL completeURL(const String& url) const;
91 
92     void setHasSyntacticallyValidCSSHeader(bool isValidCss);
hasSyntacticallyValidCSSHeader()93     bool hasSyntacticallyValidCSSHeader() const { return m_hasSyntacticallyValidCSSHeader; }
94 
setHasFontFaceRule(bool b)95     void setHasFontFaceRule(bool b) { m_hasFontFaceRule = b; }
hasFontFaceRule()96     bool hasFontFaceRule() const { return m_hasFontFaceRule; }
97     void findFontFaceRules(WillBeHeapVector<RawPtrWillBeMember<const StyleRuleFontFace> >& fontFaceRules);
98 
99     void parserAddNamespace(const AtomicString& prefix, const AtomicString& uri);
100     void parserAppendRule(PassRefPtrWillBeRawPtr<StyleRuleBase>);
101     void parserSetEncodingFromCharsetRule(const String& encoding);
parserSetUsesRemUnits(bool b)102     void parserSetUsesRemUnits(bool b) { m_usesRemUnits = b; }
103 
104     void clearRules();
105 
hasCharsetRule()106     bool hasCharsetRule() const { return !m_encodingFromCharsetRule.isNull(); }
encodingFromCharsetRule()107     String encodingFromCharsetRule() const { return m_encodingFromCharsetRule; }
108     // Rules other than @charset and @import.
childRules()109     const WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> >& childRules() const { return m_childRules; }
importRules()110     const WillBeHeapVector<RefPtrWillBeMember<StyleRuleImport> >& importRules() const { return m_importRules; }
111 
112     void notifyLoadedSheet(const CSSStyleSheetResource*);
113 
114     StyleSheetContents* parentStyleSheet() const;
ownerRule()115     StyleRuleImport* ownerRule() const { return m_ownerRule; }
clearOwnerRule()116     void clearOwnerRule() { m_ownerRule = nullptr; }
117 
118     // Note that href is the URL that started the redirect chain that led to
119     // this style sheet. This property probably isn't useful for much except
120     // the JavaScript binding (which needs to use this value for security).
originalURL()121     String originalURL() const { return m_originalURL; }
baseURL()122     const KURL& baseURL() const { return m_parserContext.baseURL(); }
123 
124     unsigned ruleCount() const;
125     StyleRuleBase* ruleAt(unsigned index) const;
126 
usesRemUnits()127     bool usesRemUnits() const { return m_usesRemUnits; }
128 
129     unsigned estimatedSizeInBytes() const;
130 
131     bool wrapperInsertRule(PassRefPtrWillBeRawPtr<StyleRuleBase>, unsigned index);
132     void wrapperDeleteRule(unsigned index);
133 
copy()134     PassRefPtrWillBeRawPtr<StyleSheetContents> copy() const
135     {
136         return adoptRefWillBeNoop(new StyleSheetContents(*this));
137     }
138 
139     void registerClient(CSSStyleSheet*);
140     void unregisterClient(CSSStyleSheet*);
clientSize()141     size_t clientSize() const { return m_loadingClients.size() + m_completedClients.size(); }
hasOneClient()142     bool hasOneClient() { return clientSize() == 1; }
143     void clientLoadCompleted(CSSStyleSheet*);
144     void clientLoadStarted(CSSStyleSheet*);
145 
isMutable()146     bool isMutable() const { return m_isMutable; }
setMutable()147     void setMutable() { m_isMutable = true; }
148 
149     void removeSheetFromCache(Document*);
150 
isInMemoryCache()151     bool isInMemoryCache() const { return m_isInMemoryCache; }
152     void addedToMemoryCache();
153     void removedFromMemoryCache();
154 
155     void setHasMediaQueries();
hasMediaQueries()156     bool hasMediaQueries() const { return m_hasMediaQueries; }
157 
didLoadErrorOccur()158     bool didLoadErrorOccur() const { return m_didLoadErrorOccur; }
159 
160     void shrinkToFit();
ruleSet()161     RuleSet& ruleSet() { ASSERT(m_ruleSet); return *m_ruleSet.get(); }
162     RuleSet& ensureRuleSet(const MediaQueryEvaluator&, AddRuleFlags);
163     void clearRuleSet();
164 
165     void trace(Visitor*);
166 
167 private:
168     StyleSheetContents(StyleRuleImport* ownerRule, const String& originalURL, const CSSParserContext&);
169     StyleSheetContents(const StyleSheetContents&);
170     void notifyRemoveFontFaceRule(const StyleRuleFontFace*);
171 
172     Document* clientSingleOwnerDocument() const;
173     void clearCharsetRule();
174 
175     RawPtrWillBeMember<StyleRuleImport> m_ownerRule;
176 
177     String m_originalURL;
178 
179     String m_encodingFromCharsetRule;
180     WillBeHeapVector<RefPtrWillBeMember<StyleRuleImport> > m_importRules;
181     WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> > m_childRules;
182     typedef HashMap<AtomicString, AtomicString> PrefixNamespaceURIMap;
183     PrefixNamespaceURIMap m_namespaces;
184 
185     bool m_hasSyntacticallyValidCSSHeader : 1;
186     bool m_didLoadErrorOccur : 1;
187     bool m_usesRemUnits : 1;
188     bool m_isMutable : 1;
189     bool m_isInMemoryCache : 1;
190     bool m_hasFontFaceRule : 1;
191     bool m_hasMediaQueries : 1;
192     bool m_hasSingleOwnerDocument : 1;
193 
194     CSSParserContext m_parserContext;
195 
196     WillBeHeapHashSet<RawPtrWillBeWeakMember<CSSStyleSheet> > m_loadingClients;
197     WillBeHeapHashSet<RawPtrWillBeWeakMember<CSSStyleSheet> > m_completedClients;
198     typedef WillBeHeapHashSet<RawPtrWillBeWeakMember<CSSStyleSheet> >::iterator ClientsIterator;
199 
200     OwnPtrWillBeMember<RuleSet> m_ruleSet;
201 };
202 
203 } // namespace
204 
205 #endif
206