2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "StorageAreaImpl.h"
30 #include "ExceptionCode.h"
33 #include "SchemeRegistry.h"
34 #include "SecurityOrigin.h"
36 #include "StorageAreaSync.h"
37 #include "StorageEventDispatcher.h"
38 #include "StorageMap.h"
39 #include "StorageSyncManager.h"
40 #include "StorageTracker.h"
41 #include <wtf/MainThread.h>
45 StorageAreaImpl::~StorageAreaImpl()
47 ASSERT(isMainThread());
50 inline StorageAreaImpl::StorageAreaImpl(StorageType storageType, PassRefPtr<SecurityOrigin> origin, PassRefPtr<StorageSyncManager> syncManager, unsigned quota)
51 : m_storageType(storageType)
52 , m_securityOrigin(origin)
53 , m_storageMap(StorageMap::create(quota))
54 , m_storageSyncManager(syncManager)
59 ASSERT(isMainThread());
60 ASSERT(m_securityOrigin);
63 // Accessing the shared global StorageTracker when a StorageArea is created
64 // ensures that the tracker is properly initialized before anyone actually needs to use it.
65 StorageTracker::tracker();
68 PassRefPtr<StorageAreaImpl> StorageAreaImpl::create(StorageType storageType, PassRefPtr<SecurityOrigin> origin, PassRefPtr<StorageSyncManager> syncManager, unsigned quota)
70 RefPtr<StorageAreaImpl> area = adoptRef(new StorageAreaImpl(storageType, origin, syncManager, quota));
72 // FIXME: If there's no backing storage for LocalStorage, the default WebKit behavior should be that of private browsing,
73 // not silently ignoring it. https://bugs.webkit.org/show_bug.cgi?id=25894
74 if (area->m_storageSyncManager) {
75 area->m_storageAreaSync = StorageAreaSync::create(area->m_storageSyncManager, area.get(), area->m_securityOrigin->databaseIdentifier());
76 ASSERT(area->m_storageAreaSync);
79 return area.release();
82 PassRefPtr<StorageAreaImpl> StorageAreaImpl::copy()
84 ASSERT(!m_isShutdown);
85 return adoptRef(new StorageAreaImpl(this));
88 StorageAreaImpl::StorageAreaImpl(StorageAreaImpl* area)
89 : m_storageType(area->m_storageType)
90 , m_securityOrigin(area->m_securityOrigin)
91 , m_storageMap(area->m_storageMap)
92 , m_storageSyncManager(area->m_storageSyncManager)
94 , m_isShutdown(area->m_isShutdown)
97 ASSERT(isMainThread());
98 ASSERT(m_securityOrigin);
100 ASSERT(!m_isShutdown);
103 bool StorageAreaImpl::disabledByPrivateBrowsingInFrame(const Frame* frame) const
105 #if PLATFORM(CHROMIUM)
106 // The frame pointer can be NULL in Chromium since this call is made in a different
107 // process from where the Frame object exists. Luckily, private browseing is
108 // implemented differently in Chromium, so it'd never return true anyway.
112 if (!frame->page() || !frame->page()->settings()->privateBrowsingEnabled())
114 if (m_storageType != LocalStorage)
116 return !SchemeRegistry::allowsLocalStorageAccessInPrivateBrowsing(frame->document()->securityOrigin()->protocol());
120 unsigned StorageAreaImpl::length(Frame*) const
122 ASSERT(!m_isShutdown);
123 blockUntilImportComplete();
125 return m_storageMap->length();
128 String StorageAreaImpl::key(unsigned index, Frame*) const
130 ASSERT(!m_isShutdown);
131 blockUntilImportComplete();
133 return m_storageMap->key(index);
136 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
137 std::pair<String, bool> StorageAreaImpl::getItem(const String& key) const
139 String StorageAreaImpl::getItem(const String& key, Frame*) const
142 ASSERT(!m_isShutdown);
143 blockUntilImportComplete();
145 return m_storageMap->getItem(key);
148 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
149 std::pair<String, bool> StorageAreaImpl::setItem(const String& key, const String& value, ExceptionCode& ec, Frame* frame, bool readOnly)
151 String StorageAreaImpl::setItem(const String& key, const String& value, ExceptionCode& ec, Frame* frame)
154 ASSERT(!m_isShutdown);
155 ASSERT(!value.isNull());
156 blockUntilImportComplete();
158 if (disabledByPrivateBrowsingInFrame(frame)) {
159 ec = QUOTA_EXCEEDED_ERR;
160 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
161 return std::make_pair(String(), false);
167 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
168 std::pair<String, bool> currentValue = m_storageMap->getItem(key);
169 if (!currentValue.first.isNull() && currentValue.second)
171 ec = NO_MODIFICATION_ALLOWED_ERR;
172 return std::make_pair(String(), false);
176 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
177 std::pair<String, bool> oldValue;
178 std::pair<String, bool> newValue = std::make_pair(value, readOnly);
183 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
184 RefPtr<StorageMap> newMap = m_storageMap->setItem(key, newValue, oldValue, quotaException);
186 RefPtr<StorageMap> newMap = m_storageMap->setItem(key, value, oldValue, quotaException);
189 m_storageMap = newMap.release();
191 if (quotaException) {
192 ec = QUOTA_EXCEEDED_ERR;
196 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
197 if (oldValue == newValue)
199 if (oldValue == value)
203 if (m_storageAreaSync)
204 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
205 m_storageAreaSync->scheduleItemForSync(key, newValue);
206 StorageEventDispatcher::dispatch(key, oldValue.first, newValue.first, m_storageType, m_securityOrigin.get(), frame);
208 m_storageAreaSync->scheduleItemForSync(key, value);
209 StorageEventDispatcher::dispatch(key, oldValue, value, m_storageType, m_securityOrigin.get(), frame);
214 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
215 std::pair<String, bool> StorageAreaImpl::removeItem(const String& key, Frame* frame, ExceptionCode* ec)
217 String StorageAreaImpl::removeItem(const String& key, Frame* frame)
220 ASSERT(!m_isShutdown);
221 blockUntilImportComplete();
223 if (disabledByPrivateBrowsingInFrame(frame))
224 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
225 return std::make_pair(String(), false);
230 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
231 std::pair<String, bool> currentValue = m_storageMap->getItem(key);
232 if (!currentValue.first.isNull() && currentValue.second)
235 *ec = NO_MODIFICATION_ALLOWED_ERR;
236 return std::make_pair(String(), false);
240 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
241 std::pair<String, bool> oldValue;
245 RefPtr<StorageMap> newMap = m_storageMap->removeItem(key, oldValue);
247 m_storageMap = newMap.release();
249 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
250 if (oldValue.first.isNull())
252 if (oldValue.isNull())
256 if (m_storageAreaSync)
257 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
258 m_storageAreaSync->scheduleItemForSync(key, std::make_pair(String(), false));
259 StorageEventDispatcher::dispatch(key, oldValue.first, String(), m_storageType, m_securityOrigin.get(), frame);
261 m_storageAreaSync->scheduleItemForSync(key, String());
262 StorageEventDispatcher::dispatch(key, oldValue, String(), m_storageType, m_securityOrigin.get(), frame);
267 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
268 bool StorageAreaImpl::clear(Frame* frame, bool removeReadOnly)
270 bool StorageAreaImpl::clear(Frame* frame)
273 ASSERT(!m_isShutdown);
274 blockUntilImportComplete();
276 if (disabledByPrivateBrowsingInFrame(frame))
279 if (!m_storageMap->length())
282 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
283 if (removeReadOnly) {
284 unsigned quota = m_storageMap->quota();
285 m_storageMap = StorageMap::create(quota);
287 int numberItemsToRemove = 0;
288 for (int i = 0; i < m_storageMap->length(); ++i) {
289 std::pair<String, bool> item = m_storageMap->getItem(m_storageMap->key(i));
290 if (!item.second) { // if attribute read only isn't set it will be removed
291 std::pair<String, bool> oldValue;
292 m_storageMap->removeItem(m_storageMap->key(i), oldValue);
293 ++numberItemsToRemove;
296 if (!numberItemsToRemove)
300 unsigned quota = m_storageMap->quota();
301 m_storageMap = StorageMap::create(quota);
304 if (m_storageAreaSync)
305 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
306 m_storageAreaSync->scheduleClear(removeReadOnly);
308 m_storageAreaSync->scheduleClear();
310 StorageEventDispatcher::dispatch(String(), String(), String(), m_storageType, m_securityOrigin.get(), frame);
314 bool StorageAreaImpl::contains(const String& key, Frame*) const
316 ASSERT(!m_isShutdown);
317 blockUntilImportComplete();
319 return m_storageMap->contains(key);
322 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
323 void StorageAreaImpl::importItem(const String& key, const std::pair<String, bool>& value)
325 void StorageAreaImpl::importItem(const String& key, const String& value)
328 ASSERT(!m_isShutdown);
329 m_storageMap->importItem(key, value);
332 void StorageAreaImpl::close()
334 if (m_storageAreaSync)
335 m_storageAreaSync->scheduleFinalSync();
342 void StorageAreaImpl::clearForOriginDeletion()
344 ASSERT(!m_isShutdown);
345 blockUntilImportComplete();
347 if (m_storageMap->length()) {
348 unsigned quota = m_storageMap->quota();
349 m_storageMap = StorageMap::create(quota);
352 if (m_storageAreaSync) {
353 m_storageAreaSync->scheduleClear();
354 m_storageAreaSync->scheduleCloseDatabase();
358 void StorageAreaImpl::sync()
360 ASSERT(!m_isShutdown);
361 blockUntilImportComplete();
363 if (m_storageAreaSync)
364 m_storageAreaSync->scheduleSync();
367 void StorageAreaImpl::blockUntilImportComplete() const
369 if (m_storageAreaSync)
370 m_storageAreaSync->blockUntilImportComplete();