tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebCore / storage / IDBCursorBackendImpl.cpp
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
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     m_transaction->registerOpenCursor(this);
52 }
53
54 IDBCursorBackendImpl::~IDBCursorBackendImpl()
55 {
56     m_transaction->unregisterOpenCursor(this);
57 }
58
59 unsigned short IDBCursorBackendImpl::direction() const
60 {
61     return m_direction;
62 }
63
64 PassRefPtr<IDBKey> IDBCursorBackendImpl::key() const
65 {
66     return m_cursor->key();
67 }
68
69 PassRefPtr<IDBKey> IDBCursorBackendImpl::primaryKey() const
70 {
71     return m_cursor->primaryKey();
72 }
73
74 PassRefPtr<SerializedScriptValue> IDBCursorBackendImpl::value() const
75 {
76     if (m_cursorType == IndexKeyCursor)
77       return SerializedScriptValue::nullValue();
78     return SerializedScriptValue::createFromWire(m_cursor->value());
79 }
80
81 void IDBCursorBackendImpl::update(PassRefPtr<SerializedScriptValue> value, PassRefPtr<IDBCallbacks> callbacks, ExceptionCode& ec)
82 {
83     if (!m_cursor || m_cursorType == IndexKeyCursor) {
84         ec = IDBDatabaseException::NOT_ALLOWED_ERR;
85         return;
86     }
87
88     m_objectStore->put(value, m_cursor->primaryKey(), IDBObjectStoreBackendInterface::CursorUpdate, callbacks, m_transaction.get(), ec);
89 }
90
91 void IDBCursorBackendImpl::continueFunction(PassRefPtr<IDBKey> prpKey, PassRefPtr<IDBCallbacks> prpCallbacks, ExceptionCode& ec)
92 {
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;
98 }
99
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)
103 {
104     RefPtr<IDBCursorBackendImpl> cursor = prpCursor;
105     RefPtr<IDBKey> key = prpKey;
106
107     if (!cursor->m_cursor || !cursor->m_cursor->continueFunction(key.get())) {
108         cursor->m_cursor = 0;
109         callbacks->onSuccess(SerializedScriptValue::nullValue());
110         return;
111     }
112
113     callbacks->onSuccessWithContinuation();
114 }
115
116 void IDBCursorBackendImpl::deleteFunction(PassRefPtr<IDBCallbacks> prpCallbacks, ExceptionCode& ec)
117 {
118     if (!m_cursor || m_cursorType == IndexKeyCursor) {
119         ec = IDBDatabaseException::NOT_ALLOWED_ERR;
120         return;
121     }
122
123     m_objectStore->deleteFunction(m_cursor->primaryKey(), prpCallbacks, m_transaction.get(), ec);
124 }
125
126 void IDBCursorBackendImpl::close()
127 {
128     if (m_cursor)
129         m_cursor->close();
130 }
131
132 } // namespace WebCore
133
134 #endif // ENABLE(INDEXED_DATABASE)