#include "config.h"
#include "Storage.h"
#include "Document.h"
#include "ExceptionCode.h"
#include "Frame.h"
#include "Page.h"
#include "SchemeRegistry.h"
#include "SecurityOrigin.h"
#include "StorageArea.h"
#include <wtf/text/WTFString.h>
namespace WebCore {
Ref<Storage> Storage::create(Frame* frame, RefPtr<StorageArea>&& storageArea)
{
return adoptRef(*new Storage(frame, WTFMove(storageArea)));
}
Storage::Storage(Frame* frame, RefPtr<StorageArea>&& storageArea)
: DOMWindowProperty(frame)
, m_storageArea(WTFMove(storageArea))
{
ASSERT(m_frame);
ASSERT(m_storageArea);
m_storageArea->incrementAccessCount();
}
Storage::~Storage()
{
m_storageArea->decrementAccessCount();
}
ExceptionOr<unsigned> Storage::length() const
{
if (!m_storageArea->canAccessStorage(m_frame))
return Exception { SECURITY_ERR };
if (isDisabledByPrivateBrowsing())
return 0;
return m_storageArea->length();
}
ExceptionOr<String> Storage::key(unsigned index) const
{
if (!m_storageArea->canAccessStorage(m_frame))
return Exception { SECURITY_ERR };
if (isDisabledByPrivateBrowsing())
return String();
return m_storageArea->key(index);
}
ExceptionOr<String> Storage::getItem(const String& key) const
{
if (!m_storageArea->canAccessStorage(m_frame))
return Exception { SECURITY_ERR };
if (isDisabledByPrivateBrowsing())
return String();
return m_storageArea->item(key);
}
ExceptionOr<void> Storage::setItem(const String& key, const String& value)
{
if (!m_storageArea->canAccessStorage(m_frame))
return Exception { SECURITY_ERR };
if (isDisabledByPrivateBrowsing())
return Exception { QUOTA_EXCEEDED_ERR };
bool quotaException = false;
m_storageArea->setItem(m_frame, key, value, quotaException);
if (quotaException)
return Exception { QUOTA_EXCEEDED_ERR };
return { };
}
ExceptionOr<void> Storage::removeItem(const String& key)
{
if (!m_storageArea->canAccessStorage(m_frame))
return Exception { SECURITY_ERR };
if (isDisabledByPrivateBrowsing())
return { };
m_storageArea->removeItem(m_frame, key);
return { };
}
ExceptionOr<void> Storage::clear()
{
if (!m_storageArea->canAccessStorage(m_frame))
return Exception { SECURITY_ERR };
if (isDisabledByPrivateBrowsing())
return { };
m_storageArea->clear(m_frame);
return { };
}
ExceptionOr<bool> Storage::contains(const String& key) const
{
if (!m_storageArea->canAccessStorage(m_frame))
return Exception { SECURITY_ERR };
if (isDisabledByPrivateBrowsing())
return false;
return m_storageArea->contains(key);
}
bool Storage::isDisabledByPrivateBrowsing() const
{
if (!m_frame->page()->usesEphemeralSession())
return false;
if (m_storageArea->storageType() == LocalStorage) {
if (SchemeRegistry::allowsLocalStorageAccessInPrivateBrowsing(m_frame->document()->securityOrigin()->protocol()))
return false;
}
return true;
}
}