[Telemetry] Rename test methods in page_test_results_unittest to follow existing...
[chromium-blink-merge.git] / chromeos / network / network_device_handler_impl.cc
blob54a414cf63c669b8844d18c73811faaa0fc8286d
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
3 // found in the LICENSE file.
5 #include "chromeos/network/network_device_handler_impl.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/message_loop/message_loop_proxy.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/time/time.h"
12 #include "base/values.h"
13 #include "chromeos/dbus/dbus_thread_manager.h"
14 #include "chromeos/dbus/shill_device_client.h"
15 #include "chromeos/dbus/shill_ipconfig_client.h"
16 #include "chromeos/device_event_log.h"
17 #include "chromeos/network/device_state.h"
18 #include "chromeos/network/network_handler_callbacks.h"
19 #include "chromeos/network/network_state_handler.h"
20 #include "dbus/object_path.h"
21 #include "third_party/cros_system_api/dbus/service_constants.h"
23 namespace chromeos {
25 namespace {
27 std::string GetErrorNameForShillError(const std::string& shill_error_name) {
28 if (shill_error_name == shill::kErrorResultFailure)
29 return NetworkDeviceHandler::kErrorFailure;
30 if (shill_error_name == shill::kErrorResultNotSupported)
31 return NetworkDeviceHandler::kErrorNotSupported;
32 if (shill_error_name == shill::kErrorResultIncorrectPin)
33 return NetworkDeviceHandler::kErrorIncorrectPin;
34 if (shill_error_name == shill::kErrorResultPinBlocked)
35 return NetworkDeviceHandler::kErrorPinBlocked;
36 if (shill_error_name == shill::kErrorResultPinRequired)
37 return NetworkDeviceHandler::kErrorPinRequired;
38 if (shill_error_name == shill::kErrorResultNotFound)
39 return NetworkDeviceHandler::kErrorDeviceMissing;
40 return NetworkDeviceHandler::kErrorUnknown;
43 void InvokeErrorCallback(const std::string& device_path,
44 const network_handler::ErrorCallback& error_callback,
45 const std::string& error_name) {
46 std::string error_msg = "Device Error: " + error_name;
47 NET_LOG(ERROR) << error_msg << ": " << device_path;
48 network_handler::RunErrorCallback(error_callback, device_path, error_name,
49 error_msg);
52 void HandleShillCallFailure(
53 const std::string& device_path,
54 const network_handler::ErrorCallback& error_callback,
55 const std::string& shill_error_name,
56 const std::string& shill_error_message) {
57 network_handler::ShillErrorCallbackFunction(
58 GetErrorNameForShillError(shill_error_name),
59 device_path,
60 error_callback,
61 shill_error_name,
62 shill_error_message);
65 void IPConfigRefreshCallback(const std::string& ipconfig_path,
66 DBusMethodCallStatus call_status) {
67 if (call_status != DBUS_METHOD_CALL_SUCCESS) {
68 NET_LOG(ERROR) << "IPConfigs.Refresh Failed: " << call_status << ": "
69 << ipconfig_path;
70 } else {
71 NET_LOG(EVENT) << "IPConfigs.Refresh Succeeded: " << ipconfig_path;
75 void RefreshIPConfigsCallback(
76 const base::Closure& callback,
77 const network_handler::ErrorCallback& error_callback,
78 const std::string& device_path,
79 const base::DictionaryValue& properties) {
80 const base::ListValue* ip_configs;
81 if (!properties.GetListWithoutPathExpansion(
82 shill::kIPConfigsProperty, &ip_configs)) {
83 NET_LOG(ERROR) << "RequestRefreshIPConfigs Failed: " << device_path;
84 network_handler::ShillErrorCallbackFunction(
85 "RequestRefreshIPConfigs Failed",
86 device_path,
87 error_callback,
88 std::string("Missing ") + shill::kIPConfigsProperty, "");
89 return;
92 for (size_t i = 0; i < ip_configs->GetSize(); i++) {
93 std::string ipconfig_path;
94 if (!ip_configs->GetString(i, &ipconfig_path))
95 continue;
96 DBusThreadManager::Get()->GetShillIPConfigClient()->Refresh(
97 dbus::ObjectPath(ipconfig_path),
98 base::Bind(&IPConfigRefreshCallback, ipconfig_path));
100 // It is safe to invoke |callback| here instead of waiting for the
101 // IPConfig.Refresh callbacks to complete because the Refresh DBus calls will
102 // be executed in order and thus before any further DBus requests that
103 // |callback| may issue.
104 if (!callback.is_null())
105 callback.Run();
108 void ProposeScanCallback(
109 const std::string& device_path,
110 const base::Closure& callback,
111 const network_handler::ErrorCallback& error_callback,
112 DBusMethodCallStatus call_status) {
113 if (call_status != DBUS_METHOD_CALL_SUCCESS) {
114 network_handler::ShillErrorCallbackFunction(
115 "Device.ProposeScan Failed",
116 device_path,
117 error_callback,
118 base::StringPrintf("DBus call failed: %d", call_status), "");
119 return;
121 NET_LOG(EVENT) << "Device.ProposeScan succeeded: " << device_path;
122 if (!callback.is_null())
123 callback.Run();
126 void SetDevicePropertyInternal(
127 const std::string& device_path,
128 const std::string& property_name,
129 const base::Value& value,
130 const base::Closure& callback,
131 const network_handler::ErrorCallback& error_callback) {
132 DBusThreadManager::Get()->GetShillDeviceClient()->SetProperty(
133 dbus::ObjectPath(device_path),
134 property_name,
135 value,
136 callback,
137 base::Bind(&HandleShillCallFailure, device_path, error_callback));
140 // Struct containing TDLS Operation parameters.
141 struct TDLSOperationParams {
142 TDLSOperationParams() : retry_count(0) {}
143 std::string operation;
144 std::string next_operation;
145 std::string ip_or_mac_address;
146 int retry_count;
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);
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;
167 if (params.operation != shill::kTDLSStatusOperation && !result.empty()) {
168 NET_LOG(ERROR) << "Unexpected TDLS result: " + result << ": "
169 << device_path;
172 TDLSOperationParams new_params;
173 const int64 kRequestStatusDelayMs = 500;
174 int64 request_delay_ms = 0;
175 if (params.operation == shill::kTDLSStatusOperation) {
176 // If this is the last operation, or the result is 'Nonexistent',
177 // return the result.
178 if (params.next_operation.empty() ||
179 result == shill::kTDLSNonexistentState) {
180 if (!callback.is_null())
181 callback.Run(result);
182 return;
184 // Otherwise start the next operation.
185 new_params.operation = params.next_operation;
186 } else if (params.operation == shill::kTDLSDiscoverOperation) {
187 // Send a delayed Status request followed by a Setup request.
188 request_delay_ms = kRequestStatusDelayMs;
189 new_params.operation = shill::kTDLSStatusOperation;
190 new_params.next_operation = shill::kTDLSSetupOperation;
191 } else if (params.operation == shill::kTDLSSetupOperation ||
192 params.operation == shill::kTDLSTeardownOperation) {
193 // Send a delayed Status request.
194 request_delay_ms = kRequestStatusDelayMs;
195 new_params.operation = shill::kTDLSStatusOperation;
196 } else {
197 NET_LOG(ERROR) << "Unexpected TDLS operation: " + params.operation;
198 NOTREACHED();
201 new_params.ip_or_mac_address = params.ip_or_mac_address;
203 base::TimeDelta request_delay;
204 if (!DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface())
205 request_delay = base::TimeDelta::FromMilliseconds(request_delay_ms);
207 base::MessageLoopProxy::current()->PostDelayedTask(
208 FROM_HERE, base::Bind(&CallPerformTDLSOperation, device_path, new_params,
209 callback, error_callback),
210 request_delay);
213 void TDLSErrorCallback(
214 const std::string& device_path,
215 const TDLSOperationParams& params,
216 const network_handler::StringResultCallback& callback,
217 const network_handler::ErrorCallback& error_callback,
218 const std::string& dbus_error_name,
219 const std::string& dbus_error_message) {
220 // If a Setup operation receives an InProgress error, retry.
221 const int kMaxRetries = 5;
222 if ((params.operation == shill::kTDLSDiscoverOperation ||
223 params.operation == shill::kTDLSSetupOperation) &&
224 dbus_error_name == shill::kErrorResultInProgress &&
225 params.retry_count < kMaxRetries) {
226 TDLSOperationParams retry_params = params;
227 ++retry_params.retry_count;
228 NET_LOG(EVENT) << "TDLS Retry: " << params.retry_count << ": "
229 << device_path;
230 const int64 kReRequestDelayMs = 1000;
231 base::TimeDelta request_delay;
232 if (!DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface())
233 request_delay = base::TimeDelta::FromMilliseconds(kReRequestDelayMs);
235 base::MessageLoopProxy::current()->PostDelayedTask(
236 FROM_HERE,
237 base::Bind(&CallPerformTDLSOperation,
238 device_path, retry_params, callback, error_callback),
239 request_delay);
240 return;
243 NET_LOG(ERROR) << "TDLS Operation: " << params.operation
244 << " Error: " << dbus_error_name << ": " << dbus_error_message
245 << ": " << device_path;
246 if (error_callback.is_null())
247 return;
249 const std::string error_name =
250 dbus_error_name == shill::kErrorResultInProgress ?
251 NetworkDeviceHandler::kErrorTimeout : NetworkDeviceHandler::kErrorUnknown;
252 const std::string& error_detail = params.ip_or_mac_address;
253 scoped_ptr<base::DictionaryValue> error_data(
254 network_handler::CreateDBusErrorData(
255 device_path, error_name, error_detail,
256 dbus_error_name, dbus_error_message));
257 error_callback.Run(error_name, error_data.Pass());
260 void CallPerformTDLSOperation(
261 const std::string& device_path,
262 const TDLSOperationParams& params,
263 const network_handler::StringResultCallback& callback,
264 const network_handler::ErrorCallback& error_callback) {
265 LOG(ERROR) << "TDLS: " << params.operation;
266 NET_LOG(EVENT) << "CallPerformTDLSOperation: " << params.operation << ": "
267 << device_path;
268 DBusThreadManager::Get()->GetShillDeviceClient()->PerformTDLSOperation(
269 dbus::ObjectPath(device_path),
270 params.operation,
271 params.ip_or_mac_address,
272 base::Bind(&TDLSSuccessCallback,
273 device_path, params, callback, error_callback),
274 base::Bind(&TDLSErrorCallback,
275 device_path, params, callback, error_callback));
278 } // namespace
280 NetworkDeviceHandlerImpl::~NetworkDeviceHandlerImpl() {
281 if (network_state_handler_)
282 network_state_handler_->RemoveObserver(this, FROM_HERE);
285 void NetworkDeviceHandlerImpl::GetDeviceProperties(
286 const std::string& device_path,
287 const network_handler::DictionaryResultCallback& callback,
288 const network_handler::ErrorCallback& error_callback) const {
289 DBusThreadManager::Get()->GetShillDeviceClient()->GetProperties(
290 dbus::ObjectPath(device_path),
291 base::Bind(&network_handler::GetPropertiesCallback,
292 callback, error_callback, device_path));
295 void NetworkDeviceHandlerImpl::SetDeviceProperty(
296 const std::string& device_path,
297 const std::string& property_name,
298 const base::Value& value,
299 const base::Closure& callback,
300 const network_handler::ErrorCallback& error_callback) {
301 const char* const property_blacklist[] = {
302 // Must only be changed by policy/owner through.
303 shill::kCellularAllowRoamingProperty
306 for (size_t i = 0; i < arraysize(property_blacklist); ++i) {
307 if (property_name == property_blacklist[i]) {
308 InvokeErrorCallback(
309 device_path,
310 error_callback,
311 "SetDeviceProperty called on blacklisted property " + property_name);
312 return;
316 SetDevicePropertyInternal(
317 device_path, property_name, value, callback, error_callback);
320 void NetworkDeviceHandlerImpl::RequestRefreshIPConfigs(
321 const std::string& device_path,
322 const base::Closure& callback,
323 const network_handler::ErrorCallback& error_callback) {
324 GetDeviceProperties(device_path,
325 base::Bind(&RefreshIPConfigsCallback,
326 callback, error_callback),
327 error_callback);
330 void NetworkDeviceHandlerImpl::ProposeScan(
331 const std::string& device_path,
332 const base::Closure& callback,
333 const network_handler::ErrorCallback& error_callback) {
334 DBusThreadManager::Get()->GetShillDeviceClient()->ProposeScan(
335 dbus::ObjectPath(device_path),
336 base::Bind(&ProposeScanCallback, device_path, callback, error_callback));
339 void NetworkDeviceHandlerImpl::RegisterCellularNetwork(
340 const std::string& device_path,
341 const std::string& network_id,
342 const base::Closure& callback,
343 const network_handler::ErrorCallback& error_callback) {
344 DBusThreadManager::Get()->GetShillDeviceClient()->Register(
345 dbus::ObjectPath(device_path),
346 network_id,
347 callback,
348 base::Bind(&HandleShillCallFailure, device_path, error_callback));
351 void NetworkDeviceHandlerImpl::SetCarrier(
352 const std::string& device_path,
353 const std::string& carrier,
354 const base::Closure& callback,
355 const network_handler::ErrorCallback& error_callback) {
356 DBusThreadManager::Get()->GetShillDeviceClient()->SetCarrier(
357 dbus::ObjectPath(device_path),
358 carrier,
359 callback,
360 base::Bind(&HandleShillCallFailure, device_path, error_callback));
363 void NetworkDeviceHandlerImpl::RequirePin(
364 const std::string& device_path,
365 bool require_pin,
366 const std::string& pin,
367 const base::Closure& callback,
368 const network_handler::ErrorCallback& error_callback) {
369 DBusThreadManager::Get()->GetShillDeviceClient()->RequirePin(
370 dbus::ObjectPath(device_path),
371 pin,
372 require_pin,
373 callback,
374 base::Bind(&HandleShillCallFailure, device_path, error_callback));
377 void NetworkDeviceHandlerImpl::EnterPin(
378 const std::string& device_path,
379 const std::string& pin,
380 const base::Closure& callback,
381 const network_handler::ErrorCallback& error_callback) {
382 DBusThreadManager::Get()->GetShillDeviceClient()->EnterPin(
383 dbus::ObjectPath(device_path),
384 pin,
385 callback,
386 base::Bind(&HandleShillCallFailure, device_path, error_callback));
389 void NetworkDeviceHandlerImpl::UnblockPin(
390 const std::string& device_path,
391 const std::string& puk,
392 const std::string& new_pin,
393 const base::Closure& callback,
394 const network_handler::ErrorCallback& error_callback) {
395 DBusThreadManager::Get()->GetShillDeviceClient()->UnblockPin(
396 dbus::ObjectPath(device_path),
397 puk,
398 new_pin,
399 callback,
400 base::Bind(&HandleShillCallFailure, device_path, error_callback));
403 void NetworkDeviceHandlerImpl::ChangePin(
404 const std::string& device_path,
405 const std::string& old_pin,
406 const std::string& new_pin,
407 const base::Closure& callback,
408 const network_handler::ErrorCallback& error_callback) {
409 DBusThreadManager::Get()->GetShillDeviceClient()->ChangePin(
410 dbus::ObjectPath(device_path),
411 old_pin,
412 new_pin,
413 callback,
414 base::Bind(&HandleShillCallFailure, device_path, error_callback));
417 void NetworkDeviceHandlerImpl::SetCellularAllowRoaming(
418 const bool allow_roaming) {
419 cellular_allow_roaming_ = allow_roaming;
420 ApplyCellularAllowRoamingToShill();
423 void NetworkDeviceHandlerImpl::SetWifiTDLSEnabled(
424 const std::string& ip_or_mac_address,
425 bool enabled,
426 const network_handler::StringResultCallback& callback,
427 const network_handler::ErrorCallback& error_callback) {
428 const DeviceState* device_state = GetWifiDeviceState(error_callback);
429 if (!device_state)
430 return;
432 TDLSOperationParams params;
433 params.operation =
434 enabled ? shill::kTDLSDiscoverOperation : shill::kTDLSTeardownOperation;
435 params.ip_or_mac_address = ip_or_mac_address;
436 CallPerformTDLSOperation(
437 device_state->path(), params, callback, error_callback);
440 void NetworkDeviceHandlerImpl::GetWifiTDLSStatus(
441 const std::string& ip_or_mac_address,
442 const network_handler::StringResultCallback& callback,
443 const network_handler::ErrorCallback& error_callback) {
444 const DeviceState* device_state = GetWifiDeviceState(error_callback);
445 if (!device_state)
446 return;
448 TDLSOperationParams params;
449 params.operation = shill::kTDLSStatusOperation;
450 params.ip_or_mac_address = ip_or_mac_address;
451 CallPerformTDLSOperation(
452 device_state->path(), params, callback, error_callback);
455 void NetworkDeviceHandlerImpl::AddWifiWakeOnPacketConnection(
456 const net::IPEndPoint& ip_endpoint,
457 const base::Closure& callback,
458 const network_handler::ErrorCallback& error_callback) {
459 const DeviceState* device_state = GetWifiDeviceState(error_callback);
460 if (!device_state)
461 return;
463 DBusThreadManager::Get()->GetShillDeviceClient()->AddWakeOnPacketConnection(
464 dbus::ObjectPath(device_state->path()),
465 ip_endpoint,
466 callback,
467 base::Bind(&HandleShillCallFailure,
468 device_state->path(),
469 error_callback));
472 void NetworkDeviceHandlerImpl::RemoveWifiWakeOnPacketConnection(
473 const net::IPEndPoint& ip_endpoint,
474 const base::Closure& callback,
475 const network_handler::ErrorCallback& error_callback) {
476 const DeviceState* device_state = GetWifiDeviceState(error_callback);
477 if (!device_state)
478 return;
480 DBusThreadManager::Get()
481 ->GetShillDeviceClient()
482 ->RemoveWakeOnPacketConnection(dbus::ObjectPath(device_state->path()),
483 ip_endpoint,
484 callback,
485 base::Bind(&HandleShillCallFailure,
486 device_state->path(),
487 error_callback));
490 void NetworkDeviceHandlerImpl::RemoveAllWifiWakeOnPacketConnections(
491 const base::Closure& callback,
492 const network_handler::ErrorCallback& error_callback) {
493 const DeviceState* device_state = GetWifiDeviceState(error_callback);
494 if (!device_state)
495 return;
497 DBusThreadManager::Get()
498 ->GetShillDeviceClient()
499 ->RemoveAllWakeOnPacketConnections(dbus::ObjectPath(device_state->path()),
500 callback,
501 base::Bind(&HandleShillCallFailure,
502 device_state->path(),
503 error_callback));
506 void NetworkDeviceHandlerImpl::DeviceListChanged() {
507 ApplyCellularAllowRoamingToShill();
510 NetworkDeviceHandlerImpl::NetworkDeviceHandlerImpl()
511 : network_state_handler_(NULL),
512 cellular_allow_roaming_(false) {}
514 void NetworkDeviceHandlerImpl::Init(
515 NetworkStateHandler* network_state_handler) {
516 DCHECK(network_state_handler);
517 network_state_handler_ = network_state_handler;
518 network_state_handler_->AddObserver(this, FROM_HERE);
521 void NetworkDeviceHandlerImpl::ApplyCellularAllowRoamingToShill() {
522 NetworkStateHandler::DeviceStateList list;
523 network_state_handler_->GetDeviceListByType(NetworkTypePattern::Cellular(),
524 &list);
525 if (list.empty()) {
526 NET_LOG(DEBUG) << "No cellular device available. Roaming is only supported "
527 "by cellular devices.";
528 return;
530 for (NetworkStateHandler::DeviceStateList::const_iterator it = list.begin();
531 it != list.end(); ++it) {
532 const DeviceState* device_state = *it;
533 bool current_allow_roaming = device_state->allow_roaming();
535 // If roaming is required by the provider, always try to set to true.
536 bool new_device_value =
537 device_state->provider_requires_roaming() || cellular_allow_roaming_;
539 // Only set the value if the current value is different from
540 // |new_device_value|.
541 if (new_device_value == current_allow_roaming)
542 continue;
544 SetDevicePropertyInternal(device_state->path(),
545 shill::kCellularAllowRoamingProperty,
546 base::FundamentalValue(new_device_value),
547 base::Bind(&base::DoNothing),
548 network_handler::ErrorCallback());
552 const DeviceState* NetworkDeviceHandlerImpl::GetWifiDeviceState(
553 const network_handler::ErrorCallback& error_callback) {
554 const DeviceState* device_state =
555 network_state_handler_->GetDeviceStateByType(NetworkTypePattern::WiFi());
556 if (!device_state) {
557 if (error_callback.is_null())
558 return NULL;
559 scoped_ptr<base::DictionaryValue> error_data(new base::DictionaryValue);
560 error_data->SetString(network_handler::kErrorName, kErrorDeviceMissing);
561 error_callback.Run(kErrorDeviceMissing, error_data.Pass());
562 return NULL;
565 return device_state;
568 } // namespace chromeos