Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1 | /* |
| 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 Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 28 | #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) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 33 | |
| 34 | namespace WebCore { |
| 35 | |
| 36 | class CachedCSSStyleSheet; |
| 37 | class CachedResource; |
| 38 | class CachedResourceLoader; |
| 39 | class KURL; |
| 40 | class ScriptExecutionContext; |
| 41 | class SecurityOrigin; |
| 42 | struct 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) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 57 | // Enable this macro to periodically log information about the memory cache. |
| 58 | #undef MEMORY_CACHE_STATS |
| 59 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 60 | class MemoryCache { |
| 61 | WTF_MAKE_NONCOPYABLE(MemoryCache); WTF_MAKE_FAST_ALLOCATED; |
| 62 | public: |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 63 | MemoryCache(); |
| 64 | ~MemoryCache() { } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 65 | |
| 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) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 79 | int encodedSize; |
| 80 | int encodedSizeDuplicatedInDataURLs; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 81 | int purgeableSize; |
| 82 | int purgedSize; |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 83 | |
| 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) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 96 | void addResource(CachedResource*); |
| 97 | }; |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 98 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 99 | struct Statistics { |
| 100 | TypeStatistic images; |
| 101 | TypeStatistic cssStyleSheets; |
| 102 | TypeStatistic scripts; |
| 103 | TypeStatistic xslStyleSheets; |
| 104 | TypeStatistic fonts; |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 105 | TypeStatistic other; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 106 | }; |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 107 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 108 | CachedResource* resourceForURL(const KURL&); |
| 109 | |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 110 | void add(CachedResource*); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame^] | 111 | void replace(CachedResource* newResource, CachedResource* oldResource); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 112 | void remove(CachedResource* resource) { evict(resource); } |
| 113 | |
| 114 | static KURL removeFragmentIdentifierIfNeeded(const KURL& originalURL); |
| 115 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 116 | // 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) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 123 | void evictResources(); |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 124 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 125 | void prune(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 126 | |
| 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) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 141 | static void removeURLFromCache(ScriptExecutionContext*, const KURL&); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 142 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 143 | Statistics getStatistics(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 144 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 145 | 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 | |
| 153 | private: |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 154 | LRUList* lruListFor(CachedResource*); |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 155 | |
| 156 | #ifdef MEMORY_CACHE_STATS |
| 157 | void dumpStats(Timer<MemoryCache>*); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 158 | void dumpLRULists(bool includeLive) const; |
| 159 | #endif |
| 160 | |
| 161 | unsigned liveCapacity() const; |
| 162 | unsigned deadCapacity() const; |
| 163 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame^] | 164 | // 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) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 166 | void pruneDeadResources(); // Automatically decide how much to prune. |
| 167 | void pruneLiveResources(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 168 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 169 | void evict(CachedResource*); |
| 170 | |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 171 | static void removeURLFromCacheInternal(ScriptExecutionContext*, const KURL&); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 172 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 173 | bool m_inPruneResources; |
| 174 | |
| 175 | unsigned m_capacity; |
| 176 | unsigned m_minDeadCapacity; |
| 177 | unsigned m_maxDeadCapacity; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 178 | |
| 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) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 193 | |
| 194 | #ifdef MEMORY_CACHE_STATS |
| 195 | Timer<MemoryCache> m_statsTimer; |
| 196 | #endif |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 197 | }; |
| 198 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 199 | // Returns the global cache. |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 200 | MemoryCache* memoryCache(); |
| 201 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 202 | // Sets the global cache, used to swap in a test instance. |
| 203 | void setMemoryCacheForTesting(MemoryCache*); |
| 204 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | #endif |