Index: third_party/WebKit/Source/modules/indexeddb/IDBValueUnwrapper.cpp |
diff --git a/third_party/WebKit/Source/modules/indexeddb/IDBValueUnwrapper.cpp b/third_party/WebKit/Source/modules/indexeddb/IDBValueUnwrapper.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..89869cde028fc839d171c7efe401f4ddc41683ba |
--- /dev/null |
+++ b/third_party/WebKit/Source/modules/indexeddb/IDBValueUnwrapper.cpp |
@@ -0,0 +1,124 @@ |
+// 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/IDBValueUnwrapper.h" |
+ |
+#include <memory> |
+#include <utility> |
+ |
+#include "bindings/core/v8/serialization/SerializationTag.h" |
+#include "modules/indexeddb/IDBValue.h" |
+#include "modules/indexeddb/IDBValueWrapper.h" |
+#include "platform/blob/BlobData.h" |
+#include "platform/wtf/text/WTFString.h" |
+ |
+namespace blink { |
+ |
+IDBValueUnwrapper::IDBValueUnwrapper() { |
+ Reset(); |
+} |
+ |
+bool IDBValueUnwrapper::IsWrapped(IDBValue* value) { |
+ DCHECK(value); |
+ if (!value->data_ || value->data_->size() < 3) |
jsbell
2017/05/08 22:08:01
Constants/documentation for these magic numbers.
pwnall
2017/05/11 23:54:25
Done.
|
+ return false; |
+ |
+ uint8_t header[3]; |
+ value->data_->GetPartAsBytes(header, static_cast<size_t>(0), sizeof(header)); |
+ return header[0] == kVersionTag && header[1] == 17 && header[2] == 1; |
jsbell
2017/05/08 22:08:01
Ditto.
pwnall
2017/05/11 23:54:25
Done.
|
+} |
+ |
+PassRefPtr<IDBValue> IDBValueUnwrapper::CreateUnwrapped( |
+ IDBValue* wrapped_value, |
+ PassRefPtr<SharedBuffer> wrapper_blob_content) { |
+ DCHECK(wrapped_value); |
+ DCHECK(wrapped_value->data_); |
+ DCHECK_GT(wrapped_value->blob_info_->size(), 0U); |
+ DCHECK_EQ(wrapped_value->blob_info_->size(), |
+ wrapped_value->blob_data_->size()); |
+ |
+ // Create an IDBValue with the same blob information, minus the last blob. |
+ unsigned blob_count = wrapped_value->BlobInfo()->size() - 1; |
+ std::unique_ptr<Vector<RefPtr<BlobDataHandle>>> blob_data = |
+ WTF::MakeUnique<Vector<RefPtr<BlobDataHandle>>>(); |
+ blob_data->ReserveCapacity(blob_count); |
+ std::unique_ptr<Vector<WebBlobInfo>> blob_info = |
+ WTF::MakeUnique<Vector<WebBlobInfo>>(); |
+ blob_info->ReserveCapacity(blob_count); |
+ |
+ for (unsigned i = 0; i < blob_count; ++i) { |
+ blob_data->push_back((*wrapped_value->blob_data_)[i]); |
+ blob_info->push_back((*wrapped_value->blob_info_)[i]); |
+ } |
+ |
+ return AdoptRef(new IDBValue(std::move(wrapper_blob_content), |
+ std::move(blob_data), std::move(blob_info))); |
+} |
+ |
+bool IDBValueUnwrapper::Parse(IDBValue* value) { |
+ // Fast path that avoids unnecessary dynamic allocations. |
+ if (!IDBValueUnwrapper::IsWrapped(value)) |
+ return false; |
+ |
+ const uint8_t* data = reinterpret_cast<const uint8_t*>(value->data_->Data()); |
+ end_ = data + value->data_->size(); |
+ current_ = data + 3; |
+ |
+ String blob_uuid; |
+ if (!ReadVarint(blob_size_)) |
+ return Reset(); |
+ if (!ReadAsciiString(blob_uuid)) |
+ return Reset(); |
+ |
+ blob_handle_ = value->blob_data_->back(); |
+ |
+ // TODO(pwnall): Blobs seem to get different UUIDs when they're resurrected? |
+ // If this is true, stashing the UUID is useless. |
+ if (blob_handle_->size() != blob_size_) |
+ return Reset(); |
+ |
+ return true; |
+} |
+ |
+PassRefPtr<BlobDataHandle> IDBValueUnwrapper::WrapperBlobHandle() { |
+ DCHECK(blob_handle_); |
+ |
+ return std::move(blob_handle_); |
+} |
+ |
+bool IDBValueUnwrapper::ReadVarint(unsigned& value) { |
+ value = 0; |
+ unsigned shift = 0; |
+ bool has_another_byte; |
+ do { |
+ if (current_ >= end_) |
+ return false; |
+ |
+ if (shift >= sizeof(unsigned) * 8) |
+ return false; |
+ uint8_t byte = *current_; |
+ ++current_; |
+ value |= static_cast<unsigned>(byte & 0x7F) << shift; |
+ shift += 7; |
+ |
+ has_another_byte = byte & 0x80; |
+ } while (has_another_byte); |
+ return true; |
+} |
+ |
+bool IDBValueUnwrapper::ReadAsciiString(String& value) { |
+ unsigned length; |
+ if (!ReadVarint(length)) |
+ return false; |
+ |
+ DCHECK_LE(current_, end_); |
+ if (end_ - current_ < static_cast<ptrdiff_t>(length)) |
+ return false; |
+ String output(current_, length); |
+ value.swap(output); |
+ current_ += length; |
+ return true; |
+} |
+ |
+} // namespace blink |