#ifndef Cache_h
#define Cache_h
#include "NativeImagePtr.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>
#include <wtf/text/WTFString.h>
namespace WebCore {
class CachedResource;
class URL;
class ResourceRequest;
class ResourceResponse;
class ScriptExecutionContext;
class SecurityOrigin;
class MemoryCache {
WTF_MAKE_NONCOPYABLE(MemoryCache); WTF_MAKE_FAST_ALLOCATED;
friend NeverDestroyed<MemoryCache>;
friend class Internals;
public:
struct TypeStatistic {
int count;
int size;
int liveSize;
int decodedSize;
TypeStatistic()
: count(0)
, size(0)
, liveSize(0)
, decodedSize(0)
{
}
void addResource(CachedResource&);
};
struct Statistics {
TypeStatistic images;
TypeStatistic cssStyleSheets;
TypeStatistic scripts;
TypeStatistic xslStyleSheets;
TypeStatistic fonts;
};
WEBCORE_EXPORT static MemoryCache& singleton();
WEBCORE_EXPORT CachedResource* resourceForRequest(const ResourceRequest&, SessionID);
bool add(CachedResource&);
void remove(CachedResource&);
static URL removeFragmentIdentifierIfNeeded(const URL& originalURL);
void revalidationSucceeded(CachedResource& revalidatingResource, const ResourceResponse&);
void revalidationFailed(CachedResource& revalidatingResource);
WEBCORE_EXPORT void setCapacities(unsigned minDeadBytes, unsigned maxDeadBytes, unsigned totalBytes);
WEBCORE_EXPORT void setDisabled(bool);
bool disabled() const { return m_disabled; }
WEBCORE_EXPORT void evictResources();
WEBCORE_EXPORT void evictResources(SessionID);
void prune();
void pruneSoon();
unsigned size() const { return m_liveSize + m_deadSize; }
void setDeadDecodedDataDeletionInterval(std::chrono::milliseconds interval) { m_deadDecodedDataDeletionInterval = interval; }
std::chrono::milliseconds 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 void removeRequestFromSessionCaches(ScriptExecutionContext&, const ResourceRequest&);
WEBCORE_EXPORT Statistics getStatistics();
void resourceAccessed(CachedResource&);
bool inLiveDecodedResourcesList(CachedResource& resource) const { return m_liveDecodedResources.contains(&resource); }
typedef HashSet<RefPtr<SecurityOrigin>> SecurityOriginSet;
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;
#if USE(CG)
WEBCORE_EXPORT bool addImageToCache(NativeImagePtr, const URL&, const String& domainForCachePartition);
WEBCORE_EXPORT void removeImageFromCache(const URL&, const String& domainForCachePartition);
#endif
WEBCORE_EXPORT void pruneDeadResources(); WEBCORE_EXPORT void pruneLiveResources(bool shouldDestroyDecodedDataForAllLiveResources = false);
WEBCORE_EXPORT void pruneDeadResourcesToSize(unsigned targetSize);
WEBCORE_EXPORT void pruneLiveResourcesToSize(unsigned targetSize, bool shouldDestroyDecodedDataForAllLiveResources = false);
private:
#if ENABLE(CACHE_PARTITIONING)
typedef HashMap<std::pair<URL, String >, CachedResource*> CachedResourceMap;
#else
typedef HashMap<URL, CachedResource*> CachedResourceMap;
#endif
typedef ListHashSet<CachedResource*> LRUList;
MemoryCache();
~MemoryCache();
LRUList& lruListFor(CachedResource&);
#ifndef NDEBUG
void dumpStats();
void dumpLRULists(bool includeLive) const;
#endif
unsigned liveCapacity() const;
unsigned deadCapacity() const;
bool needsPruning() const;
void pruneTimerFired();
CachedResource* resourceForRequestImpl(const ResourceRequest&, CachedResourceMap&);
CachedResourceMap& ensureSessionResourceMap(SessionID);
CachedResourceMap* sessionResourceMap(SessionID) const;
bool m_disabled; bool m_inPruneResources;
unsigned m_capacity;
unsigned m_minDeadCapacity;
unsigned m_maxDeadCapacity;
std::chrono::milliseconds m_deadDecodedDataDeletionInterval;
unsigned m_liveSize; unsigned m_deadSize;
Vector<std::unique_ptr<LRUList>, 32> m_allResources;
LRUList m_liveDecodedResources;
typedef HashMap<SessionID, std::unique_ptr<CachedResourceMap>> SessionCachedResourceMap;
SessionCachedResourceMap m_sessionResources;
Timer m_pruneTimer;
};
}
#endif