ViewSnapshotStore.cpp [plain text]
#include "config.h"
#include "ViewSnapshotStore.h"
#include "WebBackForwardList.h"
#include "WebPageProxy.h"
#include <wtf/NeverDestroyed.h>
#if PLATFORM(IOS_FAMILY)
static const size_t maximumSnapshotCacheSize = 50 * (1024 * 1024);
#else
static const size_t maximumSnapshotCacheSize = 400 * (1024 * 1024);
#endif
namespace WebKit {
using namespace WebCore;
ViewSnapshotStore::ViewSnapshotStore()
{
}
ViewSnapshotStore::~ViewSnapshotStore()
{
discardSnapshotImages();
}
ViewSnapshotStore& ViewSnapshotStore::singleton()
{
static NeverDestroyed<ViewSnapshotStore> store;
return store;
}
void ViewSnapshotStore::didAddImageToSnapshot(ViewSnapshot& snapshot)
{
bool isNewEntry = m_snapshotsWithImages.add(&snapshot).isNewEntry;
ASSERT_UNUSED(isNewEntry, isNewEntry);
m_snapshotCacheSize += snapshot.imageSizeInBytes();
}
void ViewSnapshotStore::willRemoveImageFromSnapshot(ViewSnapshot& snapshot)
{
bool removed = m_snapshotsWithImages.remove(&snapshot);
ASSERT_UNUSED(removed, removed);
m_snapshotCacheSize -= snapshot.imageSizeInBytes();
}
void ViewSnapshotStore::pruneSnapshots(WebPageProxy& webPageProxy)
{
if (m_snapshotCacheSize <= maximumSnapshotCacheSize)
return;
ASSERT(!m_snapshotsWithImages.isEmpty());
m_snapshotsWithImages.first()->clearImage();
}
void ViewSnapshotStore::recordSnapshot(WebPageProxy& webPageProxy, WebBackForwardListItem& item)
{
if (webPageProxy.isShowingNavigationGestureSnapshot())
return;
pruneSnapshots(webPageProxy);
webPageProxy.willRecordNavigationSnapshot(item);
auto snapshot = webPageProxy.takeViewSnapshot();
if (!snapshot)
return;
snapshot->setRenderTreeSize(webPageProxy.renderTreeSize());
snapshot->setDeviceScaleFactor(webPageProxy.deviceScaleFactor());
snapshot->setBackgroundColor(webPageProxy.pageExtendedBackgroundColor());
snapshot->setViewScrollPosition(WebCore::roundedIntPoint(webPageProxy.viewScrollPosition()));
item.setSnapshot(WTFMove(snapshot));
}
void ViewSnapshotStore::discardSnapshotImages()
{
while (!m_snapshotsWithImages.isEmpty())
m_snapshotsWithImages.first()->clearImage();
}
ViewSnapshot::~ViewSnapshot()
{
clearImage();
}
}