OLD | NEW |
(Empty) | |
| 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/managed_network_configuration_handler.h" |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/guid.h" |
| 12 #include "base/logging.h" |
| 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/values.h" |
| 16 #include "chromeos/dbus/dbus_method_call_status.h" |
| 17 #include "chromeos/dbus/dbus_thread_manager.h" |
| 18 #include "chromeos/dbus/shill_manager_client.h" |
| 19 #include "chromeos/dbus/shill_service_client.h" |
| 20 #include "chromeos/network/network_configuration_handler.h" |
| 21 #include "chromeos/network/network_state.h" |
| 22 #include "chromeos/network/network_state_handler.h" |
| 23 #include "chromeos/network/onc/onc_signature.h" |
| 24 #include "chromeos/network/onc/onc_translator.h" |
| 25 #include "dbus/object_path.h" |
| 26 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 27 |
| 28 namespace chromeos { |
| 29 |
| 30 namespace { |
| 31 |
| 32 ManagedNetworkConfigurationHandler* g_configuration_handler_instance = NULL; |
| 33 |
| 34 const char kGuidTag[] = "guid"; |
| 35 const char kUnableToFindGuid[] = "Error.UnableToFindGuid"; |
| 36 const char kUnableToFindGuidMessage[] = |
| 37 "Unable to match GUID to a service path."; |
| 38 const char kServicePathTag[] = "servicePath"; |
| 39 const char kUnableToFindServicePath[] = "Error.UnableToFindServicePath"; |
| 40 const char kUnableToFindServicePathMessage[] = |
| 41 "Unable to match service path to a GUID."; |
| 42 const char kErrorName[] = "errorName"; |
| 43 const char kErrorMessage[] = "errorMessage"; |
| 44 |
| 45 void RunErrorCallback(const std::string& guid, |
| 46 const std::string& service_path, |
| 47 const network_handler::ErrorCallback& error_callback, |
| 48 const std::string& error_name, |
| 49 const std::string& error_message) { |
| 50 // Couldn't find the GUID, so fire off the error callback. |
| 51 scoped_ptr<base::DictionaryValue> error_data(new base::DictionaryValue); |
| 52 error_data->SetString(kErrorName, error_name); |
| 53 error_data->SetString(kErrorMessage, error_message); |
| 54 if (!guid.empty()) |
| 55 error_data->SetString(kGuidTag, guid); |
| 56 if (!service_path.empty()) |
| 57 error_data->SetString(kServicePathTag, service_path); |
| 58 error_callback.Run(kUnableToFindGuid, error_data.Pass()); |
| 59 } |
| 60 |
| 61 std::string FindServicePathForGuid( |
| 62 const std::string& guid, |
| 63 const network_handler::ErrorCallback& error_callback) { |
| 64 NetworkStateHandler::NetworkStateList networks; |
| 65 NetworkStateHandler::Get()->GetNetworkList(&networks); |
| 66 |
| 67 for (NetworkStateHandler::NetworkStateList::iterator iter = networks.begin(); |
| 68 iter != networks.end(); ++iter) { |
| 69 if ((*iter)->guid() == guid) |
| 70 return (*iter)->path(); |
| 71 } |
| 72 RunErrorCallback(guid, |
| 73 "", |
| 74 error_callback, |
| 75 kUnableToFindGuid, |
| 76 kUnableToFindGuidMessage); |
| 77 return std::string(); |
| 78 } |
| 79 |
| 80 std::string FindGuidForServicePath( |
| 81 const std::string& service_path, |
| 82 const network_handler::ErrorCallback& error_callback) { |
| 83 NetworkStateHandler::NetworkStateList networks; |
| 84 NetworkStateHandler::Get()->GetNetworkList(&networks); |
| 85 |
| 86 for (NetworkStateHandler::NetworkStateList::iterator iter = networks.begin(); |
| 87 iter != networks.end(); ++iter) { |
| 88 if ((*iter)->path() == service_path) |
| 89 return (*iter)->guid(); |
| 90 } |
| 91 RunErrorCallback("", |
| 92 service_path, |
| 93 error_callback, |
| 94 kUnableToFindServicePath, |
| 95 kUnableToFindServicePathMessage); |
| 96 return std::string(); |
| 97 } |
| 98 |
| 99 void ErrorCallback(const std::string& guid, |
| 100 const network_handler::ErrorCallback& error_callback, |
| 101 const std::string& error_name, |
| 102 scoped_ptr<base::DictionaryValue> error_data) { |
| 103 error_data->SetString(kGuidTag, guid); |
| 104 error_callback.Run(error_name, error_data.Pass()); |
| 105 } |
| 106 |
| 107 // This is here so that we can pass back the GUID instead of the service path. |
| 108 void ConfigurationCallback( |
| 109 const network_handler::StringResultCallback& callback, |
| 110 scoped_ptr<base::DictionaryValue> properties, |
| 111 const std::string& service_path) { |
| 112 std::string guid; |
| 113 if (!properties->GetString(flimflam::kGuidProperty, &guid)) { |
| 114 NOTREACHED() << "Missing GUID in new configuration."; |
| 115 return; |
| 116 } |
| 117 callback.Run(guid); |
| 118 } |
| 119 |
| 120 void TranslatePropertiesCallback( |
| 121 const network_handler::DictionaryResultCallback& callback, |
| 122 const network_handler::ErrorCallback& error_callback, |
| 123 const std::string& service_path, |
| 124 const base::DictionaryValue& result) { |
| 125 std::string guid = FindGuidForServicePath(service_path, error_callback); |
| 126 if (guid.empty()) |
| 127 return; |
| 128 |
| 129 scoped_ptr<base::DictionaryValue> onc_result( |
| 130 onc::TranslateShillServiceToONCPart( |
| 131 result, |
| 132 &onc::kNetworkConfigurationSignature)); |
| 133 callback.Run(guid, *onc_result); |
| 134 } |
| 135 |
| 136 } // namespace |
| 137 |
| 138 // static |
| 139 void ManagedNetworkConfigurationHandler::Initialize() { |
| 140 CHECK(!g_configuration_handler_instance); |
| 141 g_configuration_handler_instance = new ManagedNetworkConfigurationHandler; |
| 142 } |
| 143 |
| 144 // static |
| 145 void ManagedNetworkConfigurationHandler::Shutdown() { |
| 146 CHECK(g_configuration_handler_instance); |
| 147 delete g_configuration_handler_instance; |
| 148 g_configuration_handler_instance = NULL; |
| 149 } |
| 150 |
| 151 // static |
| 152 ManagedNetworkConfigurationHandler* ManagedNetworkConfigurationHandler::Get() { |
| 153 CHECK(g_configuration_handler_instance); |
| 154 return g_configuration_handler_instance; |
| 155 } |
| 156 |
| 157 void ManagedNetworkConfigurationHandler::GetProperties( |
| 158 const std::string& guid, |
| 159 const network_handler::DictionaryResultCallback& callback, |
| 160 const network_handler::ErrorCallback& error_callback) const { |
| 161 std::string service_path = FindServicePathForGuid(guid, error_callback); |
| 162 if (service_path.empty()) |
| 163 return; |
| 164 |
| 165 NetworkConfigurationHandler::Get()->GetProperties( |
| 166 service_path, |
| 167 base::Bind(&TranslatePropertiesCallback, callback, error_callback), |
| 168 base::Bind(&ErrorCallback, guid, error_callback)); |
| 169 } |
| 170 |
| 171 void ManagedNetworkConfigurationHandler::SetProperties( |
| 172 const std::string& guid, |
| 173 const base::DictionaryValue& properties, |
| 174 const base::Closure& callback, |
| 175 const network_handler::ErrorCallback& error_callback) const { |
| 176 std::string service_path = FindServicePathForGuid(guid, error_callback); |
| 177 if (service_path.empty()) |
| 178 return; |
| 179 |
| 180 // Convert the given ONC properties into a Shill dictionary. |
| 181 scoped_ptr<base::DictionaryValue> shill_result( |
| 182 onc::TranslateONCObjectToShill( |
| 183 properties, |
| 184 &onc::kNetworkConfigurationSignature)); |
| 185 |
| 186 NetworkConfigurationHandler::Get()->SetProperties( |
| 187 service_path, |
| 188 *shill_result, |
| 189 callback, |
| 190 base::Bind(&ErrorCallback, guid, error_callback)); |
| 191 } |
| 192 |
| 193 void ManagedNetworkConfigurationHandler::ClearProperties( |
| 194 const std::string& guid, |
| 195 const std::vector<std::string>& names, |
| 196 const base::Closure& callback, |
| 197 const network_handler::ErrorCallback& error_callback) { |
| 198 std::string service_path = FindServicePathForGuid(guid, error_callback); |
| 199 if (service_path.empty()) |
| 200 return; |
| 201 |
| 202 std::vector<std::string> shill_names = onc::TranslateONCPropertyNamesToShill( |
| 203 names, &onc::kNetworkConfigurationSignature); |
| 204 |
| 205 NetworkConfigurationHandler::Get()->ClearProperties( |
| 206 service_path, |
| 207 shill_names, |
| 208 callback, |
| 209 base::Bind(&ErrorCallback, guid, error_callback)); |
| 210 } |
| 211 |
| 212 void ManagedNetworkConfigurationHandler::Connect( |
| 213 const std::string& guid, |
| 214 const base::Closure& callback, |
| 215 const network_handler::ErrorCallback& error_callback) const { |
| 216 std::string service_path = FindServicePathForGuid(guid, error_callback); |
| 217 if (service_path.empty()) |
| 218 return; |
| 219 |
| 220 NetworkConfigurationHandler::Get()->Connect( |
| 221 service_path, |
| 222 callback, |
| 223 base::Bind(&ErrorCallback, guid, error_callback)); |
| 224 } |
| 225 |
| 226 void ManagedNetworkConfigurationHandler::Disconnect( |
| 227 const std::string& guid, |
| 228 const base::Closure& callback, |
| 229 const network_handler::ErrorCallback& error_callback) const { |
| 230 std::string service_path = FindServicePathForGuid(guid, error_callback); |
| 231 if (service_path.empty()) |
| 232 return; |
| 233 |
| 234 NetworkConfigurationHandler::Get()->Disconnect( |
| 235 service_path, |
| 236 callback, |
| 237 base::Bind(&ErrorCallback, guid, error_callback)); |
| 238 } |
| 239 |
| 240 void ManagedNetworkConfigurationHandler::CreateConfiguration( |
| 241 const base::DictionaryValue& properties, |
| 242 const network_handler::StringResultCallback& callback, |
| 243 const network_handler::ErrorCallback& error_callback) const { |
| 244 // Convert this to a Shill dictionary. |
| 245 scoped_ptr<base::DictionaryValue> shill_properties( |
| 246 onc::TranslateONCObjectToShill( |
| 247 properties, |
| 248 &onc::kNetworkConfigurationSignature)); |
| 249 |
| 250 // If there isn't already a GUID attached to these properties, then |
| 251 // generate one and add it. |
| 252 std::string existing_guid; |
| 253 if (!properties.GetString(flimflam::kGuidProperty, &existing_guid)) { |
| 254 existing_guid = base::GenerateGUID(); |
| 255 shill_properties->SetString(flimflam::kGuidProperty, existing_guid); |
| 256 } |
| 257 |
| 258 NetworkConfigurationHandler::Get()->CreateConfiguration( |
| 259 *shill_properties, |
| 260 base::Bind(&ConfigurationCallback, |
| 261 callback, |
| 262 base::Passed(&shill_properties)), |
| 263 base::Bind(&ErrorCallback, existing_guid, error_callback)); |
| 264 } |
| 265 |
| 266 void ManagedNetworkConfigurationHandler::RemoveConfiguration( |
| 267 const std::string& guid, |
| 268 const base::Closure& callback, |
| 269 const network_handler::ErrorCallback& error_callback) const { |
| 270 std::string service_path = FindServicePathForGuid(guid, error_callback); |
| 271 if (service_path.empty()) |
| 272 return; |
| 273 |
| 274 NetworkConfigurationHandler::Get()->RemoveConfiguration( |
| 275 service_path, |
| 276 callback, |
| 277 base::Bind(&ErrorCallback, guid, error_callback)); |
| 278 } |
| 279 |
| 280 ManagedNetworkConfigurationHandler::ManagedNetworkConfigurationHandler() { |
| 281 } |
| 282 |
| 283 ManagedNetworkConfigurationHandler::~ManagedNetworkConfigurationHandler() { |
| 284 } |
| 285 |
| 286 } // namespace chromeos |
OLD | NEW |