Chromium Code Reviews| 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..83f4ea3a2ef162d6fc686a2e900ec38e075fa074 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/modules/indexeddb/IDBValueUnwrapper.cpp |
| @@ -0,0 +1,123 @@ |
| +// 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/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) |
| + 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; |
| +} |
| + |
| +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) { |
|
dmurph
2017/05/04 22:27:07
There must be a library method for this... maybe i
pwnall
2017/05/11 23:54:24
As far as I know, there isn't. Please correct me i
|
| + 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; |
| + |
| + if (end_ - current_ < length) |
| + return false; |
| + String output(current_, length); |
| + value.swap(output); |
| + current_ += length; |
| + return true; |
| +} |
| + |
| +} // namespace blink |