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