tizen beta release
[framework/web/webkit-efl.git] / 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 #include "DOMWindow.h"
31 #include "Document.h"
32 #include "EventNames.h"
33 #include "ExceptionCode.h"
34 #include "Frame.h"
35 #include "Page.h"
36 #include "PageGroup.h"
37 #include "SecurityOrigin.h"
38 #include "StorageAreaImpl.h"
39 #include "StorageEvent.h"
40
41 #include "WebFrameImpl.h"
42 #include "WebPermissionClient.h"
43 #include "WebStorageArea.h"
44 #include "WebString.h"
45 #include "WebURL.h"
46 #include "WebViewImpl.h"
47
48 namespace WebCore {
49
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)
54 {
55 }
56
57 StorageAreaProxy::~StorageAreaProxy()
58 {
59 }
60
61 unsigned StorageAreaProxy::length(Frame* frame) const
62 {
63     if (canAccessStorage(frame))
64         return m_storageArea->length();
65     return 0;
66 }
67
68 String StorageAreaProxy::key(unsigned index, Frame* frame) const
69 {
70     if (canAccessStorage(frame))
71         return m_storageArea->key(index);
72     return String();
73 }
74
75 String StorageAreaProxy::getItem(const String& key, Frame* frame) const
76 {
77     if (canAccessStorage(frame))
78         return m_storageArea->getItem(key);
79     return String();
80 }
81
82 String StorageAreaProxy::setItem(const String& key, const String& value, ExceptionCode& ec, Frame* frame)
83 {
84     WebKit::WebStorageArea::Result result = WebKit::WebStorageArea::ResultOK;
85     WebKit::WebString oldValue;
86     if (!canAccessStorage(frame))
87         ec = QUOTA_EXCEEDED_ERR;
88     else {
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);
94     }
95     return oldValue;
96 }
97
98 String StorageAreaProxy::removeItem(const String& key, Frame* frame)
99 {
100     if (!canAccessStorage(frame))
101         return String();
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);
106     return oldValue;
107 }
108
109 bool StorageAreaProxy::clear(Frame* frame)
110 {
111     if (!canAccessStorage(frame))
112         return false;
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;
118 }
119
120 bool StorageAreaProxy::contains(const String& key, Frame* frame) const
121 {
122     return !getItem(key, frame).isNull();
123 }
124
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)
127 {
128     Page* page = sourceFrame->page();
129     if (!page)
130         return;
131
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);
140         }
141
142         for (unsigned i = 0; i < frames.size(); ++i) {
143             ExceptionCode ec = 0;
144             Storage* storage = frames[i]->domWindow()->sessionStorage(ec);
145             if (!ec)
146                 frames[i]->document()->enqueueWindowEvent(StorageEvent::create(eventNames().storageEvent, key, oldValue, newValue, sourceFrame->document()->url(), storage));
147         }
148     } else {
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);
156             }
157         }
158
159         for (unsigned i = 0; i < frames.size(); ++i) {
160             ExceptionCode ec = 0;
161             Storage* storage = frames[i]->domWindow()->localStorage(ec);
162             if (!ec)
163                 frames[i]->document()->enqueueWindowEvent(StorageEvent::create(eventNames().storageEvent, key, oldValue, newValue, sourceFrame->document()->url(), storage));
164         }
165     }
166 }
167
168 bool StorageAreaProxy::canAccessStorage(Frame* frame) const
169 {
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);
173 }
174
175 } // namespace WebCore