Chromium Code Reviews| 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..79fcc0248a52de96f15e26705ce45cdf6b084d5d 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,7 @@ class BluetoothGattCharacteristicServiceProviderImpl |
| BluetoothGattCharacteristicServiceProviderImpl( |
| dbus::Bus* bus, |
| const dbus::ObjectPath& object_path, |
| - Delegate* delegate, |
| + std::unique_ptr<BluetoothGattAttributeValueDelegate> delegate, |
| const std::string& uuid, |
| const std::vector<std::string>& flags, |
| const std::vector<std::string>& permissions, |
| @@ -45,7 +42,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 +159,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 +261,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 +304,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 +333,18 @@ class BluetoothGattCharacteristicServiceProviderImpl |
| std::unique_ptr<dbus::Response> response = |
| dbus::Response::FromMethodCall(method_call); |
|
scheib
2016/04/28 04:54:28
Are protections against the callback being called
rkc
2016/04/28 18:13:23
I'm pretty certain that our DBus client code has p
|
| 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 +358,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 +411,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 +426,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<BluetoothGattAttributeValueDelegate> delegate_; |
| // D-Bus object path of object we are exporting, kept so we can unregister |
| // again in our destructor. |
| @@ -454,17 +460,18 @@ BluetoothGattCharacteristicServiceProvider* |
| BluetoothGattCharacteristicServiceProvider::Create( |
| dbus::Bus* bus, |
| const dbus::ObjectPath& object_path, |
| - Delegate* delegate, |
| + std::unique_ptr<BluetoothGattAttributeValueDelegate> 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 |