OLD | NEW |
| (Empty) |
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 | |
22 sheets and html | |
23 pages from the web. It has a memory cache for these objects. | |
24 */ | |
25 | |
26 #ifndef MemoryCache_h | |
27 #define MemoryCache_h | |
28 | |
29 #include "core/CoreExport.h" | |
30 #include "core/fetch/Resource.h" | |
31 #include "platform/MemoryCoordinator.h" | |
32 #include "platform/instrumentation/tracing/MemoryCacheDumpProvider.h" | |
33 #include "public/platform/WebThread.h" | |
34 #include "wtf/Allocator.h" | |
35 #include "wtf/HashMap.h" | |
36 #include "wtf/Noncopyable.h" | |
37 #include "wtf/Vector.h" | |
38 #include "wtf/text/StringHash.h" | |
39 #include "wtf/text/WTFString.h" | |
40 | |
41 namespace blink { | |
42 | |
43 class KURL; | |
44 | |
45 // Member<MemoryCacheEntry> + MemoryCacheEntry::clearResourceWeak() monitors | |
46 // eviction from MemoryCache due to Resource garbage collection. | |
47 // WeakMember<Resource> + Resource's prefinalizer cannot determine whether the | |
48 // Resource was on MemoryCache or not, because WeakMember is already cleared | |
49 // when the prefinalizer is executed. | |
50 class MemoryCacheEntry final : public GarbageCollected<MemoryCacheEntry> { | |
51 public: | |
52 static MemoryCacheEntry* create(Resource* resource) { | |
53 return new MemoryCacheEntry(resource); | |
54 } | |
55 DECLARE_TRACE(); | |
56 Resource* resource() const { return m_resource; } | |
57 | |
58 double m_lastDecodedAccessTime; // Used as a thrash guard | |
59 | |
60 private: | |
61 explicit MemoryCacheEntry(Resource* resource) | |
62 : m_lastDecodedAccessTime(0.0), m_resource(resource) {} | |
63 | |
64 void clearResourceWeak(Visitor*); | |
65 | |
66 WeakMember<Resource> m_resource; | |
67 }; | |
68 | |
69 WILL_NOT_BE_EAGERLY_TRACED_CLASS(MemoryCacheEntry); | |
70 | |
71 // This cache holds subresources used by Web pages: images, scripts, | |
72 // stylesheets, etc. | |
73 class CORE_EXPORT MemoryCache final | |
74 : public GarbageCollectedFinalized<MemoryCache>, | |
75 public WebThread::TaskObserver, | |
76 public MemoryCacheDumpClient, | |
77 public MemoryCoordinatorClient { | |
78 USING_GARBAGE_COLLECTED_MIXIN(MemoryCache); | |
79 WTF_MAKE_NONCOPYABLE(MemoryCache); | |
80 | |
81 public: | |
82 static MemoryCache* create(); | |
83 ~MemoryCache(); | |
84 DECLARE_TRACE(); | |
85 | |
86 struct TypeStatistic { | |
87 STACK_ALLOCATED(); | |
88 size_t count; | |
89 size_t size; | |
90 size_t decodedSize; | |
91 size_t encodedSize; | |
92 size_t overheadSize; | |
93 size_t encodedSizeDuplicatedInDataURLs; | |
94 | |
95 TypeStatistic() | |
96 : count(0), | |
97 size(0), | |
98 decodedSize(0), | |
99 encodedSize(0), | |
100 overheadSize(0), | |
101 encodedSizeDuplicatedInDataURLs(0) {} | |
102 | |
103 void addResource(Resource*); | |
104 }; | |
105 | |
106 struct Statistics { | |
107 STACK_ALLOCATED(); | |
108 TypeStatistic images; | |
109 TypeStatistic cssStyleSheets; | |
110 TypeStatistic scripts; | |
111 TypeStatistic xslStyleSheets; | |
112 TypeStatistic fonts; | |
113 TypeStatistic other; | |
114 }; | |
115 | |
116 Resource* resourceForURL(const KURL&) const; | |
117 Resource* resourceForURL(const KURL&, const String& cacheIdentifier) const; | |
118 HeapVector<Member<Resource>> resourcesForURL(const KURL&) const; | |
119 | |
120 void add(Resource*); | |
121 void remove(Resource*); | |
122 bool contains(const Resource*) const; | |
123 | |
124 static KURL removeFragmentIdentifierIfNeeded(const KURL& originalURL); | |
125 | |
126 static String defaultCacheIdentifier(); | |
127 | |
128 // Sets the cache's memory capacities, in bytes. These will hold only | |
129 // approximately, since the decoded cost of resources like scripts and | |
130 // stylesheets is not known. | |
131 // - totalBytes: The maximum number of bytes that the cache should consume | |
132 // overall. | |
133 void setCapacity(size_t totalBytes); | |
134 void setDelayBeforeLiveDecodedPrune(double seconds) { | |
135 m_delayBeforeLiveDecodedPrune = seconds; | |
136 } | |
137 void setMaxPruneDeferralDelay(double seconds) { | |
138 m_maxPruneDeferralDelay = seconds; | |
139 } | |
140 | |
141 enum EvictResourcePolicy { EvictAllResources, DoNotEvictUnusedPreloads }; | |
142 void evictResources(EvictResourcePolicy = EvictAllResources); | |
143 | |
144 void prune(); | |
145 | |
146 // Called to update MemoryCache::size(). | |
147 void update(Resource*, size_t oldSize, size_t newSize); | |
148 | |
149 void removeURLFromCache(const KURL&); | |
150 | |
151 Statistics getStatistics() const; | |
152 | |
153 size_t capacity() const { return m_capacity; } | |
154 size_t size() const { return m_size; } | |
155 | |
156 // TaskObserver implementation | |
157 void willProcessTask() override; | |
158 void didProcessTask() override; | |
159 | |
160 void pruneAll(); | |
161 | |
162 void updateFramePaintTimestamp(); | |
163 | |
164 // Take memory usage snapshot for tracing. | |
165 bool onMemoryDump(WebMemoryDumpLevelOfDetail, WebProcessMemoryDump*) override; | |
166 | |
167 void onMemoryPressure(WebMemoryPressureLevel) override; | |
168 | |
169 private: | |
170 enum PruneStrategy { | |
171 // Automatically decide how much to prune. | |
172 AutomaticPrune, | |
173 // Maximally prune resources. | |
174 MaximalPrune | |
175 }; | |
176 | |
177 // A URL-based map of all resources that are in the cache (including the | |
178 // freshest version of objects that are currently being referenced by a Web | |
179 // page). removeFragmentIdentifierIfNeeded() should be called for the url | |
180 // before using it as a key for the map. | |
181 using ResourceMap = HeapHashMap<String, Member<MemoryCacheEntry>>; | |
182 using ResourceMapIndex = HeapHashMap<String, Member<ResourceMap>>; | |
183 ResourceMap* ensureResourceMap(const String& cacheIdentifier); | |
184 ResourceMapIndex m_resourceMaps; | |
185 | |
186 MemoryCache(); | |
187 | |
188 void addInternal(ResourceMap*, MemoryCacheEntry*); | |
189 void removeInternal(ResourceMap*, const ResourceMap::iterator&); | |
190 | |
191 void pruneResources(PruneStrategy); | |
192 void pruneNow(double currentTime, PruneStrategy); | |
193 | |
194 bool m_inPruneResources; | |
195 bool m_prunePending; | |
196 double m_maxPruneDeferralDelay; | |
197 double m_pruneTimeStamp; | |
198 double m_pruneFrameTimeStamp; | |
199 double m_lastFramePaintTimeStamp; // used for detecting decoded resource | |
200 // thrash in the cache | |
201 | |
202 size_t m_capacity; | |
203 double m_delayBeforeLiveDecodedPrune; | |
204 | |
205 // The number of bytes currently consumed by resources in the cache. | |
206 size_t m_size; | |
207 | |
208 friend class MemoryCacheTest; | |
209 }; | |
210 | |
211 // Returns the global cache. | |
212 CORE_EXPORT MemoryCache* memoryCache(); | |
213 | |
214 // Sets the global cache, used to swap in a test instance. Returns the old | |
215 // MemoryCache object. | |
216 CORE_EXPORT MemoryCache* replaceMemoryCacheForTesting(MemoryCache*); | |
217 | |
218 } // namespace blink | |
219 | |
220 #endif | |
OLD | NEW |