Chromium Code Reviews| Index: device/bluetooth/bluetooth_device.cc |
| diff --git a/device/bluetooth/bluetooth_device.cc b/device/bluetooth/bluetooth_device.cc |
| index 6ee5ade8ba51bf27896e0c599ce75dd0976ee105..ac172a1f9f8cfb557eac372866eb5f700a7ffaa9 100644 |
| --- a/device/bluetooth/bluetooth_device.cc |
| +++ b/device/bluetooth/bluetooth_device.cc |
| @@ -4,12 +4,15 @@ |
| #include "device/bluetooth/bluetooth_device.h" |
| +#include <limits> |
| #include <string> |
| #include "base/stl_util.h" |
| #include "base/strings/string_util.h" |
| #include "base/strings/utf_string_conversions.h" |
| #include "base/values.h" |
| +#include "device/bluetooth/bluetooth_adapter.h" |
| +#include "device/bluetooth/bluetooth_gatt_connection.h" |
| #include "device/bluetooth/bluetooth_gatt_service.h" |
| #include "grit/bluetooth_strings.h" |
| #include "ui/base/l10n/l10n_util.h" |
| @@ -21,6 +24,7 @@ BluetoothDevice::BluetoothDevice(BluetoothAdapter* adapter) |
| BluetoothDevice::~BluetoothDevice() { |
| STLDeleteValues(&gatt_services_); |
| + DidDisconnectGatt(); |
| } |
| BluetoothDevice::ConnectionInfo::ConnectionInfo() |
| @@ -200,6 +204,18 @@ bool BluetoothDevice::IsTrustable() const { |
| return false; |
| } |
| +void BluetoothDevice::CreateGattConnection( |
| + const GattConnectionCallback& callback, |
| + const ConnectErrorCallback& error_callback) { |
| + create_gatt_connection_success_callbacks_.push_back(callback); |
| + create_gatt_connection_error_callbacks_.push_back(error_callback); |
| + |
| + if (IsGattConnected()) |
| + DidConnectGatt(); |
| + |
| + CreateGattConnectionImpl(); |
| +} |
| + |
| std::vector<BluetoothGattService*> |
| BluetoothDevice::GetGattServices() const { |
| std::vector<BluetoothGattService*> services; |
| @@ -273,6 +289,51 @@ BluetoothDevice::UUIDList BluetoothDevice::GetServiceDataUUIDs() const { |
| return uuids; |
| } |
| +void BluetoothDevice::DidConnectGatt() { |
| + for (const auto& callback : create_gatt_connection_success_callbacks_) { |
| + callback.Run( |
| + make_scoped_ptr(new BluetoothGattConnection(adapter_, GetAddress()))); |
| + } |
| + create_gatt_connection_success_callbacks_.clear(); |
| + create_gatt_connection_error_callbacks_.clear(); |
| +} |
| + |
| +void BluetoothDevice::DidFailToConnectGatt(ConnectErrorCode error) { |
| + for (const auto& error_callback : create_gatt_connection_error_callbacks_) |
| + error_callback.Run(error); |
| + create_gatt_connection_success_callbacks_.clear(); |
| + create_gatt_connection_error_callbacks_.clear(); |
| +} |
| + |
| +void BluetoothDevice::DidDisconnectGatt() { |
| + // Pending calls to connect GATT are not expected, if they were then |
| + // DidFailToConnectGatt should be called. But in case callbacks exist |
| + // flush them to ensure a consistent state. |
| + if (create_gatt_connection_error_callbacks_.size() > 0) { |
| + VLOG(1) << "Unexpected / unexplained DidDisconnectGatt call while " |
| + "create_gatt_connection_error_callbacks_ are pending."; |
| + } |
| + DidFailToConnectGatt(ERROR_FAILED); |
| + |
| + // Invalidate all BluetoothGattConnection objects. |
| + for (auto connection : gatt_connections_) { |
|
Jeffrey Yasskin
2015/09/16 00:45:39
I think you need to gatt_connections_.clear() sinc
Jeffrey Yasskin
2015/09/16 00:45:39
'auto' often causes extra copies in range-based fo
scheib
2015/09/16 21:19:07
Done.
scheib
2015/09/16 21:19:08
Done.
|
| + connection->InvalidateConnectionReference(); |
| + } |
| +} |
| + |
| +void BluetoothDevice::AddGattConnection(BluetoothGattConnection* connection) { |
| + auto result = gatt_connections_.insert(connection); |
| + DCHECK(result.second); // Check insert happened; there was no duplicate. |
| +} |
| + |
| +void BluetoothDevice::RemoveGattConnection( |
| + BluetoothGattConnection* connection) { |
| + size_t erased_count = gatt_connections_.erase(connection); |
| + DCHECK(erased_count); |
| + if (gatt_connections_.size() == 0) |
| + DisconnectGatt(); |
| +} |
| + |
| void BluetoothDevice::ClearServiceData() { services_data_->Clear(); } |
| void BluetoothDevice::SetServiceData(BluetoothUUID serviceUUID, |