Chromium Code Reviews| 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 #ifndef CONTENT_CHILD_INDEXED_DB_INDEXED_DB_DISPATCHER_H_ | 5 #ifndef CONTENT_CHILD_INDEXED_DB_INDEXED_DB_DISPATCHER_H_ |
| 6 #define CONTENT_CHILD_INDEXED_DB_INDEXED_DB_DISPATCHER_H_ | 6 #define CONTENT_CHILD_INDEXED_DB_INDEXED_DB_DISPATCHER_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <map> | 11 #include <map> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
| 16 #include "base/id_map.h" | 16 #include "base/id_map.h" |
| 17 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
| 19 #include "base/strings/nullable_string16.h" | 19 #include "base/strings/nullable_string16.h" |
| 20 #include "content/child/indexed_db/indexed_db_callbacks_impl.h" | 20 #include "content/child/indexed_db/indexed_db_callbacks_impl.h" |
| 21 #include "content/child/indexed_db/indexed_db_database_callbacks_impl.h" | 21 #include "content/child/indexed_db/indexed_db_database_callbacks_impl.h" |
| 22 #include "content/common/content_export.h" | 22 #include "content/common/content_export.h" |
| 23 #include "content/common/indexed_db/indexed_db_constants.h" | 23 #include "content/common/indexed_db/indexed_db_constants.h" |
| 24 #include "content/public/child/worker_thread.h" | 24 #include "content/public/child/worker_thread.h" |
| 25 #include "ipc/ipc_sync_message_filter.h" | 25 #include "ipc/ipc_sync_message_filter.h" |
| 26 #include "third_party/WebKit/public/platform/WebBlobInfo.h" | |
| 27 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBCallbacks.h " | 26 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBCallbacks.h " |
| 28 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBObserver.h" | 27 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBObserver.h" |
| 29 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBTypes.h" | 28 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBTypes.h" |
| 30 #include "url/origin.h" | 29 #include "url/origin.h" |
| 31 | 30 |
| 32 struct IndexedDBMsg_CallbacksSuccessCursorContinue_Params; | |
| 33 struct IndexedDBMsg_CallbacksSuccessCursorPrefetch_Params; | |
| 34 struct IndexedDBMsg_CallbacksSuccessIDBCursor_Params; | |
| 35 struct IndexedDBMsg_CallbacksSuccessArray_Params; | |
| 36 struct IndexedDBMsg_CallbacksSuccessValue_Params; | |
| 37 struct IndexedDBMsg_Observation; | 31 struct IndexedDBMsg_Observation; |
| 38 struct IndexedDBMsg_ObserverChanges; | 32 struct IndexedDBMsg_ObserverChanges; |
| 39 | 33 |
| 40 namespace blink { | 34 namespace blink { |
| 41 class WebData; | |
| 42 struct WebIDBObservation; | 35 struct WebIDBObservation; |
| 43 } | 36 } |
| 44 | 37 |
| 45 namespace content { | 38 namespace content { |
| 46 class IndexedDBKey; | |
| 47 class IndexedDBKeyRange; | |
| 48 class WebIDBCursorImpl; | 39 class WebIDBCursorImpl; |
| 49 class WebIDBDatabaseImpl; | |
| 50 class ThreadSafeSender; | |
| 51 | 40 |
| 52 // Handle the indexed db related communication for this context thread - the | 41 // Handle the indexed db related communication for this context thread - the |
| 53 // main thread and each worker thread have their own copies. | 42 // main thread and each worker thread have their own copies. |
| 54 class CONTENT_EXPORT IndexedDBDispatcher : public WorkerThread::Observer { | 43 class CONTENT_EXPORT IndexedDBDispatcher : public WorkerThread::Observer { |
| 55 public: | 44 public: |
| 56 // Constructor made public to allow RenderThreadImpl to own a copy without | 45 // Constructor made public to allow RenderThreadImpl to own a copy without |
| 57 // failing a NOTREACHED in ThreadSpecificInstance in tests that instantiate | 46 // failing a NOTREACHED in ThreadSpecificInstance in tests that instantiate |
| 58 // two copies of RenderThreadImpl on the same thread. Everyone else probably | 47 // two copies of RenderThreadImpl on the same thread. Everyone else probably |
| 59 // wants to use ThreadSpecificInstance(). | 48 // wants to use ThreadSpecificInstance(). |
| 60 explicit IndexedDBDispatcher(ThreadSafeSender* thread_safe_sender); | 49 explicit IndexedDBDispatcher(); |
|
dcheng
2016/11/17 03:46:23
Nit: no explicit
Reilly Grant (use Gerrit)
2016/11/17 20:04:52
Done.
| |
| 61 ~IndexedDBDispatcher() override; | 50 ~IndexedDBDispatcher() override; |
| 62 | 51 |
| 63 // |thread_safe_sender| needs to be passed in because if the call leads to | 52 static IndexedDBDispatcher* ThreadSpecificInstance(); |
| 64 // construction it will be needed. | |
| 65 static IndexedDBDispatcher* ThreadSpecificInstance( | |
| 66 ThreadSafeSender* thread_safe_sender); | |
| 67 | 53 |
| 68 // WorkerThread::Observer implementation. | 54 // WorkerThread::Observer implementation. |
| 69 void WillStopCurrentWorkerThread() override; | 55 void WillStopCurrentWorkerThread() override; |
| 70 | 56 |
| 71 static std::vector<blink::WebIDBObservation> ConvertObservations( | 57 static std::vector<blink::WebIDBObservation> ConvertObservations( |
| 72 const std::vector<IndexedDBMsg_Observation>& idb_observation); | 58 const std::vector<IndexedDBMsg_Observation>& idb_observation); |
| 73 | 59 |
| 74 void OnMessageReceived(const IPC::Message& msg); | 60 void OnMessageReceived(const IPC::Message& msg); |
| 75 | 61 |
| 76 // This method is virtual so it can be overridden in unit tests. | |
| 77 virtual bool Send(IPC::Message* msg); | |
| 78 | |
| 79 int32_t RegisterObserver(std::unique_ptr<blink::WebIDBObserver> observer); | 62 int32_t RegisterObserver(std::unique_ptr<blink::WebIDBObserver> observer); |
| 80 | 63 |
| 81 // Removes observers from our local map observers_. | 64 // Removes observers from our local map observers_. |
| 82 void RemoveObservers(const std::vector<int32_t>& observer_ids_to_remove); | 65 void RemoveObservers(const std::vector<int32_t>& observer_ids_to_remove); |
| 83 | 66 |
| 84 // This method is virtual so it can be overridden in unit tests. | |
| 85 virtual void RequestIDBCursorAdvance(unsigned long count, | |
| 86 blink::WebIDBCallbacks* callbacks_ptr, | |
| 87 int32_t ipc_cursor_id, | |
| 88 int64_t transaction_id); | |
| 89 | |
| 90 // This method is virtual so it can be overridden in unit tests. | |
| 91 virtual void RequestIDBCursorContinue(const IndexedDBKey& key, | |
| 92 const IndexedDBKey& primary_key, | |
| 93 blink::WebIDBCallbacks* callbacks_ptr, | |
| 94 int32_t ipc_cursor_id, | |
| 95 int64_t transaction_id); | |
| 96 | |
| 97 // This method is virtual so it can be overridden in unit tests. | |
| 98 virtual void RequestIDBCursorPrefetch(int n, | |
| 99 blink::WebIDBCallbacks* callbacks_ptr, | |
| 100 int32_t ipc_cursor_id); | |
| 101 | |
| 102 // This method is virtual so it can be overridden in unit tests. | |
| 103 virtual void RequestIDBCursorPrefetchReset(int used_prefetches, | |
| 104 int unused_prefetches, | |
| 105 int32_t ipc_cursor_id); | |
| 106 | |
| 107 void RegisterCursor(int32_t ipc_cursor_id, WebIDBCursorImpl* cursor); | |
| 108 | |
| 109 virtual void CursorDestroyed(int32_t ipc_cursor_id); | |
| 110 | |
| 111 enum { kAllCursors = -1 }; | 67 enum { kAllCursors = -1 }; |
| 112 | 68 |
| 113 // Reset cursor prefetch caches for all cursors except exception_cursor_id. | 69 void RegisterCursor(WebIDBCursorImpl* cursor); |
| 70 void UnregisterCursor(WebIDBCursorImpl* cursor); | |
| 71 // Reset cursor prefetch caches for all cursors except exception_cursor. | |
| 114 void ResetCursorPrefetchCaches(int64_t transaction_id, | 72 void ResetCursorPrefetchCaches(int64_t transaction_id, |
| 115 int32_t ipc_exception_cursor_id); | 73 WebIDBCursorImpl* exception_cursor); |
| 116 | 74 |
| 117 void RegisterMojoOwnedCallbacks( | 75 void RegisterMojoOwnedCallbacks( |
| 118 IndexedDBCallbacksImpl::InternalState* callback_state); | 76 IndexedDBCallbacksImpl::InternalState* callback_state); |
| 119 void UnregisterMojoOwnedCallbacks( | 77 void UnregisterMojoOwnedCallbacks( |
| 120 IndexedDBCallbacksImpl::InternalState* callback_state); | 78 IndexedDBCallbacksImpl::InternalState* callback_state); |
| 121 void RegisterMojoOwnedDatabaseCallbacks( | 79 void RegisterMojoOwnedDatabaseCallbacks( |
| 122 blink::WebIDBDatabaseCallbacks* callback_state); | 80 blink::WebIDBDatabaseCallbacks* callback_state); |
| 123 void UnregisterMojoOwnedDatabaseCallbacks( | 81 void UnregisterMojoOwnedDatabaseCallbacks( |
| 124 blink::WebIDBDatabaseCallbacks* callback_state); | 82 blink::WebIDBDatabaseCallbacks* callback_state); |
| 125 | 83 |
| 126 private: | 84 private: |
| 127 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest, CursorReset); | 85 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest, CursorReset); |
| 128 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest, CursorTransactionId); | 86 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest, CursorTransactionId); |
| 129 | 87 |
| 130 static int32_t CurrentWorkerId() { return WorkerThread::GetCurrentId(); } | 88 static int32_t CurrentWorkerId() { return WorkerThread::GetCurrentId(); } |
| 131 | 89 |
| 132 template <typename T> | |
| 133 void init_params(T* params, blink::WebIDBCallbacks* callbacks_ptr) { | |
| 134 std::unique_ptr<blink::WebIDBCallbacks> callbacks(callbacks_ptr); | |
| 135 params->ipc_thread_id = CurrentWorkerId(); | |
| 136 params->ipc_callbacks_id = pending_callbacks_.Add(callbacks.release()); | |
| 137 } | |
| 138 | |
| 139 // IDBCallback message handlers. | 90 // IDBCallback message handlers. |
| 140 void OnSuccessCursorContinue( | |
| 141 const IndexedDBMsg_CallbacksSuccessCursorContinue_Params& p); | |
| 142 void OnSuccessCursorPrefetch( | |
| 143 const IndexedDBMsg_CallbacksSuccessCursorPrefetch_Params& p); | |
| 144 void OnSuccessValue(const IndexedDBMsg_CallbacksSuccessValue_Params& p); | |
| 145 void OnSuccessInteger(int32_t ipc_thread_id, | |
| 146 int32_t ipc_callbacks_id, | |
| 147 int64_t value); | |
| 148 void OnError(int32_t ipc_thread_id, | |
| 149 int32_t ipc_callbacks_id, | |
| 150 int code, | |
| 151 const base::string16& message); | |
| 152 void OnDatabaseChanges(int32_t ipc_thread_id, | 91 void OnDatabaseChanges(int32_t ipc_thread_id, |
| 153 const IndexedDBMsg_ObserverChanges&); | 92 const IndexedDBMsg_ObserverChanges&); |
| 154 | 93 |
| 155 scoped_refptr<ThreadSafeSender> thread_safe_sender_; | |
| 156 | |
| 157 // Careful! WebIDBCallbacks wraps non-threadsafe data types. It must be | |
| 158 // destroyed and used on the same thread it was created on. | |
| 159 IDMap<blink::WebIDBCallbacks, IDMapOwnPointer> pending_callbacks_; | |
| 160 IDMap<blink::WebIDBObserver, IDMapOwnPointer> observers_; | 94 IDMap<blink::WebIDBObserver, IDMapOwnPointer> observers_; |
| 95 std::unordered_set<WebIDBCursorImpl*> cursors_; | |
| 161 | 96 |
| 162 // Holds pointers to the worker-thread owned state of IndexedDBCallbacksImpl | 97 // Holds pointers to the worker-thread owned state of IndexedDBCallbacksImpl |
| 163 // and IndexedDBDatabaseCallbacksImpl objects to makes sure that it is | 98 // and IndexedDBDatabaseCallbacksImpl objects to makes sure that it is |
| 164 // destroyed on thread exit if the Mojo pipe is not yet closed. Otherwise the | 99 // destroyed on thread exit if the Mojo pipe is not yet closed. Otherwise the |
| 165 // object will leak because the thread's task runner is no longer executing | 100 // object will leak because the thread's task runner is no longer executing |
| 166 // tasks. | 101 // tasks. |
| 167 std::unordered_set<IndexedDBCallbacksImpl::InternalState*> | 102 std::unordered_set<IndexedDBCallbacksImpl::InternalState*> |
| 168 mojo_owned_callback_state_; | 103 mojo_owned_callback_state_; |
| 169 std::unordered_set<blink::WebIDBDatabaseCallbacks*> | 104 std::unordered_set<blink::WebIDBDatabaseCallbacks*> |
| 170 mojo_owned_database_callback_state_; | 105 mojo_owned_database_callback_state_; |
| 171 | 106 |
| 172 // Maps the ipc_callback_id from an open cursor request to the request's | |
| 173 // transaction_id. Used to assign the transaction_id to the WebIDBCursorImpl | |
| 174 // when it is created. | |
| 175 std::map<int32_t, int64_t> cursor_transaction_ids_; | |
| 176 | |
| 177 // Map from cursor id to WebIDBCursorImpl. | |
| 178 std::map<int32_t, WebIDBCursorImpl*> cursors_; | |
| 179 | |
| 180 DISALLOW_COPY_AND_ASSIGN(IndexedDBDispatcher); | 107 DISALLOW_COPY_AND_ASSIGN(IndexedDBDispatcher); |
| 181 }; | 108 }; |
| 182 | 109 |
| 183 } // namespace content | 110 } // namespace content |
| 184 | 111 |
| 185 #endif // CONTENT_CHILD_INDEXED_DB_INDEXED_DB_DISPATCHER_H_ | 112 #endif // CONTENT_CHILD_INDEXED_DB_INDEXED_DB_DISPATCHER_H_ |
| OLD | NEW |