| 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 | #include <wtf/SetForScope.h> |
| 30 | |
| 31 | namespace WebCore { |
| 32 | |
| 33 | Ref<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 | Ref<StorageMap> StorageMap::copy() |
| 47 | { |
| 48 | Ref<StorageMap> newMap = create(m_quotaSize); |
| 49 | newMap->m_map = m_map; |
| 50 | newMap->m_currentLength = m_currentLength; |
| 51 | return newMap; |
| 52 | } |
| 53 | |
| 54 | void StorageMap::invalidateIterator() |
| 55 | { |
| 56 | m_iterator = m_map.end(); |
| 57 | m_iteratorIndex = UINT_MAX; |
| 58 | } |
| 59 | |
| 60 | void StorageMap::setIteratorToIndex(unsigned index) |
| 61 | { |
| 62 | // FIXME: Once we have bidirectional iterators for HashMap we can be more intelligent about this. |
| 63 | // The requested index will be closest to begin(), our current iterator, or end(), and we |
| 64 | // can take the shortest route. |
| 65 | // Until that mechanism is available, we'll always increment our iterator from begin() or current. |
| 66 | |
| 67 | if (m_iteratorIndex == index) |
| 68 | return; |
| 69 | |
| 70 | if (index < m_iteratorIndex) { |
| 71 | m_iteratorIndex = 0; |
| 72 | m_iterator = m_map.begin(); |
| 73 | ASSERT(m_iterator != m_map.end()); |
| 74 | } |
| 75 | |
| 76 | while (m_iteratorIndex < index) { |
| 77 | ++m_iteratorIndex; |
| 78 | ++m_iterator; |
| 79 | ASSERT(m_iterator != m_map.end()); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | unsigned StorageMap::length() const |
| 84 | { |
| 85 | return m_map.size(); |
| 86 | } |
| 87 | |
| 88 | String StorageMap::key(unsigned index) |
| 89 | { |
| 90 | if (index >= length()) |
| 91 | return String(); |
| 92 | |
| 93 | setIteratorToIndex(index); |
| 94 | return m_iterator->key; |
| 95 | } |
| 96 | |
| 97 | String StorageMap::getItem(const String& key) const |
| 98 | { |
| 99 | return m_map.get(key); |
| 100 | } |
| 101 | |
| 102 | RefPtr<StorageMap> StorageMap::setItem(const String& key, const String& value, String& oldValue, bool& quotaException) |
| 103 | { |
| 104 | ASSERT(!value.isNull()); |
| 105 | quotaException = false; |
| 106 | |
| 107 | // Implement copy-on-write semantics here. We're guaranteed that the only refs of StorageMaps belong to Storage objects |
| 108 | // so if more than one Storage object refs this map, copy it before mutating it. |
| 109 | if (refCount() > 1) { |
| 110 | RefPtr<StorageMap> newStorageMap = copy(); |
| 111 | newStorageMap->setItem(key, value, oldValue, quotaException); |
| 112 | return newStorageMap; |
| 113 | } |
| 114 | |
| 115 | // Quota tracking. This is done in a couple of steps to keep the overflow tracking simple. |
| 116 | unsigned newLength = m_currentLength; |
| 117 | bool overflow = newLength + value.length() < newLength; |
| 118 | newLength += value.length(); |
| 119 | |
| 120 | oldValue = m_map.get(key); |
| 121 | overflow |= newLength - oldValue.length() > newLength; |
| 122 | newLength -= oldValue.length(); |
| 123 | |
| 124 | unsigned adjustedKeyLength = oldValue.isNull() ? key.length() : 0; |
| 125 | overflow |= newLength + adjustedKeyLength < newLength; |
| 126 | newLength += adjustedKeyLength; |
| 127 | |
| 128 | ASSERT(!overflow); // Overflow is bad...even if quotas are off. |
| 129 | bool overQuota = newLength > m_quotaSize / sizeof(UChar); |
| 130 | if (m_quotaSize != noQuota && (overflow || overQuota)) { |
| 131 | quotaException = true; |
| 132 | return nullptr; |
| 133 | } |
| 134 | m_currentLength = newLength; |
| 135 | |
| 136 | HashMap<String, String>::AddResult addResult = m_map.add(key, value); |
| 137 | if (!addResult.isNewEntry) |
| 138 | addResult.iterator->value = value; |
| 139 | |
| 140 | invalidateIterator(); |
| 141 | |
| 142 | return nullptr; |
| 143 | } |
| 144 | |
| 145 | RefPtr<StorageMap> StorageMap::setItemIgnoringQuota(const String& key, const String& value) |
| 146 | { |
| 147 | SetForScope<unsigned> quotaSizeChange(m_quotaSize, static_cast<unsigned>(noQuota)); |
| 148 | |
| 149 | String oldValue; |
| 150 | bool quotaException; |
| 151 | |
| 152 | RefPtr<StorageMap> map = setItem(key, value, oldValue, quotaException); |
| 153 | ASSERT(!quotaException); |
| 154 | |
| 155 | return map; |
| 156 | } |
| 157 | |
| 158 | RefPtr<StorageMap> StorageMap::removeItem(const String& key, String& oldValue) |
| 159 | { |
| 160 | // Implement copy-on-write semantics here. We're guaranteed that the only refs of StorageMaps belong to Storage objects |
| 161 | // so if more than one Storage object refs this map, copy it before mutating it. |
| 162 | if (refCount() > 1) { |
| 163 | RefPtr<StorageMap> newStorage = copy(); |
| 164 | newStorage->removeItem(key, oldValue); |
| 165 | return newStorage; |
| 166 | } |
| 167 | |
| 168 | oldValue = m_map.take(key); |
| 169 | if (!oldValue.isNull()) { |
| 170 | invalidateIterator(); |
| 171 | ASSERT(m_currentLength - key.length() <= m_currentLength); |
| 172 | m_currentLength -= key.length(); |
| 173 | } |
| 174 | ASSERT(m_currentLength - oldValue.length() <= m_currentLength); |
| 175 | m_currentLength -= oldValue.length(); |
| 176 | |
| 177 | return nullptr; |
| 178 | } |
| 179 | |
| 180 | bool StorageMap::contains(const String& key) const |
| 181 | { |
| 182 | return m_map.contains(key); |
| 183 | } |
| 184 | |
| 185 | void StorageMap::importItems(const HashMap<String, String>& items) |
| 186 | { |
| 187 | for (auto& item : items) { |
| 188 | const String& key = item.key; |
| 189 | const String& value = item.value; |
| 190 | |
| 191 | HashMap<String, String>::AddResult result = m_map.add(key, value); |
| 192 | ASSERT_UNUSED(result, result.isNewEntry); // True if the key didn't exist previously. |
| 193 | |
| 194 | ASSERT(m_currentLength + key.length() >= m_currentLength); |
| 195 | m_currentLength += key.length(); |
| 196 | ASSERT(m_currentLength + value.length() >= m_currentLength); |
| 197 | m_currentLength += value.length(); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | } |
| 202 | |