OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CONTENT_CHILD_INDEXED_DB_INDEXED_DB_DISPATCHER_H_ | |
6 #define CONTENT_CHILD_INDEXED_DB_INDEXED_DB_DISPATCHER_H_ | |
7 | |
8 #include <stddef.h> | |
9 #include <stdint.h> | |
10 | |
11 #include <map> | |
12 #include <string> | |
13 #include <vector> | |
14 | |
15 #include "base/gtest_prod_util.h" | |
16 #include "base/id_map.h" | |
17 #include "base/macros.h" | |
18 #include "base/memory/ref_counted.h" | |
19 #include "base/strings/nullable_string16.h" | |
20 #include "content/common/content_export.h" | |
21 #include "content/common/indexed_db/indexed_db_constants.h" | |
22 #include "content/public/child/worker_thread.h" | |
23 #include "ipc/ipc_sync_message_filter.h" | |
24 #include "third_party/WebKit/public/platform/WebBlobInfo.h" | |
25 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBCallbacks.h
" | |
26 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseCal
lbacks.h" | |
27 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBObserver.h" | |
28 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBTypes.h" | |
29 #include "url/origin.h" | |
30 | |
31 struct IndexedDBDatabaseMetadata; | |
32 struct IndexedDBMsg_CallbacksSuccessCursorContinue_Params; | |
33 struct IndexedDBMsg_CallbacksSuccessCursorPrefetch_Params; | |
34 struct IndexedDBMsg_CallbacksSuccessIDBCursor_Params; | |
35 struct IndexedDBMsg_CallbacksSuccessArray_Params; | |
36 struct IndexedDBMsg_CallbacksSuccessValue_Params; | |
37 struct IndexedDBMsg_CallbacksUpgradeNeeded_Params; | |
38 struct IndexedDBMsg_Observation; | |
39 struct IndexedDBMsg_ObserverChanges; | |
40 | |
41 namespace blink { | |
42 class WebData; | |
43 struct WebIDBObservation; | |
44 } | |
45 | |
46 namespace content { | |
47 class IndexedDBKey; | |
48 class IndexedDBKeyPath; | |
49 class IndexedDBKeyRange; | |
50 class WebIDBCursorImpl; | |
51 class WebIDBDatabaseImpl; | |
52 class ThreadSafeSender; | |
53 | |
54 // Handle the indexed db related communication for this context thread - the | |
55 // main thread and each worker thread have their own copies. | |
56 class CONTENT_EXPORT IndexedDBDispatcher : public WorkerThread::Observer { | |
57 public: | |
58 // Constructor made public to allow RenderThreadImpl to own a copy without | |
59 // failing a NOTREACHED in ThreadSpecificInstance in tests that instantiate | |
60 // two copies of RenderThreadImpl on the same thread. Everyone else probably | |
61 // wants to use ThreadSpecificInstance(). | |
62 explicit IndexedDBDispatcher(ThreadSafeSender* thread_safe_sender); | |
63 ~IndexedDBDispatcher() override; | |
64 | |
65 // |thread_safe_sender| needs to be passed in because if the call leads to | |
66 // construction it will be needed. | |
67 static IndexedDBDispatcher* ThreadSpecificInstance( | |
68 ThreadSafeSender* thread_safe_sender); | |
69 | |
70 // WorkerThread::Observer implementation. | |
71 void WillStopCurrentWorkerThread() override; | |
72 | |
73 static blink::WebIDBMetadata ConvertMetadata( | |
74 const IndexedDBDatabaseMetadata& idb_metadata); | |
75 static std::vector<blink::WebIDBObservation> ConvertObservations( | |
76 const std::vector<IndexedDBMsg_Observation>& idb_observation); | |
77 | |
78 void OnMessageReceived(const IPC::Message& msg); | |
79 | |
80 // This method is virtual so it can be overridden in unit tests. | |
81 virtual bool Send(IPC::Message* msg); | |
82 | |
83 int32_t AddIDBObserver(int32_t ipc_database_id, | |
84 int64_t transaction_id, | |
85 std::unique_ptr<blink::WebIDBObserver> observer); | |
86 | |
87 // The observer with ID's in |observer_ids_to_remove| observe the | |
88 // |ipc_database_id|. | |
89 // We remove our local references to these observer objects, and send an IPC | |
90 // to clean up the observers from the backend. | |
91 void RemoveIDBObserversFromDatabase( | |
92 int32_t ipc_database_id, | |
93 const std::vector<int32_t>& observer_ids_to_remove); | |
94 | |
95 // Removes observers from our local map observers_ . No IPC message generated. | |
96 void RemoveIDBObservers(const std::set<int32_t>& observer_ids_to_remove); | |
97 | |
98 void RequestIDBFactoryGetDatabaseNames(blink::WebIDBCallbacks* callbacks, | |
99 const url::Origin& origin); | |
100 | |
101 void RequestIDBFactoryOpen(const base::string16& name, | |
102 int64_t version, | |
103 int64_t transaction_id, | |
104 blink::WebIDBCallbacks* callbacks, | |
105 blink::WebIDBDatabaseCallbacks* database_callbacks, | |
106 const url::Origin& origin); | |
107 | |
108 void RequestIDBFactoryDeleteDatabase(const base::string16& name, | |
109 blink::WebIDBCallbacks* callbacks, | |
110 const url::Origin& origin); | |
111 | |
112 // This method is virtual so it can be overridden in unit tests. | |
113 virtual void RequestIDBCursorAdvance(unsigned long count, | |
114 blink::WebIDBCallbacks* callbacks_ptr, | |
115 int32_t ipc_cursor_id, | |
116 int64_t transaction_id); | |
117 | |
118 // This method is virtual so it can be overridden in unit tests. | |
119 virtual void RequestIDBCursorContinue(const IndexedDBKey& key, | |
120 const IndexedDBKey& primary_key, | |
121 blink::WebIDBCallbacks* callbacks_ptr, | |
122 int32_t ipc_cursor_id, | |
123 int64_t transaction_id); | |
124 | |
125 // This method is virtual so it can be overridden in unit tests. | |
126 virtual void RequestIDBCursorPrefetch(int n, | |
127 blink::WebIDBCallbacks* callbacks_ptr, | |
128 int32_t ipc_cursor_id); | |
129 | |
130 // This method is virtual so it can be overridden in unit tests. | |
131 virtual void RequestIDBCursorPrefetchReset(int used_prefetches, | |
132 int unused_prefetches, | |
133 int32_t ipc_cursor_id); | |
134 | |
135 void RequestIDBDatabaseClose(int32_t ipc_database_id, | |
136 int32_t ipc_database_callbacks_id); | |
137 | |
138 void NotifyIDBDatabaseVersionChangeIgnored(int32_t ipc_database_id); | |
139 | |
140 void RequestIDBDatabaseCreateTransaction( | |
141 int32_t ipc_database_id, | |
142 int64_t transaction_id, | |
143 blink::WebIDBDatabaseCallbacks* database_callbacks_ptr, | |
144 blink::WebVector<long long> object_store_ids, | |
145 blink::WebIDBTransactionMode mode); | |
146 | |
147 void RequestIDBDatabaseGet(int32_t ipc_database_id, | |
148 int64_t transaction_id, | |
149 int64_t object_store_id, | |
150 int64_t index_id, | |
151 const IndexedDBKeyRange& key_range, | |
152 bool key_only, | |
153 blink::WebIDBCallbacks* callbacks); | |
154 | |
155 void RequestIDBDatabaseGetAll(int32_t ipc_database_id, | |
156 int64_t transaction_id, | |
157 int64_t object_store_id, | |
158 int64_t index_id, | |
159 const IndexedDBKeyRange& key_range, | |
160 bool key_only, | |
161 int64_t max_count, | |
162 blink::WebIDBCallbacks* callbacks); | |
163 | |
164 void RequestIDBDatabasePut( | |
165 int32_t ipc_database_id, | |
166 int64_t transaction_id, | |
167 int64_t object_store_id, | |
168 const blink::WebData& value, | |
169 const blink::WebVector<blink::WebBlobInfo>& web_blob_info, | |
170 const IndexedDBKey& key, | |
171 blink::WebIDBPutMode put_mode, | |
172 blink::WebIDBCallbacks* callbacks, | |
173 const blink::WebVector<long long>& index_ids, | |
174 const blink::WebVector<blink::WebVector<blink::WebIDBKey>>& index_keys); | |
175 | |
176 void RequestIDBDatabaseOpenCursor(int32_t ipc_database_id, | |
177 int64_t transaction_id, | |
178 int64_t object_store_id, | |
179 int64_t index_id, | |
180 const IndexedDBKeyRange& key_range, | |
181 blink::WebIDBCursorDirection direction, | |
182 bool key_only, | |
183 blink::WebIDBTaskType task_type, | |
184 blink::WebIDBCallbacks* callbacks); | |
185 | |
186 void RequestIDBDatabaseCount(int32_t ipc_database_id, | |
187 int64_t transaction_id, | |
188 int64_t object_store_id, | |
189 int64_t index_id, | |
190 const IndexedDBKeyRange& key_range, | |
191 blink::WebIDBCallbacks* callbacks); | |
192 | |
193 void RequestIDBDatabaseDeleteRange(int32_t ipc_database_id, | |
194 int64_t transaction_id, | |
195 int64_t object_store_id, | |
196 const IndexedDBKeyRange& key_range, | |
197 blink::WebIDBCallbacks* callbacks); | |
198 | |
199 void RequestIDBDatabaseClear(int32_t ipc_database_id, | |
200 int64_t transaction_id, | |
201 int64_t object_store_id, | |
202 blink::WebIDBCallbacks* callbacks); | |
203 | |
204 virtual void CursorDestroyed(int32_t ipc_cursor_id); | |
205 void DatabaseDestroyed(int32_t ipc_database_id); | |
206 | |
207 private: | |
208 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest, CursorReset); | |
209 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest, CursorTransactionId); | |
210 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest, ValueSizeTest); | |
211 FRIEND_TEST_ALL_PREFIXES(IndexedDBDispatcherTest, KeyAndValueSizeTest); | |
212 | |
213 enum { kAllCursors = -1 }; | |
214 | |
215 static int32_t CurrentWorkerId() { return WorkerThread::GetCurrentId(); } | |
216 | |
217 template <typename T> | |
218 void init_params(T* params, blink::WebIDBCallbacks* callbacks_ptr) { | |
219 std::unique_ptr<blink::WebIDBCallbacks> callbacks(callbacks_ptr); | |
220 params->ipc_thread_id = CurrentWorkerId(); | |
221 params->ipc_callbacks_id = pending_callbacks_.Add(callbacks.release()); | |
222 } | |
223 | |
224 // IDBCallback message handlers. | |
225 void OnSuccessIDBDatabase(int32_t ipc_thread_id, | |
226 int32_t ipc_callbacks_id, | |
227 int32_t ipc_database_callbacks_id, | |
228 int32_t ipc_object_id, | |
229 const IndexedDBDatabaseMetadata& idb_metadata); | |
230 void OnSuccessIndexedDBKey(int32_t ipc_thread_id, | |
231 int32_t ipc_callbacks_id, | |
232 const IndexedDBKey& key); | |
233 | |
234 void OnSuccessOpenCursor( | |
235 const IndexedDBMsg_CallbacksSuccessIDBCursor_Params& p); | |
236 void OnSuccessCursorContinue( | |
237 const IndexedDBMsg_CallbacksSuccessCursorContinue_Params& p); | |
238 void OnSuccessCursorPrefetch( | |
239 const IndexedDBMsg_CallbacksSuccessCursorPrefetch_Params& p); | |
240 void OnSuccessStringList(int32_t ipc_thread_id, | |
241 int32_t ipc_callbacks_id, | |
242 const std::vector<base::string16>& value); | |
243 void OnSuccessValue(const IndexedDBMsg_CallbacksSuccessValue_Params& p); | |
244 void OnSuccessArray(const IndexedDBMsg_CallbacksSuccessArray_Params& p); | |
245 void OnSuccessInteger(int32_t ipc_thread_id, | |
246 int32_t ipc_callbacks_id, | |
247 int64_t value); | |
248 void OnSuccessUndefined(int32_t ipc_thread_id, int32_t ipc_callbacks_id); | |
249 void OnError(int32_t ipc_thread_id, | |
250 int32_t ipc_callbacks_id, | |
251 int code, | |
252 const base::string16& message); | |
253 void OnIntBlocked(int32_t ipc_thread_id, | |
254 int32_t ipc_callbacks_id, | |
255 int64_t existing_version); | |
256 void OnUpgradeNeeded(const IndexedDBMsg_CallbacksUpgradeNeeded_Params& p); | |
257 void OnAbort(int32_t ipc_thread_id, | |
258 int32_t ipc_database_id, | |
259 int64_t transaction_id, | |
260 int code, | |
261 const base::string16& message); | |
262 void OnComplete(int32_t ipc_thread_id, | |
263 int32_t ipc_database_id, | |
264 int64_t transaction_id); | |
265 void OnDatabaseChanges(int32_t ipc_thread_id, | |
266 int32_t ipc_database_id, | |
267 const IndexedDBMsg_ObserverChanges&); | |
268 | |
269 void OnForcedClose(int32_t ipc_thread_id, int32_t ipc_database_id); | |
270 void OnVersionChange(int32_t ipc_thread_id, | |
271 int32_t ipc_database_id, | |
272 int64_t old_version, | |
273 int64_t new_version); | |
274 | |
275 // Reset cursor prefetch caches for all cursors except exception_cursor_id. | |
276 void ResetCursorPrefetchCaches(int64_t transaction_id, | |
277 int32_t ipc_exception_cursor_id); | |
278 | |
279 scoped_refptr<ThreadSafeSender> thread_safe_sender_; | |
280 | |
281 // Maximum size (in bytes) of value/key pair allowed for put requests. Any | |
282 // requests larger than this size will be rejected. | |
283 // Used by unit tests to exercise behavior without allocating huge chunks | |
284 // of memory. | |
285 size_t max_put_value_size_ = kMaxIDBMessageSizeInBytes; | |
286 | |
287 // Careful! WebIDBCallbacks wraps non-threadsafe data types. It must be | |
288 // destroyed and used on the same thread it was created on. | |
289 IDMap<blink::WebIDBCallbacks, IDMapOwnPointer> pending_callbacks_; | |
290 IDMap<blink::WebIDBDatabaseCallbacks, IDMapOwnPointer> | |
291 pending_database_callbacks_; | |
292 IDMap<blink::WebIDBObserver, IDMapOwnPointer> observers_; | |
293 | |
294 // Maps the ipc_callback_id from an open cursor request to the request's | |
295 // transaction_id. Used to assign the transaction_id to the WebIDBCursorImpl | |
296 // when it is created. | |
297 std::map<int32_t, int64_t> cursor_transaction_ids_; | |
298 | |
299 // Map from cursor id to WebIDBCursorImpl. | |
300 std::map<int32_t, WebIDBCursorImpl*> cursors_; | |
301 | |
302 std::map<int32_t, WebIDBDatabaseImpl*> databases_; | |
303 | |
304 DISALLOW_COPY_AND_ASSIGN(IndexedDBDispatcher); | |
305 }; | |
306 | |
307 } // namespace content | |
308 | |
309 #endif // CONTENT_CHILD_INDEXED_DB_INDEXED_DB_DISPATCHER_H_ | |
OLD | NEW |