• 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 #include "mojo/public/cpp/base/values_mojom_traits.h"
6 
7 #include <memory>
8 #include <utility>
9 
10 #include "base/strings/string_piece.h"
11 
12 namespace mojo {
13 
Read(mojo_base::mojom::DictionaryValueDataView data,base::Value * value_out)14 bool StructTraits<mojo_base::mojom::DictionaryValueDataView, base::Value>::Read(
15     mojo_base::mojom::DictionaryValueDataView data,
16     base::Value* value_out) {
17   mojo::MapDataView<mojo::StringDataView, mojo_base::mojom::ValueDataView> view;
18   data.GetStorageDataView(&view);
19   std::vector<base::Value::DictStorage::value_type> dict_storage;
20   dict_storage.reserve(view.size());
21   for (size_t i = 0; i < view.size(); ++i) {
22     base::StringPiece key;
23     auto value = std::make_unique<base::Value>();
24     if (!view.keys().Read(i, &key) || !view.values().Read(i, value.get()))
25       return false;
26     dict_storage.emplace_back(key.as_string(), std::move(value));
27   }
28   *value_out = base::Value(base::Value::DictStorage(std::move(dict_storage),
29                                                     base::KEEP_LAST_OF_DUPES));
30   return true;
31 }
32 
Read(mojo_base::mojom::ListValueDataView data,base::Value * value_out)33 bool StructTraits<mojo_base::mojom::ListValueDataView, base::Value>::Read(
34     mojo_base::mojom::ListValueDataView data,
35     base::Value* value_out) {
36   mojo::ArrayDataView<mojo_base::mojom::ValueDataView> view;
37   data.GetStorageDataView(&view);
38   base::Value::ListStorage list_storage(view.size());
39   for (size_t i = 0; i < view.size(); ++i) {
40     if (!view.Read(i, &list_storage[i]))
41       return false;
42   }
43   *value_out = base::Value(std::move(list_storage));
44   return true;
45 }
46 
Read(mojo_base::mojom::ValueDataView data,base::Value * value_out)47 bool UnionTraits<mojo_base::mojom::ValueDataView, base::Value>::Read(
48     mojo_base::mojom::ValueDataView data,
49     base::Value* value_out) {
50   switch (data.tag()) {
51     case mojo_base::mojom::ValueDataView::Tag::NULL_VALUE: {
52       *value_out = base::Value();
53       return true;
54     }
55     case mojo_base::mojom::ValueDataView::Tag::BOOL_VALUE: {
56       *value_out = base::Value(data.bool_value());
57       return true;
58     }
59     case mojo_base::mojom::ValueDataView::Tag::INT_VALUE: {
60       *value_out = base::Value(data.int_value());
61       return true;
62     }
63     case mojo_base::mojom::ValueDataView::Tag::DOUBLE_VALUE: {
64       *value_out = base::Value(data.double_value());
65       return true;
66     }
67     case mojo_base::mojom::ValueDataView::Tag::STRING_VALUE: {
68       base::StringPiece string_piece;
69       if (!data.ReadStringValue(&string_piece))
70         return false;
71       *value_out = base::Value(string_piece);
72       return true;
73     }
74     case mojo_base::mojom::ValueDataView::Tag::BINARY_VALUE: {
75       mojo::ArrayDataView<uint8_t> binary_data_view;
76       data.GetBinaryValueDataView(&binary_data_view);
77       const char* data_pointer =
78           reinterpret_cast<const char*>(binary_data_view.data());
79       base::Value::BlobStorage blob_storage(
80           data_pointer, data_pointer + binary_data_view.size());
81       *value_out = base::Value(std::move(blob_storage));
82       return true;
83     }
84     case mojo_base::mojom::ValueDataView::Tag::DICTIONARY_VALUE: {
85       return data.ReadDictionaryValue(value_out);
86     }
87     case mojo_base::mojom::ValueDataView::Tag::LIST_VALUE: {
88       return data.ReadListValue(value_out);
89     }
90   }
91   return false;
92 }
93 
94 }  // namespace mojo
95