blob: 1bf7c244c4b5663a576bcafc9e72b55380898461 [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
2 Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de)
3 Copyright (C) 2001 Dirk Mueller <mueller@kde.org>
4 Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple 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 This class provides all functionality needed for loading images, style sheets and html
22 pages from the web. It has a memory cache for these objects.
23*/
24
25#ifndef Cache_h
26#define Cache_h
27
Ben Murdoch591b9582013-07-10 11:41:44 +010028#include "wtf/HashMap.h"
29#include "wtf/Noncopyable.h"
30#include "wtf/Vector.h"
31#include "wtf/text/StringHash.h"
32#include "wtf/text/WTFString.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010033
34namespace WebCore {
35
36class CachedCSSStyleSheet;
37class CachedResource;
38class CachedResourceLoader;
39class KURL;
40class ScriptExecutionContext;
41class SecurityOrigin;
42struct SecurityOriginHash;
43
44// This cache holds subresources used by Web pages: images, scripts, stylesheets, etc.
45
46// The cache keeps a flexible but bounded window of dead resources that grows/shrinks
47// depending on the live resource load. Here's an example of cache growth over time,
48// with a min dead resource capacity of 25% and a max dead resource capacity of 50%:
49
50// |-----| Dead: -
51// |----------| Live: +
52// --|----------| Cache boundary: | (objects outside this mark have been evicted)
53// --|----------++++++++++|
54// -------|-----+++++++++++++++|
55// -------|-----+++++++++++++++|+++++
56
Torne (Richard Coles)5267f702013-06-11 10:57:24 +010057// Enable this macro to periodically log information about the memory cache.
58#undef MEMORY_CACHE_STATS
59
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010060class MemoryCache {
61 WTF_MAKE_NONCOPYABLE(MemoryCache); WTF_MAKE_FAST_ALLOCATED;
62public:
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +010063 MemoryCache();
64 ~MemoryCache() { }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010065
66 typedef HashMap<String, CachedResource*> CachedResourceMap;
67
68 struct LRUList {
69 CachedResource* m_head;
70 CachedResource* m_tail;
71 LRUList() : m_head(0), m_tail(0) { }
72 };
73
74 struct TypeStatistic {
75 int count;
76 int size;
77 int liveSize;
78 int decodedSize;
Torne (Richard Coles)5267f702013-06-11 10:57:24 +010079 int encodedSize;
80 int encodedSizeDuplicatedInDataURLs;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010081 int purgeableSize;
82 int purgedSize;
Torne (Richard Coles)5267f702013-06-11 10:57:24 +010083
84 TypeStatistic()
85 : count(0)
86 , size(0)
87 , liveSize(0)
88 , decodedSize(0)
89 , encodedSize(0)
90 , encodedSizeDuplicatedInDataURLs(0)
91 , purgeableSize(0)
92 , purgedSize(0)
93 {
94 }
95
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010096 void addResource(CachedResource*);
97 };
Torne (Richard Coles)5267f702013-06-11 10:57:24 +010098
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010099 struct Statistics {
100 TypeStatistic images;
101 TypeStatistic cssStyleSheets;
102 TypeStatistic scripts;
103 TypeStatistic xslStyleSheets;
104 TypeStatistic fonts;
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100105 TypeStatistic other;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100106 };
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100107
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100108 CachedResource* resourceForURL(const KURL&);
109
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +0100110 void add(CachedResource*);
Ben Murdoche69819b2013-07-17 14:56:49 +0100111 void replace(CachedResource* newResource, CachedResource* oldResource);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100112 void remove(CachedResource* resource) { evict(resource); }
113
114 static KURL removeFragmentIdentifierIfNeeded(const KURL& originalURL);
115
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100116 // Sets the cache's memory capacities, in bytes. These will hold only approximately,
117 // since the decoded cost of resources like scripts and stylesheets is not known.
118 // - minDeadBytes: The maximum number of bytes that dead resources should consume when the cache is under pressure.
119 // - maxDeadBytes: The maximum number of bytes that dead resources should consume when the cache is not under pressure.
120 // - totalBytes: The maximum number of bytes that the cache should consume overall.
121 void setCapacities(unsigned minDeadBytes, unsigned maxDeadBytes, unsigned totalBytes);
122
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100123 void evictResources();
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +0100124
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100125 void prune();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100126
127 // Calls to put the cached resource into and out of LRU lists.
128 void insertInLRUList(CachedResource*);
129 void removeFromLRUList(CachedResource*);
130
131 // Called to adjust the cache totals when a resource changes size.
132 void adjustSize(bool live, int delta);
133
134 // Track decoded resources that are in the cache and referenced by a Web page.
135 void insertInLiveDecodedResourcesList(CachedResource*);
136 void removeFromLiveDecodedResourcesList(CachedResource*);
137
138 void addToLiveResourcesSize(CachedResource*);
139 void removeFromLiveResourcesSize(CachedResource*);
140
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100141 static void removeURLFromCache(ScriptExecutionContext*, const KURL&);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100142
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100143 Statistics getStatistics();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100144
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100145 unsigned minDeadCapacity() const { return m_minDeadCapacity; }
146 unsigned maxDeadCapacity() const { return m_maxDeadCapacity; }
147 unsigned capacity() const { return m_capacity; }
148 unsigned liveSize() const { return m_liveSize; }
149 unsigned deadSize() const { return m_deadSize; }
150
151 void reportMemoryUsage(MemoryObjectInfo*) const;
152
153private:
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100154 LRUList* lruListFor(CachedResource*);
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100155
156#ifdef MEMORY_CACHE_STATS
157 void dumpStats(Timer<MemoryCache>*);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100158 void dumpLRULists(bool includeLive) const;
159#endif
160
161 unsigned liveCapacity() const;
162 unsigned deadCapacity() const;
163
Ben Murdoche69819b2013-07-17 14:56:49 +0100164 // pruneDeadResources() - Flush decoded and encoded data from resources not referenced by Web pages.
165 // pruneLiveResources() - Flush decoded data from resources still referenced by Web pages.
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100166 void pruneDeadResources(); // Automatically decide how much to prune.
167 void pruneLiveResources();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100168
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100169 void evict(CachedResource*);
170
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100171 static void removeURLFromCacheInternal(ScriptExecutionContext*, const KURL&);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100172
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100173 bool m_inPruneResources;
174
175 unsigned m_capacity;
176 unsigned m_minDeadCapacity;
177 unsigned m_maxDeadCapacity;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100178
179 unsigned m_liveSize; // The number of bytes currently consumed by "live" resources in the cache.
180 unsigned m_deadSize; // The number of bytes currently consumed by "dead" resources in the cache.
181
182 // Size-adjusted and popularity-aware LRU list collection for cache objects. This collection can hold
183 // more resources than the cached resource map, since it can also hold "stale" multiple versions of objects that are
184 // waiting to die when the clients referencing them go away.
185 Vector<LRUList, 32> m_allResources;
186
187 // List just for live resources with decoded data. Access to this list is based off of painting the resource.
188 LRUList m_liveDecodedResources;
189
190 // A URL-based map of all resources that are in the cache (including the freshest version of objects that are currently being
191 // referenced by a Web page).
192 HashMap<String, CachedResource*> m_resources;
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100193
194#ifdef MEMORY_CACHE_STATS
195 Timer<MemoryCache> m_statsTimer;
196#endif
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100197};
198
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100199// Returns the global cache.
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100200MemoryCache* memoryCache();
201
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100202// Sets the global cache, used to swap in a test instance.
203void setMemoryCacheForTesting(MemoryCache*);
204
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100205}
206
207#endif