| 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..b531a1219706999f0ca3a84d8a7be3fd9d5aed1f
|
| --- /dev/null
|
| +++ b/components/arc/bluetooth/bluetooth_struct_traits.cc
|
| @@ -0,0 +1,447 @@
|
| +// 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: {
|
| + base::ListValue* list_value = new base::ListValue();
|
| + return list_value;
|
| + break;
|
| + }
|
| + 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: {
|
| + bluez::BluetoothServiceAttributeValueBlueZ::Sequence* sequence =
|
| + new bluez::BluetoothServiceAttributeValueBlueZ::Sequence();
|
| + *sequence = value.sequence();
|
| + return 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::BluetoothSdpAttributeLayer2,
|
| + bluez::BluetoothServiceAttributeValueBlueZ>::
|
| + SetUpContext(const bluez::BluetoothServiceAttributeValueBlueZ& value) {
|
| + return SetUpContextForAttributeValue(value);
|
| +}
|
| +
|
| +// static
|
| +void StructTraits<arc::mojom::BluetoothSdpAttributeLayer2,
|
| + bluez::BluetoothServiceAttributeValueBlueZ>::
|
| + TearDownContext(const bluez::BluetoothServiceAttributeValueBlueZ& value,
|
| + void* context) {
|
| + TearDownContextForAttributeValue(value, context);
|
| +}
|
| +
|
| +// static
|
| +bluez::BluetoothServiceAttributeValueBlueZ::Type
|
| +StructTraits<arc::mojom::BluetoothSdpAttributeLayer2,
|
| + bluez::BluetoothServiceAttributeValueBlueZ>::
|
| + type(const bluez::BluetoothServiceAttributeValueBlueZ& value,
|
| + void* context) {
|
| + return value.type();
|
| +}
|
| +
|
| +// static
|
| +uint32_t StructTraits<arc::mojom::BluetoothSdpAttributeLayer2,
|
| + bluez::BluetoothServiceAttributeValueBlueZ>::
|
| + type_size(const bluez::BluetoothServiceAttributeValueBlueZ& value,
|
| + void* context) {
|
| + return static_cast<uint32_t>(value.size());
|
| +}
|
| +
|
| +// static
|
| +const base::ListValue&
|
| +StructTraits<arc::mojom::BluetoothSdpAttributeLayer2,
|
| + bluez::BluetoothServiceAttributeValueBlueZ>::
|
| + value(const bluez::BluetoothServiceAttributeValueBlueZ& value,
|
| + void* context) {
|
| + return *reinterpret_cast<base::ListValue*>(context);
|
| +}
|
| +
|
| +// static
|
| +bool StructTraits<arc::mojom::BluetoothSdpAttributeLayer2,
|
| + 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:
|
| + 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::BluetoothSdpAttributeLayer1,
|
| + bluez::BluetoothServiceAttributeValueBlueZ>::
|
| + SetUpContext(const bluez::BluetoothServiceAttributeValueBlueZ& value) {
|
| + return SetUpContextForAttributeValue(value);
|
| +}
|
| +
|
| +// static
|
| +void StructTraits<arc::mojom::BluetoothSdpAttributeLayer1,
|
| + bluez::BluetoothServiceAttributeValueBlueZ>::
|
| + TearDownContext(const bluez::BluetoothServiceAttributeValueBlueZ& value,
|
| + void* context) {
|
| + TearDownContextForAttributeValue(value, context);
|
| +}
|
| +
|
| +// static
|
| +bluez::BluetoothServiceAttributeValueBlueZ::Type
|
| +StructTraits<arc::mojom::BluetoothSdpAttributeLayer1,
|
| + bluez::BluetoothServiceAttributeValueBlueZ>::
|
| + type(const bluez::BluetoothServiceAttributeValueBlueZ& value,
|
| + void* context) {
|
| + return value.type();
|
| +}
|
| +
|
| +// static
|
| +uint32_t StructTraits<arc::mojom::BluetoothSdpAttributeLayer1,
|
| + bluez::BluetoothServiceAttributeValueBlueZ>::
|
| + type_size(const bluez::BluetoothServiceAttributeValueBlueZ& value,
|
| + void* context) {
|
| + return static_cast<uint32_t>(value.size());
|
| +}
|
| +
|
| +// static
|
| +const base::ListValue&
|
| +StructTraits<arc::mojom::BluetoothSdpAttributeLayer1,
|
| + bluez::BluetoothServiceAttributeValueBlueZ>::
|
| + value(const bluez::BluetoothServiceAttributeValueBlueZ& value,
|
| + void* context) {
|
| + return *reinterpret_cast<base::ListValue*>(context);
|
| +}
|
| +
|
| +// static
|
| +const bluez::BluetoothServiceAttributeValueBlueZ::Sequence&
|
| +StructTraits<arc::mojom::BluetoothSdpAttributeLayer1,
|
| + bluez::BluetoothServiceAttributeValueBlueZ>::
|
| + sequence(const bluez::BluetoothServiceAttributeValueBlueZ& value,
|
| + void* context) {
|
| + return *reinterpret_cast<
|
| + bluez::BluetoothServiceAttributeValueBlueZ::Sequence*>(context);
|
| +}
|
| +
|
| +// static
|
| +bool StructTraits<arc::mojom::BluetoothSdpAttributeLayer1,
|
| + 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:
|
| + 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()));
|
| + break;
|
| + }
|
| + default:
|
| + return false;
|
| + }
|
| + return true;
|
| +}
|
| +
|
| +// static
|
| +void* StructTraits<arc::mojom::BluetoothSdpAttribute,
|
| + bluez::BluetoothServiceAttributeValueBlueZ>::
|
| + SetUpContext(const bluez::BluetoothServiceAttributeValueBlueZ& value) {
|
| + return SetUpContextForAttributeValue(value);
|
| +}
|
| +
|
| +// static
|
| +void StructTraits<arc::mojom::BluetoothSdpAttribute,
|
| + bluez::BluetoothServiceAttributeValueBlueZ>::
|
| + TearDownContext(const bluez::BluetoothServiceAttributeValueBlueZ& value,
|
| + void* context) {
|
| + TearDownContextForAttributeValue(value, context);
|
| +}
|
| +
|
| +// static
|
| +bluez::BluetoothServiceAttributeValueBlueZ::Type
|
| +StructTraits<arc::mojom::BluetoothSdpAttribute,
|
| + bluez::BluetoothServiceAttributeValueBlueZ>::
|
| + type(const bluez::BluetoothServiceAttributeValueBlueZ& value,
|
| + void* context) {
|
| + return value.type();
|
| +}
|
| +
|
| +// static
|
| +uint32_t StructTraits<arc::mojom::BluetoothSdpAttribute,
|
| + bluez::BluetoothServiceAttributeValueBlueZ>::
|
| + type_size(const bluez::BluetoothServiceAttributeValueBlueZ& value,
|
| + void* context) {
|
| + return static_cast<uint32_t>(value.size());
|
| +}
|
| +
|
| +// static
|
| +const base::ListValue&
|
| +StructTraits<arc::mojom::BluetoothSdpAttribute,
|
| + bluez::BluetoothServiceAttributeValueBlueZ>::
|
| + value(const bluez::BluetoothServiceAttributeValueBlueZ& value,
|
| + void* context) {
|
| + return *reinterpret_cast<base::ListValue*>(context);
|
| +}
|
| +
|
| +// static
|
| +const bluez::BluetoothServiceAttributeValueBlueZ::Sequence&
|
| +StructTraits<arc::mojom::BluetoothSdpAttribute,
|
| + bluez::BluetoothServiceAttributeValueBlueZ>::
|
| + sequence(const bluez::BluetoothServiceAttributeValueBlueZ& value,
|
| + void* context) {
|
| + return *reinterpret_cast<
|
| + bluez::BluetoothServiceAttributeValueBlueZ::Sequence*>(context);
|
| +}
|
| +
|
| +// static
|
| +bool StructTraits<arc::mojom::BluetoothSdpAttribute,
|
| + 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:
|
| + 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()));
|
| + break;
|
| + }
|
| + default:
|
| + return false;
|
| + }
|
| + return true;
|
| +}
|
| +
|
| +// static
|
| +void* StructTraits<arc::mojom::BluetoothSdpRecord,
|
| + bluez::BluetoothServiceRecordBlueZ>::
|
| + SetUpContext(const bluez::BluetoothServiceRecordBlueZ& value) {
|
| + std::map<uint16_t, bluez::BluetoothServiceAttributeValueBlueZ>* attributes =
|
| + 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::BluetoothSdpRecord,
|
| + 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::BluetoothSdpRecord,
|
| + bluez::BluetoothServiceRecordBlueZ>::
|
| + attrs(const bluez::BluetoothServiceRecordBlueZ& value, void* context) {
|
| + return *reinterpret_cast<
|
| + std::map<uint16_t, bluez::BluetoothServiceAttributeValueBlueZ>*>(context);
|
| +}
|
| +
|
| +// static
|
| +bool StructTraits<arc::mojom::BluetoothSdpRecord,
|
| + 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
|
|
|