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..6f01421ed16ac9452f4b98285ca379160dbd01af |
--- /dev/null |
+++ b/third_party/WebKit/Source/modules/indexeddb/IDBValueWrapper.cpp |
@@ -0,0 +1,120 @@ |
+// 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 <utility> |
+ |
+#include "bindings/core/v8/ScriptValue.h" |
+#include "bindings/core/v8/serialization/SerializationTag.h" |
+#include "bindings/core/v8/serialization/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.for_storage = true; |
+ 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) { |
+#if DCHECK_IS_ON() |
+ DCHECK(!had_exception_) |
+ << "Clone() called on wrapper with serialization exception"; |
+ DCHECK(!wrap_called_) << "Clone() called after WrapIfBiggerThan()"; |
+#endif // DCHECK_IS_ON() |
+ *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) { |
+#if DCHECK_IS_ON() |
+ DCHECK(!had_exception_) |
+ << "WrapIfBiggerThan() called on wrapper with serialization exception"; |
jsbell
2017/05/08 22:08:01
tip: could use __FUNCTION__ to make this more refa
pwnall
2017/05/11 23:54:25
Done.
Thank you very much for the tip!
|
+ DCHECK(!wrap_called_) |
+ << "WrapIfBiggerThan() called twice on the same wrapper"; |
+ 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 = |
+ Blob::Create(reinterpret_cast<unsigned char*>(wire_bytes_.data()), |
+ wire_bytes_.size(), mime_type); |
+ |
+ wrapper_handle_ = std::move(wrapper->GetBlobDataHandle()); |
+ blob_info_.emplace_back(wrapper_handle_->Uuid(), wrapper_handle_->GetType(), |
+ 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; |
+} |
+ |
+void IDBValueWrapper::ExtractBlobDataHandles( |
+ Vector<RefPtr<BlobDataHandle>>* blob_data_handles) { |
+ for (const auto& kvp : serialized_value_->BlobDataHandles()) |
+ blob_data_handles->push_back(kvp.value); |
+ if (wrapper_handle_) |
+ blob_data_handles->push_back(std::move(wrapper_handle_)); |
+} |
+ |
+PassRefPtr<SharedBuffer> IDBValueWrapper::ExtractWireBytes() { |
+#if DCHECK_IS_ON() |
+ DCHECK(!had_exception_) |
+ << "ExtractWireBytes() called on wrapper with serialization exception"; |
+#endif // DCHECK_IS_ON() |
+ |
+ return SharedBuffer::AdoptVector(wire_bytes_); |
+} |
+ |
+} // namespace blink |