2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "IDBCursorBackendImpl.h"
29 #if ENABLE(INDEXED_DATABASE)
31 #include "CrossThreadTask.h"
32 #include "IDBBackingStore.h"
33 #include "IDBCallbacks.h"
34 #include "IDBDatabaseError.h"
35 #include "IDBDatabaseException.h"
36 #include "IDBKeyRange.h"
37 #include "IDBObjectStoreBackendImpl.h"
38 #include "IDBRequest.h"
39 #include "IDBTransactionBackendInterface.h"
40 #include "SerializedScriptValue.h"
44 IDBCursorBackendImpl::IDBCursorBackendImpl(PassRefPtr<IDBBackingStore::Cursor> cursor, IDBCursor::Direction direction, CursorType cursorType, IDBTransactionBackendInterface* transaction, IDBObjectStoreBackendInterface* objectStore)
46 , m_direction(direction)
47 , m_cursorType(cursorType)
48 , m_transaction(transaction)
49 , m_objectStore(objectStore)
51 m_transaction->registerOpenCursor(this);
54 IDBCursorBackendImpl::~IDBCursorBackendImpl()
56 m_transaction->unregisterOpenCursor(this);
59 unsigned short IDBCursorBackendImpl::direction() const
64 PassRefPtr<IDBKey> IDBCursorBackendImpl::key() const
66 return m_cursor->key();
69 PassRefPtr<IDBKey> IDBCursorBackendImpl::primaryKey() const
71 return m_cursor->primaryKey();
74 PassRefPtr<SerializedScriptValue> IDBCursorBackendImpl::value() const
76 if (m_cursorType == IndexKeyCursor)
77 return SerializedScriptValue::nullValue();
78 return SerializedScriptValue::createFromWire(m_cursor->value());
81 void IDBCursorBackendImpl::update(PassRefPtr<SerializedScriptValue> value, PassRefPtr<IDBCallbacks> callbacks, ExceptionCode& ec)
83 if (!m_cursor || m_cursorType == IndexKeyCursor) {
84 ec = IDBDatabaseException::NOT_ALLOWED_ERR;
88 m_objectStore->put(value, m_cursor->primaryKey(), IDBObjectStoreBackendInterface::CursorUpdate, callbacks, m_transaction.get(), ec);
91 void IDBCursorBackendImpl::continueFunction(PassRefPtr<IDBKey> prpKey, PassRefPtr<IDBCallbacks> prpCallbacks, ExceptionCode& ec)
93 RefPtr<IDBCursorBackendImpl> cursor = this;
94 RefPtr<IDBKey> key = prpKey;
95 RefPtr<IDBCallbacks> callbacks = prpCallbacks;
96 if (!m_transaction->scheduleTask(createCallbackTask(&IDBCursorBackendImpl::continueFunctionInternal, cursor, key, callbacks)))
97 ec = IDBDatabaseException::NOT_ALLOWED_ERR;
100 // IMPORTANT: If this ever 1) fires an 'error' event and 2) it's possible to fire another event afterwards,
101 // IDBRequest::hasPendingActivity() will need to be modified to handle this!!!
102 void IDBCursorBackendImpl::continueFunctionInternal(ScriptExecutionContext*, PassRefPtr<IDBCursorBackendImpl> prpCursor, PassRefPtr<IDBKey> prpKey, PassRefPtr<IDBCallbacks> callbacks)
104 RefPtr<IDBCursorBackendImpl> cursor = prpCursor;
105 RefPtr<IDBKey> key = prpKey;
107 if (!cursor->m_cursor || !cursor->m_cursor->continueFunction(key.get())) {
108 cursor->m_cursor = 0;
109 callbacks->onSuccess(SerializedScriptValue::nullValue());
113 callbacks->onSuccessWithContinuation();
116 void IDBCursorBackendImpl::deleteFunction(PassRefPtr<IDBCallbacks> prpCallbacks, ExceptionCode& ec)
118 if (!m_cursor || m_cursorType == IndexKeyCursor) {
119 ec = IDBDatabaseException::NOT_ALLOWED_ERR;
123 m_objectStore->deleteFunction(m_cursor->primaryKey(), prpCallbacks, m_transaction.get(), ec);
126 void IDBCursorBackendImpl::close()
132 } // namespace WebCore
134 #endif // ENABLE(INDEXED_DATABASE)