| 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_BLUETOOTH_LOW_ENERGY_CONNECTION_FINDER_H | |
| 6 #define COMPONENTS_PROXIMITY_AUTH_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/background_eid_generator.h" | |
| 17 #include "components/cryptauth/bluetooth_throttler.h" | |
| 18 #include "components/cryptauth/connection.h" | |
| 19 #include "components/cryptauth/connection_finder.h" | |
| 20 #include "components/cryptauth/connection_observer.h" | |
| 21 #include "components/cryptauth/remote_beacon_seed_fetcher.h" | |
| 22 #include "components/cryptauth/remote_device.h" | |
| 23 #include "device/bluetooth/bluetooth_adapter.h" | |
| 24 #include "device/bluetooth/bluetooth_device.h" | |
| 25 #include "device/bluetooth/bluetooth_discovery_session.h" | |
| 26 #include "device/bluetooth/bluetooth_gatt_connection.h" | |
| 27 | |
| 28 namespace proximity_auth { | |
| 29 | |
| 30 // This cryptauth::ConnectionFinder implementation is specialized in finding a | |
| 31 // Bluetooth Low Energy remote device. We look for remote devices advertising | |
| 32 // the expected EID for the current or nearby time intervals. | |
| 33 class BluetoothLowEnergyConnectionFinder | |
| 34 : public cryptauth::ConnectionFinder, | |
| 35 public cryptauth::ConnectionObserver, | |
| 36 public device::BluetoothAdapter::Observer { | |
| 37 public: | |
| 38 // Finds (and connects) to a Bluetooth low energy device, based on the EID | |
| 39 // advertised by the remote device. | |
| 40 // | |
| 41 // |remote_device|: The BLE remote device. | |
| 42 // |beacon_seeds|: The BeaconSeeds for the |remote_device. | |
| 43 // |bluetooth_throttler|: The reconnection throttler. | |
| 44 BluetoothLowEnergyConnectionFinder( | |
| 45 const cryptauth::RemoteDevice remote_device, | |
| 46 const std::vector<cryptauth::BeaconSeed>& beacon_seeds, | |
| 47 cryptauth::BluetoothThrottler* bluetooth_throttler); | |
| 48 | |
| 49 ~BluetoothLowEnergyConnectionFinder() override; | |
| 50 | |
| 51 // Finds a connection to the remote device. | |
| 52 void Find(const cryptauth::ConnectionFinder::ConnectionCallback& | |
| 53 connection_callback) override; | |
| 54 | |
| 55 // cryptauth::ConnectionObserver: | |
| 56 void OnConnectionStatusChanged( | |
| 57 cryptauth::Connection* connection, | |
| 58 cryptauth::Connection::Status old_status, | |
| 59 cryptauth::Connection::Status new_status) override; | |
| 60 | |
| 61 // device::BluetoothAdapter::Observer: | |
| 62 void AdapterPoweredChanged(device::BluetoothAdapter* adapter, | |
| 63 bool powered) override; | |
| 64 void DeviceAdded(device::BluetoothAdapter* adapter, | |
| 65 device::BluetoothDevice* device) override; | |
| 66 void DeviceChanged(device::BluetoothAdapter* adapter, | |
| 67 device::BluetoothDevice* device) override; | |
| 68 | |
| 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 | |
| 76 // Creates a proximity_auth::Connection with the device given by | |
| 77 // |device_address|. Exposed for testing. | |
| 78 virtual std::unique_ptr<cryptauth::Connection> CreateConnection( | |
| 79 const std::string& device_address); | |
| 80 | |
| 81 private: | |
| 82 // Callback to be called when the Bluetooth adapter is initialized. | |
| 83 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter); | |
| 84 | |
| 85 // Checks if |remote_device| contains |remote_service_uuid| and creates a | |
| 86 // connection in that case. | |
| 87 void HandleDeviceUpdated(device::BluetoothDevice* remote_device); | |
| 88 | |
| 89 // Callback called when a new discovery session is started. | |
| 90 void OnDiscoverySessionStarted( | |
| 91 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session); | |
| 92 | |
| 93 // Callback called when there is an error starting a new discovery session. | |
| 94 void OnStartDiscoverySessionError(); | |
| 95 | |
| 96 // Starts a discovery session for |adapter_|. | |
| 97 void StartDiscoverySession(); | |
| 98 | |
| 99 // Stops the discovery session given by |discovery_session_|. | |
| 100 void StopDiscoverySession(); | |
| 101 | |
| 102 // Checks if |device| is the right device: (i) has the adversement data or | |
| 103 // (ii) is paired and is the same as |remote_device|. | |
| 104 bool IsRightDevice(device::BluetoothDevice* device); | |
| 105 | |
| 106 // Restarts the discovery session after creating |connection_| fails. | |
| 107 void RestartDiscoverySessionAsync(); | |
| 108 | |
| 109 // Used to invoke |connection_callback_| asynchronously, decoupling the | |
| 110 // callback invocation from the ConnectionObserver callstack. | |
| 111 void InvokeCallbackAsync(); | |
| 112 | |
| 113 // The remote BLE device being searched. It maybe empty, in this case the | |
| 114 // remote device should advertise |remote_service_uuid_| and | |
| 115 // |advertised_name_|. | |
| 116 cryptauth::RemoteDevice remote_device_; | |
| 117 | |
| 118 // The BeaconSeeds of the |remote_device|. | |
| 119 std::vector<cryptauth::BeaconSeed> beacon_seeds_; | |
| 120 | |
| 121 // Generates the expected EIDs that may be advertised by |remote_device_|. If | |
| 122 // an EID matches, we know its a device we should connect to. | |
| 123 std::unique_ptr<cryptauth::BackgroundEidGenerator> eid_generator_; | |
| 124 | |
| 125 // Throttles repeated connection attempts to the same device. This is a | |
| 126 // workaround for crbug.com/508919. Not owned, must outlive this instance. | |
| 127 cryptauth::BluetoothThrottler* bluetooth_throttler_; | |
| 128 | |
| 129 // The Bluetooth adapter over which the Bluetooth connection will be made. | |
| 130 scoped_refptr<device::BluetoothAdapter> adapter_; | |
| 131 | |
| 132 // The discovery session associated to this object. | |
| 133 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session_; | |
| 134 | |
| 135 // The connection with |remote_device|. | |
| 136 std::unique_ptr<cryptauth::Connection> connection_; | |
| 137 | |
| 138 // Callback called when the connection is established. | |
| 139 cryptauth::ConnectionFinder::ConnectionCallback connection_callback_; | |
| 140 | |
| 141 base::WeakPtrFactory<BluetoothLowEnergyConnectionFinder> weak_ptr_factory_; | |
| 142 | |
| 143 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyConnectionFinder); | |
| 144 }; | |
| 145 | |
| 146 } // namespace proximity_auth | |
| 147 | |
| 148 #endif // COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_LOW_ENERGY_CONNECTION_FINDER_H | |
| OLD | NEW |