| 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/tether_host_fetcher.h" | 10 #include "chromeos/components/tether/tether_host_fetcher.h" |
| 11 #include "chromeos/components/tether/wifi_hotspot_connector.h" | 11 #include "chromeos/components/tether/wifi_hotspot_connector.h" |
| 12 #include "chromeos/network/network_handler.h" | 12 #include "chromeos/network/network_handler.h" |
| 13 #include "chromeos/network/network_state_handler.h" | 13 #include "chromeos/network/network_state_handler.h" |
| 14 #include "components/proximity_auth/logging/logging.h" | 14 #include "components/proximity_auth/logging/logging.h" |
| 15 | 15 |
| 16 namespace chromeos { | 16 namespace chromeos { |
| 17 | 17 |
| 18 namespace tether { | 18 namespace tether { |
| 19 | 19 |
| 20 TetherConnector::TetherConnector( | 20 TetherConnector::TetherConnector( |
| 21 NetworkConnectionHandler* network_connection_handler, | |
| 22 NetworkStateHandler* network_state_handler, | 21 NetworkStateHandler* network_state_handler, |
| 23 WifiHotspotConnector* wifi_hotspot_connector, | 22 WifiHotspotConnector* wifi_hotspot_connector, |
| 24 ActiveHost* active_host, | 23 ActiveHost* active_host, |
| 25 TetherHostFetcher* tether_host_fetcher, | 24 TetherHostFetcher* tether_host_fetcher, |
| 26 BleConnectionManager* connection_manager, | 25 BleConnectionManager* connection_manager, |
| 27 TetherHostResponseRecorder* tether_host_response_recorder, | 26 TetherHostResponseRecorder* tether_host_response_recorder, |
| 28 DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map) | 27 DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map) |
| 29 : network_connection_handler_(network_connection_handler), | 28 : network_state_handler_(network_state_handler), |
| 30 network_state_handler_(network_state_handler), | |
| 31 wifi_hotspot_connector_(wifi_hotspot_connector), | 29 wifi_hotspot_connector_(wifi_hotspot_connector), |
| 32 active_host_(active_host), | 30 active_host_(active_host), |
| 33 tether_host_fetcher_(tether_host_fetcher), | 31 tether_host_fetcher_(tether_host_fetcher), |
| 34 connection_manager_(connection_manager), | 32 connection_manager_(connection_manager), |
| 35 tether_host_response_recorder_(tether_host_response_recorder), | 33 tether_host_response_recorder_(tether_host_response_recorder), |
| 36 device_id_tether_network_guid_map_(device_id_tether_network_guid_map), | 34 device_id_tether_network_guid_map_(device_id_tether_network_guid_map), |
| 37 weak_ptr_factory_(this) { | 35 weak_ptr_factory_(this) {} |
| 38 network_connection_handler_->SetTetherDelegate(this); | |
| 39 } | |
| 40 | 36 |
| 41 TetherConnector::~TetherConnector() { | 37 TetherConnector::~TetherConnector() { |
| 42 network_connection_handler_->SetTetherDelegate(nullptr); | |
| 43 if (connect_tethering_operation_) { | 38 if (connect_tethering_operation_) { |
| 44 connect_tethering_operation_->RemoveObserver(this); | 39 connect_tethering_operation_->RemoveObserver(this); |
| 45 } | 40 } |
| 46 } | 41 } |
| 47 | 42 |
| 48 void TetherConnector::ConnectToNetwork( | 43 void TetherConnector::ConnectToNetwork( |
| 49 const std::string& tether_network_guid, | 44 const std::string& tether_network_guid, |
| 50 const base::Closure& success_callback, | 45 const base::Closure& success_callback, |
| 51 const network_handler::StringResultCallback& error_callback) { | 46 const network_handler::StringResultCallback& error_callback) { |
| 52 DCHECK(!tether_network_guid.empty()); | 47 DCHECK(!tether_network_guid.empty()); |
| 53 DCHECK(!success_callback.is_null()); | 48 DCHECK(!success_callback.is_null()); |
| 54 DCHECK(!error_callback.is_null()); | 49 DCHECK(!error_callback.is_null()); |
| 55 | 50 |
| 56 PA_LOG(INFO) << "Attempting to connect to network with GUID " | 51 PA_LOG(INFO) << "Attempting to connect to network with GUID " |
| 57 << tether_network_guid << "."; | 52 << tether_network_guid << "."; |
| 58 | 53 |
| 59 std::string device_id = | 54 const std::string device_id = |
| 60 device_id_tether_network_guid_map_->GetDeviceIdForTetherNetworkGuid( | 55 device_id_tether_network_guid_map_->GetDeviceIdForTetherNetworkGuid( |
| 61 tether_network_guid); | 56 tether_network_guid); |
| 62 | 57 |
| 63 // If NetworkConnectionHandler receives a connection request for a network | 58 // If NetworkConnectionHandler receives a connection request for a network |
| 64 // to which it is already attempting a connection, it should stop the | 59 // to which it is already attempting a connection, it should stop the |
| 65 // duplicate connection request itself before invoking its TetherDelegate. | 60 // duplicate connection request itself before invoking its TetherDelegate. |
| 66 // Thus, ConnectToNetwork() should never be called for a device which is | 61 // Thus, ConnectToNetwork() should never be called for a device which is |
| 67 // already pending connection. | 62 // already pending connection. |
| 68 DCHECK(device_id_pending_connection_ != device_id); | 63 DCHECK(device_id_pending_connection_ != device_id); |
| 69 | 64 |
| 70 if (!device_id_pending_connection_.empty()) { | 65 if (!device_id_pending_connection_.empty()) { |
| 71 PA_LOG(INFO) << "A connection attempt was already in progress to device " | 66 PA_LOG(INFO) << "A connection attempt was already in progress to device " |
| 72 << "with ID " << device_id_pending_connection_ << ". " | 67 << "with ID " << device_id_pending_connection_ << ". " |
| 73 << "Canceling that connection attempt before continuing."; | 68 << "Canceling that connection attempt before continuing."; |
| 74 | 69 CancelConnectionAttempt( |
| 75 if (connect_tethering_operation_) { | 70 device_id_tether_network_guid_map_->GetTetherNetworkGuidForDeviceId( |
| 76 // If a ConnectTetheringOperation is in progress, stop it. | 71 device_id_pending_connection_)); |
| 77 connect_tethering_operation_->RemoveObserver(this); | |
| 78 connect_tethering_operation_.reset(); | |
| 79 } | |
| 80 | |
| 81 // Since the previous connection attempt did not complete before the new | |
| 82 // attempt began, call the error callback. | |
| 83 DCHECK(!error_callback_.is_null()); | |
| 84 error_callback_.Run(NetworkConnectionHandler::kErrorConnectCanceled); | |
| 85 } | 72 } |
| 86 | 73 |
| 87 device_id_pending_connection_ = device_id; | 74 device_id_pending_connection_ = device_id; |
| 88 success_callback_ = success_callback; | 75 success_callback_ = success_callback; |
| 89 error_callback_ = error_callback; | 76 error_callback_ = error_callback; |
| 90 active_host_->SetActiveHostConnecting(device_id, tether_network_guid); | 77 active_host_->SetActiveHostConnecting(device_id, tether_network_guid); |
| 91 | 78 |
| 92 tether_host_fetcher_->FetchTetherHost( | 79 tether_host_fetcher_->FetchTetherHost( |
| 93 device_id_pending_connection_, | 80 device_id_pending_connection_, |
| 94 base::Bind(&TetherConnector::OnTetherHostToConnectFetched, | 81 base::Bind(&TetherConnector::OnTetherHostToConnectFetched, |
| 95 weak_ptr_factory_.GetWeakPtr(), | 82 weak_ptr_factory_.GetWeakPtr(), |
| 96 device_id_pending_connection_)); | 83 device_id_pending_connection_)); |
| 97 } | 84 } |
| 98 | 85 |
| 86 bool TetherConnector::CancelConnectionAttempt( |
| 87 const std::string& tether_network_guid) { |
| 88 const std::string device_id = |
| 89 device_id_tether_network_guid_map_->GetDeviceIdForTetherNetworkGuid( |
| 90 tether_network_guid); |
| 91 |
| 92 if (device_id != device_id_pending_connection_) { |
| 93 PA_LOG(ERROR) << "CancelConnectionAttempt(): Cancel requested for Tether " |
| 94 << "network with GUID " << tether_network_guid << ", but " |
| 95 << "there was no active connection to that network."; |
| 96 return false; |
| 97 } |
| 98 |
| 99 PA_LOG(INFO) << "Canceling connection attempt to Tether network with GUID " |
| 100 << tether_network_guid; |
| 101 |
| 102 if (connect_tethering_operation_) { |
| 103 // If a ConnectTetheringOperation is in progress, stop it. |
| 104 connect_tethering_operation_->RemoveObserver(this); |
| 105 connect_tethering_operation_.reset(); |
| 106 } |
| 107 |
| 108 SetConnectionFailed(NetworkConnectionHandler::kErrorConnectCanceled); |
| 109 return true; |
| 110 } |
| 111 |
| 99 void TetherConnector::OnSuccessfulConnectTetheringResponse( | 112 void TetherConnector::OnSuccessfulConnectTetheringResponse( |
| 100 const cryptauth::RemoteDevice& remote_device, | 113 const cryptauth::RemoteDevice& remote_device, |
| 101 const std::string& ssid, | 114 const std::string& ssid, |
| 102 const std::string& password) { | 115 const std::string& password) { |
| 103 if (device_id_pending_connection_ != remote_device.GetDeviceId()) { | 116 if (device_id_pending_connection_ != remote_device.GetDeviceId()) { |
| 104 // If the success was part of a previous attempt for a different device, | 117 // If the success was part of a previous attempt for a different device, |
| 105 // ignore it. | 118 // ignore it. |
| 106 PA_LOG(INFO) << "Received successful ConnectTetheringResponse from " | 119 PA_LOG(INFO) << "Received successful ConnectTetheringResponse from " |
| 107 << "device with ID " | 120 << "device with ID " |
| 108 << remote_device.GetTruncatedDeviceIdForLogs() | 121 << remote_device.GetTruncatedDeviceIdForLogs() |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 << ", but a connection to another device has already started."; | 155 << ", but a connection to another device has already started."; |
| 143 return; | 156 return; |
| 144 } | 157 } |
| 145 | 158 |
| 146 PA_LOG(WARNING) << "Connection to device with ID " | 159 PA_LOG(WARNING) << "Connection to device with ID " |
| 147 << remote_device.GetTruncatedDeviceIdForLogs() | 160 << remote_device.GetTruncatedDeviceIdForLogs() |
| 148 << " could not complete. Error code: " << error_code; | 161 << " could not complete. Error code: " << error_code; |
| 149 | 162 |
| 150 connect_tethering_operation_->RemoveObserver(this); | 163 connect_tethering_operation_->RemoveObserver(this); |
| 151 connect_tethering_operation_.reset(); | 164 connect_tethering_operation_.reset(); |
| 152 SetConnectionFailed(); | 165 SetConnectionFailed(NetworkConnectionHandler::kErrorConnectFailed); |
| 153 } | 166 } |
| 154 | 167 |
| 155 void TetherConnector::OnTetherHostToConnectFetched( | 168 void TetherConnector::OnTetherHostToConnectFetched( |
| 156 const std::string& device_id, | 169 const std::string& device_id, |
| 157 std::unique_ptr<cryptauth::RemoteDevice> tether_host_to_connect) { | 170 std::unique_ptr<cryptauth::RemoteDevice> tether_host_to_connect) { |
| 158 if (device_id_pending_connection_ != device_id) { | 171 if (device_id_pending_connection_ != device_id) { |
| 159 PA_LOG(INFO) << "Device to connect to has changed while device with ID " | 172 PA_LOG(INFO) << "Device to connect to has changed while device with ID " |
| 160 << cryptauth::RemoteDevice::TruncateDeviceIdForLogs(device_id) | 173 << cryptauth::RemoteDevice::TruncateDeviceIdForLogs(device_id) |
| 161 << " was being fetched."; | 174 << " was being fetched."; |
| 162 return; | 175 return; |
| 163 } | 176 } |
| 164 | 177 |
| 165 if (!tether_host_to_connect) { | 178 if (!tether_host_to_connect) { |
| 166 PA_LOG(ERROR) << "Could not fetch tether host with device ID " | 179 PA_LOG(ERROR) << "Could not fetch tether host with device ID " |
| 167 << cryptauth::RemoteDevice::TruncateDeviceIdForLogs(device_id) | 180 << cryptauth::RemoteDevice::TruncateDeviceIdForLogs(device_id) |
| 168 << ". Cannot connect."; | 181 << ". Cannot connect."; |
| 169 SetConnectionFailed(); | 182 SetConnectionFailed(NetworkConnectionHandler::kErrorConnectFailed); |
| 170 return; | 183 return; |
| 171 } | 184 } |
| 172 | 185 |
| 173 DCHECK(device_id == tether_host_to_connect->GetDeviceId()); | 186 DCHECK(device_id == tether_host_to_connect->GetDeviceId()); |
| 174 | 187 |
| 175 connect_tethering_operation_ = | 188 connect_tethering_operation_ = |
| 176 ConnectTetheringOperation::Factory::NewInstance( | 189 ConnectTetheringOperation::Factory::NewInstance( |
| 177 *tether_host_to_connect, connection_manager_, | 190 *tether_host_to_connect, connection_manager_, |
| 178 tether_host_response_recorder_); | 191 tether_host_response_recorder_); |
| 179 connect_tethering_operation_->AddObserver(this); | 192 connect_tethering_operation_->AddObserver(this); |
| 180 connect_tethering_operation_->Initialize(); | 193 connect_tethering_operation_->Initialize(); |
| 181 } | 194 } |
| 182 | 195 |
| 183 void TetherConnector::SetConnectionFailed() { | 196 void TetherConnector::SetConnectionFailed(const std::string& error_name) { |
| 184 DCHECK(!device_id_pending_connection_.empty()); | 197 DCHECK(!device_id_pending_connection_.empty()); |
| 185 DCHECK(!error_callback_.is_null()); | 198 DCHECK(!error_callback_.is_null()); |
| 186 | 199 |
| 187 // Save a copy of the callback before resetting it below. | 200 // Save a copy of the callback before resetting it below. |
| 188 network_handler::StringResultCallback error_callback = error_callback_; | 201 network_handler::StringResultCallback error_callback = error_callback_; |
| 189 | 202 |
| 190 device_id_pending_connection_.clear(); | 203 device_id_pending_connection_.clear(); |
| 191 success_callback_.Reset(); | 204 success_callback_.Reset(); |
| 192 error_callback_.Reset(); | 205 error_callback_.Reset(); |
| 193 | 206 |
| 194 error_callback.Run(NetworkConnectionHandler::kErrorConnectFailed); | 207 error_callback.Run(error_name); |
| 195 active_host_->SetActiveHostDisconnected(); | 208 active_host_->SetActiveHostDisconnected(); |
| 196 } | 209 } |
| 197 | 210 |
| 198 void TetherConnector::SetConnectionSucceeded( | 211 void TetherConnector::SetConnectionSucceeded( |
| 199 const std::string& device_id, | 212 const std::string& device_id, |
| 200 const std::string& wifi_network_guid) { | 213 const std::string& wifi_network_guid) { |
| 201 DCHECK(!device_id_pending_connection_.empty()); | 214 DCHECK(!device_id_pending_connection_.empty()); |
| 202 DCHECK(device_id_pending_connection_ == device_id); | 215 DCHECK(device_id_pending_connection_ == device_id); |
| 203 DCHECK(!success_callback_.is_null()); | 216 DCHECK(!success_callback_.is_null()); |
| 204 | 217 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 231 return; | 244 return; |
| 232 } | 245 } |
| 233 | 246 |
| 234 if (wifi_network_guid.empty()) { | 247 if (wifi_network_guid.empty()) { |
| 235 // If the Wi-Fi network ID is empty, then the connection did not succeed. | 248 // If the Wi-Fi network ID is empty, then the connection did not succeed. |
| 236 PA_LOG(ERROR) << "Failed to connect to the hotspot belonging to the device " | 249 PA_LOG(ERROR) << "Failed to connect to the hotspot belonging to the device " |
| 237 << "with ID " | 250 << "with ID " |
| 238 << cryptauth::RemoteDevice::TruncateDeviceIdForLogs(device_id) | 251 << cryptauth::RemoteDevice::TruncateDeviceIdForLogs(device_id) |
| 239 << "."; | 252 << "."; |
| 240 | 253 |
| 241 SetConnectionFailed(); | 254 SetConnectionFailed(NetworkConnectionHandler::kErrorConnectFailed); |
| 242 return; | 255 return; |
| 243 } | 256 } |
| 244 | 257 |
| 245 bool successful_association = | 258 bool successful_association = |
| 246 network_state_handler_->AssociateTetherNetworkStateWithWifiNetwork( | 259 network_state_handler_->AssociateTetherNetworkStateWithWifiNetwork( |
| 247 device_id, wifi_network_guid); | 260 device_id, wifi_network_guid); |
| 248 if (successful_association) { | 261 if (successful_association) { |
| 249 PA_LOG(INFO) << "Successfully connected to host device with ID " | 262 PA_LOG(INFO) << "Successfully connected to host device with ID " |
| 250 << cryptauth::RemoteDevice::TruncateDeviceIdForLogs(device_id) | 263 << cryptauth::RemoteDevice::TruncateDeviceIdForLogs(device_id) |
| 251 << ". Tether network ID: \"" << device_id | 264 << ". Tether network ID: \"" << device_id |
| 252 << "\", Wi-Fi network ID: \"" << wifi_network_guid << "\""; | 265 << "\", Wi-Fi network ID: \"" << wifi_network_guid << "\""; |
| 253 } else { | 266 } else { |
| 254 PA_LOG(WARNING) << "Successfully connected to host device with ID " | 267 PA_LOG(WARNING) << "Successfully connected to host device with ID " |
| 255 << cryptauth::RemoteDevice::TruncateDeviceIdForLogs( | 268 << cryptauth::RemoteDevice::TruncateDeviceIdForLogs( |
| 256 device_id) | 269 device_id) |
| 257 << ", but failed to associate tether network with ID \"" | 270 << ", but failed to associate tether network with ID \"" |
| 258 << device_id << "\" to Wi-Fi network with ID \"" | 271 << device_id << "\" to Wi-Fi network with ID \"" |
| 259 << wifi_network_guid << "\"."; | 272 << wifi_network_guid << "\"."; |
| 260 } | 273 } |
| 261 | 274 |
| 262 SetConnectionSucceeded(device_id, wifi_network_guid); | 275 SetConnectionSucceeded(device_id, wifi_network_guid); |
| 263 } | 276 } |
| 264 | 277 |
| 265 } // namespace tether | 278 } // namespace tether |
| 266 | 279 |
| 267 } // namespace chromeos | 280 } // namespace chromeos |
| OLD | NEW |