1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4 *           (C) 2001 Dirk Mueller (mueller@kde.org)
5 *           (C) 2006 Alexey Proskuryakov (ap@webkit.org)
6 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012 Apple Inc. All rights reserved.
7 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
8 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
9 * Copyright (C) 2011 Google Inc. All rights reserved.
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Library General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 * Library General Public License for more details.
20 *
21 * You should have received a copy of the GNU Library General Public License
22 * along with this library; see the file COPYING.LIB.  If not, write to
23 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 * Boston, MA 02110-1301, USA.
25 *
26 */
27
28#ifndef DocumentStyleSheetCollection_h
29#define DocumentStyleSheetCollection_h
30
31#include <wtf/FastAllocBase.h>
32#include <wtf/ListHashSet.h>
33#include <wtf/RefPtr.h>
34#include <wtf/Vector.h>
35#include <wtf/text/WTFString.h>
36
37namespace WebCore {
38
39class CSSStyleSheet;
40class Document;
41class Node;
42class StyleSheet;
43class StyleSheetContents;
44class StyleSheetList;
45
46class DocumentStyleSheetCollection {
47    WTF_MAKE_FAST_ALLOCATED;
48public:
49    static PassOwnPtr<DocumentStyleSheetCollection> create(Document* document) { return adoptPtr(new DocumentStyleSheetCollection(document)); }
50
51    ~DocumentStyleSheetCollection();
52
53    const Vector<RefPtr<StyleSheet> >& styleSheetsForStyleSheetList() const { return m_styleSheetsForStyleSheetList; }
54
55    const Vector<RefPtr<CSSStyleSheet> >& activeAuthorStyleSheets() const { return m_activeAuthorStyleSheets; }
56
57    CSSStyleSheet* pageUserSheet();
58    const Vector<RefPtr<CSSStyleSheet> >& documentUserStyleSheets() const { return m_userStyleSheets; }
59    const Vector<RefPtr<CSSStyleSheet> >& documentAuthorStyleSheets() const { return m_authorStyleSheets; }
60    const Vector<RefPtr<CSSStyleSheet> >& injectedUserStyleSheets() const;
61    const Vector<RefPtr<CSSStyleSheet> >& injectedAuthorStyleSheets() const;
62
63    void addStyleSheetCandidateNode(Node*, bool createdByParser);
64    void removeStyleSheetCandidateNode(Node*);
65
66    void clearPageUserSheet();
67    void updatePageUserSheet();
68    void invalidateInjectedStyleSheetCache();
69    void updateInjectedStyleSheetCache() const;
70
71    void addAuthorSheet(PassRefPtr<StyleSheetContents> authorSheet);
72    void addUserSheet(PassRefPtr<StyleSheetContents> userSheet);
73
74    enum UpdateFlag { NoUpdate = 0, OptimizedUpdate, FullUpdate };
75
76    UpdateFlag pendingUpdateType() const { return m_pendingUpdateType; }
77    void setPendingUpdateType(UpdateFlag updateType)
78    {
79        if (updateType > m_pendingUpdateType)
80            m_pendingUpdateType = updateType;
81    }
82
83    void flushPendingUpdates()
84    {
85        if (m_pendingUpdateType != NoUpdate)
86            updateActiveStyleSheets(m_pendingUpdateType);
87    }
88
89    bool updateActiveStyleSheets(UpdateFlag);
90
91    String preferredStylesheetSetName() const { return m_preferredStylesheetSetName; }
92    String selectedStylesheetSetName() const { return m_selectedStylesheetSetName; }
93    void setPreferredStylesheetSetName(const String& name) { m_preferredStylesheetSetName = name; }
94    void setSelectedStylesheetSetName(const String& name) { m_selectedStylesheetSetName = name; }
95
96    void addPendingSheet() { m_pendingStylesheets++; }
97    enum RemovePendingSheetNotificationType {
98        RemovePendingSheetNotifyImmediately,
99        RemovePendingSheetNotifyLater
100    };
101    void removePendingSheet(RemovePendingSheetNotificationType = RemovePendingSheetNotifyImmediately);
102
103    bool hasPendingSheets() const { return m_pendingStylesheets > 0; }
104
105    bool usesSiblingRules() const { return m_usesSiblingRules || m_usesSiblingRulesOverride; }
106    void setUsesSiblingRulesOverride(bool b) { m_usesSiblingRulesOverride = b; }
107    bool usesFirstLineRules() const { return m_usesFirstLineRules; }
108    bool usesFirstLetterRules() const { return m_usesFirstLetterRules; }
109    void setUsesFirstLetterRules(bool b) { m_usesFirstLetterRules = b; }
110    bool usesBeforeAfterRules() const { return m_usesBeforeAfterRules || m_usesBeforeAfterRulesOverride; }
111    void setUsesBeforeAfterRulesOverride(bool b) { m_usesBeforeAfterRulesOverride = b; }
112    bool usesRemUnits() const { return m_usesRemUnits; }
113    void setUsesRemUnit(bool b) { m_usesRemUnits = b; }
114
115    void combineCSSFeatureFlags();
116    void resetCSSFeatureFlags();
117
118    bool activeStyleSheetsContains(const CSSStyleSheet*) const;
119
120private:
121    DocumentStyleSheetCollection(Document*);
122
123    void collectActiveStyleSheets(Vector<RefPtr<StyleSheet> >&);
124    enum StyleResolverUpdateType {
125        Reconstruct,
126        Reset,
127        Additive
128    };
129    void analyzeStyleSheetChange(UpdateFlag, const Vector<RefPtr<CSSStyleSheet> >& newStylesheets, StyleResolverUpdateType&, bool& requiresFullStyleRecalc);
130
131    Document* m_document;
132
133    Vector<RefPtr<StyleSheet> > m_styleSheetsForStyleSheetList;
134    Vector<RefPtr<CSSStyleSheet> > m_activeAuthorStyleSheets;
135
136    // This is a mirror of m_activeAuthorStyleSheets that gets populated on demand for activeStyleSheetsContains().
137    mutable OwnPtr<HashSet<const CSSStyleSheet*>> m_weakCopyOfActiveStyleSheetListForFastLookup;
138
139    // Track the number of currently loading top-level stylesheets needed for rendering.
140    // Sheets loaded using the @import directive are not included in this count.
141    // We use this count of pending sheets to detect when we can begin attaching
142    // elements and when it is safe to execute scripts.
143    int m_pendingStylesheets;
144
145    RefPtr<CSSStyleSheet> m_pageUserSheet;
146
147    mutable Vector<RefPtr<CSSStyleSheet> > m_injectedUserStyleSheets;
148    mutable Vector<RefPtr<CSSStyleSheet> > m_injectedAuthorStyleSheets;
149    mutable bool m_injectedStyleSheetCacheValid;
150
151    Vector<RefPtr<CSSStyleSheet> > m_userStyleSheets;
152    Vector<RefPtr<CSSStyleSheet> > m_authorStyleSheets;
153
154    bool m_hadActiveLoadingStylesheet;
155    UpdateFlag m_pendingUpdateType;
156
157    typedef ListHashSet<Node*, 32> StyleSheetCandidateListHashSet;
158    StyleSheetCandidateListHashSet m_styleSheetCandidateNodes;
159
160    String m_preferredStylesheetSetName;
161    String m_selectedStylesheetSetName;
162
163    bool m_usesSiblingRules;
164    bool m_usesSiblingRulesOverride;
165    bool m_usesFirstLineRules;
166    bool m_usesFirstLetterRules;
167    bool m_usesBeforeAfterRules;
168    bool m_usesBeforeAfterRulesOverride;
169    bool m_usesRemUnits;
170};
171
172}
173
174#endif
175
176