OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 "chrome/browser/extensions/api/bluetooth_low_energy/utils.h" | |
6 | |
7 #include <stddef.h> | |
8 #include <iterator> | |
9 #include <vector> | |
10 | |
11 #include "base/logging.h" | |
12 | |
13 namespace extensions { | |
14 namespace api { | |
15 namespace bluetooth_low_energy { | |
16 | |
17 namespace { | |
18 | |
19 // Converts a list of CharacteristicProperty to a base::ListValue of strings. | |
20 std::unique_ptr<base::ListValue> CharacteristicPropertiesToValue( | |
21 const std::vector<CharacteristicProperty> properties) { | |
22 std::unique_ptr<base::ListValue> property_list(new base::ListValue()); | |
23 for (std::vector<CharacteristicProperty>::const_iterator iter = | |
24 properties.begin(); | |
25 iter != properties.end(); | |
26 ++iter) | |
27 property_list->AppendString(ToString(*iter)); | |
28 return property_list; | |
29 } | |
30 | |
31 } // namespace | |
32 | |
33 std::unique_ptr<base::DictionaryValue> CharacteristicToValue( | |
34 Characteristic* from) { | |
35 // Copy the properties. Use Characteristic::ToValue to generate the result | |
36 // dictionary without the properties, to prevent json_schema_compiler from | |
37 // failing. | |
38 std::vector<CharacteristicProperty> properties = from->properties; | |
39 from->properties.clear(); | |
40 std::unique_ptr<base::DictionaryValue> to = from->ToValue(); | |
41 to->SetWithoutPathExpansion( | |
42 "properties", CharacteristicPropertiesToValue(properties).release()); | |
43 return to; | |
44 } | |
45 | |
46 std::unique_ptr<base::DictionaryValue> DescriptorToValue(Descriptor* from) { | |
47 if (!from->characteristic) | |
48 return from->ToValue(); | |
49 | |
50 // Copy the characteristic properties and set them later manually. | |
51 std::vector<CharacteristicProperty> properties = | |
52 from->characteristic->properties; | |
53 from->characteristic->properties.clear(); | |
54 std::unique_ptr<base::DictionaryValue> to = from->ToValue(); | |
55 | |
56 base::DictionaryValue* chrc_value = NULL; | |
57 to->GetDictionaryWithoutPathExpansion("characteristic", &chrc_value); | |
58 DCHECK(chrc_value); | |
59 chrc_value->SetWithoutPathExpansion( | |
60 "properties", CharacteristicPropertiesToValue(properties).release()); | |
61 return to; | |
62 } | |
63 | |
64 } // namespace bluetooth_low_energy | |
65 } // namespace api | |
66 } // namespace extensions | |
OLD | NEW |