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 #include "DOMWindow.h"
32 #include "EventNames.h"
33 #include "ExceptionCode.h"
36 #include "PageGroup.h"
37 #include "SecurityOrigin.h"
38 #include "StorageAreaImpl.h"
39 #include "StorageEvent.h"
41 #include "WebFrameImpl.h"
42 #include "WebPermissionClient.h"
43 #include "WebStorageArea.h"
44 #include "WebString.h"
46 #include "WebViewImpl.h"
50 // FIXME: storageArea argument should be a PassOwnPtr.
51 StorageAreaProxy::StorageAreaProxy(WebKit::WebStorageArea* storageArea, StorageType storageType)
52 : m_storageArea(adoptPtr(storageArea))
53 , m_storageType(storageType)
57 StorageAreaProxy::~StorageAreaProxy()
61 unsigned StorageAreaProxy::length(Frame* frame) const
63 if (canAccessStorage(frame))
64 return m_storageArea->length();
68 String StorageAreaProxy::key(unsigned index, Frame* frame) const
70 if (canAccessStorage(frame))
71 return m_storageArea->key(index);
75 String StorageAreaProxy::getItem(const String& key, Frame* frame) const
77 if (canAccessStorage(frame))
78 return m_storageArea->getItem(key);
82 String StorageAreaProxy::setItem(const String& key, const String& value, ExceptionCode& ec, Frame* frame)
84 WebKit::WebStorageArea::Result result = WebKit::WebStorageArea::ResultOK;
85 WebKit::WebString oldValue;
86 if (!canAccessStorage(frame))
87 ec = QUOTA_EXCEEDED_ERR;
89 m_storageArea->setItem(key, value, frame->document()->url(), result, oldValue);
90 ec = (result == WebKit::WebStorageArea::ResultOK) ? 0 : QUOTA_EXCEEDED_ERR;
91 String oldValueString = oldValue;
92 if (oldValueString != value && result == WebKit::WebStorageArea::ResultOK)
93 storageEvent(key, oldValue, value, m_storageType, frame->document()->securityOrigin(), frame);
98 String StorageAreaProxy::removeItem(const String& key, Frame* frame)
100 if (!canAccessStorage(frame))
102 WebKit::WebString oldValue;
103 m_storageArea->removeItem(key, frame->document()->url(), oldValue);
104 if (!oldValue.isNull())
105 storageEvent(key, oldValue, String(), m_storageType, frame->document()->securityOrigin(), frame);
109 bool StorageAreaProxy::clear(Frame* frame)
111 if (!canAccessStorage(frame))
113 bool clearedSomething;
114 m_storageArea->clear(frame->document()->url(), clearedSomething);
115 if (clearedSomething)
116 storageEvent(String(), String(), String(), m_storageType, frame->document()->securityOrigin(), frame);
117 return clearedSomething;
120 bool StorageAreaProxy::contains(const String& key, Frame* frame) const
122 return !getItem(key, frame).isNull();
125 // Copied from WebCore/storage/StorageEventDispatcher.cpp out of necessity. It's probably best to keep it current.
126 void StorageAreaProxy::storageEvent(const String& key, const String& oldValue, const String& newValue, StorageType storageType, SecurityOrigin* securityOrigin, Frame* sourceFrame)
128 Page* page = sourceFrame->page();
132 // 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
133 // of any given page in the group or mutate the page group itself.
134 Vector<RefPtr<Frame> > frames;
135 if (storageType == SessionStorage) {
136 // Send events only to our page.
137 for (Frame* frame = page->mainFrame(); frame; frame = frame->tree()->traverseNext()) {
138 if (sourceFrame != frame && frame->document()->securityOrigin()->equal(securityOrigin))
139 frames.append(frame);
142 for (unsigned i = 0; i < frames.size(); ++i) {
143 ExceptionCode ec = 0;
144 Storage* storage = frames[i]->domWindow()->sessionStorage(ec);
146 frames[i]->document()->enqueueWindowEvent(StorageEvent::create(eventNames().storageEvent, key, oldValue, newValue, sourceFrame->document()->url(), storage));
149 // Send events to every page.
150 const HashSet<Page*>& pages = page->group().pages();
151 HashSet<Page*>::const_iterator end = pages.end();
152 for (HashSet<Page*>::const_iterator it = pages.begin(); it != end; ++it) {
153 for (Frame* frame = (*it)->mainFrame(); frame; frame = frame->tree()->traverseNext()) {
154 if (sourceFrame != frame && frame->document()->securityOrigin()->equal(securityOrigin))
155 frames.append(frame);
159 for (unsigned i = 0; i < frames.size(); ++i) {
160 ExceptionCode ec = 0;
161 Storage* storage = frames[i]->domWindow()->localStorage(ec);
163 frames[i]->document()->enqueueWindowEvent(StorageEvent::create(eventNames().storageEvent, key, oldValue, newValue, sourceFrame->document()->url(), storage));
168 bool StorageAreaProxy::canAccessStorage(Frame* frame) const
170 WebKit::WebFrameImpl* webFrame = WebKit::WebFrameImpl::fromFrame(frame);
171 WebKit::WebViewImpl* webView = webFrame->viewImpl();
172 return !webView->permissionClient() || webView->permissionClient()->allowStorage(webFrame, m_storageType == LocalStorage);
175 } // namespace WebCore