Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: chromeos/components/tether/ble_connection_manager.h

Issue 2697763002: [CrOS Tether]: Create BleConnectionManager, which manages secure connections between the current de… (Closed)
Patch Set: hansberry@ comments. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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. When a device is registered,
32 // BleConnectionManager intiates a connection to that device. If the connection
33 // succeeds and authenticates successfully, messages can be sent/received
34 // to/from the device. If the connection does not succeed, BleConnectionManager
35 // continues attempting connections until a connection is established or the
36 // device is unregistered.
37 //
38 // To use this class, construct an instance and observe it via |AddObserver()|.
39 // Then, register device(s) to connect to and wait for the
Ryan Hansberry 2017/02/16 19:29:36 nit: Please state what method to use
Kyle Horimoto 2017/02/17 01:06:52 Done.
40 // |OnSecureChannelStatusChanged()| callback to be invoked. If the status for a
41 // device changes from |DISCONNECTED| to |CONNECTING| then back to
42 // |DISCONNECTED|, a connection attempt has failed. Clients should set a retry
43 // limit and unregister a device if multiple connection attempts have failed.
Ryan Hansberry 2017/02/16 19:29:35 same here
Kyle Horimoto 2017/02/17 01:06:52 Done.
44 // If, instead, a connection succeeds the status changes to |AUTHENTICATED|, the
45 // device can safely send and receive messages. To send a message, call
46 // |SendMessage()|, and to listen for received messages, implement the
47 // |OnMessageReceived()| callback.
48 //
49 // Note that a single device can be registered for multiple connection reasons.
50 // If a device is registered for more than one reason, its connections (and
51 // connection attempts) will remain active until all connection reasons have
52 // been unregistered for the device.
53 class BleConnectionManager : public BleScanner::Observer {
54 public:
55 static std::string MessageTypeToString(const MessageType& reason);
56
57 class Observer {
58 public:
59 virtual void OnSecureChannelStatusChanged(
60 const cryptauth::RemoteDevice& remote_device,
61 const cryptauth::SecureChannel::Status& old_status,
62 const cryptauth::SecureChannel::Status& new_status) = 0;
63
64 virtual void OnMessageReceived(const cryptauth::RemoteDevice& remote_device,
65 const std::string& payload) = 0;
66 };
67
68 class Delegate {
69 public:
70 virtual std::unique_ptr<cryptauth::SecureChannel::Delegate>
71 CreateSecureChannelDelegate() = 0;
72 };
73
74 BleConnectionManager(
75 std::unique_ptr<Delegate> delegate,
76 scoped_refptr<device::BluetoothAdapter> adapter,
77 const LocalDeviceDataProvider* local_device_data_provider,
78 const cryptauth::RemoteBeaconSeedFetcher* remote_beacon_seed_fetcher,
79 cryptauth::BluetoothThrottler* bluetooth_throttler);
80 virtual ~BleConnectionManager();
81
82 // Registers |remote_device| for |connection_reason|. Once registered, this
83 // instance will continue to attempt to connect and authenticate to that
84 // device until the device is unregistered.
85 void RegisterRemoteDevice(const cryptauth::RemoteDevice& remote_device,
86 const MessageType& connection_reason);
87
88 // Unregisters |remote_device| for |connection_reason|. Once registered, a
89 // device will continue trying to connect until *ALL* of its
90 // MessageTypes have been unregistered.
91 void UnregisterRemoteDevice(const cryptauth::RemoteDevice& remote_device,
92 const MessageType& connection_reason);
93
94 // Sends |message| to |remote_device|. This function can only be called if the
95 // given device is authenticated.
96 void SendMessage(const cryptauth::RemoteDevice& remote_device,
97 const std::string& message);
98
99 void AddObserver(Observer* observer);
100 void RemoveObserver(Observer* observer);
101
102 // BleScanner::Observer:
103 void OnReceivedAdvertisementFromDevice(
104 const std::string& device_address,
105 cryptauth::RemoteDevice remote_device) override;
106
107 private:
108 friend class BleConnectionManagerTest;
109
110 // Data associated with a registered device. Each registered device has an
111 // associated |ConnectionMetadata| stored in |device_to_metadata_map_|, and
112 // the |ConnectionMetadata| is removed when the device is unregistered. A
113 // |ConnectionMetadata| stores the associated |SecureChannel| for registered
114 // devices which have an active connection.
115 class ConnectionMetadata : public cryptauth::SecureChannel::Observer {
116 public:
117 ConnectionMetadata(const cryptauth::RemoteDevice remote_device,
118 std::shared_ptr<base::Timer> timer,
119 base::WeakPtr<BleConnectionManager> manager);
120 ~ConnectionMetadata();
121
122 void RegisterConnectionReason(const MessageType& connection_reason);
123 void UnregisterConnectionReason(const MessageType& connection_reason);
124 bool HasReasonForConnection() const;
125
126 bool HasEstablishedConnection() const;
127 cryptauth::SecureChannel::Status GetStatus() const;
128
129 void StartConnectionAttemptTimer(bool use_short_error_timeout);
130 void SetSecureChannel(
131 std::unique_ptr<cryptauth::SecureChannel> secure_channel);
132 void SendMessage(const std::string& payload);
133
134 // cryptauth::SecureChannel::Observer:
135 void OnSecureChannelStatusChanged(
136 cryptauth::SecureChannel* secure_channel,
137 const cryptauth::SecureChannel::Status& old_status,
138 const cryptauth::SecureChannel::Status& new_status) override;
139 void OnMessageReceived(cryptauth::SecureChannel* secure_channel,
140 const std::string& feature,
141 const std::string& payload) override;
142
143 private:
144 friend class BleConnectionManagerTest;
145
146 void OnConnectionAttemptTimeout();
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 base::WeakPtr<BleConnectionManager> manager_;
153
154 base::WeakPtrFactory<ConnectionMetadata> weak_ptr_factory_;
155 };
156
157 class TimerFactory {
158 public:
159 virtual std::unique_ptr<base::Timer> CreateTimer();
160 };
161
162 BleConnectionManager(
163 std::unique_ptr<Delegate> delegate,
164 scoped_refptr<device::BluetoothAdapter> adapter,
165 std::unique_ptr<BleScanner> ble_scanner,
166 std::unique_ptr<BleAdvertiser> ble_advertiser,
167 std::unique_ptr<BleAdvertisementDeviceQueue> device_queue,
168 std::unique_ptr<TimerFactory> timer_factory,
169 cryptauth::BluetoothThrottler* bluetooth_throttler);
170
171 std::shared_ptr<ConnectionMetadata> GetConnectionMetadata(
172 const cryptauth::RemoteDevice& remote_device);
173 std::shared_ptr<ConnectionMetadata> AddMetadataForDevice(
174 const cryptauth::RemoteDevice& remote_device);
175
176 void UpdateConnectionAttempts();
177 void UpdateAdvertisementQueue();
178
179 void StartConnectionAttempt(const cryptauth::RemoteDevice& remote_device);
180 void StopConnectionAttemptAndMoveToEndOfQueue(
181 const cryptauth::RemoteDevice& remote_device);
182
183 void OnConnectionAttemptTimeout(const cryptauth::RemoteDevice& remote_device);
184 void OnSecureChannelStatusChanged(
185 const cryptauth::RemoteDevice& remote_device,
186 const cryptauth::SecureChannel::Status& old_status,
187 const cryptauth::SecureChannel::Status& new_status);
188
189 void SendMessageReceivedEvent(const cryptauth::RemoteDevice& remote_device,
190 const std::string& payload);
191 void SendSecureChannelStatusChangeEvent(
192 const cryptauth::RemoteDevice& remote_device,
193 const cryptauth::SecureChannel::Status& old_status,
194 const cryptauth::SecureChannel::Status& new_status);
195
196 std::unique_ptr<Delegate> delegate_;
197 scoped_refptr<device::BluetoothAdapter> adapter_;
198 std::unique_ptr<BleScanner> ble_scanner_;
199 std::unique_ptr<BleAdvertiser> ble_advertiser_;
200 std::unique_ptr<BleAdvertisementDeviceQueue> device_queue_;
201 std::unique_ptr<TimerFactory> timer_factory_;
202 cryptauth::BluetoothThrottler* bluetooth_throttler_;
203
204 std::map<cryptauth::RemoteDevice, std::shared_ptr<ConnectionMetadata>>
205 device_to_metadata_map_;
206
207 base::ObserverList<Observer> observer_list_;
208 base::WeakPtrFactory<BleConnectionManager> weak_ptr_factory_;
209
210 DISALLOW_COPY_AND_ASSIGN(BleConnectionManager);
211 };
212
213 } // namespace tether
214
215 } // namespace chromeos
216
217 #endif // CHROMEOS_COMPONENTS_TETHER_BLE_CONNECTION_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698