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 "StorageAreaProxy.h"
30 #if ENABLE(DOM_STORAGE)
32 #include "DOMWindow.h"
34 #include "EventNames.h"
35 #include "ExceptionCode.h"
38 #include "PageGroup.h"
39 #include "SecurityOrigin.h"
40 #include "StorageAreaImpl.h"
41 #include "StorageEvent.h"
43 #include "WebFrameImpl.h"
44 #include "WebPermissionClient.h"
45 #include "WebStorageArea.h"
46 #include "WebString.h"
48 #include "WebViewImpl.h"
52 // FIXME: storageArea argument should be a PassOwnPtr.
53 StorageAreaProxy::StorageAreaProxy(WebKit::WebStorageArea* storageArea, StorageType storageType)
54 : m_storageArea(adoptPtr(storageArea))
55 , m_storageType(storageType)
59 StorageAreaProxy::~StorageAreaProxy()
63 unsigned StorageAreaProxy::length(Frame* frame) const
65 if (canAccessStorage(frame))
66 return m_storageArea->length();
70 String StorageAreaProxy::key(unsigned index, Frame* frame) const
72 if (canAccessStorage(frame))
73 return m_storageArea->key(index);
77 String StorageAreaProxy::getItem(const String& key, Frame* frame) const
79 if (canAccessStorage(frame))
80 return m_storageArea->getItem(key);
84 String StorageAreaProxy::setItem(const String& key, const String& value, ExceptionCode& ec, Frame* frame)
86 WebKit::WebStorageArea::Result result = WebKit::WebStorageArea::ResultOK;
87 WebKit::WebString oldValue;
88 if (!canAccessStorage(frame))
89 ec = QUOTA_EXCEEDED_ERR;
91 m_storageArea->setItem(key, value, frame->document()->url(), result, oldValue);
92 ec = (result == WebKit::WebStorageArea::ResultOK) ? 0 : QUOTA_EXCEEDED_ERR;
93 String oldValueString = oldValue;
94 if (oldValueString != value && result == WebKit::WebStorageArea::ResultOK)
95 storageEvent(key, oldValue, value, m_storageType, frame->document()->securityOrigin(), frame);
100 String StorageAreaProxy::removeItem(const String& key, Frame* frame)
102 if (!canAccessStorage(frame))
104 WebKit::WebString oldValue;
105 m_storageArea->removeItem(key, frame->document()->url(), oldValue);
106 if (!oldValue.isNull())
107 storageEvent(key, oldValue, String(), m_storageType, frame->document()->securityOrigin(), frame);
111 bool StorageAreaProxy::clear(Frame* frame)
113 if (!canAccessStorage(frame))
115 bool clearedSomething;
116 m_storageArea->clear(frame->document()->url(), clearedSomething);
117 if (clearedSomething)
118 storageEvent(String(), String(), String(), m_storageType, frame->document()->securityOrigin(), frame);
119 return clearedSomething;
122 bool StorageAreaProxy::contains(const String& key, Frame* frame) const
124 return !getItem(key, frame).isNull();
127 // Copied from WebCore/storage/StorageEventDispatcher.cpp out of necessity. It's probably best to keep it current.
128 void StorageAreaProxy::storageEvent(const String& key, const String& oldValue, const String& newValue, StorageType storageType, SecurityOrigin* securityOrigin, Frame* sourceFrame)
130 Page* page = sourceFrame->page();
134 // We need to copy all relevant frames from every page to a vector since sending the event to one frame might mutate the frame tree
135 // of any given page in the group or mutate the page group itself.
136 Vector<RefPtr<Frame> > frames;
137 if (storageType == SessionStorage) {
138 // Send events only to our page.
139 for (Frame* frame = page->mainFrame(); frame; frame = frame->tree()->traverseNext()) {
140 if (sourceFrame != frame && frame->document()->securityOrigin()->equal(securityOrigin))
141 frames.append(frame);
144 for (unsigned i = 0; i < frames.size(); ++i) {
145 ExceptionCode ec = 0;
146 Storage* storage = frames[i]->domWindow()->sessionStorage(ec);
148 frames[i]->document()->enqueueWindowEvent(StorageEvent::create(eventNames().storageEvent, key, oldValue, newValue, sourceFrame->document()->url(), storage));
151 // Send events to every page.
152 const HashSet<Page*>& pages = page->group().pages();
153 HashSet<Page*>::const_iterator end = pages.end();
154 for (HashSet<Page*>::const_iterator it = pages.begin(); it != end; ++it) {
155 for (Frame* frame = (*it)->mainFrame(); frame; frame = frame->tree()->traverseNext()) {
156 if (sourceFrame != frame && frame->document()->securityOrigin()->equal(securityOrigin))
157 frames.append(frame);
161 for (unsigned i = 0; i < frames.size(); ++i) {
162 ExceptionCode ec = 0;
163 Storage* storage = frames[i]->domWindow()->localStorage(ec);
165 frames[i]->document()->enqueueWindowEvent(StorageEvent::create(eventNames().storageEvent, key, oldValue, newValue, sourceFrame->document()->url(), storage));
170 bool StorageAreaProxy::canAccessStorage(Frame* frame) const
172 WebKit::WebFrameImpl* webFrame = WebKit::WebFrameImpl::fromFrame(frame);
173 WebKit::WebViewImpl* webView = webFrame->viewImpl();
174 return !webView->permissionClient() || webView->permissionClient()->allowStorage(webFrame, m_storageType == LocalStorage);
177 } // namespace WebCore
179 #endif // ENABLE(DOM_STORAGE)