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 PassOwnPtr<CachedStyleSheetContents> CachedStyleSheetContents::create(StyleSheet Contents* contents) | |
31 { | |
32 ASSERT(contents); | |
33 ASSERT(contents->isCacheable()); | |
34 return adoptPtr(new CachedStyleSheetContents(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); | |
61 } | |
62 | |
63 ASSERT(contents); | |
64 ASSERT(!sheetText.isEmpty()); | |
65 | |
66 Cache::AddResult result = m_cache.add(sheetText, nullptr); | |
67 if (result.isNewEntry) | |
68 result.iterator->value = CachedStyleSheetContents::create(contents); | |
69 | |
70 result.iterator->value->addref(); | |
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 if (it->value->deref()) | |
79 return; | |
80 m_cache.remove(sheetText); | |
81 } | |
82 | |
83 void StyleSheetContentsCache::sweep(Timer<StyleSheetContentsCache>*) | |
84 { | |
85 // Look for cache entries containing a style declaration with a single ref a nd remove them. | |
86 Vector<AtomicString*, 16> toRemove; | |
87 Cache::iterator it = m_cache.begin(); | |
88 Cache::iterator end = m_cache.end(); | |
89 for (; it != end; ++it) { | |
90 CachedStyleSheetContents* cacheItem = it->value.get(); | |
91 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
| |
92 toRemove.append(&it->key); | |
93 } | |
94 for (size_t i = 0; i < toRemove.size(); ++i) | |
95 m_cache.remove(*toRemove[i]); | |
96 | |
97 m_additionsSinceLastSweep = 0; | |
98 } | |
99 | |
100 void StyleSheetContentsCache::clear() | |
101 { | |
102 m_cache.clear(); | |
103 } | |
104 | |
105 } | |
OLD | NEW |