• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 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 #ifndef MOJO_PUBLIC_CPP_BASE_VALUES_MOJOM_TRAITS_H_
6 #define MOJO_PUBLIC_CPP_BASE_VALUES_MOJOM_TRAITS_H_
7 
8 #include <vector>
9 
10 #include "base/component_export.h"
11 #include "base/values.h"
12 #include "mojo/public/cpp/bindings/map_traits.h"
13 #include "mojo/public/cpp/bindings/struct_traits.h"
14 #include "mojo/public/cpp/bindings/union_traits.h"
15 #include "mojo/public/mojom/base/values.mojom-shared.h"
16 
17 namespace mojo {
18 
19 template <>
20 struct MapTraits<base::Value> {
21   using Key = std::string;
22   using Value = base::Value;
23   using Iterator = base::Value::const_dict_iterator_proxy::const_iterator;
24 
25   static size_t GetSize(const base::Value& input) {
26     DCHECK(input.is_dict());
27     return static_cast<const base::DictionaryValue&>(input).size();
28   }
29 
30   static Iterator GetBegin(const base::Value& input) {
31     DCHECK(input.is_dict());
32     return input.DictItems().cbegin();
33   }
34 
35   static void AdvanceIterator(Iterator& iterator) { ++iterator; }
36 
37   static const Key& GetKey(const Iterator& iterator) { return iterator->first; }
38 
39   static const Value& GetValue(const Iterator& iterator) {
40     return iterator->second;
41   }
42 };
43 
44 template <>
45 struct COMPONENT_EXPORT(MOJO_BASE_SHARED_TRAITS)
46     StructTraits<mojo_base::mojom::DictionaryValueDataView, base::Value> {
47   static const base::Value& storage(const base::Value& value) {
48     DCHECK(value.is_dict());
49     return value;
50   }
51 
52   static bool Read(mojo_base::mojom::DictionaryValueDataView data,
53                    base::Value* value);
54 };
55 
56 template <>
57 struct COMPONENT_EXPORT(MOJO_BASE_SHARED_TRAITS)
58     StructTraits<mojo_base::mojom::ListValueDataView, base::Value> {
59   static const base::Value::ListStorage& storage(const base::Value& value) {
60     DCHECK(value.is_list());
61     return value.GetList();
62   }
63 
64   static bool Read(mojo_base::mojom::ListValueDataView data,
65                    base::Value* value);
66 };
67 
68 template <>
69 struct COMPONENT_EXPORT(MOJO_BASE_SHARED_TRAITS)
70     UnionTraits<mojo_base::mojom::ValueDataView, base::Value> {
71   static mojo_base::mojom::ValueDataView::Tag GetTag(const base::Value& data) {
72     switch (data.type()) {
73       case base::Value::Type::NONE:
74         return mojo_base::mojom::ValueDataView::Tag::NULL_VALUE;
75       case base::Value::Type::BOOLEAN:
76         return mojo_base::mojom::ValueDataView::Tag::BOOL_VALUE;
77       case base::Value::Type::INTEGER:
78         return mojo_base::mojom::ValueDataView::Tag::INT_VALUE;
79       case base::Value::Type::DOUBLE:
80         return mojo_base::mojom::ValueDataView::Tag::DOUBLE_VALUE;
81       case base::Value::Type::STRING:
82         return mojo_base::mojom::ValueDataView::Tag::STRING_VALUE;
83       case base::Value::Type::BINARY:
84         return mojo_base::mojom::ValueDataView::Tag::BINARY_VALUE;
85       case base::Value::Type::DICTIONARY:
86         return mojo_base::mojom::ValueDataView::Tag::DICTIONARY_VALUE;
87       case base::Value::Type::LIST:
88         return mojo_base::mojom::ValueDataView::Tag::LIST_VALUE;
89     }
90     NOTREACHED();
91     return mojo_base::mojom::ValueDataView::Tag::NULL_VALUE;
92   }
93 
94   static uint8_t null_value(const base::Value& value) { return 0; }
95 
96   static bool bool_value(const base::Value& value) { return value.GetBool(); }
97 
98   static int32_t int_value(const base::Value& value) { return value.GetInt(); }
99 
100   static double double_value(const base::Value& value) {
101     return value.GetDouble();
102   }
103 
104   static base::StringPiece string_value(const base::Value& value) {
105     return value.GetString();
106   }
107 
108   static base::span<const uint8_t> binary_value(const base::Value& value) {
109     // TODO(dcheng): Change base::Value::BlobStorage to uint8_t.
110     return base::make_span(
111         reinterpret_cast<const uint8_t*>(value.GetBlob().data()),
112         value.GetBlob().size());
113   }
114 
115   static const base::Value& list_value(const base::Value& value) {
116     DCHECK(value.is_list());
117     return value;
118   }
119   static const base::Value& dictionary_value(const base::Value& value) {
120     DCHECK(value.is_dict());
121     return value;
122   }
123 
124   static bool Read(mojo_base::mojom::ValueDataView view,
125                    base::Value* value_out);
126 };
127 
128 }  // namespace mojo
129 
130 #endif  // MOJO_PUBLIC_CPP_BASE_VALUES_MOJOM_TRAITS_H_
131