Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 CHROMEOS_COMPONENTS_TETHER_BLE_CONNECTION_MANAGER_H_ | |
| 6 #define CHROMEOS_COMPONENTS_TETHER_BLE_CONNECTION_MANAGER_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <set> | |
| 10 #include <string> | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "base/macros.h" | |
| 14 #include "base/memory/weak_ptr.h" | |
| 15 #include "base/timer/timer.h" | |
| 16 #include "chromeos/components/tether/ble_advertisement_device_queue.h" | |
| 17 #include "chromeos/components/tether/ble_advertiser.h" | |
| 18 #include "chromeos/components/tether/ble_scanner.h" | |
| 19 #include "components/cryptauth/remote_device.h" | |
| 20 #include "components/cryptauth/secure_channel.h" | |
| 21 | |
| 22 namespace cryptauth { | |
| 23 class BluetoothThrottler; | |
| 24 } // namespace cryptauth | |
| 25 | |
| 26 namespace chromeos { | |
| 27 | |
| 28 namespace tether { | |
| 29 | |
| 30 // Manages connections to remote devices. To attempt a connection attempt, call | |
| 31 // |RegisterRemoteDevice()| and register to observer status change events. Once | |
|
Ryan Hansberry
2017/02/14 18:27:28
observe*
Kyle Horimoto
2017/02/14 22:54:46
Done.
| |
| 32 // a device's status changes to |AUTHENTICATED|, messages can be sent and | |
| 33 // received to/from that device via the |SendMessage()| function and | |
| 34 // |OnMessageReceived()| observer callback. | |
| 35 class BleConnectionManager : public BleScanner::Observer { | |
| 36 public: | |
| 37 enum class ConnectionReason { | |
|
Ryan Hansberry
2017/02/14 18:27:28
We should just use the real proto constants.
Kyle Horimoto
2017/02/14 22:54:46
Done.
| |
| 38 TETHER_AVAILABILITY_REQUEST, | |
| 39 CONNECT_TETHERING_REQUEST, | |
| 40 KEEP_ALIVE_REQUEST, | |
| 41 DISCONNECT_REQUEST | |
| 42 }; | |
| 43 | |
| 44 static std::string ConnectionReasonToString(const ConnectionReason& reason); | |
| 45 | |
| 46 class Observer { | |
| 47 public: | |
| 48 virtual void OnSecureChannelStatusChanged( | |
| 49 const cryptauth::RemoteDevice& remote_device, | |
| 50 const cryptauth::SecureChannel::Status& old_status, | |
| 51 const cryptauth::SecureChannel::Status& new_status) = 0; | |
| 52 | |
| 53 virtual void OnMessageReceived(const cryptauth::RemoteDevice& remote_device, | |
| 54 const std::string& payload) = 0; | |
| 55 }; | |
| 56 | |
| 57 class Delegate { | |
| 58 public: | |
| 59 virtual std::unique_ptr<cryptauth::SecureChannel::Delegate> | |
| 60 CreateSecureChannelDelegate() = 0; | |
| 61 }; | |
| 62 | |
| 63 BleConnectionManager( | |
| 64 std::unique_ptr<Delegate> delegate, | |
| 65 scoped_refptr<device::BluetoothAdapter> adapter, | |
| 66 const LocalDeviceDataProvider* local_device_data_provider, | |
| 67 const cryptauth::RemoteBeaconSeedFetcher* remote_beacon_seed_fetcher, | |
| 68 cryptauth::BluetoothThrottler* bluetooth_throttler); | |
| 69 virtual ~BleConnectionManager(); | |
| 70 | |
| 71 // Registers |remote_device| for |connection_reason|. Once registered, this | |
| 72 // instance will continue to attempt to connect and authenticate to that | |
| 73 // device until the device is unregistered. | |
| 74 void RegisterRemoteDevice(const cryptauth::RemoteDevice& remote_device, | |
| 75 const ConnectionReason& connection_reason); | |
| 76 | |
| 77 // Unregisters |remote_device| for |connection_reason|. Once registered, a | |
| 78 // device will continue trying to connect until *ALL* of its | |
| 79 // |ConnectionReason|s have been unregistered. | |
| 80 void UnregisterRemoteDevice(const cryptauth::RemoteDevice& remote_device, | |
| 81 const ConnectionReason& connection_reason); | |
| 82 | |
| 83 // Sends |message| to |remote_device|. This function can only be called if the | |
| 84 // given device is authenticated. | |
| 85 void SendMessage(const cryptauth::RemoteDevice& remote_device, | |
| 86 const std::string& message); | |
| 87 | |
| 88 void AddObserver(Observer* observer); | |
| 89 void RemoveObserver(Observer* observer); | |
| 90 | |
| 91 // BleScanner::Observer: | |
| 92 void OnReceivedAdvertisementFromDevice( | |
| 93 const std::string& device_address, | |
| 94 cryptauth::RemoteDevice remote_device) override; | |
| 95 | |
| 96 private: | |
| 97 friend class BleConnectionManagerTest; | |
| 98 | |
| 99 class ConnectionMetadata : public cryptauth::SecureChannel::Observer { | |
|
Ryan Hansberry
2017/02/14 18:27:28
Does ConnectionMetadata need to be an inner class?
Ryan Hansberry
2017/02/14 18:27:28
This inner class is quite large; it needs document
Kyle Horimoto
2017/02/14 22:54:46
Yes, it should be an inner class. It is a bookkeep
Kyle Horimoto
2017/02/14 22:54:46
Done.
| |
| 100 public: | |
| 101 ConnectionMetadata(const cryptauth::RemoteDevice remote_device, | |
| 102 std::shared_ptr<base::Timer> timer); | |
| 103 ~ConnectionMetadata(); | |
| 104 | |
| 105 void RegisterConnectionReason(const ConnectionReason& connection_reason); | |
| 106 void UnregisterConnectionReason(const ConnectionReason& connection_reason); | |
| 107 bool HasReasonForConnection() const; | |
| 108 | |
| 109 cryptauth::SecureChannel::Status GetStatus() const; | |
| 110 | |
| 111 bool IsActivelyConnecting() const; | |
| 112 bool HasEstablishedConnection() const; | |
| 113 bool CanSendMessage() const; | |
| 114 | |
| 115 using ConnectionAttemptTimeoutHandler = | |
| 116 base::Callback<void(const cryptauth::RemoteDevice& remote_device)>; | |
| 117 void StartConnectionAttemptTimeout( | |
| 118 const ConnectionAttemptTimeoutHandler& handler, | |
| 119 bool use_short_error_timeout); | |
| 120 | |
| 121 void OnConnectionAttemptTimeout(); | |
| 122 | |
| 123 using SecureChannelStatusChangeHandler = base::Callback<void( | |
|
Ryan Hansberry
2017/02/14 18:27:28
Is 'using' standard practice? I've only seen 'type
Ryan Hansberry
2017/02/14 18:27:28
Please use '*Callback', not '*Handler'
Kyle Horimoto
2017/02/14 22:54:45
Done.
Kyle Horimoto
2017/02/14 22:54:46
Yep, pretty common. For example: https://cs.chromi
| |
| 124 const cryptauth::RemoteDevice& remote_device, | |
| 125 const cryptauth::SecureChannel::Status& old_status, | |
| 126 const cryptauth::SecureChannel::Status& new_status)>; | |
| 127 using MessageReceivedHandler = | |
|
Ryan Hansberry
2017/02/14 18:27:28
Same here.
Kyle Horimoto
2017/02/14 22:54:45
Done.
| |
| 128 base::Callback<void(const cryptauth::RemoteDevice& remote_device, | |
| 129 const std::string& payload)>; | |
| 130 void SetSecureChannel( | |
| 131 std::unique_ptr<cryptauth::SecureChannel> secure_channel, | |
| 132 const SecureChannelStatusChangeHandler& status_change_handler, | |
| 133 const MessageReceivedHandler message_received_handler); | |
| 134 | |
| 135 void SendMessage(const std::string& payload); | |
| 136 | |
| 137 // cryptauth::SecureChannel::Observer: | |
| 138 void OnSecureChannelStatusChanged( | |
| 139 cryptauth::SecureChannel* secure_channel, | |
| 140 const cryptauth::SecureChannel::Status& old_status, | |
| 141 const cryptauth::SecureChannel::Status& new_status) override; | |
| 142 void OnMessageReceived(cryptauth::SecureChannel* secure_channel, | |
| 143 const std::string& feature, | |
| 144 const std::string& payload) override; | |
| 145 | |
| 146 private: | |
| 147 friend class BleConnectionManagerTest; | |
| 148 | |
| 149 cryptauth::RemoteDevice remote_device_; | |
| 150 std::set<ConnectionReason> active_connection_reasons_; | |
| 151 std::shared_ptr<cryptauth::SecureChannel> secure_channel_; | |
| 152 std::shared_ptr<base::Timer> timer_; | |
| 153 | |
| 154 SecureChannelStatusChangeHandler status_change_handler_; | |
| 155 MessageReceivedHandler message_received_handler_; | |
| 156 ConnectionAttemptTimeoutHandler timeout_handler_; | |
| 157 | |
| 158 base::WeakPtrFactory<ConnectionMetadata> weak_ptr_factory_; | |
| 159 }; | |
| 160 | |
| 161 class TimerFactory { | |
| 162 public: | |
| 163 virtual std::unique_ptr<base::Timer> CreateTimer(); | |
| 164 }; | |
| 165 | |
| 166 BleConnectionManager( | |
| 167 std::unique_ptr<Delegate> delegate, | |
| 168 scoped_refptr<device::BluetoothAdapter> adapter, | |
| 169 std::unique_ptr<BleScanner> ble_scanner, | |
| 170 std::unique_ptr<BleAdvertiser> ble_advertiser, | |
| 171 std::unique_ptr<BleAdvertisementDeviceQueue> device_queue, | |
| 172 std::unique_ptr<TimerFactory> timer_factory, | |
| 173 cryptauth::BluetoothThrottler* bluetooth_throttler); | |
| 174 | |
| 175 std::shared_ptr<ConnectionMetadata> GetConnectionMetadata( | |
| 176 const cryptauth::RemoteDevice& remote_device); | |
| 177 std::shared_ptr<ConnectionMetadata> AddMetadataForDevice( | |
| 178 const cryptauth::RemoteDevice& remote_device); | |
| 179 | |
| 180 void UpdateConnectionAttempts(); | |
| 181 void UpdateAdvertisementQueue(); | |
| 182 | |
| 183 void StartConnectionAttempt(const cryptauth::RemoteDevice& remote_device); | |
| 184 void StopConnectionAttemptAndMoveToEndOfQueue( | |
| 185 const cryptauth::RemoteDevice& remote_device); | |
| 186 | |
| 187 void OnConnectionAttemptTimeout(const cryptauth::RemoteDevice& remote_device); | |
| 188 void OnSecureChannelStatusChanged( | |
| 189 const cryptauth::RemoteDevice& remote_device, | |
| 190 const cryptauth::SecureChannel::Status& old_status, | |
| 191 const cryptauth::SecureChannel::Status& new_status); | |
| 192 | |
| 193 void SendMessageReceivedEvent(const cryptauth::RemoteDevice& remote_device, | |
| 194 const std::string& payload); | |
| 195 void SendSecureChannelStatusChangeEvent( | |
| 196 const cryptauth::RemoteDevice& remote_device, | |
| 197 const cryptauth::SecureChannel::Status& old_status, | |
| 198 const cryptauth::SecureChannel::Status& new_status); | |
| 199 | |
| 200 std::unique_ptr<Delegate> delegate_; | |
| 201 scoped_refptr<device::BluetoothAdapter> adapter_; | |
| 202 std::unique_ptr<BleScanner> ble_scanner_; | |
| 203 std::unique_ptr<BleAdvertiser> ble_advertiser_; | |
| 204 std::unique_ptr<BleAdvertisementDeviceQueue> device_queue_; | |
| 205 std::unique_ptr<TimerFactory> timer_factory_; | |
| 206 cryptauth::BluetoothThrottler* bluetooth_throttler_; | |
| 207 | |
| 208 std::map<cryptauth::RemoteDevice, std::shared_ptr<ConnectionMetadata>> | |
| 209 device_to_metadata_map_; | |
| 210 | |
| 211 base::ObserverList<Observer> observer_list_; | |
| 212 base::WeakPtrFactory<BleConnectionManager> weak_ptr_factory_; | |
| 213 | |
| 214 DISALLOW_COPY_AND_ASSIGN(BleConnectionManager); | |
| 215 }; | |
| 216 | |
| 217 } // namespace tether | |
| 218 | |
| 219 } // namespace chromeos | |
| 220 | |
| 221 #endif // CHROMEOS_COMPONENTS_TETHER_BLE_CONNECTION_MANAGER_H_ | |
| OLD | NEW |