Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(457)

Side by Side Diff: mojo/common/values_struct_traits.cc

Issue 2577563002: Add struct traits for base::Value. (Closed)
Patch Set: rebase Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "mojo/common/values_struct_traits.h"
6
7 namespace mojo {
8
9 bool StructTraits<common::mojom::ListValueDataView,
10 std::unique_ptr<base::ListValue>>::
11 Read(common::mojom::ListValueDataView data,
12 std::unique_ptr<base::ListValue>* value_out) {
13 mojo::ArrayDataView<common::mojom::ValueDataView> view;
14 data.GetValuesDataView(&view);
15
16 auto list_value = base::MakeUnique<base::ListValue>();
17 for (size_t i = 0; i < view.size(); ++i) {
18 std::unique_ptr<base::Value> value;
19 if (!view.Read(i, &value))
20 return false;
21
22 list_value->Append(std::move(value));
23 }
24 *value_out = std::move(list_value);
25 return true;
26 }
27
28 bool StructTraits<common::mojom::DictionaryValueDataView,
29 std::unique_ptr<base::DictionaryValue>>::
30 Read(common::mojom::DictionaryValueDataView data,
31 std::unique_ptr<base::DictionaryValue>* value_out) {
32 mojo::MapDataView<mojo::StringDataView, common::mojom::ValueDataView> view;
33 data.GetValuesDataView(&view);
34 auto dictionary_value = base::MakeUnique<base::DictionaryValue>();
35 for (size_t i = 0; i < view.size(); ++i) {
36 base::StringPiece key;
37 std::unique_ptr<base::Value> value;
38 if (!view.keys().Read(i, &key) || !view.values().Read(i, &value))
39 return false;
40
41 dictionary_value->Set(key, std::move(value));
42 }
43 *value_out = std::move(dictionary_value);
44 return true;
45 }
46
47 bool UnionTraits<common::mojom::ValueDataView, std::unique_ptr<base::Value>>::
48 Read(common::mojom::ValueDataView data,
49 std::unique_ptr<base::Value>* value_out) {
50 switch (data.tag()) {
51 case common::mojom::ValueDataView::Tag::NULL_VALUE: {
52 *value_out = base::Value::CreateNullValue();
53 return true;
54 }
55 case common::mojom::ValueDataView::Tag::BOOL_VALUE: {
56 *value_out = base::MakeUnique<base::FundamentalValue>(data.bool_value());
57 return true;
58 }
59 case common::mojom::ValueDataView::Tag::INT_VALUE: {
60 *value_out = base::MakeUnique<base::FundamentalValue>(data.int_value());
61 return true;
62 }
63 case common::mojom::ValueDataView::Tag::DOUBLE_VALUE: {
64 *value_out =
65 base::MakeUnique<base::FundamentalValue>(data.double_value());
66 return true;
67 }
68 case common::mojom::ValueDataView::Tag::STRING_VALUE: {
69 base::StringPiece string_value;
70 if (!data.ReadStringValue(&string_value))
71 return false;
72 *value_out = base::MakeUnique<base::StringValue>(string_value);
73 return true;
74 }
75 case common::mojom::ValueDataView::Tag::BINARY_VALUE: {
76 mojo::ArrayDataView<uint8_t> binary_data;
77 data.GetBinaryValueDataView(&binary_data);
78 *value_out = base::BinaryValue::CreateWithCopiedBuffer(
79 reinterpret_cast<const char*>(binary_data.data()),
80 binary_data.size());
81 return true;
82 }
83 case common::mojom::ValueDataView::Tag::DICTIONARY_VALUE: {
84 std::unique_ptr<base::DictionaryValue> dictionary_value;
85 if (!data.ReadDictionaryValue(&dictionary_value))
86 return false;
87 *value_out = std::move(dictionary_value);
88 return true;
89 }
90 case common::mojom::ValueDataView::Tag::LIST_VALUE: {
91 std::unique_ptr<base::ListValue> list_value;
92 if (!data.ReadListValue(&list_value))
93 return false;
94 *value_out = std::move(list_value);
95 return true;
96 }
97 }
98 return false;
99 }
100
101 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/common/values_struct_traits.h ('k') | services/preferences/public/cpp/pref_observer_store.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698