Source/WebCore: IndexedDB: Propagate more leveldb errors to IDBIndex and IDBObjectStore
[WebKit-https.git] / Source / WebCore / Modules / indexeddb / IDBIndexBackendImpl.cpp
1 /*
2  * Copyright (C) 2011 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 "IDBIndexBackendImpl.h"
28
29 #if ENABLE(INDEXED_DATABASE)
30
31 #include "CrossThreadTask.h"
32 #include "IDBBackingStore.h"
33 #include "IDBCallbacks.h"
34 #include "IDBCursorBackendImpl.h"
35 #include "IDBDatabaseBackendImpl.h"
36 #include "IDBDatabaseException.h"
37 #include "IDBKey.h"
38 #include "IDBKeyRange.h"
39 #include "IDBMetadata.h"
40 #include "IDBObjectStoreBackendImpl.h"
41 #include "IDBTracing.h"
42 #include "IDBTransactionBackendImpl.h"
43
44 namespace WebCore {
45
46 class IDBIndexBackendImpl::OpenIndexCursorOperation {
47 public:
48     static PassOwnPtr<ScriptExecutionContext::Task> create(PassRefPtr<IDBIndexBackendImpl> index, PassRefPtr<IDBKeyRange> keyRange, unsigned short direction, IDBCursorBackendInterface::CursorType cursorType, PassRefPtr<IDBCallbacks> callbacks, PassRefPtr<IDBTransactionBackendImpl> transaction)
49     {
50         return createCallbackTask(&OpenIndexCursorOperation::perform, index, keyRange, direction, cursorType, callbacks, transaction);
51     }
52 private:
53     static void perform(ScriptExecutionContext*, PassRefPtr<IDBIndexBackendImpl>, PassRefPtr<IDBKeyRange>, unsigned short untypedDirection, IDBCursorBackendInterface::CursorType, PassRefPtr<IDBCallbacks>, PassRefPtr<IDBTransactionBackendImpl>);
54 };
55
56 class IDBIndexBackendImpl::IndexCountOperation {
57 public:
58     static PassOwnPtr<ScriptExecutionContext::Task> create(PassRefPtr<IDBIndexBackendImpl> index, PassRefPtr<IDBKeyRange> keyRange, PassRefPtr<IDBCallbacks> callbacks, PassRefPtr<IDBTransactionBackendImpl> transaction)
59     {
60         return createCallbackTask(&IndexCountOperation::perform, index, keyRange, callbacks, transaction);
61     }
62 private:
63     static void perform(ScriptExecutionContext*, PassRefPtr<IDBIndexBackendImpl>, PassRefPtr<IDBKeyRange>, PassRefPtr<IDBCallbacks>, PassRefPtr<IDBTransactionBackendImpl>);
64 };
65
66 class IDBIndexBackendImpl::IndexReferencedValueRetrievalOperation {
67 public:
68     static PassOwnPtr<ScriptExecutionContext::Task> create(PassRefPtr<IDBIndexBackendImpl> index, PassRefPtr<IDBKeyRange> keyRange, PassRefPtr<IDBCallbacks> callbacks, PassRefPtr<IDBTransactionBackendImpl> transaction)
69     {
70         return createCallbackTask(&IndexReferencedValueRetrievalOperation::perform, index, keyRange, callbacks, transaction);
71     }
72 private:
73     static void perform(ScriptExecutionContext*, PassRefPtr<IDBIndexBackendImpl>, PassRefPtr<IDBKeyRange>, PassRefPtr<IDBCallbacks>, PassRefPtr<IDBTransactionBackendImpl>);
74 };
75
76 class IDBIndexBackendImpl::IndexValueRetrievalOperation {
77 public:
78     static PassOwnPtr<ScriptExecutionContext::Task> create(PassRefPtr<IDBIndexBackendImpl> index, PassRefPtr<IDBKeyRange> keyRange, PassRefPtr<IDBCallbacks> callbacks, PassRefPtr<IDBTransactionBackendImpl> transaction)
79     {
80         return createCallbackTask(&IndexValueRetrievalOperation::perform, index, keyRange, callbacks, transaction);
81     }
82 private:
83     static void perform(ScriptExecutionContext*, PassRefPtr<IDBIndexBackendImpl>, PassRefPtr<IDBKeyRange>, PassRefPtr<IDBCallbacks>, PassRefPtr<IDBTransactionBackendImpl>);
84 };
85
86
87 IDBIndexBackendImpl::IDBIndexBackendImpl(const IDBDatabaseBackendImpl* database, IDBObjectStoreBackendImpl* objectStoreBackend, const IDBIndexMetadata& metadata)
88     : m_database(database)
89     , m_objectStoreBackend(objectStoreBackend)
90     , m_metadata(metadata)
91 {
92 }
93
94 IDBIndexBackendImpl::~IDBIndexBackendImpl()
95 {
96 }
97
98 void IDBIndexBackendImpl::OpenIndexCursorOperation::perform(ScriptExecutionContext*, PassRefPtr<IDBIndexBackendImpl> index, PassRefPtr<IDBKeyRange> range, unsigned short untypedDirection, IDBCursorBackendInterface::CursorType cursorType, PassRefPtr<IDBCallbacks> callbacks, PassRefPtr<IDBTransactionBackendImpl> transaction)
99 {
100     IDB_TRACE("OpenIndexCursorOperation");
101     IDBCursor::Direction direction = static_cast<IDBCursor::Direction>(untypedDirection);
102
103     RefPtr<IDBBackingStore::Cursor> backingStoreCursor;
104
105     switch (cursorType) {
106     case IDBCursorBackendInterface::IndexKeyCursor:
107         backingStoreCursor = index->backingStore()->openIndexKeyCursor(transaction->backingStoreTransaction(), index->databaseId(), index->m_objectStoreBackend->id(), index->id(), range.get(), direction);
108         break;
109     case IDBCursorBackendInterface::IndexCursor:
110         backingStoreCursor = index->backingStore()->openIndexCursor(transaction->backingStoreTransaction(), index->databaseId(), index->m_objectStoreBackend->id(), index->id(), range.get(), direction);
111         break;
112     case IDBCursorBackendInterface::ObjectStoreCursor:
113     case IDBCursorBackendInterface::InvalidCursorType:
114         ASSERT_NOT_REACHED();
115         break;
116     }
117
118     if (!backingStoreCursor) {
119         callbacks->onSuccess(static_cast<SerializedScriptValue*>(0));
120         return;
121     }
122
123     RefPtr<IDBCursorBackendImpl> cursor = IDBCursorBackendImpl::create(backingStoreCursor.get(), cursorType, transaction.get(), index->m_objectStoreBackend);
124     callbacks->onSuccess(cursor, cursor->key(), cursor->primaryKey(), cursor->value());
125 }
126
127 void IDBIndexBackendImpl::openCursor(PassRefPtr<IDBKeyRange> keyRange, unsigned short direction, PassRefPtr<IDBCallbacks> prpCallbacks, IDBTransactionBackendInterface* transactionPtr, ExceptionCode&)
128 {
129     IDB_TRACE("IDBIndexBackendImpl::openCursor");
130     RefPtr<IDBCallbacks> callbacks = prpCallbacks;
131     RefPtr<IDBTransactionBackendImpl> transaction = IDBTransactionBackendImpl::from(transactionPtr);
132     if (!transaction->scheduleTask(OpenIndexCursorOperation::create(this, keyRange, direction, IDBCursorBackendInterface::IndexCursor, callbacks, transaction)))
133         callbacks->onError(IDBDatabaseError::create(IDBDatabaseException::AbortError));
134 }
135
136 void IDBIndexBackendImpl::openKeyCursor(PassRefPtr<IDBKeyRange> keyRange, unsigned short direction, PassRefPtr<IDBCallbacks> prpCallbacks, IDBTransactionBackendInterface* transactionPtr, ExceptionCode&)
137 {
138     IDB_TRACE("IDBIndexBackendImpl::openKeyCursor");
139     RefPtr<IDBCallbacks> callbacks = prpCallbacks;
140     RefPtr<IDBTransactionBackendImpl> transaction = IDBTransactionBackendImpl::from(transactionPtr);
141     if (!transaction->scheduleTask(OpenIndexCursorOperation::create(this, keyRange, direction, IDBCursorBackendInterface::IndexKeyCursor, callbacks, transaction)))
142         callbacks->onError(IDBDatabaseError::create(IDBDatabaseException::AbortError));
143 }
144
145 void IDBIndexBackendImpl::IndexCountOperation::perform(ScriptExecutionContext*, PassRefPtr<IDBIndexBackendImpl> index, PassRefPtr<IDBKeyRange> range, PassRefPtr<IDBCallbacks> callbacks, PassRefPtr<IDBTransactionBackendImpl> transaction)
146 {
147     IDB_TRACE("IndexCountOperation");
148     uint32_t count = 0;
149
150     RefPtr<IDBBackingStore::Cursor> backingStoreCursor = index->backingStore()->openIndexKeyCursor(transaction->backingStoreTransaction(), index->databaseId(), index->m_objectStoreBackend->id(), index->id(), range.get(), IDBCursor::NEXT);
151     if (!backingStoreCursor) {
152         callbacks->onSuccess(count);
153         return;
154     }
155
156     do {
157         ++count;
158     } while (backingStoreCursor->continueFunction(0));
159
160     callbacks->onSuccess(count);
161 }
162
163 void IDBIndexBackendImpl::count(PassRefPtr<IDBKeyRange> range, PassRefPtr<IDBCallbacks> prpCallbacks, IDBTransactionBackendInterface* transactionPtr, ExceptionCode&)
164 {
165     IDB_TRACE("IDBIndexBackendImpl::count");
166     RefPtr<IDBCallbacks> callbacks = prpCallbacks;
167     RefPtr<IDBTransactionBackendImpl> transaction = IDBTransactionBackendImpl::from(transactionPtr);
168     if (!transaction->scheduleTask(IndexCountOperation::create(this, range, callbacks, transaction)))
169         callbacks->onError(IDBDatabaseError::create(IDBDatabaseException::AbortError));
170 }
171
172 void IDBIndexBackendImpl::IndexReferencedValueRetrievalOperation::perform(ScriptExecutionContext*, PassRefPtr<IDBIndexBackendImpl> index, PassRefPtr<IDBKeyRange> keyRange, PassRefPtr<IDBCallbacks> callbacks, PassRefPtr<IDBTransactionBackendImpl> transaction)
173 {
174     IDB_TRACE("IndexReferencedValueRetrievalOperation");
175
176     RefPtr<IDBKey> key;
177
178     if (keyRange->isOnlyKey())
179         key = keyRange->lower();
180     else {
181         RefPtr<IDBBackingStore::Cursor> backingStoreCursor = index->backingStore()->openIndexCursor(transaction->backingStoreTransaction(), index->databaseId(), index->m_objectStoreBackend->id(), index->id(), keyRange.get(), IDBCursor::NEXT);
182
183         if (!backingStoreCursor) {
184             callbacks->onSuccess();
185             return;
186         }
187         key = backingStoreCursor->key();
188     }
189
190     RefPtr<IDBKey> primaryKey;
191     bool ok = index->backingStore()->getPrimaryKeyViaIndex(transaction->backingStoreTransaction(), index->databaseId(), index->m_objectStoreBackend->id(), index->id(), *key, primaryKey);
192     if (!ok) {
193         callbacks->onError(IDBDatabaseError::create(IDBDatabaseException::UnknownError, "Internal error in getPrimaryKeyViaIndex."));
194         return;
195     }
196
197     String value;
198     ok = index->backingStore()->getRecord(transaction->backingStoreTransaction(), index->databaseId(), index->m_objectStoreBackend->id(), *primaryKey, value);
199     if (!ok) {
200         callbacks->onError(IDBDatabaseError::create(IDBDatabaseException::UnknownError, "Internal error in getRecord."));
201         return;
202     }
203
204     if (value.isNull()) {
205         callbacks->onSuccess();
206         return;
207     }
208     if (index->m_objectStoreBackend->autoIncrement() && !index->m_objectStoreBackend->keyPath().isNull()) {
209         callbacks->onSuccess(SerializedScriptValue::createFromWire(value),
210                              primaryKey, index->m_objectStoreBackend->keyPath());
211         return;
212     }
213     callbacks->onSuccess(SerializedScriptValue::createFromWire(value));
214 }
215
216
217 void IDBIndexBackendImpl::IndexValueRetrievalOperation::perform(ScriptExecutionContext*, PassRefPtr<IDBIndexBackendImpl> index, PassRefPtr<IDBKeyRange> keyRange, PassRefPtr<IDBCallbacks> callbacks, PassRefPtr<IDBTransactionBackendImpl> transaction)
218 {
219     IDB_TRACE("IndexValueRetrievalOperation");
220
221     RefPtr<IDBBackingStore::Cursor> backingStoreCursor =
222             index->backingStore()->openIndexKeyCursor(transaction->backingStoreTransaction(), index->databaseId(), index->m_objectStoreBackend->id(), index->id(), keyRange.get(), IDBCursor::NEXT);
223
224     if (!backingStoreCursor) {
225         callbacks->onSuccess(static_cast<IDBKey*>(0));
226         return;
227     }
228
229     RefPtr<IDBKey> keyResult;
230     bool ok = index->backingStore()->getPrimaryKeyViaIndex(transaction->backingStoreTransaction(), index->databaseId(), index->m_objectStoreBackend->id(), index->id(), *backingStoreCursor->key(), keyResult);
231     if (!ok) {
232         callbacks->onError(IDBDatabaseError::create(IDBDatabaseException::UnknownError, "Internal error in getPrimaryKeyViaIndex."));
233         return;
234     }
235     if (!keyResult) {
236         callbacks->onSuccess(static_cast<IDBKey*>(0));
237         return;
238     }
239     callbacks->onSuccess(keyResult.get());
240 }
241
242 void IDBIndexBackendImpl::get(PassRefPtr<IDBKeyRange> keyRange, PassRefPtr<IDBCallbacks> prpCallbacks, IDBTransactionBackendInterface* transactionPtr, ExceptionCode&)
243 {
244     IDB_TRACE("IDBIndexBackendImpl::get");
245     RefPtr<IDBCallbacks> callbacks = prpCallbacks;
246     RefPtr<IDBTransactionBackendImpl> transaction = IDBTransactionBackendImpl::from(transactionPtr);
247     if (!transaction->scheduleTask(IndexReferencedValueRetrievalOperation::create(this, keyRange, callbacks, transaction)))
248         callbacks->onError(IDBDatabaseError::create(IDBDatabaseException::AbortError));
249 }
250
251 void IDBIndexBackendImpl::getKey(PassRefPtr<IDBKeyRange> keyRange, PassRefPtr<IDBCallbacks> prpCallbacks, IDBTransactionBackendInterface* transactionPtr, ExceptionCode&)
252 {
253     IDB_TRACE("IDBIndexBackendImpl::getKey");
254     RefPtr<IDBCallbacks> callbacks = prpCallbacks;
255     RefPtr<IDBTransactionBackendImpl> transaction = IDBTransactionBackendImpl::from(transactionPtr);
256     if (!transaction->scheduleTask(IndexValueRetrievalOperation::create(this, keyRange, callbacks, transaction)))
257         callbacks->onError(IDBDatabaseError::create(IDBDatabaseException::AbortError));
258 }
259
260 } // namespace WebCore
261
262 #endif // ENABLE(INDEXED_DATABASE)