Convert raw pointers to scoped_ptr in net module.
[chromium-blink-merge.git] / content / child / indexed_db / indexed_db_dispatcher.h
blobf0128810b246140fe57540bbdb35259c39313277
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 int64 index_id,
136 const IndexedDBKeyRange& key_range,
137 int64 max_count,
138 blink::WebIDBCallbacks* callbacks);
140 void RequestIDBDatabasePut(
141 int32 ipc_database_id,
142 int64 transaction_id,
143 int64 object_store_id,
144 const blink::WebData& value,
145 const blink::WebVector<blink::WebBlobInfo>& web_blob_info,
146 const IndexedDBKey& key,
147 blink::WebIDBPutMode put_mode,
148 blink::WebIDBCallbacks* callbacks,
149 const blink::WebVector<long long>& index_ids,
150 const blink::WebVector<blink::WebVector<blink::WebIDBKey> >& index_keys);
152 void RequestIDBDatabaseOpenCursor(int32 ipc_database_id,
153 int64 transaction_id,
154 int64 object_store_id,
155 int64 index_id,
156 const IndexedDBKeyRange& key_range,
157 blink::WebIDBCursorDirection direction,
158 bool key_only,
159 blink::WebIDBTaskType task_type,
160 blink::WebIDBCallbacks* callbacks);
162 void RequestIDBDatabaseCount(int32 ipc_database_id,
163 int64 transaction_id,
164 int64 object_store_id,
165 int64 index_id,
166 const IndexedDBKeyRange& key_range,
167 blink::WebIDBCallbacks* callbacks);
169 void RequestIDBDatabaseDeleteRange(int32 ipc_database_id,
170 int64 transaction_id,
171 int64 object_store_id,
172 const IndexedDBKeyRange& key_range,
173 blink::WebIDBCallbacks* callbacks);
175 void RequestIDBDatabaseClear(int32 ipc_database_id,
176 int64 transaction_id,
177 int64 object_store_id,
178 blink::WebIDBCallbacks* callbacks);
180 virtual void CursorDestroyed(int32 ipc_cursor_id);
181 void DatabaseDestroyed(int32 ipc_database_id);
183 private:
184 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest, CursorReset);
185 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest, CursorTransactionId);
186 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest, ValueSizeTest);
188 enum { kAllCursors = -1 };
190 static int32 CurrentWorkerId() {
191 return WorkerTaskRunner::Instance()->CurrentWorkerId();
194 template <typename T>
195 void init_params(T* params, blink::WebIDBCallbacks* callbacks_ptr) {
196 scoped_ptr<blink::WebIDBCallbacks> callbacks(callbacks_ptr);
197 params->ipc_thread_id = CurrentWorkerId();
198 params->ipc_callbacks_id = pending_callbacks_.Add(callbacks.release());
201 // IDBCallback message handlers.
202 void OnSuccessIDBDatabase(int32 ipc_thread_id,
203 int32 ipc_callbacks_id,
204 int32 ipc_database_callbacks_id,
205 int32 ipc_object_id,
206 const IndexedDBDatabaseMetadata& idb_metadata);
207 void OnSuccessIndexedDBKey(int32 ipc_thread_id,
208 int32 ipc_callbacks_id,
209 const IndexedDBKey& key);
211 void OnSuccessOpenCursor(
212 const IndexedDBMsg_CallbacksSuccessIDBCursor_Params& p);
213 void OnSuccessCursorContinue(
214 const IndexedDBMsg_CallbacksSuccessCursorContinue_Params& p);
215 void OnSuccessCursorPrefetch(
216 const IndexedDBMsg_CallbacksSuccessCursorPrefetch_Params& p);
217 void OnSuccessStringList(int32 ipc_thread_id,
218 int32 ipc_callbacks_id,
219 const std::vector<base::string16>& value);
220 void OnSuccessValue(const IndexedDBMsg_CallbacksSuccessValue_Params& p);
221 void OnSuccessArray(const IndexedDBMsg_CallbacksSuccessArray_Params& p);
222 void OnSuccessInteger(int32 ipc_thread_id,
223 int32 ipc_callbacks_id,
224 int64 value);
225 void OnSuccessUndefined(int32 ipc_thread_id, int32 ipc_callbacks_id);
226 void OnError(int32 ipc_thread_id,
227 int32 ipc_callbacks_id,
228 int code,
229 const base::string16& message);
230 void OnIntBlocked(int32 ipc_thread_id,
231 int32 ipc_callbacks_id,
232 int64 existing_version);
233 void OnUpgradeNeeded(const IndexedDBMsg_CallbacksUpgradeNeeded_Params& p);
234 void OnAbort(int32 ipc_thread_id,
235 int32 ipc_database_id,
236 int64 transaction_id,
237 int code,
238 const base::string16& message);
239 void OnComplete(int32 ipc_thread_id,
240 int32 ipc_database_id,
241 int64 transaction_id);
242 void OnForcedClose(int32 ipc_thread_id, int32 ipc_database_id);
243 void OnIntVersionChange(int32 ipc_thread_id,
244 int32 ipc_database_id,
245 int64 old_version,
246 int64 new_version);
248 // Reset cursor prefetch caches for all cursors except exception_cursor_id.
249 void ResetCursorPrefetchCaches(int64 transaction_id,
250 int32 ipc_exception_cursor_id);
252 scoped_refptr<ThreadSafeSender> thread_safe_sender_;
254 // Careful! WebIDBCallbacks wraps non-threadsafe data types. It must be
255 // destroyed and used on the same thread it was created on.
256 IDMap<blink::WebIDBCallbacks, IDMapOwnPointer> pending_callbacks_;
257 IDMap<blink::WebIDBDatabaseCallbacks, IDMapOwnPointer>
258 pending_database_callbacks_;
260 // Maps the ipc_callback_id from an open cursor request to the request's
261 // transaction_id. Used to assign the transaction_id to the WebIDBCursorImpl
262 // when it is created.
263 std::map<int32, int64> cursor_transaction_ids_;
265 // Map from cursor id to WebIDBCursorImpl.
266 std::map<int32, WebIDBCursorImpl*> cursors_;
268 std::map<int32, WebIDBDatabaseImpl*> databases_;
270 DISALLOW_COPY_AND_ASSIGN(IndexedDBDispatcher);
273 } // namespace content
275 #endif // CONTENT_CHILD_INDEXED_DB_INDEXED_DB_DISPATCHER_H_