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

Side by Side Diff: third_party/WebKit/Source/modules/indexeddb/IDBObjectStore.cpp

Issue 2822453003: Wrap large IndexedDB values into Blobs before writing to LevelDB. (Closed)
Patch Set: Rebase + start addressing feedback. Created 3 years, 7 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
1 /* 1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved. 2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 7 *
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 22 matching lines...) Expand all
33 #include "bindings/modules/v8/ToV8ForModules.h" 33 #include "bindings/modules/v8/ToV8ForModules.h"
34 #include "bindings/modules/v8/V8BindingForModules.h" 34 #include "bindings/modules/v8/V8BindingForModules.h"
35 #include "core/dom/DOMStringList.h" 35 #include "core/dom/DOMStringList.h"
36 #include "core/dom/ExceptionCode.h" 36 #include "core/dom/ExceptionCode.h"
37 #include "core/dom/ExecutionContext.h" 37 #include "core/dom/ExecutionContext.h"
38 #include "modules/indexeddb/IDBAny.h" 38 #include "modules/indexeddb/IDBAny.h"
39 #include "modules/indexeddb/IDBCursorWithValue.h" 39 #include "modules/indexeddb/IDBCursorWithValue.h"
40 #include "modules/indexeddb/IDBDatabase.h" 40 #include "modules/indexeddb/IDBDatabase.h"
41 #include "modules/indexeddb/IDBKeyPath.h" 41 #include "modules/indexeddb/IDBKeyPath.h"
42 #include "modules/indexeddb/IDBTracing.h" 42 #include "modules/indexeddb/IDBTracing.h"
43 #include "modules/indexeddb/IDBValueWrapper.h"
43 #include "platform/Histogram.h" 44 #include "platform/Histogram.h"
44 #include "platform/SharedBuffer.h" 45 #include "platform/SharedBuffer.h"
45 #include "platform/bindings/ScriptState.h" 46 #include "platform/bindings/ScriptState.h"
47 #include "platform/wtf/RefPtr.h"
46 #include "public/platform/WebBlobInfo.h" 48 #include "public/platform/WebBlobInfo.h"
47 #include "public/platform/WebData.h" 49 #include "public/platform/WebData.h"
48 #include "public/platform/WebVector.h" 50 #include "public/platform/WebVector.h"
49 #include "public/platform/modules/indexeddb/WebIDBKey.h" 51 #include "public/platform/modules/indexeddb/WebIDBKey.h"
50 #include "public/platform/modules/indexeddb/WebIDBKeyRange.h" 52 #include "public/platform/modules/indexeddb/WebIDBKeyRange.h"
51 #include "v8/include/v8.h" 53 #include "v8/include/v8.h"
52 54
53 using blink::WebBlobInfo; 55 using blink::WebBlobInfo;
54 using blink::WebIDBCallbacks; 56 using blink::WebIDBCallbacks;
55 using blink::WebIDBCursor; 57 using blink::WebIDBCursor;
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after
369 return nullptr; 371 return nullptr;
370 } 372 }
371 if (transaction_->IsReadOnly()) { 373 if (transaction_->IsReadOnly()) {
372 exception_state.ThrowDOMException( 374 exception_state.ThrowDOMException(
373 kReadOnlyError, IDBDatabase::kTransactionReadOnlyErrorMessage); 375 kReadOnlyError, IDBDatabase::kTransactionReadOnlyErrorMessage);
374 return nullptr; 376 return nullptr;
375 } 377 }
376 378
377 v8::Isolate* isolate = script_state->GetIsolate(); 379 v8::Isolate* isolate = script_state->GetIsolate();
378 DCHECK(isolate->InContext()); 380 DCHECK(isolate->InContext());
379 Vector<WebBlobInfo> blob_info; 381 bool write_wasm_to_stream =
380 SerializedScriptValue::SerializeOptions options;
381 options.blob_info = &blob_info;
382 options.write_wasm_to_stream =
383 ExecutionContext::From(script_state)->IsSecureContext(); 382 ExecutionContext::From(script_state)->IsSecureContext();
384 options.for_storage = true; 383 IDBValueWrapper value_wrapper(isolate, value.V8Value(), write_wasm_to_stream,
jsbell 2017/05/08 22:08:01 Is the motivation for introducing the wrapper here
pwnall 2017/05/11 23:54:25 Yes. I wanted to pull out all the serialization co
385 RefPtr<SerializedScriptValue> serialized_value = 384 exception_state);
386 SerializedScriptValue::Serialize(isolate, value.V8Value(), options,
387 exception_state);
388 if (exception_state.HadException()) 385 if (exception_state.HadException())
389 return nullptr; 386 return nullptr;
390 387
391 // Keys that need to be extracted must be taken from a clone so that 388 // Keys that need to be extracted must be taken from a clone so that
392 // side effects (i.e. getters) are not triggered. Construct the 389 // side effects (i.e. getters) are not triggered. Construct the
393 // clone lazily since the operation may be expensive. 390 // clone lazily since the operation may be expensive.
394 ScriptValue clone; 391 ScriptValue clone;
395 392
396 const IDBKeyPath& key_path = IdbKeyPath(); 393 const IDBKeyPath& key_path = IdbKeyPath();
397 const bool uses_in_line_keys = !key_path.IsNull(); 394 const bool uses_in_line_keys = !key_path.IsNull();
398 const bool has_key_generator = autoIncrement(); 395 const bool has_key_generator = autoIncrement();
399 396
400 if (put_mode != kWebIDBPutModeCursorUpdate && uses_in_line_keys && key) { 397 if (put_mode != kWebIDBPutModeCursorUpdate && uses_in_line_keys && key) {
401 exception_state.ThrowDOMException(kDataError, 398 exception_state.ThrowDOMException(kDataError,
402 "The object store uses in-line keys and " 399 "The object store uses in-line keys and "
403 "the key parameter was provided."); 400 "the key parameter was provided.");
404 return nullptr; 401 return nullptr;
405 } 402 }
406 403
407 // This test logically belongs in IDBCursor, but must operate on the cloned 404 // This test logically belongs in IDBCursor, but must operate on the cloned
408 // value. 405 // value.
409 if (put_mode == kWebIDBPutModeCursorUpdate && uses_in_line_keys) { 406 if (put_mode == kWebIDBPutModeCursorUpdate && uses_in_line_keys) {
410 DCHECK(key); 407 DCHECK(key);
411 if (clone.IsEmpty()) 408 DCHECK(clone.IsEmpty());
412 clone = DeserializeScriptValue(script_state, serialized_value.Get(), 409 value_wrapper.Clone(script_state, &clone);
413 &blob_info);
414 IDBKey* key_path_key = ScriptValue::To<IDBKey*>( 410 IDBKey* key_path_key = ScriptValue::To<IDBKey*>(
415 script_state->GetIsolate(), clone, exception_state, key_path); 411 script_state->GetIsolate(), clone, exception_state, key_path);
416 if (exception_state.HadException()) 412 if (exception_state.HadException())
417 return nullptr; 413 return nullptr;
418 if (!key_path_key || !key_path_key->IsEqual(key)) { 414 if (!key_path_key || !key_path_key->IsEqual(key)) {
419 exception_state.ThrowDOMException( 415 exception_state.ThrowDOMException(
420 kDataError, 416 kDataError,
421 "The effective object store of this cursor uses in-line keys and " 417 "The effective object store of this cursor uses in-line keys and "
422 "evaluating the key path of the value parameter results in a " 418 "evaluating the key path of the value parameter results in a "
423 "different value than the cursor's effective key."); 419 "different value than the cursor's effective key.");
424 return nullptr; 420 return nullptr;
425 } 421 }
426 } 422 }
427 423
428 if (!uses_in_line_keys && !has_key_generator && !key) { 424 if (!uses_in_line_keys && !has_key_generator && !key) {
429 exception_state.ThrowDOMException(kDataError, 425 exception_state.ThrowDOMException(kDataError,
430 "The object store uses out-of-line keys " 426 "The object store uses out-of-line keys "
431 "and has no key generator and the key " 427 "and has no key generator and the key "
432 "parameter was not provided."); 428 "parameter was not provided.");
433 return nullptr; 429 return nullptr;
434 } 430 }
435 if (uses_in_line_keys) { 431 if (uses_in_line_keys) {
436 if (clone.IsEmpty()) { 432 if (clone.IsEmpty())
437 clone = DeserializeScriptValue(script_state, serialized_value.Get(), 433 value_wrapper.Clone(script_state, &clone);
438 &blob_info);
439 }
440 IDBKey* key_path_key = ScriptValue::To<IDBKey*>( 434 IDBKey* key_path_key = ScriptValue::To<IDBKey*>(
441 script_state->GetIsolate(), clone, exception_state, key_path); 435 script_state->GetIsolate(), clone, exception_state, key_path);
442 if (exception_state.HadException()) 436 if (exception_state.HadException())
443 return nullptr; 437 return nullptr;
444 if (key_path_key && !key_path_key->IsValid()) { 438 if (key_path_key && !key_path_key->IsValid()) {
445 exception_state.ThrowDOMException( 439 exception_state.ThrowDOMException(
446 kDataError, 440 kDataError,
447 "Evaluating the object store's key path " 441 "Evaluating the object store's key path "
448 "yielded a value that is not a valid " 442 "yielded a value that is not a valid "
449 "key."); 443 "key.");
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 DEFINE_THREAD_SAFE_STATIC_LOCAL( 477 DEFINE_THREAD_SAFE_STATIC_LOCAL(
484 EnumerationHistogram, key_type_histogram, 478 EnumerationHistogram, key_type_histogram,
485 new EnumerationHistogram("WebCore.IndexedDB.ObjectStore.Record.KeyType", 479 new EnumerationHistogram("WebCore.IndexedDB.ObjectStore.Record.KeyType",
486 static_cast<int>(IDBKey::kTypeEnumMax))); 480 static_cast<int>(IDBKey::kTypeEnumMax)));
487 key_type_histogram.Count(static_cast<int>(key->GetType())); 481 key_type_histogram.Count(static_cast<int>(key->GetType()));
488 } 482 }
489 483
490 Vector<int64_t> index_ids; 484 Vector<int64_t> index_ids;
491 HeapVector<IndexKeys> index_keys; 485 HeapVector<IndexKeys> index_keys;
492 for (const auto& it : Metadata().indexes) { 486 for (const auto& it : Metadata().indexes) {
493 if (clone.IsEmpty()) { 487 if (clone.IsEmpty())
494 clone = DeserializeScriptValue(script_state, serialized_value.Get(), 488 value_wrapper.Clone(script_state, &clone);
495 &blob_info);
496 }
497 IndexKeys keys; 489 IndexKeys keys;
498 GenerateIndexKeysForValue(script_state->GetIsolate(), *it.value, clone, 490 GenerateIndexKeysForValue(script_state->GetIsolate(), *it.value, clone,
499 &keys); 491 &keys);
500 index_ids.push_back(it.key); 492 index_ids.push_back(it.key);
501 index_keys.push_back(keys); 493 index_keys.push_back(keys);
502 } 494 }
503 495
504 IDBRequest* request = 496 IDBRequest* request =
505 IDBRequest::Create(script_state, source, transaction_.Get()); 497 IDBRequest::Create(script_state, source, transaction_.Get());
506 Vector<char> wire_bytes;
507 serialized_value->ToWireBytes(wire_bytes);
508 RefPtr<SharedBuffer> value_buffer = SharedBuffer::AdoptVector(wire_bytes);
509 498
510 request->StorePutOperationBlobs(serialized_value->BlobDataHandles()); 499 request->ExtractOutgoingBlobHandlesFrom(&value_wrapper);
500 value_wrapper.WrapIfBiggerThan(IDBValueWrapper::kWrapThreshold);
511 501
512 BackendDB()->Put(transaction_->Id(), Id(), WebData(value_buffer), blob_info, 502 BackendDB()->Put(
513 key, static_cast<WebIDBPutMode>(put_mode), 503 transaction_->Id(), Id(), WebData(value_wrapper.ExtractWireBytes()),
514 request->CreateWebCallbacks().release(), index_ids, 504 value_wrapper.WrappedBlobInfo(), key,
515 index_keys); 505 static_cast<WebIDBPutMode>(put_mode),
506 request->CreateWebCallbacks().release(), index_ids, index_keys);
507
516 return request; 508 return request;
517 } 509 }
518 510
519 IDBRequest* IDBObjectStore::deleteFunction(ScriptState* script_state, 511 IDBRequest* IDBObjectStore::deleteFunction(ScriptState* script_state,
520 const ScriptValue& key, 512 const ScriptValue& key,
521 ExceptionState& exception_state) { 513 ExceptionState& exception_state) {
522 IDB_TRACE("IDBObjectStore::delete"); 514 IDB_TRACE("IDBObjectStore::delete");
523 if (IsDeleted()) { 515 if (IsDeleted()) {
524 exception_state.ThrowDOMException( 516 exception_state.ThrowDOMException(
525 kInvalidStateError, IDBDatabase::kObjectStoreDeletedErrorMessage); 517 kInvalidStateError, IDBDatabase::kObjectStoreDeletedErrorMessage);
(...skipping 529 matching lines...) Expand 10 before | Expand all | Expand 10 after
1055 } 1047 }
1056 } 1048 }
1057 return IDBIndexMetadata::kInvalidId; 1049 return IDBIndexMetadata::kInvalidId;
1058 } 1050 }
1059 1051
1060 WebIDBDatabase* IDBObjectStore::BackendDB() const { 1052 WebIDBDatabase* IDBObjectStore::BackendDB() const {
1061 return transaction_->BackendDB(); 1053 return transaction_->BackendDB();
1062 } 1054 }
1063 1055
1064 } // namespace blink 1056 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698