network_device_handler.cc revision a3f7b4e666c476898878fa745f637129375cd889
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.
4
5#include "chromeos/network/network_device_handler.h"
6
7#include "base/bind.h"
8#include "base/values.h"
9#include "chromeos/dbus/dbus_thread_manager.h"
10#include "chromeos/dbus/shill_device_client.h"
11#include "chromeos/dbus/shill_ipconfig_client.h"
12#include "chromeos/network/device_state.h"
13#include "chromeos/network/network_event_log.h"
14#include "dbus/object_path.h"
15#include "third_party/cros_system_api/dbus/service_constants.h"
16
17namespace chromeos {
18
19namespace {
20
21const char kDevicePath[] = "devicePath";
22
23// TODO(armansito): Add bindings for these to service_constants.h
24// (crbug.com/256889)
25const char kShillErrorFailure[] = "org.chromium.flimflam.Error.Failure";
26const char kShillErrorNotSupported[] =
27    "org.chromium.flimflam.Error.NotSupported";
28
29std::string GetErrorNameForShillError(const std::string& shill_error_name) {
30  // TODO(armansito): Use the new SIM error names once the ones below get
31  // deprecated (crbug.com/256855)
32  if (shill_error_name == kShillErrorFailure)
33    return NetworkDeviceHandler::kErrorFailure;
34  if (shill_error_name == kShillErrorNotSupported)
35    return NetworkDeviceHandler::kErrorNotSupported;
36  if (shill_error_name == flimflam::kErrorIncorrectPinMsg)
37    return NetworkDeviceHandler::kErrorIncorrectPin;
38  if (shill_error_name == flimflam::kErrorPinBlockedMsg)
39    return NetworkDeviceHandler::kErrorPinBlocked;
40  if (shill_error_name == flimflam::kErrorPinRequiredMsg)
41    return NetworkDeviceHandler::kErrorPinRequired;
42  return NetworkDeviceHandler::kErrorUnknown;
43}
44
45void HandleShillCallFailure(
46    const std::string& device_path,
47    const network_handler::ErrorCallback& error_callback,
48    const std::string& shill_error_name,
49    const std::string& shill_error_message) {
50  network_handler::ShillErrorCallbackFunction(
51      GetErrorNameForShillError(shill_error_name),
52      device_path,
53      error_callback,
54      shill_error_name,
55      shill_error_message);
56}
57
58void IPConfigRefreshCallback(const std::string& ipconfig_path,
59                             DBusMethodCallStatus call_status) {
60  if (call_status != DBUS_METHOD_CALL_SUCCESS) {
61    NET_LOG_ERROR(
62        base::StringPrintf("IPConfigs.Refresh Failed: %d", call_status),
63        ipconfig_path);
64  } else {
65    NET_LOG_EVENT("IPConfigs.Refresh Succeeded", ipconfig_path);
66  }
67}
68
69void RefreshIPConfigsCallback(
70    const base::Closure& callback,
71    const network_handler::ErrorCallback& error_callback,
72    const std::string& device_path,
73    const base::DictionaryValue& properties) {
74  const ListValue* ip_configs;
75  if (!properties.GetListWithoutPathExpansion(
76          flimflam::kIPConfigsProperty, &ip_configs)) {
77    NET_LOG_ERROR("RequestRefreshIPConfigs Failed", device_path);
78    network_handler::ShillErrorCallbackFunction(
79        "RequestRefreshIPConfigs Failed",
80        device_path,
81        error_callback,
82        std::string("Missing ") + flimflam::kIPConfigsProperty, "");
83    return;
84  }
85
86  for (size_t i = 0; i < ip_configs->GetSize(); i++) {
87    std::string ipconfig_path;
88    if (!ip_configs->GetString(i, &ipconfig_path))
89      continue;
90    DBusThreadManager::Get()->GetShillIPConfigClient()->Refresh(
91        dbus::ObjectPath(ipconfig_path),
92        base::Bind(&IPConfigRefreshCallback, ipconfig_path));
93  }
94  // It is safe to invoke |callback| here instead of waiting for the
95  // IPConfig.Refresh callbacks to complete because the Refresh DBus calls will
96  // be executed in order and thus before any further DBus requests that
97  // |callback| may issue.
98  if (!callback.is_null())
99    callback.Run();
100}
101
102void ProposeScanCallback(
103    const std::string& device_path,
104    const base::Closure& callback,
105    const network_handler::ErrorCallback& error_callback,
106    DBusMethodCallStatus call_status) {
107  if (call_status != DBUS_METHOD_CALL_SUCCESS) {
108    NET_LOG_ERROR(
109        base::StringPrintf("Device.ProposeScan failed: %d", call_status),
110        device_path);
111    network_handler::ShillErrorCallbackFunction(
112        "Device.ProposeScan Failed",
113        device_path,
114        error_callback,
115        base::StringPrintf("DBus call failed: %d", call_status), "");
116    return;
117  }
118  NET_LOG_EVENT("Device.ProposeScan succeeded.", device_path);
119  if (!callback.is_null())
120    callback.Run();
121}
122
123}  // namespace
124
125const char NetworkDeviceHandler::kErrorFailure[] = "failure";
126const char NetworkDeviceHandler::kErrorIncorrectPin[] = "incorrect-pin";
127const char NetworkDeviceHandler::kErrorNotSupported[] = "not-supported";
128const char NetworkDeviceHandler::kErrorPinBlocked[] = "pin-blocked";
129const char NetworkDeviceHandler::kErrorPinRequired[] = "pin-required";
130const char NetworkDeviceHandler::kErrorUnknown[] = "unknown";
131
132NetworkDeviceHandler::NetworkDeviceHandler() {
133}
134
135NetworkDeviceHandler::~NetworkDeviceHandler() {
136}
137
138void NetworkDeviceHandler::GetDeviceProperties(
139    const std::string& device_path,
140    const network_handler::DictionaryResultCallback& callback,
141    const network_handler::ErrorCallback& error_callback) const {
142  DBusThreadManager::Get()->GetShillDeviceClient()->GetProperties(
143      dbus::ObjectPath(device_path),
144      base::Bind(&network_handler::GetPropertiesCallback,
145                 callback, error_callback, device_path));
146}
147
148void NetworkDeviceHandler::SetDeviceProperty(
149    const std::string& device_path,
150    const std::string& name,
151    const base::Value& value,
152    const base::Closure& callback,
153    const network_handler::ErrorCallback& error_callback) {
154  DBusThreadManager::Get()->GetShillDeviceClient()->SetProperty(
155      dbus::ObjectPath(device_path),
156      name,
157      value,
158      callback,
159      base::Bind(&HandleShillCallFailure, device_path, error_callback));
160}
161
162void NetworkDeviceHandler::RequestRefreshIPConfigs(
163    const std::string& device_path,
164    const base::Closure& callback,
165    const network_handler::ErrorCallback& error_callback) {
166  GetDeviceProperties(device_path,
167                      base::Bind(&RefreshIPConfigsCallback,
168                                 callback, error_callback),
169                      error_callback);
170}
171
172void NetworkDeviceHandler::ProposeScan(
173    const std::string& device_path,
174    const base::Closure& callback,
175    const network_handler::ErrorCallback& error_callback) {
176  DBusThreadManager::Get()->GetShillDeviceClient()->ProposeScan(
177      dbus::ObjectPath(device_path),
178      base::Bind(&ProposeScanCallback, device_path, callback, error_callback));
179}
180
181void NetworkDeviceHandler::SetCarrier(
182    const std::string& device_path,
183    const std::string& carrier,
184    const base::Closure& callback,
185    const network_handler::ErrorCallback& error_callback) {
186  DBusThreadManager::Get()->GetShillDeviceClient()->SetCarrier(
187      dbus::ObjectPath(device_path),
188      carrier,
189      callback,
190      base::Bind(&HandleShillCallFailure, device_path, error_callback));
191}
192
193void NetworkDeviceHandler::RequirePin(
194    const std::string& device_path,
195    bool require_pin,
196    const std::string& pin,
197    const base::Closure& callback,
198    const network_handler::ErrorCallback& error_callback) {
199  DBusThreadManager::Get()->GetShillDeviceClient()->RequirePin(
200      dbus::ObjectPath(device_path),
201      pin,
202      require_pin,
203      callback,
204      base::Bind(&HandleShillCallFailure, device_path, error_callback));
205}
206
207void NetworkDeviceHandler::EnterPin(
208    const std::string& device_path,
209    const std::string& pin,
210    const base::Closure& callback,
211    const network_handler::ErrorCallback& error_callback) {
212  DBusThreadManager::Get()->GetShillDeviceClient()->EnterPin(
213      dbus::ObjectPath(device_path),
214      pin,
215      callback,
216      base::Bind(&HandleShillCallFailure, device_path, error_callback));
217}
218
219void NetworkDeviceHandler::UnblockPin(
220    const std::string& device_path,
221    const std::string& puk,
222    const std::string& new_pin,
223    const base::Closure& callback,
224    const network_handler::ErrorCallback& error_callback) {
225  DBusThreadManager::Get()->GetShillDeviceClient()->UnblockPin(
226      dbus::ObjectPath(device_path),
227      puk,
228      new_pin,
229      callback,
230      base::Bind(&HandleShillCallFailure, device_path, error_callback));
231}
232
233void NetworkDeviceHandler::ChangePin(
234    const std::string& device_path,
235    const std::string& old_pin,
236    const std::string& new_pin,
237    const base::Closure& callback,
238    const network_handler::ErrorCallback& error_callback) {
239  DBusThreadManager::Get()->GetShillDeviceClient()->ChangePin(
240      dbus::ObjectPath(device_path),
241      old_pin,
242      new_pin,
243      callback,
244      base::Bind(&HandleShillCallFailure, device_path, error_callback));
245}
246
247void NetworkDeviceHandler::HandleShillCallFailureForTest(
248    const std::string& device_path,
249    const network_handler::ErrorCallback& error_callback,
250    const std::string& shill_error_name,
251    const std::string& shill_error_message) {
252  HandleShillCallFailure(
253      device_path, error_callback, shill_error_name, shill_error_message);
254}
255
256}  // namespace chromeos
257