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