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 "modules/indexeddb/IDBIndex.h"
29 #include "bindings/core/v8/ExceptionState.h"
30 #include "bindings/modules/v8/IDBBindingUtilities.h"
31 #include "core/dom/ExceptionCode.h"
32 #include "core/dom/ExecutionContext.h"
33 #include "modules/indexeddb/IDBDatabase.h"
34 #include "modules/indexeddb/IDBKey.h"
35 #include "modules/indexeddb/IDBObjectStore.h"
36 #include "modules/indexeddb/IDBTracing.h"
37 #include "modules/indexeddb/IDBTransaction.h"
38 #include "modules/indexeddb/WebIDBCallbacksImpl.h"
39 #include "public/platform/WebIDBKeyRange.h"
41 using blink::WebIDBCallbacks;
42 using blink::WebIDBCursor;
43 using blink::WebIDBDatabase;
47 IDBIndex::IDBIndex(const IDBIndexMetadata& metadata, IDBObjectStore* objectStore, IDBTransaction* transaction)
48 : m_metadata(metadata)
49 , m_objectStore(objectStore)
50 , m_transaction(transaction)
53 ASSERT(m_objectStore);
54 ASSERT(m_transaction);
55 ASSERT(m_metadata.id != IDBIndexMetadata::InvalidId);
62 void IDBIndex::trace(Visitor* visitor)
64 visitor->trace(m_objectStore);
65 visitor->trace(m_transaction);
68 ScriptValue IDBIndex::keyPath(ScriptState* scriptState) const
70 return idbAnyToScriptValue(scriptState, IDBAny::create(m_metadata.keyPath));
73 IDBRequest* IDBIndex::openCursor(ScriptState* scriptState, const ScriptValue& range, const String& directionString, ExceptionState& exceptionState)
75 IDB_TRACE("IDBIndex::openCursor");
77 exceptionState.throwDOMException(InvalidStateError, IDBDatabase::indexDeletedErrorMessage);
80 if (m_transaction->isFinished() || m_transaction->isFinishing()) {
81 exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::transactionFinishedErrorMessage);
84 if (!m_transaction->isActive()) {
85 exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::transactionInactiveErrorMessage);
88 WebIDBCursorDirection direction = IDBCursor::stringToDirection(directionString, exceptionState);
89 if (exceptionState.hadException())
92 IDBKeyRange* keyRange = IDBKeyRange::fromScriptValue(scriptState->executionContext(), range, exceptionState);
93 if (exceptionState.hadException())
97 exceptionState.throwDOMException(InvalidStateError, IDBDatabase::databaseClosedErrorMessage);
101 return openCursor(scriptState, keyRange, direction);
104 IDBRequest* IDBIndex::openCursor(ScriptState* scriptState, IDBKeyRange* keyRange, WebIDBCursorDirection direction)
106 IDBRequest* request = IDBRequest::create(scriptState, IDBAny::create(this), m_transaction.get());
107 request->setCursorDetails(IndexedDB::CursorKeyAndValue, direction);
108 backendDB()->openCursor(m_transaction->id(), m_objectStore->id(), m_metadata.id, keyRange, direction, false, WebIDBTaskTypeNormal, WebIDBCallbacksImpl::create(request).leakPtr());
112 IDBRequest* IDBIndex::count(ScriptState* scriptState, const ScriptValue& range, ExceptionState& exceptionState)
114 IDB_TRACE("IDBIndex::count");
116 exceptionState.throwDOMException(InvalidStateError, IDBDatabase::indexDeletedErrorMessage);
119 if (m_transaction->isFinished() || m_transaction->isFinishing()) {
120 exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::transactionFinishedErrorMessage);
123 if (!m_transaction->isActive()) {
124 exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::transactionInactiveErrorMessage);
128 IDBKeyRange* keyRange = IDBKeyRange::fromScriptValue(scriptState->executionContext(), range, exceptionState);
129 if (exceptionState.hadException())
133 exceptionState.throwDOMException(InvalidStateError, IDBDatabase::databaseClosedErrorMessage);
137 IDBRequest* request = IDBRequest::create(scriptState, IDBAny::create(this), m_transaction.get());
138 backendDB()->count(m_transaction->id(), m_objectStore->id(), m_metadata.id, keyRange, WebIDBCallbacksImpl::create(request).leakPtr());
142 IDBRequest* IDBIndex::openKeyCursor(ScriptState* scriptState, const ScriptValue& range, const String& directionString, ExceptionState& exceptionState)
144 IDB_TRACE("IDBIndex::openKeyCursor");
146 exceptionState.throwDOMException(InvalidStateError, IDBDatabase::indexDeletedErrorMessage);
149 if (m_transaction->isFinished() || m_transaction->isFinishing()) {
150 exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::transactionFinishedErrorMessage);
153 if (!m_transaction->isActive()) {
154 exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::transactionInactiveErrorMessage);
157 WebIDBCursorDirection direction = IDBCursor::stringToDirection(directionString, exceptionState);
158 if (exceptionState.hadException())
161 IDBKeyRange* keyRange = IDBKeyRange::fromScriptValue(scriptState->executionContext(), range, exceptionState);
162 if (exceptionState.hadException())
165 exceptionState.throwDOMException(InvalidStateError, IDBDatabase::databaseClosedErrorMessage);
169 IDBRequest* request = IDBRequest::create(scriptState, IDBAny::create(this), m_transaction.get());
170 request->setCursorDetails(IndexedDB::CursorKeyOnly, direction);
171 backendDB()->openCursor(m_transaction->id(), m_objectStore->id(), m_metadata.id, keyRange, direction, true, WebIDBTaskTypeNormal, WebIDBCallbacksImpl::create(request).leakPtr());
175 IDBRequest* IDBIndex::get(ScriptState* scriptState, const ScriptValue& key, ExceptionState& exceptionState)
177 IDB_TRACE("IDBIndex::get");
178 return getInternal(scriptState, key, exceptionState, false);
181 IDBRequest* IDBIndex::getKey(ScriptState* scriptState, const ScriptValue& key, ExceptionState& exceptionState)
183 IDB_TRACE("IDBIndex::getKey");
184 return getInternal(scriptState, key, exceptionState, true);
187 IDBRequest* IDBIndex::getInternal(ScriptState* scriptState, const ScriptValue& key, ExceptionState& exceptionState, bool keyOnly)
190 exceptionState.throwDOMException(InvalidStateError, IDBDatabase::indexDeletedErrorMessage);
193 if (m_transaction->isFinished() || m_transaction->isFinishing()) {
194 exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::transactionFinishedErrorMessage);
197 if (!m_transaction->isActive()) {
198 exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::transactionInactiveErrorMessage);
202 IDBKeyRange* keyRange = IDBKeyRange::fromScriptValue(scriptState->executionContext(), key, exceptionState);
203 if (exceptionState.hadException())
206 exceptionState.throwDOMException(DataError, IDBDatabase::noKeyOrKeyRangeErrorMessage);
210 exceptionState.throwDOMException(InvalidStateError, IDBDatabase::databaseClosedErrorMessage);
214 IDBRequest* request = IDBRequest::create(scriptState, IDBAny::create(this), m_transaction.get());
215 backendDB()->get(m_transaction->id(), m_objectStore->id(), m_metadata.id, keyRange, keyOnly, WebIDBCallbacksImpl::create(request).leakPtr());
219 WebIDBDatabase* IDBIndex::backendDB() const
221 return m_transaction->backendDB();
224 bool IDBIndex::isDeleted() const
226 return m_deleted || m_objectStore->isDeleted();