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