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 "CSSParserMode.h"
25#include "KURL.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>
31
32namespace WebCore {
33
34class CSSStyleSheet;
35class CachedCSSStyleSheet;
36class Document;
37class Node;
38class SecurityOrigin;
39class StyleRuleBase;
40class StyleRuleImport;
41
42class StyleSheetContents : public RefCounted<StyleSheetContents> {
43public:
44    static PassRefPtr<StyleSheetContents> create(const CSSParserContext& context = CSSParserContext(CSSStrictMode))
45    {
46        return adoptRef(new StyleSheetContents(0, String(), context));
47    }
48    static PassRefPtr<StyleSheetContents> create(const String& originalURL, const CSSParserContext& context)
49    {
50        return adoptRef(new StyleSheetContents(0, originalURL, context));
51    }
52    static PassRefPtr<StyleSheetContents> create(StyleRuleImport* ownerRule, const String& originalURL, const CSSParserContext& context)
53    {
54        return adoptRef(new StyleSheetContents(ownerRule, originalURL, context));
55    }
56
57    ~StyleSheetContents();
58
59    const CSSParserContext& parserContext() const { return m_parserContext; }
60
61    const AtomicString& determineNamespace(const AtomicString& prefix);
62
63    void parseAuthorStyleSheet(const CachedCSSStyleSheet*, const SecurityOrigin*);
64    bool parseString(const String&);
65    bool parseStringAtLine(const String&, int startLineNumber, bool);
66
67    bool isCacheable() const;
68
69    bool isLoading() const;
70
71    void checkLoaded();
72    void startLoadingDynamicSheet();
73
74    StyleSheetContents* rootStyleSheet() const;
75    Node* singleOwnerNode() const;
76    Document* singleOwnerDocument() const;
77
78    const String& charset() const { return m_parserContext.charset; }
79
80    bool loadCompleted() const { return m_loadCompleted; }
81    bool hasFailedOrCanceledSubresources() const;
82
83    KURL completeURL(const String& url) const;
84    void addSubresourceStyleURLs(ListHashSet<KURL>&);
85
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; }
90
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; }
95
96    void clearRules();
97
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; }
103
104    void notifyLoadedSheet(const CachedCSSStyleSheet*);
105
106    StyleSheetContents* parentStyleSheet() const;
107    StyleRuleImport* ownerRule() const { return m_ownerRule; }
108    void clearOwnerRule() { m_ownerRule = 0; }
109
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; }
114    const KURL& baseURL() const { return m_parserContext.baseURL; }
115
116    unsigned ruleCount() const;
117    StyleRuleBase* ruleAt(unsigned index) const;
118
119    bool usesRemUnits() const { return m_usesRemUnits; }
120
121    unsigned estimatedSizeInBytes() const;
122
123    bool wrapperInsertRule(PassRefPtr<StyleRuleBase>, unsigned index);
124    void wrapperDeleteRule(unsigned index);
125
126    PassRefPtr<StyleSheetContents> copy() const { return adoptRef(new StyleSheetContents(*this)); }
127
128    void registerClient(CSSStyleSheet*);
129    void unregisterClient(CSSStyleSheet*);
130    bool hasOneClient() { return m_clients.size() == 1; }
131
132    bool isMutable() const { return m_isMutable; }
133    void setMutable() { m_isMutable = true; }
134
135    bool isInMemoryCache() const { return m_isInMemoryCache; }
136    void addedToMemoryCache();
137    void removedFromMemoryCache();
138
139    void shrinkToFit();
140
141private:
142    StyleSheetContents(StyleRuleImport* ownerRule, const String& originalURL, const CSSParserContext&);
143    StyleSheetContents(const StyleSheetContents&);
144
145    void clearCharsetRule();
146
147    StyleRuleImport* m_ownerRule;
148
149    String m_originalURL;
150
151    String m_encodingFromCharsetRule;
152    Vector<RefPtr<StyleRuleImport> > m_importRules;
153    Vector<RefPtr<StyleRuleBase> > m_childRules;
154    typedef HashMap<AtomicString, AtomicString> PrefixNamespaceURIMap;
155    PrefixNamespaceURIMap m_namespaces;
156
157    bool m_loadCompleted : 1;
158    bool m_isUserStyleSheet : 1;
159    bool m_hasSyntacticallyValidCSSHeader : 1;
160    bool m_didLoadErrorOccur : 1;
161    bool m_usesRemUnits : 1;
162    bool m_isMutable : 1;
163    bool m_isInMemoryCache : 1;
164
165    CSSParserContext m_parserContext;
166
167    Vector<CSSStyleSheet*> m_clients;
168};
169
170} // namespace
171
172#endif
173