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_
11 #include "base/gtest_prod_util.h"
12 #include "base/id_map.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/strings/nullable_string16.h"
15 #include "content/common/content_export.h"
16 #include "ipc/ipc_sync_message_filter.h"
17 #include "third_party/WebKit/public/platform/WebIDBCallbacks.h"
18 #include "third_party/WebKit/public/platform/WebIDBCursor.h"
19 #include "third_party/WebKit/public/platform/WebIDBDatabase.h"
20 #include "third_party/WebKit/public/platform/WebIDBDatabaseCallbacks.h"
21 #include "webkit/child/worker_task_runner.h"
23 struct IndexedDBDatabaseMetadata
;
24 struct IndexedDBMsg_CallbacksSuccessCursorContinue_Params
;
25 struct IndexedDBMsg_CallbacksSuccessCursorPrefetch_Params
;
26 struct IndexedDBMsg_CallbacksSuccessIDBCursor_Params
;
27 struct IndexedDBMsg_CallbacksUpgradeNeeded_Params
;
35 class IndexedDBKeyPath
;
36 class IndexedDBKeyRange
;
37 class WebIDBCursorImpl
;
38 class WebIDBDatabaseImpl
;
39 class ThreadSafeSender
;
41 CONTENT_EXPORT
extern const size_t kMaxIDBValueSizeInBytes
;
43 // Handle the indexed db related communication for this context thread - the
44 // main thread and each worker thread have their own copies.
45 class CONTENT_EXPORT IndexedDBDispatcher
46 : public webkit_glue::WorkerTaskRunner::Observer
{
48 // Constructor made public to allow RenderThreadImpl to own a copy without
49 // failing a NOTREACHED in ThreadSpecificInstance in tests that instantiate
50 // two copies of RenderThreadImpl on the same thread. Everyone else probably
51 // wants to use ThreadSpecificInstance().
52 explicit IndexedDBDispatcher(ThreadSafeSender
* thread_safe_sender
);
53 virtual ~IndexedDBDispatcher();
55 // |thread_safe_sender| needs to be passed in because if the call leads to
56 // construction it will be needed.
57 static IndexedDBDispatcher
* ThreadSpecificInstance(
58 ThreadSafeSender
* thread_safe_sender
);
60 // webkit_glue::WorkerTaskRunner::Observer implementation.
61 virtual void OnWorkerRunLoopStopped() OVERRIDE
;
63 static blink::WebIDBMetadata
ConvertMetadata(
64 const IndexedDBDatabaseMetadata
& idb_metadata
);
66 void OnMessageReceived(const IPC::Message
& msg
);
67 bool Send(IPC::Message
* msg
);
69 void RequestIDBFactoryGetDatabaseNames(
70 blink::WebIDBCallbacks
* callbacks
,
71 const std::string
& database_identifier
);
73 void RequestIDBFactoryOpen(
74 const base::string16
& name
,
77 blink::WebIDBCallbacks
* callbacks
,
78 blink::WebIDBDatabaseCallbacks
* database_callbacks
,
79 const std::string
& database_identifier
);
81 void RequestIDBFactoryDeleteDatabase(const base::string16
& name
,
82 blink::WebIDBCallbacks
* callbacks
,
83 const std::string
& database_identifier
);
85 // This method is virtual so it can be overridden in unit tests.
86 virtual void RequestIDBCursorAdvance(unsigned long count
,
87 blink::WebIDBCallbacks
* callbacks_ptr
,
90 // This method is virtual so it can be overridden in unit tests.
91 virtual void RequestIDBCursorContinue(const IndexedDBKey
& key
,
92 const IndexedDBKey
& primary_key
,
93 blink::WebIDBCallbacks
* callbacks_ptr
,
96 // This method is virtual so it can be overridden in unit tests.
97 virtual void RequestIDBCursorPrefetch(int n
,
98 blink::WebIDBCallbacks
* callbacks_ptr
,
101 // This method is virtual so it can be overridden in unit tests.
102 virtual void RequestIDBCursorPrefetchReset(int used_prefetches
,
103 int unused_prefetches
,
104 int32 ipc_cursor_id
);
106 void RequestIDBDatabaseClose(int32 ipc_database_id
,
107 int32 ipc_database_callbacks_id
);
109 void RequestIDBDatabaseCreateTransaction(
110 int32 ipc_database_id
,
111 int64 transaction_id
,
112 blink::WebIDBDatabaseCallbacks
* database_callbacks_ptr
,
113 blink::WebVector
<long long> object_store_ids
,
114 unsigned short mode
);
116 void RequestIDBDatabaseGet(int32 ipc_database_id
,
117 int64 transaction_id
,
118 int64 object_store_id
,
120 const IndexedDBKeyRange
& key_range
,
122 blink::WebIDBCallbacks
* callbacks
);
124 void RequestIDBDatabasePut(
125 int32 ipc_database_id
,
126 int64 transaction_id
,
127 int64 object_store_id
,
128 const blink::WebData
& value
,
129 const IndexedDBKey
& key
,
130 blink::WebIDBDatabase::PutMode put_mode
,
131 blink::WebIDBCallbacks
* callbacks
,
132 const blink::WebVector
<long long>& index_ids
,
133 const blink::WebVector
<blink::WebVector
<blink::WebIDBKey
> >&
136 void RequestIDBDatabaseOpenCursor(int32 ipc_database_id
,
137 int64 transaction_id
,
138 int64 object_store_id
,
140 const IndexedDBKeyRange
& key_range
,
141 unsigned short direction
,
143 blink::WebIDBDatabase::TaskType task_type
,
144 blink::WebIDBCallbacks
* callbacks
);
146 void RequestIDBDatabaseCount(int32 ipc_database_id
,
147 int64 transaction_id
,
148 int64 object_store_id
,
150 const IndexedDBKeyRange
& key_range
,
151 blink::WebIDBCallbacks
* callbacks
);
153 void RequestIDBDatabaseDeleteRange(int32 ipc_database_id
,
154 int64 transaction_id
,
155 int64 object_store_id
,
156 const IndexedDBKeyRange
& key_range
,
157 blink::WebIDBCallbacks
* callbacks
);
159 void RequestIDBDatabaseClear(int32 ipc_database_id
,
160 int64 transaction_id
,
161 int64 object_store_id
,
162 blink::WebIDBCallbacks
* callbacks
);
164 virtual void CursorDestroyed(int32 ipc_cursor_id
);
165 void DatabaseDestroyed(int32 ipc_database_id
);
168 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest
, ValueSizeTest
);
170 static int32
CurrentWorkerId() {
171 return webkit_glue::WorkerTaskRunner::Instance()->CurrentWorkerId();
174 template <typename T
>
175 void init_params(T
& params
, blink::WebIDBCallbacks
* callbacks_ptr
) {
176 scoped_ptr
<blink::WebIDBCallbacks
> callbacks(callbacks_ptr
);
177 params
.ipc_thread_id
= CurrentWorkerId();
178 params
.ipc_callbacks_id
= pending_callbacks_
.Add(callbacks
.release());
181 // IDBCallback message handlers.
182 void OnSuccessIDBDatabase(int32 ipc_thread_id
,
183 int32 ipc_callbacks_id
,
184 int32 ipc_database_callbacks_id
,
186 const IndexedDBDatabaseMetadata
& idb_metadata
);
187 void OnSuccessIndexedDBKey(int32 ipc_thread_id
,
188 int32 ipc_callbacks_id
,
189 const IndexedDBKey
& key
);
191 void OnSuccessOpenCursor(
192 const IndexedDBMsg_CallbacksSuccessIDBCursor_Params
& p
);
193 void OnSuccessCursorContinue(
194 const IndexedDBMsg_CallbacksSuccessCursorContinue_Params
& p
);
195 void OnSuccessCursorPrefetch(
196 const IndexedDBMsg_CallbacksSuccessCursorPrefetch_Params
& p
);
197 void OnSuccessStringList(int32 ipc_thread_id
,
198 int32 ipc_callbacks_id
,
199 const std::vector
<base::string16
>& value
);
200 void OnSuccessValue(int32 ipc_thread_id
,
201 int32 ipc_callbacks_id
,
202 const std::string
& value
);
203 void OnSuccessValueWithKey(int32 ipc_thread_id
,
204 int32 ipc_callbacks_id
,
205 const std::string
& value
,
206 const IndexedDBKey
& primary_key
,
207 const IndexedDBKeyPath
& key_path
);
208 void OnSuccessInteger(int32 ipc_thread_id
,
209 int32 ipc_callbacks_id
,
211 void OnSuccessUndefined(int32 ipc_thread_id
, int32 ipc_callbacks_id
);
212 void OnError(int32 ipc_thread_id
,
213 int32 ipc_callbacks_id
,
215 const base::string16
& message
);
216 void OnIntBlocked(int32 ipc_thread_id
,
217 int32 ipc_callbacks_id
,
218 int64 existing_version
);
219 void OnUpgradeNeeded(const IndexedDBMsg_CallbacksUpgradeNeeded_Params
& p
);
220 void OnAbort(int32 ipc_thread_id
,
221 int32 ipc_database_id
,
222 int64 transaction_id
,
224 const base::string16
& message
);
225 void OnComplete(int32 ipc_thread_id
,
226 int32 ipc_database_id
,
227 int64 transaction_id
);
228 void OnForcedClose(int32 ipc_thread_id
, int32 ipc_database_id
);
229 void OnIntVersionChange(int32 ipc_thread_id
,
230 int32 ipc_database_id
,
234 // Reset cursor prefetch caches for all cursors except exception_cursor_id.
235 void ResetCursorPrefetchCaches(int32 ipc_exception_cursor_id
= -1);
237 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
239 // Careful! WebIDBCallbacks wraps non-threadsafe data types. It must be
240 // destroyed and used on the same thread it was created on.
241 IDMap
<blink::WebIDBCallbacks
, IDMapOwnPointer
> pending_callbacks_
;
242 IDMap
<blink::WebIDBDatabaseCallbacks
, IDMapOwnPointer
>
243 pending_database_callbacks_
;
245 // Map from cursor id to WebIDBCursorImpl.
246 std::map
<int32
, WebIDBCursorImpl
*> cursors_
;
248 std::map
<int32
, WebIDBDatabaseImpl
*> databases_
;
250 DISALLOW_COPY_AND_ASSIGN(IndexedDBDispatcher
);
253 } // namespace content
255 #endif // CONTENT_CHILD_INDEXED_DB_INDEXED_DB_DISPATCHER_H_