| 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 <memory> | 8 #include <memory> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "components/cryptauth/bluetooth_throttler.h" |
| 17 #include "components/cryptauth/connection.h" |
| 18 #include "components/cryptauth/connection_finder.h" |
| 19 #include "components/cryptauth/connection_observer.h" |
| 16 #include "components/cryptauth/remote_device.h" | 20 #include "components/cryptauth/remote_device.h" |
| 17 #include "components/proximity_auth/connection.h" | |
| 18 #include "components/proximity_auth/connection_finder.h" | |
| 19 #include "components/proximity_auth/connection_observer.h" | |
| 20 #include "device/bluetooth/bluetooth_adapter.h" | 21 #include "device/bluetooth/bluetooth_adapter.h" |
| 21 #include "device/bluetooth/bluetooth_device.h" | 22 #include "device/bluetooth/bluetooth_device.h" |
| 22 #include "device/bluetooth/bluetooth_discovery_session.h" | 23 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 23 #include "device/bluetooth/bluetooth_gatt_connection.h" | 24 #include "device/bluetooth/bluetooth_gatt_connection.h" |
| 24 | 25 |
| 25 namespace proximity_auth { | 26 namespace proximity_auth { |
| 26 | 27 |
| 27 class BluetoothLowEnergyDeviceWhitelist; | 28 class BluetoothLowEnergyDeviceWhitelist; |
| 28 class BluetoothThrottler; | |
| 29 | 29 |
| 30 // This ConnectionFinder implementation is specialized in finding a Bluetooth | 30 // This cryptauth::ConnectionFinder implementation is specialized in finding a |
| 31 // Bluetooth |
| 31 // Low Energy remote device. | 32 // Low Energy remote device. |
| 32 class BluetoothLowEnergyConnectionFinder | 33 class BluetoothLowEnergyConnectionFinder |
| 33 : public ConnectionFinder, | 34 : public cryptauth::ConnectionFinder, |
| 34 public ConnectionObserver, | 35 public cryptauth::ConnectionObserver, |
| 35 public device::BluetoothAdapter::Observer { | 36 public device::BluetoothAdapter::Observer { |
| 36 public: | 37 public: |
| 37 enum FinderStrategy { FIND_PAIRED_DEVICE, FIND_ANY_DEVICE }; | 38 enum FinderStrategy { FIND_PAIRED_DEVICE, FIND_ANY_DEVICE }; |
| 38 | 39 |
| 39 // Finds (and connects) to a Bluetooth low energy device. There are two | 40 // Finds (and connects) to a Bluetooth low energy device. There are two |
| 40 // possible search strategies depending on |finder_strategy|: | 41 // possible search strategies depending on |finder_strategy|: |
| 41 // (i) |FIND_PAIRED_DEVICE| searches for the unique paired bluetooth | 42 // (i) |FIND_PAIRED_DEVICE| searches for the unique paired bluetooth |
| 42 // |remote_device|; | 43 // |remote_device|; |
| 43 // (ii) |FIND_ANY_DEVICE| searches for any device advertising | 44 // (ii) |FIND_ANY_DEVICE| searches for any device advertising |
| 44 // |remote_service_uuid|. | 45 // |remote_service_uuid|. |
| 45 // | 46 // |
| 46 // |remote_device|: The BLE remote device. |remote_device.bluetooth_adress| | 47 // |remote_device|: The BLE remote device. |remote_device.bluetooth_adress| |
| 47 // should be empty when |has_public_bluetooth_address| is false. | 48 // should be empty when |has_public_bluetooth_address| is false. |
| 48 // |remote_service_uuid|: The UUID of the service used to send/receive data in | 49 // |remote_service_uuid|: The UUID of the service used to send/receive data in |
| 49 // remote device. | 50 // remote device. |
| 50 // |bluetooth_throttler|: The reconnection throttler. | 51 // |bluetooth_throttler|: The reconnection throttler. |
| 51 // |max_number_of_tries|: Maximum number attempts to send a message before | 52 // |max_number_of_tries|: Maximum number attempts to send a message before |
| 52 // disconnecting. | 53 // disconnecting. |
| 53 // TODO(sacomoto): Remove |device_whitelist| when ProximityAuthBleSystem is | 54 // TODO(sacomoto): Remove |device_whitelist| when ProximityAuthBleSystem is |
| 54 // not needed anymore. | 55 // not needed anymore. |
| 55 BluetoothLowEnergyConnectionFinder( | 56 BluetoothLowEnergyConnectionFinder( |
| 56 const cryptauth::RemoteDevice remote_device, | 57 const cryptauth::RemoteDevice remote_device, |
| 57 const std::string& remote_service_uuid, | 58 const std::string& remote_service_uuid, |
| 58 const FinderStrategy finder_strategy, | 59 const FinderStrategy finder_strategy, |
| 59 const BluetoothLowEnergyDeviceWhitelist* device_whitelist, | 60 const BluetoothLowEnergyDeviceWhitelist* device_whitelist, |
| 60 BluetoothThrottler* bluetooth_throttler, | 61 cryptauth::BluetoothThrottler* bluetooth_throttler, |
| 61 int max_number_of_tries); | 62 int max_number_of_tries); |
| 62 | 63 |
| 63 ~BluetoothLowEnergyConnectionFinder() override; | 64 ~BluetoothLowEnergyConnectionFinder() override; |
| 64 | 65 |
| 65 // Finds a connection to the remote device. | 66 // Finds a connection to the remote device. |
| 66 void Find(const ConnectionCallback& connection_callback) override; | 67 void Find(const cryptauth::ConnectionFinder::ConnectionCallback& |
| 68 connection_callback) override; |
| 67 | 69 |
| 68 // proximity_auth::ConnectionObserver: | 70 // cryptauth::ConnectionObserver: |
| 69 void OnConnectionStatusChanged(Connection* connection, | 71 void OnConnectionStatusChanged( |
| 70 Connection::Status old_status, | 72 cryptauth::Connection* connection, |
| 71 Connection::Status new_status) override; | 73 cryptauth::Connection::Status old_status, |
| 74 cryptauth::Connection::Status new_status) override; |
| 72 | 75 |
| 73 // device::BluetoothAdapter::Observer: | 76 // device::BluetoothAdapter::Observer: |
| 74 void AdapterPoweredChanged(device::BluetoothAdapter* adapter, | 77 void AdapterPoweredChanged(device::BluetoothAdapter* adapter, |
| 75 bool powered) override; | 78 bool powered) override; |
| 76 void DeviceAdded(device::BluetoothAdapter* adapter, | 79 void DeviceAdded(device::BluetoothAdapter* adapter, |
| 77 device::BluetoothDevice* device) override; | 80 device::BluetoothDevice* device) override; |
| 78 void DeviceChanged(device::BluetoothAdapter* adapter, | 81 void DeviceChanged(device::BluetoothAdapter* adapter, |
| 79 device::BluetoothDevice* device) override; | 82 device::BluetoothDevice* device) override; |
| 80 | 83 |
| 81 protected: | 84 protected: |
| 82 // Creates a proximity_auth::Connection with the device given by | 85 // Creates a proximity_auth::Connection with the device given by |
| 83 // |device_address|. Exposed for testing. | 86 // |device_address|. Exposed for testing. |
| 84 virtual std::unique_ptr<Connection> CreateConnection( | 87 virtual std::unique_ptr<cryptauth::Connection> CreateConnection( |
| 85 const std::string& device_address); | 88 const std::string& device_address); |
| 86 | 89 |
| 87 private: | 90 private: |
| 88 // Callback to be called when the Bluetooth adapter is initialized. | 91 // Callback to be called when the Bluetooth adapter is initialized. |
| 89 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter); | 92 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter); |
| 90 | 93 |
| 91 // Checks if |remote_device| contains |remote_service_uuid| and creates a | 94 // Checks if |remote_device| contains |remote_service_uuid| and creates a |
| 92 // connection in that case. | 95 // connection in that case. |
| 93 void HandleDeviceUpdated(device::BluetoothDevice* remote_device); | 96 void HandleDeviceUpdated(device::BluetoothDevice* remote_device); |
| 94 | 97 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 const FinderStrategy finder_strategy_; | 137 const FinderStrategy finder_strategy_; |
| 135 | 138 |
| 136 // Devices in |device_whitelist_| don't need to have |remote_service_uuid_| | 139 // Devices in |device_whitelist_| don't need to have |remote_service_uuid_| |
| 137 // cached or advertised. Not owned, must outlive this instance. | 140 // cached or advertised. Not owned, must outlive this instance. |
| 138 // TODO(sacomoto): Remove |device_whitelist_| when ProximityAuthBleSystem is | 141 // TODO(sacomoto): Remove |device_whitelist_| when ProximityAuthBleSystem is |
| 139 // not needed anymore. | 142 // not needed anymore. |
| 140 const BluetoothLowEnergyDeviceWhitelist* device_whitelist_; | 143 const BluetoothLowEnergyDeviceWhitelist* device_whitelist_; |
| 141 | 144 |
| 142 // Throttles repeated connection attempts to the same device. This is a | 145 // Throttles repeated connection attempts to the same device. This is a |
| 143 // workaround for crbug.com/508919. Not owned, must outlive this instance. | 146 // workaround for crbug.com/508919. Not owned, must outlive this instance. |
| 144 BluetoothThrottler* bluetooth_throttler_; | 147 cryptauth::BluetoothThrottler* bluetooth_throttler_; |
| 145 | 148 |
| 146 // The Bluetooth adapter over which the Bluetooth connection will be made. | 149 // The Bluetooth adapter over which the Bluetooth connection will be made. |
| 147 scoped_refptr<device::BluetoothAdapter> adapter_; | 150 scoped_refptr<device::BluetoothAdapter> adapter_; |
| 148 | 151 |
| 149 // The discovery session associated to this object. | 152 // The discovery session associated to this object. |
| 150 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session_; | 153 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session_; |
| 151 | 154 |
| 152 // The connection with |remote_device|. | 155 // The connection with |remote_device|. |
| 153 std::unique_ptr<Connection> connection_; | 156 std::unique_ptr<cryptauth::Connection> connection_; |
| 154 | 157 |
| 155 // Callback called when the connection is established. | 158 // Callback called when the connection is established. |
| 156 // device::BluetoothDevice::GattConnectionCallback connection_callback_; | 159 cryptauth::ConnectionFinder::ConnectionCallback connection_callback_; |
| 157 ConnectionCallback connection_callback_; | |
| 158 | 160 |
| 159 // BluetoothLowEnergyConnection parameter. | 161 // BluetoothLowEnergyConnection parameter. |
| 160 int max_number_of_tries_; | 162 int max_number_of_tries_; |
| 161 | 163 |
| 162 base::WeakPtrFactory<BluetoothLowEnergyConnectionFinder> weak_ptr_factory_; | 164 base::WeakPtrFactory<BluetoothLowEnergyConnectionFinder> weak_ptr_factory_; |
| 163 | 165 |
| 164 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyConnectionFinder); | 166 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyConnectionFinder); |
| 165 }; | 167 }; |
| 166 | 168 |
| 167 } // namespace proximity_auth | 169 } // namespace proximity_auth |
| 168 | 170 |
| 169 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_CONNECTION_FINDER_H | 171 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_CONNECTION_FINDER_H |
| OLD | NEW |