Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2162)

Unified Diff: device/bluetooth/bluetooth_device.cc

Issue 1292263002: bluetooth: Create base class BluetoothDevice::CreateGattConnection impl. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bt-adapter-
Patch Set: fix ProximityAuthBluetoothLowEnergyConnectionTest Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « device/bluetooth/bluetooth_device.h ('k') | device/bluetooth/bluetooth_device_android.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: device/bluetooth/bluetooth_device.cc
diff --git a/device/bluetooth/bluetooth_device.cc b/device/bluetooth/bluetooth_device.cc
index 6ee5ade8ba51bf27896e0c599ce75dd0976ee105..34b2260059f1895a515b2dc7d127d89b7f5f1a48 100644
--- a/device/bluetooth/bluetooth_device.cc
+++ b/device/bluetooth/bluetooth_device.cc
@@ -10,6 +10,8 @@
#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 +23,7 @@ BluetoothDevice::BluetoothDevice(BluetoothAdapter* adapter)
BluetoothDevice::~BluetoothDevice() {
STLDeleteValues(&gatt_services_);
+ DidDisconnectGatt();
}
BluetoothDevice::ConnectionInfo::ConnectionInfo()
@@ -200,6 +203,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 +288,52 @@ 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 (BluetoothGattConnection* connection : gatt_connections_) {
+ connection->InvalidateConnectionReference();
+ }
+ gatt_connections_.clear();
+}
+
+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,
« no previous file with comments | « device/bluetooth/bluetooth_device.h ('k') | device/bluetooth/bluetooth_device_android.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698