| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_CONNECTION_FINDER_H | |
| 6 #define COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_CONNECTION_FINDER_H | |
| 7 | |
| 8 #include <memory> | |
| 9 #include <set> | |
| 10 #include <string> | |
| 11 | |
| 12 #include "base/callback.h" | |
| 13 #include "base/macros.h" | |
| 14 #include "base/memory/ref_counted.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" | |
| 20 #include "components/cryptauth/remote_device.h" | |
| 21 #include "device/bluetooth/bluetooth_adapter.h" | |
| 22 #include "device/bluetooth/bluetooth_device.h" | |
| 23 #include "device/bluetooth/bluetooth_discovery_session.h" | |
| 24 #include "device/bluetooth/bluetooth_gatt_connection.h" | |
| 25 | |
| 26 namespace proximity_auth { | |
| 27 | |
| 28 class BluetoothLowEnergyDeviceWhitelist; | |
| 29 | |
| 30 // This cryptauth::ConnectionFinder implementation is specialized in finding a | |
| 31 // Bluetooth | |
| 32 // Low Energy remote device. | |
| 33 class BluetoothLowEnergyConnectionFinder | |
| 34 : public cryptauth::ConnectionFinder, | |
| 35 public cryptauth::ConnectionObserver, | |
| 36 public device::BluetoothAdapter::Observer { | |
| 37 public: | |
| 38 enum FinderStrategy { FIND_PAIRED_DEVICE, FIND_ANY_DEVICE }; | |
| 39 | |
| 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 // | |
| 47 // |remote_device|: The BLE remote device. |remote_device.bluetooth_adress| | |
| 48 // should be empty when |has_public_bluetooth_address| is false. | |
| 49 // |remote_service_uuid|: The UUID of the service used to send/receive data in | |
| 50 // remote device. | |
| 51 // |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( | |
| 57 const cryptauth::RemoteDevice remote_device, | |
| 58 const std::string& remote_service_uuid, | |
| 59 const FinderStrategy finder_strategy, | |
| 60 const BluetoothLowEnergyDeviceWhitelist* device_whitelist, | |
| 61 cryptauth::BluetoothThrottler* bluetooth_throttler, | |
| 62 int max_number_of_tries); | |
| 63 | |
| 64 ~BluetoothLowEnergyConnectionFinder() override; | |
| 65 | |
| 66 // Finds a connection to the remote device. | |
| 67 void Find(const cryptauth::ConnectionFinder::ConnectionCallback& | |
| 68 connection_callback) override; | |
| 69 | |
| 70 // cryptauth::ConnectionObserver: | |
| 71 void OnConnectionStatusChanged( | |
| 72 cryptauth::Connection* connection, | |
| 73 cryptauth::Connection::Status old_status, | |
| 74 cryptauth::Connection::Status new_status) override; | |
| 75 | |
| 76 // device::BluetoothAdapter::Observer: | |
| 77 void AdapterPoweredChanged(device::BluetoothAdapter* adapter, | |
| 78 bool powered) override; | |
| 79 void DeviceAdded(device::BluetoothAdapter* adapter, | |
| 80 device::BluetoothDevice* device) override; | |
| 81 void DeviceChanged(device::BluetoothAdapter* adapter, | |
| 82 device::BluetoothDevice* device) override; | |
| 83 | |
| 84 protected: | |
| 85 // Creates a proximity_auth::Connection with the device given by | |
| 86 // |device_address|. Exposed for testing. | |
| 87 virtual std::unique_ptr<cryptauth::Connection> CreateConnection( | |
| 88 const std::string& device_address); | |
| 89 | |
| 90 private: | |
| 91 // Callback to be called when the Bluetooth adapter is initialized. | |
| 92 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter); | |
| 93 | |
| 94 // Checks if |remote_device| contains |remote_service_uuid| and creates a | |
| 95 // connection in that case. | |
| 96 void HandleDeviceUpdated(device::BluetoothDevice* remote_device); | |
| 97 | |
| 98 // Callback called when a new discovery session is started. | |
| 99 void OnDiscoverySessionStarted( | |
| 100 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session); | |
| 101 | |
| 102 // Callback called when there is an error starting a new discovery session. | |
| 103 void OnStartDiscoverySessionError(); | |
| 104 | |
| 105 // Starts a discovery session for |adapter_|. | |
| 106 void StartDiscoverySession(); | |
| 107 | |
| 108 // Stops the discovery session given by |discovery_session_|. | |
| 109 void StopDiscoverySession(); | |
| 110 | |
| 111 // Checks if |device| is the right device: (i) has the adversement data or | |
| 112 // (ii) is paired and is the same as |remote_device|. | |
| 113 bool IsRightDevice(device::BluetoothDevice* device); | |
| 114 | |
| 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. | |
| 119 void RestartDiscoverySessionAsync(); | |
| 120 | |
| 121 // Used to invoke |connection_callback_| asynchronously, decoupling the | |
| 122 // callback invocation from the ConnectionObserver callstack. | |
| 123 void InvokeCallbackAsync(); | |
| 124 | |
| 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 | |
| 129 // remote device should advertise |remote_service_uuid_| and | |
| 130 // |advertised_name_|. | |
| 131 cryptauth::RemoteDevice remote_device_; | |
| 132 | |
| 133 // The uuid of the service it looks for to establish a GattConnection. | |
| 134 device::BluetoothUUID remote_service_uuid_; | |
| 135 | |
| 136 // The finder strategy being used. See |IsRightDevice()|. | |
| 137 const FinderStrategy finder_strategy_; | |
| 138 | |
| 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 | |
| 145 // Throttles repeated connection attempts to the same device. This is a | |
| 146 // workaround for crbug.com/508919. Not owned, must outlive this instance. | |
| 147 cryptauth::BluetoothThrottler* bluetooth_throttler_; | |
| 148 | |
| 149 // The Bluetooth adapter over which the Bluetooth connection will be made. | |
| 150 scoped_refptr<device::BluetoothAdapter> adapter_; | |
| 151 | |
| 152 // The discovery session associated to this object. | |
| 153 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session_; | |
| 154 | |
| 155 // The connection with |remote_device|. | |
| 156 std::unique_ptr<cryptauth::Connection> connection_; | |
| 157 | |
| 158 // Callback called when the connection is established. | |
| 159 cryptauth::ConnectionFinder::ConnectionCallback connection_callback_; | |
| 160 | |
| 161 // BluetoothLowEnergyConnection parameter. | |
| 162 int max_number_of_tries_; | |
| 163 | |
| 164 base::WeakPtrFactory<BluetoothLowEnergyConnectionFinder> weak_ptr_factory_; | |
| 165 | |
| 166 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyConnectionFinder); | |
| 167 }; | |
| 168 | |
| 169 } // namespace proximity_auth | |
| 170 | |
| 171 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_CONNECTION_FINDER_H | |
| OLD | NEW |