OLD | NEW |
---|---|
(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 "components/arc/bluetooth/bluetooth_struct_traits.h" | |
6 | |
7 #include <memory> | |
8 #include <utility> | |
9 | |
10 namespace { | |
11 | |
12 void* SetUpContextForAttributeValue( | |
13 const bluez::BluetoothServiceAttributeValueBlueZ& value) { | |
14 switch (value.type()) { | |
15 case bluez::BluetoothServiceAttributeValueBlueZ::NULLTYPE: | |
16 return new base::ListValue(); | |
17 case bluez::BluetoothServiceAttributeValueBlueZ::UINT: | |
18 case bluez::BluetoothServiceAttributeValueBlueZ::INT: | |
19 case bluez::BluetoothServiceAttributeValueBlueZ::UUID: | |
20 case bluez::BluetoothServiceAttributeValueBlueZ::STRING: | |
21 case bluez::BluetoothServiceAttributeValueBlueZ::BOOL: | |
22 case bluez::BluetoothServiceAttributeValueBlueZ::URL: { | |
23 // Mojo does not have IPC::ParamTraits for base::Value currently, so we | |
24 // need to wrap the return of value() in a base::ListValue. The return of | |
25 // value() is a const reference owned by |value|, so we need to a deep | |
26 // copy of base::Value to construct base::ListValue. | |
27 base::ListValue* list_value = new base::ListValue(); | |
28 list_value->Append(value.value().CreateDeepCopy()); | |
29 return list_value; | |
30 } | |
31 case bluez::BluetoothServiceAttributeValueBlueZ::SEQUENCE: { | |
32 return new bluez::BluetoothServiceAttributeValueBlueZ::Sequence( | |
yzshen1
2016/08/26 18:41:39
why do you need to make a copy of this?
| |
33 value.sequence()); | |
34 } | |
35 default: | |
36 NOTREACHED() << "Invalid type: " << static_cast<uint32_t>(value.type()); | |
37 return nullptr; | |
38 } | |
39 } | |
40 | |
41 void TearDownContextForAttributeValue( | |
42 const bluez::BluetoothServiceAttributeValueBlueZ& value, | |
43 void* context) { | |
44 switch (value.type()) { | |
45 case bluez::BluetoothServiceAttributeValueBlueZ::NULLTYPE: | |
46 case bluez::BluetoothServiceAttributeValueBlueZ::UINT: | |
47 case bluez::BluetoothServiceAttributeValueBlueZ::INT: | |
48 case bluez::BluetoothServiceAttributeValueBlueZ::UUID: | |
49 case bluez::BluetoothServiceAttributeValueBlueZ::STRING: | |
50 case bluez::BluetoothServiceAttributeValueBlueZ::BOOL: | |
51 case bluez::BluetoothServiceAttributeValueBlueZ::URL: | |
52 delete reinterpret_cast<base::ListValue*>(context); | |
53 break; | |
54 case bluez::BluetoothServiceAttributeValueBlueZ::SEQUENCE: | |
55 delete reinterpret_cast< | |
56 bluez::BluetoothServiceAttributeValueBlueZ::Sequence*>(context); | |
57 break; | |
58 default: | |
59 NOTREACHED() << "Invalid type: " << static_cast<uint32_t>(value.type()); | |
60 } | |
61 } | |
62 | |
63 } // namespace | |
64 | |
65 namespace mojo { | |
66 | |
67 // static | |
68 arc::mojom::BluetoothSdpAttributeType | |
69 EnumTraits<arc::mojom::BluetoothSdpAttributeType, | |
70 bluez::BluetoothServiceAttributeValueBlueZ::Type>:: | |
71 ToMojom(bluez::BluetoothServiceAttributeValueBlueZ::Type input) { | |
72 switch (input) { | |
73 case bluez::BluetoothServiceAttributeValueBlueZ::NULLTYPE: | |
74 case bluez::BluetoothServiceAttributeValueBlueZ::UINT: | |
75 case bluez::BluetoothServiceAttributeValueBlueZ::INT: | |
76 case bluez::BluetoothServiceAttributeValueBlueZ::UUID: | |
77 case bluez::BluetoothServiceAttributeValueBlueZ::STRING: | |
78 case bluez::BluetoothServiceAttributeValueBlueZ::BOOL: | |
79 case bluez::BluetoothServiceAttributeValueBlueZ::SEQUENCE: | |
80 case bluez::BluetoothServiceAttributeValueBlueZ::URL: | |
81 return static_cast<arc::mojom::BluetoothSdpAttributeType>(input); | |
82 default: | |
83 NOTREACHED() << "Invalid type: " << static_cast<uint32_t>(input); | |
84 return arc::mojom::BluetoothSdpAttributeType::NULLTYPE; | |
85 } | |
86 } | |
87 | |
88 // static | |
89 bool EnumTraits<arc::mojom::BluetoothSdpAttributeType, | |
90 bluez::BluetoothServiceAttributeValueBlueZ::Type>:: | |
91 FromMojom(arc::mojom::BluetoothSdpAttributeType input, | |
92 bluez::BluetoothServiceAttributeValueBlueZ::Type* output) { | |
93 switch (input) { | |
94 case arc::mojom::BluetoothSdpAttributeType::NULLTYPE: | |
95 case arc::mojom::BluetoothSdpAttributeType::UINT: | |
96 case arc::mojom::BluetoothSdpAttributeType::INT: | |
97 case arc::mojom::BluetoothSdpAttributeType::UUID: | |
98 case arc::mojom::BluetoothSdpAttributeType::STRING: | |
99 case arc::mojom::BluetoothSdpAttributeType::BOOL: | |
100 case arc::mojom::BluetoothSdpAttributeType::SEQUENCE: | |
101 case arc::mojom::BluetoothSdpAttributeType::URL: | |
102 *output = | |
103 static_cast<bluez::BluetoothServiceAttributeValueBlueZ::Type>(input); | |
104 return true; | |
105 default: | |
106 NOTREACHED() << "Invalid type: " << static_cast<uint32_t>(input); | |
107 return false; | |
108 } | |
109 } | |
110 | |
111 // static | |
112 void* StructTraits<arc::mojom::BluetoothSdpAttributeLayer2DataView, | |
113 bluez::BluetoothServiceAttributeValueBlueZ>:: | |
114 SetUpContext(const bluez::BluetoothServiceAttributeValueBlueZ& value) { | |
115 return SetUpContextForAttributeValue(value); | |
116 } | |
117 | |
118 // static | |
119 void StructTraits<arc::mojom::BluetoothSdpAttributeLayer2DataView, | |
120 bluez::BluetoothServiceAttributeValueBlueZ>:: | |
121 TearDownContext(const bluez::BluetoothServiceAttributeValueBlueZ& value, | |
122 void* context) { | |
123 TearDownContextForAttributeValue(value, context); | |
124 } | |
125 | |
126 // static | |
127 bluez::BluetoothServiceAttributeValueBlueZ::Type | |
128 StructTraits<arc::mojom::BluetoothSdpAttributeLayer2DataView, | |
129 bluez::BluetoothServiceAttributeValueBlueZ>:: | |
130 type(const bluez::BluetoothServiceAttributeValueBlueZ& value, | |
131 void* context) { | |
132 return value.type(); | |
yzshen1
2016/08/26 18:41:39
Please move this kind of short methods into the .h
| |
133 } | |
134 | |
135 // static | |
136 uint32_t StructTraits<arc::mojom::BluetoothSdpAttributeLayer2DataView, | |
137 bluez::BluetoothServiceAttributeValueBlueZ>:: | |
138 type_size(const bluez::BluetoothServiceAttributeValueBlueZ& value, | |
139 void* context) { | |
140 return static_cast<uint32_t>(value.size()); | |
141 } | |
142 | |
143 // static | |
144 const base::ListValue& | |
145 StructTraits<arc::mojom::BluetoothSdpAttributeLayer2DataView, | |
146 bluez::BluetoothServiceAttributeValueBlueZ>:: | |
147 value(const bluez::BluetoothServiceAttributeValueBlueZ& value, | |
148 void* context) { | |
149 return *reinterpret_cast<base::ListValue*>(context); | |
150 } | |
151 | |
152 // static | |
153 bool StructTraits<arc::mojom::BluetoothSdpAttributeLayer2DataView, | |
154 bluez::BluetoothServiceAttributeValueBlueZ>:: | |
155 Read(arc::mojom::BluetoothSdpAttributeLayer2DataView data, | |
156 bluez::BluetoothServiceAttributeValueBlueZ* output) { | |
157 bluez::BluetoothServiceAttributeValueBlueZ::Type type; | |
158 if (!data.ReadType(&type)) | |
159 return false; | |
160 | |
161 switch (type) { | |
162 case bluez::BluetoothServiceAttributeValueBlueZ::NULLTYPE: | |
163 *output = bluez::BluetoothServiceAttributeValueBlueZ(); | |
164 break; | |
165 case bluez::BluetoothServiceAttributeValueBlueZ::UINT: | |
166 case bluez::BluetoothServiceAttributeValueBlueZ::INT: | |
167 case bluez::BluetoothServiceAttributeValueBlueZ::UUID: | |
168 case bluez::BluetoothServiceAttributeValueBlueZ::STRING: | |
169 case bluez::BluetoothServiceAttributeValueBlueZ::URL: | |
170 case bluez::BluetoothServiceAttributeValueBlueZ::BOOL: { | |
171 base::ListValue list_value; | |
172 if (!data.ReadValue(&list_value) || list_value.GetSize() != 1) | |
173 return false; | |
174 | |
175 size_t size = data.type_size(); | |
176 std::unique_ptr<base::Value> value; | |
177 list_value.Remove(0, &value); | |
178 | |
179 *output = bluez::BluetoothServiceAttributeValueBlueZ(type, size, | |
180 std::move(value)); | |
181 break; | |
182 } | |
183 case bluez::BluetoothServiceAttributeValueBlueZ::SEQUENCE: // Flow through | |
184 default: | |
185 return false; | |
186 } | |
187 return true; | |
188 } | |
189 | |
190 // static | |
191 void* StructTraits<arc::mojom::BluetoothSdpAttributeLayer1DataView, | |
192 bluez::BluetoothServiceAttributeValueBlueZ>:: | |
193 SetUpContext(const bluez::BluetoothServiceAttributeValueBlueZ& value) { | |
194 return SetUpContextForAttributeValue(value); | |
195 } | |
196 | |
197 // static | |
198 void StructTraits<arc::mojom::BluetoothSdpAttributeLayer1DataView, | |
199 bluez::BluetoothServiceAttributeValueBlueZ>:: | |
200 TearDownContext(const bluez::BluetoothServiceAttributeValueBlueZ& value, | |
201 void* context) { | |
202 TearDownContextForAttributeValue(value, context); | |
203 } | |
204 | |
205 // static | |
206 bluez::BluetoothServiceAttributeValueBlueZ::Type | |
207 StructTraits<arc::mojom::BluetoothSdpAttributeLayer1DataView, | |
208 bluez::BluetoothServiceAttributeValueBlueZ>:: | |
209 type(const bluez::BluetoothServiceAttributeValueBlueZ& value, | |
210 void* context) { | |
211 return value.type(); | |
212 } | |
213 | |
214 // static | |
215 uint32_t StructTraits<arc::mojom::BluetoothSdpAttributeLayer1DataView, | |
216 bluez::BluetoothServiceAttributeValueBlueZ>:: | |
217 type_size(const bluez::BluetoothServiceAttributeValueBlueZ& value, | |
218 void* context) { | |
219 return static_cast<uint32_t>(value.size()); | |
220 } | |
221 | |
222 // static | |
223 const base::ListValue& | |
224 StructTraits<arc::mojom::BluetoothSdpAttributeLayer1DataView, | |
225 bluez::BluetoothServiceAttributeValueBlueZ>:: | |
226 value(const bluez::BluetoothServiceAttributeValueBlueZ& value, | |
227 void* context) { | |
228 return *reinterpret_cast<base::ListValue*>(context); | |
229 } | |
230 | |
231 // static | |
232 const bluez::BluetoothServiceAttributeValueBlueZ::Sequence& | |
233 StructTraits<arc::mojom::BluetoothSdpAttributeLayer1DataView, | |
234 bluez::BluetoothServiceAttributeValueBlueZ>:: | |
235 sequence(const bluez::BluetoothServiceAttributeValueBlueZ& value, | |
yzshen1
2016/08/26 18:41:39
why not directly returning value.sequence()?
| |
236 void* context) { | |
237 return *reinterpret_cast< | |
238 bluez::BluetoothServiceAttributeValueBlueZ::Sequence*>(context); | |
239 } | |
240 | |
241 // static | |
242 bool StructTraits<arc::mojom::BluetoothSdpAttributeLayer1DataView, | |
243 bluez::BluetoothServiceAttributeValueBlueZ>:: | |
244 Read(arc::mojom::BluetoothSdpAttributeLayer1DataView data, | |
245 bluez::BluetoothServiceAttributeValueBlueZ* output) { | |
246 bluez::BluetoothServiceAttributeValueBlueZ::Type type; | |
247 if (!data.ReadType(&type)) | |
248 return false; | |
249 | |
250 switch (type) { | |
251 case bluez::BluetoothServiceAttributeValueBlueZ::NULLTYPE: | |
252 *output = bluez::BluetoothServiceAttributeValueBlueZ(); | |
253 break; | |
254 case bluez::BluetoothServiceAttributeValueBlueZ::UINT: | |
255 case bluez::BluetoothServiceAttributeValueBlueZ::INT: | |
256 case bluez::BluetoothServiceAttributeValueBlueZ::UUID: | |
257 case bluez::BluetoothServiceAttributeValueBlueZ::STRING: | |
258 case bluez::BluetoothServiceAttributeValueBlueZ::URL: | |
259 case bluez::BluetoothServiceAttributeValueBlueZ::BOOL: { | |
260 base::Optional<base::ListValue> list_value; | |
261 if (!data.ReadValue(&list_value) || !list_value.has_value() || | |
262 list_value->GetSize() != 1) { | |
263 return false; | |
264 } | |
265 | |
266 std::unique_ptr<base::Value> value; | |
267 list_value->Remove(0, &value); | |
268 | |
269 *output = bluez::BluetoothServiceAttributeValueBlueZ( | |
270 type, static_cast<size_t>(data.type_size()), std::move(value)); | |
271 break; | |
272 } | |
273 case bluez::BluetoothServiceAttributeValueBlueZ::SEQUENCE: { | |
274 base::Optional<bluez::BluetoothServiceAttributeValueBlueZ::Sequence> | |
275 sequence; | |
276 if (!data.ReadSequence(&sequence) || !sequence.has_value() || | |
277 sequence->empty()) { | |
278 return false; | |
279 } | |
280 *output = bluez::BluetoothServiceAttributeValueBlueZ( | |
281 base::MakeUnique< | |
282 bluez::BluetoothServiceAttributeValueBlueZ::Sequence>( | |
283 sequence.value())); | |
yzshen1
2016/08/26 18:41:39
Use std::move(sequence.value()) to save a copy her
| |
284 break; | |
285 } | |
286 default: | |
287 return false; | |
288 } | |
289 return true; | |
290 } | |
291 | |
292 // static | |
293 void* StructTraits<arc::mojom::BluetoothSdpAttributeDataView, | |
294 bluez::BluetoothServiceAttributeValueBlueZ>:: | |
295 SetUpContext(const bluez::BluetoothServiceAttributeValueBlueZ& value) { | |
296 return SetUpContextForAttributeValue(value); | |
297 } | |
298 | |
299 // static | |
300 void StructTraits<arc::mojom::BluetoothSdpAttributeDataView, | |
301 bluez::BluetoothServiceAttributeValueBlueZ>:: | |
302 TearDownContext(const bluez::BluetoothServiceAttributeValueBlueZ& value, | |
303 void* context) { | |
304 TearDownContextForAttributeValue(value, context); | |
305 } | |
306 | |
307 // static | |
308 bluez::BluetoothServiceAttributeValueBlueZ::Type | |
309 StructTraits<arc::mojom::BluetoothSdpAttributeDataView, | |
310 bluez::BluetoothServiceAttributeValueBlueZ>:: | |
311 type(const bluez::BluetoothServiceAttributeValueBlueZ& value, | |
312 void* context) { | |
313 return value.type(); | |
314 } | |
315 | |
316 // static | |
317 uint32_t StructTraits<arc::mojom::BluetoothSdpAttributeDataView, | |
318 bluez::BluetoothServiceAttributeValueBlueZ>:: | |
319 type_size(const bluez::BluetoothServiceAttributeValueBlueZ& value, | |
320 void* context) { | |
321 return static_cast<uint32_t>(value.size()); | |
322 } | |
323 | |
324 // static | |
325 const base::ListValue& | |
326 StructTraits<arc::mojom::BluetoothSdpAttributeDataView, | |
327 bluez::BluetoothServiceAttributeValueBlueZ>:: | |
328 value(const bluez::BluetoothServiceAttributeValueBlueZ& value, | |
329 void* context) { | |
330 return *reinterpret_cast<base::ListValue*>(context); | |
331 } | |
332 | |
333 // static | |
334 const bluez::BluetoothServiceAttributeValueBlueZ::Sequence& | |
335 StructTraits<arc::mojom::BluetoothSdpAttributeDataView, | |
336 bluez::BluetoothServiceAttributeValueBlueZ>:: | |
337 sequence(const bluez::BluetoothServiceAttributeValueBlueZ& value, | |
338 void* context) { | |
339 return *reinterpret_cast< | |
340 bluez::BluetoothServiceAttributeValueBlueZ::Sequence*>(context); | |
341 } | |
342 | |
343 // static | |
344 bool StructTraits<arc::mojom::BluetoothSdpAttributeDataView, | |
345 bluez::BluetoothServiceAttributeValueBlueZ>:: | |
346 Read(arc::mojom::BluetoothSdpAttributeDataView data, | |
347 bluez::BluetoothServiceAttributeValueBlueZ* output) { | |
348 bluez::BluetoothServiceAttributeValueBlueZ::Type type; | |
349 if (!data.ReadType(&type)) | |
350 return false; | |
351 | |
352 switch (type) { | |
353 case bluez::BluetoothServiceAttributeValueBlueZ::NULLTYPE: | |
354 *output = bluez::BluetoothServiceAttributeValueBlueZ(); | |
355 break; | |
356 case bluez::BluetoothServiceAttributeValueBlueZ::UINT: | |
357 case bluez::BluetoothServiceAttributeValueBlueZ::INT: | |
358 case bluez::BluetoothServiceAttributeValueBlueZ::UUID: | |
359 case bluez::BluetoothServiceAttributeValueBlueZ::STRING: | |
360 case bluez::BluetoothServiceAttributeValueBlueZ::URL: | |
361 case bluez::BluetoothServiceAttributeValueBlueZ::BOOL: { | |
362 base::Optional<base::ListValue> list_value; | |
363 if (!data.ReadValue(&list_value) || !list_value.has_value() || | |
364 list_value->GetSize() != 1) { | |
365 return false; | |
366 } | |
367 | |
368 std::unique_ptr<base::Value> value; | |
369 list_value->Remove(0, &value); | |
370 | |
371 *output = bluez::BluetoothServiceAttributeValueBlueZ( | |
372 type, static_cast<size_t>(data.type_size()), std::move(value)); | |
373 break; | |
374 } | |
375 case bluez::BluetoothServiceAttributeValueBlueZ::SEQUENCE: { | |
376 base::Optional<bluez::BluetoothServiceAttributeValueBlueZ::Sequence> | |
377 sequence; | |
378 if (!data.ReadSequence(&sequence) || !sequence.has_value() || | |
379 sequence->empty()) { | |
380 return false; | |
381 } | |
382 *output = bluez::BluetoothServiceAttributeValueBlueZ( | |
puthik_chromium
2016/08/26 01:05:35
It crashes at this line when tested with CTS Verif
Miao
2016/08/26 16:48:56
This should be resolved by https://codereview.chro
| |
383 base::MakeUnique< | |
384 bluez::BluetoothServiceAttributeValueBlueZ::Sequence>( | |
385 sequence.value())); | |
yzshen1
2016/08/26 18:41:39
Use std::move(sequence.value()) to save a copy her
| |
386 break; | |
387 } | |
388 default: | |
389 return false; | |
390 } | |
391 return true; | |
392 } | |
393 | |
394 // static | |
395 void* StructTraits<arc::mojom::BluetoothSdpRecordDataView, | |
396 bluez::BluetoothServiceRecordBlueZ>:: | |
397 SetUpContext(const bluez::BluetoothServiceRecordBlueZ& value) { | |
398 std::map<uint16_t, bluez::BluetoothServiceAttributeValueBlueZ>* attributes = | |
yzshen1
2016/08/26 18:41:39
Can you make BluetoothServiceRecordBluz expose the
| |
399 new std::map<uint16_t, bluez::BluetoothServiceAttributeValueBlueZ>(); | |
400 std::vector<uint16_t> attribute_ids = value.GetAttributeIds(); | |
401 | |
402 for (auto it : attribute_ids) { | |
403 attributes->at(it) = | |
404 bluez::BluetoothServiceAttributeValueBlueZ(value.GetAttributeValue(it)); | |
405 } | |
406 return attributes; | |
407 } | |
408 | |
409 // static | |
410 void StructTraits<arc::mojom::BluetoothSdpRecordDataView, | |
411 bluez::BluetoothServiceRecordBlueZ>:: | |
412 TearDownContext(const bluez::BluetoothServiceRecordBlueZ& value, | |
413 void* context) { | |
414 delete reinterpret_cast< | |
415 std::map<uint16_t, bluez::BluetoothServiceAttributeValueBlueZ>*>(context); | |
416 } | |
417 | |
418 // static | |
419 const std::map<uint16_t, bluez::BluetoothServiceAttributeValueBlueZ>& | |
420 StructTraits<arc::mojom::BluetoothSdpRecordDataView, | |
421 bluez::BluetoothServiceRecordBlueZ>:: | |
422 attrs(const bluez::BluetoothServiceRecordBlueZ& value, void* context) { | |
423 return *reinterpret_cast< | |
424 std::map<uint16_t, bluez::BluetoothServiceAttributeValueBlueZ>*>(context); | |
425 } | |
426 | |
427 // static | |
428 bool StructTraits<arc::mojom::BluetoothSdpRecordDataView, | |
429 bluez::BluetoothServiceRecordBlueZ>:: | |
430 Read(arc::mojom::BluetoothSdpRecordDataView data, | |
431 bluez::BluetoothServiceRecordBlueZ* output) { | |
432 mojo::MapDataView<uint16_t, arc::mojom::BluetoothSdpAttributeDataView> | |
433 map_data_view; | |
434 data.GetAttrsDataView(&map_data_view); | |
435 if (map_data_view.is_null()) | |
436 return false; | |
437 | |
438 for (size_t i = 0; i < map_data_view.size(); ++i) { | |
439 bluez::BluetoothServiceAttributeValueBlueZ value; | |
440 if (!map_data_view.values().Read(i, &value)) | |
441 return false; | |
442 output->AddRecordEntry(map_data_view.keys()[i], value); | |
443 } | |
444 | |
445 return true; | |
446 } | |
447 | |
448 } // namespace mojo | |
OLD | NEW |