Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(537)

Side by Side Diff: content/child/indexed_db/indexed_db_dispatcher.cc

Issue 1963293002: Replacing Indexed DB Chromium IPC with Mojo Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Refactoring after Passing URLRequestContextGetter. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #include "content/child/indexed_db/indexed_db_dispatcher.h"
6
7 #include <utility>
8
9 #include "base/format_macros.h"
10 #include "base/lazy_instance.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/threading/thread_local.h"
13 #include "content/child/indexed_db/indexed_db_key_builders.h"
14 #include "content/child/indexed_db/webidbcursor_impl.h"
15 #include "content/child/indexed_db/webidbdatabase_impl.h"
16 #include "content/child/thread_safe_sender.h"
17 #include "content/common/indexed_db/indexed_db_messages.h"
18 #include "ipc/ipc_channel.h"
19 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseCal lbacks.h"
20 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseErr or.h"
21 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseExc eption.h"
22 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBObservation .h"
23 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBValue.h"
24
25 using blink::WebBlobInfo;
26 using blink::WebData;
27 using blink::WebIDBCallbacks;
28 using blink::WebIDBCursor;
29 using blink::WebIDBDatabase;
30 using blink::WebIDBDatabaseCallbacks;
31 using blink::WebIDBDatabaseError;
32 using blink::WebIDBKey;
33 using blink::WebIDBMetadata;
34 using blink::WebIDBObservation;
35 using blink::WebIDBObserver;
36 using blink::WebIDBValue;
37 using blink::WebString;
38 using blink::WebVector;
39 using base::ThreadLocalPointer;
40
41 namespace content {
42 static base::LazyInstance<ThreadLocalPointer<IndexedDBDispatcher> >::Leaky
43 g_idb_dispatcher_tls = LAZY_INSTANCE_INITIALIZER;
44
45 namespace {
46
47 IndexedDBDispatcher* const kHasBeenDeleted =
48 reinterpret_cast<IndexedDBDispatcher*>(0x1);
49
50 } // unnamed namespace
51
52 IndexedDBDispatcher::IndexedDBDispatcher(ThreadSafeSender* thread_safe_sender)
53 : thread_safe_sender_(thread_safe_sender) {
54 g_idb_dispatcher_tls.Pointer()->Set(this);
55 }
56
57 IndexedDBDispatcher::~IndexedDBDispatcher() {
58 // Clear any pending callbacks - which may result in dispatch requests -
59 // before marking the dispatcher as deleted.
60 pending_callbacks_.Clear();
61 pending_database_callbacks_.Clear();
62
63 DCHECK(pending_callbacks_.IsEmpty());
64 DCHECK(pending_database_callbacks_.IsEmpty());
65
66 g_idb_dispatcher_tls.Pointer()->Set(kHasBeenDeleted);
67 }
68
69 IndexedDBDispatcher* IndexedDBDispatcher::ThreadSpecificInstance(
70 ThreadSafeSender* thread_safe_sender) {
71 if (g_idb_dispatcher_tls.Pointer()->Get() == kHasBeenDeleted) {
72 NOTREACHED() << "Re-instantiating TLS IndexedDBDispatcher.";
73 g_idb_dispatcher_tls.Pointer()->Set(NULL);
74 }
75 if (g_idb_dispatcher_tls.Pointer()->Get())
76 return g_idb_dispatcher_tls.Pointer()->Get();
77
78 IndexedDBDispatcher* dispatcher = new IndexedDBDispatcher(thread_safe_sender);
79 if (WorkerThread::GetCurrentId())
80 WorkerThread::AddObserver(dispatcher);
81 return dispatcher;
82 }
83
84 void IndexedDBDispatcher::WillStopCurrentWorkerThread() {
85 delete this;
86 }
87
88 WebIDBMetadata IndexedDBDispatcher::ConvertMetadata(
89 const IndexedDBDatabaseMetadata& idb_metadata) {
90 WebIDBMetadata web_metadata;
91 web_metadata.id = idb_metadata.id;
92 web_metadata.name = idb_metadata.name;
93 web_metadata.version = idb_metadata.version;
94 web_metadata.maxObjectStoreId = idb_metadata.max_object_store_id;
95 web_metadata.objectStores =
96 WebVector<WebIDBMetadata::ObjectStore>(idb_metadata.object_stores.size());
97
98 for (size_t i = 0; i < idb_metadata.object_stores.size(); ++i) {
99 const IndexedDBObjectStoreMetadata& idb_store_metadata =
100 idb_metadata.object_stores[i];
101 WebIDBMetadata::ObjectStore& web_store_metadata =
102 web_metadata.objectStores[i];
103
104 web_store_metadata.id = idb_store_metadata.id;
105 web_store_metadata.name = idb_store_metadata.name;
106 web_store_metadata.keyPath =
107 WebIDBKeyPathBuilder::Build(idb_store_metadata.key_path);
108 web_store_metadata.autoIncrement = idb_store_metadata.auto_increment;
109 web_store_metadata.maxIndexId = idb_store_metadata.max_index_id;
110 web_store_metadata.indexes =
111 WebVector<WebIDBMetadata::Index>(idb_store_metadata.indexes.size());
112
113 for (size_t j = 0; j < idb_store_metadata.indexes.size(); ++j) {
114 const IndexedDBIndexMetadata& idb_index_metadata =
115 idb_store_metadata.indexes[j];
116 WebIDBMetadata::Index& web_index_metadata = web_store_metadata.indexes[j];
117
118 web_index_metadata.id = idb_index_metadata.id;
119 web_index_metadata.name = idb_index_metadata.name;
120 web_index_metadata.keyPath =
121 WebIDBKeyPathBuilder::Build(idb_index_metadata.key_path);
122 web_index_metadata.unique = idb_index_metadata.unique;
123 web_index_metadata.multiEntry = idb_index_metadata.multi_entry;
124 }
125 }
126
127 return web_metadata;
128 }
129
130 std::vector<WebIDBObservation> IndexedDBDispatcher::ConvertObservations(
131 const std::vector<IndexedDBMsg_Observation>& idb_observations) {
132 std::vector<WebIDBObservation> web_observations;
133 for (const auto& idb_observation : idb_observations) {
134 WebIDBObservation web_observation;
135 web_observation.objectStoreId = idb_observation.object_store_id;
136 web_observation.type = idb_observation.type;
137 web_observation.keyRange =
138 WebIDBKeyRangeBuilder::Build(idb_observation.key_range);
139 // TODO(palakj): Assign value to web_observation.
140 web_observations.push_back(std::move(web_observation));
141 }
142 return web_observations;
143 }
144
145 void IndexedDBDispatcher::OnMessageReceived(const IPC::Message& msg) {
146 bool handled = true;
147 IPC_BEGIN_MESSAGE_MAP(IndexedDBDispatcher, msg)
148 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksSuccessIDBCursor,
149 OnSuccessOpenCursor)
150 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksSuccessCursorAdvance,
151 OnSuccessCursorContinue)
152 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksSuccessCursorContinue,
153 OnSuccessCursorContinue)
154 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksSuccessCursorPrefetch,
155 OnSuccessCursorPrefetch)
156 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksSuccessIDBDatabase,
157 OnSuccessIDBDatabase)
158 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksSuccessIndexedDBKey,
159 OnSuccessIndexedDBKey)
160 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksSuccessStringList,
161 OnSuccessStringList)
162 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksSuccessArray, OnSuccessArray)
163 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksSuccessValue, OnSuccessValue)
164 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksSuccessInteger, OnSuccessInteger)
165 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksSuccessUndefined,
166 OnSuccessUndefined)
167 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksError, OnError)
168 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksIntBlocked, OnIntBlocked)
169 IPC_MESSAGE_HANDLER(IndexedDBMsg_CallbacksUpgradeNeeded, OnUpgradeNeeded)
170 IPC_MESSAGE_HANDLER(IndexedDBMsg_DatabaseCallbacksForcedClose,
171 OnForcedClose)
172 IPC_MESSAGE_HANDLER(IndexedDBMsg_DatabaseCallbacksVersionChange,
173 OnVersionChange)
174 IPC_MESSAGE_HANDLER(IndexedDBMsg_DatabaseCallbacksAbort, OnAbort)
175 IPC_MESSAGE_HANDLER(IndexedDBMsg_DatabaseCallbacksComplete, OnComplete)
176 IPC_MESSAGE_HANDLER(IndexedDBMsg_DatabaseCallbacksChanges,
177 OnDatabaseChanges)
178 IPC_MESSAGE_UNHANDLED(handled = false)
179 IPC_END_MESSAGE_MAP()
180 // If a message gets here, IndexedDBMessageFilter already determined that it
181 // is an IndexedDB message.
182 DCHECK(handled) << "Didn't handle a message defined at line "
183 << IPC_MESSAGE_ID_LINE(msg.type());
184 }
185
186 bool IndexedDBDispatcher::Send(IPC::Message* msg) {
187 return thread_safe_sender_->Send(msg);
188 }
189
190 int32_t IndexedDBDispatcher::AddIDBObserver(
191 int32_t ipc_database_id,
192 int64_t transaction_id,
193 std::unique_ptr<WebIDBObserver> observer) {
194 IndexedDBHostMsg_DatabaseObserve_Params params;
195 static_assert(blink::WebIDBOperationTypeCount < sizeof(uint16_t) * CHAR_BIT,
196 "WebIDBOperationType Count exceeds size of uint16_t");
197 params.operation_types =
198 static_cast<uint16_t>(observer->operationTypes().to_ulong());
199 params.include_transaction = observer->transaction();
200 params.no_records = observer->noRecords();
201 params.values = observer->values();
202 int32_t observer_id = observers_.Add(observer.release());
203 params.ipc_database_id = ipc_database_id;
204 params.transaction_id = transaction_id;
205 params.observer_id = observer_id;
206 Send(new IndexedDBHostMsg_DatabaseObserve(params));
207 return observer_id;
208 }
209
210 void IndexedDBDispatcher::RemoveIDBObserversFromDatabase(
211 int32_t ipc_database_id,
212 const std::vector<int32_t>& observer_ids_to_remove) {
213 for (int32_t id_to_remove : observer_ids_to_remove) {
214 observers_.Remove(id_to_remove);
215 }
216 Send(new IndexedDBHostMsg_DatabaseUnobserve(ipc_database_id,
217 observer_ids_to_remove));
218 }
219
220 void IndexedDBDispatcher::RemoveIDBObservers(
221 const std::set<int32_t>& observer_ids_to_remove) {
222 for (int32_t id : observer_ids_to_remove)
223 observers_.Remove(id);
224 }
225
226 void IndexedDBDispatcher::RequestIDBCursorAdvance(
227 unsigned long count,
228 WebIDBCallbacks* callbacks_ptr,
229 int32_t ipc_cursor_id,
230 int64_t transaction_id) {
231 // Reset all cursor prefetch caches except for this cursor.
232 ResetCursorPrefetchCaches(transaction_id, ipc_cursor_id);
233
234 std::unique_ptr<WebIDBCallbacks> callbacks(callbacks_ptr);
235
236 int32_t ipc_callbacks_id = pending_callbacks_.Add(callbacks.release());
237 Send(new IndexedDBHostMsg_CursorAdvance(
238 ipc_cursor_id, CurrentWorkerId(), ipc_callbacks_id, count));
239 }
240
241 void IndexedDBDispatcher::RequestIDBCursorContinue(
242 const IndexedDBKey& key,
243 const IndexedDBKey& primary_key,
244 WebIDBCallbacks* callbacks_ptr,
245 int32_t ipc_cursor_id,
246 int64_t transaction_id) {
247 // Reset all cursor prefetch caches except for this cursor.
248 ResetCursorPrefetchCaches(transaction_id, ipc_cursor_id);
249
250 std::unique_ptr<WebIDBCallbacks> callbacks(callbacks_ptr);
251
252 int32_t ipc_callbacks_id = pending_callbacks_.Add(callbacks.release());
253 Send(new IndexedDBHostMsg_CursorContinue(
254 ipc_cursor_id, CurrentWorkerId(), ipc_callbacks_id, key, primary_key));
255 }
256
257 void IndexedDBDispatcher::RequestIDBCursorPrefetch(
258 int n,
259 WebIDBCallbacks* callbacks_ptr,
260 int32_t ipc_cursor_id) {
261 std::unique_ptr<WebIDBCallbacks> callbacks(callbacks_ptr);
262
263 int32_t ipc_callbacks_id = pending_callbacks_.Add(callbacks.release());
264 Send(new IndexedDBHostMsg_CursorPrefetch(
265 ipc_cursor_id, CurrentWorkerId(), ipc_callbacks_id, n));
266 }
267
268 void IndexedDBDispatcher::RequestIDBCursorPrefetchReset(int used_prefetches,
269 int unused_prefetches,
270 int32_t ipc_cursor_id) {
271 Send(new IndexedDBHostMsg_CursorPrefetchReset(
272 ipc_cursor_id, used_prefetches, unused_prefetches));
273 }
274
275 void IndexedDBDispatcher::RequestIDBFactoryOpen(
276 const base::string16& name,
277 int64_t version,
278 int64_t transaction_id,
279 WebIDBCallbacks* callbacks_ptr,
280 WebIDBDatabaseCallbacks* database_callbacks_ptr,
281 const url::Origin& origin) {
282 std::unique_ptr<WebIDBCallbacks> callbacks(callbacks_ptr);
283 std::unique_ptr<WebIDBDatabaseCallbacks> database_callbacks(
284 database_callbacks_ptr);
285
286 IndexedDBHostMsg_FactoryOpen_Params params;
287 params.ipc_thread_id = CurrentWorkerId();
288 params.ipc_callbacks_id = pending_callbacks_.Add(callbacks.release());
289 params.ipc_database_callbacks_id =
290 pending_database_callbacks_.Add(database_callbacks.release());
291 params.origin = origin;
292 params.name = name;
293 params.transaction_id = transaction_id;
294 params.version = version;
295 Send(new IndexedDBHostMsg_FactoryOpen(params));
296 }
297
298 void IndexedDBDispatcher::RequestIDBFactoryGetDatabaseNames(
299 WebIDBCallbacks* callbacks_ptr,
300 const url::Origin& origin) {
301 std::unique_ptr<WebIDBCallbacks> callbacks(callbacks_ptr);
302
303 IndexedDBHostMsg_FactoryGetDatabaseNames_Params params;
304 params.ipc_thread_id = CurrentWorkerId();
305 params.ipc_callbacks_id = pending_callbacks_.Add(callbacks.release());
306 params.origin = origin;
307 Send(new IndexedDBHostMsg_FactoryGetDatabaseNames(params));
308 }
309
310 void IndexedDBDispatcher::RequestIDBFactoryDeleteDatabase(
311 const base::string16& name,
312 WebIDBCallbacks* callbacks_ptr,
313 const url::Origin& origin) {
314 std::unique_ptr<WebIDBCallbacks> callbacks(callbacks_ptr);
315
316 IndexedDBHostMsg_FactoryDeleteDatabase_Params params;
317 params.ipc_thread_id = CurrentWorkerId();
318 params.ipc_callbacks_id = pending_callbacks_.Add(callbacks.release());
319 params.origin = origin;
320 params.name = name;
321 Send(new IndexedDBHostMsg_FactoryDeleteDatabase(params));
322 }
323
324 void IndexedDBDispatcher::RequestIDBDatabaseClose(
325 int32_t ipc_database_id,
326 int32_t ipc_database_callbacks_id) {
327 Send(new IndexedDBHostMsg_DatabaseClose(ipc_database_id));
328 // There won't be pending database callbacks if the transaction was aborted in
329 // the initial upgradeneeded event handler.
330 if (pending_database_callbacks_.Lookup(ipc_database_callbacks_id))
331 pending_database_callbacks_.Remove(ipc_database_callbacks_id);
332 }
333
334 void IndexedDBDispatcher::NotifyIDBDatabaseVersionChangeIgnored(
335 int32_t ipc_database_id) {
336 Send(new IndexedDBHostMsg_DatabaseVersionChangeIgnored(ipc_database_id));
337 }
338
339 void IndexedDBDispatcher::RequestIDBDatabaseCreateTransaction(
340 int32_t ipc_database_id,
341 int64_t transaction_id,
342 WebIDBDatabaseCallbacks* database_callbacks_ptr,
343 WebVector<long long> object_store_ids,
344 blink::WebIDBTransactionMode mode) {
345 std::unique_ptr<WebIDBDatabaseCallbacks> database_callbacks(
346 database_callbacks_ptr);
347 IndexedDBHostMsg_DatabaseCreateTransaction_Params params;
348 params.ipc_thread_id = CurrentWorkerId();
349 params.ipc_database_id = ipc_database_id;
350 params.transaction_id = transaction_id;
351 params.ipc_database_callbacks_id =
352 pending_database_callbacks_.Add(database_callbacks.release());
353 params.object_store_ids
354 .assign(object_store_ids.data(),
355 object_store_ids.data() + object_store_ids.size());
356 params.mode = mode;
357
358 Send(new IndexedDBHostMsg_DatabaseCreateTransaction(params));
359 }
360
361 void IndexedDBDispatcher::RequestIDBDatabaseGet(
362 int32_t ipc_database_id,
363 int64_t transaction_id,
364 int64_t object_store_id,
365 int64_t index_id,
366 const IndexedDBKeyRange& key_range,
367 bool key_only,
368 WebIDBCallbacks* callbacks) {
369 ResetCursorPrefetchCaches(transaction_id, kAllCursors);
370 IndexedDBHostMsg_DatabaseGet_Params params;
371 init_params(&params, callbacks);
372 params.ipc_database_id = ipc_database_id;
373 params.transaction_id = transaction_id;
374 params.object_store_id = object_store_id;
375 params.index_id = index_id;
376 params.key_range = key_range;
377 params.key_only = key_only;
378 Send(new IndexedDBHostMsg_DatabaseGet(params));
379 }
380
381 void IndexedDBDispatcher::RequestIDBDatabaseGetAll(
382 int32_t ipc_database_id,
383 int64_t transaction_id,
384 int64_t object_store_id,
385 int64_t index_id,
386 const IndexedDBKeyRange& key_range,
387 bool key_only,
388 int64_t max_count,
389 WebIDBCallbacks* callbacks) {
390 ResetCursorPrefetchCaches(transaction_id, kAllCursors);
391 IndexedDBHostMsg_DatabaseGetAll_Params params;
392 init_params(&params, callbacks);
393 params.ipc_database_id = ipc_database_id;
394 params.transaction_id = transaction_id;
395 params.object_store_id = object_store_id;
396 params.index_id = index_id;
397 params.key_range = key_range;
398 params.key_only = key_only;
399 params.max_count = max_count;
400 Send(new IndexedDBHostMsg_DatabaseGetAll(params));
401 }
402
403 void IndexedDBDispatcher::RequestIDBDatabasePut(
404 int32_t ipc_database_id,
405 int64_t transaction_id,
406 int64_t object_store_id,
407 const WebData& value,
408 const blink::WebVector<WebBlobInfo>& web_blob_info,
409 const IndexedDBKey& key,
410 blink::WebIDBPutMode put_mode,
411 WebIDBCallbacks* callbacks,
412 const WebVector<long long>& index_ids,
413 const WebVector<WebVector<WebIDBKey>>& index_keys) {
414 if (value.size() + key.size_estimate() > max_put_value_size_) {
415 callbacks->onError(WebIDBDatabaseError(
416 blink::WebIDBDatabaseExceptionUnknownError,
417 WebString::fromUTF8(base::StringPrintf(
418 "The serialized value is too large"
419 " (size=%" PRIuS " bytes, max=%" PRIuS " bytes).",
420 value.size(),
421 max_put_value_size_).c_str())));
422 return;
423 }
424
425 ResetCursorPrefetchCaches(transaction_id, kAllCursors);
426 IndexedDBHostMsg_DatabasePut_Params params;
427 init_params(&params, callbacks);
428 params.ipc_database_id = ipc_database_id;
429 params.transaction_id = transaction_id;
430 params.object_store_id = object_store_id;
431
432 params.value.bits.assign(value.data(), value.data() + value.size());
433 params.key = key;
434 params.put_mode = put_mode;
435
436 DCHECK_EQ(index_ids.size(), index_keys.size());
437 params.index_keys.resize(index_ids.size());
438 for (size_t i = 0, len = index_ids.size(); i < len; ++i) {
439 params.index_keys[i].first = index_ids[i];
440 params.index_keys[i].second.resize(index_keys[i].size());
441 for (size_t j = 0; j < index_keys[i].size(); ++j) {
442 params.index_keys[i].second[j] =
443 IndexedDBKey(IndexedDBKeyBuilder::Build(index_keys[i][j]));
444 }
445 }
446
447 params.value.blob_or_file_info.resize(web_blob_info.size());
448 for (size_t i = 0; i < web_blob_info.size(); ++i) {
449 const WebBlobInfo& info = web_blob_info[i];
450 IndexedDBMsg_BlobOrFileInfo& blob_or_file_info =
451 params.value.blob_or_file_info[i];
452 blob_or_file_info.is_file = info.isFile();
453 if (info.isFile()) {
454 blob_or_file_info.file_path = info.filePath();
455 blob_or_file_info.file_name = info.fileName();
456 blob_or_file_info.last_modified = info.lastModified();
457 }
458 blob_or_file_info.size = info.size();
459 blob_or_file_info.uuid = info.uuid().latin1();
460 DCHECK(blob_or_file_info.uuid.size());
461 blob_or_file_info.mime_type = info.type();
462 }
463
464 Send(new IndexedDBHostMsg_DatabasePut(params));
465 }
466
467 void IndexedDBDispatcher::RequestIDBDatabaseOpenCursor(
468 int32_t ipc_database_id,
469 int64_t transaction_id,
470 int64_t object_store_id,
471 int64_t index_id,
472 const IndexedDBKeyRange& key_range,
473 blink::WebIDBCursorDirection direction,
474 bool key_only,
475 blink::WebIDBTaskType task_type,
476 WebIDBCallbacks* callbacks) {
477 ResetCursorPrefetchCaches(transaction_id, kAllCursors);
478 IndexedDBHostMsg_DatabaseOpenCursor_Params params;
479 init_params(&params, callbacks);
480 params.ipc_database_id = ipc_database_id;
481 params.transaction_id = transaction_id;
482 params.object_store_id = object_store_id;
483 params.index_id = index_id;
484 params.key_range = key_range;
485 params.direction = direction;
486 params.key_only = key_only;
487 params.task_type = task_type;
488 Send(new IndexedDBHostMsg_DatabaseOpenCursor(params));
489
490 DCHECK(cursor_transaction_ids_.find(params.ipc_callbacks_id) ==
491 cursor_transaction_ids_.end());
492 cursor_transaction_ids_[params.ipc_callbacks_id] = transaction_id;
493 }
494
495 void IndexedDBDispatcher::RequestIDBDatabaseCount(
496 int32_t ipc_database_id,
497 int64_t transaction_id,
498 int64_t object_store_id,
499 int64_t index_id,
500 const IndexedDBKeyRange& key_range,
501 WebIDBCallbacks* callbacks) {
502 ResetCursorPrefetchCaches(transaction_id, kAllCursors);
503 IndexedDBHostMsg_DatabaseCount_Params params;
504 init_params(&params, callbacks);
505 params.ipc_database_id = ipc_database_id;
506 params.transaction_id = transaction_id;
507 params.object_store_id = object_store_id;
508 params.index_id = index_id;
509 params.key_range = key_range;
510 Send(new IndexedDBHostMsg_DatabaseCount(params));
511 }
512
513 void IndexedDBDispatcher::RequestIDBDatabaseDeleteRange(
514 int32_t ipc_database_id,
515 int64_t transaction_id,
516 int64_t object_store_id,
517 const IndexedDBKeyRange& key_range,
518 WebIDBCallbacks* callbacks) {
519 ResetCursorPrefetchCaches(transaction_id, kAllCursors);
520 IndexedDBHostMsg_DatabaseDeleteRange_Params params;
521 init_params(&params, callbacks);
522 params.ipc_database_id = ipc_database_id;
523 params.transaction_id = transaction_id;
524 params.object_store_id = object_store_id;
525 params.key_range = key_range;
526 Send(new IndexedDBHostMsg_DatabaseDeleteRange(params));
527 }
528
529 void IndexedDBDispatcher::RequestIDBDatabaseClear(
530 int32_t ipc_database_id,
531 int64_t transaction_id,
532 int64_t object_store_id,
533 WebIDBCallbacks* callbacks_ptr) {
534 ResetCursorPrefetchCaches(transaction_id, kAllCursors);
535 std::unique_ptr<WebIDBCallbacks> callbacks(callbacks_ptr);
536 int32_t ipc_callbacks_id = pending_callbacks_.Add(callbacks.release());
537 Send(new IndexedDBHostMsg_DatabaseClear(CurrentWorkerId(),
538 ipc_callbacks_id,
539 ipc_database_id,
540 transaction_id,
541 object_store_id));
542 }
543
544 void IndexedDBDispatcher::CursorDestroyed(int32_t ipc_cursor_id) {
545 cursors_.erase(ipc_cursor_id);
546 }
547
548 void IndexedDBDispatcher::DatabaseDestroyed(int32_t ipc_database_id) {
549 DCHECK_EQ(databases_.count(ipc_database_id), 1u);
550 databases_.erase(ipc_database_id);
551 }
552
553 void IndexedDBDispatcher::OnSuccessIDBDatabase(
554 int32_t ipc_thread_id,
555 int32_t ipc_callbacks_id,
556 int32_t ipc_database_callbacks_id,
557 int32_t ipc_object_id,
558 const IndexedDBDatabaseMetadata& idb_metadata) {
559 DCHECK_EQ(ipc_thread_id, CurrentWorkerId());
560 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(ipc_callbacks_id);
561 if (!callbacks)
562 return;
563 WebIDBMetadata metadata(ConvertMetadata(idb_metadata));
564 // If an upgrade was performed, count will be non-zero.
565 WebIDBDatabase* database = NULL;
566
567 // Back-end will send kNoDatabase if it was already sent in OnUpgradeNeeded.
568 // May already be deleted and removed from the table, but do not recreate..
569 if (ipc_object_id != kNoDatabase) {
570 DCHECK(!databases_.count(ipc_object_id));
571 database = databases_[ipc_object_id] = new WebIDBDatabaseImpl(
572 ipc_object_id, ipc_database_callbacks_id, thread_safe_sender_.get());
573 }
574
575 callbacks->onSuccess(database, metadata);
576 pending_callbacks_.Remove(ipc_callbacks_id);
577 }
578
579 void IndexedDBDispatcher::OnSuccessIndexedDBKey(int32_t ipc_thread_id,
580 int32_t ipc_callbacks_id,
581 const IndexedDBKey& key) {
582 DCHECK_EQ(ipc_thread_id, CurrentWorkerId());
583 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(ipc_callbacks_id);
584 if (!callbacks)
585 return;
586 callbacks->onSuccess(WebIDBKeyBuilder::Build(key));
587 pending_callbacks_.Remove(ipc_callbacks_id);
588 }
589
590 void IndexedDBDispatcher::OnSuccessStringList(
591 int32_t ipc_thread_id,
592 int32_t ipc_callbacks_id,
593 const std::vector<base::string16>& value) {
594 DCHECK_EQ(ipc_thread_id, CurrentWorkerId());
595 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(ipc_callbacks_id);
596 if (!callbacks)
597 return;
598 callbacks->onSuccess(WebVector<WebString>(value));
599 pending_callbacks_.Remove(ipc_callbacks_id);
600 }
601
602 // Populate some WebIDBValue members (data & blob info) from the supplied
603 // value message (IndexedDBMsg_Value or one that includes it).
604 template <class IndexedDBMsgValueType>
605 static void PrepareWebValue(const IndexedDBMsgValueType& value,
606 WebIDBValue* web_value) {
607 if (value.bits.empty())
608 return;
609
610 web_value->data.assign(&*value.bits.begin(), value.bits.size());
611 blink::WebVector<WebBlobInfo> local_blob_info(value.blob_or_file_info.size());
612 for (size_t i = 0; i < value.blob_or_file_info.size(); ++i) {
613 const IndexedDBMsg_BlobOrFileInfo& info = value.blob_or_file_info[i];
614 if (info.is_file) {
615 local_blob_info[i] = WebBlobInfo(
616 WebString::fromUTF8(info.uuid.c_str()), info.file_path,
617 info.file_name, info.mime_type, info.last_modified, info.size);
618 } else {
619 local_blob_info[i] = WebBlobInfo(WebString::fromUTF8(info.uuid.c_str()),
620 info.mime_type, info.size);
621 }
622 }
623
624 web_value->webBlobInfo.swap(local_blob_info);
625 }
626
627 static void PrepareReturnWebValue(const IndexedDBMsg_ReturnValue& value,
628 WebIDBValue* web_value) {
629 PrepareWebValue(value, web_value);
630 web_value->primaryKey = WebIDBKeyBuilder::Build(value.primary_key);
631 web_value->keyPath = WebIDBKeyPathBuilder::Build(value.key_path);
632 }
633
634 void IndexedDBDispatcher::OnSuccessArray(
635 const IndexedDBMsg_CallbacksSuccessArray_Params& p) {
636 DCHECK_EQ(p.ipc_thread_id, CurrentWorkerId());
637 int32_t ipc_callbacks_id = p.ipc_callbacks_id;
638 blink::WebVector<WebIDBValue> web_values(p.values.size());
639 for (size_t i = 0; i < p.values.size(); ++i)
640 PrepareReturnWebValue(p.values[i], &web_values[i]);
641 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(ipc_callbacks_id);
642 DCHECK(callbacks);
643 callbacks->onSuccess(web_values);
644 pending_callbacks_.Remove(ipc_callbacks_id);
645 }
646
647 void IndexedDBDispatcher::OnSuccessValue(
648 const IndexedDBMsg_CallbacksSuccessValue_Params& params) {
649 DCHECK_EQ(params.ipc_thread_id, CurrentWorkerId());
650 WebIDBCallbacks* callbacks =
651 pending_callbacks_.Lookup(params.ipc_callbacks_id);
652 if (!callbacks)
653 return;
654 WebIDBValue web_value;
655 PrepareReturnWebValue(params.value, &web_value);
656 if (params.value.primary_key.IsValid()) {
657 web_value.primaryKey = WebIDBKeyBuilder::Build(params.value.primary_key);
658 web_value.keyPath = WebIDBKeyPathBuilder::Build(params.value.key_path);
659 }
660 callbacks->onSuccess(web_value);
661 cursor_transaction_ids_.erase(params.ipc_callbacks_id);
662 pending_callbacks_.Remove(params.ipc_callbacks_id);
663 }
664
665 void IndexedDBDispatcher::OnSuccessInteger(int32_t ipc_thread_id,
666 int32_t ipc_callbacks_id,
667 int64_t value) {
668 DCHECK_EQ(ipc_thread_id, CurrentWorkerId());
669 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(ipc_callbacks_id);
670 if (!callbacks)
671 return;
672 callbacks->onSuccess(value);
673 pending_callbacks_.Remove(ipc_callbacks_id);
674 }
675
676 void IndexedDBDispatcher::OnSuccessUndefined(int32_t ipc_thread_id,
677 int32_t ipc_callbacks_id) {
678 DCHECK_EQ(ipc_thread_id, CurrentWorkerId());
679 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(ipc_callbacks_id);
680 if (!callbacks)
681 return;
682 callbacks->onSuccess();
683 pending_callbacks_.Remove(ipc_callbacks_id);
684 }
685
686 void IndexedDBDispatcher::OnSuccessOpenCursor(
687 const IndexedDBMsg_CallbacksSuccessIDBCursor_Params& p) {
688 DCHECK_EQ(p.ipc_thread_id, CurrentWorkerId());
689 int32_t ipc_callbacks_id = p.ipc_callbacks_id;
690 int32_t ipc_object_id = p.ipc_cursor_id;
691 const IndexedDBKey& key = p.key;
692 const IndexedDBKey& primary_key = p.primary_key;
693 WebIDBValue web_value;
694 PrepareWebValue(p.value, &web_value);
695
696 DCHECK(cursor_transaction_ids_.find(ipc_callbacks_id) !=
697 cursor_transaction_ids_.end());
698 int64_t transaction_id = cursor_transaction_ids_[ipc_callbacks_id];
699 cursor_transaction_ids_.erase(ipc_callbacks_id);
700
701 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(ipc_callbacks_id);
702 if (!callbacks)
703 return;
704
705 WebIDBCursorImpl* cursor = new WebIDBCursorImpl(
706 ipc_object_id, transaction_id, thread_safe_sender_.get());
707 cursors_[ipc_object_id] = cursor;
708 callbacks->onSuccess(cursor, WebIDBKeyBuilder::Build(key),
709 WebIDBKeyBuilder::Build(primary_key), web_value);
710
711 pending_callbacks_.Remove(ipc_callbacks_id);
712 }
713
714 void IndexedDBDispatcher::OnSuccessCursorContinue(
715 const IndexedDBMsg_CallbacksSuccessCursorContinue_Params& p) {
716 DCHECK_EQ(p.ipc_thread_id, CurrentWorkerId());
717 int32_t ipc_callbacks_id = p.ipc_callbacks_id;
718 int32_t ipc_cursor_id = p.ipc_cursor_id;
719 const IndexedDBKey& key = p.key;
720 const IndexedDBKey& primary_key = p.primary_key;
721
722 if (cursors_.find(ipc_cursor_id) == cursors_.end())
723 return;
724
725 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(ipc_callbacks_id);
726 if (!callbacks)
727 return;
728
729 WebIDBValue web_value;
730 PrepareWebValue(p.value, &web_value);
731 callbacks->onSuccess(WebIDBKeyBuilder::Build(key),
732 WebIDBKeyBuilder::Build(primary_key), web_value);
733
734 pending_callbacks_.Remove(ipc_callbacks_id);
735 }
736
737 void IndexedDBDispatcher::OnSuccessCursorPrefetch(
738 const IndexedDBMsg_CallbacksSuccessCursorPrefetch_Params& p) {
739 DCHECK_EQ(p.ipc_thread_id, CurrentWorkerId());
740 int32_t ipc_callbacks_id = p.ipc_callbacks_id;
741 int32_t ipc_cursor_id = p.ipc_cursor_id;
742 std::vector<WebIDBValue> values(p.values.size());
743 for (size_t i = 0; i < p.values.size(); ++i)
744 PrepareWebValue(p.values[i], &values[i]);
745 std::map<int32_t, WebIDBCursorImpl*>::const_iterator cur_iter =
746 cursors_.find(ipc_cursor_id);
747 if (cur_iter == cursors_.end())
748 return;
749
750 cur_iter->second->SetPrefetchData(p.keys, p.primary_keys, values);
751
752 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(ipc_callbacks_id);
753 DCHECK(callbacks);
754 cur_iter->second->CachedContinue(callbacks);
755 pending_callbacks_.Remove(ipc_callbacks_id);
756 }
757
758 void IndexedDBDispatcher::OnIntBlocked(int32_t ipc_thread_id,
759 int32_t ipc_callbacks_id,
760 int64_t existing_version) {
761 DCHECK_EQ(ipc_thread_id, CurrentWorkerId());
762 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(ipc_callbacks_id);
763 DCHECK(callbacks);
764 callbacks->onBlocked(existing_version);
765 }
766
767 void IndexedDBDispatcher::OnUpgradeNeeded(
768 const IndexedDBMsg_CallbacksUpgradeNeeded_Params& p) {
769 DCHECK_EQ(p.ipc_thread_id, CurrentWorkerId());
770 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(p.ipc_callbacks_id);
771 DCHECK(callbacks);
772 WebIDBMetadata metadata(ConvertMetadata(p.idb_metadata));
773 DCHECK(!databases_.count(p.ipc_database_id));
774 databases_[p.ipc_database_id] =
775 new WebIDBDatabaseImpl(p.ipc_database_id,
776 p.ipc_database_callbacks_id,
777 thread_safe_sender_.get());
778 callbacks->onUpgradeNeeded(
779 p.old_version,
780 databases_[p.ipc_database_id],
781 metadata,
782 static_cast<blink::WebIDBDataLoss>(p.data_loss),
783 WebString::fromUTF8(p.data_loss_message));
784 }
785
786 void IndexedDBDispatcher::OnError(int32_t ipc_thread_id,
787 int32_t ipc_callbacks_id,
788 int code,
789 const base::string16& message) {
790 DCHECK_EQ(ipc_thread_id, CurrentWorkerId());
791 WebIDBCallbacks* callbacks = pending_callbacks_.Lookup(ipc_callbacks_id);
792 if (!callbacks)
793 return;
794 if (message.empty())
795 callbacks->onError(WebIDBDatabaseError(code));
796 else
797 callbacks->onError(WebIDBDatabaseError(code, message));
798 pending_callbacks_.Remove(ipc_callbacks_id);
799 cursor_transaction_ids_.erase(ipc_callbacks_id);
800 }
801
802 void IndexedDBDispatcher::OnAbort(int32_t ipc_thread_id,
803 int32_t ipc_database_callbacks_id,
804 int64_t transaction_id,
805 int code,
806 const base::string16& message) {
807 DCHECK_EQ(ipc_thread_id, CurrentWorkerId());
808 WebIDBDatabaseCallbacks* callbacks =
809 pending_database_callbacks_.Lookup(ipc_database_callbacks_id);
810 if (!callbacks)
811 return;
812 if (message.empty())
813 callbacks->onAbort(transaction_id, WebIDBDatabaseError(code));
814 else
815 callbacks->onAbort(transaction_id, WebIDBDatabaseError(code, message));
816 }
817
818 void IndexedDBDispatcher::OnComplete(int32_t ipc_thread_id,
819 int32_t ipc_database_callbacks_id,
820 int64_t transaction_id) {
821 DCHECK_EQ(ipc_thread_id, CurrentWorkerId());
822 WebIDBDatabaseCallbacks* callbacks =
823 pending_database_callbacks_.Lookup(ipc_database_callbacks_id);
824 if (!callbacks)
825 return;
826 callbacks->onComplete(transaction_id);
827 }
828
829 void IndexedDBDispatcher::OnDatabaseChanges(
830 int32_t ipc_thread_id,
831 int32_t ipc_database_id,
832 const IndexedDBMsg_ObserverChanges& changes) {
833 DCHECK_EQ(ipc_thread_id, CurrentWorkerId());
834 std::vector<WebIDBObservation> observations(
835 ConvertObservations(changes.observations));
836 for (auto& it : changes.observation_index) {
837 WebIDBObserver* observer = observers_.Lookup(it.first);
838 // An observer can be removed from the renderer, but still exist in the
839 // backend. Moreover, observer might have recorded some changes before being
840 // removed from the backend and thus, have its id be present in changes.
841 if (!observer)
842 continue;
843 observer->onChange(observations, std::move(it.second));
844 }
845 }
846
847 void IndexedDBDispatcher::OnForcedClose(int32_t ipc_thread_id,
848 int32_t ipc_database_callbacks_id) {
849 DCHECK_EQ(ipc_thread_id, CurrentWorkerId());
850 WebIDBDatabaseCallbacks* callbacks =
851 pending_database_callbacks_.Lookup(ipc_database_callbacks_id);
852 if (!callbacks)
853 return;
854 callbacks->onForcedClose();
855 }
856
857 void IndexedDBDispatcher::OnVersionChange(int32_t ipc_thread_id,
858 int32_t ipc_database_callbacks_id,
859 int64_t old_version,
860 int64_t new_version) {
861 DCHECK_EQ(ipc_thread_id, CurrentWorkerId());
862 WebIDBDatabaseCallbacks* callbacks =
863 pending_database_callbacks_.Lookup(ipc_database_callbacks_id);
864 // callbacks would be NULL if a versionchange event is received after close
865 // has been called.
866 if (!callbacks)
867 return;
868 callbacks->onVersionChange(old_version, new_version);
869 }
870
871 void IndexedDBDispatcher::ResetCursorPrefetchCaches(
872 int64_t transaction_id,
873 int32_t ipc_exception_cursor_id) {
874 typedef std::map<int32_t, WebIDBCursorImpl*>::iterator Iterator;
875 for (Iterator i = cursors_.begin(); i != cursors_.end(); ++i) {
876 if (i->first == ipc_exception_cursor_id ||
877 i->second->transaction_id() != transaction_id)
878 continue;
879 i->second->ResetPrefetchCache();
880 }
881 }
882
883 } // namespace content
OLDNEW
« no previous file with comments | « content/child/indexed_db/indexed_db_dispatcher.h ('k') | content/child/indexed_db/indexed_db_dispatcher_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698