1 /*
2 * Copyright (C) 2010 Google 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 *
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.
13 *
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.
24 */
25
26 #include "config.h"
27 #include "IDBCursorBackendImpl.h"
28
29 #if ENABLE(INDEXED_DATABASE)
30
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"
41
42 namespace WebCore {
43
IDBCursorBackendImpl(PassRefPtr<IDBBackingStore::Cursor> cursor,IDBCursor::Direction direction,CursorType cursorType,IDBTransactionBackendInterface * transaction,IDBObjectStoreBackendInterface * objectStore)44 IDBCursorBackendImpl::IDBCursorBackendImpl(PassRefPtr<IDBBackingStore::Cursor> cursor, IDBCursor::Direction direction, CursorType cursorType, IDBTransactionBackendInterface* transaction, IDBObjectStoreBackendInterface* objectStore)
45 : m_cursor(cursor)
46 , m_direction(direction)
47 , m_cursorType(cursorType)
48 , m_transaction(transaction)
49 , m_objectStore(objectStore)
50 {
51 }
52
~IDBCursorBackendImpl()53 IDBCursorBackendImpl::~IDBCursorBackendImpl()
54 {
55 }
56
direction() const57 unsigned short IDBCursorBackendImpl::direction() const
58 {
59 return m_direction;
60 }
61
key() const62 PassRefPtr<IDBKey> IDBCursorBackendImpl::key() const
63 {
64 return m_cursor->key();
65 }
66
primaryKey() const67 PassRefPtr<IDBKey> IDBCursorBackendImpl::primaryKey() const
68 {
69 return m_cursor->primaryKey();
70 }
71
value() const72 PassRefPtr<SerializedScriptValue> IDBCursorBackendImpl::value() const
73 {
74 ASSERT(m_cursorType != IndexKeyCursor);
75 return SerializedScriptValue::createFromWire(m_cursor->value());
76 }
77
update(PassRefPtr<SerializedScriptValue> value,PassRefPtr<IDBCallbacks> callbacks,ExceptionCode & ec)78 void IDBCursorBackendImpl::update(PassRefPtr<SerializedScriptValue> value, PassRefPtr<IDBCallbacks> callbacks, ExceptionCode& ec)
79 {
80 if (!m_cursor || m_cursorType == IndexKeyCursor) {
81 ec = IDBDatabaseException::NOT_ALLOWED_ERR;
82 return;
83 }
84
85 m_objectStore->put(value, m_cursor->primaryKey(), IDBObjectStoreBackendInterface::CursorUpdate, callbacks, m_transaction.get(), ec);
86 }
87
continueFunction(PassRefPtr<IDBKey> prpKey,PassRefPtr<IDBCallbacks> prpCallbacks,ExceptionCode & ec)88 void IDBCursorBackendImpl::continueFunction(PassRefPtr<IDBKey> prpKey, PassRefPtr<IDBCallbacks> prpCallbacks, ExceptionCode& ec)
89 {
90 RefPtr<IDBCursorBackendImpl> cursor = this;
91 RefPtr<IDBKey> key = prpKey;
92 RefPtr<IDBCallbacks> callbacks = prpCallbacks;
93 if (!m_transaction->scheduleTask(createCallbackTask(&IDBCursorBackendImpl::continueFunctionInternal, cursor, key, callbacks)))
94 ec = IDBDatabaseException::NOT_ALLOWED_ERR;
95 }
96
97 // IMPORTANT: If this ever 1) fires an 'error' event and 2) it's possible to fire another event afterwards,
98 // IDBRequest::hasPendingActivity() will need to be modified to handle this!!!
continueFunctionInternal(ScriptExecutionContext *,PassRefPtr<IDBCursorBackendImpl> prpCursor,PassRefPtr<IDBKey> prpKey,PassRefPtr<IDBCallbacks> callbacks)99 void IDBCursorBackendImpl::continueFunctionInternal(ScriptExecutionContext*, PassRefPtr<IDBCursorBackendImpl> prpCursor, PassRefPtr<IDBKey> prpKey, PassRefPtr<IDBCallbacks> callbacks)
100 {
101 RefPtr<IDBCursorBackendImpl> cursor = prpCursor;
102 RefPtr<IDBKey> key = prpKey;
103
104 if (!cursor->m_cursor || !cursor->m_cursor->continueFunction(key.get())) {
105 cursor->m_cursor = 0;
106 callbacks->onSuccess(SerializedScriptValue::nullValue());
107 return;
108 }
109
110 callbacks->onSuccess(cursor.get());
111 }
112
deleteFunction(PassRefPtr<IDBCallbacks> prpCallbacks,ExceptionCode & ec)113 void IDBCursorBackendImpl::deleteFunction(PassRefPtr<IDBCallbacks> prpCallbacks, ExceptionCode& ec)
114 {
115 if (!m_cursor || m_cursorType == IndexKeyCursor) {
116 ec = IDBDatabaseException::NOT_ALLOWED_ERR;
117 return;
118 }
119
120 m_objectStore->deleteFunction(m_cursor->primaryKey(), prpCallbacks, m_transaction.get(), ec);
121 }
122
123 } // namespace WebCore
124
125 #endif // ENABLE(INDEXED_DATABASE)
126