OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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 #include "chromeos/components/tether/tether_connector.h" | 5 #include "chromeos/components/tether/tether_connector.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "chromeos/components/tether/active_host.h" | 8 #include "chromeos/components/tether/active_host.h" |
9 #include "chromeos/components/tether/device_id_tether_network_guid_map.h" | 9 #include "chromeos/components/tether/device_id_tether_network_guid_map.h" |
| 10 #include "chromeos/components/tether/host_connection_metrics_logger.h" |
10 #include "chromeos/components/tether/host_scan_cache.h" | 11 #include "chromeos/components/tether/host_scan_cache.h" |
11 #include "chromeos/components/tether/notification_presenter.h" | 12 #include "chromeos/components/tether/notification_presenter.h" |
12 #include "chromeos/components/tether/tether_host_fetcher.h" | 13 #include "chromeos/components/tether/tether_host_fetcher.h" |
13 #include "chromeos/components/tether/wifi_hotspot_connector.h" | 14 #include "chromeos/components/tether/wifi_hotspot_connector.h" |
14 #include "chromeos/network/network_handler.h" | 15 #include "chromeos/network/network_handler.h" |
15 #include "chromeos/network/network_state.h" | 16 #include "chromeos/network/network_state.h" |
16 #include "chromeos/network/network_state_handler.h" | 17 #include "chromeos/network/network_state_handler.h" |
17 #include "components/proximity_auth/logging/logging.h" | 18 #include "components/proximity_auth/logging/logging.h" |
18 | 19 |
19 namespace chromeos { | 20 namespace chromeos { |
20 | 21 |
21 namespace tether { | 22 namespace tether { |
22 | 23 |
23 TetherConnector::TetherConnector( | 24 TetherConnector::TetherConnector( |
24 NetworkStateHandler* network_state_handler, | 25 NetworkStateHandler* network_state_handler, |
25 WifiHotspotConnector* wifi_hotspot_connector, | 26 WifiHotspotConnector* wifi_hotspot_connector, |
26 ActiveHost* active_host, | 27 ActiveHost* active_host, |
27 TetherHostFetcher* tether_host_fetcher, | 28 TetherHostFetcher* tether_host_fetcher, |
28 BleConnectionManager* connection_manager, | 29 BleConnectionManager* connection_manager, |
29 TetherHostResponseRecorder* tether_host_response_recorder, | 30 TetherHostResponseRecorder* tether_host_response_recorder, |
30 DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map, | 31 DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map, |
31 HostScanCache* host_scan_cache, | 32 HostScanCache* host_scan_cache, |
32 NotificationPresenter* notification_presenter) | 33 NotificationPresenter* notification_presenter, |
| 34 HostConnectionMetricsLogger* host_connection_metrics_logger) |
33 : network_state_handler_(network_state_handler), | 35 : network_state_handler_(network_state_handler), |
34 wifi_hotspot_connector_(wifi_hotspot_connector), | 36 wifi_hotspot_connector_(wifi_hotspot_connector), |
35 active_host_(active_host), | 37 active_host_(active_host), |
36 tether_host_fetcher_(tether_host_fetcher), | 38 tether_host_fetcher_(tether_host_fetcher), |
37 connection_manager_(connection_manager), | 39 connection_manager_(connection_manager), |
38 tether_host_response_recorder_(tether_host_response_recorder), | 40 tether_host_response_recorder_(tether_host_response_recorder), |
39 device_id_tether_network_guid_map_(device_id_tether_network_guid_map), | 41 device_id_tether_network_guid_map_(device_id_tether_network_guid_map), |
40 host_scan_cache_(host_scan_cache), | 42 host_scan_cache_(host_scan_cache), |
41 notification_presenter_(notification_presenter), | 43 notification_presenter_(notification_presenter), |
| 44 host_connection_metrics_logger_(host_connection_metrics_logger), |
42 weak_ptr_factory_(this) {} | 45 weak_ptr_factory_(this) {} |
43 | 46 |
44 TetherConnector::~TetherConnector() { | 47 TetherConnector::~TetherConnector() { |
45 if (connect_tethering_operation_) { | 48 if (connect_tethering_operation_) { |
46 connect_tethering_operation_->RemoveObserver(this); | 49 connect_tethering_operation_->RemoveObserver(this); |
47 } | 50 } |
48 } | 51 } |
49 | 52 |
50 void TetherConnector::ConnectToNetwork( | 53 void TetherConnector::ConnectToNetwork( |
51 const std::string& tether_network_guid, | 54 const std::string& tether_network_guid, |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 | 115 |
113 PA_LOG(INFO) << "Canceling connection attempt to Tether network with GUID " | 116 PA_LOG(INFO) << "Canceling connection attempt to Tether network with GUID " |
114 << tether_network_guid; | 117 << tether_network_guid; |
115 | 118 |
116 if (connect_tethering_operation_) { | 119 if (connect_tethering_operation_) { |
117 // If a ConnectTetheringOperation is in progress, stop it. | 120 // If a ConnectTetheringOperation is in progress, stop it. |
118 connect_tethering_operation_->RemoveObserver(this); | 121 connect_tethering_operation_->RemoveObserver(this); |
119 connect_tethering_operation_.reset(); | 122 connect_tethering_operation_.reset(); |
120 } | 123 } |
121 | 124 |
122 SetConnectionFailed(NetworkConnectionHandler::kErrorConnectCanceled); | 125 SetConnectionFailed( |
| 126 NetworkConnectionHandler::kErrorConnectCanceled, |
| 127 HostConnectionMetricsLogger::ConnectionToHostResult:: |
| 128 CONNECTION_RESULT_FAILURE_CLIENT_CONNECTION_CANCELED_BY_USER); |
123 return true; | 129 return true; |
124 } | 130 } |
125 | 131 |
126 void TetherConnector::OnSuccessfulConnectTetheringResponse( | 132 void TetherConnector::OnSuccessfulConnectTetheringResponse( |
127 const cryptauth::RemoteDevice& remote_device, | 133 const cryptauth::RemoteDevice& remote_device, |
128 const std::string& ssid, | 134 const std::string& ssid, |
129 const std::string& password) { | 135 const std::string& password) { |
130 if (device_id_pending_connection_ != remote_device.GetDeviceId()) { | 136 if (device_id_pending_connection_ != remote_device.GetDeviceId()) { |
131 // If the success was part of a previous attempt for a different device, | 137 // If the success was part of a previous attempt for a different device, |
132 // ignore it. | 138 // ignore it. |
(...skipping 21 matching lines...) Expand all Loading... |
154 | 160 |
155 wifi_hotspot_connector_->ConnectToWifiHotspot( | 161 wifi_hotspot_connector_->ConnectToWifiHotspot( |
156 ssid_copy, password_copy, active_host_->GetTetherNetworkGuid(), | 162 ssid_copy, password_copy, active_host_->GetTetherNetworkGuid(), |
157 base::Bind(&TetherConnector::OnWifiConnection, | 163 base::Bind(&TetherConnector::OnWifiConnection, |
158 weak_ptr_factory_.GetWeakPtr(), remote_device_id)); | 164 weak_ptr_factory_.GetWeakPtr(), remote_device_id)); |
159 } | 165 } |
160 | 166 |
161 void TetherConnector::OnConnectTetheringFailure( | 167 void TetherConnector::OnConnectTetheringFailure( |
162 const cryptauth::RemoteDevice& remote_device, | 168 const cryptauth::RemoteDevice& remote_device, |
163 ConnectTetheringResponse_ResponseCode error_code) { | 169 ConnectTetheringResponse_ResponseCode error_code) { |
164 if (device_id_pending_connection_ != remote_device.GetDeviceId()) { | 170 std::string device_id_copy = remote_device.GetDeviceId(); |
| 171 if (device_id_pending_connection_ != device_id_copy) { |
165 // If the failure was part of a previous attempt for a different device, | 172 // If the failure was part of a previous attempt for a different device, |
166 // ignore it. | 173 // ignore it. |
167 PA_LOG(INFO) << "Received failed ConnectTetheringResponse from device with " | 174 PA_LOG(INFO) << "Received failed ConnectTetheringResponse from device with " |
168 << "ID " << remote_device.GetTruncatedDeviceIdForLogs() | 175 << "ID " << remote_device.GetTruncatedDeviceIdForLogs() |
169 << ", but a connection to another device has already started."; | 176 << ", but a connection to another device has already started."; |
170 return; | 177 return; |
171 } | 178 } |
172 | 179 |
173 PA_LOG(WARNING) << "Connection to device with ID " | 180 PA_LOG(WARNING) << "Connection to device with ID " |
174 << remote_device.GetTruncatedDeviceIdForLogs() | 181 << remote_device.GetTruncatedDeviceIdForLogs() |
175 << " could not complete. Error code: " << error_code; | 182 << " could not complete. Error code: " << error_code; |
176 | 183 |
177 connect_tethering_operation_->RemoveObserver(this); | 184 connect_tethering_operation_->RemoveObserver(this); |
178 connect_tethering_operation_.reset(); | 185 connect_tethering_operation_.reset(); |
179 SetConnectionFailed(NetworkConnectionHandler::kErrorConnectFailed); | 186 SetConnectionFailed( |
| 187 NetworkConnectionHandler::kErrorConnectFailed, |
| 188 GetConnectionToHostResultFromErrorCode(device_id_copy, error_code)); |
180 } | 189 } |
181 | 190 |
182 void TetherConnector::OnTetherHostToConnectFetched( | 191 void TetherConnector::OnTetherHostToConnectFetched( |
183 const std::string& device_id, | 192 const std::string& device_id, |
184 std::unique_ptr<cryptauth::RemoteDevice> tether_host_to_connect) { | 193 std::unique_ptr<cryptauth::RemoteDevice> tether_host_to_connect) { |
185 if (device_id_pending_connection_ != device_id) { | 194 if (device_id_pending_connection_ != device_id) { |
186 PA_LOG(INFO) << "Device to connect to has changed while device with ID " | 195 PA_LOG(INFO) << "Device to connect to has changed while device with ID " |
187 << cryptauth::RemoteDevice::TruncateDeviceIdForLogs(device_id) | 196 << cryptauth::RemoteDevice::TruncateDeviceIdForLogs(device_id) |
188 << " was being fetched."; | 197 << " was being fetched."; |
189 return; | 198 return; |
190 } | 199 } |
191 | 200 |
192 if (!tether_host_to_connect) { | 201 if (!tether_host_to_connect) { |
193 PA_LOG(ERROR) << "Could not fetch tether host with device ID " | 202 PA_LOG(ERROR) << "Could not fetch tether host with device ID " |
194 << cryptauth::RemoteDevice::TruncateDeviceIdForLogs(device_id) | 203 << cryptauth::RemoteDevice::TruncateDeviceIdForLogs(device_id) |
195 << ". Cannot connect."; | 204 << ". Cannot connect."; |
196 SetConnectionFailed(NetworkConnectionHandler::kErrorConnectFailed); | 205 SetConnectionFailed( |
| 206 NetworkConnectionHandler::kErrorConnectFailed, |
| 207 HostConnectionMetricsLogger::ConnectionToHostResult:: |
| 208 CONNECTION_RESULT_FAILURE_CLIENT_CONNECTION_INTERNAL_ERROR); |
197 return; | 209 return; |
198 } | 210 } |
199 | 211 |
200 DCHECK(device_id == tether_host_to_connect->GetDeviceId()); | 212 DCHECK(device_id == tether_host_to_connect->GetDeviceId()); |
201 | 213 |
202 const std::string& tether_network_guid = | 214 const std::string tether_network_guid = |
203 device_id_tether_network_guid_map_->GetTetherNetworkGuidForDeviceId( | 215 device_id_tether_network_guid_map_->GetTetherNetworkGuidForDeviceId( |
204 device_id); | 216 device_id); |
205 connect_tethering_operation_ = | 217 connect_tethering_operation_ = |
206 ConnectTetheringOperation::Factory::NewInstance( | 218 ConnectTetheringOperation::Factory::NewInstance( |
207 *tether_host_to_connect, connection_manager_, | 219 *tether_host_to_connect, connection_manager_, |
208 tether_host_response_recorder_, | 220 tether_host_response_recorder_, |
209 host_scan_cache_->DoesHostRequireSetup(tether_network_guid)); | 221 host_scan_cache_->DoesHostRequireSetup(tether_network_guid)); |
210 connect_tethering_operation_->AddObserver(this); | 222 connect_tethering_operation_->AddObserver(this); |
211 connect_tethering_operation_->Initialize(); | 223 connect_tethering_operation_->Initialize(); |
212 } | 224 } |
213 | 225 |
214 void TetherConnector::SetConnectionFailed(const std::string& error_name) { | 226 void TetherConnector::SetConnectionFailed( |
| 227 const std::string& error_name, |
| 228 HostConnectionMetricsLogger::ConnectionToHostResult |
| 229 connection_to_host_result) { |
215 DCHECK(!device_id_pending_connection_.empty()); | 230 DCHECK(!device_id_pending_connection_.empty()); |
216 DCHECK(!error_callback_.is_null()); | 231 DCHECK(!error_callback_.is_null()); |
217 | 232 |
218 notification_presenter_->RemoveSetupRequiredNotification(); | 233 notification_presenter_->RemoveSetupRequiredNotification(); |
219 | 234 |
220 // Save a copy of the callback before resetting it below. | 235 // Save a copy of the callback before resetting it below. |
221 network_handler::StringResultCallback error_callback = error_callback_; | 236 network_handler::StringResultCallback error_callback = error_callback_; |
222 | 237 |
223 device_id_pending_connection_.clear(); | 238 device_id_pending_connection_.clear(); |
224 success_callback_.Reset(); | 239 success_callback_.Reset(); |
225 error_callback_.Reset(); | 240 error_callback_.Reset(); |
226 | 241 |
227 error_callback.Run(error_name); | 242 error_callback.Run(error_name); |
228 active_host_->SetActiveHostDisconnected(); | 243 active_host_->SetActiveHostDisconnected(); |
| 244 |
| 245 host_connection_metrics_logger_->RecordConnectionToHostResult( |
| 246 connection_to_host_result); |
229 } | 247 } |
230 | 248 |
231 void TetherConnector::SetConnectionSucceeded( | 249 void TetherConnector::SetConnectionSucceeded( |
232 const std::string& device_id, | 250 const std::string& device_id, |
233 const std::string& wifi_network_guid) { | 251 const std::string& wifi_network_guid) { |
234 DCHECK(!device_id_pending_connection_.empty()); | 252 DCHECK(!device_id_pending_connection_.empty()); |
235 DCHECK(device_id_pending_connection_ == device_id); | 253 DCHECK(device_id_pending_connection_ == device_id); |
236 DCHECK(!success_callback_.is_null()); | 254 DCHECK(!success_callback_.is_null()); |
237 | 255 |
| 256 host_connection_metrics_logger_->RecordConnectionToHostResult( |
| 257 HostConnectionMetricsLogger::ConnectionToHostResult:: |
| 258 CONNECTION_RESULT_SUCCESS); |
| 259 |
238 notification_presenter_->RemoveSetupRequiredNotification(); | 260 notification_presenter_->RemoveSetupRequiredNotification(); |
239 | 261 |
240 // Save a copy of the callback before resetting it below. | 262 // Save a copy of the callback before resetting it below. |
241 base::Closure success_callback = success_callback_; | 263 base::Closure success_callback = success_callback_; |
242 | 264 |
243 device_id_pending_connection_.clear(); | 265 device_id_pending_connection_.clear(); |
244 success_callback_.Reset(); | 266 success_callback_.Reset(); |
245 error_callback_.Reset(); | 267 error_callback_.Reset(); |
246 | 268 |
247 success_callback.Run(); | 269 success_callback.Run(); |
(...skipping 18 matching lines...) Expand all Loading... |
266 return; | 288 return; |
267 } | 289 } |
268 | 290 |
269 if (wifi_network_guid.empty()) { | 291 if (wifi_network_guid.empty()) { |
270 // If the Wi-Fi network ID is empty, then the connection did not succeed. | 292 // If the Wi-Fi network ID is empty, then the connection did not succeed. |
271 PA_LOG(ERROR) << "Failed to connect to the hotspot belonging to the device " | 293 PA_LOG(ERROR) << "Failed to connect to the hotspot belonging to the device " |
272 << "with ID " | 294 << "with ID " |
273 << cryptauth::RemoteDevice::TruncateDeviceIdForLogs(device_id) | 295 << cryptauth::RemoteDevice::TruncateDeviceIdForLogs(device_id) |
274 << "."; | 296 << "."; |
275 | 297 |
276 SetConnectionFailed(NetworkConnectionHandler::kErrorConnectFailed); | 298 SetConnectionFailed( |
| 299 NetworkConnectionHandler::kErrorConnectFailed, |
| 300 HostConnectionMetricsLogger::ConnectionToHostResult:: |
| 301 CONNECTION_RESULT_FAILURE_CLIENT_CONNECTION_TIMEOUT); |
277 return; | 302 return; |
278 } | 303 } |
279 | 304 |
280 SetConnectionSucceeded(device_id, wifi_network_guid); | 305 SetConnectionSucceeded(device_id, wifi_network_guid); |
281 } | 306 } |
282 | 307 |
| 308 HostConnectionMetricsLogger::ConnectionToHostResult |
| 309 TetherConnector::GetConnectionToHostResultFromErrorCode( |
| 310 const std::string& device_id, |
| 311 ConnectTetheringResponse_ResponseCode error_code) { |
| 312 if (error_code == |
| 313 ConnectTetheringResponse_ResponseCode:: |
| 314 ConnectTetheringResponse_ResponseCode_PROVISIONING_FAILED) { |
| 315 return HostConnectionMetricsLogger::ConnectionToHostResult:: |
| 316 CONNECTION_RESULT_PROVISIONING_FAILED; |
| 317 } |
| 318 |
| 319 if (error_code == |
| 320 ConnectTetheringResponse_ResponseCode:: |
| 321 ConnectTetheringResponse_ResponseCode_TETHERING_TIMEOUT) { |
| 322 const std::string tether_network_guid = |
| 323 device_id_tether_network_guid_map_->GetTetherNetworkGuidForDeviceId( |
| 324 device_id); |
| 325 if (host_scan_cache_->DoesHostRequireSetup(tether_network_guid)) { |
| 326 return HostConnectionMetricsLogger::ConnectionToHostResult:: |
| 327 CONNECTION_RESULT_FAILURE_TETHERING_TIMED_OUT_FIRST_TIME_SETUP_WAS_REQ
UIRED; |
| 328 } |
| 329 |
| 330 return HostConnectionMetricsLogger::ConnectionToHostResult:: |
| 331 CONNECTION_RESULT_FAILURE_TETHERING_TIMED_OUT_FIRST_TIME_SETUP_WAS_NOT_R
EQUIRED; |
| 332 } |
| 333 |
| 334 return HostConnectionMetricsLogger::ConnectionToHostResult:: |
| 335 CONNECTION_RESULT_FAILURE_UNKNOWN_ERROR; |
| 336 } |
| 337 |
283 } // namespace tether | 338 } // namespace tether |
284 | 339 |
285 } // namespace chromeos | 340 } // namespace chromeos |
OLD | NEW |