initial import
[vuplus_webkit] / Source / WebKit / chromium / src / StorageAreaProxy.cpp
1 /*
2  * Copyright (C) 2009 Google Inc. All Rights Reserved.
3  *           (C) 2008 Apple Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
25  */
26
27 #include "config.h"
28 #include "StorageAreaProxy.h"
29
30 #if ENABLE(DOM_STORAGE)
31
32 #include "DOMWindow.h"
33 #include "Document.h"
34 #include "EventNames.h"
35 #include "ExceptionCode.h"
36 #include "Frame.h"
37 #include "Page.h"
38 #include "PageGroup.h"
39 #include "SecurityOrigin.h"
40 #include "StorageAreaImpl.h"
41 #include "StorageEvent.h"
42
43 #include "WebFrameImpl.h"
44 #include "WebPermissionClient.h"
45 #include "WebStorageArea.h"
46 #include "WebString.h"
47 #include "WebURL.h"
48 #include "WebViewImpl.h"
49
50 namespace WebCore {
51
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)
56 {
57 }
58
59 StorageAreaProxy::~StorageAreaProxy()
60 {
61 }
62
63 unsigned StorageAreaProxy::length(Frame* frame) const
64 {
65     if (canAccessStorage(frame))
66         return m_storageArea->length();
67     return 0;
68 }
69
70 String StorageAreaProxy::key(unsigned index, Frame* frame) const
71 {
72     if (canAccessStorage(frame))
73         return m_storageArea->key(index);
74     return String();
75 }
76
77 String StorageAreaProxy::getItem(const String& key, Frame* frame) const
78 {
79     if (canAccessStorage(frame))
80         return m_storageArea->getItem(key);
81     return String();
82 }
83
84 String StorageAreaProxy::setItem(const String& key, const String& value, ExceptionCode& ec, Frame* frame)
85 {
86     WebKit::WebStorageArea::Result result = WebKit::WebStorageArea::ResultOK;
87     WebKit::WebString oldValue;
88     if (!canAccessStorage(frame))
89         ec = QUOTA_EXCEEDED_ERR;
90     else {
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);
96     }
97     return oldValue;
98 }
99
100 String StorageAreaProxy::removeItem(const String& key, Frame* frame)
101 {
102     if (!canAccessStorage(frame))
103         return String();
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);
108     return oldValue;
109 }
110
111 bool StorageAreaProxy::clear(Frame* frame)
112 {
113     if (!canAccessStorage(frame))
114         return false;
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;
120 }
121
122 bool StorageAreaProxy::contains(const String& key, Frame* frame) const
123 {
124     return !getItem(key, frame).isNull();
125 }
126
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)
129 {
130     Page* page = sourceFrame->page();
131     if (!page)
132         return;
133
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);
142         }
143
144         for (unsigned i = 0; i < frames.size(); ++i) {
145             ExceptionCode ec = 0;
146             Storage* storage = frames[i]->domWindow()->sessionStorage(ec);
147             if (!ec)
148                 frames[i]->document()->enqueueWindowEvent(StorageEvent::create(eventNames().storageEvent, key, oldValue, newValue, sourceFrame->document()->url(), storage));
149         }
150     } else {
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);
158             }
159         }
160
161         for (unsigned i = 0; i < frames.size(); ++i) {
162             ExceptionCode ec = 0;
163             Storage* storage = frames[i]->domWindow()->localStorage(ec);
164             if (!ec)
165                 frames[i]->document()->enqueueWindowEvent(StorageEvent::create(eventNames().storageEvent, key, oldValue, newValue, sourceFrame->document()->url(), storage));
166         }
167     }
168 }
169
170 bool StorageAreaProxy::canAccessStorage(Frame* frame) const
171 {
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);
175 }
176
177 } // namespace WebCore
178
179 #endif // ENABLE(DOM_STORAGE)