1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
|
/*
* Copyright (C) 2015 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "IDBCursor.h"
#include "IDBBindingUtilities.h"
#include "IDBDatabase.h"
#include "IDBGetResult.h"
#include "IDBIndex.h"
#include "IDBIterateCursorData.h"
#include "IDBObjectStore.h"
#include "IDBRequest.h"
#include "IDBTransaction.h"
#include "Logging.h"
#include "ScriptExecutionContext.h"
#include "SerializedScriptValue.h"
#include <JavaScriptCore/HeapInlines.h>
#include <JavaScriptCore/JSCJSValueInlines.h>
#include <JavaScriptCore/StrongInlines.h>
#include <wtf/TZoneMallocInlines.h>
namespace WebCore {
using namespace JSC;
WTF_MAKE_TZONE_OR_ISO_ALLOCATED_IMPL(IDBCursor);
Ref<IDBCursor> IDBCursor::create(IDBObjectStore& objectStore, const IDBCursorInfo& info)
{
return adoptRef(*new IDBCursor(objectStore, info));
}
Ref<IDBCursor> IDBCursor::create(IDBIndex& index, const IDBCursorInfo& info)
{
return adoptRef(*new IDBCursor(index, info));
}
IDBCursor::IDBCursor(IDBObjectStore& objectStore, const IDBCursorInfo& info)
: m_info(info)
, m_source(&objectStore)
{
ASSERT(canCurrentThreadAccessThreadLocalData(effectiveObjectStore().transaction().database().originThread()));
}
IDBCursor::IDBCursor(IDBIndex& index, const IDBCursorInfo& info)
: m_info(info)
, m_source(&index)
{
ASSERT(canCurrentThreadAccessThreadLocalData(effectiveObjectStore().transaction().database().originThread()));
}
IDBCursor::~IDBCursor()
{
ASSERT(canCurrentThreadAccessThreadLocalData(effectiveObjectStore().transaction().database().originThread()));
}
bool IDBCursor::sourcesDeleted() const
{
ASSERT(canCurrentThreadAccessThreadLocalData(effectiveObjectStore().transaction().database().originThread()));
return WTF::switchOn(m_source,
[] (const RefPtr<IDBObjectStore>& objectStore) { return objectStore->isDeleted(); },
[] (const RefPtr<IDBIndex>& index) { return index->isDeleted() || index->objectStore().isDeleted(); }
);
}
IDBObjectStore& IDBCursor::effectiveObjectStore() const
{
return WTF::switchOn(m_source,
[] (const RefPtr<IDBObjectStore>& objectStore) -> IDBObjectStore& { return *objectStore; },
[] (const RefPtr<IDBIndex>& index) -> IDBObjectStore& { return index->objectStore(); }
);
}
IDBTransaction& IDBCursor::transaction() const
{
ASSERT(canCurrentThreadAccessThreadLocalData(effectiveObjectStore().transaction().database().originThread()));
return effectiveObjectStore().transaction();
}
ExceptionOr<Ref<IDBRequest>> IDBCursor::update(JSGlobalObject& state, JSValue value)
{
LOG(IndexedDB, "IDBCursor::update");
ASSERT(canCurrentThreadAccessThreadLocalData(effectiveObjectStore().transaction().database().originThread()));
if (sourcesDeleted())
return Exception { ExceptionCode::InvalidStateError, "Failed to execute 'update' on 'IDBCursor': The cursor's source or effective object store has been deleted."_s };
if (!transaction().isActive())
return Exception { ExceptionCode::TransactionInactiveError, "Failed to execute 'update' on 'IDBCursor': The transaction is inactive or finished."_s };
if (transaction().isReadOnly())
return Exception { ExceptionCode::ReadonlyError, "Failed to execute 'update' on 'IDBCursor': The record may not be updated inside a read-only transaction."_s };
if (!m_gotValue)
return Exception { ExceptionCode::InvalidStateError, "Failed to execute 'update' on 'IDBCursor': The cursor is being iterated or has iterated past its end."_s };
if (!isKeyCursorWithValue())
return Exception { ExceptionCode::InvalidStateError, "Failed to execute 'update' on 'IDBCursor': The cursor is a key cursor."_s };
VM& vm = state.vm();
auto scope = DECLARE_CATCH_SCOPE(vm);
// Transaction should be inactive during structured clone.
Ref transaction = effectiveObjectStore().transaction();
transaction->deactivate();
auto serializedValue = SerializedScriptValue::create(state, value, SerializationForStorage::Yes);
transaction->activate();
if (UNLIKELY(scope.exception()))
return Exception { ExceptionCode::DataCloneError, "Failed to store record in an IDBObjectStore: An object could not be cloned."_s };
auto& objectStore = effectiveObjectStore();
auto& optionalKeyPath = objectStore.info().keyPath();
const bool usesInLineKeys = !!optionalKeyPath;
if (usesInLineKeys) {
auto clonedValue = serializedValue->deserialize(state, &state, SerializationErrorMode::NonThrowing);
RefPtr<IDBKey> keyPathKey = maybeCreateIDBKeyFromScriptValueAndKeyPath(state, clonedValue, optionalKeyPath.value());
IDBKeyData keyPathKeyData(keyPathKey.get());
if (!keyPathKey || keyPathKeyData != m_primaryKeyData)
return Exception { ExceptionCode::DataError, "Failed to execute 'update' on 'IDBCursor': The effective object store of this cursor uses in-line keys and evaluating the key path of the value parameter results in a different value than the cursor's effective key."_s };
}
auto putResult = effectiveObjectStore().putForCursorUpdate(state, value, m_primaryKey.copyRef(), WTFMove(serializedValue));
if (putResult.hasException())
return putResult.releaseException();
auto request = putResult.releaseReturnValue();
request->setSource(*this);
return request;
}
ExceptionOr<void> IDBCursor::advance(unsigned count)
{
LOG(IndexedDB, "IDBCursor::advance");
ASSERT(canCurrentThreadAccessThreadLocalData(effectiveObjectStore().transaction().database().originThread()));
if (!m_request)
return Exception { ExceptionCode::InvalidStateError };
if (!count)
return Exception { ExceptionCode::TypeError, "Failed to execute 'advance' on 'IDBCursor': A count argument with value 0 (zero) was supplied, must be greater than 0."_s };
if (!transaction().isActive())
return Exception { ExceptionCode::TransactionInactiveError, "Failed to execute 'advance' on 'IDBCursor': The transaction is inactive or finished."_s };
if (sourcesDeleted())
return Exception { ExceptionCode::InvalidStateError, "Failed to execute 'advance' on 'IDBCursor': The cursor's source or effective object store has been deleted."_s };
if (!m_gotValue)
return Exception { ExceptionCode::InvalidStateError, "Failed to execute 'advance' on 'IDBCursor': The cursor is being iterated or has iterated past its end."_s };
m_gotValue = false;
uncheckedIterateCursor(IDBKeyData(), count);
return { };
}
ExceptionOr<void> IDBCursor::continuePrimaryKey(JSGlobalObject& state, JSValue keyValue, JSValue primaryKeyValue)
{
if (!m_request)
return Exception { ExceptionCode::InvalidStateError };
if (!transaction().isActive())
return Exception { ExceptionCode::TransactionInactiveError, "Failed to execute 'continuePrimaryKey' on 'IDBCursor': The transaction is inactive or finished."_s };
if (sourcesDeleted())
return Exception { ExceptionCode::InvalidStateError, "Failed to execute 'continuePrimaryKey' on 'IDBCursor': The cursor's source or effective object store has been deleted."_s };
if (!std::holds_alternative<RefPtr<IDBIndex>>(m_source))
return Exception { ExceptionCode::InvalidAccessError, "Failed to execute 'continuePrimaryKey' on 'IDBCursor': The cursor's source is not an index."_s };
auto direction = m_info.cursorDirection();
if (direction != IndexedDB::CursorDirection::Next && direction != IndexedDB::CursorDirection::Prev)
return Exception { ExceptionCode::InvalidAccessError, "Failed to execute 'continuePrimaryKey' on 'IDBCursor': The cursor's direction must be either \"next\" or \"prev\"."_s };
if (!m_gotValue)
return Exception { ExceptionCode::InvalidStateError, "Failed to execute 'continuePrimaryKey' on 'IDBCursor': The cursor is being iterated or has iterated past its end."_s };
RefPtr<IDBKey> key = scriptValueToIDBKey(state, keyValue);
if (!key->isValid())
return Exception { ExceptionCode::DataError, "Failed to execute 'continuePrimaryKey' on 'IDBCursor': The first parameter is not a valid key."_s };
RefPtr<IDBKey> primaryKey = scriptValueToIDBKey(state, primaryKeyValue);
if (!primaryKey->isValid())
return Exception { ExceptionCode::DataError, "Failed to execute 'continuePrimaryKey' on 'IDBCursor': The second parameter is not a valid key."_s };
IDBKeyData keyData = { key.get() };
IDBKeyData primaryKeyData = { primaryKey.get() };
if (keyData < m_keyData && direction == IndexedDB::CursorDirection::Next)
return Exception { ExceptionCode::DataError, "Failed to execute 'continuePrimaryKey' on 'IDBCursor': The first parameter is less than this cursor's position and this cursor's direction is \"next\"."_s };
if (keyData > m_keyData && direction == IndexedDB::CursorDirection::Prev)
return Exception { ExceptionCode::DataError, "Failed to execute 'continuePrimaryKey' on 'IDBCursor': The first parameter is greater than this cursor's position and this cursor's direction is \"prev\"."_s };
if (keyData == m_keyData) {
if (primaryKeyData <= m_primaryKeyData && direction == IndexedDB::CursorDirection::Next)
return Exception { ExceptionCode::DataError, "Failed to execute 'continuePrimaryKey' on 'IDBCursor': The key parameters represent a position less-than-or-equal-to this cursor's position and this cursor's direction is \"next\"."_s };
if (primaryKeyData >= m_primaryKeyData && direction == IndexedDB::CursorDirection::Prev)
return Exception { ExceptionCode::DataError, "Failed to execute 'continuePrimaryKey' on 'IDBCursor': The key parameters represent a position greater-than-or-equal-to this cursor's position and this cursor's direction is \"prev\"."_s };
}
m_gotValue = false;
uncheckedIterateCursor(keyData, primaryKeyData);
return { };
}
ExceptionOr<void> IDBCursor::continueFunction(JSGlobalObject& execState, JSValue keyValue)
{
RefPtr<IDBKey> key;
if (!keyValue.isUndefined())
key = scriptValueToIDBKey(execState, keyValue);
return continueFunction(key.get());
}
ExceptionOr<void> IDBCursor::continueFunction(const IDBKeyData& key)
{
LOG(IndexedDB, "IDBCursor::continueFunction (to key %s)", key.loggingString().utf8().data());
ASSERT(canCurrentThreadAccessThreadLocalData(effectiveObjectStore().transaction().database().originThread()));
if (!m_request)
return Exception { ExceptionCode::InvalidStateError };
if (!transaction().isActive())
return Exception { ExceptionCode::TransactionInactiveError, "Failed to execute 'continue' on 'IDBCursor': The transaction is inactive or finished."_s };
if (sourcesDeleted())
return Exception { ExceptionCode::InvalidStateError, "Failed to execute 'continue' on 'IDBCursor': The cursor's source or effective object store has been deleted."_s };
if (!m_gotValue)
return Exception { ExceptionCode::InvalidStateError, "Failed to execute 'continue' on 'IDBCursor': The cursor is being iterated or has iterated past its end."_s };
if (!key.isNull() && !key.isValid())
return Exception { ExceptionCode::DataError, "Failed to execute 'continue' on 'IDBCursor': The parameter is not a valid key."_s };
if (m_info.isDirectionForward()) {
if (!key.isNull() && key.compare(m_keyData) <= 0)
return Exception { ExceptionCode::DataError, "Failed to execute 'continue' on 'IDBCursor': The parameter is less than or equal to this cursor's position."_s };
} else {
if (!key.isNull() && key.compare(m_keyData) >= 0)
return Exception { ExceptionCode::DataError, "Failed to execute 'continue' on 'IDBCursor': The parameter is greater than or equal to this cursor's position."_s };
}
m_gotValue = false;
uncheckedIterateCursor(key, 0);
return { };
}
void IDBCursor::uncheckedIterateCursor(const IDBKeyData& key, unsigned count)
{
ASSERT(m_request);
ASSERT(canCurrentThreadAccessThreadLocalData(effectiveObjectStore().transaction().database().originThread()));
m_request->willIterateCursor(*this);
transaction().iterateCursor(*this, { key, { }, count });
}
void IDBCursor::uncheckedIterateCursor(const IDBKeyData& key, const IDBKeyData& primaryKey)
{
ASSERT(m_request);
ASSERT(canCurrentThreadAccessThreadLocalData(effectiveObjectStore().transaction().database().originThread()));
m_request->willIterateCursor(*this);
transaction().iterateCursor(*this, { key, primaryKey, 0 });
}
ExceptionOr<Ref<WebCore::IDBRequest>> IDBCursor::deleteFunction()
{
LOG(IndexedDB, "IDBCursor::deleteFunction");
ASSERT(canCurrentThreadAccessThreadLocalData(effectiveObjectStore().transaction().database().originThread()));
if (sourcesDeleted())
return Exception { ExceptionCode::InvalidStateError, "Failed to execute 'delete' on 'IDBCursor': The cursor's source or effective object store has been deleted."_s };
if (!transaction().isActive())
return Exception { ExceptionCode::TransactionInactiveError, "Failed to execute 'delete' on 'IDBCursor': The transaction is inactive or finished."_s };
if (transaction().isReadOnly())
return Exception { ExceptionCode::ReadonlyError, "Failed to execute 'delete' on 'IDBCursor': The record may not be deleted inside a read-only transaction."_s };
if (!m_gotValue)
return Exception { ExceptionCode::InvalidStateError, "Failed to execute 'delete' on 'IDBCursor': The cursor is being iterated or has iterated past its end."_s };
if (!isKeyCursorWithValue())
return Exception { ExceptionCode::InvalidStateError, "Failed to execute 'delete' on 'IDBCursor': The cursor is a key cursor."_s };
auto result = effectiveObjectStore().deleteFunction(IDBKeyRange::create(m_primaryKey.copyRef()).ptr());
if (result.hasException())
return result.releaseException();
auto request = result.releaseReturnValue();
request->setSource(*this);
return request;
}
bool IDBCursor::setGetResult(IDBRequest& request, const IDBGetResult& getResult, uint64_t operationID)
{
LOG(IndexedDB, "IDBCursor::setGetResult - current key %s", getResult.keyData().loggingString().left(100).utf8().data());
ASSERT(canCurrentThreadAccessThreadLocalData(effectiveObjectStore().transaction().database().originThread()));
RefPtr context = request.scriptExecutionContext();
if (!context)
return false;
VM& vm = context->vm();
JSLockHolder lock(vm);
clearWrappers();
if (!getResult.isDefined()) {
m_keyData = { };
m_key = nullptr;
m_primaryKeyData = { };
m_primaryKey = nullptr;
m_value = { };
m_gotValue = false;
return false;
}
m_keyData = getResult.keyData();
m_key = m_keyData.maybeCreateIDBKey();
m_primaryKeyData = getResult.primaryKeyData();
m_primaryKey = m_primaryKeyData.maybeCreateIDBKey();
if (isKeyCursorWithValue()) {
m_value = getResult.value();
m_keyPath = getResult.keyPath();
}
auto prefetchedRecords = getResult.prefetchedRecords();
if (!prefetchedRecords.isEmpty()) {
for (auto& record : prefetchedRecords)
m_prefetchedRecords.append(record);
m_prefetchOperationID = operationID;
}
m_gotValue = true;
return true;
}
void IDBCursor::clearWrappers()
{
m_keyWrapper.clear();
m_primaryKeyWrapper.clear();
m_valueWrapper.clear();
}
std::optional<IDBGetResult> IDBCursor::iterateWithPrefetchedRecords(unsigned count, uint64_t lastWriteOperationID)
{
unsigned step = count > 0 ? count : 1;
if (step > m_prefetchedRecords.size() || m_prefetchOperationID <= lastWriteOperationID)
return std::nullopt;
while (--step)
m_prefetchedRecords.removeFirst();
auto record = m_prefetchedRecords.takeFirst();
LOG(IndexedDB, "IDBTransaction::iterateWithPrefetchedRecords consumes %u records", count > 0 ? count : 1);
return IDBGetResult(record.key, record.primaryKey, IDBValue(record.value), effectiveObjectStore().keyPath());
}
void IDBCursor::clearPrefetchedRecords()
{
m_prefetchedRecords.clear();
}
} // namespace WebCore
|