1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/child/indexed_db/webidbdatabase_impl.h"
10 #include "content/child/indexed_db/indexed_db_dispatcher.h"
11 #include "content/child/indexed_db/indexed_db_key_builders.h"
12 #include "content/child/thread_safe_sender.h"
13 #include "content/child/worker_task_runner.h"
14 #include "content/common/indexed_db/indexed_db_messages.h"
15 #include "third_party/WebKit/public/platform/WebBlobInfo.h"
16 #include "third_party/WebKit/public/platform/WebString.h"
17 #include "third_party/WebKit/public/platform/WebVector.h"
18 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBKeyPath.h"
19 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBMetadata.h"
21 using blink::WebBlobInfo
;
22 using blink::WebIDBCallbacks
;
23 using blink::WebIDBCursor
;
24 using blink::WebIDBDatabase
;
25 using blink::WebIDBDatabaseCallbacks
;
26 using blink::WebIDBMetadata
;
27 using blink::WebIDBKey
;
28 using blink::WebIDBKeyPath
;
29 using blink::WebIDBKeyRange
;
30 using blink::WebString
;
31 using blink::WebVector
;
35 WebIDBDatabaseImpl::WebIDBDatabaseImpl(int32 ipc_database_id
,
36 int32 ipc_database_callbacks_id
,
37 ThreadSafeSender
* thread_safe_sender
)
38 : ipc_database_id_(ipc_database_id
),
39 ipc_database_callbacks_id_(ipc_database_callbacks_id
),
40 thread_safe_sender_(thread_safe_sender
) {}
42 WebIDBDatabaseImpl::~WebIDBDatabaseImpl() {
43 // It's not possible for there to be pending callbacks that address this
44 // object since inside WebKit, they hold a reference to the object which owns
45 // this object. But, if that ever changed, then we'd need to invalidate
47 thread_safe_sender_
->Send(
48 new IndexedDBHostMsg_DatabaseDestroyed(ipc_database_id_
));
49 IndexedDBDispatcher
* dispatcher
=
50 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_
.get());
51 dispatcher
->DatabaseDestroyed(ipc_database_id_
);
54 void WebIDBDatabaseImpl::createObjectStore(long long transaction_id
,
55 long long object_store_id
,
56 const WebString
& name
,
57 const WebIDBKeyPath
& key_path
,
58 bool auto_increment
) {
59 IndexedDBHostMsg_DatabaseCreateObjectStore_Params params
;
60 params
.ipc_database_id
= ipc_database_id_
;
61 params
.transaction_id
= transaction_id
;
62 params
.object_store_id
= object_store_id
;
64 params
.key_path
= IndexedDBKeyPathBuilder::Build(key_path
);
65 params
.auto_increment
= auto_increment
;
67 thread_safe_sender_
->Send(
68 new IndexedDBHostMsg_DatabaseCreateObjectStore(params
));
71 void WebIDBDatabaseImpl::deleteObjectStore(long long transaction_id
,
72 long long object_store_id
) {
73 thread_safe_sender_
->Send(new IndexedDBHostMsg_DatabaseDeleteObjectStore(
74 ipc_database_id_
, transaction_id
, object_store_id
));
77 void WebIDBDatabaseImpl::createTransaction(
78 long long transaction_id
,
79 WebIDBDatabaseCallbacks
* callbacks
,
80 const WebVector
<long long>& object_store_ids
,
81 blink::WebIDBTransactionMode mode
) {
82 IndexedDBDispatcher
* dispatcher
=
83 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_
.get());
84 dispatcher
->RequestIDBDatabaseCreateTransaction(
85 ipc_database_id_
, transaction_id
, callbacks
, object_store_ids
, mode
);
88 void WebIDBDatabaseImpl::close() {
89 IndexedDBDispatcher
* dispatcher
=
90 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_
.get());
91 dispatcher
->RequestIDBDatabaseClose(ipc_database_id_
,
92 ipc_database_callbacks_id_
);
95 void WebIDBDatabaseImpl::versionChangeIgnored() {
96 IndexedDBDispatcher
* dispatcher
=
97 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_
.get());
98 dispatcher
->NotifyIDBDatabaseVersionChangeIgnored(ipc_database_id_
);
101 void WebIDBDatabaseImpl::get(long long transaction_id
,
102 long long object_store_id
,
104 const WebIDBKeyRange
& key_range
,
106 WebIDBCallbacks
* callbacks
) {
107 IndexedDBDispatcher
* dispatcher
=
108 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_
.get());
109 dispatcher
->RequestIDBDatabaseGet(ipc_database_id_
,
113 IndexedDBKeyRangeBuilder::Build(key_range
),
118 void WebIDBDatabaseImpl::getAll(long long transaction_id
,
119 long long object_store_id
,
121 const WebIDBKeyRange
& key_range
,
124 WebIDBCallbacks
* callbacks
) {
125 IndexedDBDispatcher
* dispatcher
=
126 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_
.get());
127 dispatcher
->RequestIDBDatabaseGetAll(
128 ipc_database_id_
, transaction_id
, object_store_id
, index_id
,
129 IndexedDBKeyRangeBuilder::Build(key_range
), key_only
, max_count
,
133 void WebIDBDatabaseImpl::put(long long transaction_id
,
134 long long object_store_id
,
135 const blink::WebData
& value
,
136 const blink::WebVector
<WebBlobInfo
>& web_blob_info
,
137 const WebIDBKey
& key
,
138 blink::WebIDBPutMode put_mode
,
139 WebIDBCallbacks
* callbacks
,
140 const WebVector
<long long>& web_index_ids
,
141 const WebVector
<WebIndexKeys
>& web_index_keys
) {
142 IndexedDBDispatcher
* dispatcher
=
143 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_
.get());
144 dispatcher
->RequestIDBDatabasePut(ipc_database_id_
,
149 IndexedDBKeyBuilder::Build(key
),
156 void WebIDBDatabaseImpl::setIndexKeys(
157 long long transaction_id
,
158 long long object_store_id
,
159 const WebIDBKey
& primary_key
,
160 const WebVector
<long long>& index_ids
,
161 const WebVector
<WebIndexKeys
>& index_keys
) {
162 IndexedDBHostMsg_DatabaseSetIndexKeys_Params params
;
163 params
.ipc_database_id
= ipc_database_id_
;
164 params
.transaction_id
= transaction_id
;
165 params
.object_store_id
= object_store_id
;
166 params
.primary_key
= IndexedDBKeyBuilder::Build(primary_key
);
168 DCHECK_EQ(index_ids
.size(), index_keys
.size());
169 params
.index_keys
.resize(index_ids
.size());
170 for (size_t i
= 0, len
= index_ids
.size(); i
< len
; ++i
) {
171 params
.index_keys
[i
].first
= index_ids
[i
];
172 params
.index_keys
[i
].second
.resize(index_keys
[i
].size());
173 for (size_t j
= 0; j
< index_keys
[i
].size(); ++j
) {
174 params
.index_keys
[i
].second
[j
] =
175 IndexedDBKey(IndexedDBKeyBuilder::Build(index_keys
[i
][j
]));
179 thread_safe_sender_
->Send(new IndexedDBHostMsg_DatabaseSetIndexKeys(params
));
182 void WebIDBDatabaseImpl::setIndexesReady(
183 long long transaction_id
,
184 long long object_store_id
,
185 const WebVector
<long long>& web_index_ids
) {
186 std::vector
<int64
> index_ids(web_index_ids
.data(),
187 web_index_ids
.data() + web_index_ids
.size());
188 thread_safe_sender_
->Send(new IndexedDBHostMsg_DatabaseSetIndexesReady(
189 ipc_database_id_
, transaction_id
, object_store_id
, index_ids
));
192 void WebIDBDatabaseImpl::openCursor(long long transaction_id
,
193 long long object_store_id
,
195 const WebIDBKeyRange
& key_range
,
196 blink::WebIDBCursorDirection direction
,
198 blink::WebIDBTaskType task_type
,
199 WebIDBCallbacks
* callbacks
) {
200 IndexedDBDispatcher
* dispatcher
=
201 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_
.get());
202 dispatcher
->RequestIDBDatabaseOpenCursor(
207 IndexedDBKeyRangeBuilder::Build(key_range
),
214 void WebIDBDatabaseImpl::count(long long transaction_id
,
215 long long object_store_id
,
217 const WebIDBKeyRange
& key_range
,
218 WebIDBCallbacks
* callbacks
) {
219 IndexedDBDispatcher
* dispatcher
=
220 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_
.get());
221 dispatcher
->RequestIDBDatabaseCount(
226 IndexedDBKeyRangeBuilder::Build(key_range
),
230 void WebIDBDatabaseImpl::deleteRange(long long transaction_id
,
231 long long object_store_id
,
232 const WebIDBKeyRange
& key_range
,
233 WebIDBCallbacks
* callbacks
) {
234 IndexedDBDispatcher
* dispatcher
=
235 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_
.get());
236 dispatcher
->RequestIDBDatabaseDeleteRange(
240 IndexedDBKeyRangeBuilder::Build(key_range
),
244 void WebIDBDatabaseImpl::clear(long long transaction_id
,
245 long long object_store_id
,
246 WebIDBCallbacks
* callbacks
) {
247 IndexedDBDispatcher
* dispatcher
=
248 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_
.get());
249 dispatcher
->RequestIDBDatabaseClear(
250 ipc_database_id_
, transaction_id
, object_store_id
, callbacks
);
253 void WebIDBDatabaseImpl::createIndex(long long transaction_id
,
254 long long object_store_id
,
256 const WebString
& name
,
257 const WebIDBKeyPath
& key_path
,
260 IndexedDBHostMsg_DatabaseCreateIndex_Params params
;
261 params
.ipc_database_id
= ipc_database_id_
;
262 params
.transaction_id
= transaction_id
;
263 params
.object_store_id
= object_store_id
;
264 params
.index_id
= index_id
;
266 params
.key_path
= IndexedDBKeyPathBuilder::Build(key_path
);
267 params
.unique
= unique
;
268 params
.multi_entry
= multi_entry
;
270 thread_safe_sender_
->Send(new IndexedDBHostMsg_DatabaseCreateIndex(params
));
273 void WebIDBDatabaseImpl::deleteIndex(long long transaction_id
,
274 long long object_store_id
,
275 long long index_id
) {
276 thread_safe_sender_
->Send(new IndexedDBHostMsg_DatabaseDeleteIndex(
277 ipc_database_id_
, transaction_id
, object_store_id
, index_id
));
280 void WebIDBDatabaseImpl::abort(long long transaction_id
) {
281 thread_safe_sender_
->Send(
282 new IndexedDBHostMsg_DatabaseAbort(ipc_database_id_
, transaction_id
));
285 void WebIDBDatabaseImpl::commit(long long transaction_id
) {
286 thread_safe_sender_
->Send(
287 new IndexedDBHostMsg_DatabaseCommit(ipc_database_id_
, transaction_id
));
290 void WebIDBDatabaseImpl::ackReceivedBlobs(const WebVector
<WebString
>& uuids
) {
291 DCHECK(uuids
.size());
292 std::vector
<std::string
> param(uuids
.size());
293 for (size_t i
= 0; i
< uuids
.size(); ++i
)
294 param
[i
] = uuids
[i
].latin1().data();
295 thread_safe_sender_
->Send(new IndexedDBHostMsg_AckReceivedBlobs(param
));
298 } // namespace content