| 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_scan_cache.h" |
| 11 #include "chromeos/components/tether/notification_presenter.h" |
| 10 #include "chromeos/components/tether/tether_host_fetcher.h" | 12 #include "chromeos/components/tether/tether_host_fetcher.h" |
| 11 #include "chromeos/components/tether/wifi_hotspot_connector.h" | 13 #include "chromeos/components/tether/wifi_hotspot_connector.h" |
| 12 #include "chromeos/network/network_handler.h" | 14 #include "chromeos/network/network_handler.h" |
| 15 #include "chromeos/network/network_state.h" |
| 13 #include "chromeos/network/network_state_handler.h" | 16 #include "chromeos/network/network_state_handler.h" |
| 14 #include "components/proximity_auth/logging/logging.h" | 17 #include "components/proximity_auth/logging/logging.h" |
| 15 | 18 |
| 16 namespace chromeos { | 19 namespace chromeos { |
| 17 | 20 |
| 18 namespace tether { | 21 namespace tether { |
| 19 | 22 |
| 20 TetherConnector::TetherConnector( | 23 TetherConnector::TetherConnector( |
| 21 NetworkStateHandler* network_state_handler, | 24 NetworkStateHandler* network_state_handler, |
| 22 WifiHotspotConnector* wifi_hotspot_connector, | 25 WifiHotspotConnector* wifi_hotspot_connector, |
| 23 ActiveHost* active_host, | 26 ActiveHost* active_host, |
| 24 TetherHostFetcher* tether_host_fetcher, | 27 TetherHostFetcher* tether_host_fetcher, |
| 25 BleConnectionManager* connection_manager, | 28 BleConnectionManager* connection_manager, |
| 26 TetherHostResponseRecorder* tether_host_response_recorder, | 29 TetherHostResponseRecorder* tether_host_response_recorder, |
| 27 DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map) | 30 DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map, |
| 31 HostScanCache* host_scan_cache, |
| 32 NotificationPresenter* notification_presenter) |
| 28 : network_state_handler_(network_state_handler), | 33 : network_state_handler_(network_state_handler), |
| 29 wifi_hotspot_connector_(wifi_hotspot_connector), | 34 wifi_hotspot_connector_(wifi_hotspot_connector), |
| 30 active_host_(active_host), | 35 active_host_(active_host), |
| 31 tether_host_fetcher_(tether_host_fetcher), | 36 tether_host_fetcher_(tether_host_fetcher), |
| 32 connection_manager_(connection_manager), | 37 connection_manager_(connection_manager), |
| 33 tether_host_response_recorder_(tether_host_response_recorder), | 38 tether_host_response_recorder_(tether_host_response_recorder), |
| 34 device_id_tether_network_guid_map_(device_id_tether_network_guid_map), | 39 device_id_tether_network_guid_map_(device_id_tether_network_guid_map), |
| 40 host_scan_cache_(host_scan_cache), |
| 41 notification_presenter_(notification_presenter), |
| 35 weak_ptr_factory_(this) {} | 42 weak_ptr_factory_(this) {} |
| 36 | 43 |
| 37 TetherConnector::~TetherConnector() { | 44 TetherConnector::~TetherConnector() { |
| 38 if (connect_tethering_operation_) { | 45 if (connect_tethering_operation_) { |
| 39 connect_tethering_operation_->RemoveObserver(this); | 46 connect_tethering_operation_->RemoveObserver(this); |
| 40 } | 47 } |
| 41 } | 48 } |
| 42 | 49 |
| 43 void TetherConnector::ConnectToNetwork( | 50 void TetherConnector::ConnectToNetwork( |
| 44 const std::string& tether_network_guid, | 51 const std::string& tether_network_guid, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 64 | 71 |
| 65 if (!device_id_pending_connection_.empty()) { | 72 if (!device_id_pending_connection_.empty()) { |
| 66 PA_LOG(INFO) << "A connection attempt was already in progress to device " | 73 PA_LOG(INFO) << "A connection attempt was already in progress to device " |
| 67 << "with ID " << device_id_pending_connection_ << ". " | 74 << "with ID " << device_id_pending_connection_ << ". " |
| 68 << "Canceling that connection attempt before continuing."; | 75 << "Canceling that connection attempt before continuing."; |
| 69 CancelConnectionAttempt( | 76 CancelConnectionAttempt( |
| 70 device_id_tether_network_guid_map_->GetTetherNetworkGuidForDeviceId( | 77 device_id_tether_network_guid_map_->GetTetherNetworkGuidForDeviceId( |
| 71 device_id_pending_connection_)); | 78 device_id_pending_connection_)); |
| 72 } | 79 } |
| 73 | 80 |
| 81 if (host_scan_cache_->DoesHostRequireSetup(tether_network_guid)) { |
| 82 const std::string& device_name = |
| 83 network_state_handler_->GetNetworkStateFromGuid(tether_network_guid) |
| 84 ->name(); |
| 85 notification_presenter_->NotifySetupRequired(device_name); |
| 86 } |
| 87 |
| 74 device_id_pending_connection_ = device_id; | 88 device_id_pending_connection_ = device_id; |
| 75 success_callback_ = success_callback; | 89 success_callback_ = success_callback; |
| 76 error_callback_ = error_callback; | 90 error_callback_ = error_callback; |
| 77 active_host_->SetActiveHostConnecting(device_id, tether_network_guid); | 91 active_host_->SetActiveHostConnecting(device_id, tether_network_guid); |
| 78 | 92 |
| 79 tether_host_fetcher_->FetchTetherHost( | 93 tether_host_fetcher_->FetchTetherHost( |
| 80 device_id_pending_connection_, | 94 device_id_pending_connection_, |
| 81 base::Bind(&TetherConnector::OnTetherHostToConnectFetched, | 95 base::Bind(&TetherConnector::OnTetherHostToConnectFetched, |
| 82 weak_ptr_factory_.GetWeakPtr(), | 96 weak_ptr_factory_.GetWeakPtr(), |
| 83 device_id_pending_connection_)); | 97 device_id_pending_connection_)); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 if (!tether_host_to_connect) { | 192 if (!tether_host_to_connect) { |
| 179 PA_LOG(ERROR) << "Could not fetch tether host with device ID " | 193 PA_LOG(ERROR) << "Could not fetch tether host with device ID " |
| 180 << cryptauth::RemoteDevice::TruncateDeviceIdForLogs(device_id) | 194 << cryptauth::RemoteDevice::TruncateDeviceIdForLogs(device_id) |
| 181 << ". Cannot connect."; | 195 << ". Cannot connect."; |
| 182 SetConnectionFailed(NetworkConnectionHandler::kErrorConnectFailed); | 196 SetConnectionFailed(NetworkConnectionHandler::kErrorConnectFailed); |
| 183 return; | 197 return; |
| 184 } | 198 } |
| 185 | 199 |
| 186 DCHECK(device_id == tether_host_to_connect->GetDeviceId()); | 200 DCHECK(device_id == tether_host_to_connect->GetDeviceId()); |
| 187 | 201 |
| 202 // TODO (hansberry): Indicate to ConnectTetheringOperation if first-time setup |
| 203 // is required, so that it can adjust its timeout duration. |
| 188 connect_tethering_operation_ = | 204 connect_tethering_operation_ = |
| 189 ConnectTetheringOperation::Factory::NewInstance( | 205 ConnectTetheringOperation::Factory::NewInstance( |
| 190 *tether_host_to_connect, connection_manager_, | 206 *tether_host_to_connect, connection_manager_, |
| 191 tether_host_response_recorder_); | 207 tether_host_response_recorder_); |
| 192 connect_tethering_operation_->AddObserver(this); | 208 connect_tethering_operation_->AddObserver(this); |
| 193 connect_tethering_operation_->Initialize(); | 209 connect_tethering_operation_->Initialize(); |
| 194 } | 210 } |
| 195 | 211 |
| 196 void TetherConnector::SetConnectionFailed(const std::string& error_name) { | 212 void TetherConnector::SetConnectionFailed(const std::string& error_name) { |
| 197 DCHECK(!device_id_pending_connection_.empty()); | 213 DCHECK(!device_id_pending_connection_.empty()); |
| 198 DCHECK(!error_callback_.is_null()); | 214 DCHECK(!error_callback_.is_null()); |
| 199 | 215 |
| 216 notification_presenter_->RemoveSetupRequiredNotification(); |
| 217 |
| 200 // Save a copy of the callback before resetting it below. | 218 // Save a copy of the callback before resetting it below. |
| 201 network_handler::StringResultCallback error_callback = error_callback_; | 219 network_handler::StringResultCallback error_callback = error_callback_; |
| 202 | 220 |
| 203 device_id_pending_connection_.clear(); | 221 device_id_pending_connection_.clear(); |
| 204 success_callback_.Reset(); | 222 success_callback_.Reset(); |
| 205 error_callback_.Reset(); | 223 error_callback_.Reset(); |
| 206 | 224 |
| 207 error_callback.Run(error_name); | 225 error_callback.Run(error_name); |
| 208 active_host_->SetActiveHostDisconnected(); | 226 active_host_->SetActiveHostDisconnected(); |
| 209 } | 227 } |
| 210 | 228 |
| 211 void TetherConnector::SetConnectionSucceeded( | 229 void TetherConnector::SetConnectionSucceeded( |
| 212 const std::string& device_id, | 230 const std::string& device_id, |
| 213 const std::string& wifi_network_guid) { | 231 const std::string& wifi_network_guid) { |
| 214 DCHECK(!device_id_pending_connection_.empty()); | 232 DCHECK(!device_id_pending_connection_.empty()); |
| 215 DCHECK(device_id_pending_connection_ == device_id); | 233 DCHECK(device_id_pending_connection_ == device_id); |
| 216 DCHECK(!success_callback_.is_null()); | 234 DCHECK(!success_callback_.is_null()); |
| 217 | 235 |
| 236 notification_presenter_->RemoveSetupRequiredNotification(); |
| 237 |
| 218 // Save a copy of the callback before resetting it below. | 238 // Save a copy of the callback before resetting it below. |
| 219 base::Closure success_callback = success_callback_; | 239 base::Closure success_callback = success_callback_; |
| 220 | 240 |
| 221 device_id_pending_connection_.clear(); | 241 device_id_pending_connection_.clear(); |
| 222 success_callback_.Reset(); | 242 success_callback_.Reset(); |
| 223 error_callback_.Reset(); | 243 error_callback_.Reset(); |
| 224 | 244 |
| 225 success_callback.Run(); | 245 success_callback.Run(); |
| 226 active_host_->SetActiveHostConnected( | 246 active_host_->SetActiveHostConnected( |
| 227 device_id, | 247 device_id, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 254 SetConnectionFailed(NetworkConnectionHandler::kErrorConnectFailed); | 274 SetConnectionFailed(NetworkConnectionHandler::kErrorConnectFailed); |
| 255 return; | 275 return; |
| 256 } | 276 } |
| 257 | 277 |
| 258 SetConnectionSucceeded(device_id, wifi_network_guid); | 278 SetConnectionSucceeded(device_id, wifi_network_guid); |
| 259 } | 279 } |
| 260 | 280 |
| 261 } // namespace tether | 281 } // namespace tether |
| 262 | 282 |
| 263 } // namespace chromeos | 283 } // namespace chromeos |
| OLD | NEW |