Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / css / resolver / MatchedPropertiesCache.cpp
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 2004-2005 Allan Sandfeld Jensen (kde@carewolf.com)
4  * Copyright (C) 2006, 2007 Nicholas Shanks (webkit@nickshanks.com)
5  * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Apple Inc. All rights reserved.
6  * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
7  * Copyright (C) 2007, 2008 Eric Seidel <eric@webkit.org>
8  * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
9  * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
10  * Copyright (C) Research In Motion Limited 2011. All rights reserved.
11  * Copyright (C) 2013 Google Inc. All rights reserved.
12  *
13  * This library is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Library General Public
15  * License as published by the Free Software Foundation; either
16  * version 2 of the License, or (at your option) any later version.
17  *
18  * This library is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * Library General Public License for more details.
22  *
23  * You should have received a copy of the GNU Library General Public License
24  * along with this library; see the file COPYING.LIB.  If not, write to
25  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26  * Boston, MA 02110-1301, USA.
27  */
28
29 #include "config.h"
30 #include "core/css/resolver/MatchedPropertiesCache.h"
31
32 #include "core/css/StylePropertySet.h"
33 #include "core/css/resolver/StyleResolverState.h"
34 #include "core/rendering/style/RenderStyle.h"
35
36 namespace blink {
37
38 #if ENABLE(OILPAN)
39 bool CachedMatchedPropertiesHashTraits::traceInCollection(Visitor* visitor, Member<CachedMatchedProperties>& cachedProperties, WTF::ShouldWeakPointersBeMarkedStrongly strongify)
40 {
41     // Only honor the cache's weakness semantics if the collection is traced
42     // with WeakPointersActWeak. Otherwise just trace the cachedProperties
43     // strongly, ie. call trace on it.
44     if (cachedProperties && strongify == WTF::WeakPointersActWeak) {
45         // A given cache entry is only kept alive if none of the MatchedProperties
46         // in the CachedMatchedProperties value contain a dead "properties" field.
47         // If there is a dead field the entire cache entry is removed.
48         for (const auto& matchedProperties : cachedProperties->matchedProperties) {
49             if (!visitor->isAlive(matchedProperties.properties)) {
50                 // For now report the cache entry as dead. This might not
51                 // be the final result if in a subsequent call for this entry,
52                 // the "properties" field has been marked via another path.
53                 return true;
54             }
55         }
56     }
57     // At this point none of the entries in the matchedProperties vector
58     // had a dead "properties" field so trace CachedMatchedProperties strongly.
59     visitor->trace(cachedProperties);
60     return false;
61 }
62 #endif
63
64 void CachedMatchedProperties::set(const RenderStyle* style, const RenderStyle* parentStyle, const MatchResult& matchResult)
65 {
66     matchedProperties.appendVector(matchResult.matchedProperties);
67     ranges = matchResult.ranges;
68
69     // Note that we don't cache the original RenderStyle instance. It may be further modified.
70     // The RenderStyle in the cache is really just a holder for the substructures and never used as-is.
71     this->renderStyle = RenderStyle::clone(style);
72     this->parentRenderStyle = RenderStyle::clone(parentStyle);
73 }
74
75 void CachedMatchedProperties::clear()
76 {
77     matchedProperties.clear();
78     renderStyle = nullptr;
79     parentRenderStyle = nullptr;
80 }
81
82 MatchedPropertiesCache::MatchedPropertiesCache()
83 #if !ENABLE(OILPAN)
84     : m_additionsSinceLastSweep(0)
85     , m_sweepTimer(this, &MatchedPropertiesCache::sweep)
86 #endif
87 {
88 }
89
90 const CachedMatchedProperties* MatchedPropertiesCache::find(unsigned hash, const StyleResolverState& styleResolverState, const MatchResult& matchResult)
91 {
92     ASSERT(hash);
93
94     Cache::iterator it = m_cache.find(hash);
95     if (it == m_cache.end())
96         return 0;
97     CachedMatchedProperties* cacheItem = it->value.get();
98     ASSERT(cacheItem);
99
100     size_t size = matchResult.matchedProperties.size();
101     if (size != cacheItem->matchedProperties.size())
102         return 0;
103     if (cacheItem->renderStyle->insideLink() != styleResolverState.style()->insideLink())
104         return 0;
105     for (size_t i = 0; i < size; ++i) {
106         if (matchResult.matchedProperties[i] != cacheItem->matchedProperties[i])
107             return 0;
108     }
109     if (cacheItem->ranges != matchResult.ranges)
110         return 0;
111     return cacheItem;
112 }
113
114 void MatchedPropertiesCache::add(const RenderStyle* style, const RenderStyle* parentStyle, unsigned hash, const MatchResult& matchResult)
115 {
116 #if !ENABLE(OILPAN)
117     static const unsigned maxAdditionsBetweenSweeps = 100;
118     if (++m_additionsSinceLastSweep >= maxAdditionsBetweenSweeps
119         && !m_sweepTimer.isActive()) {
120         static const unsigned sweepTimeInSeconds = 60;
121         m_sweepTimer.startOneShot(sweepTimeInSeconds, FROM_HERE);
122     }
123 #endif
124
125     ASSERT(hash);
126     Cache::AddResult addResult = m_cache.add(hash, nullptr);
127     if (addResult.isNewEntry)
128         addResult.storedValue->value = adoptPtrWillBeNoop(new CachedMatchedProperties);
129
130     CachedMatchedProperties* cacheItem = addResult.storedValue->value.get();
131     if (!addResult.isNewEntry)
132         cacheItem->clear();
133
134     cacheItem->set(style, parentStyle, matchResult);
135 }
136
137 void MatchedPropertiesCache::clear()
138 {
139     m_cache.clear();
140 }
141
142 void MatchedPropertiesCache::clearViewportDependent()
143 {
144     Vector<unsigned, 16> toRemove;
145     for (const auto& cacheEntry : m_cache) {
146         CachedMatchedProperties* cacheItem = cacheEntry.value.get();
147         if (cacheItem->renderStyle->hasViewportUnits())
148             toRemove.append(cacheEntry.key);
149     }
150     m_cache.removeAll(toRemove);
151 }
152
153 #if !ENABLE(OILPAN)
154 void MatchedPropertiesCache::sweep(Timer<MatchedPropertiesCache>*)
155 {
156     // Look for cache entries containing a style declaration with a single ref and remove them.
157     // This may happen when an element attribute mutation causes it to generate a new inlineStyle()
158     // or presentationAttributeStyle(), potentially leaving this cache with the last ref on the old one.
159     Vector<unsigned, 16> toRemove;
160     for (const auto& cacheEntry : m_cache) {
161         CachedMatchedProperties* cacheItem = cacheEntry.value.get();
162         Vector<MatchedProperties>& matchedProperties = cacheItem->matchedProperties;
163         for (size_t i = 0; i < matchedProperties.size(); ++i) {
164             if (matchedProperties[i].properties->hasOneRef()) {
165                 toRemove.append(cacheEntry.key);
166                 break;
167             }
168         }
169     }
170     m_cache.removeAll(toRemove);
171     m_additionsSinceLastSweep = 0;
172 }
173 #endif
174
175 bool MatchedPropertiesCache::isCacheable(const Element* element, const RenderStyle* style, const RenderStyle* parentStyle)
176 {
177     // FIXME: CSSPropertyWebkitWritingMode modifies state when applying to document element. We can't skip the applying by caching.
178     if (element == element->document().documentElement() && element->document().writingModeSetOnDocumentElement())
179         return false;
180     if (style->unique() || (style->styleType() != NOPSEUDO && parentStyle->unique()))
181         return false;
182     if (style->hasAppearance())
183         return false;
184     if (style->zoom() != RenderStyle::initialZoom())
185         return false;
186     if (style->writingMode() != RenderStyle::initialWritingMode())
187         return false;
188     // The cache assumes static knowledge about which properties are inherited.
189     if (parentStyle->hasExplicitlyInheritedProperties())
190         return false;
191     return true;
192 }
193
194 void MatchedPropertiesCache::trace(Visitor* visitor)
195 {
196 #if ENABLE(OILPAN)
197     visitor->trace(m_cache);
198 #endif
199 }
200
201 }