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"
9 #include "content/child/indexed_db/indexed_db_dispatcher.h"
10 #include "content/child/indexed_db/indexed_db_key_builders.h"
11 #include "content/child/thread_safe_sender.h"
12 #include "content/child/worker_task_runner.h"
13 #include "content/common/indexed_db/indexed_db_messages.h"
14 #include "third_party/WebKit/public/platform/WebBlobInfo.h"
15 #include "third_party/WebKit/public/platform/WebIDBKeyPath.h"
16 #include "third_party/WebKit/public/platform/WebIDBMetadata.h"
17 #include "third_party/WebKit/public/platform/WebString.h"
18 #include "third_party/WebKit/public/platform/WebVector.h"
20 using blink::WebBlobInfo;
21 using blink::WebIDBCallbacks;
22 using blink::WebIDBCursor;
23 using blink::WebIDBDatabase;
24 using blink::WebIDBDatabaseCallbacks;
25 using blink::WebIDBMetadata;
26 using blink::WebIDBKey;
27 using blink::WebIDBKeyPath;
28 using blink::WebIDBKeyRange;
29 using blink::WebString;
30 using blink::WebVector;
34 WebIDBDatabaseImpl::WebIDBDatabaseImpl(int32 ipc_database_id,
35 int32 ipc_database_callbacks_id,
36 ThreadSafeSender* thread_safe_sender)
37 : ipc_database_id_(ipc_database_id),
38 ipc_database_callbacks_id_(ipc_database_callbacks_id),
39 thread_safe_sender_(thread_safe_sender) {}
41 WebIDBDatabaseImpl::~WebIDBDatabaseImpl() {
42 // It's not possible for there to be pending callbacks that address this
43 // object since inside WebKit, they hold a reference to the object which owns
44 // this object. But, if that ever changed, then we'd need to invalidate
46 thread_safe_sender_->Send(
47 new IndexedDBHostMsg_DatabaseDestroyed(ipc_database_id_));
48 IndexedDBDispatcher* dispatcher =
49 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get());
50 dispatcher->DatabaseDestroyed(ipc_database_id_);
53 void WebIDBDatabaseImpl::createObjectStore(long long transaction_id,
54 long long object_store_id,
55 const WebString& name,
56 const WebIDBKeyPath& key_path,
57 bool auto_increment) {
58 IndexedDBHostMsg_DatabaseCreateObjectStore_Params params;
59 params.ipc_database_id = ipc_database_id_;
60 params.transaction_id = transaction_id;
61 params.object_store_id = object_store_id;
63 params.key_path = IndexedDBKeyPathBuilder::Build(key_path);
64 params.auto_increment = auto_increment;
66 thread_safe_sender_->Send(
67 new IndexedDBHostMsg_DatabaseCreateObjectStore(params));
70 void WebIDBDatabaseImpl::deleteObjectStore(long long transaction_id,
71 long long object_store_id) {
72 thread_safe_sender_->Send(new IndexedDBHostMsg_DatabaseDeleteObjectStore(
73 ipc_database_id_, transaction_id, object_store_id));
76 void WebIDBDatabaseImpl::createTransaction(
77 long long transaction_id,
78 WebIDBDatabaseCallbacks* callbacks,
79 const WebVector<long long>& object_store_ids,
80 WebIDBDatabase::TransactionMode mode) {
81 IndexedDBDispatcher* dispatcher =
82 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get());
83 dispatcher->RequestIDBDatabaseCreateTransaction(
84 ipc_database_id_, transaction_id, callbacks, object_store_ids, mode);
87 void WebIDBDatabaseImpl::close() {
88 IndexedDBDispatcher* dispatcher =
89 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get());
90 dispatcher->RequestIDBDatabaseClose(ipc_database_id_,
91 ipc_database_callbacks_id_);
94 void WebIDBDatabaseImpl::get(long long transaction_id,
95 long long object_store_id,
97 const WebIDBKeyRange& key_range,
99 WebIDBCallbacks* callbacks) {
100 IndexedDBDispatcher* dispatcher =
101 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get());
102 dispatcher->RequestIDBDatabaseGet(ipc_database_id_,
106 IndexedDBKeyRangeBuilder::Build(key_range),
111 // TODO(ericu): Remove this once it's obsolete. It's only here for the
112 // three-sided-patch dance.
113 void WebIDBDatabaseImpl::put(long long transaction_id,
114 long long object_store_id,
115 const blink::WebData& value,
116 const WebIDBKey& key,
118 WebIDBCallbacks* callbacks,
119 const WebVector<long long>& web_index_ids,
120 const WebVector<WebIndexKeys>& web_index_keys) {
121 IndexedDBDispatcher* dispatcher =
122 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get());
123 const blink::WebVector<WebBlobInfo> web_blob_info;
124 dispatcher->RequestIDBDatabasePut(ipc_database_id_,
129 IndexedDBKeyBuilder::Build(key),
136 void WebIDBDatabaseImpl::put(long long transaction_id,
137 long long object_store_id,
138 const blink::WebData& value,
139 const blink::WebVector<WebBlobInfo>& web_blob_info,
140 const WebIDBKey& key,
142 WebIDBCallbacks* callbacks,
143 const WebVector<long long>& web_index_ids,
144 const WebVector<WebIndexKeys>& web_index_keys) {
145 IndexedDBDispatcher* dispatcher =
146 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get());
147 dispatcher->RequestIDBDatabasePut(ipc_database_id_,
152 IndexedDBKeyBuilder::Build(key),
159 void WebIDBDatabaseImpl::setIndexKeys(
160 long long transaction_id,
161 long long object_store_id,
162 const WebIDBKey& primary_key,
163 const WebVector<long long>& index_ids,
164 const WebVector<WebIndexKeys>& index_keys) {
165 IndexedDBHostMsg_DatabaseSetIndexKeys_Params params;
166 params.ipc_database_id = ipc_database_id_;
167 params.transaction_id = transaction_id;
168 params.object_store_id = object_store_id;
169 params.primary_key = IndexedDBKeyBuilder::Build(primary_key);
171 DCHECK_EQ(index_ids.size(), index_keys.size());
172 params.index_keys.resize(index_ids.size());
173 for (size_t i = 0, len = index_ids.size(); i < len; ++i) {
174 params.index_keys[i].first = index_ids[i];
175 params.index_keys[i].second.resize(index_keys[i].size());
176 for (size_t j = 0; j < index_keys[i].size(); ++j) {
177 params.index_keys[i].second[j] =
178 IndexedDBKey(IndexedDBKeyBuilder::Build(index_keys[i][j]));
182 thread_safe_sender_->Send(new IndexedDBHostMsg_DatabaseSetIndexKeys(params));
185 void WebIDBDatabaseImpl::setIndexesReady(
186 long long transaction_id,
187 long long object_store_id,
188 const WebVector<long long>& web_index_ids) {
189 std::vector<int64> index_ids(web_index_ids.data(),
190 web_index_ids.data() + web_index_ids.size());
191 thread_safe_sender_->Send(new IndexedDBHostMsg_DatabaseSetIndexesReady(
192 ipc_database_id_, transaction_id, object_store_id, index_ids));
195 void WebIDBDatabaseImpl::openCursor(long long transaction_id,
196 long long object_store_id,
198 const WebIDBKeyRange& key_range,
199 WebIDBCursor::Direction direction,
202 WebIDBCallbacks* callbacks) {
203 IndexedDBDispatcher* dispatcher =
204 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get());
205 dispatcher->RequestIDBDatabaseOpenCursor(
210 IndexedDBKeyRangeBuilder::Build(key_range),
217 void WebIDBDatabaseImpl::count(long long transaction_id,
218 long long object_store_id,
220 const WebIDBKeyRange& key_range,
221 WebIDBCallbacks* callbacks) {
222 IndexedDBDispatcher* dispatcher =
223 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get());
224 dispatcher->RequestIDBDatabaseCount(
229 IndexedDBKeyRangeBuilder::Build(key_range),
233 void WebIDBDatabaseImpl::deleteRange(long long transaction_id,
234 long long object_store_id,
235 const WebIDBKeyRange& key_range,
236 WebIDBCallbacks* callbacks) {
237 IndexedDBDispatcher* dispatcher =
238 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get());
239 dispatcher->RequestIDBDatabaseDeleteRange(
243 IndexedDBKeyRangeBuilder::Build(key_range),
247 void WebIDBDatabaseImpl::clear(long long transaction_id,
248 long long object_store_id,
249 WebIDBCallbacks* callbacks) {
250 IndexedDBDispatcher* dispatcher =
251 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get());
252 dispatcher->RequestIDBDatabaseClear(
253 ipc_database_id_, transaction_id, object_store_id, callbacks);
256 void WebIDBDatabaseImpl::createIndex(long long transaction_id,
257 long long object_store_id,
259 const WebString& name,
260 const WebIDBKeyPath& key_path,
263 IndexedDBHostMsg_DatabaseCreateIndex_Params params;
264 params.ipc_database_id = ipc_database_id_;
265 params.transaction_id = transaction_id;
266 params.object_store_id = object_store_id;
267 params.index_id = index_id;
269 params.key_path = IndexedDBKeyPathBuilder::Build(key_path);
270 params.unique = unique;
271 params.multi_entry = multi_entry;
273 thread_safe_sender_->Send(new IndexedDBHostMsg_DatabaseCreateIndex(params));
276 void WebIDBDatabaseImpl::deleteIndex(long long transaction_id,
277 long long object_store_id,
278 long long index_id) {
279 thread_safe_sender_->Send(new IndexedDBHostMsg_DatabaseDeleteIndex(
280 ipc_database_id_, transaction_id, object_store_id, index_id));
283 void WebIDBDatabaseImpl::abort(long long transaction_id) {
284 thread_safe_sender_->Send(
285 new IndexedDBHostMsg_DatabaseAbort(ipc_database_id_, transaction_id));
288 void WebIDBDatabaseImpl::commit(long long transaction_id) {
289 thread_safe_sender_->Send(
290 new IndexedDBHostMsg_DatabaseCommit(ipc_database_id_, transaction_id));
293 void WebIDBDatabaseImpl::ackReceivedBlobs(const WebVector<WebString>& uuids) {
294 DCHECK(uuids.size());
295 std::vector<std::string> param(uuids.size());
296 for (size_t i = 0; i < uuids.size(); ++i)
297 param[i] = uuids[i].latin1().data();
298 thread_safe_sender_->Send(new IndexedDBHostMsg_AckReceivedBlobs(param));
301 } // namespace content