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..10b9f1e5787b2da012a776bf1cc01e86c3eedd8b |
--- /dev/null |
+++ b/Source/core/css/StyleSheetContentsCache.cpp |
@@ -0,0 +1,105 @@ |
+/* |
+ * 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 { |
+ |
+PassOwnPtr<CachedStyleSheetContents> CachedStyleSheetContents::create(StyleSheetContents* contents) |
+{ |
+ ASSERT(contents); |
+ ASSERT(contents->isCacheable()); |
+ return adoptPtr(new CachedStyleSheetContents(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); |
+ } |
+ |
+ ASSERT(contents); |
+ ASSERT(!sheetText.isEmpty()); |
+ |
+ Cache::AddResult result = m_cache.add(sheetText, nullptr); |
+ if (result.isNewEntry) |
+ result.iterator->value = CachedStyleSheetContents::create(contents); |
+ |
+ result.iterator->value->addref(); |
+} |
+ |
+void StyleSheetContentsCache::remove(const AtomicString& sheetText) |
+{ |
+ Cache::iterator it = m_cache.find(sheetText); |
+ if (it == m_cache.end()) |
+ return; |
+ if (it->value->deref()) |
+ return; |
+ m_cache.remove(sheetText); |
+} |
+ |
+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) { |
+ CachedStyleSheetContents* cacheItem = it->value.get(); |
+ if (cacheItem->hasOneRef()) |
dglazkov
2013/10/21 18:24:10
I see. Can you not just make them RefPtrs and swee
tasak
2013/10/25 05:23:13
I see.
I changed the code to use RefPtrs and to re
|
+ 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(); |
+} |
+ |
+} |