| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/child/indexed_db/indexed_db_dispatcher.h" | 5 #include "content/child/indexed_db/indexed_db_dispatcher.h" |
| 6 | 6 |
| 7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
| 8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "base/threading/thread_local.h" | 10 #include "base/threading/thread_local.h" |
| 11 #include "content/child/child_thread.h" | |
| 12 #include "content/child/indexed_db/proxy_webidbcursor_impl.h" | 11 #include "content/child/indexed_db/proxy_webidbcursor_impl.h" |
| 13 #include "content/child/indexed_db/proxy_webidbdatabase_impl.h" | 12 #include "content/child/indexed_db/proxy_webidbdatabase_impl.h" |
| 13 #include "content/child/thread_safe_sender.h" |
| 14 #include "content/common/indexed_db/indexed_db_messages.h" | 14 #include "content/common/indexed_db/indexed_db_messages.h" |
| 15 #include "ipc/ipc_channel.h" | 15 #include "ipc/ipc_channel.h" |
| 16 #include "third_party/WebKit/public/platform/WebIDBDatabaseCallbacks.h" | 16 #include "third_party/WebKit/public/platform/WebIDBDatabaseCallbacks.h" |
| 17 #include "third_party/WebKit/public/platform/WebIDBDatabaseError.h" | 17 #include "third_party/WebKit/public/platform/WebIDBDatabaseError.h" |
| 18 #include "third_party/WebKit/public/platform/WebIDBDatabaseException.h" | 18 #include "third_party/WebKit/public/platform/WebIDBDatabaseException.h" |
| 19 #include "third_party/WebKit/public/platform/WebIDBKeyRange.h" | 19 #include "third_party/WebKit/public/platform/WebIDBKeyRange.h" |
| 20 | 20 |
| 21 using WebKit::WebData; | 21 using WebKit::WebData; |
| 22 using WebKit::WebIDBCallbacks; | 22 using WebKit::WebIDBCallbacks; |
| 23 using WebKit::WebIDBDatabase; | 23 using WebKit::WebIDBDatabase; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 40 IndexedDBDispatcher* const kHasBeenDeleted = | 40 IndexedDBDispatcher* const kHasBeenDeleted = |
| 41 reinterpret_cast<IndexedDBDispatcher*>(0x1); | 41 reinterpret_cast<IndexedDBDispatcher*>(0x1); |
| 42 | 42 |
| 43 int32 CurrentWorkerId() { | 43 int32 CurrentWorkerId() { |
| 44 return WorkerTaskRunner::Instance()->CurrentWorkerId(); | 44 return WorkerTaskRunner::Instance()->CurrentWorkerId(); |
| 45 } | 45 } |
| 46 } // unnamed namespace | 46 } // unnamed namespace |
| 47 | 47 |
| 48 const size_t kMaxIDBValueSizeInBytes = 64 * 1024 * 1024; | 48 const size_t kMaxIDBValueSizeInBytes = 64 * 1024 * 1024; |
| 49 | 49 |
| 50 IndexedDBDispatcher::IndexedDBDispatcher() { | 50 IndexedDBDispatcher::IndexedDBDispatcher(ThreadSafeSender* thread_safe_sender) |
| 51 : thread_safe_sender_(thread_safe_sender) { |
| 51 g_idb_dispatcher_tls.Pointer()->Set(this); | 52 g_idb_dispatcher_tls.Pointer()->Set(this); |
| 52 } | 53 } |
| 53 | 54 |
| 54 IndexedDBDispatcher::~IndexedDBDispatcher() { | 55 IndexedDBDispatcher::~IndexedDBDispatcher() { |
| 55 // Clear any pending callbacks - which may result in dispatch requests - | 56 // Clear any pending callbacks - which may result in dispatch requests - |
| 56 // before marking the dispatcher as deleted. | 57 // before marking the dispatcher as deleted. |
| 57 pending_callbacks_.Clear(); | 58 pending_callbacks_.Clear(); |
| 58 pending_database_callbacks_.Clear(); | 59 pending_database_callbacks_.Clear(); |
| 59 | 60 |
| 60 DCHECK(pending_callbacks_.IsEmpty()); | 61 DCHECK(pending_callbacks_.IsEmpty()); |
| 61 DCHECK(pending_database_callbacks_.IsEmpty()); | 62 DCHECK(pending_database_callbacks_.IsEmpty()); |
| 62 | 63 |
| 63 g_idb_dispatcher_tls.Pointer()->Set(kHasBeenDeleted); | 64 g_idb_dispatcher_tls.Pointer()->Set(kHasBeenDeleted); |
| 64 } | 65 } |
| 65 | 66 |
| 66 IndexedDBDispatcher* IndexedDBDispatcher::ThreadSpecificInstance() { | 67 IndexedDBDispatcher* IndexedDBDispatcher::ThreadSpecificInstance( |
| 68 ThreadSafeSender* thread_safe_sender) { |
| 67 if (g_idb_dispatcher_tls.Pointer()->Get() == kHasBeenDeleted) { | 69 if (g_idb_dispatcher_tls.Pointer()->Get() == kHasBeenDeleted) { |
| 68 NOTREACHED() << "Re-instantiating TLS IndexedDBDispatcher."; | 70 NOTREACHED() << "Re-instantiating TLS IndexedDBDispatcher."; |
| 69 g_idb_dispatcher_tls.Pointer()->Set(NULL); | 71 g_idb_dispatcher_tls.Pointer()->Set(NULL); |
| 70 } | 72 } |
| 71 if (g_idb_dispatcher_tls.Pointer()->Get()) | 73 if (g_idb_dispatcher_tls.Pointer()->Get()) |
| 72 return g_idb_dispatcher_tls.Pointer()->Get(); | 74 return g_idb_dispatcher_tls.Pointer()->Get(); |
| 73 | 75 |
| 74 IndexedDBDispatcher* dispatcher = new IndexedDBDispatcher; | 76 IndexedDBDispatcher* dispatcher = new IndexedDBDispatcher(thread_safe_sender); |
| 75 if (WorkerTaskRunner::Instance()->CurrentWorkerId()) | 77 if (WorkerTaskRunner::Instance()->CurrentWorkerId()) |
| 76 webkit_glue::WorkerTaskRunner::Instance()->AddStopObserver(dispatcher); | 78 webkit_glue::WorkerTaskRunner::Instance()->AddStopObserver(dispatcher); |
| 77 return dispatcher; | 79 return dispatcher; |
| 78 } | 80 } |
| 79 | 81 |
| 80 void IndexedDBDispatcher::OnWorkerRunLoopStopped() { delete this; } | 82 void IndexedDBDispatcher::OnWorkerRunLoopStopped() { delete this; } |
| 81 | 83 |
| 82 WebIDBMetadata IndexedDBDispatcher::ConvertMetadata( | 84 WebIDBMetadata IndexedDBDispatcher::ConvertMetadata( |
| 83 const IndexedDBDatabaseMetadata& idb_metadata) { | 85 const IndexedDBDatabaseMetadata& idb_metadata) { |
| 84 WebIDBMetadata web_metadata; | 86 WebIDBMetadata web_metadata; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 IPC_MESSAGE_HANDLER(IndexedDBMsg_DatabaseCallbacksComplete, OnComplete) | 156 IPC_MESSAGE_HANDLER(IndexedDBMsg_DatabaseCallbacksComplete, OnComplete) |
| 155 IPC_MESSAGE_UNHANDLED(handled = false) | 157 IPC_MESSAGE_UNHANDLED(handled = false) |
| 156 IPC_END_MESSAGE_MAP() | 158 IPC_END_MESSAGE_MAP() |
| 157 // If a message gets here, IndexedDBMessageFilter already determined that it | 159 // If a message gets here, IndexedDBMessageFilter already determined that it |
| 158 // is an IndexedDB message. | 160 // is an IndexedDB message. |
| 159 DCHECK(handled) << "Didn't handle a message defined at line " | 161 DCHECK(handled) << "Didn't handle a message defined at line " |
| 160 << IPC_MESSAGE_ID_LINE(msg.type()); | 162 << IPC_MESSAGE_ID_LINE(msg.type()); |
| 161 } | 163 } |
| 162 | 164 |
| 163 bool IndexedDBDispatcher::Send(IPC::Message* msg) { | 165 bool IndexedDBDispatcher::Send(IPC::Message* msg) { |
| 164 if (!ChildThread::current()) { | 166 return thread_safe_sender_->Send(msg); |
| 165 // Unexpected - this may be happening during shutdown. | |
| 166 NOTREACHED(); | |
| 167 return false; | |
| 168 } | |
| 169 if (CurrentWorkerId()) { | |
| 170 scoped_refptr<IPC::SyncMessageFilter> filter( | |
| 171 ChildThread::current()->sync_message_filter()); | |
| 172 return filter->Send(msg); | |
| 173 } | |
| 174 return ChildThread::current()->Send(msg); | |
| 175 } | 167 } |
| 176 | 168 |
| 177 void IndexedDBDispatcher::RequestIDBCursorAdvance( | 169 void IndexedDBDispatcher::RequestIDBCursorAdvance( |
| 178 unsigned long count, | 170 unsigned long count, |
| 179 WebIDBCallbacks* callbacks_ptr, | 171 WebIDBCallbacks* callbacks_ptr, |
| 180 int32 ipc_cursor_id) { | 172 int32 ipc_cursor_id) { |
| 181 // Reset all cursor prefetch caches except for this cursor. | 173 // Reset all cursor prefetch caches except for this cursor. |
| 182 ResetCursorPrefetchCaches(ipc_cursor_id); | 174 ResetCursorPrefetchCaches(ipc_cursor_id); |
| 183 | 175 |
| 184 scoped_ptr<WebIDBCallbacks> callbacks(callbacks_ptr); | 176 scoped_ptr<WebIDBCallbacks> callbacks(callbacks_ptr); |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 461 int32 ipc_object_id, | 453 int32 ipc_object_id, |
| 462 const IndexedDBDatabaseMetadata& idb_metadata) { | 454 const IndexedDBDatabaseMetadata& idb_metadata) { |
| 463 DCHECK_EQ(ipc_thread_id, CurrentWorkerId()); | 455 DCHECK_EQ(ipc_thread_id, CurrentWorkerId()); |
| 464 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(ipc_callbacks_id); | 456 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(ipc_callbacks_id); |
| 465 if (!callbacks) | 457 if (!callbacks) |
| 466 return; | 458 return; |
| 467 WebIDBMetadata metadata(ConvertMetadata(idb_metadata)); | 459 WebIDBMetadata metadata(ConvertMetadata(idb_metadata)); |
| 468 // If an upgrade was performed, count will be non-zero. | 460 // If an upgrade was performed, count will be non-zero. |
| 469 if (!databases_.count(ipc_object_id)) | 461 if (!databases_.count(ipc_object_id)) |
| 470 databases_[ipc_object_id] = new RendererWebIDBDatabaseImpl( | 462 databases_[ipc_object_id] = new RendererWebIDBDatabaseImpl( |
| 471 ipc_object_id, ipc_database_callbacks_id); | 463 ipc_object_id, ipc_database_callbacks_id, thread_safe_sender_); |
| 472 DCHECK_EQ(databases_.count(ipc_object_id), 1u); | 464 DCHECK_EQ(databases_.count(ipc_object_id), 1u); |
| 473 callbacks->onSuccess(databases_[ipc_object_id], metadata); | 465 callbacks->onSuccess(databases_[ipc_object_id], metadata); |
| 474 pending_callbacks_.Remove(ipc_callbacks_id); | 466 pending_callbacks_.Remove(ipc_callbacks_id); |
| 475 } | 467 } |
| 476 | 468 |
| 477 void IndexedDBDispatcher::OnSuccessIndexedDBKey(int32 ipc_thread_id, | 469 void IndexedDBDispatcher::OnSuccessIndexedDBKey(int32 ipc_thread_id, |
| 478 int32 ipc_callbacks_id, | 470 int32 ipc_callbacks_id, |
| 479 const IndexedDBKey& key) { | 471 const IndexedDBKey& key) { |
| 480 DCHECK_EQ(ipc_thread_id, CurrentWorkerId()); | 472 DCHECK_EQ(ipc_thread_id, CurrentWorkerId()); |
| 481 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(ipc_callbacks_id); | 473 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(ipc_callbacks_id); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 558 const IndexedDBKey& primary_key = p.primary_key; | 550 const IndexedDBKey& primary_key = p.primary_key; |
| 559 WebData web_value; | 551 WebData web_value; |
| 560 if (p.value.size()) | 552 if (p.value.size()) |
| 561 web_value.assign(&p.value.front(), p.value.size()); | 553 web_value.assign(&p.value.front(), p.value.size()); |
| 562 | 554 |
| 563 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(ipc_callbacks_id); | 555 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(ipc_callbacks_id); |
| 564 if (!callbacks) | 556 if (!callbacks) |
| 565 return; | 557 return; |
| 566 | 558 |
| 567 RendererWebIDBCursorImpl* cursor = | 559 RendererWebIDBCursorImpl* cursor = |
| 568 new RendererWebIDBCursorImpl(ipc_object_id); | 560 new RendererWebIDBCursorImpl(ipc_object_id, thread_safe_sender_); |
| 569 cursors_[ipc_object_id] = cursor; | 561 cursors_[ipc_object_id] = cursor; |
| 570 callbacks->onSuccess(cursor, key, primary_key, web_value); | 562 callbacks->onSuccess(cursor, key, primary_key, web_value); |
| 571 | 563 |
| 572 pending_callbacks_.Remove(ipc_callbacks_id); | 564 pending_callbacks_.Remove(ipc_callbacks_id); |
| 573 } | 565 } |
| 574 | 566 |
| 575 void IndexedDBDispatcher::OnSuccessCursorContinue( | 567 void IndexedDBDispatcher::OnSuccessCursorContinue( |
| 576 const IndexedDBMsg_CallbacksSuccessCursorContinue_Params& p) { | 568 const IndexedDBMsg_CallbacksSuccessCursorContinue_Params& p) { |
| 577 DCHECK_EQ(p.ipc_thread_id, CurrentWorkerId()); | 569 DCHECK_EQ(p.ipc_thread_id, CurrentWorkerId()); |
| 578 int32 ipc_callbacks_id = p.ipc_callbacks_id; | 570 int32 ipc_callbacks_id = p.ipc_callbacks_id; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 628 } | 620 } |
| 629 | 621 |
| 630 void IndexedDBDispatcher::OnUpgradeNeeded( | 622 void IndexedDBDispatcher::OnUpgradeNeeded( |
| 631 const IndexedDBMsg_CallbacksUpgradeNeeded_Params& p) { | 623 const IndexedDBMsg_CallbacksUpgradeNeeded_Params& p) { |
| 632 DCHECK_EQ(p.ipc_thread_id, CurrentWorkerId()); | 624 DCHECK_EQ(p.ipc_thread_id, CurrentWorkerId()); |
| 633 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(p.ipc_callbacks_id); | 625 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(p.ipc_callbacks_id); |
| 634 DCHECK(callbacks); | 626 DCHECK(callbacks); |
| 635 WebIDBMetadata metadata(ConvertMetadata(p.idb_metadata)); | 627 WebIDBMetadata metadata(ConvertMetadata(p.idb_metadata)); |
| 636 DCHECK(!databases_.count(p.ipc_database_id)); | 628 DCHECK(!databases_.count(p.ipc_database_id)); |
| 637 databases_[p.ipc_database_id] = new RendererWebIDBDatabaseImpl( | 629 databases_[p.ipc_database_id] = new RendererWebIDBDatabaseImpl( |
| 638 p.ipc_database_id, p.ipc_database_callbacks_id); | 630 p.ipc_database_id, p.ipc_database_callbacks_id, thread_safe_sender_); |
| 639 callbacks->onUpgradeNeeded( | 631 callbacks->onUpgradeNeeded( |
| 640 p.old_version, | 632 p.old_version, |
| 641 databases_[p.ipc_database_id], | 633 databases_[p.ipc_database_id], |
| 642 metadata, | 634 metadata, |
| 643 static_cast<WebIDBCallbacks::DataLoss>(p.data_loss)); | 635 static_cast<WebIDBCallbacks::DataLoss>(p.data_loss)); |
| 644 } | 636 } |
| 645 | 637 |
| 646 void IndexedDBDispatcher::OnError(int32 ipc_thread_id, | 638 void IndexedDBDispatcher::OnError(int32 ipc_thread_id, |
| 647 int32 ipc_callbacks_id, | 639 int32 ipc_callbacks_id, |
| 648 int code, | 640 int code, |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 707 int32 ipc_exception_cursor_id) { | 699 int32 ipc_exception_cursor_id) { |
| 708 typedef std::map<int32, RendererWebIDBCursorImpl*>::iterator Iterator; | 700 typedef std::map<int32, RendererWebIDBCursorImpl*>::iterator Iterator; |
| 709 for (Iterator i = cursors_.begin(); i != cursors_.end(); ++i) { | 701 for (Iterator i = cursors_.begin(); i != cursors_.end(); ++i) { |
| 710 if (i->first == ipc_exception_cursor_id) | 702 if (i->first == ipc_exception_cursor_id) |
| 711 continue; | 703 continue; |
| 712 i->second->ResetPrefetchCache(); | 704 i->second->ResetPrefetchCache(); |
| 713 } | 705 } |
| 714 } | 706 } |
| 715 | 707 |
| 716 } // namespace content | 708 } // namespace content |
| OLD | NEW |