#include "config.h"
#include "MemoryCache.h"
#include "BitmapImage.h"
#include "CachedImage.h"
#include "CachedImageClient.h"
#include "CachedResource.h"
#include "CachedResourceHandle.h"
#include "Document.h"
#include "FrameLoader.h"
#include "FrameLoaderTypes.h"
#include "FrameView.h"
#include "Image.h"
#include "Logging.h"
#include "PublicSuffix.h"
#include "SharedBuffer.h"
#include "WorkerGlobalScope.h"
#include "WorkerLoaderProxy.h"
#include "WorkerThread.h"
#include <stdio.h>
#include <wtf/CurrentTime.h>
#include <wtf/MathExtras.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/SetForScope.h>
#include <wtf/text/CString.h>
namespace WebCore {
static const int cDefaultCacheCapacity = 8192 * 1024;
static const double cMinDelayBeforeLiveDecodedPrune = 1; static const float cTargetPrunePercentage = .95f;
MemoryCache& MemoryCache::singleton()
{
ASSERT(WTF::isMainThread());
static NeverDestroyed<MemoryCache> memoryCache;
return memoryCache;
}
MemoryCache::MemoryCache()
: m_disabled(false)
, m_inPruneResources(false)
, m_capacity(cDefaultCacheCapacity)
, m_minDeadCapacity(0)
, m_maxDeadCapacity(cDefaultCacheCapacity)
, m_liveSize(0)
, m_deadSize(0)
, m_pruneTimer(*this, &MemoryCache::prune)
{
static_assert(sizeof(long long) > sizeof(unsigned), "Numerical overflow can happen when adjusting the size of the cached memory.");
}
auto MemoryCache::sessionResourceMap(SessionID sessionID) const -> CachedResourceMap*
{
ASSERT(sessionID.isValid());
return m_sessionResources.get(sessionID);
}
auto MemoryCache::ensureSessionResourceMap(SessionID sessionID) -> CachedResourceMap&
{
ASSERT(sessionID.isValid());
auto& map = m_sessionResources.add(sessionID, nullptr).iterator->value;
if (!map)
map = std::make_unique<CachedResourceMap>();
return *map;
}
bool MemoryCache::shouldRemoveFragmentIdentifier(const URL& originalURL)
{
if (!originalURL.hasFragmentIdentifier())
return false;
return originalURL.protocolIsInHTTPFamily();
}
URL MemoryCache::removeFragmentIdentifierIfNeeded(const URL& originalURL)
{
if (!shouldRemoveFragmentIdentifier(originalURL))
return originalURL;
URL url = originalURL;
url.removeFragmentIdentifier();
return url;
}
bool MemoryCache::add(CachedResource& resource)
{
if (disabled())
return false;
ASSERT(WTF::isMainThread());
auto key = std::make_pair(resource.url(), resource.cachePartition());
ensureSessionResourceMap(resource.sessionID()).set(key, &resource);
resource.setInCache(true);
resourceAccessed(resource);
LOG(ResourceLoading, "MemoryCache::add Added '%s', resource %p\n", resource.url().string().latin1().data(), &resource);
return true;
}
void MemoryCache::revalidationSucceeded(CachedResource& revalidatingResource, const ResourceResponse& response)
{
ASSERT(response.source() == ResourceResponse::Source::MemoryCacheAfterValidation);
ASSERT(revalidatingResource.resourceToRevalidate());
CachedResource& resource = *revalidatingResource.resourceToRevalidate();
ASSERT(!resource.inCache());
ASSERT(resource.isLoaded());
ASSERT(!revalidatingResource.canDelete());
remove(revalidatingResource);
auto& resources = ensureSessionResourceMap(resource.sessionID());
auto key = std::make_pair(resource.url(), resource.cachePartition());
ASSERT(!resources.get(key));
resources.set(key, &resource);
resource.setInCache(true);
resource.updateResponseAfterRevalidation(response);
insertInLRUList(resource);
long long delta = resource.size();
if (resource.decodedSize() && resource.hasClients())
insertInLiveDecodedResourcesList(resource);
if (delta)
adjustSize(resource.hasClients(), delta);
revalidatingResource.switchClientsToRevalidatedResource();
ASSERT(!revalidatingResource.m_deleted);
revalidatingResource.clearResourceToRevalidate();
}
void MemoryCache::revalidationFailed(CachedResource& revalidatingResource)
{
ASSERT(WTF::isMainThread());
LOG(ResourceLoading, "Revalidation failed for %p", &revalidatingResource);
ASSERT(revalidatingResource.resourceToRevalidate());
revalidatingResource.clearResourceToRevalidate();
}
CachedResource* MemoryCache::resourceForRequest(const ResourceRequest& request, SessionID sessionID)
{
auto* resources = sessionResourceMap(sessionID);
if (!resources)
return nullptr;
return resourceForRequestImpl(request, *resources);
}
CachedResource* MemoryCache::resourceForRequestImpl(const ResourceRequest& request, CachedResourceMap& resources)
{
ASSERT(WTF::isMainThread());
URL url = removeFragmentIdentifierIfNeeded(request.url());
auto key = std::make_pair(url, request.cachePartition());
return resources.get(key);
}
unsigned MemoryCache::deadCapacity() const
{
unsigned capacity = m_capacity - std::min(m_liveSize, m_capacity); capacity = std::max(capacity, m_minDeadCapacity); capacity = std::min(capacity, m_maxDeadCapacity); return capacity;
}
unsigned MemoryCache::liveCapacity() const
{
return m_capacity - deadCapacity();
}
static CachedImageClient& dummyCachedImageClient()
{
static NeverDestroyed<CachedImageClient> client;
return client;
}
bool MemoryCache::addImageToCache(NativeImagePtr&& image, const URL& url, const String& domainForCachePartition)
{
ASSERT(image);
SessionID sessionID = SessionID::defaultSessionID();
removeImageFromCache(url, domainForCachePartition);
RefPtr<BitmapImage> bitmapImage = BitmapImage::create(WTFMove(image), nullptr);
if (!bitmapImage)
return false;
auto cachedImage = std::make_unique<CachedImage>(url, bitmapImage.get(), sessionID);
cachedImage->addClient(dummyCachedImageClient());
cachedImage->setDecodedSize(bitmapImage->decodedSize());
cachedImage->resourceRequest().setDomainForCachePartition(domainForCachePartition);
return add(*cachedImage.release());
}
void MemoryCache::removeImageFromCache(const URL& url, const String& domainForCachePartition)
{
auto* resources = sessionResourceMap(SessionID::defaultSessionID());
if (!resources)
return;
auto key = std::make_pair(url, ResourceRequest::partitionName(domainForCachePartition));
CachedResource* resource = resources->get(key);
if (!resource)
return;
if (!is<CachedImage>(*resource) || !downcast<CachedImage>(*resource).isManuallyCached()) {
remove(*resource);
return;
}
downcast<CachedImage>(*resource).removeClient(dummyCachedImageClient());
}
void MemoryCache::pruneLiveResources(bool shouldDestroyDecodedDataForAllLiveResources)
{
unsigned capacity = shouldDestroyDecodedDataForAllLiveResources ? 0 : liveCapacity();
if (capacity && m_liveSize <= capacity)
return;
unsigned targetSize = static_cast<unsigned>(capacity * cTargetPrunePercentage);
pruneLiveResourcesToSize(targetSize, shouldDestroyDecodedDataForAllLiveResources);
}
void MemoryCache::forEachResource(const WTF::Function<void(CachedResource&)>& function)
{
for (auto& unprotectedLRUList : m_allResources) {
Vector<CachedResourceHandle<CachedResource>> lruList;
copyToVector(*unprotectedLRUList, lruList);
for (auto& resource : lruList)
function(*resource);
}
}
void MemoryCache::forEachSessionResource(SessionID sessionID, const WTF::Function<void (CachedResource&)>& function)
{
auto it = m_sessionResources.find(sessionID);
if (it == m_sessionResources.end())
return;
Vector<CachedResourceHandle<CachedResource>> resourcesForSession;
copyValuesToVector(*it->value, resourcesForSession);
for (auto& resource : resourcesForSession)
function(*resource);
}
void MemoryCache::destroyDecodedDataForAllImages()
{
MemoryCache::singleton().forEachResource([](CachedResource& resource) {
if (!resource.isImage())
return;
if (auto image = downcast<CachedImage>(resource).image())
image->destroyDecodedData();
});
}
void MemoryCache::pruneLiveResourcesToSize(unsigned targetSize, bool shouldDestroyDecodedDataForAllLiveResources)
{
if (m_inPruneResources)
return;
SetForScope<bool> reentrancyProtector(m_inPruneResources, true);
double currentTime = FrameView::currentPaintTimeStamp();
if (!currentTime) currentTime = monotonicallyIncreasingTime();
auto it = m_liveDecodedResources.begin();
while (it != m_liveDecodedResources.end()) {
auto* current = *it;
++it;
ASSERT(current->hasClients());
if (current->isLoaded() && current->decodedSize()) {
double elapsedTime = currentTime - current->m_lastDecodedAccessTime;
if (!shouldDestroyDecodedDataForAllLiveResources && elapsedTime < cMinDelayBeforeLiveDecodedPrune)
return;
current->destroyDecodedData();
if (targetSize && m_liveSize <= targetSize)
return;
}
}
}
void MemoryCache::pruneDeadResources()
{
unsigned capacity = deadCapacity();
if (capacity && m_deadSize <= capacity)
return;
unsigned targetSize = static_cast<unsigned>(capacity * cTargetPrunePercentage); pruneDeadResourcesToSize(targetSize);
}
void MemoryCache::pruneDeadResourcesToSize(unsigned targetSize)
{
if (m_inPruneResources)
return;
SetForScope<bool> reentrancyProtector(m_inPruneResources, true);
if (targetSize && m_deadSize <= targetSize)
return;
bool canShrinkLRULists = true;
for (int i = m_allResources.size() - 1; i >= 0; i--) {
Vector<CachedResourceHandle<CachedResource>> lruList;
copyToVector(*m_allResources[i], lruList);
for (auto& resource : lruList) {
if (!resource->inCache())
continue;
if (!resource->hasClients() && !resource->isPreloaded() && resource->isLoaded()) {
resource->destroyDecodedData();
if (targetSize && m_deadSize <= targetSize)
return;
}
}
for (auto& resource : lruList) {
if (!resource->inCache())
continue;
if (!resource->hasClients() && !resource->isPreloaded() && !resource->isCacheValidator()) {
remove(*resource);
if (targetSize && m_deadSize <= targetSize)
return;
}
}
if (!m_allResources[i]->isEmpty())
canShrinkLRULists = false;
else if (canShrinkLRULists)
m_allResources.shrink(i);
}
}
void MemoryCache::setCapacities(unsigned minDeadBytes, unsigned maxDeadBytes, unsigned totalBytes)
{
ASSERT(minDeadBytes <= maxDeadBytes);
ASSERT(maxDeadBytes <= totalBytes);
m_minDeadCapacity = minDeadBytes;
m_maxDeadCapacity = maxDeadBytes;
m_capacity = totalBytes;
prune();
}
void MemoryCache::remove(CachedResource& resource)
{
ASSERT(WTF::isMainThread());
LOG(ResourceLoading, "Evicting resource %p for '%s' from cache", &resource, resource.url().string().latin1().data());
if (auto* resources = sessionResourceMap(resource.sessionID())) {
auto key = std::make_pair(resource.url(), resource.cachePartition());
if (resource.inCache()) {
resources->remove(key);
resource.setInCache(false);
if (resources->isEmpty())
m_sessionResources.remove(resource.sessionID());
removeFromLRUList(resource);
removeFromLiveDecodedResourcesList(resource);
adjustSize(resource.hasClients(), -static_cast<long long>(resource.size()));
} else
ASSERT(resources->get(key) != &resource);
}
resource.deleteIfPossible();
}
auto MemoryCache::lruListFor(CachedResource& resource) -> LRUList&
{
unsigned accessCount = std::max(resource.accessCount(), 1U);
unsigned queueIndex = WTF::fastLog2(resource.size() / accessCount);
#ifndef NDEBUG
resource.m_lruIndex = queueIndex;
#endif
m_allResources.reserveCapacity(queueIndex + 1);
while (m_allResources.size() <= queueIndex)
m_allResources.uncheckedAppend(std::make_unique<LRUList>());
return *m_allResources[queueIndex];
}
void MemoryCache::removeFromLRUList(CachedResource& resource)
{
if (!resource.accessCount())
return;
#if !ASSERT_DISABLED
unsigned oldListIndex = resource.m_lruIndex;
#endif
LRUList& list = lruListFor(resource);
ASSERT(resource.m_lruIndex == oldListIndex);
bool removed = list.remove(&resource);
ASSERT_UNUSED(removed, removed);
}
void MemoryCache::insertInLRUList(CachedResource& resource)
{
ASSERT(resource.inCache());
ASSERT(resource.accessCount() > 0);
auto addResult = lruListFor(resource).add(&resource);
ASSERT_UNUSED(addResult, addResult.isNewEntry);
}
void MemoryCache::resourceAccessed(CachedResource& resource)
{
ASSERT(resource.inCache());
removeFromLRUList(resource);
if (!resource.accessCount())
adjustSize(resource.hasClients(), resource.size());
resource.increaseAccessCount();
insertInLRUList(resource);
}
void MemoryCache::removeResourcesWithOrigin(SecurityOrigin& origin)
{
String originPartition = ResourceRequest::partitionName(origin.host());
Vector<CachedResource*> resourcesWithOrigin;
for (auto& resources : m_sessionResources.values()) {
for (auto& keyValue : *resources) {
auto& resource = *keyValue.value;
auto& partitionName = keyValue.key.second;
if (partitionName == originPartition) {
resourcesWithOrigin.append(&resource);
continue;
}
RefPtr<SecurityOrigin> resourceOrigin = SecurityOrigin::create(resource.url());
if (resourceOrigin->equal(&origin))
resourcesWithOrigin.append(&resource);
}
}
for (auto* resource : resourcesWithOrigin)
remove(*resource);
}
void MemoryCache::removeResourcesWithOrigins(SessionID sessionID, const HashSet<RefPtr<SecurityOrigin>>& origins)
{
auto* resourceMap = sessionResourceMap(sessionID);
if (!resourceMap)
return;
HashSet<String> originPartitions;
for (auto& origin : origins)
originPartitions.add(ResourceRequest::partitionName(origin->host()));
Vector<CachedResource*> resourcesToRemove;
for (auto& keyValuePair : *resourceMap) {
auto& resource = *keyValuePair.value;
auto& partitionName = keyValuePair.key.second;
if (originPartitions.contains(partitionName)) {
resourcesToRemove.append(&resource);
continue;
}
if (origins.contains(SecurityOrigin::create(resource.url()).ptr()))
resourcesToRemove.append(&resource);
}
for (auto& resource : resourcesToRemove)
remove(*resource);
}
void MemoryCache::getOriginsWithCache(SecurityOriginSet& origins)
{
for (auto& resources : m_sessionResources.values()) {
for (auto& keyValue : *resources) {
auto& resource = *keyValue.value;
auto& partitionName = keyValue.key.second;
if (!partitionName.isEmpty())
origins.add(SecurityOrigin::create(ASCIILiteral("http"), partitionName, 0));
else
origins.add(SecurityOrigin::create(resource.url()));
}
}
}
HashSet<RefPtr<SecurityOrigin>> MemoryCache::originsWithCache(SessionID sessionID) const
{
HashSet<RefPtr<SecurityOrigin>> origins;
auto it = m_sessionResources.find(sessionID);
if (it != m_sessionResources.end()) {
for (auto& keyValue : *it->value) {
auto& resource = *keyValue.value;
auto& partitionName = keyValue.key.second;
if (!partitionName.isEmpty())
origins.add(SecurityOrigin::create("http", partitionName, 0));
else
origins.add(SecurityOrigin::create(resource.url()));
}
}
return origins;
}
void MemoryCache::removeFromLiveDecodedResourcesList(CachedResource& resource)
{
m_liveDecodedResources.remove(&resource);
}
void MemoryCache::insertInLiveDecodedResourcesList(CachedResource& resource)
{
ASSERT(!m_liveDecodedResources.contains(&resource));
m_liveDecodedResources.add(&resource);
}
void MemoryCache::addToLiveResourcesSize(CachedResource& resource)
{
m_liveSize += resource.size();
m_deadSize -= resource.size();
}
void MemoryCache::removeFromLiveResourcesSize(CachedResource& resource)
{
m_liveSize -= resource.size();
m_deadSize += resource.size();
}
void MemoryCache::adjustSize(bool live, long long delta)
{
if (live) {
ASSERT(delta >= 0 || (static_cast<long long>(m_liveSize) + delta >= 0));
m_liveSize += delta;
} else {
ASSERT(delta >= 0 || (static_cast<long long>(m_deadSize) + delta >= 0));
m_deadSize += delta;
}
}
void MemoryCache::removeRequestFromSessionCaches(ScriptExecutionContext& context, const ResourceRequest& request)
{
if (is<WorkerGlobalScope>(context)) {
downcast<WorkerGlobalScope>(context).thread().workerLoaderProxy().postTaskToLoader([request = request.isolatedCopy()] (ScriptExecutionContext& context) {
MemoryCache::removeRequestFromSessionCaches(context, request);
});
return;
}
auto& memoryCache = MemoryCache::singleton();
for (auto& resources : memoryCache.m_sessionResources) {
if (CachedResource* resource = memoryCache.resourceForRequestImpl(request, *resources.value))
memoryCache.remove(*resource);
}
}
void MemoryCache::TypeStatistic::addResource(CachedResource& resource)
{
count++;
size += resource.size();
liveSize += resource.hasClients() ? resource.size() : 0;
decodedSize += resource.decodedSize();
}
MemoryCache::Statistics MemoryCache::getStatistics()
{
Statistics stats;
for (auto& resources : m_sessionResources.values()) {
for (auto* resource : resources->values()) {
switch (resource->type()) {
case CachedResource::ImageResource:
stats.images.addResource(*resource);
break;
case CachedResource::CSSStyleSheet:
stats.cssStyleSheets.addResource(*resource);
break;
case CachedResource::Script:
stats.scripts.addResource(*resource);
break;
#if ENABLE(XSLT)
case CachedResource::XSLStyleSheet:
stats.xslStyleSheets.addResource(*resource);
break;
#endif
#if ENABLE(SVG_FONTS)
case CachedResource::SVGFontResource:
#endif
case CachedResource::FontResource:
stats.fonts.addResource(*resource);
break;
default:
break;
}
}
}
return stats;
}
void MemoryCache::setDisabled(bool disabled)
{
m_disabled = disabled;
if (!m_disabled)
return;
while (!m_sessionResources.isEmpty()) {
auto& resources = *m_sessionResources.begin()->value;
ASSERT(!resources.isEmpty());
remove(*resources.begin()->value);
}
}
void MemoryCache::evictResources()
{
if (disabled())
return;
setDisabled(true);
setDisabled(false);
}
void MemoryCache::evictResources(SessionID sessionID)
{
if (disabled())
return;
forEachSessionResource(sessionID, [this] (CachedResource& resource) { remove(resource); });
ASSERT(!m_sessionResources.contains(sessionID));
}
bool MemoryCache::needsPruning() const
{
return m_liveSize + m_deadSize > m_capacity || m_deadSize > m_maxDeadCapacity;
}
void MemoryCache::prune()
{
if (!needsPruning())
return;
pruneDeadResources(); pruneLiveResources();
}
void MemoryCache::pruneSoon()
{
if (m_pruneTimer.isActive())
return;
if (!needsPruning())
return;
m_pruneTimer.startOneShot(0_s);
}
#ifndef NDEBUG
void MemoryCache::dumpStats()
{
Statistics s = getStatistics();
printf("%-13s %-13s %-13s %-13s %-13s\n", "", "Count", "Size", "LiveSize", "DecodedSize");
printf("%-13s %-13s %-13s %-13s %-13s\n", "-------------", "-------------", "-------------", "-------------", "-------------");
printf("%-13s %13d %13d %13d %13d\n", "Images", s.images.count, s.images.size, s.images.liveSize, s.images.decodedSize);
printf("%-13s %13d %13d %13d %13d\n", "CSS", s.cssStyleSheets.count, s.cssStyleSheets.size, s.cssStyleSheets.liveSize, s.cssStyleSheets.decodedSize);
#if ENABLE(XSLT)
printf("%-13s %13d %13d %13d %13d\n", "XSL", s.xslStyleSheets.count, s.xslStyleSheets.size, s.xslStyleSheets.liveSize, s.xslStyleSheets.decodedSize);
#endif
printf("%-13s %13d %13d %13d %13d\n", "JavaScript", s.scripts.count, s.scripts.size, s.scripts.liveSize, s.scripts.decodedSize);
printf("%-13s %13d %13d %13d %13d\n", "Fonts", s.fonts.count, s.fonts.size, s.fonts.liveSize, s.fonts.decodedSize);
printf("%-13s %-13s %-13s %-13s %-13s\n\n", "-------------", "-------------", "-------------", "-------------", "-------------");
}
void MemoryCache::dumpLRULists(bool includeLive) const
{
printf("LRU-SP lists in eviction order (Kilobytes decoded, Kilobytes encoded, Access count, Referenced):\n");
int size = m_allResources.size();
for (int i = size - 1; i >= 0; i--) {
printf("\n\nList %d: ", i);
for (auto* resource : *m_allResources[i]) {
if (includeLive || !resource->hasClients())
printf("(%.1fK, %.1fK, %uA, %dR); ", resource->decodedSize() / 1024.0f, (resource->encodedSize() + resource->overheadSize()) / 1024.0f, resource->accessCount(), resource->hasClients());
}
}
}
#endif
}