StorageAreaMap.cpp [plain text]
#include "config.h"
#include "StorageAreaMap.h"
#include "StorageAreaImpl.h"
#include "StorageAreaMapMessages.h"
#include "StorageManagerMessages.h"
#include "StorageNamespaceImpl.h"
#include "WebPage.h"
#include "WebPageGroupProxy.h"
#include "WebProcess.h"
#include <WebCore/DOMWindow.h>
#include <WebCore/Document.h>
#include <WebCore/MainFrame.h>
#include <WebCore/Page.h>
#include <WebCore/PageGroup.h>
#include <WebCore/SecurityOriginData.h>
#include <WebCore/Storage.h>
#include <WebCore/StorageEventDispatcher.h>
#include <WebCore/StorageMap.h>
using namespace WebCore;
namespace WebKit {
static uint64_t generateStorageMapID()
{
static uint64_t storageMapID;
return ++storageMapID;
}
Ref<StorageAreaMap> StorageAreaMap::create(StorageNamespaceImpl* storageNamespace, Ref<WebCore::SecurityOrigin>&& securityOrigin)
{
return adoptRef(*new StorageAreaMap(storageNamespace, WTFMove(securityOrigin)));
}
StorageAreaMap::StorageAreaMap(StorageNamespaceImpl* storageNamespace, Ref<WebCore::SecurityOrigin>&& securityOrigin)
: m_storageNamespace(*storageNamespace)
, m_storageMapID(generateStorageMapID())
, m_storageType(storageNamespace->storageType())
, m_storageNamespaceID(storageNamespace->storageNamespaceID())
, m_quotaInBytes(storageNamespace->quotaInBytes())
, m_securityOrigin(WTFMove(securityOrigin))
, m_currentSeed(0)
, m_hasPendingClear(false)
, m_hasPendingGetValues(false)
{
switch (m_storageType) {
case WebCore::LocalStorage:
if (SecurityOrigin* topLevelOrigin = storageNamespace->topLevelOrigin())
WebProcess::singleton().parentProcessConnection()->send(Messages::StorageManager::CreateTransientLocalStorageMap(m_storageMapID, storageNamespace->storageNamespaceID(), SecurityOriginData::fromSecurityOrigin(*topLevelOrigin), SecurityOriginData::fromSecurityOrigin(m_securityOrigin)), 0);
else
WebProcess::singleton().parentProcessConnection()->send(Messages::StorageManager::CreateLocalStorageMap(m_storageMapID, storageNamespace->storageNamespaceID(), SecurityOriginData::fromSecurityOrigin(m_securityOrigin)), 0);
break;
case WebCore::SessionStorage:
WebProcess::singleton().parentProcessConnection()->send(Messages::StorageManager::CreateSessionStorageMap(m_storageMapID, storageNamespace->storageNamespaceID(), SecurityOriginData::fromSecurityOrigin(m_securityOrigin)), 0);
break;
}
WebProcess::singleton().addMessageReceiver(Messages::StorageAreaMap::messageReceiverName(), m_storageMapID, *this);
}
StorageAreaMap::~StorageAreaMap()
{
WebProcess::singleton().parentProcessConnection()->send(Messages::StorageManager::DestroyStorageMap(m_storageMapID), 0);
WebProcess::singleton().removeMessageReceiver(Messages::StorageAreaMap::messageReceiverName(), m_storageMapID);
m_storageNamespace->didDestroyStorageAreaMap(*this);
}
unsigned StorageAreaMap::length()
{
loadValuesIfNeeded();
return m_storageMap->length();
}
String StorageAreaMap::key(unsigned index)
{
loadValuesIfNeeded();
return m_storageMap->key(index);
}
String StorageAreaMap::item(const String& key)
{
loadValuesIfNeeded();
return m_storageMap->getItem(key);
}
void StorageAreaMap::setItem(Frame* sourceFrame, StorageAreaImpl* sourceArea, const String& key, const String& value, bool& quotaException)
{
loadValuesIfNeeded();
ASSERT(m_storageMap->hasOneRef());
String oldValue;
quotaException = false;
m_storageMap->setItem(key, value, oldValue, quotaException);
if (quotaException)
return;
if (oldValue == value)
return;
m_pendingValueChanges.add(key);
WebProcess::singleton().parentProcessConnection()->send(Messages::StorageManager::SetItem(m_storageMapID, sourceArea->storageAreaID(), m_currentSeed, key, value, sourceFrame->document()->url()), 0);
}
void StorageAreaMap::removeItem(WebCore::Frame* sourceFrame, StorageAreaImpl* sourceArea, const String& key)
{
loadValuesIfNeeded();
ASSERT(m_storageMap->hasOneRef());
String oldValue;
m_storageMap->removeItem(key, oldValue);
if (oldValue.isNull())
return;
m_pendingValueChanges.add(key);
WebProcess::singleton().parentProcessConnection()->send(Messages::StorageManager::RemoveItem(m_storageMapID, sourceArea->storageAreaID(), m_currentSeed, key, sourceFrame->document()->url()), 0);
}
void StorageAreaMap::clear(WebCore::Frame* sourceFrame, StorageAreaImpl* sourceArea)
{
resetValues();
m_hasPendingClear = true;
m_storageMap = StorageMap::create(m_quotaInBytes);
WebProcess::singleton().parentProcessConnection()->send(Messages::StorageManager::Clear(m_storageMapID, sourceArea->storageAreaID(), m_currentSeed, sourceFrame->document()->url()), 0);
}
bool StorageAreaMap::contains(const String& key)
{
loadValuesIfNeeded();
return m_storageMap->contains(key);
}
void StorageAreaMap::resetValues()
{
m_storageMap = nullptr;
m_pendingValueChanges.clear();
m_hasPendingClear = false;
m_hasPendingGetValues = false;
m_currentSeed++;
}
void StorageAreaMap::loadValuesIfNeeded()
{
if (m_storageMap)
return;
HashMap<String, String> values;
WebProcess::singleton().parentProcessConnection()->sendSync(Messages::StorageManager::GetValues(m_storageMapID, m_currentSeed), Messages::StorageManager::GetValues::Reply(values), 0);
m_storageMap = StorageMap::create(m_quotaInBytes);
m_storageMap->importItems(values);
m_hasPendingGetValues = true;
}
void StorageAreaMap::didGetValues(uint64_t storageMapSeed)
{
if (m_currentSeed != storageMapSeed)
return;
ASSERT(m_hasPendingGetValues);
m_hasPendingGetValues = false;
}
void StorageAreaMap::didSetItem(uint64_t storageMapSeed, const String& key, bool quotaError)
{
if (m_currentSeed != storageMapSeed)
return;
ASSERT(m_pendingValueChanges.contains(key));
if (quotaError) {
resetValues();
return;
}
m_pendingValueChanges.remove(key);
}
void StorageAreaMap::didRemoveItem(uint64_t storageMapSeed, const String& key)
{
if (m_currentSeed != storageMapSeed)
return;
ASSERT(m_pendingValueChanges.contains(key));
m_pendingValueChanges.remove(key);
}
void StorageAreaMap::didClear(uint64_t storageMapSeed)
{
if (m_currentSeed != storageMapSeed)
return;
ASSERT(m_hasPendingClear);
m_hasPendingClear = false;
}
bool StorageAreaMap::shouldApplyChangeForKey(const String& key) const
{
if (!m_storageMap)
return false;
if (m_pendingValueChanges.contains(key))
return false;
return true;
}
void StorageAreaMap::applyChange(const String& key, const String& newValue)
{
ASSERT(!m_storageMap || m_storageMap->hasOneRef());
if (m_hasPendingClear || m_hasPendingGetValues)
return;
if (!key) {
auto newStorageMap = StorageMap::create(m_quotaInBytes);
for (auto it = m_pendingValueChanges.begin().keys(), end = m_pendingValueChanges.end().keys(); it != end; ++it) {
const String& key = *it;
String value = m_storageMap->getItem(key);
if (!value) {
continue;
}
String oldValue;
newStorageMap->setItemIgnoringQuota(key, oldValue);
}
m_storageMap = WTFMove(newStorageMap);
return;
}
if (!shouldApplyChangeForKey(key))
return;
if (!newValue) {
String oldValue;
m_storageMap->removeItem(key, oldValue);
return;
}
m_storageMap->setItemIgnoringQuota(key, newValue);
}
void StorageAreaMap::dispatchStorageEvent(uint64_t sourceStorageAreaID, const String& key, const String& oldValue, const String& newValue, const String& urlString)
{
if (!sourceStorageAreaID) {
applyChange(key, newValue);
}
if (storageType() == SessionStorage)
dispatchSessionStorageEvent(sourceStorageAreaID, key, oldValue, newValue, urlString);
else
dispatchLocalStorageEvent(sourceStorageAreaID, key, oldValue, newValue, urlString);
}
void StorageAreaMap::clearCache()
{
resetValues();
}
void StorageAreaMap::dispatchSessionStorageEvent(uint64_t sourceStorageAreaID, const String& key, const String& oldValue, const String& newValue, const String& urlString)
{
ASSERT(storageType() == SessionStorage);
WebPage* webPage = WebProcess::singleton().webPage(m_storageNamespaceID);
if (!webPage)
return;
Vector<RefPtr<Frame>> frames;
Page* page = webPage->corePage();
for (Frame* frame = &page->mainFrame(); frame; frame = frame->tree().traverseNext()) {
Document* document = frame->document();
if (!document->securityOrigin()->equal(m_securityOrigin.ptr()))
continue;
Storage* storage = document->domWindow()->optionalSessionStorage();
if (!storage)
continue;
StorageAreaImpl& storageArea = static_cast<StorageAreaImpl&>(storage->area());
if (storageArea.storageAreaID() == sourceStorageAreaID) {
continue;
}
frames.append(frame);
}
StorageEventDispatcher::dispatchSessionStorageEventsToFrames(*page, frames, key, oldValue, newValue, urlString, m_securityOrigin.ptr());
}
void StorageAreaMap::dispatchLocalStorageEvent(uint64_t sourceStorageAreaID, const String& key, const String& oldValue, const String& newValue, const String& urlString)
{
ASSERT(storageType() == LocalStorage);
Vector<RefPtr<Frame>> frames;
PageGroup& pageGroup = *WebProcess::singleton().webPageGroup(m_storageNamespaceID)->corePageGroup();
const HashSet<Page*>& pages = pageGroup.pages();
for (HashSet<Page*>::const_iterator it = pages.begin(), end = pages.end(); it != end; ++it) {
for (Frame* frame = &(*it)->mainFrame(); frame; frame = frame->tree().traverseNext()) {
Document* document = frame->document();
if (!document->securityOrigin()->equal(m_securityOrigin.ptr()))
continue;
Storage* storage = document->domWindow()->optionalLocalStorage();
if (!storage)
continue;
StorageAreaImpl& storageArea = static_cast<StorageAreaImpl&>(storage->area());
if (storageArea.storageAreaID() == sourceStorageAreaID) {
continue;
}
frames.append(frame);
}
}
StorageEventDispatcher::dispatchLocalStorageEventsToFrames(pageGroup, frames, key, oldValue, newValue, urlString, m_securityOrigin.ptr());
}
}