2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "core/dom/PresentationAttributeStyle.h"
34 #include "core/css/StylePropertySet.h"
35 #include "core/dom/Attribute.h"
36 #include "core/dom/Element.h"
37 #include "core/html/HTMLInputElement.h"
38 #include "platform/Timer.h"
39 #include "wtf/HashFunctions.h"
40 #include "wtf/HashMap.h"
41 #include "wtf/text/CString.h"
45 using namespace HTMLNames;
47 struct PresentationAttributeCacheKey {
48 PresentationAttributeCacheKey() : tagName(0) { }
50 Vector<std::pair<StringImpl*, AtomicString>, 3> attributesAndValues;
53 static bool operator!=(const PresentationAttributeCacheKey& a, const PresentationAttributeCacheKey& b)
55 if (a.tagName != b.tagName)
57 return a.attributesAndValues != b.attributesAndValues;
60 struct PresentationAttributeCacheEntry FINAL : public NoBaseWillBeGarbageCollectedFinalized<PresentationAttributeCacheEntry> {
61 WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
63 void trace(Visitor* visitor) { visitor->trace(value); }
65 PresentationAttributeCacheKey key;
66 RefPtrWillBeMember<StylePropertySet> value;
69 typedef WillBeHeapHashMap<unsigned, OwnPtrWillBeMember<PresentationAttributeCacheEntry>, AlreadyHashed> PresentationAttributeCache;
70 static PresentationAttributeCache& presentationAttributeCache()
72 DEFINE_STATIC_LOCAL(OwnPtrWillBePersistent<PresentationAttributeCache>, cache, (adoptPtrWillBeNoop(new PresentationAttributeCache())));
76 class PresentationAttributeCacheCleaner {
77 WTF_MAKE_NONCOPYABLE(PresentationAttributeCacheCleaner); WTF_MAKE_FAST_ALLOCATED;
79 PresentationAttributeCacheCleaner()
81 , m_cleanTimer(this, &PresentationAttributeCacheCleaner::cleanCache)
85 void didHitPresentationAttributeCache()
87 if (presentationAttributeCache().size() < minimumPresentationAttributeCacheSizeForCleaning)
92 if (!m_cleanTimer.isActive())
93 m_cleanTimer.startOneShot(presentationAttributeCacheCleanTimeInSeconds, FROM_HERE);
97 static const unsigned presentationAttributeCacheCleanTimeInSeconds = 60;
98 static const unsigned minimumPresentationAttributeCacheSizeForCleaning = 100;
99 static const unsigned minimumPresentationAttributeCacheHitCountPerMinute = (100 * presentationAttributeCacheCleanTimeInSeconds) / 60;
101 void cleanCache(Timer<PresentationAttributeCacheCleaner>* timer)
103 ASSERT_UNUSED(timer, timer == &m_cleanTimer);
104 unsigned hitCount = m_hitCount;
106 if (hitCount > minimumPresentationAttributeCacheHitCountPerMinute)
108 presentationAttributeCache().clear();
112 Timer<PresentationAttributeCacheCleaner> m_cleanTimer;
115 static bool attributeNameSort(const pair<StringImpl*, AtomicString>& p1, const pair<StringImpl*, AtomicString>& p2)
117 // Sort based on the attribute name pointers. It doesn't matter what the order is as long as it is always the same.
118 return p1.first < p2.first;
121 static void makePresentationAttributeCacheKey(Element& element, PresentationAttributeCacheKey& result)
123 // FIXME: Enable for SVG.
124 if (!element.isHTMLElement())
126 // Interpretation of the size attributes on <input> depends on the type attribute.
127 if (isHTMLInputElement(element))
129 AttributeCollection attributes = element.attributesWithoutUpdate();
130 AttributeCollection::iterator end = attributes.end();
131 for (AttributeCollection::iterator it = attributes.begin(); it != end; ++it) {
132 if (!element.isPresentationAttribute(it->name()))
134 if (!it->namespaceURI().isNull())
136 // FIXME: Background URL may depend on the base URL and can't be shared. Disallow caching.
137 if (it->name() == backgroundAttr)
139 result.attributesAndValues.append(std::make_pair(it->localName().impl(), it->value()));
141 if (result.attributesAndValues.isEmpty())
143 // Attribute order doesn't matter. Sort for easy equality comparison.
144 std::sort(result.attributesAndValues.begin(), result.attributesAndValues.end(), attributeNameSort);
145 // The cache key is non-null when the tagName is set.
146 result.tagName = element.localName().impl();
149 static unsigned computePresentationAttributeCacheHash(const PresentationAttributeCacheKey& key)
153 ASSERT(key.attributesAndValues.size());
154 unsigned attributeHash = StringHasher::hashMemory(key.attributesAndValues.data(), key.attributesAndValues.size() * sizeof(key.attributesAndValues[0]));
155 return WTF::pairIntHash(key.tagName->existingHash(), attributeHash);
158 PassRefPtrWillBeRawPtr<StylePropertySet> computePresentationAttributeStyle(Element& element)
160 DEFINE_STATIC_LOCAL(PresentationAttributeCacheCleaner, cacheCleaner, ());
162 ASSERT(element.isStyledElement());
164 PresentationAttributeCacheKey cacheKey;
165 makePresentationAttributeCacheKey(element, cacheKey);
167 unsigned cacheHash = computePresentationAttributeCacheHash(cacheKey);
169 PresentationAttributeCache::ValueType* cacheValue;
171 cacheValue = presentationAttributeCache().add(cacheHash, nullptr).storedValue;
172 if (cacheValue->value && cacheValue->value->key != cacheKey)
178 RefPtrWillBeRawPtr<StylePropertySet> style = nullptr;
179 if (cacheHash && cacheValue->value) {
180 style = cacheValue->value->value;
181 cacheCleaner.didHitPresentationAttributeCache();
183 style = MutableStylePropertySet::create(element.isSVGElement() ? SVGAttributeMode : HTMLAttributeMode);
184 AttributeCollection attributes = element.attributesWithoutUpdate();
185 AttributeCollection::iterator end = attributes.end();
186 for (AttributeCollection::iterator it = attributes.begin(); it != end; ++it)
187 element.collectStyleForPresentationAttribute(it->name(), it->value(), toMutableStylePropertySet(style));
190 if (!cacheHash || cacheValue->value)
191 return style.release();
193 OwnPtrWillBeRawPtr<PresentationAttributeCacheEntry> newEntry = adoptPtrWillBeNoop(new PresentationAttributeCacheEntry);
194 newEntry->key = cacheKey;
195 newEntry->value = style;
197 static const unsigned presentationAttributeCacheMaximumSize = 4096;
198 if (presentationAttributeCache().size() > presentationAttributeCacheMaximumSize) {
199 // FIXME: Discarding the entire cache when it gets too big is probably bad
200 // since it creates a perf "cliff". Perhaps we should use an LRU?
201 presentationAttributeCache().clear();
202 presentationAttributeCache().set(cacheHash, newEntry.release());
204 cacheValue->value = newEntry.release();
207 return style.release();