network_device_handler.cc revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
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 102} // namespace 103 104const char NetworkDeviceHandler::kErrorFailure[] = "failure"; 105const char NetworkDeviceHandler::kErrorIncorrectPin[] = "incorrect-pin"; 106const char NetworkDeviceHandler::kErrorNotSupported[] = "not-supported"; 107const char NetworkDeviceHandler::kErrorPinBlocked[] = "pin-blocked"; 108const char NetworkDeviceHandler::kErrorPinRequired[] = "pin-required"; 109const char NetworkDeviceHandler::kErrorUnknown[] = "unknown"; 110 111NetworkDeviceHandler::NetworkDeviceHandler() { 112} 113 114NetworkDeviceHandler::~NetworkDeviceHandler() { 115} 116 117void NetworkDeviceHandler::GetDeviceProperties( 118 const std::string& device_path, 119 const network_handler::DictionaryResultCallback& callback, 120 const network_handler::ErrorCallback& error_callback) const { 121 DBusThreadManager::Get()->GetShillDeviceClient()->GetProperties( 122 dbus::ObjectPath(device_path), 123 base::Bind(&network_handler::GetPropertiesCallback, 124 callback, error_callback, device_path)); 125} 126 127void NetworkDeviceHandler::RequestRefreshIPConfigs( 128 const std::string& device_path, 129 const base::Closure& callback, 130 const network_handler::ErrorCallback& error_callback) { 131 GetDeviceProperties(device_path, 132 base::Bind(&RefreshIPConfigsCallback, 133 callback, error_callback), 134 error_callback); 135} 136 137void NetworkDeviceHandler::SetCarrier( 138 const std::string& device_path, 139 const std::string& carrier, 140 const base::Closure& callback, 141 const network_handler::ErrorCallback& error_callback) { 142 DBusThreadManager::Get()->GetShillDeviceClient()->SetCarrier( 143 dbus::ObjectPath(device_path), 144 carrier, 145 callback, 146 base::Bind(&HandleShillCallFailure, device_path, error_callback)); 147} 148 149void NetworkDeviceHandler::RequirePin( 150 const std::string& device_path, 151 bool require_pin, 152 const std::string& pin, 153 const base::Closure& callback, 154 const network_handler::ErrorCallback& error_callback) { 155 DBusThreadManager::Get()->GetShillDeviceClient()->RequirePin( 156 dbus::ObjectPath(device_path), 157 pin, 158 require_pin, 159 callback, 160 base::Bind(&HandleShillCallFailure, device_path, error_callback)); 161} 162 163void NetworkDeviceHandler::EnterPin( 164 const std::string& device_path, 165 const std::string& pin, 166 const base::Closure& callback, 167 const network_handler::ErrorCallback& error_callback) { 168 DBusThreadManager::Get()->GetShillDeviceClient()->EnterPin( 169 dbus::ObjectPath(device_path), 170 pin, 171 callback, 172 base::Bind(&HandleShillCallFailure, device_path, error_callback)); 173} 174 175void NetworkDeviceHandler::UnblockPin( 176 const std::string& device_path, 177 const std::string& puk, 178 const std::string& new_pin, 179 const base::Closure& callback, 180 const network_handler::ErrorCallback& error_callback) { 181 DBusThreadManager::Get()->GetShillDeviceClient()->UnblockPin( 182 dbus::ObjectPath(device_path), 183 puk, 184 new_pin, 185 callback, 186 base::Bind(&HandleShillCallFailure, device_path, error_callback)); 187} 188 189void NetworkDeviceHandler::ChangePin( 190 const std::string& device_path, 191 const std::string& old_pin, 192 const std::string& new_pin, 193 const base::Closure& callback, 194 const network_handler::ErrorCallback& error_callback) { 195 DBusThreadManager::Get()->GetShillDeviceClient()->ChangePin( 196 dbus::ObjectPath(device_path), 197 old_pin, 198 new_pin, 199 callback, 200 base::Bind(&HandleShillCallFailure, device_path, error_callback)); 201} 202 203void NetworkDeviceHandler::HandleShillCallFailureForTest( 204 const std::string& device_path, 205 const network_handler::ErrorCallback& error_callback, 206 const std::string& shill_error_name, 207 const std::string& shill_error_message) { 208 HandleShillCallFailure( 209 device_path, error_callback, shill_error_name, shill_error_message); 210} 211 212} // namespace chromeos 213