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/child/worker_task_runner.h"
17 #include "content/common/content_export.h"
18 #include "ipc/ipc_sync_message_filter.h"
19 #include "third_party/WebKit/public/platform/WebBlobInfo.h"
20 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBCallbacks.h"
21 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseCallbacks.h"
22 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBTypes.h"
24 struct IndexedDBDatabaseMetadata
;
25 struct IndexedDBMsg_CallbacksSuccessCursorContinue_Params
;
26 struct IndexedDBMsg_CallbacksSuccessCursorPrefetch_Params
;
27 struct IndexedDBMsg_CallbacksSuccessIDBCursor_Params
;
28 struct IndexedDBMsg_CallbacksSuccessArray_Params
;
29 struct IndexedDBMsg_CallbacksSuccessValue_Params
;
30 struct IndexedDBMsg_CallbacksUpgradeNeeded_Params
;
38 class IndexedDBKeyPath
;
39 class IndexedDBKeyRange
;
40 class WebIDBCursorImpl
;
41 class WebIDBDatabaseImpl
;
42 class ThreadSafeSender
;
44 CONTENT_EXPORT
extern const size_t kMaxIDBValueSizeInBytes
;
46 // Handle the indexed db related communication for this context thread - the
47 // main thread and each worker thread have their own copies.
48 class CONTENT_EXPORT IndexedDBDispatcher
: public WorkerTaskRunner::Observer
{
50 // Constructor made public to allow RenderThreadImpl to own a copy without
51 // failing a NOTREACHED in ThreadSpecificInstance in tests that instantiate
52 // two copies of RenderThreadImpl on the same thread. Everyone else probably
53 // wants to use ThreadSpecificInstance().
54 explicit IndexedDBDispatcher(ThreadSafeSender
* thread_safe_sender
);
55 ~IndexedDBDispatcher() override
;
57 // |thread_safe_sender| needs to be passed in because if the call leads to
58 // construction it will be needed.
59 static IndexedDBDispatcher
* ThreadSpecificInstance(
60 ThreadSafeSender
* thread_safe_sender
);
62 // WorkerTaskRunner::Observer implementation.
63 void OnWorkerRunLoopStopped() override
;
65 static blink::WebIDBMetadata
ConvertMetadata(
66 const IndexedDBDatabaseMetadata
& idb_metadata
);
68 void OnMessageReceived(const IPC::Message
& msg
);
70 // This method is virtual so it can be overridden in unit tests.
71 virtual bool Send(IPC::Message
* msg
);
73 void RequestIDBFactoryGetDatabaseNames(
74 blink::WebIDBCallbacks
* callbacks
,
75 const std::string
& database_identifier
);
77 void RequestIDBFactoryOpen(
78 const base::string16
& name
,
81 blink::WebIDBCallbacks
* callbacks
,
82 blink::WebIDBDatabaseCallbacks
* database_callbacks
,
83 const std::string
& database_identifier
);
85 void RequestIDBFactoryDeleteDatabase(const base::string16
& name
,
86 blink::WebIDBCallbacks
* callbacks
,
87 const std::string
& database_identifier
);
89 // This method is virtual so it can be overridden in unit tests.
90 virtual void RequestIDBCursorAdvance(unsigned long count
,
91 blink::WebIDBCallbacks
* callbacks_ptr
,
93 int64 transaction_id
);
95 // This method is virtual so it can be overridden in unit tests.
96 virtual void RequestIDBCursorContinue(const IndexedDBKey
& key
,
97 const IndexedDBKey
& primary_key
,
98 blink::WebIDBCallbacks
* callbacks_ptr
,
100 int64 transaction_id
);
102 // This method is virtual so it can be overridden in unit tests.
103 virtual void RequestIDBCursorPrefetch(int n
,
104 blink::WebIDBCallbacks
* callbacks_ptr
,
105 int32 ipc_cursor_id
);
107 // This method is virtual so it can be overridden in unit tests.
108 virtual void RequestIDBCursorPrefetchReset(int used_prefetches
,
109 int unused_prefetches
,
110 int32 ipc_cursor_id
);
112 void RequestIDBDatabaseClose(int32 ipc_database_id
,
113 int32 ipc_database_callbacks_id
);
115 void NotifyIDBDatabaseVersionChangeIgnored(int32 ipc_database_id
);
117 void RequestIDBDatabaseCreateTransaction(
118 int32 ipc_database_id
,
119 int64 transaction_id
,
120 blink::WebIDBDatabaseCallbacks
* database_callbacks_ptr
,
121 blink::WebVector
<long long> object_store_ids
,
122 blink::WebIDBTransactionMode mode
);
124 void RequestIDBDatabaseGet(int32 ipc_database_id
,
125 int64 transaction_id
,
126 int64 object_store_id
,
128 const IndexedDBKeyRange
& key_range
,
130 blink::WebIDBCallbacks
* callbacks
);
132 void RequestIDBDatabaseGetAll(int32 ipc_database_id
,
133 int64 transaction_id
,
134 int64 object_store_id
,
136 const IndexedDBKeyRange
& key_range
,
139 blink::WebIDBCallbacks
* callbacks
);
141 void RequestIDBDatabasePut(
142 int32 ipc_database_id
,
143 int64 transaction_id
,
144 int64 object_store_id
,
145 const blink::WebData
& value
,
146 const blink::WebVector
<blink::WebBlobInfo
>& web_blob_info
,
147 const IndexedDBKey
& key
,
148 blink::WebIDBPutMode put_mode
,
149 blink::WebIDBCallbacks
* callbacks
,
150 const blink::WebVector
<long long>& index_ids
,
151 const blink::WebVector
<blink::WebVector
<blink::WebIDBKey
> >& index_keys
);
153 void RequestIDBDatabaseOpenCursor(int32 ipc_database_id
,
154 int64 transaction_id
,
155 int64 object_store_id
,
157 const IndexedDBKeyRange
& key_range
,
158 blink::WebIDBCursorDirection direction
,
160 blink::WebIDBTaskType task_type
,
161 blink::WebIDBCallbacks
* callbacks
);
163 void RequestIDBDatabaseCount(int32 ipc_database_id
,
164 int64 transaction_id
,
165 int64 object_store_id
,
167 const IndexedDBKeyRange
& key_range
,
168 blink::WebIDBCallbacks
* callbacks
);
170 void RequestIDBDatabaseDeleteRange(int32 ipc_database_id
,
171 int64 transaction_id
,
172 int64 object_store_id
,
173 const IndexedDBKeyRange
& key_range
,
174 blink::WebIDBCallbacks
* callbacks
);
176 void RequestIDBDatabaseClear(int32 ipc_database_id
,
177 int64 transaction_id
,
178 int64 object_store_id
,
179 blink::WebIDBCallbacks
* callbacks
);
181 virtual void CursorDestroyed(int32 ipc_cursor_id
);
182 void DatabaseDestroyed(int32 ipc_database_id
);
185 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest
, CursorReset
);
186 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest
, CursorTransactionId
);
187 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest
, ValueSizeTest
);
189 enum { kAllCursors
= -1 };
191 static int32
CurrentWorkerId() {
192 return WorkerTaskRunner::Instance()->CurrentWorkerId();
195 template <typename T
>
196 void init_params(T
* params
, blink::WebIDBCallbacks
* callbacks_ptr
) {
197 scoped_ptr
<blink::WebIDBCallbacks
> callbacks(callbacks_ptr
);
198 params
->ipc_thread_id
= CurrentWorkerId();
199 params
->ipc_callbacks_id
= pending_callbacks_
.Add(callbacks
.release());
202 // IDBCallback message handlers.
203 void OnSuccessIDBDatabase(int32 ipc_thread_id
,
204 int32 ipc_callbacks_id
,
205 int32 ipc_database_callbacks_id
,
207 const IndexedDBDatabaseMetadata
& idb_metadata
);
208 void OnSuccessIndexedDBKey(int32 ipc_thread_id
,
209 int32 ipc_callbacks_id
,
210 const IndexedDBKey
& key
);
212 void OnSuccessOpenCursor(
213 const IndexedDBMsg_CallbacksSuccessIDBCursor_Params
& p
);
214 void OnSuccessCursorContinue(
215 const IndexedDBMsg_CallbacksSuccessCursorContinue_Params
& p
);
216 void OnSuccessCursorPrefetch(
217 const IndexedDBMsg_CallbacksSuccessCursorPrefetch_Params
& p
);
218 void OnSuccessStringList(int32 ipc_thread_id
,
219 int32 ipc_callbacks_id
,
220 const std::vector
<base::string16
>& value
);
221 void OnSuccessValue(const IndexedDBMsg_CallbacksSuccessValue_Params
& p
);
222 void OnSuccessArray(const IndexedDBMsg_CallbacksSuccessArray_Params
& p
);
223 void OnSuccessInteger(int32 ipc_thread_id
,
224 int32 ipc_callbacks_id
,
226 void OnSuccessUndefined(int32 ipc_thread_id
, int32 ipc_callbacks_id
);
227 void OnError(int32 ipc_thread_id
,
228 int32 ipc_callbacks_id
,
230 const base::string16
& message
);
231 void OnIntBlocked(int32 ipc_thread_id
,
232 int32 ipc_callbacks_id
,
233 int64 existing_version
);
234 void OnUpgradeNeeded(const IndexedDBMsg_CallbacksUpgradeNeeded_Params
& p
);
235 void OnAbort(int32 ipc_thread_id
,
236 int32 ipc_database_id
,
237 int64 transaction_id
,
239 const base::string16
& message
);
240 void OnComplete(int32 ipc_thread_id
,
241 int32 ipc_database_id
,
242 int64 transaction_id
);
243 void OnForcedClose(int32 ipc_thread_id
, int32 ipc_database_id
);
244 void OnIntVersionChange(int32 ipc_thread_id
,
245 int32 ipc_database_id
,
249 // Reset cursor prefetch caches for all cursors except exception_cursor_id.
250 void ResetCursorPrefetchCaches(int64 transaction_id
,
251 int32 ipc_exception_cursor_id
);
253 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
255 // Careful! WebIDBCallbacks wraps non-threadsafe data types. It must be
256 // destroyed and used on the same thread it was created on.
257 IDMap
<blink::WebIDBCallbacks
, IDMapOwnPointer
> pending_callbacks_
;
258 IDMap
<blink::WebIDBDatabaseCallbacks
, IDMapOwnPointer
>
259 pending_database_callbacks_
;
261 // Maps the ipc_callback_id from an open cursor request to the request's
262 // transaction_id. Used to assign the transaction_id to the WebIDBCursorImpl
263 // when it is created.
264 std::map
<int32
, int64
> cursor_transaction_ids_
;
266 // Map from cursor id to WebIDBCursorImpl.
267 std::map
<int32
, WebIDBCursorImpl
*> cursors_
;
269 std::map
<int32
, WebIDBDatabaseImpl
*> databases_
;
271 DISALLOW_COPY_AND_ASSIGN(IndexedDBDispatcher
);
274 } // namespace content
276 #endif // CONTENT_CHILD_INDEXED_DB_INDEXED_DB_DISPATCHER_H_