Chromium Code Reviews| Index: Source/core/css/StyleSheetContentsCache.cpp |
| diff --git a/Source/core/css/StyleSheetContentsCache.cpp b/Source/core/css/StyleSheetContentsCache.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..73c1c8a59c519f7b13f2386696e4d8c123a2ef26 |
| --- /dev/null |
| +++ b/Source/core/css/StyleSheetContentsCache.cpp |
| @@ -0,0 +1,107 @@ |
| +/* |
| + * Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
| + * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved. |
| + * Copyright (C) 2013 Google Inc. All rights reserved. |
| + * |
| + * This library is free software; you can redistribute it and/or |
| + * modify it under the terms of the GNU Library General Public |
| + * License as published by the Free Software Foundation; either |
| + * version 2 of the License, or (at your option) any later version. |
| + * |
| + * This library is distributed in the hope that it will be useful, |
| + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| + * Library General Public License for more details. |
| + * |
| + * You should have received a copy of the GNU Library General Public License |
| + * along with this library; see the file COPYING.LIB. If not, write to |
| + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| + * Boston, MA 02110-1301, USA. |
| + * |
| + */ |
| + |
| +#include "config.h" |
| +#include "core/css/StyleSheetContentsCache.h" |
| + |
| +#include "core/css/StyleSheetContents.h" |
| + |
| +namespace WebCore { |
| + |
| +PassRefPtr<StyleSheetContentsCacheItem> StyleSheetContentsCacheItem::create(StyleSheetContents* contents) |
| +{ |
| + ASSERT(contents); |
| + ASSERT(contents->isCacheable()); |
| + return adoptRef(new StyleSheetContentsCacheItem(contents->copy())); |
| +} |
| + |
| +StyleSheetContentsCache::StyleSheetContentsCache() |
| + : m_additionsSinceLastSweep(0) |
| + , m_sweepTimer(this, &StyleSheetContentsCache::sweep) |
| +{ |
| +} |
| + |
| +StyleSheetContents* StyleSheetContentsCache::find(const AtomicString& sheetText) |
| +{ |
| + if (sheetText.isEmpty()) |
| + return 0; |
| + |
| + Cache::iterator it = m_cache.find(sheetText); |
| + if (it == m_cache.end()) |
| + return 0; |
| + return it->value->contents(); |
| +} |
| + |
| +void StyleSheetContentsCache::add(const AtomicString& sheetText, StyleSheetContents* contents) |
| +{ |
| + static const unsigned maxAdditionsBetweenSweeps = 100; |
| + if (++m_additionsSinceLastSweep >= maxAdditionsBetweenSweeps |
| + && !m_sweepTimer.isActive()) { |
| + static const unsigned sweepTimeInSeconds = 60; |
| + m_sweepTimer.startOneShot(sweepTimeInSeconds); |
|
esprehn
2013/10/30 21:33:50
On second thought I think this timer is too comple
|
| + } |
| + |
| + ASSERT(contents); |
| + ASSERT(!sheetText.isEmpty()); |
| + |
| + Cache::AddResult result = m_cache.add(sheetText, 0); |
| + if (result.isNewEntry) |
| + result.iterator->value = StyleSheetContentsCacheItem::create(contents); |
| + else |
| + result.iterator->value->ref(); |
| +} |
| + |
| +void StyleSheetContentsCache::remove(const AtomicString& sheetText) |
| +{ |
| + Cache::iterator it = m_cache.find(sheetText); |
| + if (it == m_cache.end()) |
| + return; |
| + |
| + if (it->value->hasOneRef()) { |
| + m_cache.remove(sheetText); |
| + return; |
| + } |
| + it->value->deref(); |
| +} |
| + |
| +void StyleSheetContentsCache::sweep(Timer<StyleSheetContentsCache>*) |
| +{ |
| + // Look for cache entries containing a style declaration with a single ref and remove them. |
| + Vector<AtomicString*, 16> toRemove; |
| + Cache::iterator it = m_cache.begin(); |
| + Cache::iterator end = m_cache.end(); |
| + for (; it != end; ++it) { |
| + if (it->value->hasOneRef()) |
| + toRemove.append(&it->key); |
| + } |
| + for (size_t i = 0; i < toRemove.size(); ++i) |
| + m_cache.remove(*toRemove[i]); |
| + |
| + m_additionsSinceLastSweep = 0; |
| +} |
| + |
| +void StyleSheetContentsCache::clear() |
| +{ |
| + m_cache.clear(); |
| +} |
| + |
| +} |