#ifndef Cache_h
#define Cache_h
#include "CachedResource.h"
#include "PlatformString.h"
#include <wtf/HashMap.h>
#include <wtf/HashSet.h>
#include <wtf/Noncopyable.h>
#include <wtf/Vector.h>
#include <wtf/text/StringHash.h>
#include "ImageSource.h"
namespace WebCore {
class CachedCSSStyleSheet;
class CachedResource;
class CachedResourceLoader;
class KURL;
class ScriptExecutionContext;
class SecurityOrigin;
struct SecurityOriginHash;
class MemoryCache {
WTF_MAKE_NONCOPYABLE(MemoryCache); WTF_MAKE_FAST_ALLOCATED;
public:
friend MemoryCache* memoryCache();
typedef HashMap<String, CachedResource*> CachedResourceMap;
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*);
};
struct Statistics {
TypeStatistic images;
TypeStatistic cssStyleSheets;
TypeStatistic scripts;
TypeStatistic xslStyleSheets;
TypeStatistic fonts;
};
CachedResource* resourceForURL(const KURL&);
bool add(CachedResource* resource);
void remove(CachedResource* resource) { evict(resource); }
static KURL removeFragmentIdentifierIfNeeded(const KURL& originalURL);
void revalidationSucceeded(CachedResource* revalidatingResource, const ResourceResponse&);
void revalidationFailed(CachedResource* revalidatingResource);
void setCapacities(unsigned minDeadBytes, unsigned maxDeadBytes, unsigned totalBytes);
void setDisabled(bool);
bool disabled() const { return m_disabled; }
void evictResources();
void setPruneEnabled(bool enabled) { m_pruneEnabled = enabled; }
void prune();
void pruneToPercentage(float targetPercentLive);
void setDeadDecodedDataDeletionInterval(double interval) { m_deadDecodedDataDeletionInterval = interval; }
double deadDecodedDataDeletionInterval() const { return m_deadDecodedDataDeletionInterval; }
void addCachedResourceLoader(CachedResourceLoader*);
void removeCachedResourceLoader(CachedResourceLoader*);
void insertInLRUList(CachedResource*);
void removeFromLRUList(CachedResource*);
void adjustSize(bool live, int delta);
void insertInLiveDecodedResourcesList(CachedResource*);
void removeFromLiveDecodedResourcesList(CachedResource*);
void addToLiveResourcesSize(CachedResource*);
void removeFromLiveResourcesSize(CachedResource*);
static bool shouldMakeResourcePurgeableOnEviction();
#if ENABLE(DISK_IMAGE_CACHE)
void flushCachedImagesToDisk(); #endif
static void removeUrlFromCache(ScriptExecutionContext*, const String& urlString);
Statistics getStatistics();
void resourceAccessed(CachedResource*);
typedef HashSet<RefPtr<SecurityOrigin>, SecurityOriginHash> 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; }
private:
MemoryCache();
~MemoryCache();
LRUList* lruListFor(CachedResource*);
#ifndef NDEBUG
void dumpStats();
void dumpLRULists(bool includeLive) const;
#endif
unsigned liveCapacity() const;
unsigned deadCapacity() const;
public:
bool addImageToCache(NativeImagePtr image, const KURL& url);
void removeImageFromCache(const KURL& url);
void pruneDeadResources(); void pruneLiveResources(bool critical = false); private:
void pruneDeadResourcesToPercentage(float prunePercentage); void pruneLiveResourcesToPercentage(float prunePercentage);
void pruneDeadResourcesToSize(unsigned targetSize);
void pruneLiveResourcesToSize(unsigned targetSize, bool critical = false);
bool makeResourcePurgeable(CachedResource*);
void evict(CachedResource*);
static void removeUrlFromCacheImpl(ScriptExecutionContext*, const String& urlString);
bool m_disabled; bool m_pruneEnabled;
bool m_inPruneResources;
unsigned m_capacity;
unsigned m_minDeadCapacity;
unsigned m_maxDeadCapacity;
double m_deadDecodedDataDeletionInterval;
unsigned m_liveSize; unsigned m_deadSize;
Vector<LRUList, 32> m_allResources;
LRUList m_liveDecodedResources;
HashMap<String, CachedResource*> m_resources;
};
inline bool MemoryCache::shouldMakeResourcePurgeableOnEviction()
{
return true;
}
MemoryCache* memoryCache();
}
#endif