Index: chromeos/components/tether/ble_connection_manager.h |
diff --git a/chromeos/components/tether/ble_connection_manager.h b/chromeos/components/tether/ble_connection_manager.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a27a3a15dd52fa9e90256065e5326fc4b7737926 |
--- /dev/null |
+++ b/chromeos/components/tether/ble_connection_manager.h |
@@ -0,0 +1,220 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef CHROMEOS_COMPONENTS_TETHER_BLE_CONNECTION_MANAGER_H_ |
+#define CHROMEOS_COMPONENTS_TETHER_BLE_CONNECTION_MANAGER_H_ |
+ |
+#include <map> |
+#include <set> |
+#include <string> |
+#include <vector> |
+ |
+#include "base/macros.h" |
+#include "base/memory/weak_ptr.h" |
+#include "base/timer/timer.h" |
+#include "chromeos/components/tether/ble_advertisement_device_queue.h" |
+#include "chromeos/components/tether/ble_advertiser.h" |
+#include "chromeos/components/tether/ble_scanner.h" |
+#include "chromeos/components/tether/proto/tether.pb.h" |
+#include "components/cryptauth/remote_device.h" |
+#include "components/cryptauth/secure_channel.h" |
+ |
+namespace cryptauth { |
+class BluetoothThrottler; |
+} // namespace cryptauth |
+ |
+namespace chromeos { |
+ |
+namespace tether { |
+ |
+// Manages connections to remote devices. To attempt a connection attempt, call |
+// |RegisterRemoteDevice()| and register to observe status change events. Once |
+// a device's status changes to |AUTHENTICATED|, messages can be sent and |
+// received to/from that device via the |SendMessage()| function and |
+// |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
|
+class BleConnectionManager : public BleScanner::Observer { |
+ public: |
+ static std::string MessageTypeToString(const MessageType& reason); |
+ |
+ class Observer { |
+ public: |
+ virtual void OnSecureChannelStatusChanged( |
+ const cryptauth::RemoteDevice& remote_device, |
+ const cryptauth::SecureChannel::Status& old_status, |
+ const cryptauth::SecureChannel::Status& new_status) = 0; |
+ |
+ virtual void OnMessageReceived(const cryptauth::RemoteDevice& remote_device, |
+ const std::string& payload) = 0; |
+ }; |
+ |
+ class Delegate { |
+ public: |
+ virtual std::unique_ptr<cryptauth::SecureChannel::Delegate> |
+ CreateSecureChannelDelegate() = 0; |
+ }; |
+ |
+ BleConnectionManager( |
+ std::unique_ptr<Delegate> delegate, |
+ scoped_refptr<device::BluetoothAdapter> adapter, |
+ const LocalDeviceDataProvider* local_device_data_provider, |
+ const cryptauth::RemoteBeaconSeedFetcher* remote_beacon_seed_fetcher, |
+ cryptauth::BluetoothThrottler* bluetooth_throttler); |
+ virtual ~BleConnectionManager(); |
+ |
+ // Registers |remote_device| for |connection_reason|. Once registered, this |
+ // instance will continue to attempt to connect and authenticate to that |
+ // device until the device is unregistered. |
+ void RegisterRemoteDevice(const cryptauth::RemoteDevice& remote_device, |
+ const MessageType& connection_reason); |
+ |
+ // Unregisters |remote_device| for |connection_reason|. Once registered, a |
+ // device will continue trying to connect until *ALL* of its |
+ // |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.
|
+ void UnregisterRemoteDevice(const cryptauth::RemoteDevice& remote_device, |
+ const MessageType& connection_reason); |
+ |
+ // Sends |message| to |remote_device|. This function can only be called if the |
+ // given device is authenticated. |
+ void SendMessage(const cryptauth::RemoteDevice& remote_device, |
+ const std::string& message); |
+ |
+ void AddObserver(Observer* observer); |
+ void RemoveObserver(Observer* observer); |
+ |
+ // BleScanner::Observer: |
+ void OnReceivedAdvertisementFromDevice( |
+ const std::string& device_address, |
+ cryptauth::RemoteDevice remote_device) override; |
+ |
+ private: |
+ friend class BleConnectionManagerTest; |
+ |
+ // Data associated with a registered device. Each registered device has an |
+ // associated |ConnectionMetadata| stored in |device_to_metadata_map_|, and |
+ // the |ConnectionMetadata| is removed when the device is unregistered. A |
+ // |ConnectionMetadata| stores the associated |SecureChannel| for registered |
+ // devices which have an active connection. |
+ class ConnectionMetadata : public cryptauth::SecureChannel::Observer { |
+ public: |
+ ConnectionMetadata(const cryptauth::RemoteDevice remote_device, |
+ std::shared_ptr<base::Timer> timer); |
+ ~ConnectionMetadata(); |
+ |
+ void RegisterConnectionReason(const MessageType& connection_reason); |
+ void UnregisterConnectionReason(const MessageType& connection_reason); |
+ bool HasReasonForConnection() const; |
+ |
+ cryptauth::SecureChannel::Status GetStatus() const; |
+ |
+ bool IsActivelyConnecting() const; |
+ bool HasEstablishedConnection() const; |
+ bool CanSendMessage() const; |
+ |
+ using ConnectionAttemptTimeoutCallback = |
+ base::Callback<void(const cryptauth::RemoteDevice& remote_device)>; |
+ void StartConnectionAttemptTimeout( |
+ const ConnectionAttemptTimeoutCallback& handler, |
+ bool use_short_error_timeout); |
+ |
+ void OnConnectionAttemptTimeout(); |
+ |
+ using SecureChannelStatusChangeCallback = base::Callback<void( |
+ const cryptauth::RemoteDevice& remote_device, |
+ const cryptauth::SecureChannel::Status& old_status, |
+ const cryptauth::SecureChannel::Status& new_status)>; |
+ using MessageReceivedCallback = |
+ base::Callback<void(const cryptauth::RemoteDevice& remote_device, |
+ const std::string& payload)>; |
+ void SetSecureChannel( |
+ std::unique_ptr<cryptauth::SecureChannel> secure_channel, |
+ const SecureChannelStatusChangeCallback& status_change_handler, |
+ const MessageReceivedCallback message_received_handler); |
+ |
+ void SendMessage(const std::string& payload); |
+ |
+ // cryptauth::SecureChannel::Observer: |
+ void OnSecureChannelStatusChanged( |
+ cryptauth::SecureChannel* secure_channel, |
+ const cryptauth::SecureChannel::Status& old_status, |
+ const cryptauth::SecureChannel::Status& new_status) override; |
+ void OnMessageReceived(cryptauth::SecureChannel* secure_channel, |
+ const std::string& feature, |
+ const std::string& payload) override; |
+ |
+ private: |
+ friend class BleConnectionManagerTest; |
+ |
+ cryptauth::RemoteDevice remote_device_; |
+ std::set<MessageType> active_connection_reasons_; |
+ std::shared_ptr<cryptauth::SecureChannel> secure_channel_; |
+ std::shared_ptr<base::Timer> connection_attempt_timeout_timer_; |
+ |
+ SecureChannelStatusChangeCallback status_change_handler_; |
+ MessageReceivedCallback message_received_handler_; |
+ ConnectionAttemptTimeoutCallback timeout_handler_; |
+ |
+ base::WeakPtrFactory<ConnectionMetadata> weak_ptr_factory_; |
+ }; |
+ |
+ class TimerFactory { |
+ public: |
+ virtual std::unique_ptr<base::Timer> CreateTimer(); |
+ }; |
+ |
+ BleConnectionManager( |
+ std::unique_ptr<Delegate> delegate, |
+ scoped_refptr<device::BluetoothAdapter> adapter, |
+ std::unique_ptr<BleScanner> ble_scanner, |
+ std::unique_ptr<BleAdvertiser> ble_advertiser, |
+ std::unique_ptr<BleAdvertisementDeviceQueue> device_queue, |
+ std::unique_ptr<TimerFactory> timer_factory, |
+ cryptauth::BluetoothThrottler* bluetooth_throttler); |
+ |
+ std::shared_ptr<ConnectionMetadata> GetConnectionMetadata( |
+ const cryptauth::RemoteDevice& remote_device); |
+ std::shared_ptr<ConnectionMetadata> AddMetadataForDevice( |
+ const cryptauth::RemoteDevice& remote_device); |
+ |
+ void UpdateConnectionAttempts(); |
+ void UpdateAdvertisementQueue(); |
+ |
+ void StartConnectionAttempt(const cryptauth::RemoteDevice& remote_device); |
+ void StopConnectionAttemptAndMoveToEndOfQueue( |
+ const cryptauth::RemoteDevice& remote_device); |
+ |
+ void OnConnectionAttemptTimeout(const cryptauth::RemoteDevice& remote_device); |
+ void OnSecureChannelStatusChanged( |
+ const cryptauth::RemoteDevice& remote_device, |
+ const cryptauth::SecureChannel::Status& old_status, |
+ const cryptauth::SecureChannel::Status& new_status); |
+ |
+ void SendMessageReceivedEvent(const cryptauth::RemoteDevice& remote_device, |
+ const std::string& payload); |
+ void SendSecureChannelStatusChangeEvent( |
+ const cryptauth::RemoteDevice& remote_device, |
+ const cryptauth::SecureChannel::Status& old_status, |
+ const cryptauth::SecureChannel::Status& new_status); |
+ |
+ std::unique_ptr<Delegate> delegate_; |
+ scoped_refptr<device::BluetoothAdapter> adapter_; |
+ std::unique_ptr<BleScanner> ble_scanner_; |
+ std::unique_ptr<BleAdvertiser> ble_advertiser_; |
+ std::unique_ptr<BleAdvertisementDeviceQueue> device_queue_; |
+ std::unique_ptr<TimerFactory> timer_factory_; |
+ cryptauth::BluetoothThrottler* bluetooth_throttler_; |
+ |
+ std::map<cryptauth::RemoteDevice, std::shared_ptr<ConnectionMetadata>> |
+ device_to_metadata_map_; |
+ |
+ base::ObserverList<Observer> observer_list_; |
+ base::WeakPtrFactory<BleConnectionManager> weak_ptr_factory_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(BleConnectionManager); |
+}; |
+ |
+} // namespace tether |
+ |
+} // namespace chromeos |
+ |
+#endif // CHROMEOS_COMPONENTS_TETHER_BLE_CONNECTION_MANAGER_H_ |