OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/memory/ptr_util.h" | 5 #include "base/memory/ptr_util.h" |
6 #include "mojo/common/values_struct_traits.h" | 6 #include "mojo/common/values_struct_traits.h" |
7 | 7 |
8 namespace mojo { | 8 namespace mojo { |
9 | 9 |
10 bool StructTraits<common::mojom::ListValueDataView, | 10 bool StructTraits<common::mojom::ListValueDataView, base::ListValue>::Read( |
11 std::unique_ptr<base::ListValue>>:: | 11 common::mojom::ListValueDataView data, |
12 Read(common::mojom::ListValueDataView data, | 12 base::ListValue* value_out) { |
13 std::unique_ptr<base::ListValue>* value_out) { | |
14 mojo::ArrayDataView<common::mojom::ValueDataView> view; | 13 mojo::ArrayDataView<common::mojom::ValueDataView> view; |
15 data.GetValuesDataView(&view); | 14 data.GetValuesDataView(&view); |
16 | 15 |
17 auto list_value = base::MakeUnique<base::ListValue>(); | 16 base::ListValue list_value; |
18 for (size_t i = 0; i < view.size(); ++i) { | 17 for (size_t i = 0; i < view.size(); ++i) { |
19 std::unique_ptr<base::Value> value; | 18 auto value = base::MakeUnique<base::Value>(); |
20 if (!view.Read(i, &value)) | 19 if (!view.Read(i, value.get())) |
21 return false; | 20 return false; |
22 | 21 |
23 list_value->Append(std::move(value)); | 22 list_value.Append(std::move(value)); |
24 } | 23 } |
25 *value_out = std::move(list_value); | 24 *value_out = std::move(list_value); |
26 return true; | 25 return true; |
27 } | 26 } |
28 | 27 |
29 bool StructTraits<common::mojom::DictionaryValueDataView, | 28 bool StructTraits< |
30 std::unique_ptr<base::DictionaryValue>>:: | 29 common::mojom::DictionaryValueDataView, |
31 Read(common::mojom::DictionaryValueDataView data, | 30 base::DictionaryValue>::Read(common::mojom::DictionaryValueDataView data, |
32 std::unique_ptr<base::DictionaryValue>* value_out) { | 31 base::DictionaryValue* value_out) { |
33 mojo::MapDataView<mojo::StringDataView, common::mojom::ValueDataView> view; | 32 mojo::MapDataView<mojo::StringDataView, common::mojom::ValueDataView> view; |
34 data.GetValuesDataView(&view); | 33 data.GetValuesDataView(&view); |
35 auto dictionary_value = base::MakeUnique<base::DictionaryValue>(); | 34 base::DictionaryValue dictionary_value; |
36 for (size_t i = 0; i < view.size(); ++i) { | 35 for (size_t i = 0; i < view.size(); ++i) { |
37 base::StringPiece key; | 36 base::StringPiece key; |
38 std::unique_ptr<base::Value> value; | 37 auto value = base::MakeUnique<base::Value>(); |
39 if (!view.keys().Read(i, &key) || !view.values().Read(i, &value)) | 38 if (!view.keys().Read(i, &key) || !view.values().Read(i, value.get())) |
40 return false; | 39 return false; |
41 | 40 |
42 dictionary_value->SetWithoutPathExpansion(key, std::move(value)); | 41 dictionary_value.SetWithoutPathExpansion(key, std::move(value)); |
43 } | 42 } |
44 *value_out = std::move(dictionary_value); | 43 *value_out = std::move(dictionary_value); |
45 return true; | 44 return true; |
46 } | 45 } |
47 | 46 |
48 std::unique_ptr<base::DictionaryValue> | 47 bool UnionTraits<common::mojom::ValueDataView, base::Value>::Read( |
49 CloneTraits<std::unique_ptr<base::DictionaryValue>, false>::Clone( | 48 common::mojom::ValueDataView data, |
50 const std::unique_ptr<base::DictionaryValue>& input) { | 49 base::Value* value_out) { |
51 auto result = base::MakeUnique<base::DictionaryValue>(); | |
52 result->MergeDictionary(input.get()); | |
53 return result; | |
54 } | |
55 | |
56 bool UnionTraits<common::mojom::ValueDataView, std::unique_ptr<base::Value>>:: | |
57 Read(common::mojom::ValueDataView data, | |
58 std::unique_ptr<base::Value>* value_out) { | |
59 switch (data.tag()) { | 50 switch (data.tag()) { |
60 case common::mojom::ValueDataView::Tag::NULL_VALUE: { | 51 case common::mojom::ValueDataView::Tag::NULL_VALUE: { |
61 *value_out = base::Value::CreateNullValue(); | 52 *value_out = *base::Value::CreateNullValue(); |
62 return true; | 53 return true; |
63 } | 54 } |
64 case common::mojom::ValueDataView::Tag::BOOL_VALUE: { | 55 case common::mojom::ValueDataView::Tag::BOOL_VALUE: { |
65 *value_out = base::MakeUnique<base::Value>(data.bool_value()); | 56 *value_out = base::Value(data.bool_value()); |
66 return true; | 57 return true; |
67 } | 58 } |
68 case common::mojom::ValueDataView::Tag::INT_VALUE: { | 59 case common::mojom::ValueDataView::Tag::INT_VALUE: { |
69 *value_out = base::MakeUnique<base::Value>(data.int_value()); | 60 *value_out = base::Value(data.int_value()); |
70 return true; | 61 return true; |
71 } | 62 } |
72 case common::mojom::ValueDataView::Tag::DOUBLE_VALUE: { | 63 case common::mojom::ValueDataView::Tag::DOUBLE_VALUE: { |
73 *value_out = base::MakeUnique<base::Value>(data.double_value()); | 64 *value_out = base::Value(data.double_value()); |
74 return true; | 65 return true; |
75 } | 66 } |
76 case common::mojom::ValueDataView::Tag::STRING_VALUE: { | 67 case common::mojom::ValueDataView::Tag::STRING_VALUE: { |
77 base::StringPiece string_value; | 68 base::StringPiece string_value; |
78 if (!data.ReadStringValue(&string_value)) | 69 if (!data.ReadStringValue(&string_value)) |
79 return false; | 70 return false; |
80 *value_out = base::MakeUnique<base::Value>(string_value); | 71 *value_out = base::Value(string_value); |
81 return true; | 72 return true; |
82 } | 73 } |
83 case common::mojom::ValueDataView::Tag::BINARY_VALUE: { | 74 case common::mojom::ValueDataView::Tag::BINARY_VALUE: { |
84 mojo::ArrayDataView<uint8_t> binary_data; | 75 mojo::ArrayDataView<uint8_t> binary_data; |
85 data.GetBinaryValueDataView(&binary_data); | 76 data.GetBinaryValueDataView(&binary_data); |
86 *value_out = base::BinaryValue::CreateWithCopiedBuffer( | 77 *value_out = base::Value( |
87 reinterpret_cast<const char*>(binary_data.data()), | 78 std::vector<char>(reinterpret_cast<const char*>(binary_data.data()), |
88 binary_data.size()); | 79 reinterpret_cast<const char*>(binary_data.data()) + |
| 80 binary_data.size())); |
89 return true; | 81 return true; |
90 } | 82 } |
91 case common::mojom::ValueDataView::Tag::DICTIONARY_VALUE: { | 83 case common::mojom::ValueDataView::Tag::DICTIONARY_VALUE: { |
92 std::unique_ptr<base::DictionaryValue> dictionary_value; | 84 base::DictionaryValue dictionary_value; |
93 if (!data.ReadDictionaryValue(&dictionary_value)) | 85 if (!data.ReadDictionaryValue(&dictionary_value)) |
94 return false; | 86 return false; |
95 *value_out = std::move(dictionary_value); | 87 |
| 88 *value_out = dictionary_value; |
96 return true; | 89 return true; |
97 } | 90 } |
98 case common::mojom::ValueDataView::Tag::LIST_VALUE: { | 91 case common::mojom::ValueDataView::Tag::LIST_VALUE: { |
99 std::unique_ptr<base::ListValue> list_value; | 92 base::ListValue list_value; |
100 if (!data.ReadListValue(&list_value)) | 93 if (!data.ReadListValue(&list_value)) |
101 return false; | 94 return false; |
102 *value_out = std::move(list_value); | 95 |
| 96 *value_out = list_value; |
103 return true; | 97 return true; |
104 } | 98 } |
105 } | 99 } |
106 return false; | 100 return false; |
107 } | 101 } |
108 | 102 |
109 } // namespace mojo | 103 } // namespace mojo |
OLD | NEW |