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 "StorageNamespaceImpl.h"
29 #include "SecurityOriginHash.h"
30 #include "StorageAreaImpl.h"
31 #include "StorageMap.h"
32 #include "StorageSyncManager.h"
33 #include "StorageTracker.h"
34 #include <wtf/MainThread.h>
35 #include <wtf/StdLibExtras.h>
36 #include <wtf/text/StringHash.h>
40 typedef HashMap<String, StorageNamespace*> LocalStorageNamespaceMap;
42 static LocalStorageNamespaceMap& localStorageNamespaceMap()
44 DEFINE_STATIC_LOCAL(LocalStorageNamespaceMap, localStorageNamespaceMap, ());
45 return localStorageNamespaceMap;
48 PassRefPtr<StorageNamespace> StorageNamespaceImpl::localStorageNamespace(const String& path, unsigned quota)
50 const String lookupPath = path.isNull() ? String("") : path;
51 LocalStorageNamespaceMap::iterator it = localStorageNamespaceMap().find(lookupPath);
52 if (it == localStorageNamespaceMap().end()) {
53 RefPtr<StorageNamespace> storageNamespace = adoptRef(new StorageNamespaceImpl(LocalStorage, lookupPath, quota));
54 localStorageNamespaceMap().set(lookupPath, storageNamespace.get());
55 return storageNamespace.release();
61 PassRefPtr<StorageNamespace> StorageNamespaceImpl::sessionStorageNamespace(unsigned quota)
63 return adoptRef(new StorageNamespaceImpl(SessionStorage, String(), quota));
66 StorageNamespaceImpl::StorageNamespaceImpl(StorageType storageType, const String& path, unsigned quota)
67 : m_storageType(storageType)
68 , m_path(path.isolatedCopy())
73 if (m_storageType == LocalStorage && !m_path.isEmpty())
74 m_syncManager = StorageSyncManager::create(m_path);
77 StorageNamespaceImpl::~StorageNamespaceImpl()
79 ASSERT(isMainThread());
81 if (m_storageType == LocalStorage) {
82 ASSERT(localStorageNamespaceMap().get(m_path) == this);
83 localStorageNamespaceMap().remove(m_path);
90 PassRefPtr<StorageNamespace> StorageNamespaceImpl::copy()
92 ASSERT(isMainThread());
93 ASSERT(!m_isShutdown);
94 ASSERT(m_storageType == SessionStorage);
96 RefPtr<StorageNamespaceImpl> newNamespace = adoptRef(new StorageNamespaceImpl(m_storageType, m_path, m_quota));
98 StorageAreaMap::iterator end = m_storageAreaMap.end();
99 for (StorageAreaMap::iterator i = m_storageAreaMap.begin(); i != end; ++i)
100 newNamespace->m_storageAreaMap.set(i->first, i->second->copy());
101 return newNamespace.release();
104 PassRefPtr<StorageArea> StorageNamespaceImpl::storageArea(PassRefPtr<SecurityOrigin> prpOrigin)
106 ASSERT(isMainThread());
107 ASSERT(!m_isShutdown);
109 RefPtr<SecurityOrigin> origin = prpOrigin;
110 RefPtr<StorageAreaImpl> storageArea;
111 if ((storageArea = m_storageAreaMap.get(origin)))
112 return storageArea.release();
114 storageArea = StorageAreaImpl::create(m_storageType, origin, m_syncManager, m_quota);
115 m_storageAreaMap.set(origin.release(), storageArea);
116 return storageArea.release();
119 void StorageNamespaceImpl::close()
121 ASSERT(isMainThread());
126 // If we're session storage, we shouldn't need to do any work here.
127 if (m_storageType == SessionStorage) {
128 ASSERT(!m_syncManager);
132 StorageAreaMap::iterator end = m_storageAreaMap.end();
133 for (StorageAreaMap::iterator it = m_storageAreaMap.begin(); it != end; ++it)
137 m_syncManager->close();
142 void StorageNamespaceImpl::clearOriginForDeletion(SecurityOrigin* origin)
144 ASSERT(isMainThread());
146 RefPtr<StorageAreaImpl> storageArea = m_storageAreaMap.get(origin);
148 storageArea->clearForOriginDeletion();
151 void StorageNamespaceImpl::clearAllOriginsForDeletion()
153 ASSERT(isMainThread());
155 StorageAreaMap::iterator end = m_storageAreaMap.end();
156 for (StorageAreaMap::iterator it = m_storageAreaMap.begin(); it != end; ++it)
157 it->second->clearForOriginDeletion();
160 void StorageNamespaceImpl::sync()
162 ASSERT(isMainThread());
163 StorageAreaMap::iterator end = m_storageAreaMap.end();
164 for (StorageAreaMap::iterator it = m_storageAreaMap.begin(); it != end; ++it)
168 } // namespace WebCore