Commit 1667e0cf authored by Chase Phillips's avatar Chase Phillips Committed by Commit Bot

IndexedDB: Add Mojom typemap for IDBValue

Bug: 717812
Change-Id: I33dd0fda6b109fbc822e784b3aa490cedef401ca
Reviewed-on: https://chromium-review.googlesource.com/c/1364302
Commit-Queue: Chase Phillips <cmp@chromium.org>
Reviewed-by: default avatarTom Sepez <tsepez@chromium.org>
Reviewed-by: default avatarDaniel Murphy <dmurph@chromium.org>
Cr-Commit-Position: refs/heads/master@{#616102}
parent 9bbb0ffe
include_rules = [
"+mojo/public/cpp/base/file_path_mojom_traits.h",
"+mojo/public/cpp/base/string16_mojom_traits.h",
"+mojo/public/cpp/base/time_mojom_traits.h",
"-third_party/blink/renderer/modules",
"+third_party/blink/renderer/modules/event_modules.h",
"+third_party/blink/renderer/modules/event_target_modules.h",
......
......@@ -9,12 +9,12 @@ public_headers = [
"//third_party/blink/renderer/modules/indexeddb/web_idb_name_and_version.h",
"//third_party/blink/renderer/modules/indexeddb/idb_key_range.h",
"//third_party/blink/renderer/modules/indexeddb/idb_metadata.h",
"//third_party/blink/renderer/modules/indexeddb/idb_value.h",
]
traits_headers = [
"//mojo/public/cpp/base/string16_mojom_traits.h",
"//mojo/public/cpp/bindings/array_traits_web_vector.h",
"//mojo/public/cpp/bindings/array_traits_wtf_vector.h",
"//third_party/blink/public/common/indexeddb/indexed_db_default_mojom_traits.h",
"//third_party/blink/renderer/modules/indexeddb/indexed_db_blink_mojom_traits.h",
"//third_party/blink/renderer/platform/mojo/string16_mojom_traits.h",
]
......@@ -29,4 +29,5 @@ type_mappings = [
"blink.mojom.IDBKey=std::unique_ptr<::blink::IDBKey>[move_only]",
"blink.mojom.IDBKeyPath=::blink::IDBKeyPath",
"blink.mojom.IDBObjectStoreMetadata=scoped_refptr<::blink::IDBObjectStoreMetadata>",
"blink.mojom.IDBValue=std::unique_ptr<::blink::IDBValue>[move_only]",
]
......@@ -7,10 +7,9 @@
#include "base/stl_util.h"
#include "mojo/public/cpp/bindings/array_traits_web_vector.h"
#include "mojo/public/cpp/bindings/array_traits_wtf_vector.h"
#include "third_party/blink/public/common/indexeddb/indexed_db_default_mojom_traits.h"
#include "third_party/blink/public/common/indexeddb/indexeddb_key.h"
#include "third_party/blink/public/common/indexeddb/indexeddb_key_range.h"
#include "third_party/blink/public/platform/web_data.h"
#include "third_party/blink/public/platform/file_path_conversion.h"
#include "third_party/blink/public/platform/web_blob_info.h"
#include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/renderer/modules/indexeddb/idb_key_range.h"
#include "third_party/blink/renderer/platform/mojo/string16_mojom_traits.h"
#include "third_party/blink/renderer/platform/wtf/std_lib_extras.h"
......@@ -181,6 +180,87 @@ bool StructTraits<
return data.ReadData(out);
}
// static
Vector<uint8_t>
StructTraits<blink::mojom::IDBValueDataView, std::unique_ptr<blink::IDBValue>>::
bits(const std::unique_ptr<blink::IDBValue>& input) {
return input->Data()->CopyAs<Vector<uint8_t>>();
}
// static
Vector<blink::mojom::blink::IDBBlobInfoPtr>
StructTraits<blink::mojom::IDBValueDataView, std::unique_ptr<blink::IDBValue>>::
blob_or_file_info(const std::unique_ptr<blink::IDBValue>& input) {
Vector<blink::mojom::blink::IDBBlobInfoPtr> blob_or_file_info;
blob_or_file_info.ReserveInitialCapacity(input->BlobInfo().size());
for (const blink::WebBlobInfo& info : input->BlobInfo()) {
auto blob_info = blink::mojom::blink::IDBBlobInfo::New();
if (info.IsFile()) {
blob_info->file = blink::mojom::blink::IDBFileInfo::New();
blob_info->file->path = blink::WebStringToFilePath(info.FilePath());
String name = info.FileName();
if (name.IsNull())
name = g_empty_string;
blob_info->file->name = name;
blob_info->file->last_modified =
base::Time::FromDoubleT(info.LastModified());
}
blob_info->size = info.size();
blob_info->uuid = info.Uuid();
DCHECK(!blob_info->uuid.IsEmpty());
String mime_type = info.GetType();
if (mime_type.IsNull())
mime_type = g_empty_string;
blob_info->mime_type = mime_type;
blob_info->blob = blink::mojom::blink::BlobPtrInfo(
info.CloneBlobHandle(), blink::mojom::blink::Blob::Version_);
blob_or_file_info.push_back(std::move(blob_info));
}
return blob_or_file_info;
}
// static
bool StructTraits<blink::mojom::IDBValueDataView,
std::unique_ptr<blink::IDBValue>>::
Read(blink::mojom::IDBValueDataView data,
std::unique_ptr<blink::IDBValue>* out) {
Vector<uint8_t> value_bits;
if (!data.ReadBits(&value_bits))
return false;
if (value_bits.IsEmpty()) {
*out = blink::IDBValue::Create(scoped_refptr<blink::SharedBuffer>(),
blink::WebVector<blink::WebBlobInfo>());
return true;
}
scoped_refptr<blink::SharedBuffer> value_buffer = blink::SharedBuffer::Create(
reinterpret_cast<const char*>(value_bits.data()), value_bits.size());
Vector<blink::mojom::blink::IDBBlobInfoPtr> blob_or_file_info;
if (!data.ReadBlobOrFileInfo(&blob_or_file_info))
return false;
blink::WebVector<blink::WebBlobInfo> value_blob_info;
value_blob_info.reserve(blob_or_file_info.size());
for (const auto& info : blob_or_file_info) {
if (info->file) {
value_blob_info.emplace_back(info->uuid,
blink::FilePathToWebString(info->file->path),
info->file->name, info->mime_type,
info->file->last_modified.ToDoubleT(),
info->size, info->blob.PassHandle());
} else {
value_blob_info.emplace_back(info->uuid, info->mime_type, info->size,
info->blob.PassHandle());
}
}
*out = blink::IDBValue::Create(std::move(value_buffer),
std::move(value_blob_info));
return true;
}
// static
blink::mojom::blink::IDBKeyPathDataPtr
StructTraits<blink::mojom::IDBKeyPathDataView, blink::IDBKeyPath>::data(
......
......@@ -8,9 +8,6 @@
#include <stdint.h>
#include "mojo/public/cpp/bindings/map_traits_wtf_hash_map.h"
#include "third_party/blink/public/common/indexeddb/indexeddb_key.h"
#include "third_party/blink/public/common/indexeddb/indexeddb_key_range.h"
#include "third_party/blink/public/common/indexeddb/indexeddb_metadata.h"
#include "third_party/blink/public/mojom/indexeddb/indexeddb.mojom-blink.h"
#include "third_party/blink/public/platform/web_vector.h"
#include "third_party/blink/renderer/modules/indexeddb/idb_metadata.h"
......@@ -125,6 +122,16 @@ struct MODULES_EXPORT
std::unique_ptr<blink::IDBKey>* out);
};
template <>
struct MODULES_EXPORT StructTraits<blink::mojom::IDBValueDataView,
std::unique_ptr<blink::IDBValue>> {
static Vector<uint8_t> bits(const std::unique_ptr<blink::IDBValue>& input);
static Vector<blink::mojom::blink::IDBBlobInfoPtr> blob_or_file_info(
const std::unique_ptr<blink::IDBValue>& input);
static bool Read(blink::mojom::IDBValueDataView data,
std::unique_ptr<blink::IDBValue>* out);
};
template <>
struct MODULES_EXPORT
StructTraits<blink::mojom::IDBKeyPathDataView, blink::IDBKeyPath> {
......
......@@ -8,9 +8,14 @@
#include "base/macros.h"
#include "base/memory/scoped_refptr.h"
#include "mojo/public/cpp/base/file_path_mojom_traits.h"
#include "mojo/public/cpp/base/string16_mojom_traits.h"
#include "mojo/public/cpp/base/time_mojom_traits.h"
#include "mojo/public/cpp/bindings/array_traits_wtf_vector.h"
#include "mojo/public/cpp/bindings/string_traits_wtf.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/modules/indexeddb/idb_key.h"
#include "third_party/blink/renderer/modules/indexeddb/idb_value.h"
#include "third_party/blink/renderer/platform/mojo/string16_mojom_traits.h"
namespace blink {
......@@ -44,4 +49,35 @@ TEST(IDBMojomTraitsTest, IDBKeyBinary) {
ASSERT_EQ(input_vector, output_vector);
}
TEST(IDBMojomTraitsTest, IDBValue) {
// Generate test data.
std::mt19937 rng(5);
size_t test_data_size = 10000;
Vector<char> test_data(test_data_size);
std::generate(test_data.begin(), test_data.end(), rng);
// Create IDBValue mojom message.
scoped_refptr<SharedBuffer> input_data =
SharedBuffer::Create(test_data.data(), test_data.size());
std::unique_ptr<IDBValue> input =
IDBValue::Create(input_data, WebVector<WebBlobInfo>());
Vector<uint8_t> input_vector = input_data->CopyAs<Vector<uint8_t>>();
mojo::Message mojo_message =
mojom::blink::IDBValue::SerializeAsMessage(&input);
// Deserialize the mojo message.
std::unique_ptr<IDBValue> output;
ASSERT_TRUE(mojom::blink::IDBValue::DeserializeFromMessage(
std::move(mojo_message), &output));
scoped_refptr<SharedBuffer> output_data = output->Data();
Vector<uint8_t> output_vector = output_data->CopyAs<Vector<uint8_t>>();
// Verify expectations.
ASSERT_EQ(input_data->size(), test_data_size);
ASSERT_EQ(input_vector.size(), test_data_size);
ASSERT_EQ(output_data->size(), test_data_size);
ASSERT_EQ(output_vector.size(), test_data_size);
ASSERT_EQ(input_vector, output_vector);
}
} // namespace blink
......@@ -13,7 +13,6 @@
#include "third_party/blink/renderer/modules/indexeddb/web_idb_database_impl.h"
#include "third_party/blink/renderer/modules/indexeddb/web_idb_name_and_version.h"
using blink::IndexedDBDatabaseMetadata;
using blink::WebBlobInfo;
using blink::WebIDBCallbacks;
using blink::WebIDBDatabase;
......@@ -33,8 +32,7 @@ std::unique_ptr<IDBValue> ConvertReturnValue(
WebVector<WebBlobInfo>());
}
std::unique_ptr<IDBValue> output =
IndexedDBCallbacksImpl::ConvertValue(input->value);
std::unique_ptr<IDBValue> output = std::move(input->value);
output->SetInjectedPrimaryKey(std::move(input->primary_key), input->key_path);
return output;
}
......@@ -47,38 +45,6 @@ WebIDBNameAndVersion ConvertNameVersion(
} // namespace
// static
std::unique_ptr<IDBValue> IndexedDBCallbacksImpl::ConvertValue(
const mojom::blink::IDBValuePtr& input) {
if (!input || input->bits.IsEmpty()) {
return IDBValue::Create(scoped_refptr<SharedBuffer>(),
WebVector<WebBlobInfo>());
}
WebVector<WebBlobInfo> local_blob_info;
local_blob_info.reserve(input->blob_or_file_info.size());
for (const auto& info : input->blob_or_file_info) {
if (info->file) {
local_blob_info.emplace_back(
WebString(info->uuid), FilePathToWebString(info->file->path),
WebString(info->file->name), WebString(info->mime_type),
info->file->last_modified.ToDoubleT(), info->size,
info->blob.PassHandle());
} else {
local_blob_info.emplace_back(WebString(info->uuid),
WebString(info->mime_type), info->size,
info->blob.PassHandle());
}
}
// TODO(crbug.com/902498): Use mojom traits to map directly to WebData.
scoped_refptr<blink::SharedBuffer> value_buffer = blink::SharedBuffer::Create(
reinterpret_cast<const char*>(input->bits.data()), input->bits.size());
// Release input->bits std::vector.
input->bits.clear();
return IDBValue::Create(std::move(value_buffer), std::move(local_blob_info));
}
IndexedDBCallbacksImpl::IndexedDBCallbacksImpl(
std::unique_ptr<WebIDBCallbacks> callbacks,
int64_t transaction_id,
......@@ -148,11 +114,19 @@ void IndexedDBCallbacksImpl::SuccessCursor(
mojom::blink::IDBCursorAssociatedPtrInfo cursor_info,
std::unique_ptr<IDBKey> key,
std::unique_ptr<IDBKey> primary_key,
mojom::blink::IDBValuePtr value) {
base::Optional<std::unique_ptr<IDBValue>> optional_value) {
WebIDBCursorImpl* cursor =
new WebIDBCursorImpl(std::move(cursor_info), transaction_id_);
std::unique_ptr<IDBValue> value;
if (optional_value.has_value()) {
value = std::move(optional_value.value());
} else {
value = IDBValue::Create(scoped_refptr<SharedBuffer>(),
WebVector<WebBlobInfo>());
}
DCHECK(value);
callbacks_->OnSuccess(cursor, std::move(key), std::move(primary_key),
ConvertValue(value));
std::move(value));
callbacks_.reset();
}
......@@ -165,24 +139,27 @@ void IndexedDBCallbacksImpl::SuccessValue(
void IndexedDBCallbacksImpl::SuccessCursorContinue(
std::unique_ptr<IDBKey> key,
std::unique_ptr<IDBKey> primary_key,
mojom::blink::IDBValuePtr value) {
base::Optional<std::unique_ptr<IDBValue>> optional_value) {
std::unique_ptr<IDBValue> value;
if (optional_value.has_value()) {
value = std::move(optional_value.value());
} else {
value = IDBValue::Create(scoped_refptr<SharedBuffer>(),
WebVector<WebBlobInfo>());
}
DCHECK(value);
callbacks_->OnSuccess(std::move(key), std::move(primary_key),
ConvertValue(value));
std::move(value));
callbacks_.reset();
}
void IndexedDBCallbacksImpl::SuccessCursorPrefetch(
Vector<std::unique_ptr<IDBKey>> keys,
Vector<std::unique_ptr<IDBKey>> primary_keys,
Vector<mojom::blink::IDBValuePtr> values) {
Vector<std::unique_ptr<IDBValue>> idb_values;
idb_values.ReserveInitialCapacity(values.size());
for (const mojom::blink::IDBValuePtr& value : values)
idb_values.emplace_back(ConvertValue(value));
Vector<std::unique_ptr<IDBValue>> values) {
if (cursor_) {
cursor_->SetPrefetchData(std::move(keys), std::move(primary_keys),
std::move(idb_values));
std::move(values));
cursor_->CachedContinue(callbacks_.get());
}
callbacks_.reset();
......
......@@ -12,8 +12,6 @@
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "mojo/public/cpp/bindings/associated_binding.h"
#include "third_party/blink/public/common/indexeddb/indexeddb_key.h"
#include "third_party/blink/public/common/indexeddb/indexeddb_metadata.h"
#include "third_party/blink/public/mojom/indexeddb/indexeddb.mojom-blink.h"
#include "third_party/blink/renderer/modules/indexeddb/idb_value.h"
......@@ -32,9 +30,6 @@ class IndexedDBCallbacksImpl : public mojom::blink::IDBCallbacks {
// cases.
enum : int64_t { kNoTransaction = -1 };
static std::unique_ptr<IDBValue> ConvertValue(
const mojom::blink::IDBValuePtr& input);
IndexedDBCallbacksImpl(std::unique_ptr<WebIDBCallbacks> callbacks,
int64_t transaction_id,
const base::WeakPtr<WebIDBCursorImpl>& cursor);
......@@ -56,14 +51,15 @@ class IndexedDBCallbacksImpl : public mojom::blink::IDBCallbacks {
void SuccessCursor(mojom::blink::IDBCursorAssociatedPtrInfo cursor,
std::unique_ptr<IDBKey> key,
std::unique_ptr<IDBKey> primary_key,
mojom::blink::IDBValuePtr value) override;
base::Optional<std::unique_ptr<IDBValue>> value) override;
void SuccessValue(mojom::blink::IDBReturnValuePtr value) override;
void SuccessCursorContinue(std::unique_ptr<IDBKey> key,
std::unique_ptr<IDBKey> primary_key,
mojom::blink::IDBValuePtr value) override;
void SuccessCursorContinue(
std::unique_ptr<IDBKey> key,
std::unique_ptr<IDBKey> primary_key,
base::Optional<std::unique_ptr<IDBValue>> value) override;
void SuccessCursorPrefetch(Vector<std::unique_ptr<IDBKey>> keys,
Vector<std::unique_ptr<IDBKey>> primary_keys,
Vector<mojom::blink::IDBValuePtr> values) override;
Vector<std::unique_ptr<IDBValue>> values) override;
void SuccessArray(Vector<mojom::blink::IDBReturnValuePtr> values) override;
void SuccessKey(std::unique_ptr<IDBKey> key) override;
void SuccessInteger(int64_t value) override;
......
......@@ -52,9 +52,16 @@ void IndexedDBDatabaseCallbacksImpl::Changes(
web_observations.reserve(changes->observations.size());
for (const auto& observation : changes->observations) {
IDBKeyRange* key_range = observation->key_range.To<IDBKeyRange*>();
web_observations.emplace_back(
observation->object_store_id, observation->type, key_range,
IndexedDBCallbacksImpl::ConvertValue(observation->value));
std::unique_ptr<IDBValue> value;
if (observation->value.has_value())
value = std::move(observation->value.value());
if (!value || value->Data()->IsEmpty()) {
value = IDBValue::Create(scoped_refptr<SharedBuffer>(),
WebVector<WebBlobInfo>());
}
web_observations.emplace_back(observation->object_store_id,
observation->type, key_range,
std::move(value));
}
std::unordered_map<int32_t, WebVector<int32_t>> observation_index_map;
......
......@@ -10,7 +10,6 @@
#include <vector>
#include "base/gtest_prod_util.h"
#include "third_party/blink/public/common/indexeddb/indexeddb_key.h"
#include "third_party/blink/public/mojom/indexeddb/indexeddb.mojom-blink.h"
#include "third_party/blink/renderer/modules/indexeddb/idb_value.h"
#include "third_party/blink/renderer/modules/indexeddb/web_idb_callbacks.h"
......
......@@ -12,8 +12,6 @@
#include "base/format_macros.h"
#include "base/memory/ptr_util.h"
#include "mojo/public/cpp/bindings/strong_associated_binding.h"
#include "third_party/blink/public/common/indexeddb/indexeddb_key.h"
#include "third_party/blink/public/platform/file_path_conversion.h"
#include "third_party/blink/public/platform/modules/indexeddb/web_idb_database_exception.h"
#include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/public/platform/web_vector.h"
......@@ -131,36 +129,8 @@ void WebIDBDatabaseImpl::Put(long long transaction_id,
Vector<IDBIndexKeys> index_keys) {
IndexedDBDispatcher::ResetCursorPrefetchCaches(transaction_id, nullptr);
auto mojo_value = mojom::blink::IDBValue::New();
// mojo_value->bits initialization.
mojo_value->bits = value->CopyAs<Vector<uint8_t>>();
// mojo_value->blob_or_file_info initialization.
mojo_value->blob_or_file_info.ReserveInitialCapacity(web_blob_info.size());
for (const WebBlobInfo& info : web_blob_info) {
auto blob_info = mojom::blink::IDBBlobInfo::New();
if (info.IsFile()) {
blob_info->file = mojom::blink::IDBFileInfo::New();
blob_info->file->path = WebStringToFilePath(info.FilePath());
String name = info.FileName();
if (name.IsNull())
name = g_empty_string;
blob_info->file->name = name;
blob_info->file->last_modified =
base::Time::FromDoubleT(info.LastModified());
}
blob_info->size = info.size();
blob_info->uuid = info.Uuid();
DCHECK(!blob_info->uuid.IsEmpty());
String mime_type = info.GetType();
if (mime_type.IsNull())
mime_type = g_empty_string;
blob_info->mime_type = mime_type;
blob_info->blob = mojom::blink::BlobPtrInfo(info.CloneBlobHandle(),
mojom::blink::Blob::Version_);
mojo_value->blob_or_file_info.push_back(std::move(blob_info));
}
std::unique_ptr<IDBValue> idb_value =
IDBValue::Create(std::move(value), std::move(web_blob_info));
size_t index_keys_size = 0;
for (const auto& index_key : index_keys) {
......@@ -171,7 +141,7 @@ void WebIDBDatabaseImpl::Put(long long transaction_id,
}
size_t arg_size =
mojo_value->bits.size() + primary_key->SizeEstimate() + index_keys_size;
value->size() + primary_key->SizeEstimate() + index_keys_size;
if (arg_size >= max_put_value_size_) {
callbacks->OnError(blink::WebIDBDatabaseError(
blink::kWebIDBDatabaseExceptionUnknownError,
......@@ -184,7 +154,7 @@ void WebIDBDatabaseImpl::Put(long long transaction_id,
auto callbacks_impl = std::make_unique<IndexedDBCallbacksImpl>(
base::WrapUnique(callbacks), transaction_id, nullptr);
database_->Put(transaction_id, object_store_id, std::move(mojo_value),
database_->Put(transaction_id, object_store_id, std::move(idb_value),
std::move(primary_key), put_mode, std::move(index_keys),
GetCallbacksProxy(std::move(callbacks_impl)));
}
......
......@@ -9,7 +9,6 @@
#include "base/macros.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/indexeddb/indexeddb_key.h"
#include "third_party/blink/public/platform/scheduler/test/renderer_scheduler_test_support.h"
#include "third_party/blink/public/platform/web_blob_info.h"
#include "third_party/blink/public/web/web_heap.h"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment