| 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_BLUETOOTH_LOW_ENERGY_CONNECTION_FINDER_H |
| 6 #define COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_CONNECTION_FINDER_H | 6 #define COMPONENTS_PROXIMITY_AUTH_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/background_eid_generator.h" |
| 16 #include "components/cryptauth/bluetooth_throttler.h" | 17 #include "components/cryptauth/bluetooth_throttler.h" |
| 17 #include "components/cryptauth/connection.h" | 18 #include "components/cryptauth/connection.h" |
| 18 #include "components/cryptauth/connection_finder.h" | 19 #include "components/cryptauth/connection_finder.h" |
| 19 #include "components/cryptauth/connection_observer.h" | 20 #include "components/cryptauth/connection_observer.h" |
| 21 #include "components/cryptauth/remote_beacon_seed_fetcher.h" |
| 20 #include "components/cryptauth/remote_device.h" | 22 #include "components/cryptauth/remote_device.h" |
| 21 #include "device/bluetooth/bluetooth_adapter.h" | 23 #include "device/bluetooth/bluetooth_adapter.h" |
| 22 #include "device/bluetooth/bluetooth_device.h" | 24 #include "device/bluetooth/bluetooth_device.h" |
| 23 #include "device/bluetooth/bluetooth_discovery_session.h" | 25 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 24 #include "device/bluetooth/bluetooth_gatt_connection.h" | 26 #include "device/bluetooth/bluetooth_gatt_connection.h" |
| 25 | 27 |
| 26 namespace proximity_auth { | 28 namespace proximity_auth { |
| 27 | 29 |
| 28 class BluetoothLowEnergyDeviceWhitelist; | |
| 29 | |
| 30 // This cryptauth::ConnectionFinder implementation is specialized in finding a | 30 // This cryptauth::ConnectionFinder implementation is specialized in finding a |
| 31 // Bluetooth | 31 // Bluetooth Low Energy remote device. We look for remote devices advertising |
| 32 // Low Energy remote device. | 32 // the expected EID for the current or nearby time intervals. |
| 33 class BluetoothLowEnergyConnectionFinder | 33 class BluetoothLowEnergyConnectionFinder |
| 34 : public cryptauth::ConnectionFinder, | 34 : public cryptauth::ConnectionFinder, |
| 35 public cryptauth::ConnectionObserver, | 35 public cryptauth::ConnectionObserver, |
| 36 public device::BluetoothAdapter::Observer { | 36 public device::BluetoothAdapter::Observer { |
| 37 public: | 37 public: |
| 38 enum FinderStrategy { FIND_PAIRED_DEVICE, FIND_ANY_DEVICE }; | 38 // Finds (and connects) to a Bluetooth low energy device, based on the EID |
| 39 | 39 // advertised by the remote device. |
| 40 // Finds (and connects) to a Bluetooth low energy device. There are two | |
| 41 // possible search strategies depending on |finder_strategy|: | |
| 42 // (i) |FIND_PAIRED_DEVICE| searches for the unique paired bluetooth | |
| 43 // |remote_device|; | |
| 44 // (ii) |FIND_ANY_DEVICE| searches for any device advertising | |
| 45 // |remote_service_uuid|. | |
| 46 // | 40 // |
| 47 // |remote_device|: The BLE remote device. |remote_device.bluetooth_adress| | 41 // |remote_device|: The BLE remote device. |
| 48 // should be empty when |has_public_bluetooth_address| is false. | 42 // |beacon_seeds|: The BeaconSeeds for the |remote_device. |
| 49 // |remote_service_uuid|: The UUID of the service used to send/receive data in | |
| 50 // remote device. | |
| 51 // |bluetooth_throttler|: The reconnection throttler. | 43 // |bluetooth_throttler|: The reconnection throttler. |
| 52 // |max_number_of_tries|: Maximum number attempts to send a message before | |
| 53 // disconnecting. | |
| 54 // TODO(sacomoto): Remove |device_whitelist| when ProximityAuthBleSystem is | |
| 55 // not needed anymore. | |
| 56 BluetoothLowEnergyConnectionFinder( | 44 BluetoothLowEnergyConnectionFinder( |
| 57 const cryptauth::RemoteDevice remote_device, | 45 const cryptauth::RemoteDevice remote_device, |
| 58 const std::string& remote_service_uuid, | 46 const std::vector<cryptauth::BeaconSeed>& beacon_seeds, |
| 59 const FinderStrategy finder_strategy, | 47 cryptauth::BluetoothThrottler* bluetooth_throttler); |
| 60 const BluetoothLowEnergyDeviceWhitelist* device_whitelist, | |
| 61 cryptauth::BluetoothThrottler* bluetooth_throttler, | |
| 62 int max_number_of_tries); | |
| 63 | 48 |
| 64 ~BluetoothLowEnergyConnectionFinder() override; | 49 ~BluetoothLowEnergyConnectionFinder() override; |
| 65 | 50 |
| 66 // Finds a connection to the remote device. | 51 // Finds a connection to the remote device. |
| 67 void Find(const cryptauth::ConnectionFinder::ConnectionCallback& | 52 void Find(const cryptauth::ConnectionFinder::ConnectionCallback& |
| 68 connection_callback) override; | 53 connection_callback) override; |
| 69 | 54 |
| 70 // cryptauth::ConnectionObserver: | 55 // cryptauth::ConnectionObserver: |
| 71 void OnConnectionStatusChanged( | 56 void OnConnectionStatusChanged( |
| 72 cryptauth::Connection* connection, | 57 cryptauth::Connection* connection, |
| 73 cryptauth::Connection::Status old_status, | 58 cryptauth::Connection::Status old_status, |
| 74 cryptauth::Connection::Status new_status) override; | 59 cryptauth::Connection::Status new_status) override; |
| 75 | 60 |
| 76 // device::BluetoothAdapter::Observer: | 61 // device::BluetoothAdapter::Observer: |
| 77 void AdapterPoweredChanged(device::BluetoothAdapter* adapter, | 62 void AdapterPoweredChanged(device::BluetoothAdapter* adapter, |
| 78 bool powered) override; | 63 bool powered) override; |
| 79 void DeviceAdded(device::BluetoothAdapter* adapter, | 64 void DeviceAdded(device::BluetoothAdapter* adapter, |
| 80 device::BluetoothDevice* device) override; | 65 device::BluetoothDevice* device) override; |
| 81 void DeviceChanged(device::BluetoothAdapter* adapter, | 66 void DeviceChanged(device::BluetoothAdapter* adapter, |
| 82 device::BluetoothDevice* device) override; | 67 device::BluetoothDevice* device) override; |
| 83 | 68 |
| 84 protected: | 69 protected: |
| 70 BluetoothLowEnergyConnectionFinder( |
| 71 const cryptauth::RemoteDevice remote_device, |
| 72 const std::vector<cryptauth::BeaconSeed>& beacon_seeds, |
| 73 std::unique_ptr<cryptauth::BackgroundEidGenerator> eid_generator, |
| 74 cryptauth::BluetoothThrottler* bluetooth_throttler); |
| 75 |
| 85 // Creates a proximity_auth::Connection with the device given by | 76 // Creates a proximity_auth::Connection with the device given by |
| 86 // |device_address|. Exposed for testing. | 77 // |device_address|. Exposed for testing. |
| 87 virtual std::unique_ptr<cryptauth::Connection> CreateConnection( | 78 virtual std::unique_ptr<cryptauth::Connection> CreateConnection( |
| 88 const std::string& device_address); | 79 const std::string& device_address); |
| 89 | 80 |
| 90 private: | 81 private: |
| 91 // Callback to be called when the Bluetooth adapter is initialized. | 82 // Callback to be called when the Bluetooth adapter is initialized. |
| 92 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter); | 83 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter); |
| 93 | 84 |
| 94 // Checks if |remote_device| contains |remote_service_uuid| and creates a | 85 // Checks if |remote_device| contains |remote_service_uuid| and creates a |
| (...skipping 10 matching lines...) Expand all Loading... |
| 105 // Starts a discovery session for |adapter_|. | 96 // Starts a discovery session for |adapter_|. |
| 106 void StartDiscoverySession(); | 97 void StartDiscoverySession(); |
| 107 | 98 |
| 108 // Stops the discovery session given by |discovery_session_|. | 99 // Stops the discovery session given by |discovery_session_|. |
| 109 void StopDiscoverySession(); | 100 void StopDiscoverySession(); |
| 110 | 101 |
| 111 // Checks if |device| is the right device: (i) has the adversement data or | 102 // Checks if |device| is the right device: (i) has the adversement data or |
| 112 // (ii) is paired and is the same as |remote_device|. | 103 // (ii) is paired and is the same as |remote_device|. |
| 113 bool IsRightDevice(device::BluetoothDevice* device); | 104 bool IsRightDevice(device::BluetoothDevice* device); |
| 114 | 105 |
| 115 // Checks if |remote_device| is advertising |remote_service_uuid_|. | |
| 116 bool HasService(device::BluetoothDevice* device); | |
| 117 | |
| 118 // Restarts the discovery session after creating |connection_| fails. | 106 // Restarts the discovery session after creating |connection_| fails. |
| 119 void RestartDiscoverySessionAsync(); | 107 void RestartDiscoverySessionAsync(); |
| 120 | 108 |
| 121 // Used to invoke |connection_callback_| asynchronously, decoupling the | 109 // Used to invoke |connection_callback_| asynchronously, decoupling the |
| 122 // callback invocation from the ConnectionObserver callstack. | 110 // callback invocation from the ConnectionObserver callstack. |
| 123 void InvokeCallbackAsync(); | 111 void InvokeCallbackAsync(); |
| 124 | 112 |
| 125 // Returns the device with |device_address|. | |
| 126 device::BluetoothDevice* GetDevice(const std::string& device_address); | |
| 127 | |
| 128 // The remote BLE device being searched. It maybe empty, in this case the | 113 // The remote BLE device being searched. It maybe empty, in this case the |
| 129 // remote device should advertise |remote_service_uuid_| and | 114 // remote device should advertise |remote_service_uuid_| and |
| 130 // |advertised_name_|. | 115 // |advertised_name_|. |
| 131 cryptauth::RemoteDevice remote_device_; | 116 cryptauth::RemoteDevice remote_device_; |
| 132 | 117 |
| 133 // The uuid of the service it looks for to establish a GattConnection. | 118 // The BeaconSeeds of the |remote_device|. |
| 134 device::BluetoothUUID remote_service_uuid_; | 119 std::vector<cryptauth::BeaconSeed> beacon_seeds_; |
| 135 | 120 |
| 136 // The finder strategy being used. See |IsRightDevice()|. | 121 // Generates the expected EIDs that may be advertised by |remote_device_|. If |
| 137 const FinderStrategy finder_strategy_; | 122 // an EID matches, we know its a device we should connect to. |
| 138 | 123 std::unique_ptr<cryptauth::BackgroundEidGenerator> eid_generator_; |
| 139 // Devices in |device_whitelist_| don't need to have |remote_service_uuid_| | |
| 140 // cached or advertised. Not owned, must outlive this instance. | |
| 141 // TODO(sacomoto): Remove |device_whitelist_| when ProximityAuthBleSystem is | |
| 142 // not needed anymore. | |
| 143 const BluetoothLowEnergyDeviceWhitelist* device_whitelist_; | |
| 144 | 124 |
| 145 // Throttles repeated connection attempts to the same device. This is a | 125 // Throttles repeated connection attempts to the same device. This is a |
| 146 // workaround for crbug.com/508919. Not owned, must outlive this instance. | 126 // workaround for crbug.com/508919. Not owned, must outlive this instance. |
| 147 cryptauth::BluetoothThrottler* bluetooth_throttler_; | 127 cryptauth::BluetoothThrottler* bluetooth_throttler_; |
| 148 | 128 |
| 149 // The Bluetooth adapter over which the Bluetooth connection will be made. | 129 // The Bluetooth adapter over which the Bluetooth connection will be made. |
| 150 scoped_refptr<device::BluetoothAdapter> adapter_; | 130 scoped_refptr<device::BluetoothAdapter> adapter_; |
| 151 | 131 |
| 152 // The discovery session associated to this object. | 132 // The discovery session associated to this object. |
| 153 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session_; | 133 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session_; |
| 154 | 134 |
| 155 // The connection with |remote_device|. | 135 // The connection with |remote_device|. |
| 156 std::unique_ptr<cryptauth::Connection> connection_; | 136 std::unique_ptr<cryptauth::Connection> connection_; |
| 157 | 137 |
| 158 // Callback called when the connection is established. | 138 // Callback called when the connection is established. |
| 159 cryptauth::ConnectionFinder::ConnectionCallback connection_callback_; | 139 cryptauth::ConnectionFinder::ConnectionCallback connection_callback_; |
| 160 | 140 |
| 161 // BluetoothLowEnergyConnection parameter. | |
| 162 int max_number_of_tries_; | |
| 163 | |
| 164 base::WeakPtrFactory<BluetoothLowEnergyConnectionFinder> weak_ptr_factory_; | 141 base::WeakPtrFactory<BluetoothLowEnergyConnectionFinder> weak_ptr_factory_; |
| 165 | 142 |
| 166 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyConnectionFinder); | 143 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyConnectionFinder); |
| 167 }; | 144 }; |
| 168 | 145 |
| 169 } // namespace proximity_auth | 146 } // namespace proximity_auth |
| 170 | 147 |
| 171 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_CONNECTION_FINDER_H | 148 #endif // COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_LOW_ENERGY_CONNECTION_FINDER_H |
| OLD | NEW |