| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/threading/thread_local.h" | 10 #include "base/threading/thread_local.h" |
| 11 #include "content/child/indexed_db/indexed_db_key_builders.h" | 11 #include "content/child/indexed_db/indexed_db_key_builders.h" |
| 12 #include "content/child/indexed_db/webidbcursor_impl.h" | 12 #include "content/child/indexed_db/webidbcursor_impl.h" |
| 13 #include "content/common/indexed_db/indexed_db_messages.h" | |
| 14 #include "ipc/ipc_channel.h" | 13 #include "ipc/ipc_channel.h" |
| 15 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseCal
lbacks.h" | 14 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseCal
lbacks.h" |
| 16 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBObservation
.h" | 15 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBObservation
.h" |
| 17 | 16 |
| 18 using blink::WebIDBKey; | 17 using blink::WebIDBKey; |
| 19 using blink::WebIDBObservation; | 18 using blink::WebIDBObservation; |
| 20 using blink::WebIDBObserver; | |
| 21 using base::ThreadLocalPointer; | 19 using base::ThreadLocalPointer; |
| 22 | 20 |
| 23 namespace content { | 21 namespace content { |
| 24 static base::LazyInstance<ThreadLocalPointer<IndexedDBDispatcher> >::Leaky | 22 static base::LazyInstance<ThreadLocalPointer<IndexedDBDispatcher> >::Leaky |
| 25 g_idb_dispatcher_tls = LAZY_INSTANCE_INITIALIZER; | 23 g_idb_dispatcher_tls = LAZY_INSTANCE_INITIALIZER; |
| 26 | 24 |
| 27 namespace { | 25 namespace { |
| 28 | 26 |
| 29 IndexedDBDispatcher* const kHasBeenDeleted = | 27 IndexedDBDispatcher* const kHasBeenDeleted = |
| 30 reinterpret_cast<IndexedDBDispatcher*>(0x1); | 28 reinterpret_cast<IndexedDBDispatcher*>(0x1); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 54 IndexedDBDispatcher* dispatcher = new IndexedDBDispatcher(); | 52 IndexedDBDispatcher* dispatcher = new IndexedDBDispatcher(); |
| 55 if (WorkerThread::GetCurrentId()) | 53 if (WorkerThread::GetCurrentId()) |
| 56 WorkerThread::AddObserver(dispatcher); | 54 WorkerThread::AddObserver(dispatcher); |
| 57 return dispatcher; | 55 return dispatcher; |
| 58 } | 56 } |
| 59 | 57 |
| 60 void IndexedDBDispatcher::WillStopCurrentWorkerThread() { | 58 void IndexedDBDispatcher::WillStopCurrentWorkerThread() { |
| 61 delete this; | 59 delete this; |
| 62 } | 60 } |
| 63 | 61 |
| 64 std::vector<WebIDBObservation> IndexedDBDispatcher::ConvertObservations( | |
| 65 const std::vector<IndexedDBMsg_Observation>& idb_observations) { | |
| 66 std::vector<WebIDBObservation> web_observations; | |
| 67 for (const auto& idb_observation : idb_observations) { | |
| 68 WebIDBObservation web_observation; | |
| 69 web_observation.objectStoreId = idb_observation.object_store_id; | |
| 70 web_observation.type = idb_observation.type; | |
| 71 web_observation.keyRange = | |
| 72 WebIDBKeyRangeBuilder::Build(idb_observation.key_range); | |
| 73 // TODO(palakj): Assign value to web_observation. | |
| 74 web_observations.push_back(std::move(web_observation)); | |
| 75 } | |
| 76 return web_observations; | |
| 77 } | |
| 78 | |
| 79 void IndexedDBDispatcher::OnMessageReceived(const IPC::Message& msg) { | |
| 80 bool handled = true; | |
| 81 IPC_BEGIN_MESSAGE_MAP(IndexedDBDispatcher, msg) | |
| 82 IPC_MESSAGE_HANDLER(IndexedDBMsg_DatabaseCallbacksChanges, | |
| 83 OnDatabaseChanges) | |
| 84 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 85 IPC_END_MESSAGE_MAP() | |
| 86 // If a message gets here, IndexedDBMessageFilter already determined that it | |
| 87 // is an IndexedDB message. | |
| 88 DCHECK(handled) << "Didn't handle a message defined at line " | |
| 89 << IPC_MESSAGE_ID_LINE(msg.type()); | |
| 90 } | |
| 91 | |
| 92 int32_t IndexedDBDispatcher::RegisterObserver( | |
| 93 std::unique_ptr<WebIDBObserver> observer) { | |
| 94 return observers_.Add(observer.release()); | |
| 95 } | |
| 96 | |
| 97 void IndexedDBDispatcher::RemoveObservers( | |
| 98 const std::vector<int32_t>& observer_ids_to_remove) { | |
| 99 for (int32_t id : observer_ids_to_remove) | |
| 100 observers_.Remove(id); | |
| 101 } | |
| 102 | |
| 103 void IndexedDBDispatcher::RegisterMojoOwnedCallbacks( | 62 void IndexedDBDispatcher::RegisterMojoOwnedCallbacks( |
| 104 IndexedDBCallbacksImpl::InternalState* callbacks) { | 63 IndexedDBCallbacksImpl::InternalState* callbacks) { |
| 105 mojo_owned_callback_state_[callbacks] = base::WrapUnique(callbacks); | 64 mojo_owned_callback_state_[callbacks] = base::WrapUnique(callbacks); |
| 106 } | 65 } |
| 107 | 66 |
| 108 void IndexedDBDispatcher::UnregisterMojoOwnedCallbacks( | 67 void IndexedDBDispatcher::UnregisterMojoOwnedCallbacks( |
| 109 IndexedDBCallbacksImpl::InternalState* callbacks) { | 68 IndexedDBCallbacksImpl::InternalState* callbacks) { |
| 110 if (in_destructor_) | 69 if (in_destructor_) |
| 111 return; | 70 return; |
| 112 | 71 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 125 blink::WebIDBDatabaseCallbacks* callbacks) { | 84 blink::WebIDBDatabaseCallbacks* callbacks) { |
| 126 if (in_destructor_) | 85 if (in_destructor_) |
| 127 return; | 86 return; |
| 128 | 87 |
| 129 auto it = mojo_owned_database_callback_state_.find(callbacks); | 88 auto it = mojo_owned_database_callback_state_.find(callbacks); |
| 130 DCHECK(it != mojo_owned_database_callback_state_.end()); | 89 DCHECK(it != mojo_owned_database_callback_state_.end()); |
| 131 it->second.release(); | 90 it->second.release(); |
| 132 mojo_owned_database_callback_state_.erase(it); | 91 mojo_owned_database_callback_state_.erase(it); |
| 133 } | 92 } |
| 134 | 93 |
| 135 void IndexedDBDispatcher::OnDatabaseChanges( | |
| 136 int32_t ipc_thread_id, | |
| 137 const IndexedDBMsg_ObserverChanges& changes) { | |
| 138 DCHECK_EQ(ipc_thread_id, CurrentWorkerId()); | |
| 139 std::vector<WebIDBObservation> observations( | |
| 140 ConvertObservations(changes.observations)); | |
| 141 for (auto& it : changes.observation_index) { | |
| 142 WebIDBObserver* observer = observers_.Lookup(it.first); | |
| 143 // An observer can be removed from the renderer, but still exist in the | |
| 144 // backend. Moreover, observer might have recorded some changes before being | |
| 145 // removed from the backend and thus, have its id be present in changes. | |
| 146 if (!observer) | |
| 147 continue; | |
| 148 observer->onChange(observations, std::move(it.second)); | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 void IndexedDBDispatcher::RegisterCursor(WebIDBCursorImpl* cursor) { | 94 void IndexedDBDispatcher::RegisterCursor(WebIDBCursorImpl* cursor) { |
| 153 DCHECK(!base::ContainsValue(cursors_, cursor)); | 95 DCHECK(!base::ContainsValue(cursors_, cursor)); |
| 154 cursors_.insert(cursor); | 96 cursors_.insert(cursor); |
| 155 } | 97 } |
| 156 | 98 |
| 157 void IndexedDBDispatcher::UnregisterCursor(WebIDBCursorImpl* cursor) { | 99 void IndexedDBDispatcher::UnregisterCursor(WebIDBCursorImpl* cursor) { |
| 158 DCHECK(base::ContainsValue(cursors_, cursor)); | 100 DCHECK(base::ContainsValue(cursors_, cursor)); |
| 159 cursors_.erase(cursor); | 101 cursors_.erase(cursor); |
| 160 } | 102 } |
| 161 | 103 |
| 162 void IndexedDBDispatcher::ResetCursorPrefetchCaches( | 104 void IndexedDBDispatcher::ResetCursorPrefetchCaches( |
| 163 int64_t transaction_id, | 105 int64_t transaction_id, |
| 164 WebIDBCursorImpl* exception_cursor) { | 106 WebIDBCursorImpl* exception_cursor) { |
| 165 for (WebIDBCursorImpl* cursor : cursors_) { | 107 for (WebIDBCursorImpl* cursor : cursors_) { |
| 166 if (cursor != exception_cursor && | 108 if (cursor != exception_cursor && |
| 167 cursor->transaction_id() == transaction_id) | 109 cursor->transaction_id() == transaction_id) |
| 168 cursor->ResetPrefetchCache(); | 110 cursor->ResetPrefetchCache(); |
| 169 } | 111 } |
| 170 } | 112 } |
| 171 | 113 |
| 172 } // namespace content | 114 } // namespace content |
| OLD | NEW |