This source file includes following definitions.
- m_canAccessStorageCachedFrame
- length
- key
- getItem
- setItem
- removeItem
- clear
- contains
- canAccessStorage
- memoryBytesUsedByCache
- dispatchLocalStorageEvent
- findPageWithSessionStorageNamespace
- dispatchSessionStorageEvent
- isEventSource
#include "config.h"
#include "core/storage/StorageArea.h"
#include "bindings/v8/ExceptionState.h"
#include "core/dom/Document.h"
#include "core/dom/ExceptionCode.h"
#include "core/frame/DOMWindow.h"
#include "core/frame/LocalFrame.h"
#include "core/inspector/InspectorInstrumentation.h"
#include "core/page/Page.h"
#include "core/page/StorageClient.h"
#include "core/storage/Storage.h"
#include "core/storage/StorageEvent.h"
#include "core/storage/StorageNamespace.h"
#include "platform/weborigin/SecurityOrigin.h"
#include "public/platform/WebStorageArea.h"
#include "public/platform/WebString.h"
#include "public/platform/WebURL.h"
namespace WebCore {
StorageArea::StorageArea(PassOwnPtr<blink::WebStorageArea> storageArea, StorageType storageType)
    : m_storageArea(storageArea)
    , m_storageType(storageType)
    , m_canAccessStorageCachedResult(false)
    , m_canAccessStorageCachedFrame(0)
{
}
StorageArea::~StorageArea()
{
}
unsigned StorageArea::length(ExceptionState& exceptionState, LocalFrame* frame)
{
    if (!canAccessStorage(frame)) {
        exceptionState.throwSecurityError("access is denied for this document.");
        return 0;
    }
    return m_storageArea->length();
}
String StorageArea::key(unsigned index, ExceptionState& exceptionState, LocalFrame* frame)
{
    if (!canAccessStorage(frame)) {
        exceptionState.throwSecurityError("access is denied for this document.");
        return String();
    }
    return m_storageArea->key(index);
}
String StorageArea::getItem(const String& key, ExceptionState& exceptionState, LocalFrame* frame)
{
    if (!canAccessStorage(frame)) {
        exceptionState.throwSecurityError("access is denied for this document.");
        return String();
    }
    return m_storageArea->getItem(key);
}
void StorageArea::setItem(const String& key, const String& value, ExceptionState& exceptionState, LocalFrame* frame)
{
    if (!canAccessStorage(frame)) {
        exceptionState.throwSecurityError("access is denied for this document.");
        return;
    }
    blink::WebStorageArea::Result result = blink::WebStorageArea::ResultOK;
    m_storageArea->setItem(key, value, frame->document()->url(), result);
    if (result != blink::WebStorageArea::ResultOK)
        exceptionState.throwDOMException(QuotaExceededError, "Setting the value of '" + key + "' exceeded the quota.");
}
void StorageArea::removeItem(const String& key, ExceptionState& exceptionState, LocalFrame* frame)
{
    if (!canAccessStorage(frame)) {
        exceptionState.throwSecurityError("access is denied for this document.");
        return;
    }
    m_storageArea->removeItem(key, frame->document()->url());
}
void StorageArea::clear(ExceptionState& exceptionState, LocalFrame* frame)
{
    if (!canAccessStorage(frame)) {
        exceptionState.throwSecurityError("access is denied for this document.");
        return;
    }
    m_storageArea->clear(frame->document()->url());
}
bool StorageArea::contains(const String& key, ExceptionState& exceptionState, LocalFrame* frame)
{
    if (!canAccessStorage(frame)) {
        exceptionState.throwSecurityError("access is denied for this document.");
        return false;
    }
    return !getItem(key, exceptionState, frame).isNull();
}
bool StorageArea::canAccessStorage(LocalFrame* frame)
{
    if (!frame || !frame->page())
        return false;
    if (m_canAccessStorageCachedFrame == frame)
        return m_canAccessStorageCachedResult;
    bool result = frame->page()->storageClient().canAccessStorage(frame, m_storageType);
    m_canAccessStorageCachedFrame = frame;
    m_canAccessStorageCachedResult = result;
    return result;
}
size_t StorageArea::memoryBytesUsedByCache()
{
    return m_storageArea->memoryBytesUsedByCache();
}
void StorageArea::dispatchLocalStorageEvent(const String& key, const String& oldValue, const String& newValue, SecurityOrigin* securityOrigin, const KURL& pageURL, blink::WebStorageArea* sourceAreaInstance, bool originatedInProcess)
{
    
    const HashSet<Page*>& pages = Page::ordinaryPages();
    for (HashSet<Page*>::const_iterator it = pages.begin(); it != pages.end(); ++it) {
        for (LocalFrame* frame = (*it)->mainFrame(); frame; frame = frame->tree().traverseNext()) {
            Storage* storage = frame->domWindow()->optionalLocalStorage();
            if (storage && frame->document()->securityOrigin()->canAccess(securityOrigin) && !isEventSource(storage, sourceAreaInstance))
                frame->domWindow()->enqueueWindowEvent(StorageEvent::create(EventTypeNames::storage, key, oldValue, newValue, pageURL, storage));
        }
        InspectorInstrumentation::didDispatchDOMStorageEvent(*it, key, oldValue, newValue, LocalStorage, securityOrigin);
    }
}
static Page* findPageWithSessionStorageNamespace(const blink::WebStorageNamespace& sessionNamespace)
{
    
    const HashSet<Page*>& pages = Page::ordinaryPages();
    for (HashSet<Page*>::const_iterator it = pages.begin(); it != pages.end(); ++it) {
        const bool dontCreateIfMissing = false;
        StorageNamespace* storageNamespace = (*it)->sessionStorage(dontCreateIfMissing);
        if (storageNamespace && storageNamespace->isSameNamespace(sessionNamespace))
            return *it;
    }
    return 0;
}
void StorageArea::dispatchSessionStorageEvent(const String& key, const String& oldValue, const String& newValue, SecurityOrigin* securityOrigin, const KURL& pageURL, const blink::WebStorageNamespace& sessionNamespace, blink::WebStorageArea* sourceAreaInstance, bool originatedInProcess)
{
    Page* page = findPageWithSessionStorageNamespace(sessionNamespace);
    if (!page)
        return;
    for (LocalFrame* frame = page->mainFrame(); frame; frame = frame->tree().traverseNext()) {
        Storage* storage = frame->domWindow()->optionalSessionStorage();
        if (storage && frame->document()->securityOrigin()->canAccess(securityOrigin) && !isEventSource(storage, sourceAreaInstance))
            frame->domWindow()->enqueueWindowEvent(StorageEvent::create(EventTypeNames::storage, key, oldValue, newValue, pageURL, storage));
    }
    InspectorInstrumentation::didDispatchDOMStorageEvent(page, key, oldValue, newValue, SessionStorage, securityOrigin);
}
bool StorageArea::isEventSource(Storage* storage, blink::WebStorageArea* sourceAreaInstance)
{
    ASSERT(storage);
    StorageArea* area = storage->area();
    return area->m_storageArea == sourceAreaInstance;
}
}