summaryrefslogtreecommitdiff
path: root/Source/WebCore/loader/cache/MemoryCache.h
diff options
context:
space:
mode:
authorLorry Tar Creator <lorry-tar-importer@lorry>2017-06-27 06:07:23 +0000
committerLorry Tar Creator <lorry-tar-importer@lorry>2017-06-27 06:07:23 +0000
commit1bf1084f2b10c3b47fd1a588d85d21ed0eb41d0c (patch)
tree46dcd36c86e7fbc6e5df36deb463b33e9967a6f7 /Source/WebCore/loader/cache/MemoryCache.h
parent32761a6cee1d0dee366b885b7b9c777e67885688 (diff)
downloadWebKitGtk-tarball-master.tar.gz
Diffstat (limited to 'Source/WebCore/loader/cache/MemoryCache.h')
-rw-r--r--Source/WebCore/loader/cache/MemoryCache.h209
1 files changed, 81 insertions, 128 deletions
diff --git a/Source/WebCore/loader/cache/MemoryCache.h b/Source/WebCore/loader/cache/MemoryCache.h
index c023a9725..c5d97e1d3 100644
--- a/Source/WebCore/loader/cache/MemoryCache.h
+++ b/Source/WebCore/loader/cache/MemoryCache.h
@@ -22,13 +22,16 @@
pages from the web. It has a memory cache for these objects.
*/
-#ifndef Cache_h
-#define Cache_h
+#pragma once
-#include "NativeImagePtr.h"
+#include "NativeImage.h"
#include "SecurityOriginHash.h"
+#include "SessionID.h"
+#include "Timer.h"
+#include <wtf/Forward.h>
#include <wtf/HashMap.h>
#include <wtf/HashSet.h>
+#include <wtf/ListHashSet.h>
#include <wtf/Noncopyable.h>
#include <wtf/Vector.h>
#include <wtf/text/StringHash.h>
@@ -36,16 +39,12 @@
namespace WebCore {
-class CachedCSSStyleSheet;
class CachedResource;
-class CachedResourceLoader;
class URL;
class ResourceRequest;
class ResourceResponse;
class ScriptExecutionContext;
class SecurityOrigin;
-struct CrossThreadResourceRequestData;
-struct SecurityOriginHash;
// This cache holds subresources used by Web pages: images, scripts, stylesheets, etc.
@@ -60,54 +59,26 @@ struct SecurityOriginHash;
// -------|-----+++++++++++++++|
// -------|-----+++++++++++++++|+++++
-// The behavior of the cache changes in the following way if shouldMakeResourcePurgeableOnEviction
-// returns true.
-//
-// 1. Dead resources in the cache are kept in non-purgeable memory.
-// 2. When we prune dead resources, instead of freeing them, we mark their memory as purgeable and
-// keep the resources until the kernel reclaims the purgeable memory.
-//
-// By leaving the in-cache dead resources in dirty resident memory, we decrease the likelihood of
-// the kernel claiming that memory and forcing us to refetch the resource (for example when a user
-// presses back).
-//
-// And by having an unbounded number of resource objects using purgeable memory, we can use as much
-// memory as is available on the machine. The trade-off here is that the CachedResource object (and
-// its member variables) are allocated in non-purgeable TC-malloc'd memory so we would see slightly
-// more memory use due to this.
-
class MemoryCache {
WTF_MAKE_NONCOPYABLE(MemoryCache); WTF_MAKE_FAST_ALLOCATED;
+ friend NeverDestroyed<MemoryCache>;
+ friend class Internals;
public:
- friend MemoryCache* memoryCache();
-
-#if ENABLE(CACHE_PARTITIONING)
- typedef HashMap<String, CachedResource*> CachedResourceItem;
- typedef HashMap<String, OwnPtr<CachedResourceItem>> CachedResourceMap;
-#else
- typedef HashMap<String, CachedResource*> CachedResourceMap;
-#endif
-
- struct LRUList {
- CachedResource* m_head;
- CachedResource* m_tail;
- LRUList() : m_head(0), m_tail(0) { }
- };
-
struct TypeStatistic {
int count;
int size;
int liveSize;
int decodedSize;
- int purgeableSize;
- int purgedSize;
-#if ENABLE(DISK_IMAGE_CACHE)
- int mappedSize;
- TypeStatistic() : count(0), size(0), liveSize(0), decodedSize(0), purgeableSize(0), purgedSize(0), mappedSize(0) { }
-#else
- TypeStatistic() : count(0), size(0), liveSize(0), decodedSize(0), purgeableSize(0), purgedSize(0) { }
-#endif
- void addResource(CachedResource*);
+
+ TypeStatistic()
+ : count(0)
+ , size(0)
+ , liveSize(0)
+ , decodedSize(0)
+ {
+ }
+
+ void addResource(CachedResource&);
};
struct Statistics {
@@ -118,98 +89,92 @@ public:
TypeStatistic fonts;
};
- CachedResource* resourceForURL(const URL&);
- CachedResource* resourceForRequest(const ResourceRequest&);
-
- bool add(CachedResource* resource);
- void remove(CachedResource* resource) { evict(resource); }
+ WEBCORE_EXPORT static MemoryCache& singleton();
- static URL removeFragmentIdentifierIfNeeded(const URL& originalURL);
-
- void revalidationSucceeded(CachedResource* revalidatingResource, const ResourceResponse&);
- void revalidationFailed(CachedResource* revalidatingResource);
-
- // Sets the cache's memory capacities, in bytes. These will hold only approximately,
+ WEBCORE_EXPORT CachedResource* resourceForRequest(const ResourceRequest&, SessionID);
+
+ bool add(CachedResource&);
+ void remove(CachedResource&);
+
+ static bool shouldRemoveFragmentIdentifier(const URL&);
+ static URL removeFragmentIdentifierIfNeeded(const URL&);
+
+ void revalidationSucceeded(CachedResource& revalidatingResource, const ResourceResponse&);
+ void revalidationFailed(CachedResource& revalidatingResource);
+
+ void forEachResource(const std::function<void(CachedResource&)>&);
+ void forEachSessionResource(SessionID, const std::function<void(CachedResource&)>&);
+ void destroyDecodedDataForAllImages();
+
+ // Sets the cache's memory capacities, in bytes. These will hold only approximately,
// since the decoded cost of resources like scripts and stylesheets is not known.
// - minDeadBytes: The maximum number of bytes that dead resources should consume when the cache is under pressure.
// - maxDeadBytes: The maximum number of bytes that dead resources should consume when the cache is not under pressure.
// - totalBytes: The maximum number of bytes that the cache should consume overall.
- void setCapacities(unsigned minDeadBytes, unsigned maxDeadBytes, unsigned totalBytes);
+ WEBCORE_EXPORT void setCapacities(unsigned minDeadBytes, unsigned maxDeadBytes, unsigned totalBytes);
// Turn the cache on and off. Disabling the cache will remove all resources from the cache. They may
// still live on if they are referenced by some Web page though.
- void setDisabled(bool);
+ WEBCORE_EXPORT void setDisabled(bool);
bool disabled() const { return m_disabled; }
- void evictResources();
-
- void setPruneEnabled(bool enabled) { m_pruneEnabled = enabled; }
+ WEBCORE_EXPORT void evictResources();
+ WEBCORE_EXPORT void evictResources(SessionID);
+
void prune();
- void pruneToPercentage(float targetPercentLive);
+ void pruneSoon();
+ unsigned size() const { return m_liveSize + m_deadSize; }
- void setDeadDecodedDataDeletionInterval(double interval) { m_deadDecodedDataDeletionInterval = interval; }
- double deadDecodedDataDeletionInterval() const { return m_deadDecodedDataDeletionInterval; }
+ void setDeadDecodedDataDeletionInterval(std::chrono::milliseconds interval) { m_deadDecodedDataDeletionInterval = interval; }
+ std::chrono::milliseconds deadDecodedDataDeletionInterval() const { return m_deadDecodedDataDeletionInterval; }
// Calls to put the cached resource into and out of LRU lists.
- void insertInLRUList(CachedResource*);
- void removeFromLRUList(CachedResource*);
+ void insertInLRUList(CachedResource&);
+ void removeFromLRUList(CachedResource&);
// Called to adjust the cache totals when a resource changes size.
- void adjustSize(bool live, int delta);
+ void adjustSize(bool live, long long delta);
// Track decoded resources that are in the cache and referenced by a Web page.
- void insertInLiveDecodedResourcesList(CachedResource*);
- void removeFromLiveDecodedResourcesList(CachedResource*);
-
- void addToLiveResourcesSize(CachedResource*);
- void removeFromLiveResourcesSize(CachedResource*);
+ void insertInLiveDecodedResourcesList(CachedResource&);
+ void removeFromLiveDecodedResourcesList(CachedResource&);
- static bool shouldMakeResourcePurgeableOnEviction();
+ void addToLiveResourcesSize(CachedResource&);
+ void removeFromLiveResourcesSize(CachedResource&);
-#if ENABLE(DISK_IMAGE_CACHE)
- void flushCachedImagesToDisk(); // Flush encoded data from resources still referenced by web pages.
-#endif
-
- static void removeUrlFromCache(ScriptExecutionContext*, const String& urlString);
- static void removeRequestFromCache(ScriptExecutionContext*, const ResourceRequest&);
+ static void removeRequestFromSessionCaches(ScriptExecutionContext&, const ResourceRequest&);
// Function to collect cache statistics for the caches window in the Safari Debug menu.
- Statistics getStatistics();
+ WEBCORE_EXPORT Statistics getStatistics();
- void resourceAccessed(CachedResource*);
+ void resourceAccessed(CachedResource&);
+ bool inLiveDecodedResourcesList(CachedResource& resource) const { return m_liveDecodedResources.contains(&resource); }
typedef HashSet<RefPtr<SecurityOrigin>> SecurityOriginSet;
- void removeResourcesWithOrigin(SecurityOrigin*);
- void getOriginsWithCache(SecurityOriginSet& origins);
-
- unsigned minDeadCapacity() const { return m_minDeadCapacity; }
- unsigned maxDeadCapacity() const { return m_maxDeadCapacity; }
- unsigned capacity() const { return m_capacity; }
- unsigned liveSize() const { return m_liveSize; }
- unsigned deadSize() const { return m_deadSize; }
-
-#if USE(CG)
- // FIXME: Remove the USE(CG) once we either make NativeImagePtr a smart pointer on all platforms or
- // remove the usage of CFRetain() in MemoryCache::addImageToCache() so as to make the code platform-independent.
- bool addImageToCache(NativeImagePtr, const URL&, const String& cachePartition);
- void removeImageFromCache(const URL&, const String& cachePartition);
-#endif
+ WEBCORE_EXPORT void removeResourcesWithOrigin(SecurityOrigin&);
+ WEBCORE_EXPORT void removeResourcesWithOrigins(SessionID, const HashSet<RefPtr<SecurityOrigin>>&);
+ WEBCORE_EXPORT void getOriginsWithCache(SecurityOriginSet& origins);
+ WEBCORE_EXPORT HashSet<RefPtr<SecurityOrigin>> originsWithCache(SessionID) const;
+
+ WEBCORE_EXPORT bool addImageToCache(NativeImagePtr&&, const URL&, const String& domainForCachePartition);
+ WEBCORE_EXPORT void removeImageFromCache(const URL&, const String& domainForCachePartition);
// pruneDead*() - Flush decoded and encoded data from resources not referenced by Web pages.
// pruneLive*() - Flush decoded data from resources still referenced by Web pages.
- void pruneDeadResources(); // Automatically decide how much to prune.
- void pruneLiveResources(bool shouldDestroyDecodedDataForAllLiveResources = false);
+ WEBCORE_EXPORT void pruneDeadResources(); // Automatically decide how much to prune.
+ WEBCORE_EXPORT void pruneLiveResources(bool shouldDestroyDecodedDataForAllLiveResources = false);
+
+ WEBCORE_EXPORT void pruneDeadResourcesToSize(unsigned targetSize);
+ WEBCORE_EXPORT void pruneLiveResourcesToSize(unsigned targetSize, bool shouldDestroyDecodedDataForAllLiveResources = false);
private:
- void pruneDeadResourcesToPercentage(float prunePercentage); // Prune to % current size
- void pruneLiveResourcesToPercentage(float prunePercentage);
- void pruneDeadResourcesToSize(unsigned targetSize);
- void pruneLiveResourcesToSize(unsigned targetSize, bool shouldDestroyDecodedDataForAllLiveResources = false);
+ typedef HashMap<std::pair<URL, String /* partitionName */>, CachedResource*> CachedResourceMap;
+ typedef ListHashSet<CachedResource*> LRUList;
MemoryCache();
~MemoryCache(); // Not implemented to make sure nobody accidentally calls delete -- WebCore does not delete singletons.
- LRUList* lruListFor(CachedResource*);
+ LRUList& lruListFor(CachedResource&);
#ifndef NDEBUG
void dumpStats();
void dumpLRULists(bool includeLive) const;
@@ -217,21 +182,20 @@ private:
unsigned liveCapacity() const;
unsigned deadCapacity() const;
+ bool needsPruning() const;
- bool makeResourcePurgeable(CachedResource*);
- void evict(CachedResource*);
+ CachedResource* resourceForRequestImpl(const ResourceRequest&, CachedResourceMap&);
- static void removeRequestFromCacheImpl(ScriptExecutionContext*, const ResourceRequest&);
- static void crossThreadRemoveRequestFromCache(ScriptExecutionContext*, PassOwnPtr<CrossThreadResourceRequestData>);
+ CachedResourceMap& ensureSessionResourceMap(SessionID);
+ CachedResourceMap* sessionResourceMap(SessionID) const;
bool m_disabled; // Whether or not the cache is enabled.
- bool m_pruneEnabled;
bool m_inPruneResources;
unsigned m_capacity;
unsigned m_minDeadCapacity;
unsigned m_maxDeadCapacity;
- double m_deadDecodedDataDeletionInterval;
+ std::chrono::milliseconds m_deadDecodedDataDeletionInterval;
unsigned m_liveSize; // The number of bytes currently consumed by "live" resources in the cache.
unsigned m_deadSize; // The number of bytes currently consumed by "dead" resources in the cache.
@@ -239,28 +203,17 @@ private:
// Size-adjusted and popularity-aware LRU list collection for cache objects. This collection can hold
// more resources than the cached resource map, since it can also hold "stale" multiple versions of objects that are
// waiting to die when the clients referencing them go away.
- Vector<LRUList, 32> m_allResources;
+ Vector<std::unique_ptr<LRUList>, 32> m_allResources;
// List just for live resources with decoded data. Access to this list is based off of painting the resource.
LRUList m_liveDecodedResources;
// A URL-based map of all resources that are in the cache (including the freshest version of objects that are currently being
// referenced by a Web page).
- CachedResourceMap m_resources;
-};
-
-inline bool MemoryCache::shouldMakeResourcePurgeableOnEviction()
-{
-#if PLATFORM(IOS)
- return true;
-#else
- return false;
-#endif
-}
+ typedef HashMap<SessionID, std::unique_ptr<CachedResourceMap>> SessionCachedResourceMap;
+ SessionCachedResourceMap m_sessionResources;
-// Function to obtain the global cache.
-MemoryCache* memoryCache();
-
-}
+ Timer m_pruneTimer;
+};
-#endif
+} // namespace WebCore