#ifndef Cache_h
#define Cache_h
#include "SecurityOriginHash.h"
#include <wtf/HashMap.h>
#include <wtf/HashSet.h>
#include <wtf/Noncopyable.h>
#include <wtf/Vector.h>
#include <wtf/text/StringHash.h>
#include <wtf/text/WTFString.h>
namespace WebCore {
class CachedCSSStyleSheet;
class CachedResource;
class CachedResourceLoader;
class KURL;
class ResourceRequest;
class ResourceResponse;
class ScriptExecutionContext;
class SecurityOrigin;
struct CrossThreadResourceRequestData;
struct SecurityOriginHash;
class MemoryCache {
WTF_MAKE_NONCOPYABLE(MemoryCache); WTF_MAKE_FAST_ALLOCATED;
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;
TypeStatistic() : count(0), size(0), liveSize(0), decodedSize(0), purgeableSize(0), purgedSize(0) { }
void addResource(CachedResource*);
};
struct Statistics {
TypeStatistic images;
TypeStatistic cssStyleSheets;
TypeStatistic scripts;
TypeStatistic xslStyleSheets;
TypeStatistic fonts;
};
CachedResource* resourceForURL(const KURL&);
CachedResource* resourceForRequest(const ResourceRequest&);
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 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();
static void removeUrlFromCache(ScriptExecutionContext*, const String& urlString);
static void removeRequestFromCache(ScriptExecutionContext*, const ResourceRequest&);
Statistics getStatistics();
void resourceAccessed(CachedResource*);
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; }
private:
MemoryCache();
~MemoryCache();
LRUList* lruListFor(CachedResource*);
#ifndef NDEBUG
void dumpStats();
void dumpLRULists(bool includeLive) const;
#endif
unsigned liveCapacity() const;
unsigned deadCapacity() const;
void pruneDeadResources(); void pruneLiveResources();
void pruneDeadResourcesToPercentage(float prunePercentage); void pruneLiveResourcesToPercentage(float prunePercentage);
void pruneDeadResourcesToSize(unsigned targetSize);
void pruneLiveResourcesToSize(unsigned targetSize);
bool makeResourcePurgeable(CachedResource*);
void evict(CachedResource*);
static void removeRequestFromCacheImpl(ScriptExecutionContext*, const ResourceRequest&);
static void crossThreadRemoveRequestFromCache(ScriptExecutionContext*, PassOwnPtr<CrossThreadResourceRequestData>);
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;
CachedResourceMap m_resources;
};
inline bool MemoryCache::shouldMakeResourcePurgeableOnEviction()
{
#if PLATFORM(IOS)
return true;
#else
return false;
#endif
}
MemoryCache* memoryCache();
}
#endif