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