Index: third_party/WebKit/Source/modules/indexeddb/IDBValueWrapper.cpp |
diff --git a/third_party/WebKit/Source/modules/indexeddb/IDBValueWrapper.cpp b/third_party/WebKit/Source/modules/indexeddb/IDBValueWrapper.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..82e33535bc08c791646185820cba772d9a6fc452 |
--- /dev/null |
+++ b/third_party/WebKit/Source/modules/indexeddb/IDBValueWrapper.cpp |
@@ -0,0 +1,104 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "modules/indexeddb/IDBValueWrapper.h" |
+ |
+#include "bindings/core/v8/ScriptValue.h" |
+#include "bindings/core/v8/SerializationTag.h" |
+#include "bindings/core/v8/SerializedScriptValue.h" |
+#include "bindings/modules/v8/V8BindingForModules.h" |
+#include "core/fileapi/Blob.h" |
+#include "modules/indexeddb/IDBRequest.h" |
+#include "platform/wtf/text/WTFString.h" |
+ |
+namespace blink { |
+ |
+IDBValueWrapper::IDBValueWrapper(v8::Isolate* isolate, |
+ v8::Local<v8::Value> value, |
+ bool write_wasm_to_stream, |
+ ExceptionState& exception_state) { |
+ SerializedScriptValue::SerializeOptions options; |
+ options.blob_info = &blob_info_; |
+ options.write_wasm_to_stream = write_wasm_to_stream; |
+ |
+ serialized_value_ = SerializedScriptValue::Serialize(isolate, value, options, |
+ exception_state); |
+#if DCHECK_IS_ON() |
+ if (exception_state.HadException()) |
+ had_exception_ = true; |
+#endif // DCHECK_IS_ON() |
+} |
+ |
+void IDBValueWrapper::Clone(ScriptState* script_state, ScriptValue* clone) { |
+ DCHECK(!had_exception_) |
+ << "Clone() called on wrapper with serialization exception"; |
+ DCHECK(!wrap_called_) << "Clone() called after WrapIfBiggerThan()"; |
+ *clone = DeserializeScriptValue(script_state, serialized_value_.Get(), |
+ &blob_info_); |
+} |
+ |
+void IDBValueWrapper::WriteVarint(unsigned value, Vector<char>& output) { |
+ // Writes an unsigned integer as a base-128 varint. |
+ // The number is written, 7 bits at a time, from the least significant to |
+ // the most significant 7 bits. Each byte, except the last, has the MSB set. |
+ // See also https://developers.google.com/protocol-buffers/docs/encoding |
+ do { |
+ output.push_back((value & 0x7F) | 0x80); |
+ value >>= 7; |
+ } while (value); |
+ output.back() &= 0x7F; |
+} |
+ |
+void IDBValueWrapper::WriteAsciiString(const String& value, |
+ Vector<char>& output) { |
+ DCHECK(value.Is8Bit() && value.ContainsOnlyASCII()); |
+ |
+ IDBValueWrapper::WriteVarint(value.length(), output); |
+ output.Append(value.Characters8(), value.length()); |
+} |
+ |
+bool IDBValueWrapper::WrapIfBiggerThan(unsigned max_bytes) { |
+ DCHECK(!had_exception_) |
+ << "WrapIfBiggerThan() called on wrapper with serialization exception"; |
+ DCHECK(!wrap_called_) |
+ << "WrapIfBiggerThan() called twice on the same wrapper"; |
+ |
+#if DCHECK_IS_ON() |
+ wrap_called_ = true; |
+#endif // DCHECK_IS_ON() |
+ |
+ serialized_value_->ToWireBytes(wire_bytes_); |
+ if (wire_bytes_.size() <= max_bytes) |
+ return false; |
+ |
+ // TODO(pwnall): The MIME type should probably be an atomic string. |
+ String mime_type(kWrapMimeType); |
+ Blob* wrapper = |
dmurph
2017/04/13 18:05:00
Ok so this is how we'll deal with the lifetime. Th
pwnall
2017/05/11 23:54:23
You implemented a solution for keeping Blobs alive
|
+ Blob::Create(reinterpret_cast<unsigned char*>(wire_bytes_.Data()), |
+ wire_bytes_.size(), mime_type); |
+ |
+ // TODO(pwnall): Do we need to stash the blob somewhere else to keep it alive? |
+ blob_info_.emplace_back(wrapper->Uuid(), wrapper->type(), wrapper->size()); |
+ |
+ wire_bytes_.Clear(); |
+ |
+ // Version 17 of SSV always writes a V8 envelope after the Blink envelope, so |
+ // its output starts with 0xFF 0x11 0xFF. Therefore, we can use 0xFF 0x11 0xvv |
+ // as an escape prefix, for 0x00 <= 0xvv < 0xFF. |
+ wire_bytes_.push_back(kVersionTag); |
+ wire_bytes_.push_back(17); |
+ wire_bytes_.push_back(1); |
+ IDBValueWrapper::WriteVarint(wrapper->size(), wire_bytes_); |
+ IDBValueWrapper::WriteAsciiString(wrapper->Uuid(), wire_bytes_); |
+ return true; |
+} |
+ |
+PassRefPtr<SharedBuffer> IDBValueWrapper::ExtractWireBytes() { |
+ DCHECK(!had_exception_) |
+ << "ExtractWireBytes() called on wrapper with serialization exception"; |
+ |
+ return SharedBuffer::AdoptVector(wire_bytes_); |
+} |
+ |
+} // namespace blink |