| File: | Volumes/Data/worker/macOS-Safer-CPP-Checks-EWS/build/Source/WebCore/Modules/indexeddb/IDBIndex.cpp |
| Warning: | line 422, column 15 Local variable 'context' is uncounted and unsafe |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 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 "IDBIndex.h" |
| 28 | |
| 29 | #include "ContextDestructionObserverInlines.h" |
| 30 | #include "ExceptionOr.h" |
| 31 | #include "IDBBindingUtilities.h" |
| 32 | #include "IDBCursor.h" |
| 33 | #include "IDBCursorDirection.h" |
| 34 | #include "IDBDatabase.h" |
| 35 | #include "IDBGetAllOptions.h" |
| 36 | #include "IDBKeyRangeData.h" |
| 37 | #include "IDBObjectStore.h" |
| 38 | #include "IDBRequest.h" |
| 39 | #include "IDBTransaction.h" |
| 40 | #include "JSIDBGetAllOptions.h" |
| 41 | #include "JSIDBKeyRange.h" |
| 42 | #include "Logging.h" |
| 43 | #include "Settings.h" |
| 44 | #include "WebCoreOpaqueRoot.h" |
| 45 | #include <JavaScriptCore/HeapInlines.h> |
| 46 | #include <wtf/TZoneMallocInlines.h> |
| 47 | #include <wtf/text/MakeString.h> |
| 48 | |
| 49 | namespace WebCore { |
| 50 | using namespace JSC; |
| 51 | |
| 52 | WTF_MAKE_TZONE_ALLOCATED_IMPL(IDBIndex)::bmalloc::api::HeapRef IDBIndex::s_heapRef; const TZoneSpecification IDBIndex::s_heapSpec = { &IDBIndex::s_heapRef, TZoneSpecification ::encodeSize<IDBIndex>(), TZoneSpecification::encodeAlignment <IDBIndex>(), TZoneSpecification::encodeCategory<IDBIndex >(), ::bmalloc::api::compactAllocationMode<IDBIndex> (), TZoneSpecification::encodeDescriptor<IDBIndex>(), } ; void* IDBIndex::operatorNewSlow(size_t size) { if constexpr (::bmalloc::api::compactAllocationMode<IDBIndex>() == CompactAllocationMode ::Compact) return ::bmalloc::api::tzoneAllocateCompactSlow(size , s_heapSpec); return ::bmalloc::api::tzoneAllocateNonCompactSlow (size, s_heapSpec); } using __makeBtzoneMallocedInlineMacroSemicolonifier __attribute__((unused)) = int; |
| 53 | |
| 54 | UniqueRef<IDBIndex> IDBIndex::create(ScriptExecutionContext& context, const IDBIndexInfo& info, IDBObjectStore& objectStore) |
| 55 | { |
| 56 | auto result = UniqueRef(*new IDBIndex(context, info, objectStore)); |
| 57 | result->suspendIfNeeded(); |
| 58 | return result; |
| 59 | } |
| 60 | |
| 61 | IDBIndex::IDBIndex(ScriptExecutionContext& context, const IDBIndexInfo& info, IDBObjectStore& objectStore) |
| 62 | : ActiveDOMObject(&context) |
| 63 | , m_info(info) |
| 64 | , m_originalInfo(info) |
| 65 | , m_objectStore(objectStore) |
| 66 | { |
| 67 | ASSERT(canCurrentThreadAccessThreadLocalData(m_objectStore->transaction().database().originThread()))((void)0); |
| 68 | } |
| 69 | |
| 70 | IDBIndex::~IDBIndex() |
| 71 | { |
| 72 | ASSERT(canCurrentThreadAccessThreadLocalData(m_objectStore->transaction().database().originThread()))((void)0); |
| 73 | } |
| 74 | |
| 75 | bool IDBIndex::virtualHasPendingActivity() const |
| 76 | { |
| 77 | return m_objectStore->hasPendingActivity(); |
| 78 | } |
| 79 | |
| 80 | const String& IDBIndex::name() const |
| 81 | { |
| 82 | ASSERT(canCurrentThreadAccessThreadLocalData(m_objectStore->transaction().database().originThread()))((void)0); |
| 83 | return m_info.name(); |
| 84 | } |
| 85 | |
| 86 | ExceptionOr<void> IDBIndex::setName(const String& name) |
| 87 | { |
| 88 | Ref transaction = m_objectStore->transaction(); |
| 89 | ASSERT(canCurrentThreadAccessThreadLocalData(transaction->database().originThread()))((void)0); |
| 90 | |
| 91 | if (m_deleted) |
| 92 | return Exception { ExceptionCode::InvalidStateError, "Failed set property 'name' on 'IDBIndex': The index has been deleted."_s }; |
| 93 | |
| 94 | if (m_objectStore->isDeleted()) |
| 95 | return Exception { ExceptionCode::InvalidStateError, "Failed set property 'name' on 'IDBIndex': The index's object store has been deleted."_s }; |
| 96 | |
| 97 | if (!transaction->isVersionChange()) |
| 98 | return Exception { ExceptionCode::InvalidStateError, "Failed set property 'name' on 'IDBIndex': The index's transaction is not a version change transaction."_s }; |
| 99 | |
| 100 | if (!transaction->isActive()) |
| 101 | return Exception { ExceptionCode::TransactionInactiveError, "Failed set property 'name' on 'IDBIndex': The index's transaction is not active."_s }; |
| 102 | |
| 103 | if (m_info.name() == name) |
| 104 | return { }; |
| 105 | |
| 106 | if (m_objectStore->info().hasIndex(name)) |
| 107 | return Exception { ExceptionCode::ConstraintError, makeString("Failed set property 'name' on 'IDBIndex': The owning object store already has an index named '"_s, name, "'."_s) }; |
| 108 | |
| 109 | transaction->database().renameIndex(*this, name); |
| 110 | m_info.rename(name); |
| 111 | |
| 112 | return { }; |
| 113 | } |
| 114 | |
| 115 | IDBObjectStore& IDBIndex::objectStore() |
| 116 | { |
| 117 | ASSERT(canCurrentThreadAccessThreadLocalData(m_objectStore->transaction().database().originThread()))((void)0); |
| 118 | return m_objectStore; |
| 119 | } |
| 120 | |
| 121 | const IDBKeyPath& IDBIndex::keyPath() const |
| 122 | { |
| 123 | ASSERT(canCurrentThreadAccessThreadLocalData(m_objectStore->transaction().database().originThread()))((void)0); |
| 124 | return m_info.keyPath(); |
| 125 | } |
| 126 | |
| 127 | bool IDBIndex::unique() const |
| 128 | { |
| 129 | ASSERT(canCurrentThreadAccessThreadLocalData(m_objectStore->transaction().database().originThread()))((void)0); |
| 130 | return m_info.unique(); |
| 131 | } |
| 132 | |
| 133 | bool IDBIndex::multiEntry() const |
| 134 | { |
| 135 | ASSERT(canCurrentThreadAccessThreadLocalData(m_objectStore->transaction().database().originThread()))((void)0); |
| 136 | return m_info.multiEntry(); |
| 137 | } |
| 138 | |
| 139 | void IDBIndex::rollbackInfoForVersionChangeAbort() |
| 140 | { |
| 141 | ASSERT(canCurrentThreadAccessThreadLocalData(m_objectStore->transaction().database().originThread()))((void)0); |
| 142 | |
| 143 | // Only rollback to the original info if this index still exists in the rolled-back database info. |
| 144 | auto* objectStoreInfo = m_objectStore->transaction().database().info().infoForExistingObjectStore(m_objectStore->info().identifier()); |
| 145 | if (!objectStoreInfo) |
| 146 | return; |
| 147 | |
| 148 | if (!objectStoreInfo->hasIndex(m_info.identifier())) { |
| 149 | m_deleted = true; |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | m_info = m_originalInfo; |
| 154 | m_deleted = false; |
| 155 | } |
| 156 | |
| 157 | ExceptionOr<Ref<IDBRequest>> IDBIndex::doOpenCursor(IDBCursorDirection direction, Function<ExceptionOr<RefPtr<IDBKeyRange>>()>&& function) |
| 158 | { |
| 159 | LOG(IndexedDB, "IDBIndex::openCursor")((void)0); |
| 160 | Ref transaction = m_objectStore->transaction(); |
| 161 | ASSERT(canCurrentThreadAccessThreadLocalData(transaction->database().originThread()))((void)0); |
| 162 | |
| 163 | if (m_deleted || m_objectStore->isDeleted()) |
| 164 | return Exception { ExceptionCode::InvalidStateError, "Failed to execute 'openCursor' on 'IDBIndex': The index or its object store has been deleted."_s }; |
| 165 | |
| 166 | if (!transaction->isActive()) |
| 167 | return Exception { ExceptionCode::TransactionInactiveError, "Failed to execute 'openCursor' on 'IDBIndex': The transaction is inactive or finished."_s }; |
| 168 | |
| 169 | auto keyRange = function(); |
| 170 | if (keyRange.hasException()) |
| 171 | return keyRange.releaseException(); |
| 172 | |
| 173 | IDBKeyRangeData rangeData = keyRange.returnValue().get(); |
| 174 | if (rangeData.lowerKey.isNull()) |
| 175 | rangeData.lowerKey = IDBKeyData::minimum(); |
| 176 | if (rangeData.upperKey.isNull()) |
| 177 | rangeData.upperKey = IDBKeyData::maximum(); |
| 178 | |
| 179 | auto info = IDBCursorInfo::indexCursor(transaction, m_objectStore->info().identifier(), m_info.identifier(), rangeData, direction, IndexedDB::CursorType::KeyAndValue); |
| 180 | return transaction->requestOpenCursor(*this, info); |
| 181 | } |
| 182 | |
| 183 | ExceptionOr<Ref<IDBRequest>> IDBIndex::openCursor(RefPtr<IDBKeyRange>&& range, IDBCursorDirection direction) |
| 184 | { |
| 185 | return doOpenCursor(direction, [range = WTF::move(range)]() { |
| 186 | return range; |
| 187 | }); |
| 188 | } |
| 189 | |
| 190 | ExceptionOr<Ref<IDBRequest>> IDBIndex::openCursor(JSGlobalObject& execState, JSValue key, IDBCursorDirection direction) |
| 191 | { |
| 192 | return doOpenCursor(direction, [state = &execState, key]() { |
| 193 | auto onlyResult = IDBKeyRange::only(*state, key); |
| 194 | if (onlyResult.hasException()) |
| 195 | return ExceptionOr<RefPtr<IDBKeyRange>> { Exception(ExceptionCode::DataError, "Failed to execute 'openCursor' on 'IDBIndex': The parameter is not a valid key."_s) }; |
| 196 | |
| 197 | return ExceptionOr<RefPtr<IDBKeyRange>> { onlyResult.releaseReturnValue() }; |
| 198 | }); |
| 199 | } |
| 200 | |
| 201 | ExceptionOr<Ref<IDBRequest>> IDBIndex::doOpenKeyCursor(IDBCursorDirection direction, Function<ExceptionOr<RefPtr<IDBKeyRange>>()>&& function) |
| 202 | { |
| 203 | LOG(IndexedDB, "IDBIndex::openKeyCursor")((void)0); |
| 204 | Ref transaction = m_objectStore->transaction(); |
| 205 | ASSERT(canCurrentThreadAccessThreadLocalData(transaction->database().originThread()))((void)0); |
| 206 | |
| 207 | if (m_deleted || m_objectStore->isDeleted()) |
| 208 | return Exception { ExceptionCode::InvalidStateError, "Failed to execute 'openKeyCursor' on 'IDBIndex': The index or its object store has been deleted."_s }; |
| 209 | |
| 210 | if (!transaction->isActive()) |
| 211 | return Exception { ExceptionCode::TransactionInactiveError, "Failed to execute 'openKeyCursor' on 'IDBIndex': The transaction is inactive or finished."_s }; |
| 212 | |
| 213 | auto keyRange = function(); |
| 214 | if (keyRange.hasException()) |
| 215 | return keyRange.releaseException(); |
| 216 | |
| 217 | RefPtr keyRangePointer = keyRange.returnValue().get(); |
| 218 | auto info = IDBCursorInfo::indexCursor(transaction, m_objectStore->info().identifier(), m_info.identifier(), keyRangePointer.get(), direction, IndexedDB::CursorType::KeyOnly); |
| 219 | return transaction->requestOpenCursor(*this, info); |
| 220 | } |
| 221 | |
| 222 | ExceptionOr<Ref<IDBRequest>> IDBIndex::openKeyCursor(RefPtr<IDBKeyRange>&& range, IDBCursorDirection direction) |
| 223 | { |
| 224 | return doOpenKeyCursor(direction, [range = WTF::move(range)]() { |
| 225 | return range; |
| 226 | }); |
| 227 | } |
| 228 | |
| 229 | ExceptionOr<Ref<IDBRequest>> IDBIndex::openKeyCursor(JSGlobalObject& execState, JSValue key, IDBCursorDirection direction) |
| 230 | { |
| 231 | return doOpenKeyCursor(direction, [state = &execState, key]() { |
| 232 | auto onlyResult = IDBKeyRange::only(*state, key); |
| 233 | if (onlyResult.hasException()) |
| 234 | return ExceptionOr<RefPtr<IDBKeyRange>> { Exception(ExceptionCode::DataError, "Failed to execute 'openKeyCursor' on 'IDBIndex': The parameter is not a valid key."_s) }; |
| 235 | |
| 236 | return ExceptionOr<RefPtr<IDBKeyRange>> { onlyResult.releaseReturnValue() }; |
| 237 | }); |
| 238 | } |
| 239 | |
| 240 | ExceptionOr<Ref<IDBRequest>> IDBIndex::count(IDBKeyRange* range) |
| 241 | { |
| 242 | LOG(IndexedDB, "IDBIndex::count")((void)0); |
| 243 | |
| 244 | return doCount(range ? IDBKeyRangeData(range) : IDBKeyRangeData::allKeys()); |
| 245 | } |
| 246 | |
| 247 | ExceptionOr<Ref<IDBRequest>> IDBIndex::count(JSGlobalObject& execState, JSValue key) |
| 248 | { |
| 249 | LOG(IndexedDB, "IDBIndex::count")((void)0); |
| 250 | |
| 251 | auto idbKey = scriptValueToIDBKey(execState, key); |
| 252 | RefPtr idbKeyPointer = idbKey->isValid() ? idbKey.ptr() : nullptr; |
| 253 | |
| 254 | return doCount(IDBKeyRangeData(idbKeyPointer.get())); |
| 255 | } |
| 256 | |
| 257 | ExceptionOr<Ref<IDBRequest>> IDBIndex::doCount(const IDBKeyRangeData& range) |
| 258 | { |
| 259 | ASSERT(canCurrentThreadAccessThreadLocalData(m_objectStore->transaction().database().originThread()))((void)0); |
| 260 | |
| 261 | if (m_deleted || m_objectStore->isDeleted()) |
| 262 | return Exception { ExceptionCode::InvalidStateError, "Failed to execute 'count' on 'IDBIndex': The index or its object store has been deleted."_s }; |
| 263 | |
| 264 | Ref transaction = m_objectStore->transaction(); |
| 265 | if (!transaction->isActive()) |
| 266 | return Exception { ExceptionCode::TransactionInactiveError, "Failed to execute 'count' on 'IDBIndex': The transaction is inactive or finished."_s }; |
| 267 | |
| 268 | if (!range.isValid()) |
| 269 | return Exception { ExceptionCode::DataError, "Failed to execute 'count' on 'IDBIndex': The parameter is not a valid key."_s }; |
| 270 | |
| 271 | return transaction->requestCount(*this, range); |
| 272 | } |
| 273 | |
| 274 | ExceptionOr<Ref<IDBRequest>> IDBIndex::get(IDBKeyRange* range) |
| 275 | { |
| 276 | LOG(IndexedDB, "IDBIndex::get")((void)0); |
| 277 | |
| 278 | return doGet(IDBKeyRangeData(range)); |
| 279 | } |
| 280 | |
| 281 | ExceptionOr<Ref<IDBRequest>> IDBIndex::get(JSGlobalObject& execState, JSValue key) |
| 282 | { |
| 283 | LOG(IndexedDB, "IDBIndex::get")((void)0); |
| 284 | |
| 285 | auto idbKey = scriptValueToIDBKey(execState, key); |
| 286 | if (!idbKey->isValid()) |
| 287 | return doGet(Exception(ExceptionCode::DataError, "Failed to execute 'get' on 'IDBIndex': The parameter is not a valid key."_s)); |
| 288 | |
| 289 | return doGet(IDBKeyRangeData(idbKey.ptr())); |
| 290 | } |
| 291 | |
| 292 | ExceptionOr<Ref<IDBRequest>> IDBIndex::doGet(ExceptionOr<IDBKeyRangeData> range) |
| 293 | { |
| 294 | ASSERT(canCurrentThreadAccessThreadLocalData(m_objectStore->transaction().database().originThread()))((void)0); |
| 295 | |
| 296 | if (m_deleted || m_objectStore->isDeleted()) |
| 297 | return Exception { ExceptionCode::InvalidStateError, "Failed to execute 'get' on 'IDBIndex': The index or its object store has been deleted."_s }; |
| 298 | |
| 299 | Ref transaction = m_objectStore->transaction(); |
| 300 | if (!transaction->isActive()) |
| 301 | return Exception { ExceptionCode::TransactionInactiveError, "Failed to execute 'get' on 'IDBIndex': The transaction is inactive or finished."_s }; |
| 302 | |
| 303 | if (range.hasException()) |
| 304 | return range.releaseException(); |
| 305 | auto keyRange = range.releaseReturnValue(); |
| 306 | |
| 307 | if (keyRange.isNull()) |
| 308 | return Exception { ExceptionCode::DataError }; |
| 309 | |
| 310 | return transaction->requestGetValue(*this, keyRange); |
| 311 | } |
| 312 | |
| 313 | ExceptionOr<Ref<IDBRequest>> IDBIndex::getKey(IDBKeyRange* range) |
| 314 | { |
| 315 | LOG(IndexedDB, "IDBIndex::getKey")((void)0); |
| 316 | |
| 317 | return doGetKey(IDBKeyRangeData(range)); |
| 318 | } |
| 319 | |
| 320 | ExceptionOr<Ref<IDBRequest>> IDBIndex::getKey(JSGlobalObject& execState, JSValue key) |
| 321 | { |
| 322 | LOG(IndexedDB, "IDBIndex::getKey")((void)0); |
| 323 | |
| 324 | auto idbKey = scriptValueToIDBKey(execState, key); |
| 325 | if (!idbKey->isValid()) |
| 326 | return doGetKey(Exception(ExceptionCode::DataError, "Failed to execute 'getKey' on 'IDBIndex': The parameter is not a valid key."_s)); |
| 327 | |
| 328 | return doGetKey(IDBKeyRangeData(idbKey.ptr())); |
| 329 | } |
| 330 | |
| 331 | ExceptionOr<Ref<IDBRequest>> IDBIndex::doGetKey(ExceptionOr<IDBKeyRangeData> range) |
| 332 | { |
| 333 | ASSERT(canCurrentThreadAccessThreadLocalData(m_objectStore->transaction().database().originThread()))((void)0); |
| 334 | |
| 335 | if (m_deleted || m_objectStore->isDeleted()) |
| 336 | return Exception { ExceptionCode::InvalidStateError, "Failed to execute 'getKey' on 'IDBIndex': The index or its object store has been deleted."_s }; |
| 337 | |
| 338 | Ref transaction = m_objectStore->transaction(); |
| 339 | if (!transaction->isActive()) |
| 340 | return Exception { ExceptionCode::TransactionInactiveError, "Failed to execute 'getKey' on 'IDBIndex': The transaction is inactive or finished."_s }; |
| 341 | |
| 342 | if (range.hasException()) |
| 343 | return range.releaseException(); |
| 344 | auto keyRange = range.releaseReturnValue(); |
| 345 | |
| 346 | if (keyRange.isNull()) |
| 347 | return Exception { ExceptionCode::DataError }; |
| 348 | |
| 349 | return transaction->requestGetKey(*this, keyRange); |
| 350 | } |
| 351 | |
| 352 | ExceptionOr<Ref<IDBRequest>> IDBIndex::doGetAll(std::optional<uint32_t> count, Function<ExceptionOr<RefPtr<IDBKeyRange>>()>&& function) |
| 353 | { |
| 354 | LOG(IndexedDB, "IDBIndex::getAll")((void)0); |
| 355 | Ref transaction = m_objectStore->transaction(); |
| 356 | ASSERT(canCurrentThreadAccessThreadLocalData(transaction->database().originThread()))((void)0); |
| 357 | |
| 358 | if (m_deleted || m_objectStore->isDeleted()) |
| 359 | return Exception { ExceptionCode::InvalidStateError, "Failed to execute 'getAll' on 'IDBIndex': The index or its object store has been deleted."_s }; |
| 360 | |
| 361 | if (!transaction->isActive()) |
| 362 | return Exception { ExceptionCode::TransactionInactiveError, "Failed to execute 'getAll' on 'IDBIndex': The transaction is inactive or finished."_s }; |
| 363 | |
| 364 | auto keyRange = function(); |
| 365 | if (keyRange.hasException()) |
| 366 | return keyRange.releaseException(); |
| 367 | |
| 368 | RefPtr keyRangePointer = keyRange.returnValue().get(); |
| 369 | return transaction->requestGetAllIndexRecords(*this, keyRangePointer.get(), IndexedDB::GetAllType::Values, count, IDBCursorDirection::Next); |
| 370 | } |
| 371 | |
| 372 | ExceptionOr<Ref<IDBRequest>> IDBIndex::getAll(RefPtr<IDBKeyRange>&& range, std::optional<uint32_t> count) |
| 373 | { |
| 374 | return doGetAll(count, [range = WTF::move(range)]() { |
| 375 | return range; |
| 376 | }); |
| 377 | } |
| 378 | |
| 379 | ExceptionOr<Ref<IDBRequest>> IDBIndex::getAll(JSGlobalObject& execState, JSValue key, std::optional<uint32_t> count) |
| 380 | { |
| 381 | return doGetAll(count, [state = &execState, key]() { |
| 382 | auto onlyResult = IDBKeyRange::only(*state, key); |
| 383 | if (onlyResult.hasException()) |
| 384 | return ExceptionOr<RefPtr<IDBKeyRange>> { Exception(ExceptionCode::DataError, "Failed to execute 'getAll' on 'IDBIndex': The parameter is not a valid key."_s) }; |
| 385 | |
| 386 | return ExceptionOr<RefPtr<IDBKeyRange>> { onlyResult.releaseReturnValue() }; |
| 387 | }); |
| 388 | } |
| 389 | |
| 390 | ExceptionOr<Ref<IDBRequest>> IDBIndex::doGetAllKeys(std::optional<uint32_t> count, IDBCursorDirection cursorDirection, Function<ExceptionOr<RefPtr<IDBKeyRange>>()>&& function) |
| 391 | { |
| 392 | LOG(IndexedDB, "IDBIndex::getAllKeys")((void)0); |
| 393 | Ref transaction = m_objectStore->transaction(); |
| 394 | ASSERT(canCurrentThreadAccessThreadLocalData(transaction->database().originThread()))((void)0); |
| 395 | |
| 396 | if (m_deleted || m_objectStore->isDeleted()) |
| 397 | return Exception { ExceptionCode::InvalidStateError, "Failed to execute 'getAllKeys' on 'IDBIndex': The index or its object store has been deleted."_s }; |
| 398 | |
| 399 | if (!transaction->isActive()) |
| 400 | return Exception { ExceptionCode::TransactionInactiveError, "Failed to execute 'getAllKeys' on 'IDBIndex': The transaction is inactive or finished."_s }; |
| 401 | |
| 402 | auto keyRange = function(); |
| 403 | if (keyRange.hasException()) |
| 404 | return keyRange.releaseException(); |
| 405 | |
| 406 | RefPtr keyRangePointer = keyRange.returnValue().get(); |
| 407 | return transaction->requestGetAllIndexRecords(*this, keyRangePointer.get(), IndexedDB::GetAllType::Keys, count, cursorDirection); |
| 408 | } |
| 409 | |
| 410 | ExceptionOr<Ref<IDBRequest>> IDBIndex::getAllKeys(RefPtr<IDBKeyRange>&& range, std::optional<uint32_t> count) |
| 411 | { |
| 412 | return doGetAllKeys(count, IDBCursorDirection::Next, [range = WTF::move(range)]() { |
| 413 | return range; |
| 414 | }); |
| 415 | } |
| 416 | |
| 417 | ExceptionOr<Ref<IDBRequest>> IDBIndex::getAllKeys(JSGlobalObject& execState, JSValue key, std::optional<uint32_t> count) |
| 418 | { |
| 419 | // Spec §2.9: For objects, check "is a potentially valid key range" before |
| 420 | // attempting IDBGetAllOptions dictionary conversion. |
| 421 | if (key.isObject() && !key.isUndefinedOrNull()) { |
| 422 | auto* context = scriptExecutionContext(); |
Local variable 'context' is uncounted and unsafe | |
| 423 | if (context && context->settingsValues().indexedDBGetAllRecordsEnabled) { |
| 424 | auto& vm = execState.vm(); |
| 425 | auto throwScope = DECLARE_THROW_SCOPE(vm)JSC::ThrowScope((vm)); |
| 426 | auto idbKey = scriptValueToIDBKey(execState, key); |
| 427 | |
| 428 | // If key conversion threw (e.g., throwing getter), let it propagate. |
| 429 | // The binding's throwScope will see the pending JS exception. |
| 430 | if (throwScope.exception()) |
| 431 | return Exception { ExceptionCode::DataError }; |
| 432 | |
| 433 | if (!idbKey->isValid()) { |
| 434 | // Not a valid key — treat as IDBGetAllOptions dictionary. |
| 435 | auto optionsResult = convertDictionary<IDBGetAllOptions>(execState, key); |
| 436 | if (throwScope.exception()) |
| 437 | return Exception { ExceptionCode::DataError }; |
| 438 | auto options = optionsResult.releaseReturnValue(); |
| 439 | return doGetAllKeys(options.count, options.direction, |
| 440 | [state = &execState, query = WTF::move(options.query)]() |
| 441 | -> ExceptionOr<RefPtr<IDBKeyRange>> { |
| 442 | if (query.isUndefinedOrNull()) |
| 443 | return { nullptr }; |
| 444 | if (auto* keyRange = JSIDBKeyRange::toWrapped(state->vm(), query)) |
| 445 | return { RefPtr<IDBKeyRange>(keyRange) }; |
| 446 | auto onlyResult = IDBKeyRange::only(*state, query); |
| 447 | if (onlyResult.hasException()) |
| 448 | return ExceptionOr<RefPtr<IDBKeyRange>> { Exception(ExceptionCode::DataError, "Failed to execute 'getAllKeys' on 'IDBIndex': The parameter is not a valid key."_s) }; |
| 449 | return ExceptionOr<RefPtr<IDBKeyRange>> { onlyResult.releaseReturnValue() }; |
| 450 | }); |
| 451 | } |
| 452 | // else: valid key — fall through to normal key path |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | // Normal key conversion path. |
| 457 | return doGetAllKeys(count, IDBCursorDirection::Next, [state = &execState, key]() { |
| 458 | auto onlyResult = IDBKeyRange::only(*state, key); |
| 459 | if (onlyResult.hasException()) |
| 460 | return ExceptionOr<RefPtr<IDBKeyRange>> { Exception(ExceptionCode::DataError, "Failed to execute 'getAllKeys' on 'IDBIndex': The parameter is not a valid key."_s) }; |
| 461 | |
| 462 | return ExceptionOr<RefPtr<IDBKeyRange>> { onlyResult.releaseReturnValue() }; |
| 463 | }); |
| 464 | } |
| 465 | |
| 466 | ExceptionOr<Ref<IDBRequest>> IDBIndex::getAllRecords(JSGlobalObject&, IDBGetAllOptions&&) |
| 467 | { |
| 468 | return Exception { ExceptionCode::NotSupportedError, "getAllRecords is not yet supported"_s }; |
| 469 | } |
| 470 | |
| 471 | void IDBIndex::markAsDeleted() |
| 472 | { |
| 473 | ASSERT(canCurrentThreadAccessThreadLocalData(m_objectStore->transaction().database().originThread()))((void)0); |
| 474 | |
| 475 | ASSERT(!m_deleted)((void)0); |
| 476 | m_deleted = true; |
| 477 | } |
| 478 | |
| 479 | void IDBIndex::ref() const |
| 480 | { |
| 481 | m_objectStore->ref(); |
| 482 | } |
| 483 | |
| 484 | void IDBIndex::deref() const |
| 485 | { |
| 486 | m_objectStore->deref(); |
| 487 | } |
| 488 | |
| 489 | WebCoreOpaqueRoot IDBIndex::opaqueRoot() |
| 490 | { |
| 491 | return WebCoreOpaqueRoot { m_objectStore.ptr() }; |
| 492 | } |
| 493 | |
| 494 | WebCoreOpaqueRoot root(IDBIndex* index) |
| 495 | { |
| 496 | return index ? index->opaqueRoot() : nullptr; |
| 497 | } |
| 498 | |
| 499 | } // namespace WebCore |