2 * Copyright (C) 2009 Google Inc. All Rights Reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "core/storage/StorageArea.h"
30 #include "bindings/v8/ExceptionState.h"
31 #include "core/dom/Document.h"
32 #include "core/dom/ExceptionCode.h"
33 #include "core/events/ThreadLocalEventNames.h"
34 #include "core/frame/DOMWindow.h"
35 #include "core/frame/Frame.h"
36 #include "core/inspector/InspectorInstrumentation.h"
37 #include "core/page/Page.h"
38 #include "core/page/PageGroup.h"
39 #include "core/page/StorageClient.h"
40 #include "core/storage/Storage.h"
41 #include "core/storage/StorageEvent.h"
42 #include "core/storage/StorageNamespace.h"
43 #include "platform/weborigin/SecurityOrigin.h"
44 #include "public/platform/WebStorageArea.h"
45 #include "public/platform/WebString.h"
46 #include "public/platform/WebURL.h"
50 DEFINE_GC_INFO(StorageArea);
52 StorageArea::StorageArea(PassOwnPtr<blink::WebStorageArea> storageArea, StorageType storageType)
53 : m_storageArea(storageArea)
54 , m_storageType(storageType)
55 , m_canAccessStorageCachedResult(false)
56 , m_canAccessStorageCachedFrame(0)
60 StorageArea::~StorageArea()
64 unsigned StorageArea::length(ExceptionState& exceptionState, Frame* frame)
66 if (!canAccessStorage(frame)) {
67 exceptionState.throwSecurityError("access is denied for this document.");
70 return m_storageArea->length();
73 String StorageArea::key(unsigned index, ExceptionState& exceptionState, Frame* frame)
75 if (!canAccessStorage(frame)) {
76 exceptionState.throwSecurityError("access is denied for this document.");
79 return m_storageArea->key(index);
82 String StorageArea::getItem(const String& key, ExceptionState& exceptionState, Frame* frame)
84 if (!canAccessStorage(frame)) {
85 exceptionState.throwSecurityError("access is denied for this document.");
88 return m_storageArea->getItem(key);
91 void StorageArea::setItem(const String& key, const String& value, ExceptionState& exceptionState, Frame* frame)
93 if (!canAccessStorage(frame)) {
94 exceptionState.throwSecurityError("access is denied for this document.");
97 blink::WebStorageArea::Result result = blink::WebStorageArea::ResultOK;
98 m_storageArea->setItem(key, value, frame->document()->url(), result);
99 if (result != blink::WebStorageArea::ResultOK)
100 exceptionState.throwDOMException(QuotaExceededError, "Setting the value of '" + key + "' exceeded the quota.");
103 void StorageArea::removeItem(const String& key, ExceptionState& exceptionState, Frame* frame)
105 if (!canAccessStorage(frame)) {
106 exceptionState.throwSecurityError("access is denied for this document.");
109 m_storageArea->removeItem(key, frame->document()->url());
112 void StorageArea::clear(ExceptionState& exceptionState, Frame* frame)
114 if (!canAccessStorage(frame)) {
115 exceptionState.throwSecurityError("access is denied for this document.");
118 m_storageArea->clear(frame->document()->url());
121 bool StorageArea::contains(const String& key, ExceptionState& exceptionState, Frame* frame)
123 if (!canAccessStorage(frame)) {
124 exceptionState.throwSecurityError("access is denied for this document.");
127 return !getItem(key, exceptionState, frame).isNull();
130 bool StorageArea::canAccessStorage(Frame* frame)
132 if (!frame || !frame->page())
134 if (m_canAccessStorageCachedFrame == frame)
135 return m_canAccessStorageCachedResult;
136 bool result = frame->page()->storageClient().canAccessStorage(frame, m_storageType);
137 m_canAccessStorageCachedFrame = frame;
138 m_canAccessStorageCachedResult = result;
142 size_t StorageArea::memoryBytesUsedByCache()
144 return m_storageArea->memoryBytesUsedByCache();
147 void StorageArea::dispatchLocalStorageEvent(const String& key, const String& oldValue, const String& newValue, SecurityOrigin* securityOrigin, const KURL& pageURL, blink::WebStorageArea* sourceAreaInstance, bool originatedInProcess)
149 // FIXME: This looks suspicious. Why doesn't this use allPages instead?
150 const HashSet<Page*>& pages = PageGroup::sharedGroup()->pages();
151 for (HashSet<Page*>::const_iterator it = pages.begin(); it != pages.end(); ++it) {
152 for (Frame* frame = (*it)->mainFrame(); frame; frame = frame->tree().traverseNext()) {
153 Storage* storage = frame->domWindow()->optionalLocalStorage();
154 if (storage && frame->document()->securityOrigin()->canAccess(securityOrigin) && !isEventSource(storage, sourceAreaInstance))
155 frame->domWindow()->enqueueWindowEvent(StorageEvent::create(EventTypeNames::storage, key, oldValue, newValue, pageURL, storage));
157 InspectorInstrumentation::didDispatchDOMStorageEvent(*it, key, oldValue, newValue, LocalStorage, securityOrigin);
161 static Page* findPageWithSessionStorageNamespace(const blink::WebStorageNamespace& sessionNamespace)
163 // FIXME: This looks suspicious. Why doesn't this use allPages instead?
164 const HashSet<Page*>& pages = PageGroup::sharedGroup()->pages();
165 for (HashSet<Page*>::const_iterator it = pages.begin(); it != pages.end(); ++it) {
166 const bool dontCreateIfMissing = false;
167 StorageNamespace* storageNamespace = (*it)->sessionStorage(dontCreateIfMissing);
168 if (storageNamespace && storageNamespace->isSameNamespace(sessionNamespace))
174 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)
176 Page* page = findPageWithSessionStorageNamespace(sessionNamespace);
180 for (Frame* frame = page->mainFrame(); frame; frame = frame->tree().traverseNext()) {
181 Storage* storage = frame->domWindow()->optionalSessionStorage();
182 if (storage && frame->document()->securityOrigin()->canAccess(securityOrigin) && !isEventSource(storage, sourceAreaInstance))
183 frame->domWindow()->enqueueWindowEvent(StorageEvent::create(EventTypeNames::storage, key, oldValue, newValue, pageURL, storage));
185 InspectorInstrumentation::didDispatchDOMStorageEvent(page, key, oldValue, newValue, SessionStorage, securityOrigin);
188 bool StorageArea::isEventSource(Storage* storage, blink::WebStorageArea* sourceAreaInstance)
191 StorageArea* area = storage->area();
192 return area->m_storageArea == sourceAreaInstance;
195 } // namespace WebCore