#pragma once
#include "NativeImage.h"
#include "SecurityOriginHash.h"
#include "Timer.h"
#include <pal/SessionID.h>
#include <wtf/Forward.h>
#include <wtf/Function.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&, PAL::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 WTF::Function<void(CachedResource&)>&);
void forEachSessionResource(PAL::SessionID, const WTF::Function<void(CachedResource&)>&);
WEBCORE_EXPORT void destroyDecodedDataForAllImages();
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(PAL::SessionID);
void prune();
void pruneSoon();
unsigned size() const { return m_liveSize + m_deadSize; }
void setDeadDecodedDataDeletionInterval(Seconds interval) { m_deadDecodedDataDeletionInterval = interval; }
Seconds deadDecodedDataDeletionInterval() const { return m_deadDecodedDataDeletionInterval; }
void insertInLRUList(CachedResource&);
void removeFromLRUList(CachedResource&);
void adjustSize(bool live, long long 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(PAL::SessionID, const HashSet<RefPtr<SecurityOrigin>>&);
WEBCORE_EXPORT void getOriginsWithCache(SecurityOriginSet& origins);
WEBCORE_EXPORT HashSet<RefPtr<SecurityOrigin>> originsWithCache(PAL::SessionID) const;
WEBCORE_EXPORT bool addImageToCache(NativeImagePtr&&, const URL&, const String& domainForCachePartition);
WEBCORE_EXPORT void removeImageFromCache(const URL&, const String& domainForCachePartition);
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:
typedef HashMap<std::pair<URL, String >, CachedResource*> CachedResourceMap;
typedef ListHashSet<CachedResource*> LRUList;
MemoryCache();
~MemoryCache();
LRUList& lruListFor(CachedResource&);
void dumpStats();
void dumpLRULists(bool includeLive) const;
unsigned liveCapacity() const;
unsigned deadCapacity() const;
bool needsPruning() const;
CachedResource* resourceForRequestImpl(const ResourceRequest&, CachedResourceMap&);
CachedResourceMap& ensureSessionResourceMap(PAL::SessionID);
CachedResourceMap* sessionResourceMap(PAL::SessionID) const;
bool m_disabled { false };
bool m_inPruneResources { false };
unsigned m_capacity;
unsigned m_minDeadCapacity { 0 };
unsigned m_maxDeadCapacity;
Seconds m_deadDecodedDataDeletionInterval;
unsigned m_liveSize { 0 }; unsigned m_deadSize { 0 };
Vector<std::unique_ptr<LRUList>, 32> m_allResources;
LRUList m_liveDecodedResources;
typedef HashMap<PAL::SessionID, std::unique_ptr<CachedResourceMap>> SessionCachedResourceMap;
SessionCachedResourceMap m_sessionResources;
Timer m_pruneTimer;
};
}