| 1 | /* |
| 2 | * Copyright (C) 2015 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. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "IDBDatabaseInfo.h" |
| 28 | |
| 29 | #include <wtf/text/StringBuilder.h> |
| 30 | |
| 31 | #if ENABLE(INDEXED_DATABASE) |
| 32 | |
| 33 | namespace WebCore { |
| 34 | |
| 35 | IDBDatabaseInfo::IDBDatabaseInfo() |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | IDBDatabaseInfo::IDBDatabaseInfo(const String& name, uint64_t version) |
| 40 | : m_name(name) |
| 41 | , m_version(version) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | IDBDatabaseInfo::IDBDatabaseInfo(const IDBDatabaseInfo& other, IsolatedCopyTag) |
| 46 | : m_name(other.m_name.isolatedCopy()) |
| 47 | , m_version(other.m_version) |
| 48 | , m_maxObjectStoreID(other.m_maxObjectStoreID) |
| 49 | { |
| 50 | for (const auto& entry : other.m_objectStoreMap) |
| 51 | m_objectStoreMap.set(entry.key, entry.value.isolatedCopy()); |
| 52 | } |
| 53 | |
| 54 | IDBDatabaseInfo IDBDatabaseInfo::isolatedCopy() const |
| 55 | { |
| 56 | return { *this, IDBDatabaseInfo::IsolatedCopy }; |
| 57 | } |
| 58 | |
| 59 | bool IDBDatabaseInfo::hasObjectStore(const String& name) const |
| 60 | { |
| 61 | for (auto& objectStore : m_objectStoreMap.values()) { |
| 62 | if (objectStore.name() == name) |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | IDBObjectStoreInfo IDBDatabaseInfo::createNewObjectStore(const String& name, Optional<IDBKeyPath>&& keyPath, bool autoIncrement) |
| 70 | { |
| 71 | IDBObjectStoreInfo info(++m_maxObjectStoreID, name, WTFMove(keyPath), autoIncrement); |
| 72 | m_objectStoreMap.set(info.identifier(), info); |
| 73 | return info; |
| 74 | } |
| 75 | |
| 76 | void IDBDatabaseInfo::addExistingObjectStore(const IDBObjectStoreInfo& info) |
| 77 | { |
| 78 | ASSERT(!m_objectStoreMap.contains(info.identifier())); |
| 79 | |
| 80 | if (info.identifier() > m_maxObjectStoreID) |
| 81 | m_maxObjectStoreID = info.identifier(); |
| 82 | |
| 83 | m_objectStoreMap.set(info.identifier(), info); |
| 84 | } |
| 85 | |
| 86 | IDBObjectStoreInfo* IDBDatabaseInfo::getInfoForExistingObjectStore(uint64_t objectStoreIdentifier) |
| 87 | { |
| 88 | auto iterator = m_objectStoreMap.find(objectStoreIdentifier); |
| 89 | if (iterator == m_objectStoreMap.end()) |
| 90 | return nullptr; |
| 91 | |
| 92 | return &iterator->value; |
| 93 | } |
| 94 | |
| 95 | IDBObjectStoreInfo* IDBDatabaseInfo::getInfoForExistingObjectStore(const String& name) |
| 96 | { |
| 97 | for (auto& objectStore : m_objectStoreMap.values()) { |
| 98 | if (objectStore.name() == name) |
| 99 | return &objectStore; |
| 100 | } |
| 101 | |
| 102 | return nullptr; |
| 103 | } |
| 104 | |
| 105 | const IDBObjectStoreInfo* IDBDatabaseInfo::infoForExistingObjectStore(uint64_t objectStoreIdentifier) const |
| 106 | { |
| 107 | return const_cast<IDBDatabaseInfo*>(this)->getInfoForExistingObjectStore(objectStoreIdentifier); |
| 108 | } |
| 109 | |
| 110 | IDBObjectStoreInfo* IDBDatabaseInfo::infoForExistingObjectStore(uint64_t objectStoreIdentifier) |
| 111 | { |
| 112 | return getInfoForExistingObjectStore(objectStoreIdentifier); |
| 113 | } |
| 114 | |
| 115 | const IDBObjectStoreInfo* IDBDatabaseInfo::infoForExistingObjectStore(const String& name) const |
| 116 | { |
| 117 | return const_cast<IDBDatabaseInfo*>(this)->getInfoForExistingObjectStore(name); |
| 118 | } |
| 119 | |
| 120 | IDBObjectStoreInfo* IDBDatabaseInfo::infoForExistingObjectStore(const String& name) |
| 121 | { |
| 122 | return getInfoForExistingObjectStore(name); |
| 123 | } |
| 124 | |
| 125 | void IDBDatabaseInfo::renameObjectStore(uint64_t objectStoreIdentifier, const String& newName) |
| 126 | { |
| 127 | auto* info = infoForExistingObjectStore(objectStoreIdentifier); |
| 128 | if (!info) |
| 129 | return; |
| 130 | |
| 131 | info->rename(newName); |
| 132 | } |
| 133 | |
| 134 | Vector<String> IDBDatabaseInfo::objectStoreNames() const |
| 135 | { |
| 136 | Vector<String> names; |
| 137 | names.reserveCapacity(m_objectStoreMap.size()); |
| 138 | for (auto& objectStore : m_objectStoreMap.values()) |
| 139 | names.uncheckedAppend(objectStore.name()); |
| 140 | |
| 141 | return names; |
| 142 | } |
| 143 | |
| 144 | void IDBDatabaseInfo::deleteObjectStore(const String& objectStoreName) |
| 145 | { |
| 146 | auto* info = infoForExistingObjectStore(objectStoreName); |
| 147 | if (!info) |
| 148 | return; |
| 149 | |
| 150 | m_objectStoreMap.remove(info->identifier()); |
| 151 | } |
| 152 | |
| 153 | void IDBDatabaseInfo::deleteObjectStore(uint64_t objectStoreIdentifier) |
| 154 | { |
| 155 | m_objectStoreMap.remove(objectStoreIdentifier); |
| 156 | } |
| 157 | |
| 158 | #if !LOG_DISABLED |
| 159 | String IDBDatabaseInfo::loggingString() const |
| 160 | { |
| 161 | StringBuilder builder; |
| 162 | builder.appendLiteral("Database:" ); |
| 163 | builder.append(m_name); |
| 164 | builder.appendLiteral(" version " ); |
| 165 | builder.appendNumber(m_version); |
| 166 | builder.append('\n'); |
| 167 | for (const auto& objectStore : m_objectStoreMap.values()) { |
| 168 | builder.append(objectStore.loggingString(1)); |
| 169 | builder.append('\n'); |
| 170 | } |
| 171 | |
| 172 | return builder.toString(); |
| 173 | } |
| 174 | #endif |
| 175 | |
| 176 | } // namespace WebCore |
| 177 | |
| 178 | #endif // ENABLE(INDEXED_DATABASE) |
| 179 | |