| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #ifndef COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_CONNECTION_FINDER_H | 5 #ifndef COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_CONNECTION_FINDER_H |
| 6 #define COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_CONNECTION_FINDER_H | 6 #define COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_CONNECTION_FINDER_H |
| 7 | 7 |
| 8 #include <set> |
| 8 #include <string> | 9 #include <string> |
| 9 | 10 |
| 10 #include "base/callback.h" | 11 #include "base/callback.h" |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 15 #include "components/proximity_auth/connection_finder.h" | 16 #include "components/proximity_auth/connection_finder.h" |
| 16 #include "device/bluetooth/bluetooth_adapter.h" | 17 #include "device/bluetooth/bluetooth_adapter.h" |
| 17 #include "device/bluetooth/bluetooth_device.h" | 18 #include "device/bluetooth/bluetooth_device.h" |
| 18 #include "device/bluetooth/bluetooth_discovery_session.h" | 19 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 19 #include "device/bluetooth/bluetooth_gatt_connection.h" | 20 #include "device/bluetooth/bluetooth_gatt_connection.h" |
| 20 | 21 |
| 21 namespace proximity_auth { | 22 namespace proximity_auth { |
| 22 | 23 |
| 23 // This ConnectionFinder implementation is specialized in finding a Bluetooth | 24 // This ConnectionFinder implementation is specialized in finding a Bluetooth |
| 24 // Low Energy remote device. | 25 // Low Energy remote device. |
| 25 class BluetoothLowEnergyConnectionFinder | 26 class BluetoothLowEnergyConnectionFinder |
| 26 : public ConnectionFinder, | 27 : public ConnectionFinder, |
| 27 public device::BluetoothAdapter::Observer { | 28 public device::BluetoothAdapter::Observer { |
| 28 public: | 29 public: |
| 29 BluetoothLowEnergyConnectionFinder(const std::string& remote_service_uuid); | 30 BluetoothLowEnergyConnectionFinder(const std::string& remote_service_uuid); |
| 30 ~BluetoothLowEnergyConnectionFinder() override; | 31 ~BluetoothLowEnergyConnectionFinder() override; |
| 31 | 32 |
| 32 // Finds a connection to the remote device. Only the first one is functional. | 33 // Finds a connection to the remote device. Only the first one is functional. |
| 33 void Find(const device::BluetoothDevice::GattConnectionCallback& | 34 void Find(const device::BluetoothDevice::GattConnectionCallback& |
| 34 connection_callback); | 35 connection_callback); |
| 35 void Find(const ConnectionCallback& connection_callback) override; | 36 void Find(const ConnectionCallback& connection_callback) override; |
| 36 | 37 |
| 38 // Closes the connection and forgets the device. |
| 39 void CloseConnection(scoped_ptr<device::BluetoothGattConnection> connection); |
| 40 |
| 37 protected: | 41 protected: |
| 38 // device::BluetoothAdapter::Observer: | 42 // device::BluetoothAdapter::Observer: |
| 39 void DeviceAdded(device::BluetoothAdapter* adapter, | 43 void DeviceAdded(device::BluetoothAdapter* adapter, |
| 40 device::BluetoothDevice* device) override; | 44 device::BluetoothDevice* device) override; |
| 45 void DeviceChanged(device::BluetoothAdapter* adapter, |
| 46 device::BluetoothDevice* device) override; |
| 47 void DeviceRemoved(device::BluetoothAdapter* adapter, |
| 48 device::BluetoothDevice* device) override; |
| 41 | 49 |
| 42 private: | 50 private: |
| 43 // Callback to be called when the Bluetooth adapter is initialized. | 51 // Callback to be called when the Bluetooth adapter is initialized. |
| 44 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter); | 52 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter); |
| 45 | 53 |
| 46 // Checks if |remote_device| contains |remote_service_uuid| and creates a | 54 // Checks if |remote_device| contains |remote_service_uuid| and creates a |
| 47 // connection in that case. | 55 // connection in that case. |
| 48 void HandleDeviceAdded(device::BluetoothDevice* remote_device); | 56 void HandleDeviceUpdated(device::BluetoothDevice* remote_device); |
| 49 | 57 |
| 50 // Callback called when a new discovery session is started. | 58 // Callback called when a new discovery session is started. |
| 51 void OnDiscoverySessionStarted( | 59 void OnDiscoverySessionStarted( |
| 52 scoped_ptr<device::BluetoothDiscoverySession> discovery_session); | 60 scoped_ptr<device::BluetoothDiscoverySession> discovery_session); |
| 53 | 61 |
| 54 // Callback called when there is an error starting a new discovery session. | 62 // Callback called when there is an error starting a new discovery session. |
| 55 void OnStartDiscoverySessionError(); | 63 void OnStartDiscoverySessionError(); |
| 56 | 64 |
| 57 // Starts a discovery session for |adapter_|. | 65 // Starts a discovery session for |adapter_|. |
| 58 void StartDiscoverySession(); | 66 void StartDiscoverySession(); |
| 59 | 67 |
| 60 // Callback called when |discovery_session_| is stopped. | 68 // Callback called when |discovery_session_| is stopped. |
| 61 void OnDiscoverySessionStopped(); | 69 void OnDiscoverySessionStopped(); |
| 62 | 70 |
| 63 // Callback called when there is an error stopping |discovery_session_|. | 71 // Callback called when there is an error stopping |discovery_session_|. |
| 64 void OnStopDiscoverySessionError(); | 72 void OnStopDiscoverySessionError(); |
| 65 | 73 |
| 66 // Stops the discovery session given by |discovery_session_|. | 74 // Stops the discovery session given by |discovery_session_|. |
| 67 void StopDiscoverySession(); | 75 void StopDiscoverySession(); |
| 68 | 76 |
| 69 // Checks if a service with |service_uuid| is offered by |remote_device|. | 77 // Checks if a service with |service_uuid| is offered by |remote_device|. |
| 70 bool HasService(device::BluetoothDevice* remote_device); | 78 bool HasService(device::BluetoothDevice* remote_device); |
| 71 | 79 |
| 72 // Callback called when there is an error creating the connection. | 80 // Callback called when there is an error creating the connection. |
| 73 void OnCreateConnectionError( | 81 void OnCreateConnectionError( |
| 82 std::string device_address, |
| 74 device::BluetoothDevice::ConnectErrorCode error_code); | 83 device::BluetoothDevice::ConnectErrorCode error_code); |
| 75 | 84 |
| 76 // Callback called when the connection is created. | 85 // Callback called when the connection is created. |
| 77 void OnConnectionCreated( | 86 void OnConnectionCreated( |
| 78 scoped_ptr<device::BluetoothGattConnection> connection); | 87 scoped_ptr<device::BluetoothGattConnection> connection); |
| 79 | 88 |
| 80 // Creates a GATT connection with |remote_device|, |connection_callback_| will | 89 // Creates a GATT connection with |remote_device|, |connection_callback_| will |
| 81 // be called once the connection is established. | 90 // be called once the connection is established. |
| 82 void CreateConnection(device::BluetoothDevice* remote_device); | 91 void CreateConnection(device::BluetoothDevice* remote_device); |
| 83 | 92 |
| 84 // The uuid of the service it looks for to establish a GattConnection. | 93 // The uuid of the service it looks for to establish a GattConnection. |
| 85 device::BluetoothUUID remote_service_uuid_; | 94 device::BluetoothUUID remote_service_uuid_; |
| 86 | 95 |
| 87 // The Bluetooth adapter over which the Bluetooth connection will be made. | 96 // The Bluetooth adapter over which the Bluetooth connection will be made. |
| 88 scoped_refptr<device::BluetoothAdapter> adapter_; | 97 scoped_refptr<device::BluetoothAdapter> adapter_; |
| 89 | 98 |
| 90 // The discovery session associated to this object. | 99 // The discovery session associated to this object. |
| 91 scoped_ptr<device::BluetoothDiscoverySession> discovery_session_; | 100 scoped_ptr<device::BluetoothDiscoverySession> discovery_session_; |
| 92 | 101 |
| 93 // True if there is a connection. | 102 // True if a connection was established to a remove device that has the |
| 103 // service |remote_service_uuid|. |
| 94 bool connected_; | 104 bool connected_; |
| 95 | 105 |
| 96 // Callback called when the connection is established. | 106 // Callback called when the connection is established. |
| 97 device::BluetoothDevice::GattConnectionCallback connection_callback_; | 107 device::BluetoothDevice::GattConnectionCallback connection_callback_; |
| 98 | 108 |
| 109 // The set of devices this connection finder has tried to connect to. |
| 110 std::set<device::BluetoothDevice*> pending_connections_; |
| 111 |
| 99 base::WeakPtrFactory<BluetoothLowEnergyConnectionFinder> weak_ptr_factory_; | 112 base::WeakPtrFactory<BluetoothLowEnergyConnectionFinder> weak_ptr_factory_; |
| 100 | 113 |
| 101 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyConnectionFinder); | 114 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyConnectionFinder); |
| 102 }; | 115 }; |
| 103 | 116 |
| 104 } // namespace proximity_auth | 117 } // namespace proximity_auth |
| 105 | 118 |
| 106 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_CONNECTION_FINDER_H | 119 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_CONNECTION_FINDER_H |
| OLD | NEW |