Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / storage / StorageArea.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 "core/storage/StorageArea.h"
29
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"
47
48 namespace WebCore {
49
50 DEFINE_GC_INFO(StorageArea);
51
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)
57 {
58 }
59
60 StorageArea::~StorageArea()
61 {
62 }
63
64 unsigned StorageArea::length(ExceptionState& exceptionState, Frame* frame)
65 {
66     if (!canAccessStorage(frame)) {
67         exceptionState.throwSecurityError("access is denied for this document.");
68         return 0;
69     }
70     return m_storageArea->length();
71 }
72
73 String StorageArea::key(unsigned index, ExceptionState& exceptionState, Frame* frame)
74 {
75     if (!canAccessStorage(frame)) {
76         exceptionState.throwSecurityError("access is denied for this document.");
77         return String();
78     }
79     return m_storageArea->key(index);
80 }
81
82 String StorageArea::getItem(const String& key, ExceptionState& exceptionState, Frame* frame)
83 {
84     if (!canAccessStorage(frame)) {
85         exceptionState.throwSecurityError("access is denied for this document.");
86         return String();
87     }
88     return m_storageArea->getItem(key);
89 }
90
91 void StorageArea::setItem(const String& key, const String& value, ExceptionState& exceptionState, Frame* frame)
92 {
93     if (!canAccessStorage(frame)) {
94         exceptionState.throwSecurityError("access is denied for this document.");
95         return;
96     }
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.");
101 }
102
103 void StorageArea::removeItem(const String& key, ExceptionState& exceptionState, Frame* frame)
104 {
105     if (!canAccessStorage(frame)) {
106         exceptionState.throwSecurityError("access is denied for this document.");
107         return;
108     }
109     m_storageArea->removeItem(key, frame->document()->url());
110 }
111
112 void StorageArea::clear(ExceptionState& exceptionState, Frame* frame)
113 {
114     if (!canAccessStorage(frame)) {
115         exceptionState.throwSecurityError("access is denied for this document.");
116         return;
117     }
118     m_storageArea->clear(frame->document()->url());
119 }
120
121 bool StorageArea::contains(const String& key, ExceptionState& exceptionState, Frame* frame)
122 {
123     if (!canAccessStorage(frame)) {
124         exceptionState.throwSecurityError("access is denied for this document.");
125         return false;
126     }
127     return !getItem(key, exceptionState, frame).isNull();
128 }
129
130 bool StorageArea::canAccessStorage(Frame* frame)
131 {
132     if (!frame || !frame->page())
133         return false;
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;
139     return result;
140 }
141
142 size_t StorageArea::memoryBytesUsedByCache()
143 {
144     return m_storageArea->memoryBytesUsedByCache();
145 }
146
147 void StorageArea::dispatchLocalStorageEvent(const String& key, const String& oldValue, const String& newValue, SecurityOrigin* securityOrigin, const KURL& pageURL, blink::WebStorageArea* sourceAreaInstance, bool originatedInProcess)
148 {
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));
156         }
157         InspectorInstrumentation::didDispatchDOMStorageEvent(*it, key, oldValue, newValue, LocalStorage, securityOrigin);
158     }
159 }
160
161 static Page* findPageWithSessionStorageNamespace(const blink::WebStorageNamespace& sessionNamespace)
162 {
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))
169             return *it;
170     }
171     return 0;
172 }
173
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)
175 {
176     Page* page = findPageWithSessionStorageNamespace(sessionNamespace);
177     if (!page)
178         return;
179
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));
184     }
185     InspectorInstrumentation::didDispatchDOMStorageEvent(page, key, oldValue, newValue, SessionStorage, securityOrigin);
186 }
187
188 bool StorageArea::isEventSource(Storage* storage, blink::WebStorageArea* sourceAreaInstance)
189 {
190     ASSERT(storage);
191     StorageArea* area = storage->area();
192     return area->m_storageArea == sourceAreaInstance;
193 }
194
195 } // namespace WebCore