Re-enable blink_perf.canvas on Windows
[chromium-blink-merge.git] / content / child / indexed_db / indexed_db_dispatcher.h
blobfa69ffb9a851fdbb87351b231af1ea0f532e120f
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_
8 #include <map>
9 #include <string>
10 #include <vector>
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;
32 namespace blink {
33 class WebData;
36 namespace content {
37 class IndexedDBKey;
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 {
49 public:
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,
79 int64 version,
80 int64 transaction_id,
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,
92 int32 ipc_cursor_id,
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,
99 int32 ipc_cursor_id,
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,
127 int64 index_id,
128 const IndexedDBKeyRange& key_range,
129 bool key_only,
130 blink::WebIDBCallbacks* callbacks);
132 void RequestIDBDatabaseGetAll(int32 ipc_database_id,
133 int64 transaction_id,
134 int64 object_store_id,
135 const IndexedDBKeyRange& key_range,
136 int64 max_count,
137 blink::WebIDBCallbacks* callbacks);
139 void RequestIDBDatabasePut(
140 int32 ipc_database_id,
141 int64 transaction_id,
142 int64 object_store_id,
143 const blink::WebData& value,
144 const blink::WebVector<blink::WebBlobInfo>& web_blob_info,
145 const IndexedDBKey& key,
146 blink::WebIDBPutMode put_mode,
147 blink::WebIDBCallbacks* callbacks,
148 const blink::WebVector<long long>& index_ids,
149 const blink::WebVector<blink::WebVector<blink::WebIDBKey> >& index_keys);
151 void RequestIDBDatabaseOpenCursor(int32 ipc_database_id,
152 int64 transaction_id,
153 int64 object_store_id,
154 int64 index_id,
155 const IndexedDBKeyRange& key_range,
156 blink::WebIDBCursorDirection direction,
157 bool key_only,
158 blink::WebIDBTaskType task_type,
159 blink::WebIDBCallbacks* callbacks);
161 void RequestIDBDatabaseCount(int32 ipc_database_id,
162 int64 transaction_id,
163 int64 object_store_id,
164 int64 index_id,
165 const IndexedDBKeyRange& key_range,
166 blink::WebIDBCallbacks* callbacks);
168 void RequestIDBDatabaseDeleteRange(int32 ipc_database_id,
169 int64 transaction_id,
170 int64 object_store_id,
171 const IndexedDBKeyRange& key_range,
172 blink::WebIDBCallbacks* callbacks);
174 void RequestIDBDatabaseClear(int32 ipc_database_id,
175 int64 transaction_id,
176 int64 object_store_id,
177 blink::WebIDBCallbacks* callbacks);
179 virtual void CursorDestroyed(int32 ipc_cursor_id);
180 void DatabaseDestroyed(int32 ipc_database_id);
182 private:
183 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest, CursorReset);
184 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest, CursorTransactionId);
185 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest, ValueSizeTest);
187 enum { kAllCursors = -1 };
189 static int32 CurrentWorkerId() {
190 return WorkerTaskRunner::Instance()->CurrentWorkerId();
193 template <typename T>
194 void init_params(T* params, blink::WebIDBCallbacks* callbacks_ptr) {
195 scoped_ptr<blink::WebIDBCallbacks> callbacks(callbacks_ptr);
196 params->ipc_thread_id = CurrentWorkerId();
197 params->ipc_callbacks_id = pending_callbacks_.Add(callbacks.release());
200 // IDBCallback message handlers.
201 void OnSuccessIDBDatabase(int32 ipc_thread_id,
202 int32 ipc_callbacks_id,
203 int32 ipc_database_callbacks_id,
204 int32 ipc_object_id,
205 const IndexedDBDatabaseMetadata& idb_metadata);
206 void OnSuccessIndexedDBKey(int32 ipc_thread_id,
207 int32 ipc_callbacks_id,
208 const IndexedDBKey& key);
210 void OnSuccessOpenCursor(
211 const IndexedDBMsg_CallbacksSuccessIDBCursor_Params& p);
212 void OnSuccessCursorContinue(
213 const IndexedDBMsg_CallbacksSuccessCursorContinue_Params& p);
214 void OnSuccessCursorPrefetch(
215 const IndexedDBMsg_CallbacksSuccessCursorPrefetch_Params& p);
216 void OnSuccessStringList(int32 ipc_thread_id,
217 int32 ipc_callbacks_id,
218 const std::vector<base::string16>& value);
219 void OnSuccessValue(const IndexedDBMsg_CallbacksSuccessValue_Params& p);
220 void OnSuccessArray(const IndexedDBMsg_CallbacksSuccessArray_Params& p);
221 void OnSuccessInteger(int32 ipc_thread_id,
222 int32 ipc_callbacks_id,
223 int64 value);
224 void OnSuccessUndefined(int32 ipc_thread_id, int32 ipc_callbacks_id);
225 void OnError(int32 ipc_thread_id,
226 int32 ipc_callbacks_id,
227 int code,
228 const base::string16& message);
229 void OnIntBlocked(int32 ipc_thread_id,
230 int32 ipc_callbacks_id,
231 int64 existing_version);
232 void OnUpgradeNeeded(const IndexedDBMsg_CallbacksUpgradeNeeded_Params& p);
233 void OnAbort(int32 ipc_thread_id,
234 int32 ipc_database_id,
235 int64 transaction_id,
236 int code,
237 const base::string16& message);
238 void OnComplete(int32 ipc_thread_id,
239 int32 ipc_database_id,
240 int64 transaction_id);
241 void OnForcedClose(int32 ipc_thread_id, int32 ipc_database_id);
242 void OnIntVersionChange(int32 ipc_thread_id,
243 int32 ipc_database_id,
244 int64 old_version,
245 int64 new_version);
247 // Reset cursor prefetch caches for all cursors except exception_cursor_id.
248 void ResetCursorPrefetchCaches(int64 transaction_id,
249 int32 ipc_exception_cursor_id);
251 scoped_refptr<ThreadSafeSender> thread_safe_sender_;
253 // Careful! WebIDBCallbacks wraps non-threadsafe data types. It must be
254 // destroyed and used on the same thread it was created on.
255 IDMap<blink::WebIDBCallbacks, IDMapOwnPointer> pending_callbacks_;
256 IDMap<blink::WebIDBDatabaseCallbacks, IDMapOwnPointer>
257 pending_database_callbacks_;
259 // Maps the ipc_callback_id from an open cursor request to the request's
260 // transaction_id. Used to assign the transaction_id to the WebIDBCursorImpl
261 // when it is created.
262 std::map<int32, int64> cursor_transaction_ids_;
264 // Map from cursor id to WebIDBCursorImpl.
265 std::map<int32, WebIDBCursorImpl*> cursors_;
267 std::map<int32, WebIDBDatabaseImpl*> databases_;
269 DISALLOW_COPY_AND_ASSIGN(IndexedDBDispatcher);
272 } // namespace content
274 #endif // CONTENT_CHILD_INDEXED_DB_INDEXED_DB_DISPATCHER_H_