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/WebIDBCursor.h"
22 #include "third_party/WebKit/public/platform/WebIDBDatabase.h"
23 #include "third_party/WebKit/public/platform/WebIDBDatabaseCallbacks.h"
25 struct IndexedDBDatabaseMetadata;
26 struct IndexedDBMsg_CallbacksSuccessCursorContinue_Params;
27 struct IndexedDBMsg_CallbacksSuccessCursorPrefetch_Params;
28 struct IndexedDBMsg_CallbacksSuccessIDBCursor_Params;
29 struct IndexedDBMsg_CallbacksSuccessValue_Params;
30 struct IndexedDBMsg_CallbacksSuccessValueWithKey_Params;
31 struct IndexedDBMsg_CallbacksUpgradeNeeded_Params;
39 class IndexedDBKeyPath;
40 class IndexedDBKeyRange;
41 class WebIDBCursorImpl;
42 class WebIDBDatabaseImpl;
43 class ThreadSafeSender;
45 CONTENT_EXPORT extern const size_t kMaxIDBValueSizeInBytes;
47 // Handle the indexed db related communication for this context thread - the
48 // main thread and each worker thread have their own copies.
49 class CONTENT_EXPORT IndexedDBDispatcher : public WorkerTaskRunner::Observer {
51 // Constructor made public to allow RenderThreadImpl to own a copy without
52 // failing a NOTREACHED in ThreadSpecificInstance in tests that instantiate
53 // two copies of RenderThreadImpl on the same thread. Everyone else probably
54 // wants to use ThreadSpecificInstance().
55 explicit IndexedDBDispatcher(ThreadSafeSender* thread_safe_sender);
56 virtual ~IndexedDBDispatcher();
58 // |thread_safe_sender| needs to be passed in because if the call leads to
59 // construction it will be needed.
60 static IndexedDBDispatcher* ThreadSpecificInstance(
61 ThreadSafeSender* thread_safe_sender);
63 // WorkerTaskRunner::Observer implementation.
64 virtual void OnWorkerRunLoopStopped() OVERRIDE;
66 static blink::WebIDBMetadata ConvertMetadata(
67 const IndexedDBDatabaseMetadata& idb_metadata);
69 void OnMessageReceived(const IPC::Message& msg);
71 // This method is virtual so it can be overridden in unit tests.
72 virtual bool Send(IPC::Message* msg);
74 void RequestIDBFactoryGetDatabaseNames(
75 blink::WebIDBCallbacks* callbacks,
76 const std::string& database_identifier);
78 void RequestIDBFactoryOpen(
79 const base::string16& name,
82 blink::WebIDBCallbacks* callbacks,
83 blink::WebIDBDatabaseCallbacks* database_callbacks,
84 const std::string& database_identifier);
86 void RequestIDBFactoryDeleteDatabase(const base::string16& name,
87 blink::WebIDBCallbacks* callbacks,
88 const std::string& database_identifier);
90 // This method is virtual so it can be overridden in unit tests.
91 virtual void RequestIDBCursorAdvance(unsigned long count,
92 blink::WebIDBCallbacks* callbacks_ptr,
94 int64 transaction_id);
96 // This method is virtual so it can be overridden in unit tests.
97 virtual void RequestIDBCursorContinue(const IndexedDBKey& key,
98 const IndexedDBKey& primary_key,
99 blink::WebIDBCallbacks* callbacks_ptr,
101 int64 transaction_id);
103 // This method is virtual so it can be overridden in unit tests.
104 virtual void RequestIDBCursorPrefetch(int n,
105 blink::WebIDBCallbacks* callbacks_ptr,
106 int32 ipc_cursor_id);
108 // This method is virtual so it can be overridden in unit tests.
109 virtual void RequestIDBCursorPrefetchReset(int used_prefetches,
110 int unused_prefetches,
111 int32 ipc_cursor_id);
113 void RequestIDBDatabaseClose(int32 ipc_database_id,
114 int32 ipc_database_callbacks_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::WebIDBDatabase::TransactionMode 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::WebIDBDatabase::PutMode 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::WebIDBCursor::Direction direction,
150 blink::WebIDBDatabase::TaskType 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 OnSuccessValueWithKey(
213 const IndexedDBMsg_CallbacksSuccessValueWithKey_Params& p);
214 void OnSuccessInteger(int32 ipc_thread_id,
215 int32 ipc_callbacks_id,
217 void OnSuccessUndefined(int32 ipc_thread_id, int32 ipc_callbacks_id);
218 void OnError(int32 ipc_thread_id,
219 int32 ipc_callbacks_id,
221 const base::string16& message);
222 void OnIntBlocked(int32 ipc_thread_id,
223 int32 ipc_callbacks_id,
224 int64 existing_version);
225 void OnUpgradeNeeded(const IndexedDBMsg_CallbacksUpgradeNeeded_Params& p);
226 void OnAbort(int32 ipc_thread_id,
227 int32 ipc_database_id,
228 int64 transaction_id,
230 const base::string16& message);
231 void OnComplete(int32 ipc_thread_id,
232 int32 ipc_database_id,
233 int64 transaction_id);
234 void OnForcedClose(int32 ipc_thread_id, int32 ipc_database_id);
235 void OnIntVersionChange(int32 ipc_thread_id,
236 int32 ipc_database_id,
240 // Reset cursor prefetch caches for all cursors except exception_cursor_id.
241 void ResetCursorPrefetchCaches(int64 transaction_id,
242 int32 ipc_exception_cursor_id);
244 scoped_refptr<ThreadSafeSender> thread_safe_sender_;
246 // Careful! WebIDBCallbacks wraps non-threadsafe data types. It must be
247 // destroyed and used on the same thread it was created on.
248 IDMap<blink::WebIDBCallbacks, IDMapOwnPointer> pending_callbacks_;
249 IDMap<blink::WebIDBDatabaseCallbacks, IDMapOwnPointer>
250 pending_database_callbacks_;
252 // Maps the ipc_callback_id from an open cursor request to the request's
253 // transaction_id. Used to assign the transaction_id to the WebIDBCursorImpl
254 // when it is created.
255 std::map<int32, int64> cursor_transaction_ids_;
257 // Map from cursor id to WebIDBCursorImpl.
258 std::map<int32, WebIDBCursorImpl*> cursors_;
260 std::map<int32, WebIDBDatabaseImpl*> databases_;
262 DISALLOW_COPY_AND_ASSIGN(IndexedDBDispatcher);
265 } // namespace content
267 #endif // CONTENT_CHILD_INDEXED_DB_INDEXED_DB_DISPATCHER_H_