0
|
1 |
/*
|
|
2 |
* Copyright (C) 2008 Apple Inc. All Rights Reserved.
|
|
3 |
*
|
|
4 |
* Redistribution and use in source and binary forms, with or without
|
|
5 |
* modification, are permitted provided that the following conditions
|
|
6 |
* are met:
|
|
7 |
* 1. Redistributions of source code must retain the above copyright
|
|
8 |
* notice, this list of conditions and the following disclaimer.
|
|
9 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
10 |
* notice, this list of conditions and the following disclaimer in the
|
|
11 |
* documentation and/or other materials provided with the distribution.
|
|
12 |
*
|
|
13 |
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
|
|
14 |
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
15 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
16 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
|
|
17 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
18 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
19 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
20 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
|
21 |
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
22 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
23 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
24 |
*/
|
|
25 |
|
|
26 |
#include "config.h"
|
|
27 |
#include "StorageAreaImpl.h"
|
|
28 |
|
|
29 |
#if ENABLE(DOM_STORAGE)
|
|
30 |
|
|
31 |
#include "ExceptionCode.h"
|
|
32 |
#include "Frame.h"
|
|
33 |
#include "Settings.h"
|
|
34 |
#include "StorageAreaSync.h"
|
|
35 |
#include "StorageEventDispatcher.h"
|
|
36 |
#include "StorageMap.h"
|
|
37 |
#include "StorageSyncManager.h"
|
|
38 |
|
|
39 |
namespace WebCore {
|
|
40 |
|
|
41 |
StorageAreaImpl::~StorageAreaImpl()
|
|
42 |
{
|
|
43 |
ASSERT(isMainThread());
|
|
44 |
}
|
|
45 |
|
|
46 |
PassRefPtr<StorageAreaImpl> StorageAreaImpl::create(StorageType storageType, PassRefPtr<SecurityOrigin> origin, PassRefPtr<StorageSyncManager> syncManager, unsigned quota)
|
|
47 |
{
|
|
48 |
return adoptRef(new StorageAreaImpl(storageType, origin, syncManager, quota));
|
|
49 |
}
|
|
50 |
|
|
51 |
StorageAreaImpl::StorageAreaImpl(StorageType storageType, PassRefPtr<SecurityOrigin> origin, PassRefPtr<StorageSyncManager> syncManager, unsigned quota)
|
|
52 |
: m_storageType(storageType)
|
|
53 |
, m_securityOrigin(origin)
|
|
54 |
, m_storageMap(StorageMap::create(quota))
|
|
55 |
, m_storageSyncManager(syncManager)
|
|
56 |
#ifndef NDEBUG
|
|
57 |
, m_isShutdown(false)
|
|
58 |
#endif
|
|
59 |
{
|
|
60 |
ASSERT(isMainThread());
|
|
61 |
ASSERT(m_securityOrigin);
|
|
62 |
ASSERT(m_storageMap);
|
|
63 |
|
|
64 |
// FIXME: If there's no backing storage for LocalStorage, the default WebKit behavior should be that of private browsing,
|
|
65 |
// not silently ignoring it. https://bugs.webkit.org/show_bug.cgi?id=25894
|
|
66 |
if (m_storageSyncManager) {
|
|
67 |
m_storageAreaSync = StorageAreaSync::create(m_storageSyncManager, this);
|
|
68 |
ASSERT(m_storageAreaSync);
|
|
69 |
}
|
|
70 |
}
|
|
71 |
|
|
72 |
PassRefPtr<StorageAreaImpl> StorageAreaImpl::copy()
|
|
73 |
{
|
|
74 |
ASSERT(!m_isShutdown);
|
|
75 |
return adoptRef(new StorageAreaImpl(this));
|
|
76 |
}
|
|
77 |
|
|
78 |
StorageAreaImpl::StorageAreaImpl(StorageAreaImpl* area)
|
|
79 |
: m_storageType(area->m_storageType)
|
|
80 |
, m_securityOrigin(area->m_securityOrigin)
|
|
81 |
, m_storageMap(area->m_storageMap)
|
|
82 |
, m_storageSyncManager(area->m_storageSyncManager)
|
|
83 |
#ifndef NDEBUG
|
|
84 |
, m_isShutdown(area->m_isShutdown)
|
|
85 |
#endif
|
|
86 |
{
|
|
87 |
ASSERT(isMainThread());
|
|
88 |
ASSERT(m_securityOrigin);
|
|
89 |
ASSERT(m_storageMap);
|
|
90 |
ASSERT(!m_isShutdown);
|
|
91 |
}
|
|
92 |
|
|
93 |
static bool privateBrowsingEnabled(Frame* frame)
|
|
94 |
{
|
|
95 |
#if PLATFORM(CHROMIUM)
|
|
96 |
// The frame pointer can be NULL in Chromium since this call is made in a different
|
|
97 |
// process from where the Frame object exists. Luckily, private browseing is
|
|
98 |
// implemented differently in Chromium, so it'd never return true anyway.
|
|
99 |
ASSERT(!frame);
|
|
100 |
return false;
|
|
101 |
#else
|
|
102 |
return frame->page()->settings()->privateBrowsingEnabled();
|
|
103 |
#endif
|
|
104 |
}
|
|
105 |
|
|
106 |
unsigned StorageAreaImpl::length() const
|
|
107 |
{
|
|
108 |
ASSERT(!m_isShutdown);
|
|
109 |
return m_storageMap->length();
|
|
110 |
}
|
|
111 |
|
|
112 |
String StorageAreaImpl::key(unsigned index) const
|
|
113 |
{
|
|
114 |
ASSERT(!m_isShutdown);
|
|
115 |
blockUntilImportComplete();
|
|
116 |
return m_storageMap->key(index);
|
|
117 |
}
|
|
118 |
|
|
119 |
String StorageAreaImpl::getItem(const String& key) const
|
|
120 |
{
|
|
121 |
ASSERT(!m_isShutdown);
|
|
122 |
blockUntilImportComplete();
|
|
123 |
|
|
124 |
return m_storageMap->getItem(key);
|
|
125 |
}
|
|
126 |
|
|
127 |
void StorageAreaImpl::setItem(const String& key, const String& value, ExceptionCode& ec, Frame* frame)
|
|
128 |
{
|
|
129 |
ASSERT(!m_isShutdown);
|
|
130 |
ASSERT(!value.isNull());
|
|
131 |
blockUntilImportComplete();
|
|
132 |
|
|
133 |
if (privateBrowsingEnabled(frame)) {
|
|
134 |
ec = QUOTA_EXCEEDED_ERR;
|
|
135 |
return;
|
|
136 |
}
|
|
137 |
|
|
138 |
String oldValue;
|
|
139 |
bool quotaException;
|
|
140 |
RefPtr<StorageMap> newMap = m_storageMap->setItem(key, value, oldValue, quotaException);
|
|
141 |
|
|
142 |
if (quotaException) {
|
|
143 |
ec = QUOTA_EXCEEDED_ERR;
|
|
144 |
return;
|
|
145 |
}
|
|
146 |
|
|
147 |
if (newMap)
|
|
148 |
m_storageMap = newMap.release();
|
|
149 |
|
|
150 |
// Only notify the client if an item was actually changed
|
|
151 |
if (oldValue != value) {
|
|
152 |
if (m_storageAreaSync)
|
|
153 |
m_storageAreaSync->scheduleItemForSync(key, value);
|
|
154 |
StorageEventDispatcher::dispatch(key, oldValue, value, m_storageType, m_securityOrigin.get(), frame);
|
|
155 |
}
|
|
156 |
}
|
|
157 |
|
|
158 |
void StorageAreaImpl::removeItem(const String& key, Frame* frame)
|
|
159 |
{
|
|
160 |
ASSERT(!m_isShutdown);
|
|
161 |
blockUntilImportComplete();
|
|
162 |
|
|
163 |
if (privateBrowsingEnabled(frame))
|
|
164 |
return;
|
|
165 |
|
|
166 |
String oldValue;
|
|
167 |
RefPtr<StorageMap> newMap = m_storageMap->removeItem(key, oldValue);
|
|
168 |
if (newMap)
|
|
169 |
m_storageMap = newMap.release();
|
|
170 |
|
|
171 |
// Only notify the client if an item was actually removed
|
|
172 |
if (!oldValue.isNull()) {
|
|
173 |
if (m_storageAreaSync)
|
|
174 |
m_storageAreaSync->scheduleItemForSync(key, String());
|
|
175 |
StorageEventDispatcher::dispatch(key, oldValue, String(), m_storageType, m_securityOrigin.get(), frame);
|
|
176 |
}
|
|
177 |
}
|
|
178 |
|
|
179 |
void StorageAreaImpl::clear(Frame* frame)
|
|
180 |
{
|
|
181 |
ASSERT(!m_isShutdown);
|
|
182 |
blockUntilImportComplete();
|
|
183 |
|
|
184 |
if (privateBrowsingEnabled(frame))
|
|
185 |
return;
|
|
186 |
|
|
187 |
unsigned quota = m_storageMap->quota();
|
|
188 |
m_storageMap = StorageMap::create(quota);
|
|
189 |
|
|
190 |
if (m_storageAreaSync)
|
|
191 |
m_storageAreaSync->scheduleClear();
|
|
192 |
StorageEventDispatcher::dispatch(String(), String(), String(), m_storageType, m_securityOrigin.get(), frame);
|
|
193 |
}
|
|
194 |
|
|
195 |
bool StorageAreaImpl::contains(const String& key) const
|
|
196 |
{
|
|
197 |
ASSERT(!m_isShutdown);
|
|
198 |
blockUntilImportComplete();
|
|
199 |
|
|
200 |
return m_storageMap->contains(key);
|
|
201 |
}
|
|
202 |
|
|
203 |
void StorageAreaImpl::importItem(const String& key, const String& value)
|
|
204 |
{
|
|
205 |
ASSERT(!m_isShutdown);
|
|
206 |
m_storageMap->importItem(key, value);
|
|
207 |
}
|
|
208 |
|
|
209 |
SecurityOrigin* StorageAreaImpl::securityOrigin()
|
|
210 |
{
|
|
211 |
return m_securityOrigin.get();
|
|
212 |
}
|
|
213 |
|
|
214 |
void StorageAreaImpl::close()
|
|
215 |
{
|
|
216 |
if (m_storageAreaSync)
|
|
217 |
m_storageAreaSync->scheduleFinalSync();
|
|
218 |
|
|
219 |
#ifndef NDEBUG
|
|
220 |
m_isShutdown = true;
|
|
221 |
#endif
|
|
222 |
}
|
|
223 |
|
|
224 |
void StorageAreaImpl::blockUntilImportComplete() const
|
|
225 |
{
|
|
226 |
if (m_storageAreaSync)
|
|
227 |
m_storageAreaSync->blockUntilImportComplete();
|
|
228 |
}
|
|
229 |
|
|
230 |
}
|
|
231 |
|
|
232 |
#endif // ENABLE(DOM_STORAGE)
|