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 "StorageNamespaceImpl.h"
|
|
28 |
|
|
29 |
#if ENABLE(DOM_STORAGE)
|
|
30 |
|
|
31 |
#include "SecurityOriginHash.h"
|
|
32 |
#include "StringHash.h"
|
|
33 |
#include "StorageAreaImpl.h"
|
|
34 |
#include "StorageMap.h"
|
|
35 |
#include "StorageSyncManager.h"
|
|
36 |
#include <wtf/StdLibExtras.h>
|
|
37 |
|
|
38 |
namespace WebCore {
|
|
39 |
|
|
40 |
typedef HashMap<String, StorageNamespace*> LocalStorageNamespaceMap;
|
|
41 |
|
|
42 |
static LocalStorageNamespaceMap& localStorageNamespaceMap()
|
|
43 |
{
|
|
44 |
DEFINE_STATIC_LOCAL(LocalStorageNamespaceMap, localStorageNamespaceMap, ());
|
|
45 |
return localStorageNamespaceMap;
|
|
46 |
}
|
|
47 |
|
|
48 |
PassRefPtr<StorageNamespace> StorageNamespaceImpl::localStorageNamespace(const String& path, unsigned quota)
|
|
49 |
{
|
|
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();
|
|
56 |
}
|
|
57 |
|
|
58 |
return it->second;
|
|
59 |
}
|
|
60 |
|
|
61 |
PassRefPtr<StorageNamespace> StorageNamespaceImpl::sessionStorageNamespace()
|
|
62 |
{
|
|
63 |
return adoptRef(new StorageNamespaceImpl(SessionStorage, String(), StorageMap::noQuota));
|
|
64 |
}
|
|
65 |
|
|
66 |
StorageNamespaceImpl::StorageNamespaceImpl(StorageType storageType, const String& path, unsigned quota)
|
|
67 |
: m_storageType(storageType)
|
|
68 |
, m_path(path.crossThreadString())
|
|
69 |
, m_syncManager(0)
|
|
70 |
, m_quota(quota)
|
|
71 |
, m_isShutdown(false)
|
|
72 |
{
|
|
73 |
if (m_storageType == LocalStorage && !m_path.isEmpty())
|
|
74 |
m_syncManager = StorageSyncManager::create(m_path);
|
|
75 |
}
|
|
76 |
|
|
77 |
StorageNamespaceImpl::~StorageNamespaceImpl()
|
|
78 |
{
|
|
79 |
ASSERT(isMainThread());
|
|
80 |
|
|
81 |
if (m_storageType == LocalStorage) {
|
|
82 |
ASSERT(localStorageNamespaceMap().get(m_path) == this);
|
|
83 |
localStorageNamespaceMap().remove(m_path);
|
|
84 |
}
|
|
85 |
|
|
86 |
if (!m_isShutdown)
|
|
87 |
close();
|
|
88 |
}
|
|
89 |
|
|
90 |
PassRefPtr<StorageNamespace> StorageNamespaceImpl::copy()
|
|
91 |
{
|
|
92 |
ASSERT(isMainThread());
|
|
93 |
ASSERT(!m_isShutdown);
|
|
94 |
ASSERT(m_storageType == SessionStorage);
|
|
95 |
|
|
96 |
StorageNamespaceImpl* newNamespace = new StorageNamespaceImpl(m_storageType, m_path, m_quota);
|
|
97 |
|
|
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 adoptRef(newNamespace);
|
|
102 |
}
|
|
103 |
|
|
104 |
PassRefPtr<StorageArea> StorageNamespaceImpl::storageArea(PassRefPtr<SecurityOrigin> prpOrigin)
|
|
105 |
{
|
|
106 |
ASSERT(isMainThread());
|
|
107 |
ASSERT(!m_isShutdown);
|
|
108 |
|
|
109 |
RefPtr<SecurityOrigin> origin = prpOrigin;
|
|
110 |
RefPtr<StorageAreaImpl> storageArea;
|
|
111 |
if (storageArea = m_storageAreaMap.get(origin))
|
|
112 |
return storageArea.release();
|
|
113 |
|
|
114 |
storageArea = StorageAreaImpl::create(m_storageType, origin, m_syncManager, m_quota);
|
|
115 |
m_storageAreaMap.set(origin.release(), storageArea);
|
|
116 |
return storageArea.release();
|
|
117 |
}
|
|
118 |
|
|
119 |
void StorageNamespaceImpl::close()
|
|
120 |
{
|
|
121 |
ASSERT(isMainThread());
|
|
122 |
ASSERT(!m_isShutdown);
|
|
123 |
|
|
124 |
// If we're session storage, we shouldn't need to do any work here.
|
|
125 |
if (m_storageType == SessionStorage) {
|
|
126 |
ASSERT(!m_syncManager);
|
|
127 |
return;
|
|
128 |
}
|
|
129 |
|
|
130 |
StorageAreaMap::iterator end = m_storageAreaMap.end();
|
|
131 |
for (StorageAreaMap::iterator it = m_storageAreaMap.begin(); it != end; ++it)
|
|
132 |
it->second->close();
|
|
133 |
|
|
134 |
if (m_syncManager)
|
|
135 |
m_syncManager->close();
|
|
136 |
|
|
137 |
m_isShutdown = true;
|
|
138 |
}
|
|
139 |
|
|
140 |
void StorageNamespaceImpl::unlock()
|
|
141 |
{
|
|
142 |
// Because there's a single event loop per-process, this is a no-op.
|
|
143 |
}
|
|
144 |
|
|
145 |
} // namespace WebCore
|
|
146 |
|
|
147 |
#endif // ENABLE(DOM_STORAGE)
|