Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / dom / StyleEngine.h
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 StyleEngine_h
29 #define StyleEngine_h
30
31 #include "core/css/resolver/StyleResolver.h"
32 #include "core/dom/Document.h"
33 #include "core/dom/DocumentOrderedList.h"
34 #include "core/dom/DocumentStyleSheetCollection.h"
35 #include "platform/heap/Handle.h"
36 #include "wtf/FastAllocBase.h"
37 #include "wtf/ListHashSet.h"
38 #include "wtf/RefPtr.h"
39 #include "wtf/TemporaryChange.h"
40 #include "wtf/Vector.h"
41 #include "wtf/text/WTFString.h"
42
43 namespace WebCore {
44
45 class CSSFontSelector;
46 class CSSStyleSheet;
47 class FontSelector;
48 class Node;
49 class RuleFeatureSet;
50 class ShadowTreeStyleSheetCollection;
51 class StyleResolver;
52 class StyleRuleFontFace;
53 class StyleSheet;
54 class StyleSheetCollection;
55 class StyleSheetContents;
56 class StyleSheetList;
57
58 class StyleResolverChange {
59 public:
60     StyleResolverChange()
61         : m_needsRepaint(false)
62         , m_needsStyleRecalc(false)
63     { }
64
65     bool needsRepaint() const { return m_needsRepaint; }
66     bool needsStyleRecalc() const { return m_needsStyleRecalc; }
67     void setNeedsRepaint() { m_needsRepaint = true; }
68     void setNeedsStyleRecalc() { m_needsStyleRecalc = true; }
69
70 private:
71     bool m_needsRepaint;
72     bool m_needsStyleRecalc;
73 };
74
75 class StyleEngine : public NoBaseWillBeGarbageCollectedFinalized<StyleEngine> {
76     WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
77 public:
78
79     class IgnoringPendingStylesheet : public TemporaryChange<bool> {
80     public:
81         IgnoringPendingStylesheet(StyleEngine* engine)
82             : TemporaryChange<bool>(engine->m_ignorePendingStylesheets, true)
83         {
84         }
85     };
86
87     friend class IgnoringPendingStylesheet;
88
89     static PassOwnPtrWillBeRawPtr<StyleEngine> create(Document& document) { return adoptPtrWillBeNoop(new StyleEngine(document)); }
90
91     ~StyleEngine();
92
93 #if !ENABLE(OILPAN)
94     void detachFromDocument();
95 #endif
96
97     const WillBeHeapVector<RefPtrWillBeMember<StyleSheet> >& styleSheetsForStyleSheetList(TreeScope&);
98     const WillBeHeapVector<RefPtrWillBeMember<CSSStyleSheet> >& activeAuthorStyleSheets() const;
99
100     const WillBeHeapVector<RefPtrWillBeMember<CSSStyleSheet> >& documentAuthorStyleSheets() const { return m_authorStyleSheets; }
101     const WillBeHeapVector<RefPtrWillBeMember<CSSStyleSheet> >& injectedAuthorStyleSheets() const;
102
103     const WillBeHeapVector<RefPtrWillBeMember<StyleSheet> > activeStyleSheetsForInspector() const;
104
105     void modifiedStyleSheet(StyleSheet*);
106     void addStyleSheetCandidateNode(Node*, bool createdByParser);
107     void removeStyleSheetCandidateNode(Node*);
108     void removeStyleSheetCandidateNode(Node*, ContainerNode* scopingNode, TreeScope&);
109     void modifiedStyleSheetCandidateNode(Node*);
110
111     void invalidateInjectedStyleSheetCache();
112     void updateInjectedStyleSheetCache() const;
113
114     void addAuthorSheet(PassRefPtrWillBeRawPtr<StyleSheetContents> authorSheet);
115
116     void clearMediaQueryRuleSetStyleSheets();
117     void updateStyleSheetsInImport(DocumentStyleSheetCollector& parentCollector);
118     bool updateActiveStyleSheets(StyleResolverUpdateMode);
119
120     String preferredStylesheetSetName() const { return m_preferredStylesheetSetName; }
121     String selectedStylesheetSetName() const { return m_selectedStylesheetSetName; }
122     void setPreferredStylesheetSetName(const String& name) { m_preferredStylesheetSetName = name; }
123     void setSelectedStylesheetSetName(const String& name) { m_selectedStylesheetSetName = name; }
124
125     void selectStylesheetSetName(const String& name)
126     {
127         setPreferredStylesheetSetName(name);
128         setSelectedStylesheetSetName(name);
129     }
130
131     void addPendingSheet();
132     enum RemovePendingSheetNotificationType {
133         RemovePendingSheetNotifyImmediately,
134         RemovePendingSheetNotifyLater
135     };
136     void removePendingSheet(Node* styleSheetCandidateNode, RemovePendingSheetNotificationType = RemovePendingSheetNotifyImmediately);
137
138     bool hasPendingSheets() const { return m_pendingStylesheets > 0; }
139     bool haveStylesheetsLoaded() const { return !hasPendingSheets() || m_ignorePendingStylesheets; }
140     bool ignoringPendingStylesheets() const { return m_ignorePendingStylesheets; }
141
142     unsigned maxDirectAdjacentSelectors() const { return m_maxDirectAdjacentSelectors; }
143     bool usesSiblingRules() const { return m_usesSiblingRules || m_usesSiblingRulesOverride; }
144     void setUsesSiblingRulesOverride(bool b) { m_usesSiblingRulesOverride = b; }
145     bool usesFirstLineRules() const { return m_usesFirstLineRules; }
146     bool usesFirstLetterRules() const { return m_usesFirstLetterRules; }
147     void setUsesFirstLetterRules(bool b) { m_usesFirstLetterRules = b; }
148     bool usesRemUnits() const { return m_usesRemUnits; }
149     void setUsesRemUnit(bool b) { m_usesRemUnits = b; }
150     bool hasScopedStyleSheet() { return m_documentStyleSheetCollection.scopingNodesForStyleScoped(); }
151
152     void combineCSSFeatureFlags(const RuleFeatureSet&);
153     void resetCSSFeatureFlags(const RuleFeatureSet&);
154
155     void didRemoveShadowRoot(ShadowRoot*);
156     void appendActiveAuthorStyleSheets();
157
158     StyleResolver* resolver() const
159     {
160         return m_resolver.get();
161     }
162
163     StyleResolver& ensureResolver()
164     {
165         if (!m_resolver) {
166             createResolver();
167         } else if (m_resolver->hasPendingAuthorStyleSheets()) {
168             m_resolver->appendPendingAuthorStyleSheets();
169         }
170         return *m_resolver.get();
171     }
172
173     bool hasResolver() const { return m_resolver.get(); }
174     void clearResolver();
175     void clearMasterResolver();
176
177     CSSFontSelector* fontSelector() { return m_fontSelector.get(); }
178     void removeFontFaceRules(const WillBeHeapVector<RawPtrWillBeMember<const StyleRuleFontFace> >&);
179     void clearFontCache();
180     // updateGenericFontFamilySettings is used from WebSettingsImpl.
181     void updateGenericFontFamilySettings();
182
183     void didDetach();
184     bool shouldClearResolver() const;
185     StyleResolverChange resolverChanged(RecalcStyleTime, StyleResolverUpdateMode);
186     unsigned resolverAccessCount() const;
187
188     void markDocumentDirty();
189
190     PassRefPtrWillBeRawPtr<CSSStyleSheet> createSheet(Element*, const String& text, TextPosition startPosition, bool createdByParser);
191     void removeSheet(StyleSheetContents*);
192
193     void trace(Visitor*);
194
195 private:
196     StyleEngine(Document&);
197
198     TreeScopeStyleSheetCollection* ensureStyleSheetCollectionFor(TreeScope&);
199     TreeScopeStyleSheetCollection* styleSheetCollectionFor(TreeScope&);
200     bool shouldUpdateShadowTreeStyleSheetCollection(StyleResolverUpdateMode);
201
202     void markTreeScopeDirty(TreeScope&);
203
204     bool isMaster() const { return m_isMaster; }
205     Document* master();
206     Document& document() const { return *m_document; }
207
208     typedef ListHashSet<TreeScope*, 16> TreeScopeSet;
209     static void insertTreeScopeInDocumentOrder(TreeScopeSet&, TreeScope*);
210     void clearMediaQueryRuleSetOnTreeScopeStyleSheets(TreeScopeSet treeScopes);
211
212     void createResolver();
213
214     static PassRefPtrWillBeRawPtr<CSSStyleSheet> parseSheet(Element*, const String& text, TextPosition startPosition, bool createdByParser);
215
216     RawPtrWillBeMember<Document> m_document;
217     bool m_isMaster;
218
219     // Track the number of currently loading top-level stylesheets needed for rendering.
220     // Sheets loaded using the @import directive are not included in this count.
221     // We use this count of pending sheets to detect when we can begin attaching
222     // elements and when it is safe to execute scripts.
223     int m_pendingStylesheets;
224
225     mutable WillBeHeapVector<RefPtrWillBeMember<CSSStyleSheet> > m_injectedAuthorStyleSheets;
226     mutable bool m_injectedStyleSheetCacheValid;
227
228     WillBeHeapVector<RefPtrWillBeMember<CSSStyleSheet> > m_authorStyleSheets;
229
230     DocumentStyleSheetCollection m_documentStyleSheetCollection;
231     typedef WillBeHeapHashMap<RawPtrWillBeWeakMember<TreeScope>, OwnPtrWillBeMember<ShadowTreeStyleSheetCollection> > StyleSheetCollectionMap;
232     StyleSheetCollectionMap m_styleSheetCollectionMap;
233
234     bool m_documentScopeDirty;
235     TreeScopeSet m_dirtyTreeScopes;
236     TreeScopeSet m_activeTreeScopes;
237
238     String m_preferredStylesheetSetName;
239     String m_selectedStylesheetSetName;
240
241     bool m_usesSiblingRules;
242     bool m_usesSiblingRulesOverride;
243     bool m_usesFirstLineRules;
244     bool m_usesFirstLetterRules;
245     bool m_usesRemUnits;
246     unsigned m_maxDirectAdjacentSelectors;
247
248     bool m_ignorePendingStylesheets;
249     bool m_didCalculateResolver;
250     OwnPtrWillBeMember<StyleResolver> m_resolver;
251
252     RefPtrWillBeMember<CSSFontSelector> m_fontSelector;
253
254     WillBeHeapHashMap<AtomicString, RawPtrWillBeMember<StyleSheetContents> > m_textToSheetCache;
255     WillBeHeapHashMap<RawPtrWillBeMember<StyleSheetContents>, AtomicString> m_sheetToTextCache;
256 };
257
258 }
259
260 #endif