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_CallbacksUpgradeNeeded_Params
;
37 class IndexedDBKeyPath
;
38 class IndexedDBKeyRange
;
39 class WebIDBCursorImpl
;
40 class WebIDBDatabaseImpl
;
41 class ThreadSafeSender
;
43 CONTENT_EXPORT
extern const size_t kMaxIDBValueSizeInBytes
;
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 WorkerTaskRunner::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 // WorkerTaskRunner::Observer implementation.
62 void OnWorkerRunLoopStopped() 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 RequestIDBDatabasePut(
132 int32 ipc_database_id
,
133 int64 transaction_id
,
134 int64 object_store_id
,
135 const blink::WebData
& value
,
136 const blink::WebVector
<blink::WebBlobInfo
>& web_blob_info
,
137 const IndexedDBKey
& key
,
138 blink::WebIDBPutMode put_mode
,
139 blink::WebIDBCallbacks
* callbacks
,
140 const blink::WebVector
<long long>& index_ids
,
141 const blink::WebVector
<blink::WebVector
<blink::WebIDBKey
> >& index_keys
);
143 void RequestIDBDatabaseOpenCursor(int32 ipc_database_id
,
144 int64 transaction_id
,
145 int64 object_store_id
,
147 const IndexedDBKeyRange
& key_range
,
148 blink::WebIDBCursorDirection direction
,
150 blink::WebIDBTaskType task_type
,
151 blink::WebIDBCallbacks
* callbacks
);
153 void RequestIDBDatabaseCount(int32 ipc_database_id
,
154 int64 transaction_id
,
155 int64 object_store_id
,
157 const IndexedDBKeyRange
& key_range
,
158 blink::WebIDBCallbacks
* callbacks
);
160 void RequestIDBDatabaseDeleteRange(int32 ipc_database_id
,
161 int64 transaction_id
,
162 int64 object_store_id
,
163 const IndexedDBKeyRange
& key_range
,
164 blink::WebIDBCallbacks
* callbacks
);
166 void RequestIDBDatabaseClear(int32 ipc_database_id
,
167 int64 transaction_id
,
168 int64 object_store_id
,
169 blink::WebIDBCallbacks
* callbacks
);
171 virtual void CursorDestroyed(int32 ipc_cursor_id
);
172 void DatabaseDestroyed(int32 ipc_database_id
);
175 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest
, CursorReset
);
176 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest
, CursorTransactionId
);
177 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest
, ValueSizeTest
);
179 enum { kAllCursors
= -1 };
181 static int32
CurrentWorkerId() {
182 return WorkerTaskRunner::Instance()->CurrentWorkerId();
185 template <typename T
>
186 void init_params(T
* params
, blink::WebIDBCallbacks
* callbacks_ptr
) {
187 scoped_ptr
<blink::WebIDBCallbacks
> callbacks(callbacks_ptr
);
188 params
->ipc_thread_id
= CurrentWorkerId();
189 params
->ipc_callbacks_id
= pending_callbacks_
.Add(callbacks
.release());
192 // IDBCallback message handlers.
193 void OnSuccessIDBDatabase(int32 ipc_thread_id
,
194 int32 ipc_callbacks_id
,
195 int32 ipc_database_callbacks_id
,
197 const IndexedDBDatabaseMetadata
& idb_metadata
);
198 void OnSuccessIndexedDBKey(int32 ipc_thread_id
,
199 int32 ipc_callbacks_id
,
200 const IndexedDBKey
& key
);
202 void OnSuccessOpenCursor(
203 const IndexedDBMsg_CallbacksSuccessIDBCursor_Params
& p
);
204 void OnSuccessCursorContinue(
205 const IndexedDBMsg_CallbacksSuccessCursorContinue_Params
& p
);
206 void OnSuccessCursorPrefetch(
207 const IndexedDBMsg_CallbacksSuccessCursorPrefetch_Params
& p
);
208 void OnSuccessStringList(int32 ipc_thread_id
,
209 int32 ipc_callbacks_id
,
210 const std::vector
<base::string16
>& value
);
211 void OnSuccessValue(const IndexedDBMsg_CallbacksSuccessValue_Params
& p
);
212 void OnSuccessInteger(int32 ipc_thread_id
,
213 int32 ipc_callbacks_id
,
215 void OnSuccessUndefined(int32 ipc_thread_id
, int32 ipc_callbacks_id
);
216 void OnError(int32 ipc_thread_id
,
217 int32 ipc_callbacks_id
,
219 const base::string16
& message
);
220 void OnIntBlocked(int32 ipc_thread_id
,
221 int32 ipc_callbacks_id
,
222 int64 existing_version
);
223 void OnUpgradeNeeded(const IndexedDBMsg_CallbacksUpgradeNeeded_Params
& p
);
224 void OnAbort(int32 ipc_thread_id
,
225 int32 ipc_database_id
,
226 int64 transaction_id
,
228 const base::string16
& message
);
229 void OnComplete(int32 ipc_thread_id
,
230 int32 ipc_database_id
,
231 int64 transaction_id
);
232 void OnForcedClose(int32 ipc_thread_id
, int32 ipc_database_id
);
233 void OnIntVersionChange(int32 ipc_thread_id
,
234 int32 ipc_database_id
,
238 // Reset cursor prefetch caches for all cursors except exception_cursor_id.
239 void ResetCursorPrefetchCaches(int64 transaction_id
,
240 int32 ipc_exception_cursor_id
);
242 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
244 // Careful! WebIDBCallbacks wraps non-threadsafe data types. It must be
245 // destroyed and used on the same thread it was created on.
246 IDMap
<blink::WebIDBCallbacks
, IDMapOwnPointer
> pending_callbacks_
;
247 IDMap
<blink::WebIDBDatabaseCallbacks
, IDMapOwnPointer
>
248 pending_database_callbacks_
;
250 // Maps the ipc_callback_id from an open cursor request to the request's
251 // transaction_id. Used to assign the transaction_id to the WebIDBCursorImpl
252 // when it is created.
253 std::map
<int32
, int64
> cursor_transaction_ids_
;
255 // Map from cursor id to WebIDBCursorImpl.
256 std::map
<int32
, WebIDBCursorImpl
*> cursors_
;
258 std::map
<int32
, WebIDBDatabaseImpl
*> databases_
;
260 DISALLOW_COPY_AND_ASSIGN(IndexedDBDispatcher
);
263 } // namespace content
265 #endif // CONTENT_CHILD_INDEXED_DB_INDEXED_DB_DISPATCHER_H_