OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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 #include "chrome/renderer/indexed_db_dispatcher.h" | |
6 | |
7 #include "chrome/renderer/render_thread.h" | |
8 #include "chrome/renderer/render_view.h" | |
9 #include "chrome/renderer/renderer_webidbcursor_impl.h" | |
10 #include "chrome/renderer/renderer_webidbdatabase_impl.h" | |
11 #include "chrome/renderer/renderer_webidbindex_impl.h" | |
12 #include "chrome/renderer/renderer_webidbobjectstore_impl.h" | |
13 #include "chrome/renderer/renderer_webidbtransaction_impl.h" | |
14 #include "content/common/indexed_db_messages.h" | |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebIDBDatabaseCallbac
ks.h" | |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebIDBDatabaseError.h
" | |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebIDBKeyRange.h" | |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" | |
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSerializedScriptVa
lue.h" | |
21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" | |
22 | |
23 using WebKit::WebExceptionCode; | |
24 using WebKit::WebFrame; | |
25 using WebKit::WebIDBCallbacks; | |
26 using WebKit::WebIDBKeyRange; | |
27 using WebKit::WebIDBDatabase; | |
28 using WebKit::WebIDBDatabaseCallbacks; | |
29 using WebKit::WebIDBDatabaseError; | |
30 using WebKit::WebIDBTransaction; | |
31 using WebKit::WebIDBTransactionCallbacks; | |
32 | |
33 IndexedDBDispatcher::IndexedDBDispatcher() { | |
34 } | |
35 | |
36 IndexedDBDispatcher::~IndexedDBDispatcher() { | |
37 } | |
38 | |
39 bool IndexedDBDispatcher::OnMessageReceived(const IPC::Message& msg) { | |
40 bool handled = true; | |
41 IPC_BEGIN_MESSAGE_MAP(IndexedDBDispatcher, msg) | |
42 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksSuccessIDBCursor, | |
43 OnSuccessOpenCursor) | |
44 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksSuccessIDBDatabase, | |
45 OnSuccessIDBDatabase) | |
46 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksSuccessIDBIndex, | |
47 OnSuccessIDBIndex) | |
48 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksSuccessIndexedDBKey, | |
49 OnSuccessIndexedDBKey) | |
50 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksSuccessIDBTransaction, | |
51 OnSuccessIDBTransaction) | |
52 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksSuccessSerializedScriptValue, | |
53 OnSuccessSerializedScriptValue) | |
54 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksError, OnError) | |
55 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksBlocked, OnBlocked) | |
56 IPC_MESSAGE_HANDLER(IndexedDBMsg_TransactionCallbacksAbort, OnAbort) | |
57 IPC_MESSAGE_HANDLER(IndexedDBMsg_TransactionCallbacksComplete, OnComplete) | |
58 IPC_MESSAGE_HANDLER(IndexedDBMsg_TransactionCallbacksTimeout, OnTimeout) | |
59 IPC_MESSAGE_HANDLER(IndexedDBMsg_DatabaseCallbacksVersionChange, | |
60 OnVersionChange) | |
61 IPC_MESSAGE_UNHANDLED(handled = false) | |
62 IPC_END_MESSAGE_MAP() | |
63 return handled; | |
64 } | |
65 | |
66 void IndexedDBDispatcher::RequestIDBCursorUpdate( | |
67 const SerializedScriptValue& value, | |
68 WebIDBCallbacks* callbacks_ptr, | |
69 int32 idb_cursor_id, | |
70 WebExceptionCode* ec) { | |
71 scoped_ptr<WebIDBCallbacks> callbacks(callbacks_ptr); | |
72 | |
73 int32 response_id = pending_callbacks_.Add(callbacks.release()); | |
74 RenderThread::current()->Send( | |
75 new IndexedDBHostMsg_CursorUpdate(idb_cursor_id, response_id, value, ec)); | |
76 if (*ec) | |
77 pending_callbacks_.Remove(response_id); | |
78 } | |
79 | |
80 void IndexedDBDispatcher::RequestIDBCursorContinue( | |
81 const IndexedDBKey& key, | |
82 WebIDBCallbacks* callbacks_ptr, | |
83 int32 idb_cursor_id, | |
84 WebExceptionCode* ec) { | |
85 scoped_ptr<WebIDBCallbacks> callbacks(callbacks_ptr); | |
86 | |
87 int32 response_id = pending_callbacks_.Add(callbacks.release()); | |
88 RenderThread::current()->Send( | |
89 new IndexedDBHostMsg_CursorContinue(idb_cursor_id, response_id, key, ec)); | |
90 if (*ec) | |
91 pending_callbacks_.Remove(response_id); | |
92 } | |
93 | |
94 void IndexedDBDispatcher::RequestIDBCursorDelete( | |
95 WebIDBCallbacks* callbacks_ptr, | |
96 int32 idb_cursor_id, | |
97 WebExceptionCode* ec) { | |
98 scoped_ptr<WebIDBCallbacks> callbacks(callbacks_ptr); | |
99 | |
100 int32 response_id = pending_callbacks_.Add(callbacks.release()); | |
101 RenderThread::current()->Send( | |
102 new IndexedDBHostMsg_CursorDelete(idb_cursor_id, response_id, ec)); | |
103 if (*ec) | |
104 pending_callbacks_.Remove(response_id); | |
105 } | |
106 | |
107 void IndexedDBDispatcher::RequestIDBFactoryOpen( | |
108 const string16& name, | |
109 WebIDBCallbacks* callbacks_ptr, | |
110 const string16& origin, | |
111 WebFrame* web_frame, | |
112 uint64 maximum_size) { | |
113 scoped_ptr<WebIDBCallbacks> callbacks(callbacks_ptr); | |
114 | |
115 if (!web_frame) | |
116 return; // We must be shutting down. | |
117 RenderView* render_view = RenderView::FromWebView(web_frame->view()); | |
118 if (!render_view) | |
119 return; // We must be shutting down. | |
120 | |
121 IndexedDBHostMsg_FactoryOpen_Params params; | |
122 params.routing_id = render_view->routing_id(); | |
123 params.response_id = pending_callbacks_.Add(callbacks.release()); | |
124 params.origin = origin; | |
125 params.name = name; | |
126 params.maximum_size = maximum_size; | |
127 RenderThread::current()->Send(new IndexedDBHostMsg_FactoryOpen(params)); | |
128 } | |
129 | |
130 void IndexedDBDispatcher::RequestIDBFactoryDeleteDatabase( | |
131 const string16& name, | |
132 WebIDBCallbacks* callbacks_ptr, | |
133 const string16& origin, | |
134 WebFrame* web_frame) { | |
135 scoped_ptr<WebIDBCallbacks> callbacks(callbacks_ptr); | |
136 | |
137 if (!web_frame) | |
138 return; // We must be shutting down. | |
139 RenderView* render_view = RenderView::FromWebView(web_frame->view()); | |
140 if (!render_view) | |
141 return; // We must be shutting down. | |
142 | |
143 IndexedDBHostMsg_FactoryDeleteDatabase_Params params; | |
144 params.routing_id = render_view->routing_id(); | |
145 params.response_id = pending_callbacks_.Add(callbacks.release()); | |
146 params.origin = origin; | |
147 params.name = name; | |
148 RenderThread::current()->Send( | |
149 new IndexedDBHostMsg_FactoryDeleteDatabase(params)); | |
150 } | |
151 | |
152 void IndexedDBDispatcher::RequestIDBDatabaseClose(int32 idb_database_id) { | |
153 RenderThread::current()->Send( | |
154 new IndexedDBHostMsg_DatabaseClose(idb_database_id)); | |
155 pending_database_callbacks_.Remove(idb_database_id); | |
156 } | |
157 | |
158 void IndexedDBDispatcher::RequestIDBDatabaseOpen( | |
159 WebIDBDatabaseCallbacks* callbacks_ptr, | |
160 int32 idb_database_id) { | |
161 scoped_ptr<WebIDBDatabaseCallbacks> callbacks(callbacks_ptr); | |
162 | |
163 int32 response_id = pending_database_callbacks_.Add(callbacks.release()); | |
164 RenderThread::current()->Send(new IndexedDBHostMsg_DatabaseOpen(response_id, | |
165 idb_database_id)); | |
166 } | |
167 | |
168 void IndexedDBDispatcher::RequestIDBDatabaseSetVersion( | |
169 const string16& version, | |
170 WebIDBCallbacks* callbacks_ptr, | |
171 int32 idb_database_id, | |
172 WebExceptionCode* ec) { | |
173 scoped_ptr<WebIDBCallbacks> callbacks(callbacks_ptr); | |
174 | |
175 int32 response_id = pending_callbacks_.Add(callbacks.release()); | |
176 RenderThread::current()->Send( | |
177 new IndexedDBHostMsg_DatabaseSetVersion(idb_database_id, response_id, | |
178 version, ec)); | |
179 if (*ec) | |
180 pending_callbacks_.Remove(response_id); | |
181 } | |
182 | |
183 void IndexedDBDispatcher::RequestIDBIndexOpenObjectCursor( | |
184 const WebIDBKeyRange& idb_key_range, | |
185 unsigned short direction, | |
186 WebIDBCallbacks* callbacks_ptr, | |
187 int32 idb_index_id, | |
188 const WebIDBTransaction& transaction, | |
189 WebExceptionCode* ec) { | |
190 scoped_ptr<WebIDBCallbacks> callbacks(callbacks_ptr); | |
191 IndexedDBHostMsg_IndexOpenCursor_Params params; | |
192 params.response_id = pending_callbacks_.Add(callbacks.release()); | |
193 params.lower_key.Set(idb_key_range.lower()); | |
194 params.upper_key.Set(idb_key_range.upper()); | |
195 params.lower_open = idb_key_range.lowerOpen(); | |
196 params.upper_open = idb_key_range.upperOpen(); | |
197 params.direction = direction; | |
198 params.idb_index_id = idb_index_id; | |
199 params.transaction_id = TransactionId(transaction); | |
200 RenderThread::current()->Send( | |
201 new IndexedDBHostMsg_IndexOpenObjectCursor(params, ec)); | |
202 if (*ec) | |
203 pending_callbacks_.Remove(params.response_id); | |
204 } | |
205 | |
206 void IndexedDBDispatcher::RequestIDBIndexOpenKeyCursor( | |
207 const WebIDBKeyRange& idb_key_range, | |
208 unsigned short direction, | |
209 WebIDBCallbacks* callbacks_ptr, | |
210 int32 idb_index_id, | |
211 const WebIDBTransaction& transaction, | |
212 WebExceptionCode* ec) { | |
213 scoped_ptr<WebIDBCallbacks> callbacks(callbacks_ptr); | |
214 IndexedDBHostMsg_IndexOpenCursor_Params params; | |
215 params.response_id = pending_callbacks_.Add(callbacks.release()); | |
216 // TODO(jorlow): We really should just create a Chromium abstraction for | |
217 // KeyRange rather than doing it ad-hoc like this. | |
218 params.lower_key.Set(idb_key_range.lower()); | |
219 params.upper_key.Set(idb_key_range.upper()); | |
220 params.lower_open = idb_key_range.lowerOpen(); | |
221 params.upper_open = idb_key_range.upperOpen(); | |
222 params.direction = direction; | |
223 params.idb_index_id = idb_index_id; | |
224 params.transaction_id = TransactionId(transaction); | |
225 RenderThread::current()->Send( | |
226 new IndexedDBHostMsg_IndexOpenKeyCursor(params, ec)); | |
227 if (*ec) | |
228 pending_callbacks_.Remove(params.response_id); | |
229 } | |
230 | |
231 void IndexedDBDispatcher::RequestIDBIndexGetObject( | |
232 const IndexedDBKey& key, | |
233 WebIDBCallbacks* callbacks_ptr, | |
234 int32 idb_index_id, | |
235 const WebIDBTransaction& transaction, | |
236 WebExceptionCode* ec) { | |
237 scoped_ptr<WebIDBCallbacks> callbacks(callbacks_ptr); | |
238 int32 response_id = pending_callbacks_.Add(callbacks.release()); | |
239 RenderThread::current()->Send( | |
240 new IndexedDBHostMsg_IndexGetObject( | |
241 idb_index_id, response_id, key, | |
242 TransactionId(transaction), ec)); | |
243 if (*ec) | |
244 pending_callbacks_.Remove(response_id); | |
245 } | |
246 | |
247 void IndexedDBDispatcher::RequestIDBIndexGetKey( | |
248 const IndexedDBKey& key, | |
249 WebIDBCallbacks* callbacks_ptr, | |
250 int32 idb_index_id, | |
251 const WebIDBTransaction& transaction, | |
252 WebExceptionCode* ec) { | |
253 scoped_ptr<WebIDBCallbacks> callbacks(callbacks_ptr); | |
254 int32 response_id = pending_callbacks_.Add(callbacks.release()); | |
255 RenderThread::current()->Send( | |
256 new IndexedDBHostMsg_IndexGetKey( | |
257 idb_index_id, response_id, key, | |
258 TransactionId(transaction), ec)); | |
259 if (*ec) | |
260 pending_callbacks_.Remove(response_id); | |
261 } | |
262 | |
263 void IndexedDBDispatcher::RequestIDBObjectStoreGet( | |
264 const IndexedDBKey& key, | |
265 WebIDBCallbacks* callbacks_ptr, | |
266 int32 idb_object_store_id, | |
267 const WebIDBTransaction& transaction, | |
268 WebExceptionCode* ec) { | |
269 scoped_ptr<WebIDBCallbacks> callbacks(callbacks_ptr); | |
270 | |
271 int32 response_id = pending_callbacks_.Add(callbacks.release()); | |
272 RenderThread::current()->Send( | |
273 new IndexedDBHostMsg_ObjectStoreGet( | |
274 idb_object_store_id, response_id, | |
275 key, TransactionId(transaction), ec)); | |
276 if (*ec) | |
277 pending_callbacks_.Remove(response_id); | |
278 } | |
279 | |
280 void IndexedDBDispatcher::RequestIDBObjectStorePut( | |
281 const SerializedScriptValue& value, | |
282 const IndexedDBKey& key, | |
283 WebKit::WebIDBObjectStore::PutMode put_mode, | |
284 WebIDBCallbacks* callbacks_ptr, | |
285 int32 idb_object_store_id, | |
286 const WebIDBTransaction& transaction, | |
287 WebExceptionCode* ec) { | |
288 scoped_ptr<WebIDBCallbacks> callbacks(callbacks_ptr); | |
289 IndexedDBHostMsg_ObjectStorePut_Params params; | |
290 params.idb_object_store_id = idb_object_store_id; | |
291 params.response_id = pending_callbacks_.Add(callbacks.release()); | |
292 params.serialized_value = value; | |
293 params.key = key; | |
294 params.put_mode = put_mode; | |
295 params.transaction_id = TransactionId(transaction); | |
296 RenderThread::current()->Send(new IndexedDBHostMsg_ObjectStorePut( | |
297 params, ec)); | |
298 if (*ec) | |
299 pending_callbacks_.Remove(params.response_id); | |
300 } | |
301 | |
302 void IndexedDBDispatcher::RequestIDBObjectStoreDelete( | |
303 const IndexedDBKey& key, | |
304 WebIDBCallbacks* callbacks_ptr, | |
305 int32 idb_object_store_id, | |
306 const WebIDBTransaction& transaction, | |
307 WebExceptionCode* ec) { | |
308 scoped_ptr<WebIDBCallbacks> callbacks(callbacks_ptr); | |
309 | |
310 int32 response_id = pending_callbacks_.Add(callbacks.release()); | |
311 RenderThread::current()->Send( | |
312 new IndexedDBHostMsg_ObjectStoreDelete( | |
313 idb_object_store_id, response_id, | |
314 key, TransactionId(transaction), ec)); | |
315 if (*ec) | |
316 pending_callbacks_.Remove(response_id); | |
317 } | |
318 | |
319 void IndexedDBDispatcher::RequestIDBObjectStoreClear( | |
320 WebIDBCallbacks* callbacks_ptr, | |
321 int32 idb_object_store_id, | |
322 const WebIDBTransaction& transaction, | |
323 WebExceptionCode* ec) { | |
324 scoped_ptr<WebIDBCallbacks> callbacks(callbacks_ptr); | |
325 | |
326 int32 response_id = pending_callbacks_.Add(callbacks.release()); | |
327 RenderThread::current()->Send( | |
328 new IndexedDBHostMsg_ObjectStoreClear( | |
329 idb_object_store_id, response_id, | |
330 TransactionId(transaction), ec)); | |
331 if (*ec) | |
332 pending_callbacks_.Remove(response_id); | |
333 } | |
334 | |
335 void IndexedDBDispatcher::RequestIDBObjectStoreOpenCursor( | |
336 const WebIDBKeyRange& idb_key_range, | |
337 unsigned short direction, | |
338 WebIDBCallbacks* callbacks_ptr, | |
339 int32 idb_object_store_id, | |
340 const WebIDBTransaction& transaction, | |
341 WebExceptionCode* ec) { | |
342 scoped_ptr<WebIDBCallbacks> callbacks(callbacks_ptr); | |
343 IndexedDBHostMsg_ObjectStoreOpenCursor_Params params; | |
344 params.response_id = pending_callbacks_.Add(callbacks.release()); | |
345 params.lower_key.Set(idb_key_range.lower()); | |
346 params.upper_key.Set(idb_key_range.upper()); | |
347 params.lower_open = idb_key_range.lowerOpen(); | |
348 params.upper_open = idb_key_range.upperOpen(); | |
349 params.direction = direction; | |
350 params.idb_object_store_id = idb_object_store_id; | |
351 params.transaction_id = TransactionId(transaction); | |
352 RenderThread::current()->Send( | |
353 new IndexedDBHostMsg_ObjectStoreOpenCursor(params, ec)); | |
354 if (*ec) | |
355 pending_callbacks_.Remove(params.response_id); | |
356 } | |
357 | |
358 void IndexedDBDispatcher::RegisterWebIDBTransactionCallbacks( | |
359 WebIDBTransactionCallbacks* callbacks, | |
360 int32 id) { | |
361 pending_transaction_callbacks_.AddWithID(callbacks, id); | |
362 } | |
363 | |
364 int32 IndexedDBDispatcher::TransactionId( | |
365 const WebIDBTransaction& transaction) { | |
366 const RendererWebIDBTransactionImpl* impl = | |
367 static_cast<const RendererWebIDBTransactionImpl*>(&transaction); | |
368 return impl->id(); | |
369 } | |
370 | |
371 void IndexedDBDispatcher::OnSuccessIDBDatabase(int32 response_id, | |
372 int32 object_id) { | |
373 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(response_id); | |
374 callbacks->onSuccess(new RendererWebIDBDatabaseImpl(object_id)); | |
375 pending_callbacks_.Remove(response_id); | |
376 } | |
377 | |
378 void IndexedDBDispatcher::OnSuccessIndexedDBKey(int32 response_id, | |
379 const IndexedDBKey& key) { | |
380 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(response_id); | |
381 callbacks->onSuccess(key); | |
382 pending_callbacks_.Remove(response_id); | |
383 } | |
384 | |
385 void IndexedDBDispatcher::OnSuccessIDBTransaction(int32 response_id, | |
386 int32 object_id) { | |
387 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(response_id); | |
388 callbacks->onSuccess(new RendererWebIDBTransactionImpl(object_id)); | |
389 pending_callbacks_.Remove(response_id); | |
390 } | |
391 | |
392 void IndexedDBDispatcher::OnSuccessIDBIndex(int32 response_id, | |
393 int32 object_id) { | |
394 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(response_id); | |
395 callbacks->onSuccess(new RendererWebIDBIndexImpl(object_id)); | |
396 pending_callbacks_.Remove(response_id); | |
397 } | |
398 | |
399 void IndexedDBDispatcher::OnSuccessSerializedScriptValue( | |
400 int32 response_id, const SerializedScriptValue& value) { | |
401 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(response_id); | |
402 callbacks->onSuccess(value); | |
403 pending_callbacks_.Remove(response_id); | |
404 } | |
405 | |
406 void IndexedDBDispatcher::OnSuccessOpenCursor(int32 repsonse_id, | |
407 int32 object_id) { | |
408 WebIDBCallbacks* callbacks = | |
409 pending_callbacks_.Lookup(repsonse_id); | |
410 callbacks->onSuccess(new RendererWebIDBCursorImpl(object_id)); | |
411 pending_callbacks_.Remove(repsonse_id); | |
412 } | |
413 | |
414 void IndexedDBDispatcher::OnBlocked(int32 response_id) { | |
415 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(response_id); | |
416 callbacks->onBlocked(); | |
417 } | |
418 | |
419 void IndexedDBDispatcher::OnError(int32 response_id, int code, | |
420 const string16& message) { | |
421 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(response_id); | |
422 callbacks->onError(WebIDBDatabaseError(code, message)); | |
423 pending_callbacks_.Remove(response_id); | |
424 } | |
425 | |
426 void IndexedDBDispatcher::OnAbort(int32 transaction_id) { | |
427 WebIDBTransactionCallbacks* callbacks = | |
428 pending_transaction_callbacks_.Lookup(transaction_id); | |
429 callbacks->onAbort(); | |
430 pending_transaction_callbacks_.Remove(transaction_id); | |
431 } | |
432 | |
433 void IndexedDBDispatcher::OnComplete(int32 transaction_id) { | |
434 WebIDBTransactionCallbacks* callbacks = | |
435 pending_transaction_callbacks_.Lookup(transaction_id); | |
436 callbacks->onComplete(); | |
437 pending_transaction_callbacks_.Remove(transaction_id); | |
438 } | |
439 | |
440 void IndexedDBDispatcher::OnTimeout(int32 transaction_id) { | |
441 WebIDBTransactionCallbacks* callbacks = | |
442 pending_transaction_callbacks_.Lookup(transaction_id); | |
443 callbacks->onTimeout(); | |
444 pending_transaction_callbacks_.Remove(transaction_id); | |
445 } | |
446 | |
447 void IndexedDBDispatcher::OnVersionChange(int32 database_id, | |
448 const string16& newVersion) { | |
449 WebIDBDatabaseCallbacks* callbacks = | |
450 pending_database_callbacks_.Lookup(database_id); | |
451 // callbacks would be NULL if a versionchange event is received after close | |
452 // has been called. | |
453 if (callbacks) | |
454 callbacks->onVersionChange(newVersion); | |
455 } | |
OLD | NEW |