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 "StorageMap.h"
|
|
28 |
|
|
29 |
#if ENABLE(DOM_STORAGE)
|
|
30 |
|
|
31 |
namespace WebCore {
|
|
32 |
|
|
33 |
PassRefPtr<StorageMap> StorageMap::create(unsigned quota)
|
|
34 |
{
|
|
35 |
return adoptRef(new StorageMap(quota));
|
|
36 |
}
|
|
37 |
|
|
38 |
StorageMap::StorageMap(unsigned quota)
|
|
39 |
: m_iterator(m_map.end())
|
|
40 |
, m_iteratorIndex(UINT_MAX)
|
|
41 |
, m_quotaSize(quota) // quota measured in bytes
|
|
42 |
, m_currentLength(0)
|
|
43 |
{
|
|
44 |
}
|
|
45 |
|
|
46 |
PassRefPtr<StorageMap> StorageMap::copy()
|
|
47 |
{
|
|
48 |
RefPtr<StorageMap> newMap = create(m_quotaSize);
|
|
49 |
newMap->m_map = m_map;
|
|
50 |
return newMap.release();
|
|
51 |
}
|
|
52 |
|
|
53 |
void StorageMap::invalidateIterator()
|
|
54 |
{
|
|
55 |
m_iterator = m_map.end();
|
|
56 |
m_iteratorIndex = UINT_MAX;
|
|
57 |
}
|
|
58 |
|
|
59 |
void StorageMap::setIteratorToIndex(unsigned index)
|
|
60 |
{
|
|
61 |
// FIXME: Once we have bidirectional iterators for HashMap we can be more intelligent about this.
|
|
62 |
// The requested index will be closest to begin(), our current iterator, or end(), and we
|
|
63 |
// can take the shortest route.
|
|
64 |
// Until that mechanism is available, we'll always increment our iterator from begin() or current.
|
|
65 |
|
|
66 |
if (m_iteratorIndex == index)
|
|
67 |
return;
|
|
68 |
|
|
69 |
if (index < m_iteratorIndex) {
|
|
70 |
m_iteratorIndex = 0;
|
|
71 |
m_iterator = m_map.begin();
|
|
72 |
ASSERT(m_iterator != m_map.end());
|
|
73 |
}
|
|
74 |
|
|
75 |
while (m_iteratorIndex < index) {
|
|
76 |
++m_iteratorIndex;
|
|
77 |
++m_iterator;
|
|
78 |
ASSERT(m_iterator != m_map.end());
|
|
79 |
}
|
|
80 |
}
|
|
81 |
|
|
82 |
unsigned StorageMap::length() const
|
|
83 |
{
|
|
84 |
return m_map.size();
|
|
85 |
}
|
|
86 |
|
|
87 |
String StorageMap::key(unsigned index)
|
|
88 |
{
|
|
89 |
if (index >= length())
|
|
90 |
return String();
|
|
91 |
|
|
92 |
setIteratorToIndex(index);
|
|
93 |
return m_iterator->first;
|
|
94 |
}
|
|
95 |
|
|
96 |
String StorageMap::getItem(const String& key) const
|
|
97 |
{
|
|
98 |
return m_map.get(key);
|
|
99 |
}
|
|
100 |
|
|
101 |
PassRefPtr<StorageMap> StorageMap::setItem(const String& key, const String& value, String& oldValue, bool& quotaException)
|
|
102 |
{
|
|
103 |
ASSERT(!value.isNull());
|
|
104 |
quotaException = false;
|
|
105 |
|
|
106 |
// Implement copy-on-write semantics here. We're guaranteed that the only refs of StorageMaps belong to Storage objects
|
|
107 |
// so if more than one Storage object refs this map, copy it before mutating it.
|
|
108 |
if (refCount() > 1) {
|
|
109 |
RefPtr<StorageMap> newStorageMap = copy();
|
|
110 |
newStorageMap->setItem(key, value, oldValue, quotaException);
|
|
111 |
return newStorageMap.release();
|
|
112 |
}
|
|
113 |
|
|
114 |
// Quota tracking. If the quota is enabled and this would go over it, bail.
|
|
115 |
oldValue = m_map.get(key);
|
|
116 |
unsigned newLength = m_currentLength + value.length() - oldValue.length();
|
|
117 |
bool overQuota = newLength > m_quotaSize / sizeof(UChar);
|
|
118 |
bool overflow = (newLength > m_currentLength) != (value.length() > oldValue.length());
|
|
119 |
ASSERT(!overflow); // If we're debugging, make a fuss. But it's still worth checking this in the following if statement.
|
|
120 |
if (m_quotaSize != noQuota && (overflow || overQuota)) {
|
|
121 |
quotaException = true;
|
|
122 |
return 0;
|
|
123 |
}
|
|
124 |
m_currentLength = newLength;
|
|
125 |
|
|
126 |
pair<HashMap<String, String>::iterator, bool> addResult = m_map.add(key, value);
|
|
127 |
if (!addResult.second)
|
|
128 |
addResult.first->second = value;
|
|
129 |
|
|
130 |
invalidateIterator();
|
|
131 |
|
|
132 |
return 0;
|
|
133 |
}
|
|
134 |
|
|
135 |
PassRefPtr<StorageMap> StorageMap::removeItem(const String& key, String& oldValue)
|
|
136 |
{
|
|
137 |
// Implement copy-on-write semantics here. We're guaranteed that the only refs of StorageMaps belong to Storage objects
|
|
138 |
// so if more than one Storage object refs this map, copy it before mutating it.
|
|
139 |
if (refCount() > 1) {
|
|
140 |
RefPtr<StorageMap> newStorage = copy();
|
|
141 |
newStorage->removeItem(key, oldValue);
|
|
142 |
return newStorage.release();
|
|
143 |
}
|
|
144 |
|
|
145 |
oldValue = m_map.take(key);
|
|
146 |
if (!oldValue.isNull())
|
|
147 |
invalidateIterator();
|
|
148 |
|
|
149 |
// Update quota.
|
|
150 |
ASSERT(m_currentLength - oldValue.length() <= m_currentLength);
|
|
151 |
m_currentLength -= oldValue.length();
|
|
152 |
|
|
153 |
return 0;
|
|
154 |
}
|
|
155 |
|
|
156 |
bool StorageMap::contains(const String& key) const
|
|
157 |
{
|
|
158 |
return m_map.contains(key);
|
|
159 |
}
|
|
160 |
|
|
161 |
void StorageMap::importItem(const String& key, const String& value)
|
|
162 |
{
|
|
163 |
// Be sure to copy the keys/values as items imported on a background thread are destined
|
|
164 |
// to cross a thread boundary
|
|
165 |
pair<HashMap<String, String>::iterator, bool> result = m_map.add(key.threadsafeCopy(), String());
|
|
166 |
|
|
167 |
if (result.second)
|
|
168 |
result.first->second = value.threadsafeCopy();
|
|
169 |
|
|
170 |
// Update quota.
|
|
171 |
ASSERT(m_currentLength + value.length() >= m_currentLength);
|
|
172 |
m_currentLength += value.length();
|
|
173 |
}
|
|
174 |
|
|
175 |
}
|
|
176 |
|
|
177 |
#endif // ENABLE(DOM_STORAGE)
|