network_configuration_handler.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
1// Copyright (c) 2012 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_configuration_handler.h" 6 7#include <string> 8#include <vector> 9 10#include "base/bind.h" 11#include "base/format_macros.h" 12#include "base/json/json_writer.h" 13#include "base/logging.h" 14#include "base/memory/ref_counted.h" 15#include "base/memory/scoped_ptr.h" 16#include "base/stl_util.h" 17#include "base/strings/stringprintf.h" 18#include "base/values.h" 19#include "chromeos/dbus/dbus_thread_manager.h" 20#include "chromeos/dbus/shill_manager_client.h" 21#include "chromeos/dbus/shill_profile_client.h" 22#include "chromeos/dbus/shill_service_client.h" 23#include "chromeos/network/network_event_log.h" 24#include "chromeos/network/network_state_handler.h" 25#include "chromeos/network/shill_property_util.h" 26#include "dbus/object_path.h" 27#include "third_party/cros_system_api/dbus/service_constants.h" 28 29namespace chromeos { 30 31namespace { 32 33// Strip surrounding "" from keys (if present). 34std::string StripQuotations(const std::string& in_str) { 35 size_t len = in_str.length(); 36 if (len >= 2 && in_str[0] == '"' && in_str[len-1] == '"') 37 return in_str.substr(1, len-2); 38 return in_str; 39} 40 41void InvokeErrorCallback(const std::string& service_path, 42 const network_handler::ErrorCallback& error_callback, 43 const std::string& error_name) { 44 std::string error_msg = "Config Error: " + error_name; 45 NET_LOG_ERROR(error_msg, service_path); 46 network_handler::RunErrorCallback( 47 error_callback, service_path, error_name, error_msg); 48} 49 50void GetPropertiesCallback( 51 const network_handler::DictionaryResultCallback& callback, 52 const network_handler::ErrorCallback& error_callback, 53 const std::string& service_path, 54 DBusMethodCallStatus call_status, 55 const base::DictionaryValue& properties) { 56 if (call_status != DBUS_METHOD_CALL_SUCCESS) { 57 // Because network services are added and removed frequently, we will see 58 // failures regularly, so don't log these. 59 network_handler::RunErrorCallback(error_callback, 60 service_path, 61 network_handler::kDBusFailedError, 62 network_handler::kDBusFailedErrorMessage); 63 return; 64 } 65 if (callback.is_null()) 66 return; 67 68 // Get the correct name from WifiHex if necessary. 69 scoped_ptr<base::DictionaryValue> properties_copy(properties.DeepCopy()); 70 std::string name = 71 shill_property_util::GetNameFromProperties(service_path, properties); 72 if (!name.empty()) 73 properties_copy->SetStringWithoutPathExpansion(shill::kNameProperty, name); 74 callback.Run(service_path, *properties_copy.get()); 75} 76 77void SetNetworkProfileErrorCallback( 78 const std::string& service_path, 79 const std::string& profile_path, 80 const network_handler::ErrorCallback& error_callback, 81 const std::string& dbus_error_name, 82 const std::string& dbus_error_message) { 83 network_handler::ShillErrorCallbackFunction( 84 "Config.SetNetworkProfile Failed: " + profile_path, 85 service_path, error_callback, 86 dbus_error_name, dbus_error_message); 87} 88 89void LogConfigProperties(const std::string& desc, 90 const std::string& path, 91 const base::DictionaryValue& properties) { 92 for (base::DictionaryValue::Iterator iter(properties); 93 !iter.IsAtEnd(); iter.Advance()) { 94 std::string v = "******"; 95 if (!shill_property_util::IsPassphraseKey(iter.key())) 96 base::JSONWriter::Write(&iter.value(), &v); 97 NET_LOG_DEBUG(desc, path + "." + iter.key() + "=" + v); 98 } 99} 100 101} // namespace 102 103// Helper class to request from Shill the profile entries associated with a 104// Service and delete the service from each profile. Triggers either 105// |callback| on success or |error_callback| on failure, and calls 106// |handler|->ProfileEntryDeleterCompleted() on completion to delete itself. 107class NetworkConfigurationHandler::ProfileEntryDeleter 108 : public base::SupportsWeakPtr<ProfileEntryDeleter> { 109 public: 110 ProfileEntryDeleter(NetworkConfigurationHandler* handler, 111 const std::string& service_path, 112 const base::Closure& callback, 113 const network_handler::ErrorCallback& error_callback) 114 : owner_(handler), 115 service_path_(service_path), 116 callback_(callback), 117 error_callback_(error_callback) { 118 } 119 120 void Run() { 121 DBusThreadManager::Get()->GetShillServiceClient()-> 122 GetLoadableProfileEntries( 123 dbus::ObjectPath(service_path_), 124 base::Bind(&ProfileEntryDeleter::GetProfileEntriesToDeleteCallback, 125 AsWeakPtr())); 126 } 127 128 private: 129 void GetProfileEntriesToDeleteCallback( 130 DBusMethodCallStatus call_status, 131 const base::DictionaryValue& profile_entries) { 132 if (call_status != DBUS_METHOD_CALL_SUCCESS) { 133 InvokeErrorCallback( 134 service_path_, error_callback_, "GetLoadableProfileEntriesFailed"); 135 owner_->ProfileEntryDeleterCompleted(service_path_); // Deletes this. 136 return; 137 } 138 139 for (base::DictionaryValue::Iterator iter(profile_entries); 140 !iter.IsAtEnd(); iter.Advance()) { 141 std::string profile_path = StripQuotations(iter.key()); 142 std::string entry_path; 143 iter.value().GetAsString(&entry_path); 144 if (profile_path.empty() || entry_path.empty()) { 145 NET_LOG_ERROR("Failed to parse Profile Entry", base::StringPrintf( 146 "%s: %s", profile_path.c_str(), entry_path.c_str())); 147 continue; 148 } 149 if (profile_delete_entries_.count(profile_path) != 0) { 150 NET_LOG_ERROR("Multiple Profile Entries", base::StringPrintf( 151 "%s: %s", profile_path.c_str(), entry_path.c_str())); 152 continue; 153 } 154 NET_LOG_DEBUG("Delete Profile Entry", base::StringPrintf( 155 "%s: %s", profile_path.c_str(), entry_path.c_str())); 156 profile_delete_entries_[profile_path] = entry_path; 157 DBusThreadManager::Get()->GetShillProfileClient()->DeleteEntry( 158 dbus::ObjectPath(profile_path), 159 entry_path, 160 base::Bind(&ProfileEntryDeleter::ProfileEntryDeletedCallback, 161 AsWeakPtr(), profile_path, entry_path), 162 base::Bind(&ProfileEntryDeleter::ShillErrorCallback, 163 AsWeakPtr(), profile_path, entry_path)); 164 } 165 } 166 167 void ProfileEntryDeletedCallback(const std::string& profile_path, 168 const std::string& entry) { 169 NET_LOG_DEBUG("Profile Entry Deleted", base::StringPrintf( 170 "%s: %s", profile_path.c_str(), entry.c_str())); 171 profile_delete_entries_.erase(profile_path); 172 if (!profile_delete_entries_.empty()) 173 return; 174 // Run the callback if this is the last pending deletion. 175 if (!callback_.is_null()) 176 callback_.Run(); 177 // Request NetworkStateHandler manager update to update ServiceCompleteList. 178 owner_->network_state_handler_->UpdateManagerProperties(); 179 owner_->ProfileEntryDeleterCompleted(service_path_); // Deletes this. 180 } 181 182 void ShillErrorCallback(const std::string& profile_path, 183 const std::string& entry, 184 const std::string& dbus_error_name, 185 const std::string& dbus_error_message) { 186 // Any Shill Error triggers a failure / error. 187 network_handler::ShillErrorCallbackFunction( 188 "GetLoadableProfileEntries Failed", profile_path, error_callback_, 189 dbus_error_name, dbus_error_message); 190 // Delete this even if there are pending deletions; any callbacks will 191 // safely become no-ops (by invalidating the WeakPtrs). 192 owner_->ProfileEntryDeleterCompleted(service_path_); // Deletes this. 193 } 194 195 NetworkConfigurationHandler* owner_; // Unowned 196 std::string service_path_; 197 base::Closure callback_; 198 network_handler::ErrorCallback error_callback_; 199 200 // Map of pending profile entry deletions, indexed by profile path. 201 std::map<std::string, std::string> profile_delete_entries_; 202 203 DISALLOW_COPY_AND_ASSIGN(ProfileEntryDeleter); 204}; 205 206// NetworkConfigurationHandler 207 208void NetworkConfigurationHandler::GetProperties( 209 const std::string& service_path, 210 const network_handler::DictionaryResultCallback& callback, 211 const network_handler::ErrorCallback& error_callback) const { 212 NET_LOG_USER("GetProperties", service_path); 213 DBusThreadManager::Get()->GetShillServiceClient()->GetProperties( 214 dbus::ObjectPath(service_path), 215 base::Bind(&GetPropertiesCallback, 216 callback, error_callback, service_path)); 217} 218 219void NetworkConfigurationHandler::SetProperties( 220 const std::string& service_path, 221 const base::DictionaryValue& properties, 222 const base::Closure& callback, 223 const network_handler::ErrorCallback& error_callback) { 224 if (properties.empty()) { 225 if (!callback.is_null()) 226 callback.Run(); 227 return; 228 } 229 NET_LOG_USER("SetProperties", service_path); 230 LogConfigProperties("SetProperty", service_path, properties); 231 232 DBusThreadManager::Get()->GetShillServiceClient()->SetProperties( 233 dbus::ObjectPath(service_path), 234 properties, 235 base::Bind(&NetworkConfigurationHandler::SetPropertiesSuccessCallback, 236 AsWeakPtr(), service_path, callback), 237 base::Bind(&NetworkConfigurationHandler::SetPropertiesErrorCallback, 238 AsWeakPtr(), service_path, error_callback)); 239} 240 241void NetworkConfigurationHandler::ClearProperties( 242 const std::string& service_path, 243 const std::vector<std::string>& names, 244 const base::Closure& callback, 245 const network_handler::ErrorCallback& error_callback) { 246 if (names.empty()) { 247 if (!callback.is_null()) 248 callback.Run(); 249 return; 250 } 251 NET_LOG_USER("ClearProperties", service_path); 252 for (std::vector<std::string>::const_iterator iter = names.begin(); 253 iter != names.end(); ++iter) { 254 NET_LOG_DEBUG("ClearProperty", service_path + "." + *iter); 255 } 256 DBusThreadManager::Get()->GetShillServiceClient()->ClearProperties( 257 dbus::ObjectPath(service_path), 258 names, 259 base::Bind(&NetworkConfigurationHandler::ClearPropertiesSuccessCallback, 260 AsWeakPtr(), service_path, names, callback), 261 base::Bind(&NetworkConfigurationHandler::ClearPropertiesErrorCallback, 262 AsWeakPtr(), service_path, error_callback)); 263} 264 265void NetworkConfigurationHandler::CreateConfiguration( 266 const base::DictionaryValue& properties, 267 const network_handler::StringResultCallback& callback, 268 const network_handler::ErrorCallback& error_callback) { 269 ShillManagerClient* manager = 270 DBusThreadManager::Get()->GetShillManagerClient(); 271 std::string type; 272 properties.GetStringWithoutPathExpansion(shill::kTypeProperty, &type); 273 DCHECK(!type.empty()); 274 if (NetworkTypePattern::Ethernet().MatchesType(type)) { 275 InvokeErrorCallback( 276 shill_property_util::GetNetworkIdFromProperties(properties), 277 error_callback, 278 "ConfigureServiceForProfile: Invalid type: " + type); 279 return; 280 } 281 282 NET_LOG_USER("CreateConfiguration: " + type, 283 shill_property_util::GetNetworkIdFromProperties(properties)); 284 LogConfigProperties("Configure", type, properties); 285 286 std::string profile; 287 properties.GetStringWithoutPathExpansion(shill::kProfileProperty, 288 &profile); 289 DCHECK(!profile.empty()); 290 manager->ConfigureServiceForProfile( 291 dbus::ObjectPath(profile), 292 properties, 293 base::Bind(&NetworkConfigurationHandler::RunCreateNetworkCallback, 294 AsWeakPtr(), 295 callback), 296 base::Bind(&network_handler::ShillErrorCallbackFunction, 297 "Config.CreateConfiguration Failed", 298 "", 299 error_callback)); 300} 301 302void NetworkConfigurationHandler::RemoveConfiguration( 303 const std::string& service_path, 304 const base::Closure& callback, 305 const network_handler::ErrorCallback& error_callback) { 306 // Service.Remove is not reliable. Instead, request the profile entries 307 // for the service and remove each entry. 308 if (ContainsKey(profile_entry_deleters_,service_path)) { 309 InvokeErrorCallback( 310 service_path, error_callback, "RemoveConfigurationInProgress"); 311 return; 312 } 313 NET_LOG_USER("Remove Configuration", service_path); 314 ProfileEntryDeleter* deleter = 315 new ProfileEntryDeleter(this, service_path, callback, error_callback); 316 profile_entry_deleters_[service_path] = deleter; 317 deleter->Run(); 318} 319 320void NetworkConfigurationHandler::SetNetworkProfile( 321 const std::string& service_path, 322 const std::string& profile_path, 323 const base::Closure& callback, 324 const network_handler::ErrorCallback& error_callback) { 325 NET_LOG_USER("SetNetworkProfile", service_path + ": " + profile_path); 326 base::StringValue profile_path_value(profile_path); 327 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( 328 dbus::ObjectPath(service_path), 329 shill::kProfileProperty, 330 profile_path_value, 331 callback, 332 base::Bind(&SetNetworkProfileErrorCallback, 333 service_path, profile_path, error_callback)); 334} 335 336// NetworkConfigurationHandler Private methods 337 338NetworkConfigurationHandler::NetworkConfigurationHandler() 339 : network_state_handler_(NULL) { 340} 341 342NetworkConfigurationHandler::~NetworkConfigurationHandler() { 343 STLDeleteContainerPairSecondPointers( 344 profile_entry_deleters_.begin(), profile_entry_deleters_.end()); 345} 346 347void NetworkConfigurationHandler::Init( 348 NetworkStateHandler* network_state_handler) { 349 network_state_handler_ = network_state_handler; 350} 351 352void NetworkConfigurationHandler::RunCreateNetworkCallback( 353 const network_handler::StringResultCallback& callback, 354 const dbus::ObjectPath& service_path) { 355 if (!callback.is_null()) 356 callback.Run(service_path.value()); 357 // This may also get called when CreateConfiguration is used to update an 358 // existing configuration, so request a service update just in case. 359 // TODO(pneubeck): Separate 'Create' and 'Update' calls and only trigger 360 // this on an update. 361 network_state_handler_->RequestUpdateForNetwork(service_path.value()); 362} 363 364void NetworkConfigurationHandler::ProfileEntryDeleterCompleted( 365 const std::string& service_path) { 366 std::map<std::string, ProfileEntryDeleter*>::iterator iter = 367 profile_entry_deleters_.find(service_path); 368 DCHECK(iter != profile_entry_deleters_.end()); 369 delete iter->second; 370 profile_entry_deleters_.erase(iter); 371} 372 373void NetworkConfigurationHandler::SetPropertiesSuccessCallback( 374 const std::string& service_path, 375 const base::Closure& callback) { 376 if (!callback.is_null()) 377 callback.Run(); 378 network_state_handler_->RequestUpdateForNetwork(service_path); 379} 380 381void NetworkConfigurationHandler::SetPropertiesErrorCallback( 382 const std::string& service_path, 383 const network_handler::ErrorCallback& error_callback, 384 const std::string& dbus_error_name, 385 const std::string& dbus_error_message) { 386 network_handler::ShillErrorCallbackFunction( 387 "Config.SetProperties Failed", 388 service_path, error_callback, 389 dbus_error_name, dbus_error_message); 390 // Some properties may have changed so request an update regardless. 391 network_state_handler_->RequestUpdateForNetwork(service_path); 392} 393 394void NetworkConfigurationHandler::ClearPropertiesSuccessCallback( 395 const std::string& service_path, 396 const std::vector<std::string>& names, 397 const base::Closure& callback, 398 const base::ListValue& result) { 399 const std::string kClearPropertiesFailedError("Error.ClearPropertiesFailed"); 400 DCHECK(names.size() == result.GetSize()) 401 << "Incorrect result size from ClearProperties."; 402 403 for (size_t i = 0; i < result.GetSize(); ++i) { 404 bool success = false; 405 result.GetBoolean(i, &success); 406 if (!success) { 407 // If a property was cleared that has never been set, the clear will fail. 408 // We do not track which properties have been set, so just log the error. 409 NET_LOG_ERROR("ClearProperties Failed: " + names[i], service_path); 410 } 411 } 412 413 if (!callback.is_null()) 414 callback.Run(); 415 network_state_handler_->RequestUpdateForNetwork(service_path); 416} 417 418void NetworkConfigurationHandler::ClearPropertiesErrorCallback( 419 const std::string& service_path, 420 const network_handler::ErrorCallback& error_callback, 421 const std::string& dbus_error_name, 422 const std::string& dbus_error_message) { 423 network_handler::ShillErrorCallbackFunction( 424 "Config.ClearProperties Failed", 425 service_path, error_callback, 426 dbus_error_name, dbus_error_message); 427 // Some properties may have changed so request an update regardless. 428 network_state_handler_->RequestUpdateForNetwork(service_path); 429} 430 431// static 432NetworkConfigurationHandler* NetworkConfigurationHandler::InitializeForTest( 433 NetworkStateHandler* network_state_handler) { 434 NetworkConfigurationHandler* handler = new NetworkConfigurationHandler(); 435 handler->Init(network_state_handler); 436 return handler; 437} 438 439} // namespace chromeos 440