1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "modules/storage/DOMWindowStorage.h"
#include "core/dom/Document.h"
#include "core/frame/FrameHost.h"
#include "core/frame/LocalDOMWindow.h"
#include "core/frame/LocalFrame.h"
#include "core/frame/Settings.h"
#include "core/page/Page.h"
#include "modules/storage/Storage.h"
#include "modules/storage/StorageNamespace.h"
#include "modules/storage/StorageNamespaceController.h"
#include "wtf/PassRefPtr.h"
namespace blink {
DOMWindowStorage::DOMWindowStorage(LocalDOMWindow& window)
: Supplement<LocalDOMWindow>(window) {}
DEFINE_TRACE(DOMWindowStorage) {
visitor->trace(m_sessionStorage);
visitor->trace(m_localStorage);
Supplement<LocalDOMWindow>::trace(visitor);
}
// static
const char* DOMWindowStorage::supplementName() {
return "DOMWindowStorage";
}
// static
DOMWindowStorage& DOMWindowStorage::from(LocalDOMWindow& window) {
DOMWindowStorage* supplement = static_cast<DOMWindowStorage*>(
Supplement<LocalDOMWindow>::from(window, supplementName()));
if (!supplement) {
supplement = new DOMWindowStorage(window);
provideTo(window, supplementName(), supplement);
}
return *supplement;
}
// static
Storage* DOMWindowStorage::sessionStorage(DOMWindow& window,
ExceptionState& exceptionState) {
return from(toLocalDOMWindow(window)).sessionStorage(exceptionState);
}
// static
Storage* DOMWindowStorage::localStorage(DOMWindow& window,
ExceptionState& exceptionState) {
return from(toLocalDOMWindow(window)).localStorage(exceptionState);
}
Storage* DOMWindowStorage::sessionStorage(
ExceptionState& exceptionState) const {
if (!supplementable()->frame())
return nullptr;
Document* document = supplementable()->frame()->document();
DCHECK(document);
String accessDeniedMessage = "Access is denied for this document.";
if (!document->getSecurityOrigin()->canAccessLocalStorage()) {
if (document->isSandboxed(SandboxOrigin))
exceptionState.throwSecurityError(
"The document is sandboxed and lacks the 'allow-same-origin' flag.");
else if (document->url().protocolIs("data"))
exceptionState.throwSecurityError(
"Storage is disabled inside 'data:' URLs.");
else
exceptionState.throwSecurityError(accessDeniedMessage);
return nullptr;
}
if (m_sessionStorage) {
if (!m_sessionStorage->area()->canAccessStorage(document->frame())) {
exceptionState.throwSecurityError(accessDeniedMessage);
return nullptr;
}
return m_sessionStorage;
}
Page* page = document->page();
if (!page)
return nullptr;
StorageArea* storageArea =
StorageNamespaceController::from(page)->sessionStorage()->storageArea(
document->getSecurityOrigin());
if (!storageArea->canAccessStorage(document->frame())) {
exceptionState.throwSecurityError(accessDeniedMessage);
return nullptr;
}
m_sessionStorage = Storage::create(document->frame(), storageArea);
return m_sessionStorage;
}
Storage* DOMWindowStorage::localStorage(ExceptionState& exceptionState) const {
if (!supplementable()->frame())
return nullptr;
Document* document = supplementable()->frame()->document();
DCHECK(document);
String accessDeniedMessage = "Access is denied for this document.";
if (!document->getSecurityOrigin()->canAccessLocalStorage()) {
if (document->isSandboxed(SandboxOrigin))
exceptionState.throwSecurityError(
"The document is sandboxed and lacks the 'allow-same-origin' flag.");
else if (document->url().protocolIs("data"))
exceptionState.throwSecurityError(
"Storage is disabled inside 'data:' URLs.");
else
exceptionState.throwSecurityError(accessDeniedMessage);
return nullptr;
}
if (m_localStorage) {
if (!m_localStorage->area()->canAccessStorage(document->frame())) {
exceptionState.throwSecurityError(accessDeniedMessage);
return nullptr;
}
return m_localStorage;
}
// FIXME: Seems this check should be much higher?
FrameHost* host = document->frameHost();
if (!host || !host->settings().getLocalStorageEnabled())
return nullptr;
StorageArea* storageArea =
StorageNamespace::localStorageArea(document->getSecurityOrigin());
if (!storageArea->canAccessStorage(document->frame())) {
exceptionState.throwSecurityError(accessDeniedMessage);
return nullptr;
}
m_localStorage = Storage::create(document->frame(), storageArea);
return m_localStorage;
}
} // namespace blink
|