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 #ifndef CONTENT_CHILD_INDEXED_DB_INDEXED_DB_DISPATCHER_H_
6 #define CONTENT_CHILD_INDEXED_DB_INDEXED_DB_DISPATCHER_H_
12 #include "base/gtest_prod_util.h"
13 #include "base/id_map.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/strings/nullable_string16.h"
16 #include "content/common/content_export.h"
17 #include "content/common/indexed_db/indexed_db_constants.h"
18 #include "content/public/child/worker_thread.h"
19 #include "ipc/ipc_sync_message_filter.h"
20 #include "third_party/WebKit/public/platform/WebBlobInfo.h"
21 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBCallbacks.h"
22 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseCallbacks.h"
23 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBTypes.h"
25 struct IndexedDBDatabaseMetadata
;
26 struct IndexedDBMsg_CallbacksSuccessCursorContinue_Params
;
27 struct IndexedDBMsg_CallbacksSuccessCursorPrefetch_Params
;
28 struct IndexedDBMsg_CallbacksSuccessIDBCursor_Params
;
29 struct IndexedDBMsg_CallbacksSuccessArray_Params
;
30 struct IndexedDBMsg_CallbacksSuccessValue_Params
;
31 struct IndexedDBMsg_CallbacksUpgradeNeeded_Params
;
39 class IndexedDBKeyPath
;
40 class IndexedDBKeyRange
;
41 class WebIDBCursorImpl
;
42 class WebIDBDatabaseImpl
;
43 class ThreadSafeSender
;
45 // Handle the indexed db related communication for this context thread - the
46 // main thread and each worker thread have their own copies.
47 class CONTENT_EXPORT IndexedDBDispatcher
: public WorkerThread::Observer
{
49 // Constructor made public to allow RenderThreadImpl to own a copy without
50 // failing a NOTREACHED in ThreadSpecificInstance in tests that instantiate
51 // two copies of RenderThreadImpl on the same thread. Everyone else probably
52 // wants to use ThreadSpecificInstance().
53 explicit IndexedDBDispatcher(ThreadSafeSender
* thread_safe_sender
);
54 ~IndexedDBDispatcher() override
;
56 // |thread_safe_sender| needs to be passed in because if the call leads to
57 // construction it will be needed.
58 static IndexedDBDispatcher
* ThreadSpecificInstance(
59 ThreadSafeSender
* thread_safe_sender
);
61 // WorkerThread::Observer implementation.
62 void WillStopCurrentWorkerThread() override
;
64 static blink::WebIDBMetadata
ConvertMetadata(
65 const IndexedDBDatabaseMetadata
& idb_metadata
);
67 void OnMessageReceived(const IPC::Message
& msg
);
69 // This method is virtual so it can be overridden in unit tests.
70 virtual bool Send(IPC::Message
* msg
);
72 void RequestIDBFactoryGetDatabaseNames(
73 blink::WebIDBCallbacks
* callbacks
,
74 const std::string
& database_identifier
);
76 void RequestIDBFactoryOpen(
77 const base::string16
& name
,
80 blink::WebIDBCallbacks
* callbacks
,
81 blink::WebIDBDatabaseCallbacks
* database_callbacks
,
82 const std::string
& database_identifier
);
84 void RequestIDBFactoryDeleteDatabase(const base::string16
& name
,
85 blink::WebIDBCallbacks
* callbacks
,
86 const std::string
& database_identifier
);
88 // This method is virtual so it can be overridden in unit tests.
89 virtual void RequestIDBCursorAdvance(unsigned long count
,
90 blink::WebIDBCallbacks
* callbacks_ptr
,
92 int64 transaction_id
);
94 // This method is virtual so it can be overridden in unit tests.
95 virtual void RequestIDBCursorContinue(const IndexedDBKey
& key
,
96 const IndexedDBKey
& primary_key
,
97 blink::WebIDBCallbacks
* callbacks_ptr
,
99 int64 transaction_id
);
101 // This method is virtual so it can be overridden in unit tests.
102 virtual void RequestIDBCursorPrefetch(int n
,
103 blink::WebIDBCallbacks
* callbacks_ptr
,
104 int32 ipc_cursor_id
);
106 // This method is virtual so it can be overridden in unit tests.
107 virtual void RequestIDBCursorPrefetchReset(int used_prefetches
,
108 int unused_prefetches
,
109 int32 ipc_cursor_id
);
111 void RequestIDBDatabaseClose(int32 ipc_database_id
,
112 int32 ipc_database_callbacks_id
);
114 void NotifyIDBDatabaseVersionChangeIgnored(int32 ipc_database_id
);
116 void RequestIDBDatabaseCreateTransaction(
117 int32 ipc_database_id
,
118 int64 transaction_id
,
119 blink::WebIDBDatabaseCallbacks
* database_callbacks_ptr
,
120 blink::WebVector
<long long> object_store_ids
,
121 blink::WebIDBTransactionMode mode
);
123 void RequestIDBDatabaseGet(int32 ipc_database_id
,
124 int64 transaction_id
,
125 int64 object_store_id
,
127 const IndexedDBKeyRange
& key_range
,
129 blink::WebIDBCallbacks
* callbacks
);
131 void RequestIDBDatabaseGetAll(int32 ipc_database_id
,
132 int64 transaction_id
,
133 int64 object_store_id
,
135 const IndexedDBKeyRange
& key_range
,
138 blink::WebIDBCallbacks
* callbacks
);
140 void RequestIDBDatabasePut(
141 int32 ipc_database_id
,
142 int64 transaction_id
,
143 int64 object_store_id
,
144 const blink::WebData
& value
,
145 const blink::WebVector
<blink::WebBlobInfo
>& web_blob_info
,
146 const IndexedDBKey
& key
,
147 blink::WebIDBPutMode put_mode
,
148 blink::WebIDBCallbacks
* callbacks
,
149 const blink::WebVector
<long long>& index_ids
,
150 const blink::WebVector
<blink::WebVector
<blink::WebIDBKey
> >& index_keys
);
152 void RequestIDBDatabaseOpenCursor(int32 ipc_database_id
,
153 int64 transaction_id
,
154 int64 object_store_id
,
156 const IndexedDBKeyRange
& key_range
,
157 blink::WebIDBCursorDirection direction
,
159 blink::WebIDBTaskType task_type
,
160 blink::WebIDBCallbacks
* callbacks
);
162 void RequestIDBDatabaseCount(int32 ipc_database_id
,
163 int64 transaction_id
,
164 int64 object_store_id
,
166 const IndexedDBKeyRange
& key_range
,
167 blink::WebIDBCallbacks
* callbacks
);
169 void RequestIDBDatabaseDeleteRange(int32 ipc_database_id
,
170 int64 transaction_id
,
171 int64 object_store_id
,
172 const IndexedDBKeyRange
& key_range
,
173 blink::WebIDBCallbacks
* callbacks
);
175 void RequestIDBDatabaseClear(int32 ipc_database_id
,
176 int64 transaction_id
,
177 int64 object_store_id
,
178 blink::WebIDBCallbacks
* callbacks
);
180 virtual void CursorDestroyed(int32 ipc_cursor_id
);
181 void DatabaseDestroyed(int32 ipc_database_id
);
184 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest
, CursorReset
);
185 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest
, CursorTransactionId
);
186 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest
, ValueSizeTest
);
187 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest
, KeyAndValueSizeTest
);
189 enum { kAllCursors
= -1 };
191 static int32
CurrentWorkerId() { return WorkerThread::GetCurrentId(); }
193 template <typename T
>
194 void init_params(T
* params
, blink::WebIDBCallbacks
* callbacks_ptr
) {
195 scoped_ptr
<blink::WebIDBCallbacks
> callbacks(callbacks_ptr
);
196 params
->ipc_thread_id
= CurrentWorkerId();
197 params
->ipc_callbacks_id
= pending_callbacks_
.Add(callbacks
.release());
200 // IDBCallback message handlers.
201 void OnSuccessIDBDatabase(int32 ipc_thread_id
,
202 int32 ipc_callbacks_id
,
203 int32 ipc_database_callbacks_id
,
205 const IndexedDBDatabaseMetadata
& idb_metadata
);
206 void OnSuccessIndexedDBKey(int32 ipc_thread_id
,
207 int32 ipc_callbacks_id
,
208 const IndexedDBKey
& key
);
210 void OnSuccessOpenCursor(
211 const IndexedDBMsg_CallbacksSuccessIDBCursor_Params
& p
);
212 void OnSuccessCursorContinue(
213 const IndexedDBMsg_CallbacksSuccessCursorContinue_Params
& p
);
214 void OnSuccessCursorPrefetch(
215 const IndexedDBMsg_CallbacksSuccessCursorPrefetch_Params
& p
);
216 void OnSuccessStringList(int32 ipc_thread_id
,
217 int32 ipc_callbacks_id
,
218 const std::vector
<base::string16
>& value
);
219 void OnSuccessValue(const IndexedDBMsg_CallbacksSuccessValue_Params
& p
);
220 void OnSuccessArray(const IndexedDBMsg_CallbacksSuccessArray_Params
& p
);
221 void OnSuccessInteger(int32 ipc_thread_id
,
222 int32 ipc_callbacks_id
,
224 void OnSuccessUndefined(int32 ipc_thread_id
, int32 ipc_callbacks_id
);
225 void OnError(int32 ipc_thread_id
,
226 int32 ipc_callbacks_id
,
228 const base::string16
& message
);
229 void OnIntBlocked(int32 ipc_thread_id
,
230 int32 ipc_callbacks_id
,
231 int64 existing_version
);
232 void OnUpgradeNeeded(const IndexedDBMsg_CallbacksUpgradeNeeded_Params
& p
);
233 void OnAbort(int32 ipc_thread_id
,
234 int32 ipc_database_id
,
235 int64 transaction_id
,
237 const base::string16
& message
);
238 void OnComplete(int32 ipc_thread_id
,
239 int32 ipc_database_id
,
240 int64 transaction_id
);
241 void OnForcedClose(int32 ipc_thread_id
, int32 ipc_database_id
);
242 void OnIntVersionChange(int32 ipc_thread_id
,
243 int32 ipc_database_id
,
247 // Reset cursor prefetch caches for all cursors except exception_cursor_id.
248 void ResetCursorPrefetchCaches(int64 transaction_id
,
249 int32 ipc_exception_cursor_id
);
251 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
253 // Maximum size (in bytes) of value/key pair allowed for put requests. Any
254 // requests larger than this size will be rejected.
255 // Used by unit tests to exercise behavior without allocating huge chunks
257 size_t max_put_value_size_
= kMaxIDBMessageSizeInBytes
;
259 // Careful! WebIDBCallbacks wraps non-threadsafe data types. It must be
260 // destroyed and used on the same thread it was created on.
261 IDMap
<blink::WebIDBCallbacks
, IDMapOwnPointer
> pending_callbacks_
;
262 IDMap
<blink::WebIDBDatabaseCallbacks
, IDMapOwnPointer
>
263 pending_database_callbacks_
;
265 // Maps the ipc_callback_id from an open cursor request to the request's
266 // transaction_id. Used to assign the transaction_id to the WebIDBCursorImpl
267 // when it is created.
268 std::map
<int32
, int64
> cursor_transaction_ids_
;
270 // Map from cursor id to WebIDBCursorImpl.
271 std::map
<int32
, WebIDBCursorImpl
*> cursors_
;
273 std::map
<int32
, WebIDBDatabaseImpl
*> databases_
;
275 DISALLOW_COPY_AND_ASSIGN(IndexedDBDispatcher
);
278 } // namespace content
280 #endif // CONTENT_CHILD_INDEXED_DB_INDEXED_DB_DISPATCHER_H_