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/v8/ExceptionState.h"
30 #include "bindings/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/IDBTracing.h"
36 #include "modules/indexeddb/IDBTransaction.h"
37 #include "modules/indexeddb/WebIDBCallbacksImpl.h"
38 #include "public/platform/WebIDBKeyRange.h"
40 using blink::WebIDBCallbacks;
41 using blink::WebIDBCursor;
42 using blink::WebIDBDatabase;
46 IDBIndex::IDBIndex(const IDBIndexMetadata& metadata, IDBObjectStore* objectStore, IDBTransaction* transaction)
47 : m_metadata(metadata)
48 , m_objectStore(objectStore)
49 , m_transaction(transaction)
52 ASSERT(m_objectStore);
53 ASSERT(m_transaction);
54 ASSERT(m_metadata.id != IDBIndexMetadata::InvalidId);
55 ScriptWrappable::init(this);
62 ScriptValue IDBIndex::keyPath(ExecutionContext* context) const
64 DOMRequestState requestState(context);
65 return idbAnyToScriptValue(&requestState, IDBAny::create(m_metadata.keyPath));
68 PassRefPtr<IDBRequest> IDBIndex::openCursor(ExecutionContext* context, const ScriptValue& range, const String& directionString, ExceptionState& exceptionState)
70 IDB_TRACE("IDBIndex::openCursor");
72 exceptionState.throwDOMException(InvalidStateError, IDBDatabase::indexDeletedErrorMessage);
75 if (m_transaction->isFinished()) {
76 exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::transactionFinishedErrorMessage);
79 if (!m_transaction->isActive()) {
80 exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::transactionInactiveErrorMessage);
83 WebIDBCursor::Direction direction = IDBCursor::stringToDirection(directionString, exceptionState);
84 if (exceptionState.hadException())
87 RefPtr<IDBKeyRange> keyRange = IDBKeyRange::fromScriptValue(context, range, exceptionState);
88 if (exceptionState.hadException())
91 return openCursor(context, keyRange.release(), direction);
94 PassRefPtr<IDBRequest> IDBIndex::openCursor(ExecutionContext* context, PassRefPtr<IDBKeyRange> keyRange, WebIDBCursor::Direction direction)
96 RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
97 request->setCursorDetails(IndexedDB::CursorKeyAndValue, direction);
98 backendDB()->openCursor(m_transaction->id(), m_objectStore->id(), m_metadata.id, keyRange, direction, false, WebIDBDatabase::NormalTask, WebIDBCallbacksImpl::create(request).leakPtr());
102 PassRefPtr<IDBRequest> IDBIndex::count(ExecutionContext* context, const ScriptValue& range, ExceptionState& exceptionState)
104 IDB_TRACE("IDBIndex::count");
106 exceptionState.throwDOMException(InvalidStateError, IDBDatabase::indexDeletedErrorMessage);
109 if (m_transaction->isFinished()) {
110 exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::transactionFinishedErrorMessage);
113 if (!m_transaction->isActive()) {
114 exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::transactionInactiveErrorMessage);
118 RefPtr<IDBKeyRange> keyRange = IDBKeyRange::fromScriptValue(context, range, exceptionState);
119 if (exceptionState.hadException())
122 RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
123 backendDB()->count(m_transaction->id(), m_objectStore->id(), m_metadata.id, keyRange.release(), WebIDBCallbacksImpl::create(request).leakPtr());
127 PassRefPtr<IDBRequest> IDBIndex::openKeyCursor(ExecutionContext* context, const ScriptValue& range, const String& directionString, ExceptionState& exceptionState)
129 IDB_TRACE("IDBIndex::openKeyCursor");
131 exceptionState.throwDOMException(InvalidStateError, IDBDatabase::indexDeletedErrorMessage);
134 if (m_transaction->isFinished()) {
135 exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::transactionFinishedErrorMessage);
138 if (!m_transaction->isActive()) {
139 exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::transactionInactiveErrorMessage);
142 WebIDBCursor::Direction direction = IDBCursor::stringToDirection(directionString, exceptionState);
143 if (exceptionState.hadException())
146 RefPtr<IDBKeyRange> keyRange = IDBKeyRange::fromScriptValue(context, range, exceptionState);
147 if (exceptionState.hadException())
150 RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
151 request->setCursorDetails(IndexedDB::CursorKeyOnly, direction);
152 backendDB()->openCursor(m_transaction->id(), m_objectStore->id(), m_metadata.id, keyRange.release(), direction, true, WebIDBDatabase::NormalTask, WebIDBCallbacksImpl::create(request).leakPtr());
156 PassRefPtr<IDBRequest> IDBIndex::get(ExecutionContext* context, const ScriptValue& key, ExceptionState& exceptionState)
158 IDB_TRACE("IDBIndex::get");
159 return getInternal(context, key, exceptionState, false);
162 PassRefPtr<IDBRequest> IDBIndex::getKey(ExecutionContext* context, const ScriptValue& key, ExceptionState& exceptionState)
164 IDB_TRACE("IDBIndex::getKey");
165 return getInternal(context, key, exceptionState, true);
168 PassRefPtr<IDBRequest> IDBIndex::getInternal(ExecutionContext* context, const ScriptValue& key, ExceptionState& exceptionState, bool keyOnly)
171 exceptionState.throwDOMException(InvalidStateError, IDBDatabase::indexDeletedErrorMessage);
174 if (m_transaction->isFinished()) {
175 exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::transactionFinishedErrorMessage);
178 if (!m_transaction->isActive()) {
179 exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::transactionInactiveErrorMessage);
183 RefPtr<IDBKeyRange> keyRange = IDBKeyRange::fromScriptValue(context, key, exceptionState);
184 if (exceptionState.hadException())
187 exceptionState.throwDOMException(DataError, IDBDatabase::noKeyOrKeyRangeErrorMessage);
191 RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
192 backendDB()->get(m_transaction->id(), m_objectStore->id(), m_metadata.id, keyRange.release(), keyOnly, WebIDBCallbacksImpl::create(request).leakPtr());
196 WebIDBDatabase* IDBIndex::backendDB() const
198 return m_transaction->backendDB();
201 bool IDBIndex::isDeleted() const
203 return m_deleted || m_objectStore->isDeleted();
206 } // namespace WebCore