IndexedDB: Added IDBIndex.getAll() implementation.
[chromium-blink-merge.git] / content / child / indexed_db / indexed_db_dispatcher.cc
blob87aa2a4022379b09a7cca216e3a376c05348895b
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/indexed_db_dispatcher.h"
7 #include <utility>
9 #include "base/format_macros.h"
10 #include "base/lazy_instance.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/threading/thread_local.h"
13 #include "content/child/indexed_db/indexed_db_key_builders.h"
14 #include "content/child/indexed_db/webidbcursor_impl.h"
15 #include "content/child/indexed_db/webidbdatabase_impl.h"
16 #include "content/child/thread_safe_sender.h"
17 #include "content/common/indexed_db/indexed_db_constants.h"
18 #include "content/common/indexed_db/indexed_db_messages.h"
19 #include "ipc/ipc_channel.h"
20 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseCallbacks.h"
21 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseError.h"
22 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseException.h"
23 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBValue.h"
25 using blink::WebBlobInfo;
26 using blink::WebData;
27 using blink::WebIDBCallbacks;
28 using blink::WebIDBCursor;
29 using blink::WebIDBDatabase;
30 using blink::WebIDBDatabaseCallbacks;
31 using blink::WebIDBDatabaseError;
32 using blink::WebIDBKey;
33 using blink::WebIDBMetadata;
34 using blink::WebIDBValue;
35 using blink::WebString;
36 using blink::WebVector;
37 using base::ThreadLocalPointer;
39 namespace content {
40 static base::LazyInstance<ThreadLocalPointer<IndexedDBDispatcher> >::Leaky
41 g_idb_dispatcher_tls = LAZY_INSTANCE_INITIALIZER;
43 namespace {
45 IndexedDBDispatcher* const kHasBeenDeleted =
46 reinterpret_cast<IndexedDBDispatcher*>(0x1);
48 } // unnamed namespace
50 const size_t kMaxIDBValueSizeInBytes =
51 IPC::Channel::kMaximumMessageSize - kMaxIDBMessageOverhead;
53 IndexedDBDispatcher::IndexedDBDispatcher(ThreadSafeSender* thread_safe_sender)
54 : thread_safe_sender_(thread_safe_sender) {
55 g_idb_dispatcher_tls.Pointer()->Set(this);
58 IndexedDBDispatcher::~IndexedDBDispatcher() {
59 // Clear any pending callbacks - which may result in dispatch requests -
60 // before marking the dispatcher as deleted.
61 pending_callbacks_.Clear();
62 pending_database_callbacks_.Clear();
64 DCHECK(pending_callbacks_.IsEmpty());
65 DCHECK(pending_database_callbacks_.IsEmpty());
67 g_idb_dispatcher_tls.Pointer()->Set(kHasBeenDeleted);
70 IndexedDBDispatcher* IndexedDBDispatcher::ThreadSpecificInstance(
71 ThreadSafeSender* thread_safe_sender) {
72 if (g_idb_dispatcher_tls.Pointer()->Get() == kHasBeenDeleted) {
73 NOTREACHED() << "Re-instantiating TLS IndexedDBDispatcher.";
74 g_idb_dispatcher_tls.Pointer()->Set(NULL);
76 if (g_idb_dispatcher_tls.Pointer()->Get())
77 return g_idb_dispatcher_tls.Pointer()->Get();
79 IndexedDBDispatcher* dispatcher = new IndexedDBDispatcher(thread_safe_sender);
80 if (WorkerTaskRunner::Instance()->CurrentWorkerId())
81 WorkerTaskRunner::Instance()->AddStopObserver(dispatcher);
82 return dispatcher;
85 void IndexedDBDispatcher::OnWorkerRunLoopStopped() { delete this; }
87 WebIDBMetadata IndexedDBDispatcher::ConvertMetadata(
88 const IndexedDBDatabaseMetadata& idb_metadata) {
89 WebIDBMetadata web_metadata;
90 web_metadata.id = idb_metadata.id;
91 web_metadata.name = idb_metadata.name;
92 web_metadata.version = idb_metadata.version;
93 web_metadata.intVersion = idb_metadata.int_version;
94 web_metadata.maxObjectStoreId = idb_metadata.max_object_store_id;
95 web_metadata.objectStores =
96 WebVector<WebIDBMetadata::ObjectStore>(idb_metadata.object_stores.size());
98 for (size_t i = 0; i < idb_metadata.object_stores.size(); ++i) {
99 const IndexedDBObjectStoreMetadata& idb_store_metadata =
100 idb_metadata.object_stores[i];
101 WebIDBMetadata::ObjectStore& web_store_metadata =
102 web_metadata.objectStores[i];
104 web_store_metadata.id = idb_store_metadata.id;
105 web_store_metadata.name = idb_store_metadata.name;
106 web_store_metadata.keyPath =
107 WebIDBKeyPathBuilder::Build(idb_store_metadata.key_path);
108 web_store_metadata.autoIncrement = idb_store_metadata.auto_increment;
109 web_store_metadata.maxIndexId = idb_store_metadata.max_index_id;
110 web_store_metadata.indexes =
111 WebVector<WebIDBMetadata::Index>(idb_store_metadata.indexes.size());
113 for (size_t j = 0; j < idb_store_metadata.indexes.size(); ++j) {
114 const IndexedDBIndexMetadata& idb_index_metadata =
115 idb_store_metadata.indexes[j];
116 WebIDBMetadata::Index& web_index_metadata = web_store_metadata.indexes[j];
118 web_index_metadata.id = idb_index_metadata.id;
119 web_index_metadata.name = idb_index_metadata.name;
120 web_index_metadata.keyPath =
121 WebIDBKeyPathBuilder::Build(idb_index_metadata.key_path);
122 web_index_metadata.unique = idb_index_metadata.unique;
123 web_index_metadata.multiEntry = idb_index_metadata.multi_entry;
127 return web_metadata;
130 void IndexedDBDispatcher::OnMessageReceived(const IPC::Message& msg) {
131 bool handled = true;
132 IPC_BEGIN_MESSAGE_MAP(IndexedDBDispatcher, msg)
133 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksSuccessIDBCursor,
134 OnSuccessOpenCursor)
135 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksSuccessCursorAdvance,
136 OnSuccessCursorContinue)
137 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksSuccessCursorContinue,
138 OnSuccessCursorContinue)
139 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksSuccessCursorPrefetch,
140 OnSuccessCursorPrefetch)
141 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksSuccessIDBDatabase,
142 OnSuccessIDBDatabase)
143 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksSuccessIndexedDBKey,
144 OnSuccessIndexedDBKey)
145 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksSuccessStringList,
146 OnSuccessStringList)
147 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksSuccessArray, OnSuccessArray)
148 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksSuccessValue, OnSuccessValue)
149 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksSuccessInteger, OnSuccessInteger)
150 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksSuccessUndefined,
151 OnSuccessUndefined)
152 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksError, OnError)
153 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksIntBlocked, OnIntBlocked)
154 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksUpgradeNeeded, OnUpgradeNeeded)
155 IPC_MESSAGE_HANDLER(IndexedDBMsg_DatabaseCallbacksForcedClose,
156 OnForcedClose)
157 IPC_MESSAGE_HANDLER(IndexedDBMsg_DatabaseCallbacksIntVersionChange,
158 OnIntVersionChange)
159 IPC_MESSAGE_HANDLER(IndexedDBMsg_DatabaseCallbacksAbort, OnAbort)
160 IPC_MESSAGE_HANDLER(IndexedDBMsg_DatabaseCallbacksComplete, OnComplete)
161 IPC_MESSAGE_UNHANDLED(handled = false)
162 IPC_END_MESSAGE_MAP()
163 // If a message gets here, IndexedDBMessageFilter already determined that it
164 // is an IndexedDB message.
165 DCHECK(handled) << "Didn't handle a message defined at line "
166 << IPC_MESSAGE_ID_LINE(msg.type());
169 bool IndexedDBDispatcher::Send(IPC::Message* msg) {
170 return thread_safe_sender_->Send(msg);
173 void IndexedDBDispatcher::RequestIDBCursorAdvance(
174 unsigned long count,
175 WebIDBCallbacks* callbacks_ptr,
176 int32 ipc_cursor_id,
177 int64 transaction_id) {
178 // Reset all cursor prefetch caches except for this cursor.
179 ResetCursorPrefetchCaches(transaction_id, ipc_cursor_id);
181 scoped_ptr<WebIDBCallbacks> callbacks(callbacks_ptr);
183 int32 ipc_callbacks_id = pending_callbacks_.Add(callbacks.release());
184 Send(new IndexedDBHostMsg_CursorAdvance(
185 ipc_cursor_id, CurrentWorkerId(), ipc_callbacks_id, count));
188 void IndexedDBDispatcher::RequestIDBCursorContinue(
189 const IndexedDBKey& key,
190 const IndexedDBKey& primary_key,
191 WebIDBCallbacks* callbacks_ptr,
192 int32 ipc_cursor_id,
193 int64 transaction_id) {
194 // Reset all cursor prefetch caches except for this cursor.
195 ResetCursorPrefetchCaches(transaction_id, ipc_cursor_id);
197 scoped_ptr<WebIDBCallbacks> callbacks(callbacks_ptr);
199 int32 ipc_callbacks_id = pending_callbacks_.Add(callbacks.release());
200 Send(new IndexedDBHostMsg_CursorContinue(
201 ipc_cursor_id, CurrentWorkerId(), ipc_callbacks_id, key, primary_key));
204 void IndexedDBDispatcher::RequestIDBCursorPrefetch(
205 int n,
206 WebIDBCallbacks* callbacks_ptr,
207 int32 ipc_cursor_id) {
208 scoped_ptr<WebIDBCallbacks> callbacks(callbacks_ptr);
210 int32 ipc_callbacks_id = pending_callbacks_.Add(callbacks.release());
211 Send(new IndexedDBHostMsg_CursorPrefetch(
212 ipc_cursor_id, CurrentWorkerId(), ipc_callbacks_id, n));
215 void IndexedDBDispatcher::RequestIDBCursorPrefetchReset(int used_prefetches,
216 int unused_prefetches,
217 int32 ipc_cursor_id) {
218 Send(new IndexedDBHostMsg_CursorPrefetchReset(
219 ipc_cursor_id, used_prefetches, unused_prefetches));
222 void IndexedDBDispatcher::RequestIDBFactoryOpen(
223 const base::string16& name,
224 int64 version,
225 int64 transaction_id,
226 WebIDBCallbacks* callbacks_ptr,
227 WebIDBDatabaseCallbacks* database_callbacks_ptr,
228 const std::string& database_identifier) {
229 scoped_ptr<WebIDBCallbacks> callbacks(callbacks_ptr);
230 scoped_ptr<WebIDBDatabaseCallbacks> database_callbacks(
231 database_callbacks_ptr);
233 IndexedDBHostMsg_FactoryOpen_Params params;
234 params.ipc_thread_id = CurrentWorkerId();
235 params.ipc_callbacks_id = pending_callbacks_.Add(callbacks.release());
236 params.ipc_database_callbacks_id =
237 pending_database_callbacks_.Add(database_callbacks.release());
238 params.database_identifier = database_identifier;
239 params.name = name;
240 params.transaction_id = transaction_id;
241 params.version = version;
242 Send(new IndexedDBHostMsg_FactoryOpen(params));
245 void IndexedDBDispatcher::RequestIDBFactoryGetDatabaseNames(
246 WebIDBCallbacks* callbacks_ptr,
247 const std::string& database_identifier) {
248 scoped_ptr<WebIDBCallbacks> callbacks(callbacks_ptr);
250 IndexedDBHostMsg_FactoryGetDatabaseNames_Params params;
251 params.ipc_thread_id = CurrentWorkerId();
252 params.ipc_callbacks_id = pending_callbacks_.Add(callbacks.release());
253 params.database_identifier = database_identifier;
254 Send(new IndexedDBHostMsg_FactoryGetDatabaseNames(params));
257 void IndexedDBDispatcher::RequestIDBFactoryDeleteDatabase(
258 const base::string16& name,
259 WebIDBCallbacks* callbacks_ptr,
260 const std::string& database_identifier) {
261 scoped_ptr<WebIDBCallbacks> callbacks(callbacks_ptr);
263 IndexedDBHostMsg_FactoryDeleteDatabase_Params params;
264 params.ipc_thread_id = CurrentWorkerId();
265 params.ipc_callbacks_id = pending_callbacks_.Add(callbacks.release());
266 params.database_identifier = database_identifier;
267 params.name = name;
268 Send(new IndexedDBHostMsg_FactoryDeleteDatabase(params));
271 void IndexedDBDispatcher::RequestIDBDatabaseClose(
272 int32 ipc_database_id,
273 int32 ipc_database_callbacks_id) {
274 Send(new IndexedDBHostMsg_DatabaseClose(ipc_database_id));
275 // There won't be pending database callbacks if the transaction was aborted in
276 // the initial upgradeneeded event handler.
277 if (pending_database_callbacks_.Lookup(ipc_database_callbacks_id))
278 pending_database_callbacks_.Remove(ipc_database_callbacks_id);
281 void IndexedDBDispatcher::NotifyIDBDatabaseVersionChangeIgnored(
282 int32 ipc_database_id) {
283 Send(new IndexedDBHostMsg_DatabaseVersionChangeIgnored(ipc_database_id));
286 void IndexedDBDispatcher::RequestIDBDatabaseCreateTransaction(
287 int32 ipc_database_id,
288 int64 transaction_id,
289 WebIDBDatabaseCallbacks* database_callbacks_ptr,
290 WebVector<long long> object_store_ids,
291 blink::WebIDBTransactionMode mode) {
292 scoped_ptr<WebIDBDatabaseCallbacks> database_callbacks(
293 database_callbacks_ptr);
294 IndexedDBHostMsg_DatabaseCreateTransaction_Params params;
295 params.ipc_thread_id = CurrentWorkerId();
296 params.ipc_database_id = ipc_database_id;
297 params.transaction_id = transaction_id;
298 params.ipc_database_callbacks_id =
299 pending_database_callbacks_.Add(database_callbacks.release());
300 params.object_store_ids
301 .assign(object_store_ids.data(),
302 object_store_ids.data() + object_store_ids.size());
303 params.mode = mode;
305 Send(new IndexedDBHostMsg_DatabaseCreateTransaction(params));
308 void IndexedDBDispatcher::RequestIDBDatabaseGet(
309 int32 ipc_database_id,
310 int64 transaction_id,
311 int64 object_store_id,
312 int64 index_id,
313 const IndexedDBKeyRange& key_range,
314 bool key_only,
315 WebIDBCallbacks* callbacks) {
316 ResetCursorPrefetchCaches(transaction_id, kAllCursors);
317 IndexedDBHostMsg_DatabaseGet_Params params;
318 init_params(&params, callbacks);
319 params.ipc_database_id = ipc_database_id;
320 params.transaction_id = transaction_id;
321 params.object_store_id = object_store_id;
322 params.index_id = index_id;
323 params.key_range = key_range;
324 params.key_only = key_only;
325 Send(new IndexedDBHostMsg_DatabaseGet(params));
328 void IndexedDBDispatcher::RequestIDBDatabaseGetAll(
329 int32 ipc_database_id,
330 int64 transaction_id,
331 int64 object_store_id,
332 int64 index_id,
333 const IndexedDBKeyRange& key_range,
334 int64 max_count,
335 WebIDBCallbacks* callbacks) {
336 ResetCursorPrefetchCaches(transaction_id, kAllCursors);
337 IndexedDBHostMsg_DatabaseGetAll_Params params;
338 init_params(&params, callbacks);
339 params.ipc_database_id = ipc_database_id;
340 params.transaction_id = transaction_id;
341 params.object_store_id = object_store_id;
342 params.index_id = index_id;
343 params.key_range = key_range;
344 params.max_count = max_count;
345 Send(new IndexedDBHostMsg_DatabaseGetAll(params));
348 void IndexedDBDispatcher::RequestIDBDatabasePut(
349 int32 ipc_database_id,
350 int64 transaction_id,
351 int64 object_store_id,
352 const WebData& value,
353 const blink::WebVector<WebBlobInfo>& web_blob_info,
354 const IndexedDBKey& key,
355 blink::WebIDBPutMode put_mode,
356 WebIDBCallbacks* callbacks,
357 const WebVector<long long>& index_ids,
358 const WebVector<WebVector<WebIDBKey> >& index_keys) {
359 if (value.size() + key.size_estimate() > kMaxIDBValueSizeInBytes) {
360 callbacks->onError(WebIDBDatabaseError(
361 blink::WebIDBDatabaseExceptionUnknownError,
362 WebString::fromUTF8(base::StringPrintf(
363 "The serialized value is too large"
364 " (size=%" PRIuS " bytes, max=%" PRIuS " bytes).",
365 value.size(),
366 kMaxIDBValueSizeInBytes).c_str())));
367 return;
370 ResetCursorPrefetchCaches(transaction_id, kAllCursors);
371 IndexedDBHostMsg_DatabasePut_Params params;
372 init_params(&params, callbacks);
373 params.ipc_database_id = ipc_database_id;
374 params.transaction_id = transaction_id;
375 params.object_store_id = object_store_id;
377 params.value.bits.assign(value.data(), value.data() + value.size());
378 params.key = key;
379 params.put_mode = put_mode;
381 DCHECK_EQ(index_ids.size(), index_keys.size());
382 params.index_keys.resize(index_ids.size());
383 for (size_t i = 0, len = index_ids.size(); i < len; ++i) {
384 params.index_keys[i].first = index_ids[i];
385 params.index_keys[i].second.resize(index_keys[i].size());
386 for (size_t j = 0; j < index_keys[i].size(); ++j) {
387 params.index_keys[i].second[j] =
388 IndexedDBKey(IndexedDBKeyBuilder::Build(index_keys[i][j]));
392 params.value.blob_or_file_info.resize(web_blob_info.size());
393 for (size_t i = 0; i < web_blob_info.size(); ++i) {
394 const WebBlobInfo& info = web_blob_info[i];
395 IndexedDBMsg_BlobOrFileInfo& blob_or_file_info =
396 params.value.blob_or_file_info[i];
397 blob_or_file_info.is_file = info.isFile();
398 if (info.isFile()) {
399 blob_or_file_info.file_path = info.filePath();
400 blob_or_file_info.file_name = info.fileName();
401 blob_or_file_info.last_modified = info.lastModified();
403 blob_or_file_info.size = info.size();
404 blob_or_file_info.uuid = info.uuid().latin1();
405 DCHECK(blob_or_file_info.uuid.size());
406 blob_or_file_info.mime_type = info.type();
409 Send(new IndexedDBHostMsg_DatabasePut(params));
412 void IndexedDBDispatcher::RequestIDBDatabaseOpenCursor(
413 int32 ipc_database_id,
414 int64 transaction_id,
415 int64 object_store_id,
416 int64 index_id,
417 const IndexedDBKeyRange& key_range,
418 blink::WebIDBCursorDirection direction,
419 bool key_only,
420 blink::WebIDBTaskType task_type,
421 WebIDBCallbacks* callbacks) {
422 ResetCursorPrefetchCaches(transaction_id, kAllCursors);
423 IndexedDBHostMsg_DatabaseOpenCursor_Params params;
424 init_params(&params, callbacks);
425 params.ipc_database_id = ipc_database_id;
426 params.transaction_id = transaction_id;
427 params.object_store_id = object_store_id;
428 params.index_id = index_id;
429 params.key_range = key_range;
430 params.direction = direction;
431 params.key_only = key_only;
432 params.task_type = task_type;
433 Send(new IndexedDBHostMsg_DatabaseOpenCursor(params));
435 DCHECK(cursor_transaction_ids_.find(params.ipc_callbacks_id) ==
436 cursor_transaction_ids_.end());
437 cursor_transaction_ids_[params.ipc_callbacks_id] = transaction_id;
440 void IndexedDBDispatcher::RequestIDBDatabaseCount(
441 int32 ipc_database_id,
442 int64 transaction_id,
443 int64 object_store_id,
444 int64 index_id,
445 const IndexedDBKeyRange& key_range,
446 WebIDBCallbacks* callbacks) {
447 ResetCursorPrefetchCaches(transaction_id, kAllCursors);
448 IndexedDBHostMsg_DatabaseCount_Params params;
449 init_params(&params, callbacks);
450 params.ipc_database_id = ipc_database_id;
451 params.transaction_id = transaction_id;
452 params.object_store_id = object_store_id;
453 params.index_id = index_id;
454 params.key_range = key_range;
455 Send(new IndexedDBHostMsg_DatabaseCount(params));
458 void IndexedDBDispatcher::RequestIDBDatabaseDeleteRange(
459 int32 ipc_database_id,
460 int64 transaction_id,
461 int64 object_store_id,
462 const IndexedDBKeyRange& key_range,
463 WebIDBCallbacks* callbacks) {
464 ResetCursorPrefetchCaches(transaction_id, kAllCursors);
465 IndexedDBHostMsg_DatabaseDeleteRange_Params params;
466 init_params(&params, callbacks);
467 params.ipc_database_id = ipc_database_id;
468 params.transaction_id = transaction_id;
469 params.object_store_id = object_store_id;
470 params.key_range = key_range;
471 Send(new IndexedDBHostMsg_DatabaseDeleteRange(params));
474 void IndexedDBDispatcher::RequestIDBDatabaseClear(
475 int32 ipc_database_id,
476 int64 transaction_id,
477 int64 object_store_id,
478 WebIDBCallbacks* callbacks_ptr) {
479 ResetCursorPrefetchCaches(transaction_id, kAllCursors);
480 scoped_ptr<WebIDBCallbacks> callbacks(callbacks_ptr);
481 int32 ipc_callbacks_id = pending_callbacks_.Add(callbacks.release());
482 Send(new IndexedDBHostMsg_DatabaseClear(CurrentWorkerId(),
483 ipc_callbacks_id,
484 ipc_database_id,
485 transaction_id,
486 object_store_id));
489 void IndexedDBDispatcher::CursorDestroyed(int32 ipc_cursor_id) {
490 cursors_.erase(ipc_cursor_id);
493 void IndexedDBDispatcher::DatabaseDestroyed(int32 ipc_database_id) {
494 DCHECK_EQ(databases_.count(ipc_database_id), 1u);
495 databases_.erase(ipc_database_id);
498 void IndexedDBDispatcher::OnSuccessIDBDatabase(
499 int32 ipc_thread_id,
500 int32 ipc_callbacks_id,
501 int32 ipc_database_callbacks_id,
502 int32 ipc_object_id,
503 const IndexedDBDatabaseMetadata& idb_metadata) {
504 DCHECK_EQ(ipc_thread_id, CurrentWorkerId());
505 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(ipc_callbacks_id);
506 if (!callbacks)
507 return;
508 WebIDBMetadata metadata(ConvertMetadata(idb_metadata));
509 // If an upgrade was performed, count will be non-zero.
510 WebIDBDatabase* database = NULL;
512 // Back-end will send kNoDatabase if it was already sent in OnUpgradeNeeded.
513 // May already be deleted and removed from the table, but do not recreate..
514 if (ipc_object_id != kNoDatabase) {
515 DCHECK(!databases_.count(ipc_object_id));
516 database = databases_[ipc_object_id] = new WebIDBDatabaseImpl(
517 ipc_object_id, ipc_database_callbacks_id, thread_safe_sender_.get());
520 callbacks->onSuccess(database, metadata);
521 pending_callbacks_.Remove(ipc_callbacks_id);
524 void IndexedDBDispatcher::OnSuccessIndexedDBKey(int32 ipc_thread_id,
525 int32 ipc_callbacks_id,
526 const IndexedDBKey& key) {
527 DCHECK_EQ(ipc_thread_id, CurrentWorkerId());
528 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(ipc_callbacks_id);
529 if (!callbacks)
530 return;
531 callbacks->onSuccess(WebIDBKeyBuilder::Build(key));
532 pending_callbacks_.Remove(ipc_callbacks_id);
535 void IndexedDBDispatcher::OnSuccessStringList(
536 int32 ipc_thread_id,
537 int32 ipc_callbacks_id,
538 const std::vector<base::string16>& value) {
539 DCHECK_EQ(ipc_thread_id, CurrentWorkerId());
540 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(ipc_callbacks_id);
541 if (!callbacks)
542 return;
543 callbacks->onSuccess(WebVector<WebString>(value));
544 pending_callbacks_.Remove(ipc_callbacks_id);
547 static void PrepareWebValue(const IndexedDBMsg_ReturnValue& value,
548 WebIDBValue* web_value) {
549 if (value.bits.empty())
550 return;
552 web_value->data.assign(&*value.bits.begin(), value.bits.size());
553 blink::WebVector<WebBlobInfo> local_blob_info(value.blob_or_file_info.size());
554 for (size_t i = 0; i < value.blob_or_file_info.size(); ++i) {
555 const IndexedDBMsg_BlobOrFileInfo& info = value.blob_or_file_info[i];
556 if (info.is_file) {
557 local_blob_info[i] = WebBlobInfo(
558 WebString::fromUTF8(info.uuid.c_str()), info.file_path,
559 info.file_name, info.mime_type, info.last_modified, info.size);
560 } else {
561 local_blob_info[i] = WebBlobInfo(WebString::fromUTF8(info.uuid.c_str()),
562 info.mime_type, info.size);
566 web_value->webBlobInfo.swap(local_blob_info);
567 web_value->primaryKey = WebIDBKeyBuilder::Build(value.primary_key);
568 web_value->keyPath = WebIDBKeyPathBuilder::Build(value.key_path);
571 static void PrepareWebValueAndBlobInfo(
572 const IndexedDBMsg_Value& value,
573 WebData* web_value,
574 blink::WebVector<WebBlobInfo>* web_blob_info) {
575 if (value.bits.empty())
576 return;
578 web_value->assign(&*value.bits.begin(), value.bits.size());
579 blink::WebVector<WebBlobInfo> local_blob_info(value.blob_or_file_info.size());
580 for (size_t i = 0; i < value.blob_or_file_info.size(); ++i) {
581 const IndexedDBMsg_BlobOrFileInfo& info = value.blob_or_file_info[i];
582 if (info.is_file) {
583 local_blob_info[i] = WebBlobInfo(WebString::fromUTF8(info.uuid.c_str()),
584 info.file_path,
585 info.file_name,
586 info.mime_type,
587 info.last_modified,
588 info.size);
589 } else {
590 local_blob_info[i] = WebBlobInfo(
591 WebString::fromUTF8(info.uuid.c_str()), info.mime_type, info.size);
594 web_blob_info->swap(local_blob_info);
597 void IndexedDBDispatcher::OnSuccessArray(
598 const IndexedDBMsg_CallbacksSuccessArray_Params& p) {
599 DCHECK_EQ(p.ipc_thread_id, CurrentWorkerId());
600 int32 ipc_callbacks_id = p.ipc_callbacks_id;
601 blink::WebVector<WebIDBValue> web_values(p.values.size());
602 for (size_t i = 0; i < p.values.size(); ++i)
603 PrepareWebValue(p.values[i], &web_values[i]);
604 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(ipc_callbacks_id);
605 DCHECK(callbacks);
606 callbacks->onSuccess(web_values);
607 pending_callbacks_.Remove(ipc_callbacks_id);
610 void IndexedDBDispatcher::OnSuccessValue(
611 const IndexedDBMsg_CallbacksSuccessValue_Params& params) {
612 DCHECK_EQ(params.ipc_thread_id, CurrentWorkerId());
613 WebIDBCallbacks* callbacks =
614 pending_callbacks_.Lookup(params.ipc_callbacks_id);
615 if (!callbacks)
616 return;
617 WebIDBValue web_value;
618 PrepareWebValue(params.value, &web_value);
619 if (params.value.primary_key.IsValid()) {
620 web_value.primaryKey = WebIDBKeyBuilder::Build(params.value.primary_key);
621 web_value.keyPath = WebIDBKeyPathBuilder::Build(params.value.key_path);
623 callbacks->onSuccess(web_value);
624 cursor_transaction_ids_.erase(params.ipc_callbacks_id);
625 pending_callbacks_.Remove(params.ipc_callbacks_id);
628 void IndexedDBDispatcher::OnSuccessInteger(int32 ipc_thread_id,
629 int32 ipc_callbacks_id,
630 int64 value) {
631 DCHECK_EQ(ipc_thread_id, CurrentWorkerId());
632 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(ipc_callbacks_id);
633 if (!callbacks)
634 return;
635 callbacks->onSuccess(value);
636 pending_callbacks_.Remove(ipc_callbacks_id);
639 void IndexedDBDispatcher::OnSuccessUndefined(int32 ipc_thread_id,
640 int32 ipc_callbacks_id) {
641 DCHECK_EQ(ipc_thread_id, CurrentWorkerId());
642 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(ipc_callbacks_id);
643 if (!callbacks)
644 return;
645 callbacks->onSuccess();
646 pending_callbacks_.Remove(ipc_callbacks_id);
649 void IndexedDBDispatcher::OnSuccessOpenCursor(
650 const IndexedDBMsg_CallbacksSuccessIDBCursor_Params& p) {
651 DCHECK_EQ(p.ipc_thread_id, CurrentWorkerId());
652 int32 ipc_callbacks_id = p.ipc_callbacks_id;
653 int32 ipc_object_id = p.ipc_cursor_id;
654 const IndexedDBKey& key = p.key;
655 const IndexedDBKey& primary_key = p.primary_key;
656 WebData web_value;
657 WebVector<WebBlobInfo> web_blob_info;
658 PrepareWebValueAndBlobInfo(p.value, &web_value, &web_blob_info);
660 DCHECK(cursor_transaction_ids_.find(ipc_callbacks_id) !=
661 cursor_transaction_ids_.end());
662 int64 transaction_id = cursor_transaction_ids_[ipc_callbacks_id];
663 cursor_transaction_ids_.erase(ipc_callbacks_id);
665 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(ipc_callbacks_id);
666 if (!callbacks)
667 return;
669 WebIDBCursorImpl* cursor = new WebIDBCursorImpl(
670 ipc_object_id, transaction_id, thread_safe_sender_.get());
671 cursors_[ipc_object_id] = cursor;
672 callbacks->onSuccess(cursor,
673 WebIDBKeyBuilder::Build(key),
674 WebIDBKeyBuilder::Build(primary_key),
675 web_value,
676 web_blob_info);
678 pending_callbacks_.Remove(ipc_callbacks_id);
681 void IndexedDBDispatcher::OnSuccessCursorContinue(
682 const IndexedDBMsg_CallbacksSuccessCursorContinue_Params& p) {
683 DCHECK_EQ(p.ipc_thread_id, CurrentWorkerId());
684 int32 ipc_callbacks_id = p.ipc_callbacks_id;
685 int32 ipc_cursor_id = p.ipc_cursor_id;
686 const IndexedDBKey& key = p.key;
687 const IndexedDBKey& primary_key = p.primary_key;
689 if (cursors_.find(ipc_cursor_id) == cursors_.end())
690 return;
692 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(ipc_callbacks_id);
693 if (!callbacks)
694 return;
696 WebData web_value;
697 WebVector<WebBlobInfo> web_blob_info;
698 PrepareWebValueAndBlobInfo(p.value, &web_value, &web_blob_info);
699 callbacks->onSuccess(WebIDBKeyBuilder::Build(key),
700 WebIDBKeyBuilder::Build(primary_key),
701 web_value,
702 web_blob_info);
704 pending_callbacks_.Remove(ipc_callbacks_id);
707 void IndexedDBDispatcher::OnSuccessCursorPrefetch(
708 const IndexedDBMsg_CallbacksSuccessCursorPrefetch_Params& p) {
709 DCHECK_EQ(p.ipc_thread_id, CurrentWorkerId());
710 int32 ipc_callbacks_id = p.ipc_callbacks_id;
711 int32 ipc_cursor_id = p.ipc_cursor_id;
712 const std::vector<IndexedDBKey>& keys = p.keys;
713 const std::vector<IndexedDBKey>& primary_keys = p.primary_keys;
714 std::vector<WebData> values(p.values.size());
715 std::vector<WebVector<WebBlobInfo>> blob_infos(p.values.size());
716 for (size_t i = 0; i < p.values.size(); ++i)
717 PrepareWebValueAndBlobInfo(p.values[i], &values[i], &blob_infos[i]);
718 std::map<int32, WebIDBCursorImpl*>::const_iterator cur_iter =
719 cursors_.find(ipc_cursor_id);
720 if (cur_iter == cursors_.end())
721 return;
723 cur_iter->second->SetPrefetchData(keys, primary_keys, values, blob_infos);
725 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(ipc_callbacks_id);
726 DCHECK(callbacks);
727 cur_iter->second->CachedContinue(callbacks);
728 pending_callbacks_.Remove(ipc_callbacks_id);
731 void IndexedDBDispatcher::OnIntBlocked(int32 ipc_thread_id,
732 int32 ipc_callbacks_id,
733 int64 existing_version) {
734 DCHECK_EQ(ipc_thread_id, CurrentWorkerId());
735 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(ipc_callbacks_id);
736 DCHECK(callbacks);
737 callbacks->onBlocked(existing_version);
740 void IndexedDBDispatcher::OnUpgradeNeeded(
741 const IndexedDBMsg_CallbacksUpgradeNeeded_Params& p) {
742 DCHECK_EQ(p.ipc_thread_id, CurrentWorkerId());
743 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(p.ipc_callbacks_id);
744 DCHECK(callbacks);
745 WebIDBMetadata metadata(ConvertMetadata(p.idb_metadata));
746 DCHECK(!databases_.count(p.ipc_database_id));
747 databases_[p.ipc_database_id] =
748 new WebIDBDatabaseImpl(p.ipc_database_id,
749 p.ipc_database_callbacks_id,
750 thread_safe_sender_.get());
751 callbacks->onUpgradeNeeded(
752 p.old_version,
753 databases_[p.ipc_database_id],
754 metadata,
755 static_cast<blink::WebIDBDataLoss>(p.data_loss),
756 WebString::fromUTF8(p.data_loss_message));
759 void IndexedDBDispatcher::OnError(int32 ipc_thread_id,
760 int32 ipc_callbacks_id,
761 int code,
762 const base::string16& message) {
763 DCHECK_EQ(ipc_thread_id, CurrentWorkerId());
764 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(ipc_callbacks_id);
765 if (!callbacks)
766 return;
767 if (message.empty())
768 callbacks->onError(WebIDBDatabaseError(code));
769 else
770 callbacks->onError(WebIDBDatabaseError(code, message));
771 pending_callbacks_.Remove(ipc_callbacks_id);
772 cursor_transaction_ids_.erase(ipc_callbacks_id);
775 void IndexedDBDispatcher::OnAbort(int32 ipc_thread_id,
776 int32 ipc_database_callbacks_id,
777 int64 transaction_id,
778 int code,
779 const base::string16& message) {
780 DCHECK_EQ(ipc_thread_id, CurrentWorkerId());
781 WebIDBDatabaseCallbacks* callbacks =
782 pending_database_callbacks_.Lookup(ipc_database_callbacks_id);
783 if (!callbacks)
784 return;
785 if (message.empty())
786 callbacks->onAbort(transaction_id, WebIDBDatabaseError(code));
787 else
788 callbacks->onAbort(transaction_id, WebIDBDatabaseError(code, message));
791 void IndexedDBDispatcher::OnComplete(int32 ipc_thread_id,
792 int32 ipc_database_callbacks_id,
793 int64 transaction_id) {
794 DCHECK_EQ(ipc_thread_id, CurrentWorkerId());
795 WebIDBDatabaseCallbacks* callbacks =
796 pending_database_callbacks_.Lookup(ipc_database_callbacks_id);
797 if (!callbacks)
798 return;
799 callbacks->onComplete(transaction_id);
802 void IndexedDBDispatcher::OnForcedClose(int32 ipc_thread_id,
803 int32 ipc_database_callbacks_id) {
804 DCHECK_EQ(ipc_thread_id, CurrentWorkerId());
805 WebIDBDatabaseCallbacks* callbacks =
806 pending_database_callbacks_.Lookup(ipc_database_callbacks_id);
807 if (!callbacks)
808 return;
809 callbacks->onForcedClose();
812 void IndexedDBDispatcher::OnIntVersionChange(int32 ipc_thread_id,
813 int32 ipc_database_callbacks_id,
814 int64 old_version,
815 int64 new_version) {
816 DCHECK_EQ(ipc_thread_id, CurrentWorkerId());
817 WebIDBDatabaseCallbacks* callbacks =
818 pending_database_callbacks_.Lookup(ipc_database_callbacks_id);
819 // callbacks would be NULL if a versionchange event is received after close
820 // has been called.
821 if (!callbacks)
822 return;
823 callbacks->onVersionChange(old_version, new_version);
826 void IndexedDBDispatcher::ResetCursorPrefetchCaches(
827 int64 transaction_id,
828 int32 ipc_exception_cursor_id) {
829 typedef std::map<int32, WebIDBCursorImpl*>::iterator Iterator;
830 for (Iterator i = cursors_.begin(); i != cursors_.end(); ++i) {
831 if (i->first == ipc_exception_cursor_id ||
832 i->second->transaction_id() != transaction_id)
833 continue;
834 i->second->ResetPrefetchCache();
838 } // namespace content