OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "device/bluetooth/bluetooth_device.h" | 5 #include "device/bluetooth/bluetooth_device.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "device/bluetooth/bluetooth_adapter.h" | 15 #include "device/bluetooth/bluetooth_adapter.h" |
16 #include "device/bluetooth/bluetooth_gatt_connection.h" | 16 #include "device/bluetooth/bluetooth_gatt_connection.h" |
17 #include "device/bluetooth/bluetooth_remote_gatt_service.h" | 17 #include "device/bluetooth/bluetooth_remote_gatt_service.h" |
18 #include "grit/bluetooth_strings.h" | 18 #include "grit/bluetooth_strings.h" |
19 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
20 | 20 |
21 namespace device { | 21 namespace device { |
22 | 22 |
23 BluetoothDevice::BluetoothDevice(BluetoothAdapter* adapter) | 23 BluetoothDevice::BluetoothDevice(BluetoothAdapter* adapter) |
24 : adapter_(adapter), | 24 : adapter_(adapter), |
25 gatt_services_discovery_complete_(false), | 25 gatt_services_discovery_complete_(false), |
26 services_data_(new base::DictionaryValue()) {} | 26 services_data_(new base::DictionaryValue()) {} |
27 | 27 |
28 BluetoothDevice::~BluetoothDevice() { | 28 BluetoothDevice::~BluetoothDevice() { |
29 DidDisconnectGatt(); | 29 for (BluetoothGattConnection* connection : gatt_connections_) { |
| 30 connection->InvalidateConnectionReference(); |
| 31 } |
30 } | 32 } |
31 | 33 |
32 BluetoothDevice::ConnectionInfo::ConnectionInfo() | 34 BluetoothDevice::ConnectionInfo::ConnectionInfo() |
33 : rssi(kUnknownPower), | 35 : rssi(kUnknownPower), |
34 transmit_power(kUnknownPower), | 36 transmit_power(kUnknownPower), |
35 max_transmit_power(kUnknownPower) {} | 37 max_transmit_power(kUnknownPower) {} |
36 | 38 |
37 BluetoothDevice::ConnectionInfo::ConnectionInfo( | 39 BluetoothDevice::ConnectionInfo::ConnectionInfo( |
38 int rssi, int transmit_power, int max_transmit_power) | 40 int rssi, int transmit_power, int max_transmit_power) |
39 : rssi(rssi), | 41 : rssi(rssi), |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 return uuids; | 330 return uuids; |
329 } | 331 } |
330 | 332 |
331 void BluetoothDevice::DidConnectGatt() { | 333 void BluetoothDevice::DidConnectGatt() { |
332 for (const auto& callback : create_gatt_connection_success_callbacks_) { | 334 for (const auto& callback : create_gatt_connection_success_callbacks_) { |
333 callback.Run( | 335 callback.Run( |
334 base::WrapUnique(new BluetoothGattConnection(adapter_, GetAddress()))); | 336 base::WrapUnique(new BluetoothGattConnection(adapter_, GetAddress()))); |
335 } | 337 } |
336 create_gatt_connection_success_callbacks_.clear(); | 338 create_gatt_connection_success_callbacks_.clear(); |
337 create_gatt_connection_error_callbacks_.clear(); | 339 create_gatt_connection_error_callbacks_.clear(); |
| 340 GetAdapter()->NotifyDeviceChanged(this); |
338 } | 341 } |
339 | 342 |
340 void BluetoothDevice::DidFailToConnectGatt(ConnectErrorCode error) { | 343 void BluetoothDevice::DidFailToConnectGatt(ConnectErrorCode error) { |
341 // Connection request should only be made if there are no active | 344 // Connection request should only be made if there are no active |
342 // connections. | 345 // connections. |
343 DCHECK(gatt_connections_.empty()); | 346 DCHECK(gatt_connections_.empty()); |
344 | 347 |
345 for (const auto& error_callback : create_gatt_connection_error_callbacks_) | 348 for (const auto& error_callback : create_gatt_connection_error_callbacks_) |
346 error_callback.Run(error); | 349 error_callback.Run(error); |
347 create_gatt_connection_success_callbacks_.clear(); | 350 create_gatt_connection_success_callbacks_.clear(); |
348 create_gatt_connection_error_callbacks_.clear(); | 351 create_gatt_connection_error_callbacks_.clear(); |
349 } | 352 } |
350 | 353 |
351 void BluetoothDevice::DidDisconnectGatt() { | 354 void BluetoothDevice::DidDisconnectGatt() { |
352 // Pending calls to connect GATT are not expected, if they were then | 355 // Pending calls to connect GATT are not expected, if they were then |
353 // DidFailToConnectGatt should have been called. | 356 // DidFailToConnectGatt should have been called. |
354 DCHECK(create_gatt_connection_error_callbacks_.empty()); | 357 DCHECK(create_gatt_connection_error_callbacks_.empty()); |
355 | 358 |
356 // Invalidate all BluetoothGattConnection objects. | 359 // Invalidate all BluetoothGattConnection objects. |
357 for (BluetoothGattConnection* connection : gatt_connections_) { | 360 for (BluetoothGattConnection* connection : gatt_connections_) { |
358 connection->InvalidateConnectionReference(); | 361 connection->InvalidateConnectionReference(); |
359 } | 362 } |
360 gatt_connections_.clear(); | 363 gatt_connections_.clear(); |
| 364 GetAdapter()->NotifyDeviceChanged(this); |
361 } | 365 } |
362 | 366 |
363 void BluetoothDevice::AddGattConnection(BluetoothGattConnection* connection) { | 367 void BluetoothDevice::AddGattConnection(BluetoothGattConnection* connection) { |
364 auto result = gatt_connections_.insert(connection); | 368 auto result = gatt_connections_.insert(connection); |
365 DCHECK(result.second); // Check insert happened; there was no duplicate. | 369 DCHECK(result.second); // Check insert happened; there was no duplicate. |
366 } | 370 } |
367 | 371 |
368 void BluetoothDevice::RemoveGattConnection( | 372 void BluetoothDevice::RemoveGattConnection( |
369 BluetoothGattConnection* connection) { | 373 BluetoothGattConnection* connection) { |
370 size_t erased_count = gatt_connections_.erase(connection); | 374 size_t erased_count = gatt_connections_.erase(connection); |
(...skipping 10 matching lines...) Expand all Loading... |
381 base::BinaryValue::CreateWithCopiedBuffer(buffer, size)); | 385 base::BinaryValue::CreateWithCopiedBuffer(buffer, size)); |
382 } | 386 } |
383 | 387 |
384 void BluetoothDevice::Pair(PairingDelegate* pairing_delegate, | 388 void BluetoothDevice::Pair(PairingDelegate* pairing_delegate, |
385 const base::Closure& callback, | 389 const base::Closure& callback, |
386 const ConnectErrorCallback& error_callback) { | 390 const ConnectErrorCallback& error_callback) { |
387 NOTREACHED(); | 391 NOTREACHED(); |
388 } | 392 } |
389 | 393 |
390 } // namespace device | 394 } // namespace device |
OLD | NEW |