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" |
| 21 #include "content/child/indexed_db/indexed_db_database_callbacks_impl.h" |
20 #include "content/common/content_export.h" | 22 #include "content/common/content_export.h" |
21 #include "content/common/indexed_db/indexed_db_constants.h" | 23 #include "content/common/indexed_db/indexed_db_constants.h" |
22 #include "content/public/child/worker_thread.h" | 24 #include "content/public/child/worker_thread.h" |
23 #include "ipc/ipc_sync_message_filter.h" | 25 #include "ipc/ipc_sync_message_filter.h" |
24 #include "third_party/WebKit/public/platform/WebBlobInfo.h" | 26 #include "third_party/WebKit/public/platform/WebBlobInfo.h" |
25 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBCallbacks.h
" | 27 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBCallbacks.h
" |
26 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseCal
lbacks.h" | |
27 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBObserver.h" | 28 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBObserver.h" |
28 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBTypes.h" | 29 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBTypes.h" |
29 #include "url/origin.h" | 30 #include "url/origin.h" |
30 | 31 |
31 struct IndexedDBDatabaseMetadata; | 32 struct IndexedDBDatabaseMetadata; |
32 struct IndexedDBMsg_CallbacksSuccessCursorContinue_Params; | 33 struct IndexedDBMsg_CallbacksSuccessCursorContinue_Params; |
33 struct IndexedDBMsg_CallbacksSuccessCursorPrefetch_Params; | 34 struct IndexedDBMsg_CallbacksSuccessCursorPrefetch_Params; |
34 struct IndexedDBMsg_CallbacksSuccessIDBCursor_Params; | 35 struct IndexedDBMsg_CallbacksSuccessIDBCursor_Params; |
35 struct IndexedDBMsg_CallbacksSuccessArray_Params; | 36 struct IndexedDBMsg_CallbacksSuccessArray_Params; |
36 struct IndexedDBMsg_CallbacksSuccessValue_Params; | 37 struct IndexedDBMsg_CallbacksSuccessValue_Params; |
(...skipping 26 matching lines...) Expand all Loading... |
63 ~IndexedDBDispatcher() override; | 64 ~IndexedDBDispatcher() override; |
64 | 65 |
65 // |thread_safe_sender| needs to be passed in because if the call leads to | 66 // |thread_safe_sender| needs to be passed in because if the call leads to |
66 // construction it will be needed. | 67 // construction it will be needed. |
67 static IndexedDBDispatcher* ThreadSpecificInstance( | 68 static IndexedDBDispatcher* ThreadSpecificInstance( |
68 ThreadSafeSender* thread_safe_sender); | 69 ThreadSafeSender* thread_safe_sender); |
69 | 70 |
70 // WorkerThread::Observer implementation. | 71 // WorkerThread::Observer implementation. |
71 void WillStopCurrentWorkerThread() override; | 72 void WillStopCurrentWorkerThread() override; |
72 | 73 |
73 static blink::WebIDBMetadata ConvertMetadata( | |
74 const IndexedDBDatabaseMetadata& idb_metadata); | |
75 static std::vector<blink::WebIDBObservation> ConvertObservations( | 74 static std::vector<blink::WebIDBObservation> ConvertObservations( |
76 const std::vector<IndexedDBMsg_Observation>& idb_observation); | 75 const std::vector<IndexedDBMsg_Observation>& idb_observation); |
77 | 76 |
78 void OnMessageReceived(const IPC::Message& msg); | 77 void OnMessageReceived(const IPC::Message& msg); |
79 | 78 |
80 // This method is virtual so it can be overridden in unit tests. | 79 // This method is virtual so it can be overridden in unit tests. |
81 virtual bool Send(IPC::Message* msg); | 80 virtual bool Send(IPC::Message* msg); |
82 | 81 |
83 int32_t AddIDBObserver(int32_t ipc_database_id, | 82 int32_t AddIDBObserver(int32_t ipc_database_id, |
84 int64_t transaction_id, | 83 int64_t transaction_id, |
85 std::unique_ptr<blink::WebIDBObserver> observer); | 84 std::unique_ptr<blink::WebIDBObserver> observer); |
86 | 85 |
87 // The observer with ID's in |observer_ids_to_remove| observe the | 86 // The observer with ID's in |observer_ids_to_remove| observe the |
88 // |ipc_database_id|. | 87 // |ipc_database_id|. |
89 // We remove our local references to these observer objects, and send an IPC | 88 // We remove our local references to these observer objects, and send an IPC |
90 // to clean up the observers from the backend. | 89 // to clean up the observers from the backend. |
91 void RemoveIDBObserversFromDatabase( | 90 void RemoveIDBObserversFromDatabase( |
92 int32_t ipc_database_id, | 91 int32_t ipc_database_id, |
93 const std::vector<int32_t>& observer_ids_to_remove); | 92 const std::vector<int32_t>& observer_ids_to_remove); |
94 | 93 |
95 // Removes observers from our local map observers_ . No IPC message generated. | 94 // Removes observers from our local map observers_ . No IPC message generated. |
96 void RemoveIDBObservers(const std::set<int32_t>& observer_ids_to_remove); | 95 void RemoveIDBObservers(const std::set<int32_t>& observer_ids_to_remove); |
97 | 96 |
98 void RequestIDBFactoryGetDatabaseNames(blink::WebIDBCallbacks* callbacks, | |
99 const url::Origin& origin); | |
100 | |
101 void RequestIDBFactoryOpen(const base::string16& name, | |
102 int64_t version, | |
103 int64_t transaction_id, | |
104 blink::WebIDBCallbacks* callbacks, | |
105 blink::WebIDBDatabaseCallbacks* database_callbacks, | |
106 const url::Origin& origin); | |
107 | |
108 void RequestIDBFactoryDeleteDatabase(const base::string16& name, | |
109 blink::WebIDBCallbacks* callbacks, | |
110 const url::Origin& origin); | |
111 | |
112 // This method is virtual so it can be overridden in unit tests. | 97 // This method is virtual so it can be overridden in unit tests. |
113 virtual void RequestIDBCursorAdvance(unsigned long count, | 98 virtual void RequestIDBCursorAdvance(unsigned long count, |
114 blink::WebIDBCallbacks* callbacks_ptr, | 99 blink::WebIDBCallbacks* callbacks_ptr, |
115 int32_t ipc_cursor_id, | 100 int32_t ipc_cursor_id, |
116 int64_t transaction_id); | 101 int64_t transaction_id); |
117 | 102 |
118 // This method is virtual so it can be overridden in unit tests. | 103 // This method is virtual so it can be overridden in unit tests. |
119 virtual void RequestIDBCursorContinue(const IndexedDBKey& key, | 104 virtual void RequestIDBCursorContinue(const IndexedDBKey& key, |
120 const IndexedDBKey& primary_key, | 105 const IndexedDBKey& primary_key, |
121 blink::WebIDBCallbacks* callbacks_ptr, | 106 blink::WebIDBCallbacks* callbacks_ptr, |
122 int32_t ipc_cursor_id, | 107 int32_t ipc_cursor_id, |
123 int64_t transaction_id); | 108 int64_t transaction_id); |
124 | 109 |
125 // This method is virtual so it can be overridden in unit tests. | 110 // This method is virtual so it can be overridden in unit tests. |
126 virtual void RequestIDBCursorPrefetch(int n, | 111 virtual void RequestIDBCursorPrefetch(int n, |
127 blink::WebIDBCallbacks* callbacks_ptr, | 112 blink::WebIDBCallbacks* callbacks_ptr, |
128 int32_t ipc_cursor_id); | 113 int32_t ipc_cursor_id); |
129 | 114 |
130 // This method is virtual so it can be overridden in unit tests. | 115 // This method is virtual so it can be overridden in unit tests. |
131 virtual void RequestIDBCursorPrefetchReset(int used_prefetches, | 116 virtual void RequestIDBCursorPrefetchReset(int used_prefetches, |
132 int unused_prefetches, | 117 int unused_prefetches, |
133 int32_t ipc_cursor_id); | 118 int32_t ipc_cursor_id); |
134 | 119 |
135 void RequestIDBDatabaseClose(int32_t ipc_database_id, | 120 void RequestIDBDatabaseClose(int32_t ipc_database_id); |
136 int32_t ipc_database_callbacks_id); | |
137 | 121 |
138 void NotifyIDBDatabaseVersionChangeIgnored(int32_t ipc_database_id); | 122 void NotifyIDBDatabaseVersionChangeIgnored(int32_t ipc_database_id); |
139 | 123 |
140 void RequestIDBDatabaseCreateTransaction( | 124 void RequestIDBDatabaseCreateTransaction( |
141 int32_t ipc_database_id, | 125 int32_t ipc_database_id, |
142 int64_t transaction_id, | 126 int64_t transaction_id, |
143 blink::WebVector<long long> object_store_ids, | 127 blink::WebVector<long long> object_store_ids, |
144 blink::WebIDBTransactionMode mode); | 128 blink::WebIDBTransactionMode mode); |
145 | 129 |
146 void RequestIDBDatabaseGet(int32_t ipc_database_id, | 130 void RequestIDBDatabaseGet(int32_t ipc_database_id, |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 const IndexedDBKeyRange& key_range, | 179 const IndexedDBKeyRange& key_range, |
196 blink::WebIDBCallbacks* callbacks); | 180 blink::WebIDBCallbacks* callbacks); |
197 | 181 |
198 void RequestIDBDatabaseClear(int32_t ipc_database_id, | 182 void RequestIDBDatabaseClear(int32_t ipc_database_id, |
199 int64_t transaction_id, | 183 int64_t transaction_id, |
200 int64_t object_store_id, | 184 int64_t object_store_id, |
201 blink::WebIDBCallbacks* callbacks); | 185 blink::WebIDBCallbacks* callbacks); |
202 | 186 |
203 virtual void CursorDestroyed(int32_t ipc_cursor_id); | 187 virtual void CursorDestroyed(int32_t ipc_cursor_id); |
204 void DatabaseDestroyed(int32_t ipc_database_id); | 188 void DatabaseDestroyed(int32_t ipc_database_id); |
| 189 blink::WebIDBDatabase* RegisterDatabase(int32_t ipc_database_id); |
| 190 |
| 191 void RegisterMojoOwnedCallbacks( |
| 192 IndexedDBCallbacksImpl::InternalState* callback_state); |
| 193 void UnregisterMojoOwnedCallbacks( |
| 194 IndexedDBCallbacksImpl::InternalState* callback_state); |
| 195 void RegisterMojoOwnedDatabaseCallbacks( |
| 196 blink::WebIDBDatabaseCallbacks* callback_state); |
| 197 void UnregisterMojoOwnedDatabaseCallbacks( |
| 198 blink::WebIDBDatabaseCallbacks* callback_state); |
205 | 199 |
206 private: | 200 private: |
207 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest, CursorReset); | 201 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest, CursorReset); |
208 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest, CursorTransactionId); | 202 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest, CursorTransactionId); |
209 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest, ValueSizeTest); | 203 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest, ValueSizeTest); |
210 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest, KeyAndValueSizeTest); | 204 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest, KeyAndValueSizeTest); |
211 | 205 |
212 enum { kAllCursors = -1 }; | 206 enum { kAllCursors = -1 }; |
213 | 207 |
214 static int32_t CurrentWorkerId() { return WorkerThread::GetCurrentId(); } | 208 static int32_t CurrentWorkerId() { return WorkerThread::GetCurrentId(); } |
215 | 209 |
216 template <typename T> | 210 template <typename T> |
217 void init_params(T* params, blink::WebIDBCallbacks* callbacks_ptr) { | 211 void init_params(T* params, blink::WebIDBCallbacks* callbacks_ptr) { |
218 std::unique_ptr<blink::WebIDBCallbacks> callbacks(callbacks_ptr); | 212 std::unique_ptr<blink::WebIDBCallbacks> callbacks(callbacks_ptr); |
219 params->ipc_thread_id = CurrentWorkerId(); | 213 params->ipc_thread_id = CurrentWorkerId(); |
220 params->ipc_callbacks_id = pending_callbacks_.Add(callbacks.release()); | 214 params->ipc_callbacks_id = pending_callbacks_.Add(callbacks.release()); |
221 } | 215 } |
222 | 216 |
223 // IDBCallback message handlers. | 217 // IDBCallback message handlers. |
224 void OnSuccessIDBDatabase(int32_t ipc_thread_id, | |
225 int32_t ipc_callbacks_id, | |
226 int32_t ipc_database_callbacks_id, | |
227 int32_t ipc_object_id, | |
228 const IndexedDBDatabaseMetadata& idb_metadata); | |
229 void OnSuccessIndexedDBKey(int32_t ipc_thread_id, | 218 void OnSuccessIndexedDBKey(int32_t ipc_thread_id, |
230 int32_t ipc_callbacks_id, | 219 int32_t ipc_callbacks_id, |
231 const IndexedDBKey& key); | 220 const IndexedDBKey& key); |
232 | 221 |
233 void OnSuccessOpenCursor( | 222 void OnSuccessOpenCursor( |
234 const IndexedDBMsg_CallbacksSuccessIDBCursor_Params& p); | 223 const IndexedDBMsg_CallbacksSuccessIDBCursor_Params& p); |
235 void OnSuccessCursorContinue( | 224 void OnSuccessCursorContinue( |
236 const IndexedDBMsg_CallbacksSuccessCursorContinue_Params& p); | 225 const IndexedDBMsg_CallbacksSuccessCursorContinue_Params& p); |
237 void OnSuccessCursorPrefetch( | 226 void OnSuccessCursorPrefetch( |
238 const IndexedDBMsg_CallbacksSuccessCursorPrefetch_Params& p); | 227 const IndexedDBMsg_CallbacksSuccessCursorPrefetch_Params& p); |
239 void OnSuccessStringList(int32_t ipc_thread_id, | |
240 int32_t ipc_callbacks_id, | |
241 const std::vector<base::string16>& value); | |
242 void OnSuccessValue(const IndexedDBMsg_CallbacksSuccessValue_Params& p); | 228 void OnSuccessValue(const IndexedDBMsg_CallbacksSuccessValue_Params& p); |
243 void OnSuccessArray(const IndexedDBMsg_CallbacksSuccessArray_Params& p); | 229 void OnSuccessArray(const IndexedDBMsg_CallbacksSuccessArray_Params& p); |
244 void OnSuccessInteger(int32_t ipc_thread_id, | 230 void OnSuccessInteger(int32_t ipc_thread_id, |
245 int32_t ipc_callbacks_id, | 231 int32_t ipc_callbacks_id, |
246 int64_t value); | 232 int64_t value); |
247 void OnSuccessUndefined(int32_t ipc_thread_id, int32_t ipc_callbacks_id); | 233 void OnSuccessUndefined(int32_t ipc_thread_id, int32_t ipc_callbacks_id); |
248 void OnError(int32_t ipc_thread_id, | 234 void OnError(int32_t ipc_thread_id, |
249 int32_t ipc_callbacks_id, | 235 int32_t ipc_callbacks_id, |
250 int code, | 236 int code, |
251 const base::string16& message); | 237 const base::string16& message); |
252 void OnIntBlocked(int32_t ipc_thread_id, | |
253 int32_t ipc_callbacks_id, | |
254 int64_t existing_version); | |
255 void OnUpgradeNeeded(const IndexedDBMsg_CallbacksUpgradeNeeded_Params& p); | |
256 void OnAbort(int32_t ipc_thread_id, | |
257 int32_t ipc_database_id, | |
258 int64_t transaction_id, | |
259 int code, | |
260 const base::string16& message); | |
261 void OnComplete(int32_t ipc_thread_id, | |
262 int32_t ipc_database_id, | |
263 int64_t transaction_id); | |
264 void OnDatabaseChanges(int32_t ipc_thread_id, | 238 void OnDatabaseChanges(int32_t ipc_thread_id, |
265 int32_t ipc_database_id, | 239 int32_t ipc_database_id, |
266 const IndexedDBMsg_ObserverChanges&); | 240 const IndexedDBMsg_ObserverChanges&); |
267 | 241 |
268 void OnForcedClose(int32_t ipc_thread_id, int32_t ipc_database_id); | |
269 void OnVersionChange(int32_t ipc_thread_id, | |
270 int32_t ipc_database_id, | |
271 int64_t old_version, | |
272 int64_t new_version); | |
273 | |
274 // Reset cursor prefetch caches for all cursors except exception_cursor_id. | 242 // Reset cursor prefetch caches for all cursors except exception_cursor_id. |
275 void ResetCursorPrefetchCaches(int64_t transaction_id, | 243 void ResetCursorPrefetchCaches(int64_t transaction_id, |
276 int32_t ipc_exception_cursor_id); | 244 int32_t ipc_exception_cursor_id); |
277 | 245 |
278 scoped_refptr<ThreadSafeSender> thread_safe_sender_; | 246 scoped_refptr<ThreadSafeSender> thread_safe_sender_; |
279 | 247 |
280 // Maximum size (in bytes) of value/key pair allowed for put requests. Any | 248 // Maximum size (in bytes) of value/key pair allowed for put requests. Any |
281 // requests larger than this size will be rejected. | 249 // requests larger than this size will be rejected. |
282 // Used by unit tests to exercise behavior without allocating huge chunks | 250 // Used by unit tests to exercise behavior without allocating huge chunks |
283 // of memory. | 251 // of memory. |
284 size_t max_put_value_size_ = kMaxIDBMessageSizeInBytes; | 252 size_t max_put_value_size_ = kMaxIDBMessageSizeInBytes; |
285 | 253 |
286 // Careful! WebIDBCallbacks wraps non-threadsafe data types. It must be | 254 // Careful! WebIDBCallbacks wraps non-threadsafe data types. It must be |
287 // destroyed and used on the same thread it was created on. | 255 // destroyed and used on the same thread it was created on. |
288 IDMap<blink::WebIDBCallbacks, IDMapOwnPointer> pending_callbacks_; | 256 IDMap<blink::WebIDBCallbacks, IDMapOwnPointer> pending_callbacks_; |
289 IDMap<blink::WebIDBDatabaseCallbacks, IDMapOwnPointer> | |
290 pending_database_callbacks_; | |
291 IDMap<blink::WebIDBObserver, IDMapOwnPointer> observers_; | 257 IDMap<blink::WebIDBObserver, IDMapOwnPointer> observers_; |
292 | 258 |
| 259 // Holds pointers to the worker-thread owned state of IndexedDBCallbacksImpl |
| 260 // and IndexedDBDatabaseCallbacksImpl objects to makes sure that it is |
| 261 // destroyed on thread exit if the Mojo pipe is not yet closed. Otherwise the |
| 262 // object will leak because the thread's task runner is no longer executing |
| 263 // tasks. |
| 264 std::unordered_set<IndexedDBCallbacksImpl::InternalState*> |
| 265 mojo_owned_callback_state_; |
| 266 std::unordered_set<blink::WebIDBDatabaseCallbacks*> |
| 267 mojo_owned_database_callback_state_; |
| 268 |
293 // Maps the ipc_callback_id from an open cursor request to the request's | 269 // Maps the ipc_callback_id from an open cursor request to the request's |
294 // transaction_id. Used to assign the transaction_id to the WebIDBCursorImpl | 270 // transaction_id. Used to assign the transaction_id to the WebIDBCursorImpl |
295 // when it is created. | 271 // when it is created. |
296 std::map<int32_t, int64_t> cursor_transaction_ids_; | 272 std::map<int32_t, int64_t> cursor_transaction_ids_; |
297 | 273 |
298 // Map from cursor id to WebIDBCursorImpl. | 274 // Map from cursor id to WebIDBCursorImpl. |
299 std::map<int32_t, WebIDBCursorImpl*> cursors_; | 275 std::map<int32_t, WebIDBCursorImpl*> cursors_; |
300 | 276 |
301 std::map<int32_t, WebIDBDatabaseImpl*> databases_; | 277 std::map<int32_t, WebIDBDatabaseImpl*> databases_; |
302 | 278 |
303 DISALLOW_COPY_AND_ASSIGN(IndexedDBDispatcher); | 279 DISALLOW_COPY_AND_ASSIGN(IndexedDBDispatcher); |
304 }; | 280 }; |
305 | 281 |
306 } // namespace content | 282 } // namespace content |
307 | 283 |
308 #endif // CONTENT_CHILD_INDEXED_DB_INDEXED_DB_DISPATCHER_H_ | 284 #endif // CONTENT_CHILD_INDEXED_DB_INDEXED_DB_DISPATCHER_H_ |
OLD | NEW |