OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/network/network_device_handler_impl.h" | 5 #include "chromeos/network/network_device_handler_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/message_loop/message_loop_proxy.h" |
| 10 #include "base/time/time.h" |
9 #include "base/values.h" | 11 #include "base/values.h" |
10 #include "chromeos/dbus/dbus_thread_manager.h" | 12 #include "chromeos/dbus/dbus_thread_manager.h" |
11 #include "chromeos/dbus/shill_device_client.h" | 13 #include "chromeos/dbus/shill_device_client.h" |
12 #include "chromeos/dbus/shill_ipconfig_client.h" | 14 #include "chromeos/dbus/shill_ipconfig_client.h" |
13 #include "chromeos/network/device_state.h" | 15 #include "chromeos/network/device_state.h" |
14 #include "chromeos/network/network_event_log.h" | 16 #include "chromeos/network/network_event_log.h" |
15 #include "chromeos/network/network_handler_callbacks.h" | 17 #include "chromeos/network/network_handler_callbacks.h" |
16 #include "chromeos/network/network_state_handler.h" | 18 #include "chromeos/network/network_state_handler.h" |
17 #include "chromeos/network/shill_property_util.h" | 19 #include "chromeos/network/shill_property_util.h" |
18 #include "dbus/object_path.h" | 20 #include "dbus/object_path.h" |
19 #include "third_party/cros_system_api/dbus/service_constants.h" | 21 #include "third_party/cros_system_api/dbus/service_constants.h" |
20 | 22 |
21 namespace chromeos { | 23 namespace chromeos { |
22 | 24 |
23 namespace { | 25 namespace { |
24 | 26 |
25 std::string GetErrorNameForShillError(const std::string& shill_error_name) { | 27 std::string GetErrorNameForShillError(const std::string& shill_error_name) { |
26 if (shill_error_name == shill::kErrorResultFailure) | 28 if (shill_error_name == shill::kErrorResultFailure) |
27 return NetworkDeviceHandler::kErrorFailure; | 29 return NetworkDeviceHandler::kErrorFailure; |
28 if (shill_error_name == shill::kErrorResultNotSupported) | 30 if (shill_error_name == shill::kErrorResultNotSupported) |
29 return NetworkDeviceHandler::kErrorNotSupported; | 31 return NetworkDeviceHandler::kErrorNotSupported; |
30 if (shill_error_name == shill::kErrorResultIncorrectPin) | 32 if (shill_error_name == shill::kErrorResultIncorrectPin) |
31 return NetworkDeviceHandler::kErrorIncorrectPin; | 33 return NetworkDeviceHandler::kErrorIncorrectPin; |
32 if (shill_error_name == shill::kErrorResultPinBlocked) | 34 if (shill_error_name == shill::kErrorResultPinBlocked) |
33 return NetworkDeviceHandler::kErrorPinBlocked; | 35 return NetworkDeviceHandler::kErrorPinBlocked; |
34 if (shill_error_name == shill::kErrorResultPinRequired) | 36 if (shill_error_name == shill::kErrorResultPinRequired) |
35 return NetworkDeviceHandler::kErrorPinRequired; | 37 return NetworkDeviceHandler::kErrorPinRequired; |
| 38 if (shill_error_name == shill::kErrorResultNotFound) |
| 39 return NetworkDeviceHandler::kErrorDeviceMissing; |
36 return NetworkDeviceHandler::kErrorUnknown; | 40 return NetworkDeviceHandler::kErrorUnknown; |
37 } | 41 } |
38 | 42 |
39 void InvokeErrorCallback(const std::string& service_path, | 43 void InvokeErrorCallback(const std::string& service_path, |
40 const network_handler::ErrorCallback& error_callback, | 44 const network_handler::ErrorCallback& error_callback, |
41 const std::string& error_name) { | 45 const std::string& error_name) { |
42 std::string error_msg = "Device Error: " + error_name; | 46 std::string error_msg = "Device Error: " + error_name; |
43 NET_LOG_ERROR(error_msg, service_path); | 47 NET_LOG_ERROR(error_msg, service_path); |
44 network_handler::RunErrorCallback( | 48 network_handler::RunErrorCallback( |
45 error_callback, service_path, error_name, error_msg); | 49 error_callback, service_path, error_name, error_msg); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 const base::Closure& callback, | 131 const base::Closure& callback, |
128 const network_handler::ErrorCallback& error_callback) { | 132 const network_handler::ErrorCallback& error_callback) { |
129 DBusThreadManager::Get()->GetShillDeviceClient()->SetProperty( | 133 DBusThreadManager::Get()->GetShillDeviceClient()->SetProperty( |
130 dbus::ObjectPath(device_path), | 134 dbus::ObjectPath(device_path), |
131 property_name, | 135 property_name, |
132 value, | 136 value, |
133 callback, | 137 callback, |
134 base::Bind(&HandleShillCallFailure, device_path, error_callback)); | 138 base::Bind(&HandleShillCallFailure, device_path, error_callback)); |
135 } | 139 } |
136 | 140 |
| 141 // Struct containing TDLS Operation parameters. |
| 142 struct TDLSOperationParams { |
| 143 TDLSOperationParams() : retry_count(0) {} |
| 144 std::string operation; |
| 145 std::string ip_or_mac_address; |
| 146 int retry_count; |
| 147 }; |
| 148 |
| 149 // Forward declare for PostDelayedTask. |
| 150 void CallPerformTDLSOperation( |
| 151 const std::string& device_path, |
| 152 const TDLSOperationParams& params, |
| 153 const network_handler::StringResultCallback& callback, |
| 154 const network_handler::ErrorCallback& error_callback); |
| 155 |
| 156 void TDLSSuccessCallback( |
| 157 const std::string& device_path, |
| 158 const TDLSOperationParams& params, |
| 159 const network_handler::StringResultCallback& callback, |
| 160 const network_handler::ErrorCallback& error_callback, |
| 161 const std::string& result) { |
| 162 std::string event_desc = "TDLSSuccessCallback: " + params.operation; |
| 163 if (!result.empty()) |
| 164 event_desc += ": " + result; |
| 165 NET_LOG_EVENT(event_desc, device_path); |
| 166 if (params.operation != shill::kTDLSSetupOperation) { |
| 167 if (!callback.is_null()) |
| 168 callback.Run(result); |
| 169 return; |
| 170 } |
| 171 |
| 172 if (!result.empty()) |
| 173 NET_LOG_ERROR("Unexpected TDLS result: " + result, device_path); |
| 174 |
| 175 // Send a delayed Status request after a successful Setup call. |
| 176 TDLSOperationParams status_params; |
| 177 status_params.operation = shill::kTDLSStatusOperation; |
| 178 status_params.ip_or_mac_address = params.ip_or_mac_address; |
| 179 |
| 180 const int64 kRequestStatusDelayMs = 500; |
| 181 base::TimeDelta request_delay; |
| 182 if (!DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface()) |
| 183 request_delay = base::TimeDelta::FromMilliseconds(kRequestStatusDelayMs); |
| 184 |
| 185 base::MessageLoopProxy::current()->PostDelayedTask( |
| 186 FROM_HERE, |
| 187 base::Bind(&CallPerformTDLSOperation, |
| 188 device_path, status_params, callback, error_callback), |
| 189 request_delay); |
| 190 } |
| 191 |
| 192 void TDLSErrorCallback( |
| 193 const std::string& device_path, |
| 194 const TDLSOperationParams& params, |
| 195 const network_handler::StringResultCallback& callback, |
| 196 const network_handler::ErrorCallback& error_callback, |
| 197 const std::string& dbus_error_name, |
| 198 const std::string& dbus_error_message) { |
| 199 // If a Setup operation receives an InProgress error, retry. |
| 200 const int kMaxRetries = 5; |
| 201 if (params.operation == shill::kTDLSSetupOperation && |
| 202 dbus_error_name == shill::kErrorResultInProgress && |
| 203 params.retry_count < kMaxRetries) { |
| 204 TDLSOperationParams retry_params = params; |
| 205 ++retry_params.retry_count; |
| 206 NET_LOG_EVENT(base::StringPrintf("TDLS Retry: %d", params.retry_count), |
| 207 device_path); |
| 208 const int64 kReRequestDelayMs = 1000; |
| 209 base::TimeDelta request_delay; |
| 210 if (!DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface()) |
| 211 request_delay = base::TimeDelta::FromMilliseconds(kReRequestDelayMs); |
| 212 |
| 213 base::MessageLoopProxy::current()->PostDelayedTask( |
| 214 FROM_HERE, |
| 215 base::Bind(&CallPerformTDLSOperation, |
| 216 device_path, retry_params, callback, error_callback), |
| 217 request_delay); |
| 218 return; |
| 219 } |
| 220 |
| 221 NET_LOG_ERROR("TDLS Error:" + dbus_error_name + ":" + dbus_error_message, |
| 222 device_path); |
| 223 if (error_callback.is_null()) |
| 224 return; |
| 225 |
| 226 const std::string error_name = |
| 227 dbus_error_name == shill::kErrorResultInProgress ? |
| 228 NetworkDeviceHandler::kErrorTimeout : NetworkDeviceHandler::kErrorUnknown; |
| 229 const std::string& error_detail = params.ip_or_mac_address; |
| 230 scoped_ptr<base::DictionaryValue> error_data( |
| 231 network_handler::CreateDBusErrorData( |
| 232 device_path, error_name, error_detail, |
| 233 dbus_error_name, dbus_error_message)); |
| 234 error_callback.Run(error_name, error_data.Pass()); |
| 235 } |
| 236 |
| 237 void CallPerformTDLSOperation( |
| 238 const std::string& device_path, |
| 239 const TDLSOperationParams& params, |
| 240 const network_handler::StringResultCallback& callback, |
| 241 const network_handler::ErrorCallback& error_callback) { |
| 242 NET_LOG_EVENT("CallPerformTDLSOperation: " + params.operation, device_path); |
| 243 DBusThreadManager::Get()->GetShillDeviceClient()->PerformTDLSOperation( |
| 244 dbus::ObjectPath(device_path), |
| 245 params.operation, |
| 246 params.ip_or_mac_address, |
| 247 base::Bind(&TDLSSuccessCallback, |
| 248 device_path, params, callback, error_callback), |
| 249 base::Bind(&TDLSErrorCallback, |
| 250 device_path, params, callback, error_callback)); |
| 251 } |
| 252 |
137 } // namespace | 253 } // namespace |
138 | 254 |
139 NetworkDeviceHandlerImpl::~NetworkDeviceHandlerImpl() { | 255 NetworkDeviceHandlerImpl::~NetworkDeviceHandlerImpl() { |
140 network_state_handler_->RemoveObserver(this, FROM_HERE); | 256 network_state_handler_->RemoveObserver(this, FROM_HERE); |
141 } | 257 } |
142 | 258 |
143 void NetworkDeviceHandlerImpl::GetDeviceProperties( | 259 void NetworkDeviceHandlerImpl::GetDeviceProperties( |
144 const std::string& device_path, | 260 const std::string& device_path, |
145 const network_handler::DictionaryResultCallback& callback, | 261 const network_handler::DictionaryResultCallback& callback, |
146 const network_handler::ErrorCallback& error_callback) const { | 262 const network_handler::ErrorCallback& error_callback) const { |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 callback, | 387 callback, |
272 base::Bind(&HandleShillCallFailure, device_path, error_callback)); | 388 base::Bind(&HandleShillCallFailure, device_path, error_callback)); |
273 } | 389 } |
274 | 390 |
275 void NetworkDeviceHandlerImpl::SetCellularAllowRoaming( | 391 void NetworkDeviceHandlerImpl::SetCellularAllowRoaming( |
276 const bool allow_roaming) { | 392 const bool allow_roaming) { |
277 cellular_allow_roaming_ = allow_roaming; | 393 cellular_allow_roaming_ = allow_roaming; |
278 ApplyCellularAllowRoamingToShill(); | 394 ApplyCellularAllowRoamingToShill(); |
279 } | 395 } |
280 | 396 |
| 397 void NetworkDeviceHandlerImpl::SetWifiTDLSEnabled( |
| 398 const std::string& ip_or_mac_address, |
| 399 bool enabled, |
| 400 const network_handler::StringResultCallback& callback, |
| 401 const network_handler::ErrorCallback& error_callback) { |
| 402 const DeviceState* device_state = |
| 403 network_state_handler_->GetDeviceStateByType(NetworkTypePattern::WiFi()); |
| 404 if (!device_state) { |
| 405 if (error_callback.is_null()) |
| 406 return; |
| 407 scoped_ptr<base::DictionaryValue> error_data(new base::DictionaryValue); |
| 408 error_data->SetString(network_handler::kErrorName, kErrorDeviceMissing); |
| 409 error_callback.Run(kErrorDeviceMissing, error_data.Pass()); |
| 410 return; |
| 411 } |
| 412 TDLSOperationParams params; |
| 413 params.operation = enabled ? shill::kTDLSSetupOperation |
| 414 : shill::kTDLSTeardownOperation; |
| 415 params.ip_or_mac_address = ip_or_mac_address; |
| 416 CallPerformTDLSOperation( |
| 417 device_state->path(), params, callback, error_callback); |
| 418 } |
| 419 |
| 420 void NetworkDeviceHandlerImpl::GetWifiTDLSStatus( |
| 421 const std::string& ip_or_mac_address, |
| 422 const network_handler::StringResultCallback& callback, |
| 423 const network_handler::ErrorCallback& error_callback) { |
| 424 const DeviceState* device_state = |
| 425 network_state_handler_->GetDeviceStateByType(NetworkTypePattern::WiFi()); |
| 426 if (!device_state) { |
| 427 if (error_callback.is_null()) |
| 428 return; |
| 429 scoped_ptr<base::DictionaryValue> error_data(new base::DictionaryValue); |
| 430 error_data->SetString(network_handler::kErrorName, kErrorDeviceMissing); |
| 431 error_callback.Run(kErrorDeviceMissing, error_data.Pass()); |
| 432 return; |
| 433 } |
| 434 TDLSOperationParams params; |
| 435 params.operation = shill::kTDLSStatusOperation; |
| 436 params.ip_or_mac_address = ip_or_mac_address; |
| 437 CallPerformTDLSOperation( |
| 438 device_state->path(), params, callback, error_callback); |
| 439 } |
| 440 |
281 void NetworkDeviceHandlerImpl::DeviceListChanged() { | 441 void NetworkDeviceHandlerImpl::DeviceListChanged() { |
282 ApplyCellularAllowRoamingToShill(); | 442 ApplyCellularAllowRoamingToShill(); |
283 } | 443 } |
284 | 444 |
285 NetworkDeviceHandlerImpl::NetworkDeviceHandlerImpl() | 445 NetworkDeviceHandlerImpl::NetworkDeviceHandlerImpl() |
286 : network_state_handler_(NULL), | 446 : network_state_handler_(NULL), |
287 cellular_allow_roaming_(false) {} | 447 cellular_allow_roaming_(false) {} |
288 | 448 |
289 void NetworkDeviceHandlerImpl::Init( | 449 void NetworkDeviceHandlerImpl::Init( |
290 NetworkStateHandler* network_state_handler) { | 450 NetworkStateHandler* network_state_handler) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 | 486 |
327 SetDevicePropertyInternal(device_state->path(), | 487 SetDevicePropertyInternal(device_state->path(), |
328 shill::kCellularAllowRoamingProperty, | 488 shill::kCellularAllowRoamingProperty, |
329 base::FundamentalValue(new_device_value), | 489 base::FundamentalValue(new_device_value), |
330 base::Bind(&base::DoNothing), | 490 base::Bind(&base::DoNothing), |
331 network_handler::ErrorCallback()); | 491 network_handler::ErrorCallback()); |
332 } | 492 } |
333 } | 493 } |
334 | 494 |
335 } // namespace chromeos | 495 } // namespace chromeos |
OLD | NEW |