OLD | NEW |
---|---|
(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 #include "chromeos/components/tether/tether_disconnector.h" | |
6 | |
7 #include "base/values.h" | |
8 #include "chromeos/components/tether/active_host.h" | |
9 #include "chromeos/components/tether/device_id_tether_network_guid_map.h" | |
10 #include "chromeos/components/tether/network_configuration_remover.h" | |
11 #include "chromeos/components/tether/tether_connector.h" | |
12 #include "chromeos/components/tether/tether_host_fetcher.h" | |
13 #include "chromeos/network/network_connection_handler.h" | |
14 #include "chromeos/network/network_state.h" | |
15 #include "chromeos/network/network_state_handler.h" | |
16 #include "components/proximity_auth/logging/logging.h" | |
17 | |
18 namespace chromeos { | |
19 | |
20 namespace tether { | |
21 | |
22 TetherDisconnector::TetherDisconnector( | |
23 NetworkConnectionHandler* network_connection_handler, | |
24 NetworkStateHandler* network_state_handler, | |
25 ActiveHost* active_host, | |
26 BleConnectionManager* ble_connection_manager, | |
27 NetworkConfigurationRemover* network_configuration_remover, | |
28 TetherConnector* tether_connector, | |
29 DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map, | |
30 TetherHostFetcher* tether_host_fetcher) | |
31 : network_connection_handler_(network_connection_handler), | |
32 network_state_handler_(network_state_handler), | |
33 active_host_(active_host), | |
34 ble_connection_manager_(ble_connection_manager), | |
35 network_configuration_remover_(network_configuration_remover), | |
36 tether_connector_(tether_connector), | |
37 device_id_tether_network_guid_map_(device_id_tether_network_guid_map), | |
38 tether_host_fetcher_(tether_host_fetcher), | |
39 weak_ptr_factory_(this) {} | |
40 | |
41 TetherDisconnector::~TetherDisconnector() { | |
42 if (disconnect_tethering_operation_) | |
43 disconnect_tethering_operation_->RemoveObserver(this); | |
44 } | |
45 | |
46 void TetherDisconnector::DisconnectFromNetwork( | |
47 const std::string& tether_network_guid, | |
48 const base::Closure& success_callback, | |
49 const network_handler::StringResultCallback& error_callback) { | |
50 DCHECK(!tether_network_guid.empty()); | |
51 | |
52 ActiveHost::ActiveHostStatus status = active_host_->GetActiveHostStatus(); | |
53 std::string active_tether_network_guid = active_host_->GetTetherNetworkGuid(); | |
54 std::string active_wifi_network_guid = active_host_->GetWifiNetworkGuid(); | |
55 | |
56 if (status == ActiveHost::ActiveHostStatus::DISCONNECTED) { | |
57 PA_LOG(ERROR) << "Disconnect requested for Tether network with GUID " | |
58 << tether_network_guid << ", but no device is connected."; | |
59 error_callback.Run(NetworkConnectionHandler::kErrorNotConnected); | |
60 return; | |
61 } | |
62 | |
63 if (tether_network_guid != active_tether_network_guid) { | |
64 PA_LOG(ERROR) << "Disconnect requested for Tether network with GUID " | |
65 << tether_network_guid << ", but that device is not the " | |
66 << "active host."; | |
67 error_callback.Run(NetworkConnectionHandler::kErrorNotConnected); | |
68 return; | |
69 } | |
70 | |
71 if (status == ActiveHost::ActiveHostStatus::CONNECTING) { | |
72 // Note: CancelConnectionAttempt() internally sets the active host to be | |
73 // disconnected. | |
74 if (tether_connector_->CancelConnectionAttempt(tether_network_guid)) { | |
75 PA_LOG(INFO) << "Disconnect requested for Tether network with GUID " | |
76 << tether_network_guid << ", which had not yet connected. " | |
77 << "Canceled in-progress connection attempt."; | |
78 success_callback.Run(); | |
79 return; | |
80 } | |
81 | |
82 PA_LOG(ERROR) << "Disconnect requested for Tether network with GUID " | |
83 << tether_network_guid << " (not yet connected), but " | |
84 << "canceling connection attempt failed."; | |
85 error_callback.Run(NetworkConnectionHandler::kErrorDisconnectFailed); | |
86 return; | |
87 } | |
88 | |
89 DCHECK(!active_wifi_network_guid.empty()); | |
90 DisconnectActiveWifiConnection(tether_network_guid, active_wifi_network_guid, | |
Ryan Hansberry
2017/05/04 01:56:28
I think it would be safe to ensure disconnect_teth
Kyle Horimoto
2017/05/04 02:00:14
That won't be possible. Once the active host is se
| |
91 success_callback, error_callback); | |
92 } | |
93 | |
94 void TetherDisconnector::DisconnectActiveWifiConnection( | |
95 const std::string& tether_network_guid, | |
96 const std::string& wifi_network_guid, | |
97 const base::Closure& success_callback, | |
98 const network_handler::StringResultCallback& error_callback) { | |
99 // First, disconnect the active host so that the user gets visual indication | |
100 // that the disconnection is in progress as quickly as possible. | |
101 // TODO(hansberry): This will result in the Tether network becoming | |
102 // disconnected, but the Wi-Fi network will still be connected until the | |
103 // DisconnectNetwork() call below completes. This will result in a jarring UI | |
104 // transition which needs to be fixed. | |
105 active_host_->SetActiveHostDisconnected(); | |
106 | |
107 const NetworkState* wifi_network_state = | |
108 network_state_handler_->GetNetworkStateFromGuid(wifi_network_guid); | |
109 if (wifi_network_state) { | |
110 network_connection_handler_->DisconnectNetwork( | |
111 wifi_network_state->path(), | |
112 base::Bind(&TetherDisconnector::OnSuccessfulWifiDisconnect, | |
113 weak_ptr_factory_.GetWeakPtr(), wifi_network_guid, | |
114 success_callback, error_callback), | |
115 base::Bind(&TetherDisconnector::OnFailedWifiDisconnect, | |
116 weak_ptr_factory_.GetWeakPtr(), wifi_network_guid, | |
117 success_callback, error_callback)); | |
118 } else { | |
119 PA_LOG(ERROR) << "Wi-Fi NetworkState for GUID " << wifi_network_guid << " " | |
120 << "was not registered. Cannot disconnect."; | |
121 error_callback.Run(NetworkConnectionHandler::kErrorDisconnectFailed); | |
122 } | |
123 | |
124 // In addition to disconnecting from the Wi-Fi network, this device must also | |
125 // send a DisconnectTetheringRequest to the tether host so that it can shut | |
126 // down its Wi-Fi hotspot if it is no longer in use. | |
127 const std::string device_id = | |
128 device_id_tether_network_guid_map_->GetDeviceIdForTetherNetworkGuid( | |
129 tether_network_guid); | |
130 tether_host_fetcher_->FetchTetherHost( | |
131 device_id, base::Bind(&TetherDisconnector::OnTetherHostFetched, | |
132 weak_ptr_factory_.GetWeakPtr(), device_id)); | |
133 } | |
134 | |
135 void TetherDisconnector::OnOperationFinished(const std::string& device_id, | |
136 bool success) { | |
137 if (success) { | |
138 PA_LOG(INFO) << "Successfully sent DisconnectTetheringRequest to device " | |
139 << "with ID " | |
140 << cryptauth::RemoteDevice::TruncateDeviceIdForLogs(device_id); | |
141 } else { | |
142 PA_LOG(ERROR) << "Failed to send DisconnectTetheringRequest to device " | |
143 << "with ID " | |
144 << cryptauth::RemoteDevice::TruncateDeviceIdForLogs( | |
145 device_id); | |
146 } | |
147 | |
148 // Regardless of success/failure, unregister as a listener and delete the | |
149 // operation. | |
150 disconnect_tethering_operation_->RemoveObserver(this); | |
151 disconnect_tethering_operation_.reset(); | |
152 } | |
153 | |
154 void TetherDisconnector::OnSuccessfulWifiDisconnect( | |
155 const std::string& wifi_network_guid, | |
156 const base::Closure& success_callback, | |
157 const network_handler::StringResultCallback& error_callback) { | |
158 PA_LOG(INFO) << "Successfully disconnected from Wi-Fi network with GUID " | |
159 << wifi_network_guid << "."; | |
160 CleanUpAfterWifiDisconnection(true /* success */, wifi_network_guid, | |
161 success_callback, error_callback); | |
162 } | |
163 | |
164 void TetherDisconnector::OnFailedWifiDisconnect( | |
165 const std::string& wifi_network_guid, | |
166 const base::Closure& success_callback, | |
167 const network_handler::StringResultCallback& error_callback, | |
168 const std::string& error_name, | |
169 std::unique_ptr<base::DictionaryValue> error_data) { | |
170 PA_LOG(ERROR) << "Failed to disconnect from Wi-Fi network with GUID " | |
171 << wifi_network_guid << ". Error name: " << error_name; | |
172 CleanUpAfterWifiDisconnection(false /* success */, wifi_network_guid, | |
173 success_callback, error_callback); | |
174 } | |
175 | |
176 void TetherDisconnector::CleanUpAfterWifiDisconnection( | |
177 bool success, | |
178 const std::string& wifi_network_guid, | |
179 const base::Closure& success_callback, | |
180 const network_handler::StringResultCallback& error_callback) { | |
181 if (success) | |
182 success_callback.Run(); | |
183 else | |
184 error_callback.Run(NetworkConnectionHandler::kErrorDisconnectFailed); | |
185 | |
186 network_configuration_remover_->RemoveNetworkConfiguration(wifi_network_guid); | |
187 } | |
188 | |
189 void TetherDisconnector::OnTetherHostFetched( | |
190 const std::string& device_id, | |
191 std::unique_ptr<cryptauth::RemoteDevice> tether_host) { | |
192 if (!tether_host) { | |
193 PA_LOG(ERROR) << "Could not fetch device with ID " | |
194 << cryptauth::RemoteDevice::TruncateDeviceIdForLogs(device_id) | |
195 << ". Unable to send DisconnectTetheringRequest."; | |
196 return; | |
197 } | |
198 | |
199 disconnect_tethering_operation_ = | |
200 DisconnectTetheringOperation::Factory::NewInstance( | |
201 *tether_host, ble_connection_manager_); | |
202 | |
203 // Start the operation; OnOperationFinished() will be called when finished. | |
204 disconnect_tethering_operation_->AddObserver(this); | |
205 disconnect_tethering_operation_->Initialize(); | |
206 } | |
207 | |
208 } // namespace tether | |
209 | |
210 } // namespace chromeos | |
OLD | NEW |