Chromium Code Reviews| 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_disconnector.h" | 5 #include "chromeos/components/tether/tether_disconnector_impl.h" |
| 6 | 6 |
| 7 #include "base/values.h" | 7 #include "base/values.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/network_configuration_remover.h" | 10 #include "chromeos/components/tether/network_configuration_remover.h" |
| 11 #include "chromeos/components/tether/pref_names.h" | |
| 11 #include "chromeos/components/tether/tether_connector.h" | 12 #include "chromeos/components/tether/tether_connector.h" |
| 12 #include "chromeos/components/tether/tether_host_fetcher.h" | 13 #include "chromeos/components/tether/tether_host_fetcher.h" |
| 13 #include "chromeos/network/network_connection_handler.h" | 14 #include "chromeos/network/network_connection_handler.h" |
| 14 #include "chromeos/network/network_state.h" | 15 #include "chromeos/network/network_state.h" |
| 15 #include "chromeos/network/network_state_handler.h" | 16 #include "chromeos/network/network_state_handler.h" |
| 17 #include "components/prefs/pref_registry_simple.h" | |
| 18 #include "components/prefs/pref_service.h" | |
| 16 #include "components/proximity_auth/logging/logging.h" | 19 #include "components/proximity_auth/logging/logging.h" |
| 17 | 20 |
| 18 namespace chromeos { | 21 namespace chromeos { |
| 19 | 22 |
| 20 namespace tether { | 23 namespace tether { |
| 21 | 24 |
| 22 TetherDisconnector::TetherDisconnector( | 25 namespace { |
| 26 | |
| 27 void OnDisconnectError(const std::string& error_name) { | |
| 28 PA_LOG(WARNING) << "Error disconnecting from Tether network during shutdown; " | |
| 29 << "Error name: " << error_name; | |
| 30 } | |
| 31 | |
| 32 } // namespace | |
| 33 | |
| 34 TetherDisconnectorImpl::TetherDisconnectorImpl( | |
| 23 NetworkConnectionHandler* network_connection_handler, | 35 NetworkConnectionHandler* network_connection_handler, |
| 24 NetworkStateHandler* network_state_handler, | 36 NetworkStateHandler* network_state_handler, |
| 25 ActiveHost* active_host, | 37 ActiveHost* active_host, |
| 26 BleConnectionManager* ble_connection_manager, | 38 BleConnectionManager* ble_connection_manager, |
| 27 NetworkConfigurationRemover* network_configuration_remover, | 39 NetworkConfigurationRemover* network_configuration_remover, |
| 28 TetherConnector* tether_connector, | 40 TetherConnector* tether_connector, |
| 29 DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map, | 41 DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map, |
| 30 TetherHostFetcher* tether_host_fetcher) | 42 TetherHostFetcher* tether_host_fetcher, |
| 43 PrefService* pref_service) | |
| 31 : network_connection_handler_(network_connection_handler), | 44 : network_connection_handler_(network_connection_handler), |
| 32 network_state_handler_(network_state_handler), | 45 network_state_handler_(network_state_handler), |
| 33 active_host_(active_host), | 46 active_host_(active_host), |
| 34 ble_connection_manager_(ble_connection_manager), | 47 ble_connection_manager_(ble_connection_manager), |
| 35 network_configuration_remover_(network_configuration_remover), | 48 network_configuration_remover_(network_configuration_remover), |
| 36 tether_connector_(tether_connector), | 49 tether_connector_(tether_connector), |
| 37 device_id_tether_network_guid_map_(device_id_tether_network_guid_map), | 50 device_id_tether_network_guid_map_(device_id_tether_network_guid_map), |
| 38 tether_host_fetcher_(tether_host_fetcher), | 51 tether_host_fetcher_(tether_host_fetcher), |
| 39 weak_ptr_factory_(this) {} | 52 pref_service_(pref_service), |
| 53 weak_ptr_factory_(this) { | |
| 54 std::string disconnecting_wifi_guid_from_previous_session = | |
| 55 pref_service_->GetString(prefs::kDisconnectingWifiNetworkGuid); | |
| 56 if (!disconnecting_wifi_guid_from_previous_session.empty()) { | |
| 57 // If a previous disconnection attempt was aborted before it could be fully | |
| 58 // completed, clean up the leftover network configuration. | |
| 59 network_configuration_remover_->RemoveNetworkConfiguration( | |
| 60 disconnecting_wifi_guid_from_previous_session); | |
| 61 pref_service_->Set(prefs::kDisconnectingWifiNetworkGuid, base::Value("")); | |
|
Ryan Hansberry
2017/07/11 22:23:56
nit: use ClearPref instead
Kyle Horimoto
2017/07/12 01:42:59
Done.
| |
| 62 } | |
| 63 } | |
| 40 | 64 |
| 41 TetherDisconnector::~TetherDisconnector() { | 65 TetherDisconnectorImpl::~TetherDisconnectorImpl() { |
| 42 if (disconnect_tethering_operation_) | 66 if (disconnect_tethering_operation_) |
| 43 disconnect_tethering_operation_->RemoveObserver(this); | 67 disconnect_tethering_operation_->RemoveObserver(this); |
| 68 | |
| 69 std::string active_tether_guid = active_host_->GetTetherNetworkGuid(); | |
| 70 if (!active_tether_guid.empty()) { | |
| 71 PA_LOG(INFO) << "There was an active Tether connection during Tether " | |
| 72 << "shutdown. Initiating disconnection from network with GUID " | |
| 73 << "\"" << active_tether_guid << "\""; | |
| 74 DisconnectFromNetwork(active_tether_guid, base::Bind(&base::DoNothing), | |
| 75 base::Bind(&OnDisconnectError)); | |
| 76 } | |
| 44 } | 77 } |
| 45 | 78 |
| 46 void TetherDisconnector::DisconnectFromNetwork( | 79 void TetherDisconnectorImpl::DisconnectFromNetwork( |
| 47 const std::string& tether_network_guid, | 80 const std::string& tether_network_guid, |
| 48 const base::Closure& success_callback, | 81 const base::Closure& success_callback, |
| 49 const network_handler::StringResultCallback& error_callback) { | 82 const network_handler::StringResultCallback& error_callback) { |
| 50 DCHECK(!tether_network_guid.empty()); | 83 DCHECK(!tether_network_guid.empty()); |
| 51 | 84 |
| 52 ActiveHost::ActiveHostStatus status = active_host_->GetActiveHostStatus(); | 85 ActiveHost::ActiveHostStatus status = active_host_->GetActiveHostStatus(); |
| 53 std::string active_tether_network_guid = active_host_->GetTetherNetworkGuid(); | 86 std::string active_tether_network_guid = active_host_->GetTetherNetworkGuid(); |
| 54 std::string active_wifi_network_guid = active_host_->GetWifiNetworkGuid(); | 87 std::string active_wifi_network_guid = active_host_->GetWifiNetworkGuid(); |
| 55 | 88 |
| 56 if (status == ActiveHost::ActiveHostStatus::DISCONNECTED) { | 89 if (status == ActiveHost::ActiveHostStatus::DISCONNECTED) { |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 85 error_callback.Run(NetworkConnectionHandler::kErrorDisconnectFailed); | 118 error_callback.Run(NetworkConnectionHandler::kErrorDisconnectFailed); |
| 86 return; | 119 return; |
| 87 } | 120 } |
| 88 | 121 |
| 89 DCHECK(!active_wifi_network_guid.empty()); | 122 DCHECK(!active_wifi_network_guid.empty()); |
| 90 DCHECK(!disconnect_tethering_operation_); | 123 DCHECK(!disconnect_tethering_operation_); |
| 91 DisconnectActiveWifiConnection(tether_network_guid, active_wifi_network_guid, | 124 DisconnectActiveWifiConnection(tether_network_guid, active_wifi_network_guid, |
| 92 success_callback, error_callback); | 125 success_callback, error_callback); |
| 93 } | 126 } |
| 94 | 127 |
| 95 void TetherDisconnector::DisconnectActiveWifiConnection( | 128 void TetherDisconnectorImpl::DisconnectActiveWifiConnection( |
| 96 const std::string& tether_network_guid, | 129 const std::string& tether_network_guid, |
| 97 const std::string& wifi_network_guid, | 130 const std::string& wifi_network_guid, |
| 98 const base::Closure& success_callback, | 131 const base::Closure& success_callback, |
| 99 const network_handler::StringResultCallback& error_callback) { | 132 const network_handler::StringResultCallback& error_callback) { |
| 100 // First, disconnect the active host so that the user gets visual indication | 133 // First, disconnect the active host so that the user gets visual indication |
| 101 // that the disconnection is in progress as quickly as possible. | 134 // that the disconnection is in progress as quickly as possible. |
| 102 // TODO(hansberry): This will result in the Tether network becoming | 135 // TODO(hansberry): This will result in the Tether network becoming |
| 103 // disconnected, but the Wi-Fi network will still be connected until the | 136 // disconnected, but the Wi-Fi network will still be connected until the |
| 104 // DisconnectNetwork() call below completes. This will result in a jarring UI | 137 // DisconnectNetwork() call below completes. This will result in a jarring UI |
| 105 // transition which needs to be fixed. | 138 // transition which needs to be fixed. |
| 106 active_host_->SetActiveHostDisconnected(); | 139 active_host_->SetActiveHostDisconnected(); |
| 107 | 140 |
| 141 // Before starting disconnection, log the disconnecting Wi-Fi GUID to prefs. | |
| 142 // Under normal circumstances, the GUID will be cleared as part of | |
| 143 // CleanUpAfterWifiDisconnection(). However, when the user logs out, | |
| 144 // this TetherDisconnectorImpl instance will be deleted before one of the | |
| 145 // callbacks passed below to DisconnectNetwork() can be called, and the | |
| 146 // GUID will remain in prefs until the next time the user logs in, at which | |
| 147 // time the associated network configuration can be removed. | |
| 148 pref_service_->Set(prefs::kDisconnectingWifiNetworkGuid, | |
| 149 base::Value(wifi_network_guid)); | |
| 150 | |
| 108 const NetworkState* wifi_network_state = | 151 const NetworkState* wifi_network_state = |
| 109 network_state_handler_->GetNetworkStateFromGuid(wifi_network_guid); | 152 network_state_handler_->GetNetworkStateFromGuid(wifi_network_guid); |
| 110 if (wifi_network_state) { | 153 if (wifi_network_state) { |
| 111 network_connection_handler_->DisconnectNetwork( | 154 network_connection_handler_->DisconnectNetwork( |
| 112 wifi_network_state->path(), | 155 wifi_network_state->path(), |
| 113 base::Bind(&TetherDisconnector::OnSuccessfulWifiDisconnect, | 156 base::Bind(&TetherDisconnectorImpl::OnSuccessfulWifiDisconnect, |
| 114 weak_ptr_factory_.GetWeakPtr(), wifi_network_guid, | 157 weak_ptr_factory_.GetWeakPtr(), wifi_network_guid, |
| 115 success_callback, error_callback), | 158 success_callback, error_callback), |
| 116 base::Bind(&TetherDisconnector::OnFailedWifiDisconnect, | 159 base::Bind(&TetherDisconnectorImpl::OnFailedWifiDisconnect, |
| 117 weak_ptr_factory_.GetWeakPtr(), wifi_network_guid, | 160 weak_ptr_factory_.GetWeakPtr(), wifi_network_guid, |
| 118 success_callback, error_callback)); | 161 success_callback, error_callback)); |
| 119 } else { | 162 } else { |
| 120 PA_LOG(ERROR) << "Wi-Fi NetworkState for GUID " << wifi_network_guid << " " | 163 PA_LOG(ERROR) << "Wi-Fi NetworkState for GUID " << wifi_network_guid << " " |
| 121 << "was not registered. Cannot disconnect."; | 164 << "was not registered. Cannot disconnect."; |
| 122 error_callback.Run(NetworkConnectionHandler::kErrorDisconnectFailed); | 165 error_callback.Run(NetworkConnectionHandler::kErrorDisconnectFailed); |
| 123 } | 166 } |
| 124 | 167 |
| 125 // In addition to disconnecting from the Wi-Fi network, this device must also | 168 // In addition to disconnecting from the Wi-Fi network, this device must also |
| 126 // send a DisconnectTetheringRequest to the tether host so that it can shut | 169 // send a DisconnectTetheringRequest to the tether host so that it can shut |
| 127 // down its Wi-Fi hotspot if it is no longer in use. | 170 // down its Wi-Fi hotspot if it is no longer in use. |
| 128 const std::string device_id = | 171 const std::string device_id = |
| 129 device_id_tether_network_guid_map_->GetDeviceIdForTetherNetworkGuid( | 172 device_id_tether_network_guid_map_->GetDeviceIdForTetherNetworkGuid( |
| 130 tether_network_guid); | 173 tether_network_guid); |
| 131 tether_host_fetcher_->FetchTetherHost( | 174 tether_host_fetcher_->FetchTetherHost( |
| 132 device_id, base::Bind(&TetherDisconnector::OnTetherHostFetched, | 175 device_id, base::Bind(&TetherDisconnectorImpl::OnTetherHostFetched, |
| 133 weak_ptr_factory_.GetWeakPtr(), device_id)); | 176 weak_ptr_factory_.GetWeakPtr(), device_id)); |
| 134 } | 177 } |
| 135 | 178 |
| 136 void TetherDisconnector::OnOperationFinished(const std::string& device_id, | 179 void TetherDisconnectorImpl::OnOperationFinished(const std::string& device_id, |
| 137 bool success) { | 180 bool success) { |
| 138 if (success) { | 181 if (success) { |
| 139 PA_LOG(INFO) << "Successfully sent DisconnectTetheringRequest to device " | 182 PA_LOG(INFO) << "Successfully sent DisconnectTetheringRequest to device " |
| 140 << "with ID " | 183 << "with ID " |
| 141 << cryptauth::RemoteDevice::TruncateDeviceIdForLogs(device_id); | 184 << cryptauth::RemoteDevice::TruncateDeviceIdForLogs(device_id); |
| 142 } else { | 185 } else { |
| 143 PA_LOG(ERROR) << "Failed to send DisconnectTetheringRequest to device " | 186 PA_LOG(ERROR) << "Failed to send DisconnectTetheringRequest to device " |
| 144 << "with ID " | 187 << "with ID " |
| 145 << cryptauth::RemoteDevice::TruncateDeviceIdForLogs( | 188 << cryptauth::RemoteDevice::TruncateDeviceIdForLogs( |
| 146 device_id); | 189 device_id); |
| 147 } | 190 } |
| 148 | 191 |
| 149 // Regardless of success/failure, unregister as a listener and delete the | 192 // Regardless of success/failure, unregister as a listener and delete the |
| 150 // operation. | 193 // operation. |
| 151 disconnect_tethering_operation_->RemoveObserver(this); | 194 disconnect_tethering_operation_->RemoveObserver(this); |
| 152 disconnect_tethering_operation_.reset(); | 195 disconnect_tethering_operation_.reset(); |
| 153 } | 196 } |
| 154 | 197 |
| 155 void TetherDisconnector::OnSuccessfulWifiDisconnect( | 198 void TetherDisconnectorImpl::OnSuccessfulWifiDisconnect( |
| 156 const std::string& wifi_network_guid, | 199 const std::string& wifi_network_guid, |
| 157 const base::Closure& success_callback, | 200 const base::Closure& success_callback, |
| 158 const network_handler::StringResultCallback& error_callback) { | 201 const network_handler::StringResultCallback& error_callback) { |
| 159 PA_LOG(INFO) << "Successfully disconnected from Wi-Fi network with GUID " | 202 PA_LOG(INFO) << "Successfully disconnected from Wi-Fi network with GUID " |
| 160 << wifi_network_guid << "."; | 203 << wifi_network_guid << "."; |
| 161 CleanUpAfterWifiDisconnection(true /* success */, wifi_network_guid, | 204 CleanUpAfterWifiDisconnection(true /* success */, wifi_network_guid, |
| 162 success_callback, error_callback); | 205 success_callback, error_callback); |
| 163 } | 206 } |
| 164 | 207 |
| 165 void TetherDisconnector::OnFailedWifiDisconnect( | 208 void TetherDisconnectorImpl::OnFailedWifiDisconnect( |
| 166 const std::string& wifi_network_guid, | 209 const std::string& wifi_network_guid, |
| 167 const base::Closure& success_callback, | 210 const base::Closure& success_callback, |
| 168 const network_handler::StringResultCallback& error_callback, | 211 const network_handler::StringResultCallback& error_callback, |
| 169 const std::string& error_name, | 212 const std::string& error_name, |
| 170 std::unique_ptr<base::DictionaryValue> error_data) { | 213 std::unique_ptr<base::DictionaryValue> error_data) { |
| 171 PA_LOG(ERROR) << "Failed to disconnect from Wi-Fi network with GUID " | 214 PA_LOG(ERROR) << "Failed to disconnect from Wi-Fi network with GUID " |
| 172 << wifi_network_guid << ". Error name: " << error_name; | 215 << wifi_network_guid << ". Error name: " << error_name; |
| 173 CleanUpAfterWifiDisconnection(false /* success */, wifi_network_guid, | 216 CleanUpAfterWifiDisconnection(false /* success */, wifi_network_guid, |
| 174 success_callback, error_callback); | 217 success_callback, error_callback); |
| 175 } | 218 } |
| 176 | 219 |
| 177 void TetherDisconnector::CleanUpAfterWifiDisconnection( | 220 void TetherDisconnectorImpl::CleanUpAfterWifiDisconnection( |
| 178 bool success, | 221 bool success, |
| 179 const std::string& wifi_network_guid, | 222 const std::string& wifi_network_guid, |
| 180 const base::Closure& success_callback, | 223 const base::Closure& success_callback, |
| 181 const network_handler::StringResultCallback& error_callback) { | 224 const network_handler::StringResultCallback& error_callback) { |
| 225 network_configuration_remover_->RemoveNetworkConfiguration(wifi_network_guid); | |
| 226 pref_service_->Set(prefs::kDisconnectingWifiNetworkGuid, base::Value("")); | |
|
Ryan Hansberry
2017/07/11 22:23:56
same nit here, use ClearPref
| |
| 227 | |
| 182 if (success) | 228 if (success) |
| 183 success_callback.Run(); | 229 success_callback.Run(); |
| 184 else | 230 else |
| 185 error_callback.Run(NetworkConnectionHandler::kErrorDisconnectFailed); | 231 error_callback.Run(NetworkConnectionHandler::kErrorDisconnectFailed); |
| 186 | |
| 187 network_configuration_remover_->RemoveNetworkConfiguration(wifi_network_guid); | |
| 188 } | 232 } |
| 189 | 233 |
| 190 void TetherDisconnector::OnTetherHostFetched( | 234 void TetherDisconnectorImpl::OnTetherHostFetched( |
| 191 const std::string& device_id, | 235 const std::string& device_id, |
| 192 std::unique_ptr<cryptauth::RemoteDevice> tether_host) { | 236 std::unique_ptr<cryptauth::RemoteDevice> tether_host) { |
| 193 if (!tether_host) { | 237 if (!tether_host) { |
| 194 PA_LOG(ERROR) << "Could not fetch device with ID " | 238 PA_LOG(ERROR) << "Could not fetch device with ID " |
| 195 << cryptauth::RemoteDevice::TruncateDeviceIdForLogs(device_id) | 239 << cryptauth::RemoteDevice::TruncateDeviceIdForLogs(device_id) |
| 196 << ". Unable to send DisconnectTetheringRequest."; | 240 << ". Unable to send DisconnectTetheringRequest."; |
| 197 return; | 241 return; |
| 198 } | 242 } |
| 199 | 243 |
| 200 disconnect_tethering_operation_ = | 244 disconnect_tethering_operation_ = |
| 201 DisconnectTetheringOperation::Factory::NewInstance( | 245 DisconnectTetheringOperation::Factory::NewInstance( |
| 202 *tether_host, ble_connection_manager_); | 246 *tether_host, ble_connection_manager_); |
| 203 | 247 |
| 204 // Start the operation; OnOperationFinished() will be called when finished. | 248 // Start the operation; OnOperationFinished() will be called when finished. |
| 205 disconnect_tethering_operation_->AddObserver(this); | 249 disconnect_tethering_operation_->AddObserver(this); |
| 206 disconnect_tethering_operation_->Initialize(); | 250 disconnect_tethering_operation_->Initialize(); |
| 207 } | 251 } |
| 208 | 252 |
| 209 } // namespace tether | 253 } // namespace tether |
| 210 | 254 |
| 211 } // namespace chromeos | 255 } // namespace chromeos |
| OLD | NEW |