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/WebIDBCallbacks.h"
21 #include "third_party/WebKit/public/platform/WebIDBDatabaseCallbacks.h"
22 #include "third_party/WebKit/public/platform/WebIDBTypes.h"
24 struct IndexedDBDatabaseMetadata
;
25 struct IndexedDBMsg_CallbacksSuccessCursorContinue_Params
;
26 struct IndexedDBMsg_CallbacksSuccessCursorPrefetch_Params
;
27 struct IndexedDBMsg_CallbacksSuccessIDBCursor_Params
;
28 struct IndexedDBMsg_CallbacksSuccessValue_Params
;
29 struct IndexedDBMsg_CallbacksSuccessValueWithKey_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 virtual ~IndexedDBDispatcher();
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 virtual 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 RequestIDBDatabasePut(
133 int32 ipc_database_id
,
134 int64 transaction_id
,
135 int64 object_store_id
,
136 const blink::WebData
& value
,
137 const blink::WebVector
<blink::WebBlobInfo
>& web_blob_info
,
138 const IndexedDBKey
& key
,
139 blink::WebIDBPutMode put_mode
,
140 blink::WebIDBCallbacks
* callbacks
,
141 const blink::WebVector
<long long>& index_ids
,
142 const blink::WebVector
<blink::WebVector
<blink::WebIDBKey
> >& index_keys
);
144 void RequestIDBDatabaseOpenCursor(int32 ipc_database_id
,
145 int64 transaction_id
,
146 int64 object_store_id
,
148 const IndexedDBKeyRange
& key_range
,
149 blink::WebIDBCursorDirection direction
,
151 blink::WebIDBTaskType task_type
,
152 blink::WebIDBCallbacks
* callbacks
);
154 void RequestIDBDatabaseCount(int32 ipc_database_id
,
155 int64 transaction_id
,
156 int64 object_store_id
,
158 const IndexedDBKeyRange
& key_range
,
159 blink::WebIDBCallbacks
* callbacks
);
161 void RequestIDBDatabaseDeleteRange(int32 ipc_database_id
,
162 int64 transaction_id
,
163 int64 object_store_id
,
164 const IndexedDBKeyRange
& key_range
,
165 blink::WebIDBCallbacks
* callbacks
);
167 void RequestIDBDatabaseClear(int32 ipc_database_id
,
168 int64 transaction_id
,
169 int64 object_store_id
,
170 blink::WebIDBCallbacks
* callbacks
);
172 virtual void CursorDestroyed(int32 ipc_cursor_id
);
173 void DatabaseDestroyed(int32 ipc_database_id
);
176 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest
, CursorReset
);
177 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest
, CursorTransactionId
);
178 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest
, ValueSizeTest
);
180 enum { kAllCursors
= -1 };
182 static int32
CurrentWorkerId() {
183 return WorkerTaskRunner::Instance()->CurrentWorkerId();
186 template <typename T
>
187 void init_params(T
* params
, blink::WebIDBCallbacks
* callbacks_ptr
) {
188 scoped_ptr
<blink::WebIDBCallbacks
> callbacks(callbacks_ptr
);
189 params
->ipc_thread_id
= CurrentWorkerId();
190 params
->ipc_callbacks_id
= pending_callbacks_
.Add(callbacks
.release());
193 // IDBCallback message handlers.
194 void OnSuccessIDBDatabase(int32 ipc_thread_id
,
195 int32 ipc_callbacks_id
,
196 int32 ipc_database_callbacks_id
,
198 const IndexedDBDatabaseMetadata
& idb_metadata
);
199 void OnSuccessIndexedDBKey(int32 ipc_thread_id
,
200 int32 ipc_callbacks_id
,
201 const IndexedDBKey
& key
);
203 void OnSuccessOpenCursor(
204 const IndexedDBMsg_CallbacksSuccessIDBCursor_Params
& p
);
205 void OnSuccessCursorContinue(
206 const IndexedDBMsg_CallbacksSuccessCursorContinue_Params
& p
);
207 void OnSuccessCursorPrefetch(
208 const IndexedDBMsg_CallbacksSuccessCursorPrefetch_Params
& p
);
209 void OnSuccessStringList(int32 ipc_thread_id
,
210 int32 ipc_callbacks_id
,
211 const std::vector
<base::string16
>& value
);
212 void OnSuccessValue(const IndexedDBMsg_CallbacksSuccessValue_Params
& p
);
213 void OnSuccessValueWithKey(
214 const IndexedDBMsg_CallbacksSuccessValueWithKey_Params
& p
);
215 void OnSuccessInteger(int32 ipc_thread_id
,
216 int32 ipc_callbacks_id
,
218 void OnSuccessUndefined(int32 ipc_thread_id
, int32 ipc_callbacks_id
);
219 void OnError(int32 ipc_thread_id
,
220 int32 ipc_callbacks_id
,
222 const base::string16
& message
);
223 void OnIntBlocked(int32 ipc_thread_id
,
224 int32 ipc_callbacks_id
,
225 int64 existing_version
);
226 void OnUpgradeNeeded(const IndexedDBMsg_CallbacksUpgradeNeeded_Params
& p
);
227 void OnAbort(int32 ipc_thread_id
,
228 int32 ipc_database_id
,
229 int64 transaction_id
,
231 const base::string16
& message
);
232 void OnComplete(int32 ipc_thread_id
,
233 int32 ipc_database_id
,
234 int64 transaction_id
);
235 void OnForcedClose(int32 ipc_thread_id
, int32 ipc_database_id
);
236 void OnIntVersionChange(int32 ipc_thread_id
,
237 int32 ipc_database_id
,
241 // Reset cursor prefetch caches for all cursors except exception_cursor_id.
242 void ResetCursorPrefetchCaches(int64 transaction_id
,
243 int32 ipc_exception_cursor_id
);
245 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
247 // Careful! WebIDBCallbacks wraps non-threadsafe data types. It must be
248 // destroyed and used on the same thread it was created on.
249 IDMap
<blink::WebIDBCallbacks
, IDMapOwnPointer
> pending_callbacks_
;
250 IDMap
<blink::WebIDBDatabaseCallbacks
, IDMapOwnPointer
>
251 pending_database_callbacks_
;
253 // Maps the ipc_callback_id from an open cursor request to the request's
254 // transaction_id. Used to assign the transaction_id to the WebIDBCursorImpl
255 // when it is created.
256 std::map
<int32
, int64
> cursor_transaction_ids_
;
258 // Map from cursor id to WebIDBCursorImpl.
259 std::map
<int32
, WebIDBCursorImpl
*> cursors_
;
261 std::map
<int32
, WebIDBDatabaseImpl
*> databases_
;
263 DISALLOW_COPY_AND_ASSIGN(IndexedDBDispatcher
);
266 } // namespace content
268 #endif // CONTENT_CHILD_INDEXED_DB_INDEXED_DB_DISPATCHER_H_