// Copyright 2016 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 "mojo/common/values_struct_traits.h" #include #include #include #include "base/containers/flat_map.h" #include "base/memory/ptr_util.h" namespace mojo { bool StructTraits>:: Read(common::mojom::ListValueDataView data, std::unique_ptr* value_out) { mojo::ArrayDataView view; data.GetValuesDataView(&view); auto list_value = std::make_unique(); for (size_t i = 0; i < view.size(); ++i) { std::unique_ptr value; if (!view.Read(i, &value)) return false; list_value->Append(std::move(value)); } *value_out = std::move(list_value); return true; } std::unique_ptr CloneTraits, false>::Clone( const std::unique_ptr& input) { return input ? input->CreateDeepCopy() : nullptr; } bool StructTraits>:: Read(common::mojom::DictionaryValueDataView data, std::unique_ptr* value_out) { mojo::MapDataView view; data.GetValuesDataView(&view); std::vector dict_storage; dict_storage.reserve(view.size()); for (size_t i = 0; i < view.size(); ++i) { base::StringPiece key; std::unique_ptr value; if (!view.keys().Read(i, &key) || !view.values().Read(i, &value)) return false; dict_storage.emplace_back(key.as_string(), std::move(value)); } *value_out = base::DictionaryValue::From( std::make_unique(base::Value::DictStorage( std::move(dict_storage), base::KEEP_LAST_OF_DUPES))); return true; } std::unique_ptr CloneTraits, false>::Clone( const std::unique_ptr& input) { return input ? input->CreateDeepCopy() : nullptr; } bool UnionTraits>:: Read(common::mojom::ValueDataView data, std::unique_ptr* value_out) { switch (data.tag()) { case common::mojom::ValueDataView::Tag::NULL_VALUE: { *value_out = std::make_unique(); return true; } case common::mojom::ValueDataView::Tag::BOOL_VALUE: { *value_out = std::make_unique(data.bool_value()); return true; } case common::mojom::ValueDataView::Tag::INT_VALUE: { *value_out = std::make_unique(data.int_value()); return true; } case common::mojom::ValueDataView::Tag::DOUBLE_VALUE: { *value_out = std::make_unique(data.double_value()); return true; } case common::mojom::ValueDataView::Tag::STRING_VALUE: { base::StringPiece string_value; if (!data.ReadStringValue(&string_value)) return false; *value_out = std::make_unique(string_value); return true; } case common::mojom::ValueDataView::Tag::BINARY_VALUE: { mojo::ArrayDataView binary_data; data.GetBinaryValueDataView(&binary_data); *value_out = base::Value::CreateWithCopiedBuffer( reinterpret_cast(binary_data.data()), binary_data.size()); return true; } case common::mojom::ValueDataView::Tag::DICTIONARY_VALUE: { std::unique_ptr dictionary_value; if (!data.ReadDictionaryValue(&dictionary_value)) return false; *value_out = std::move(dictionary_value); return true; } case common::mojom::ValueDataView::Tag::LIST_VALUE: { std::unique_ptr list_value; if (!data.ReadListValue(&list_value)) return false; *value_out = std::move(list_value); return true; } } return false; } std::unique_ptr CloneTraits, false>::Clone( const std::unique_ptr& input) { return input ? input->CreateDeepCopy() : nullptr; } } // namespace mojo