Index: device/bluetooth/dbus/bluetooth_gatt_characteristic_service_provider.cc |
diff --git a/device/bluetooth/dbus/bluetooth_gatt_characteristic_service_provider.cc b/device/bluetooth/dbus/bluetooth_gatt_characteristic_service_provider.cc |
index e69a52e222111e6eb7b5bc8008797c8fd637fdc7..8caf471a88594f2e2acd680615427370c129e638 100644 |
--- a/device/bluetooth/dbus/bluetooth_gatt_characteristic_service_provider.cc |
+++ b/device/bluetooth/dbus/bluetooth_gatt_characteristic_service_provider.cc |
@@ -6,17 +6,14 @@ |
#include <stddef.h> |
-#include <memory> |
-#include <utility> |
- |
#include "base/bind.h" |
+#include "base/callback.h" |
#include "base/logging.h" |
-#include "base/macros.h" |
+#include "base/memory/ref_counted.h" |
#include "base/memory/weak_ptr.h" |
#include "base/strings/string_util.h" |
#include "base/threading/platform_thread.h" |
#include "dbus/exported_object.h" |
-#include "dbus/message.h" |
#include "device/bluetooth/dbus/bluez_dbus_manager.h" |
#include "device/bluetooth/dbus/fake_bluetooth_gatt_characteristic_service_provider.h" |
#include "third_party/cros_system_api/dbus/service_constants.h" |
@@ -37,7 +34,8 @@ class BluetoothGattCharacteristicServiceProviderImpl |
BluetoothGattCharacteristicServiceProviderImpl( |
dbus::Bus* bus, |
const dbus::ObjectPath& object_path, |
- Delegate* delegate, |
+ std::unique_ptr<BluetoothGattServiceBlueZ::AttributeValueDelegate> |
+ delegate, |
const std::string& uuid, |
const std::vector<std::string>& flags, |
const std::vector<std::string>& permissions, |
@@ -45,7 +43,7 @@ class BluetoothGattCharacteristicServiceProviderImpl |
: origin_thread_id_(base::PlatformThread::CurrentId()), |
uuid_(uuid), |
bus_(bus), |
- delegate_(delegate), |
+ delegate_(std::move(delegate)), |
object_path_(object_path), |
service_path_(service_path), |
weak_ptr_factory_(this) { |
@@ -162,7 +160,7 @@ class BluetoothGattCharacteristicServiceProviderImpl |
// If getting the "Value" property, obtain the value from the delegate. |
if (property_name == bluetooth_gatt_characteristic::kValueProperty) { |
DCHECK(delegate_); |
- delegate_->GetCharacteristicValue( |
+ delegate_->GetValue( |
base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::OnGet, |
weak_ptr_factory_.GetWeakPtr(), method_call, |
response_sender), |
@@ -264,7 +262,7 @@ class BluetoothGattCharacteristicServiceProviderImpl |
// Pass the set request onto the delegate. |
std::vector<uint8_t> value(bytes, bytes + length); |
DCHECK(delegate_); |
- delegate_->SetCharacteristicValue( |
+ delegate_->SetValue( |
value, |
base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::OnSet, |
weak_ptr_factory_.GetWeakPtr(), method_call, |
@@ -307,7 +305,7 @@ class BluetoothGattCharacteristicServiceProviderImpl |
// Try to obtain the value from the delegate. We will construct the |
// response in the success callback. |
DCHECK(delegate_); |
- delegate_->GetCharacteristicValue( |
+ delegate_->GetValue( |
base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::OnGetAll, |
weak_ptr_factory_.GetWeakPtr(), method_call, |
response_sender), |
@@ -336,11 +334,18 @@ class BluetoothGattCharacteristicServiceProviderImpl |
std::unique_ptr<dbus::Response> response = |
dbus::Response::FromMethodCall(method_call); |
dbus::MessageWriter writer(response.get()); |
+ WriteProperties(&writer, &value); |
+ response_sender.Run(std::move(response)); |
+ } |
+ |
+ // Writes an array of the service's properties into the provided writer. |
+ void WriteProperties(dbus::MessageWriter* writer, |
+ const std::vector<uint8_t>* value) override { |
dbus::MessageWriter array_writer(NULL); |
dbus::MessageWriter dict_entry_writer(NULL); |
dbus::MessageWriter variant_writer(NULL); |
- writer.OpenArray("{sv}", &array_writer); |
+ writer->OpenArray("{sv}", &array_writer); |
array_writer.OpenDictEntry(&dict_entry_writer); |
dict_entry_writer.AppendString( |
@@ -354,20 +359,20 @@ class BluetoothGattCharacteristicServiceProviderImpl |
dict_entry_writer.AppendVariantOfObjectPath(service_path_); |
array_writer.CloseContainer(&dict_entry_writer); |
- array_writer.OpenDictEntry(&dict_entry_writer); |
- dict_entry_writer.AppendString( |
- bluetooth_gatt_characteristic::kValueProperty); |
- dict_entry_writer.OpenVariant("ay", &variant_writer); |
- variant_writer.AppendArrayOfBytes(value.data(), value.size()); |
- dict_entry_writer.CloseContainer(&variant_writer); |
- array_writer.CloseContainer(&dict_entry_writer); |
+ if (value) { |
+ array_writer.OpenDictEntry(&dict_entry_writer); |
+ dict_entry_writer.AppendString( |
+ bluetooth_gatt_characteristic::kValueProperty); |
+ dict_entry_writer.OpenVariant("ay", &variant_writer); |
+ variant_writer.AppendArrayOfBytes(value->data(), value->size()); |
+ dict_entry_writer.CloseContainer(&variant_writer); |
+ array_writer.CloseContainer(&dict_entry_writer); |
+ } |
// TODO(armansito): Process Flags & Permissions properties. |
- writer.CloseContainer(&array_writer); |
- |
- response_sender.Run(std::move(response)); |
- } |
+ writer->CloseContainer(&array_writer); |
+ }; |
// Called by the Delegate in response to a successful method call to get the |
// characteristic value. |
@@ -407,6 +412,8 @@ class BluetoothGattCharacteristicServiceProviderImpl |
response_sender.Run(std::move(error_response)); |
} |
+ const dbus::ObjectPath& object_path() const override { return object_path_; } |
+ |
// Origin thread (i.e. the UI thread in production). |
base::PlatformThreadId origin_thread_id_; |
@@ -420,7 +427,7 @@ class BluetoothGattCharacteristicServiceProviderImpl |
// Incoming methods to get and set the "Value" property are passed on to the |
// delegate and callbacks passed to generate a reply. |delegate_| is generally |
// the object that owns this one and must outlive it. |
- Delegate* delegate_; |
+ std::unique_ptr<BluetoothGattServiceBlueZ::AttributeValueDelegate> delegate_; |
// D-Bus object path of object we are exporting, kept so we can unregister |
// again in our destructor. |
@@ -454,17 +461,18 @@ BluetoothGattCharacteristicServiceProvider* |
BluetoothGattCharacteristicServiceProvider::Create( |
dbus::Bus* bus, |
const dbus::ObjectPath& object_path, |
- Delegate* delegate, |
+ std::unique_ptr<BluetoothGattServiceBlueZ::AttributeValueDelegate> delegate, |
const std::string& uuid, |
const std::vector<std::string>& flags, |
const std::vector<std::string>& permissions, |
const dbus::ObjectPath& service_path) { |
if (!bluez::BluezDBusManager::Get()->IsUsingFakes()) { |
return new BluetoothGattCharacteristicServiceProviderImpl( |
- bus, object_path, delegate, uuid, flags, permissions, service_path); |
+ bus, object_path, std::move(delegate), uuid, flags, permissions, |
+ service_path); |
} |
return new FakeBluetoothGattCharacteristicServiceProvider( |
- object_path, delegate, uuid, flags, permissions, service_path); |
+ object_path, std::move(delegate), uuid, flags, permissions, service_path); |
} |
} // namespace bluez |