Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) | |
| 3 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved. | |
| 4 * Copyright (C) 2013 Google Inc. All rights reserved. | |
| 5 * | |
| 6 * This library is free software; you can redistribute it and/or | |
| 7 * modify it under the terms of the GNU Library General Public | |
| 8 * License as published by the Free Software Foundation; either | |
| 9 * version 2 of the License, or (at your option) any later version. | |
| 10 * | |
| 11 * This library is distributed in the hope that it will be useful, | |
| 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 14 * Library General Public License for more details. | |
| 15 * | |
| 16 * You should have received a copy of the GNU Library General Public License | |
| 17 * along with this library; see the file COPYING.LIB. If not, write to | |
| 18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
| 19 * Boston, MA 02110-1301, USA. | |
| 20 * | |
| 21 */ | |
| 22 | |
| 23 #include "config.h" | |
| 24 #include "core/css/StyleSheetContentsCache.h" | |
| 25 | |
| 26 #include "core/css/StyleSheetContents.h" | |
| 27 | |
| 28 namespace WebCore { | |
| 29 | |
| 30 PassRefPtr<StyleSheetContentsCacheItem> StyleSheetContentsCacheItem::create(Styl eSheetContents* contents) | |
| 31 { | |
| 32 ASSERT(contents); | |
| 33 ASSERT(contents->isCacheable()); | |
| 34 return adoptRef(new StyleSheetContentsCacheItem(contents->copy())); | |
| 35 } | |
| 36 | |
| 37 StyleSheetContentsCache::StyleSheetContentsCache() | |
| 38 : m_additionsSinceLastSweep(0) | |
| 39 , m_sweepTimer(this, &StyleSheetContentsCache::sweep) | |
| 40 { | |
| 41 } | |
| 42 | |
| 43 StyleSheetContents* StyleSheetContentsCache::find(const AtomicString& sheetText) | |
| 44 { | |
| 45 if (sheetText.isEmpty()) | |
| 46 return 0; | |
| 47 | |
| 48 Cache::iterator it = m_cache.find(sheetText); | |
| 49 if (it == m_cache.end()) | |
| 50 return 0; | |
| 51 return it->value->contents(); | |
| 52 } | |
| 53 | |
| 54 void StyleSheetContentsCache::add(const AtomicString& sheetText, StyleSheetConte nts* contents) | |
| 55 { | |
| 56 static const unsigned maxAdditionsBetweenSweeps = 100; | |
| 57 if (++m_additionsSinceLastSweep >= maxAdditionsBetweenSweeps | |
| 58 && !m_sweepTimer.isActive()) { | |
| 59 static const unsigned sweepTimeInSeconds = 60; | |
| 60 m_sweepTimer.startOneShot(sweepTimeInSeconds); | |
|
esprehn
2013/10/30 21:33:50
On second thought I think this timer is too comple
| |
| 61 } | |
| 62 | |
| 63 ASSERT(contents); | |
| 64 ASSERT(!sheetText.isEmpty()); | |
| 65 | |
| 66 Cache::AddResult result = m_cache.add(sheetText, 0); | |
| 67 if (result.isNewEntry) | |
| 68 result.iterator->value = StyleSheetContentsCacheItem::create(contents); | |
| 69 else | |
| 70 result.iterator->value->ref(); | |
| 71 } | |
| 72 | |
| 73 void StyleSheetContentsCache::remove(const AtomicString& sheetText) | |
| 74 { | |
| 75 Cache::iterator it = m_cache.find(sheetText); | |
| 76 if (it == m_cache.end()) | |
| 77 return; | |
| 78 | |
| 79 if (it->value->hasOneRef()) { | |
| 80 m_cache.remove(sheetText); | |
| 81 return; | |
| 82 } | |
| 83 it->value->deref(); | |
| 84 } | |
| 85 | |
| 86 void StyleSheetContentsCache::sweep(Timer<StyleSheetContentsCache>*) | |
| 87 { | |
| 88 // Look for cache entries containing a style declaration with a single ref a nd remove them. | |
| 89 Vector<AtomicString*, 16> toRemove; | |
| 90 Cache::iterator it = m_cache.begin(); | |
| 91 Cache::iterator end = m_cache.end(); | |
| 92 for (; it != end; ++it) { | |
| 93 if (it->value->hasOneRef()) | |
| 94 toRemove.append(&it->key); | |
| 95 } | |
| 96 for (size_t i = 0; i < toRemove.size(); ++i) | |
| 97 m_cache.remove(*toRemove[i]); | |
| 98 | |
| 99 m_additionsSinceLastSweep = 0; | |
| 100 } | |
| 101 | |
| 102 void StyleSheetContentsCache::clear() | |
| 103 { | |
| 104 m_cache.clear(); | |
| 105 } | |
| 106 | |
| 107 } | |
| OLD | NEW |