Home | History | Annotate | Download | only in resolver
      1 /*
      2  * Copyright (C) 1999 Lars Knoll (knoll (at) kde.org)
      3  *           (C) 2004-2005 Allan Sandfeld Jensen (kde (at) carewolf.com)
      4  * Copyright (C) 2006, 2007 Nicholas Shanks (webkit (at) 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 (at) webkit.org>
      7  * Copyright (C) 2007, 2008 Eric Seidel <eric (at) 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         HeapVector<MatchedProperties>::iterator it = cachedProperties->matchedProperties.begin();
     49         HeapVector<MatchedProperties>::iterator end = cachedProperties->matchedProperties.end();
     50         for (; it != end; ++it) {
     51             if (!visitor->isAlive(it->properties)) {
     52                 // For now report the cache entry as dead. This might not
     53                 // be the final result if in a subsequent call for this entry,
     54                 // the "properties" field has been marked via another path.
     55                 return true;
     56             }
     57         }
     58     }
     59     // At this point none of the entries in the matchedProperties vector
     60     // had a dead "properties" field so trace CachedMatchedProperties strongly.
     61     visitor->trace(cachedProperties);
     62     return false;
     63 }
     64 #endif
     65 
     66 void CachedMatchedProperties::set(const RenderStyle* style, const RenderStyle* parentStyle, const MatchResult& matchResult)
     67 {
     68     matchedProperties.appendVector(matchResult.matchedProperties);
     69     ranges = matchResult.ranges;
     70 
     71     // Note that we don't cache the original RenderStyle instance. It may be further modified.
     72     // The RenderStyle in the cache is really just a holder for the substructures and never used as-is.
     73     this->renderStyle = RenderStyle::clone(style);
     74     this->parentRenderStyle = RenderStyle::clone(parentStyle);
     75 }
     76 
     77 void CachedMatchedProperties::clear()
     78 {
     79     matchedProperties.clear();
     80     renderStyle = nullptr;
     81     parentRenderStyle = nullptr;
     82 }
     83 
     84 MatchedPropertiesCache::MatchedPropertiesCache()
     85 #if !ENABLE(OILPAN)
     86     : m_additionsSinceLastSweep(0)
     87     , m_sweepTimer(this, &MatchedPropertiesCache::sweep)
     88 #endif
     89 {
     90 }
     91 
     92 const CachedMatchedProperties* MatchedPropertiesCache::find(unsigned hash, const StyleResolverState& styleResolverState, const MatchResult& matchResult)
     93 {
     94     ASSERT(hash);
     95 
     96     Cache::iterator it = m_cache.find(hash);
     97     if (it == m_cache.end())
     98         return 0;
     99     CachedMatchedProperties* cacheItem = it->value.get();
    100     ASSERT(cacheItem);
    101 
    102     size_t size = matchResult.matchedProperties.size();
    103     if (size != cacheItem->matchedProperties.size())
    104         return 0;
    105     if (cacheItem->renderStyle->insideLink() != styleResolverState.style()->insideLink())
    106         return 0;
    107     for (size_t i = 0; i < size; ++i) {
    108         if (matchResult.matchedProperties[i] != cacheItem->matchedProperties[i])
    109             return 0;
    110     }
    111     if (cacheItem->ranges != matchResult.ranges)
    112         return 0;
    113     return cacheItem;
    114 }
    115 
    116 void MatchedPropertiesCache::add(const RenderStyle* style, const RenderStyle* parentStyle, unsigned hash, const MatchResult& matchResult)
    117 {
    118 #if !ENABLE(OILPAN)
    119     static const unsigned maxAdditionsBetweenSweeps = 100;
    120     if (++m_additionsSinceLastSweep >= maxAdditionsBetweenSweeps
    121         && !m_sweepTimer.isActive()) {
    122         static const unsigned sweepTimeInSeconds = 60;
    123         m_sweepTimer.startOneShot(sweepTimeInSeconds, FROM_HERE);
    124     }
    125 #endif
    126 
    127     ASSERT(hash);
    128     Cache::AddResult addResult = m_cache.add(hash, nullptr);
    129     if (addResult.isNewEntry)
    130         addResult.storedValue->value = adoptPtrWillBeNoop(new CachedMatchedProperties);
    131 
    132     CachedMatchedProperties* cacheItem = addResult.storedValue->value.get();
    133     if (!addResult.isNewEntry)
    134         cacheItem->clear();
    135 
    136     cacheItem->set(style, parentStyle, matchResult);
    137 }
    138 
    139 void MatchedPropertiesCache::clear()
    140 {
    141     m_cache.clear();
    142 }
    143 
    144 void MatchedPropertiesCache::clearViewportDependent()
    145 {
    146     Vector<unsigned, 16> toRemove;
    147     for (Cache::iterator it = m_cache.begin(); it != m_cache.end(); ++it) {
    148         CachedMatchedProperties* cacheItem = it->value.get();
    149         if (cacheItem->renderStyle->hasViewportUnits())
    150             toRemove.append(it->key);
    151     }
    152     m_cache.removeAll(toRemove);
    153 }
    154 
    155 #if !ENABLE(OILPAN)
    156 void MatchedPropertiesCache::sweep(Timer<MatchedPropertiesCache>*)
    157 {
    158     // Look for cache entries containing a style declaration with a single ref and remove them.
    159     // This may happen when an element attribute mutation causes it to generate a new inlineStyle()
    160     // or presentationAttributeStyle(), potentially leaving this cache with the last ref on the old one.
    161     Vector<unsigned, 16> toRemove;
    162     Cache::iterator it = m_cache.begin();
    163     Cache::iterator end = m_cache.end();
    164     for (; it != end; ++it) {
    165         CachedMatchedProperties* cacheItem = it->value.get();
    166         Vector<MatchedProperties>& matchedProperties = cacheItem->matchedProperties;
    167         for (size_t i = 0; i < matchedProperties.size(); ++i) {
    168             if (matchedProperties[i].properties->hasOneRef()) {
    169                 toRemove.append(it->key);
    170                 break;
    171             }
    172         }
    173     }
    174     m_cache.removeAll(toRemove);
    175     m_additionsSinceLastSweep = 0;
    176 }
    177 #endif
    178 
    179 bool MatchedPropertiesCache::isCacheable(const Element* element, const RenderStyle* style, const RenderStyle* parentStyle)
    180 {
    181     // FIXME: CSSPropertyWebkitWritingMode modifies state when applying to document element. We can't skip the applying by caching.
    182     if (element == element->document().documentElement() && element->document().writingModeSetOnDocumentElement())
    183         return false;
    184     if (style->unique() || (style->styleType() != NOPSEUDO && parentStyle->unique()))
    185         return false;
    186     if (style->hasAppearance())
    187         return false;
    188     if (style->zoom() != RenderStyle::initialZoom())
    189         return false;
    190     if (style->writingMode() != RenderStyle::initialWritingMode())
    191         return false;
    192     // The cache assumes static knowledge about which properties are inherited.
    193     if (parentStyle->hasExplicitlyInheritedProperties())
    194         return false;
    195     return true;
    196 }
    197 
    198 void MatchedPropertiesCache::trace(Visitor* visitor)
    199 {
    200 #if ENABLE(OILPAN)
    201     visitor->trace(m_cache);
    202 #endif
    203 }
    204 
    205 }
    206