Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / indexeddb / IDBIndex.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 "modules/indexeddb/IDBIndex.h"
28
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"
39
40 using blink::WebIDBCallbacks;
41 using blink::WebIDBCursor;
42 using blink::WebIDBDatabase;
43
44 namespace WebCore {
45
46 IDBIndex::IDBIndex(const IDBIndexMetadata& metadata, IDBObjectStore* objectStore, IDBTransaction* transaction)
47     : m_metadata(metadata)
48     , m_objectStore(objectStore)
49     , m_transaction(transaction)
50     , m_deleted(false)
51 {
52     ASSERT(m_objectStore);
53     ASSERT(m_transaction);
54     ASSERT(m_metadata.id != IDBIndexMetadata::InvalidId);
55     ScriptWrappable::init(this);
56 }
57
58 IDBIndex::~IDBIndex()
59 {
60 }
61
62 ScriptValue IDBIndex::keyPath(ExecutionContext* context) const
63 {
64     DOMRequestState requestState(context);
65     return idbAnyToScriptValue(&requestState, IDBAny::create(m_metadata.keyPath));
66 }
67
68 PassRefPtr<IDBRequest> IDBIndex::openCursor(ExecutionContext* context, const ScriptValue& range, const String& directionString, ExceptionState& exceptionState)
69 {
70     IDB_TRACE("IDBIndex::openCursor");
71     if (isDeleted()) {
72         exceptionState.throwDOMException(InvalidStateError, IDBDatabase::indexDeletedErrorMessage);
73         return 0;
74     }
75     if (m_transaction->isFinished()) {
76         exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::transactionFinishedErrorMessage);
77         return 0;
78     }
79     if (!m_transaction->isActive()) {
80         exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::transactionInactiveErrorMessage);
81         return 0;
82     }
83     WebIDBCursor::Direction direction = IDBCursor::stringToDirection(directionString, exceptionState);
84     if (exceptionState.hadException())
85         return 0;
86
87     RefPtr<IDBKeyRange> keyRange = IDBKeyRange::fromScriptValue(context, range, exceptionState);
88     if (exceptionState.hadException())
89         return 0;
90
91     return openCursor(context, keyRange.release(), direction);
92 }
93
94 PassRefPtr<IDBRequest> IDBIndex::openCursor(ExecutionContext* context, PassRefPtr<IDBKeyRange> keyRange, WebIDBCursor::Direction direction)
95 {
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());
99     return request;
100 }
101
102 PassRefPtr<IDBRequest> IDBIndex::count(ExecutionContext* context, const ScriptValue& range, ExceptionState& exceptionState)
103 {
104     IDB_TRACE("IDBIndex::count");
105     if (isDeleted()) {
106         exceptionState.throwDOMException(InvalidStateError, IDBDatabase::indexDeletedErrorMessage);
107         return 0;
108     }
109     if (m_transaction->isFinished()) {
110         exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::transactionFinishedErrorMessage);
111         return 0;
112     }
113     if (!m_transaction->isActive()) {
114         exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::transactionInactiveErrorMessage);
115         return 0;
116     }
117
118     RefPtr<IDBKeyRange> keyRange = IDBKeyRange::fromScriptValue(context, range, exceptionState);
119     if (exceptionState.hadException())
120         return 0;
121
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());
124     return request;
125 }
126
127 PassRefPtr<IDBRequest> IDBIndex::openKeyCursor(ExecutionContext* context, const ScriptValue& range, const String& directionString, ExceptionState& exceptionState)
128 {
129     IDB_TRACE("IDBIndex::openKeyCursor");
130     if (isDeleted()) {
131         exceptionState.throwDOMException(InvalidStateError, IDBDatabase::indexDeletedErrorMessage);
132         return 0;
133     }
134     if (m_transaction->isFinished()) {
135         exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::transactionFinishedErrorMessage);
136         return 0;
137     }
138     if (!m_transaction->isActive()) {
139         exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::transactionInactiveErrorMessage);
140         return 0;
141     }
142     WebIDBCursor::Direction direction = IDBCursor::stringToDirection(directionString, exceptionState);
143     if (exceptionState.hadException())
144         return 0;
145
146     RefPtr<IDBKeyRange> keyRange = IDBKeyRange::fromScriptValue(context, range, exceptionState);
147     if (exceptionState.hadException())
148         return 0;
149
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());
153     return request;
154 }
155
156 PassRefPtr<IDBRequest> IDBIndex::get(ExecutionContext* context, const ScriptValue& key, ExceptionState& exceptionState)
157 {
158     IDB_TRACE("IDBIndex::get");
159     return getInternal(context, key, exceptionState, false);
160 }
161
162 PassRefPtr<IDBRequest> IDBIndex::getKey(ExecutionContext* context, const ScriptValue& key, ExceptionState& exceptionState)
163 {
164     IDB_TRACE("IDBIndex::getKey");
165     return getInternal(context, key, exceptionState, true);
166 }
167
168 PassRefPtr<IDBRequest> IDBIndex::getInternal(ExecutionContext* context, const ScriptValue& key, ExceptionState& exceptionState, bool keyOnly)
169 {
170     if (isDeleted()) {
171         exceptionState.throwDOMException(InvalidStateError, IDBDatabase::indexDeletedErrorMessage);
172         return 0;
173     }
174     if (m_transaction->isFinished()) {
175         exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::transactionFinishedErrorMessage);
176         return 0;
177     }
178     if (!m_transaction->isActive()) {
179         exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::transactionInactiveErrorMessage);
180         return 0;
181     }
182
183     RefPtr<IDBKeyRange> keyRange = IDBKeyRange::fromScriptValue(context, key, exceptionState);
184     if (exceptionState.hadException())
185         return 0;
186     if (!keyRange) {
187         exceptionState.throwDOMException(DataError, IDBDatabase::noKeyOrKeyRangeErrorMessage);
188         return 0;
189     }
190
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());
193     return request;
194 }
195
196 WebIDBDatabase* IDBIndex::backendDB() const
197 {
198     return m_transaction->backendDB();
199 }
200
201 bool IDBIndex::isDeleted() const
202 {
203     return m_deleted || m_objectStore->isDeleted();
204 }
205
206 } // namespace WebCore