Chromium Code Reviews| Index: components/arc/bluetooth/bluetooth_struct_traits.cc |
| diff --git a/components/arc/bluetooth/bluetooth_struct_traits.cc b/components/arc/bluetooth/bluetooth_struct_traits.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..bfdb4b4f50c0ccd74e46821b60d9782b150677ef |
| --- /dev/null |
| +++ b/components/arc/bluetooth/bluetooth_struct_traits.cc |
| @@ -0,0 +1,448 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/arc/bluetooth/bluetooth_struct_traits.h" |
| + |
| +#include <memory> |
| +#include <utility> |
| + |
| +namespace { |
| + |
| +void* SetUpContextForAttributeValue( |
| + const bluez::BluetoothServiceAttributeValueBlueZ& value) { |
| + switch (value.type()) { |
| + case bluez::BluetoothServiceAttributeValueBlueZ::NULLTYPE: |
| + return new base::ListValue(); |
| + case bluez::BluetoothServiceAttributeValueBlueZ::UINT: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::INT: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::UUID: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::STRING: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::BOOL: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::URL: { |
| + // Mojo does not have IPC::ParamTraits for base::Value currently, so we |
| + // need to wrap the return of value() in a base::ListValue. The return of |
| + // value() is a const reference owned by |value|, so we need to a deep |
| + // copy of base::Value to construct base::ListValue. |
| + base::ListValue* list_value = new base::ListValue(); |
| + list_value->Append(value.value().CreateDeepCopy()); |
| + return list_value; |
| + } |
| + case bluez::BluetoothServiceAttributeValueBlueZ::SEQUENCE: { |
| + return new bluez::BluetoothServiceAttributeValueBlueZ::Sequence( |
|
yzshen1
2016/08/26 18:41:39
why do you need to make a copy of this?
|
| + value.sequence()); |
| + } |
| + default: |
| + NOTREACHED() << "Invalid type: " << static_cast<uint32_t>(value.type()); |
| + return nullptr; |
| + } |
| +} |
| + |
| +void TearDownContextForAttributeValue( |
| + const bluez::BluetoothServiceAttributeValueBlueZ& value, |
| + void* context) { |
| + switch (value.type()) { |
| + case bluez::BluetoothServiceAttributeValueBlueZ::NULLTYPE: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::UINT: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::INT: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::UUID: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::STRING: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::BOOL: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::URL: |
| + delete reinterpret_cast<base::ListValue*>(context); |
| + break; |
| + case bluez::BluetoothServiceAttributeValueBlueZ::SEQUENCE: |
| + delete reinterpret_cast< |
| + bluez::BluetoothServiceAttributeValueBlueZ::Sequence*>(context); |
| + break; |
| + default: |
| + NOTREACHED() << "Invalid type: " << static_cast<uint32_t>(value.type()); |
| + } |
| +} |
| + |
| +} // namespace |
| + |
| +namespace mojo { |
| + |
| +// static |
| +arc::mojom::BluetoothSdpAttributeType |
| +EnumTraits<arc::mojom::BluetoothSdpAttributeType, |
| + bluez::BluetoothServiceAttributeValueBlueZ::Type>:: |
| + ToMojom(bluez::BluetoothServiceAttributeValueBlueZ::Type input) { |
| + switch (input) { |
| + case bluez::BluetoothServiceAttributeValueBlueZ::NULLTYPE: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::UINT: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::INT: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::UUID: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::STRING: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::BOOL: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::SEQUENCE: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::URL: |
| + return static_cast<arc::mojom::BluetoothSdpAttributeType>(input); |
| + default: |
| + NOTREACHED() << "Invalid type: " << static_cast<uint32_t>(input); |
| + return arc::mojom::BluetoothSdpAttributeType::NULLTYPE; |
| + } |
| +} |
| + |
| +// static |
| +bool EnumTraits<arc::mojom::BluetoothSdpAttributeType, |
| + bluez::BluetoothServiceAttributeValueBlueZ::Type>:: |
| + FromMojom(arc::mojom::BluetoothSdpAttributeType input, |
| + bluez::BluetoothServiceAttributeValueBlueZ::Type* output) { |
| + switch (input) { |
| + case arc::mojom::BluetoothSdpAttributeType::NULLTYPE: |
| + case arc::mojom::BluetoothSdpAttributeType::UINT: |
| + case arc::mojom::BluetoothSdpAttributeType::INT: |
| + case arc::mojom::BluetoothSdpAttributeType::UUID: |
| + case arc::mojom::BluetoothSdpAttributeType::STRING: |
| + case arc::mojom::BluetoothSdpAttributeType::BOOL: |
| + case arc::mojom::BluetoothSdpAttributeType::SEQUENCE: |
| + case arc::mojom::BluetoothSdpAttributeType::URL: |
| + *output = |
| + static_cast<bluez::BluetoothServiceAttributeValueBlueZ::Type>(input); |
| + return true; |
| + default: |
| + NOTREACHED() << "Invalid type: " << static_cast<uint32_t>(input); |
| + return false; |
| + } |
| +} |
| + |
| +// static |
| +void* StructTraits<arc::mojom::BluetoothSdpAttributeLayer2DataView, |
| + bluez::BluetoothServiceAttributeValueBlueZ>:: |
| + SetUpContext(const bluez::BluetoothServiceAttributeValueBlueZ& value) { |
| + return SetUpContextForAttributeValue(value); |
| +} |
| + |
| +// static |
| +void StructTraits<arc::mojom::BluetoothSdpAttributeLayer2DataView, |
| + bluez::BluetoothServiceAttributeValueBlueZ>:: |
| + TearDownContext(const bluez::BluetoothServiceAttributeValueBlueZ& value, |
| + void* context) { |
| + TearDownContextForAttributeValue(value, context); |
| +} |
| + |
| +// static |
| +bluez::BluetoothServiceAttributeValueBlueZ::Type |
| +StructTraits<arc::mojom::BluetoothSdpAttributeLayer2DataView, |
| + bluez::BluetoothServiceAttributeValueBlueZ>:: |
| + type(const bluez::BluetoothServiceAttributeValueBlueZ& value, |
| + void* context) { |
| + return value.type(); |
|
yzshen1
2016/08/26 18:41:39
Please move this kind of short methods into the .h
|
| +} |
| + |
| +// static |
| +uint32_t StructTraits<arc::mojom::BluetoothSdpAttributeLayer2DataView, |
| + bluez::BluetoothServiceAttributeValueBlueZ>:: |
| + type_size(const bluez::BluetoothServiceAttributeValueBlueZ& value, |
| + void* context) { |
| + return static_cast<uint32_t>(value.size()); |
| +} |
| + |
| +// static |
| +const base::ListValue& |
| +StructTraits<arc::mojom::BluetoothSdpAttributeLayer2DataView, |
| + bluez::BluetoothServiceAttributeValueBlueZ>:: |
| + value(const bluez::BluetoothServiceAttributeValueBlueZ& value, |
| + void* context) { |
| + return *reinterpret_cast<base::ListValue*>(context); |
| +} |
| + |
| +// static |
| +bool StructTraits<arc::mojom::BluetoothSdpAttributeLayer2DataView, |
| + bluez::BluetoothServiceAttributeValueBlueZ>:: |
| + Read(arc::mojom::BluetoothSdpAttributeLayer2DataView data, |
| + bluez::BluetoothServiceAttributeValueBlueZ* output) { |
| + bluez::BluetoothServiceAttributeValueBlueZ::Type type; |
| + if (!data.ReadType(&type)) |
| + return false; |
| + |
| + switch (type) { |
| + case bluez::BluetoothServiceAttributeValueBlueZ::NULLTYPE: |
| + *output = bluez::BluetoothServiceAttributeValueBlueZ(); |
| + break; |
| + case bluez::BluetoothServiceAttributeValueBlueZ::UINT: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::INT: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::UUID: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::STRING: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::URL: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::BOOL: { |
| + base::ListValue list_value; |
| + if (!data.ReadValue(&list_value) || list_value.GetSize() != 1) |
| + return false; |
| + |
| + size_t size = data.type_size(); |
| + std::unique_ptr<base::Value> value; |
| + list_value.Remove(0, &value); |
| + |
| + *output = bluez::BluetoothServiceAttributeValueBlueZ(type, size, |
| + std::move(value)); |
| + break; |
| + } |
| + case bluez::BluetoothServiceAttributeValueBlueZ::SEQUENCE: // Flow through |
| + default: |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| +// static |
| +void* StructTraits<arc::mojom::BluetoothSdpAttributeLayer1DataView, |
| + bluez::BluetoothServiceAttributeValueBlueZ>:: |
| + SetUpContext(const bluez::BluetoothServiceAttributeValueBlueZ& value) { |
| + return SetUpContextForAttributeValue(value); |
| +} |
| + |
| +// static |
| +void StructTraits<arc::mojom::BluetoothSdpAttributeLayer1DataView, |
| + bluez::BluetoothServiceAttributeValueBlueZ>:: |
| + TearDownContext(const bluez::BluetoothServiceAttributeValueBlueZ& value, |
| + void* context) { |
| + TearDownContextForAttributeValue(value, context); |
| +} |
| + |
| +// static |
| +bluez::BluetoothServiceAttributeValueBlueZ::Type |
| +StructTraits<arc::mojom::BluetoothSdpAttributeLayer1DataView, |
| + bluez::BluetoothServiceAttributeValueBlueZ>:: |
| + type(const bluez::BluetoothServiceAttributeValueBlueZ& value, |
| + void* context) { |
| + return value.type(); |
| +} |
| + |
| +// static |
| +uint32_t StructTraits<arc::mojom::BluetoothSdpAttributeLayer1DataView, |
| + bluez::BluetoothServiceAttributeValueBlueZ>:: |
| + type_size(const bluez::BluetoothServiceAttributeValueBlueZ& value, |
| + void* context) { |
| + return static_cast<uint32_t>(value.size()); |
| +} |
| + |
| +// static |
| +const base::ListValue& |
| +StructTraits<arc::mojom::BluetoothSdpAttributeLayer1DataView, |
| + bluez::BluetoothServiceAttributeValueBlueZ>:: |
| + value(const bluez::BluetoothServiceAttributeValueBlueZ& value, |
| + void* context) { |
| + return *reinterpret_cast<base::ListValue*>(context); |
| +} |
| + |
| +// static |
| +const bluez::BluetoothServiceAttributeValueBlueZ::Sequence& |
| +StructTraits<arc::mojom::BluetoothSdpAttributeLayer1DataView, |
| + bluez::BluetoothServiceAttributeValueBlueZ>:: |
| + sequence(const bluez::BluetoothServiceAttributeValueBlueZ& value, |
|
yzshen1
2016/08/26 18:41:39
why not directly returning value.sequence()?
|
| + void* context) { |
| + return *reinterpret_cast< |
| + bluez::BluetoothServiceAttributeValueBlueZ::Sequence*>(context); |
| +} |
| + |
| +// static |
| +bool StructTraits<arc::mojom::BluetoothSdpAttributeLayer1DataView, |
| + bluez::BluetoothServiceAttributeValueBlueZ>:: |
| + Read(arc::mojom::BluetoothSdpAttributeLayer1DataView data, |
| + bluez::BluetoothServiceAttributeValueBlueZ* output) { |
| + bluez::BluetoothServiceAttributeValueBlueZ::Type type; |
| + if (!data.ReadType(&type)) |
| + return false; |
| + |
| + switch (type) { |
| + case bluez::BluetoothServiceAttributeValueBlueZ::NULLTYPE: |
| + *output = bluez::BluetoothServiceAttributeValueBlueZ(); |
| + break; |
| + case bluez::BluetoothServiceAttributeValueBlueZ::UINT: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::INT: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::UUID: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::STRING: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::URL: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::BOOL: { |
| + base::Optional<base::ListValue> list_value; |
| + if (!data.ReadValue(&list_value) || !list_value.has_value() || |
| + list_value->GetSize() != 1) { |
| + return false; |
| + } |
| + |
| + std::unique_ptr<base::Value> value; |
| + list_value->Remove(0, &value); |
| + |
| + *output = bluez::BluetoothServiceAttributeValueBlueZ( |
| + type, static_cast<size_t>(data.type_size()), std::move(value)); |
| + break; |
| + } |
| + case bluez::BluetoothServiceAttributeValueBlueZ::SEQUENCE: { |
| + base::Optional<bluez::BluetoothServiceAttributeValueBlueZ::Sequence> |
| + sequence; |
| + if (!data.ReadSequence(&sequence) || !sequence.has_value() || |
| + sequence->empty()) { |
| + return false; |
| + } |
| + *output = bluez::BluetoothServiceAttributeValueBlueZ( |
| + base::MakeUnique< |
| + bluez::BluetoothServiceAttributeValueBlueZ::Sequence>( |
| + sequence.value())); |
|
yzshen1
2016/08/26 18:41:39
Use std::move(sequence.value()) to save a copy her
|
| + break; |
| + } |
| + default: |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| +// static |
| +void* StructTraits<arc::mojom::BluetoothSdpAttributeDataView, |
| + bluez::BluetoothServiceAttributeValueBlueZ>:: |
| + SetUpContext(const bluez::BluetoothServiceAttributeValueBlueZ& value) { |
| + return SetUpContextForAttributeValue(value); |
| +} |
| + |
| +// static |
| +void StructTraits<arc::mojom::BluetoothSdpAttributeDataView, |
| + bluez::BluetoothServiceAttributeValueBlueZ>:: |
| + TearDownContext(const bluez::BluetoothServiceAttributeValueBlueZ& value, |
| + void* context) { |
| + TearDownContextForAttributeValue(value, context); |
| +} |
| + |
| +// static |
| +bluez::BluetoothServiceAttributeValueBlueZ::Type |
| +StructTraits<arc::mojom::BluetoothSdpAttributeDataView, |
| + bluez::BluetoothServiceAttributeValueBlueZ>:: |
| + type(const bluez::BluetoothServiceAttributeValueBlueZ& value, |
| + void* context) { |
| + return value.type(); |
| +} |
| + |
| +// static |
| +uint32_t StructTraits<arc::mojom::BluetoothSdpAttributeDataView, |
| + bluez::BluetoothServiceAttributeValueBlueZ>:: |
| + type_size(const bluez::BluetoothServiceAttributeValueBlueZ& value, |
| + void* context) { |
| + return static_cast<uint32_t>(value.size()); |
| +} |
| + |
| +// static |
| +const base::ListValue& |
| +StructTraits<arc::mojom::BluetoothSdpAttributeDataView, |
| + bluez::BluetoothServiceAttributeValueBlueZ>:: |
| + value(const bluez::BluetoothServiceAttributeValueBlueZ& value, |
| + void* context) { |
| + return *reinterpret_cast<base::ListValue*>(context); |
| +} |
| + |
| +// static |
| +const bluez::BluetoothServiceAttributeValueBlueZ::Sequence& |
| +StructTraits<arc::mojom::BluetoothSdpAttributeDataView, |
| + bluez::BluetoothServiceAttributeValueBlueZ>:: |
| + sequence(const bluez::BluetoothServiceAttributeValueBlueZ& value, |
| + void* context) { |
| + return *reinterpret_cast< |
| + bluez::BluetoothServiceAttributeValueBlueZ::Sequence*>(context); |
| +} |
| + |
| +// static |
| +bool StructTraits<arc::mojom::BluetoothSdpAttributeDataView, |
| + bluez::BluetoothServiceAttributeValueBlueZ>:: |
| + Read(arc::mojom::BluetoothSdpAttributeDataView data, |
| + bluez::BluetoothServiceAttributeValueBlueZ* output) { |
| + bluez::BluetoothServiceAttributeValueBlueZ::Type type; |
| + if (!data.ReadType(&type)) |
| + return false; |
| + |
| + switch (type) { |
| + case bluez::BluetoothServiceAttributeValueBlueZ::NULLTYPE: |
| + *output = bluez::BluetoothServiceAttributeValueBlueZ(); |
| + break; |
| + case bluez::BluetoothServiceAttributeValueBlueZ::UINT: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::INT: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::UUID: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::STRING: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::URL: |
| + case bluez::BluetoothServiceAttributeValueBlueZ::BOOL: { |
| + base::Optional<base::ListValue> list_value; |
| + if (!data.ReadValue(&list_value) || !list_value.has_value() || |
| + list_value->GetSize() != 1) { |
| + return false; |
| + } |
| + |
| + std::unique_ptr<base::Value> value; |
| + list_value->Remove(0, &value); |
| + |
| + *output = bluez::BluetoothServiceAttributeValueBlueZ( |
| + type, static_cast<size_t>(data.type_size()), std::move(value)); |
| + break; |
| + } |
| + case bluez::BluetoothServiceAttributeValueBlueZ::SEQUENCE: { |
| + base::Optional<bluez::BluetoothServiceAttributeValueBlueZ::Sequence> |
| + sequence; |
| + if (!data.ReadSequence(&sequence) || !sequence.has_value() || |
| + sequence->empty()) { |
| + return false; |
| + } |
| + *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
|
| + base::MakeUnique< |
| + bluez::BluetoothServiceAttributeValueBlueZ::Sequence>( |
| + sequence.value())); |
|
yzshen1
2016/08/26 18:41:39
Use std::move(sequence.value()) to save a copy her
|
| + break; |
| + } |
| + default: |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| +// static |
| +void* StructTraits<arc::mojom::BluetoothSdpRecordDataView, |
| + bluez::BluetoothServiceRecordBlueZ>:: |
| + SetUpContext(const bluez::BluetoothServiceRecordBlueZ& value) { |
| + std::map<uint16_t, bluez::BluetoothServiceAttributeValueBlueZ>* attributes = |
|
yzshen1
2016/08/26 18:41:39
Can you make BluetoothServiceRecordBluz expose the
|
| + new std::map<uint16_t, bluez::BluetoothServiceAttributeValueBlueZ>(); |
| + std::vector<uint16_t> attribute_ids = value.GetAttributeIds(); |
| + |
| + for (auto it : attribute_ids) { |
| + attributes->at(it) = |
| + bluez::BluetoothServiceAttributeValueBlueZ(value.GetAttributeValue(it)); |
| + } |
| + return attributes; |
| +} |
| + |
| +// static |
| +void StructTraits<arc::mojom::BluetoothSdpRecordDataView, |
| + bluez::BluetoothServiceRecordBlueZ>:: |
| + TearDownContext(const bluez::BluetoothServiceRecordBlueZ& value, |
| + void* context) { |
| + delete reinterpret_cast< |
| + std::map<uint16_t, bluez::BluetoothServiceAttributeValueBlueZ>*>(context); |
| +} |
| + |
| +// static |
| +const std::map<uint16_t, bluez::BluetoothServiceAttributeValueBlueZ>& |
| +StructTraits<arc::mojom::BluetoothSdpRecordDataView, |
| + bluez::BluetoothServiceRecordBlueZ>:: |
| + attrs(const bluez::BluetoothServiceRecordBlueZ& value, void* context) { |
| + return *reinterpret_cast< |
| + std::map<uint16_t, bluez::BluetoothServiceAttributeValueBlueZ>*>(context); |
| +} |
| + |
| +// static |
| +bool StructTraits<arc::mojom::BluetoothSdpRecordDataView, |
| + bluez::BluetoothServiceRecordBlueZ>:: |
| + Read(arc::mojom::BluetoothSdpRecordDataView data, |
| + bluez::BluetoothServiceRecordBlueZ* output) { |
| + mojo::MapDataView<uint16_t, arc::mojom::BluetoothSdpAttributeDataView> |
| + map_data_view; |
| + data.GetAttrsDataView(&map_data_view); |
| + if (map_data_view.is_null()) |
| + return false; |
| + |
| + for (size_t i = 0; i < map_data_view.size(); ++i) { |
| + bluez::BluetoothServiceAttributeValueBlueZ value; |
| + if (!map_data_view.values().Read(i, &value)) |
| + return false; |
| + output->AddRecordEntry(map_data_view.keys()[i], value); |
| + } |
| + |
| + return true; |
| +} |
| + |
| +} // namespace mojo |