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.
5 #include "chromeos/network/network_configuration_handler.h"
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"
33 // Strip surrounding "" from keys (if present).
34 std::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);
41 void 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
);
50 void 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 // Get the correct name from WifiHex if necessary.
57 scoped_ptr
<base::DictionaryValue
> properties_copy(properties
.DeepCopy());
59 shill_property_util::GetNameFromProperties(service_path
, properties
);
61 properties_copy
->SetStringWithoutPathExpansion(shill::kNameProperty
, name
);
63 if (call_status
!= DBUS_METHOD_CALL_SUCCESS
) {
64 // Because network services are added and removed frequently, we will see
65 // failures regularly, so don't log these.
66 network_handler::RunErrorCallback(error_callback
,
68 network_handler::kDBusFailedError
,
69 network_handler::kDBusFailedErrorMessage
);
70 } else if (!callback
.is_null()) {
71 callback
.Run(service_path
, *properties_copy
.get());
75 void SetNetworkProfileErrorCallback(
76 const std::string
& service_path
,
77 const std::string
& profile_path
,
78 const network_handler::ErrorCallback
& error_callback
,
79 const std::string
& dbus_error_name
,
80 const std::string
& dbus_error_message
) {
81 network_handler::ShillErrorCallbackFunction(
82 "Config.SetNetworkProfile Failed: " + profile_path
,
83 service_path
, error_callback
,
84 dbus_error_name
, dbus_error_message
);
87 void LogConfigProperties(const std::string
& desc
,
88 const std::string
& path
,
89 const base::DictionaryValue
& properties
) {
90 for (base::DictionaryValue::Iterator
iter(properties
);
91 !iter
.IsAtEnd(); iter
.Advance()) {
92 std::string v
= "******";
93 if (!shill_property_util::IsPassphraseKey(iter
.key()))
94 base::JSONWriter::Write(&iter
.value(), &v
);
95 NET_LOG_DEBUG(desc
, path
+ "." + iter
.key() + "=" + v
);
101 // Helper class to request from Shill the profile entries associated with a
102 // Service and delete the service from each profile. Triggers either
103 // |callback| on success or |error_callback| on failure, and calls
104 // |handler|->ProfileEntryDeleterCompleted() on completion to delete itself.
105 class NetworkConfigurationHandler::ProfileEntryDeleter
106 : public base::SupportsWeakPtr
<ProfileEntryDeleter
> {
108 ProfileEntryDeleter(NetworkConfigurationHandler
* handler
,
109 const std::string
& service_path
,
110 const base::Closure
& callback
,
111 const network_handler::ErrorCallback
& error_callback
)
113 service_path_(service_path
),
115 error_callback_(error_callback
) {
119 DBusThreadManager::Get()->GetShillServiceClient()->
120 GetLoadableProfileEntries(
121 dbus::ObjectPath(service_path_
),
122 base::Bind(&ProfileEntryDeleter::GetProfileEntriesToDeleteCallback
,
127 void GetProfileEntriesToDeleteCallback(
128 DBusMethodCallStatus call_status
,
129 const base::DictionaryValue
& profile_entries
) {
130 if (call_status
!= DBUS_METHOD_CALL_SUCCESS
) {
132 service_path_
, error_callback_
, "GetLoadableProfileEntriesFailed");
133 owner_
->ProfileEntryDeleterCompleted(service_path_
); // Deletes this.
137 for (base::DictionaryValue::Iterator
iter(profile_entries
);
138 !iter
.IsAtEnd(); iter
.Advance()) {
139 std::string profile_path
= StripQuotations(iter
.key());
140 std::string entry_path
;
141 iter
.value().GetAsString(&entry_path
);
142 if (profile_path
.empty() || entry_path
.empty()) {
143 NET_LOG_ERROR("Failed to parse Profile Entry", base::StringPrintf(
144 "%s: %s", profile_path
.c_str(), entry_path
.c_str()));
147 if (profile_delete_entries_
.count(profile_path
) != 0) {
148 NET_LOG_ERROR("Multiple Profile Entries", base::StringPrintf(
149 "%s: %s", profile_path
.c_str(), entry_path
.c_str()));
152 NET_LOG_DEBUG("Delete Profile Entry", base::StringPrintf(
153 "%s: %s", profile_path
.c_str(), entry_path
.c_str()));
154 profile_delete_entries_
[profile_path
] = entry_path
;
155 DBusThreadManager::Get()->GetShillProfileClient()->DeleteEntry(
156 dbus::ObjectPath(profile_path
),
158 base::Bind(&ProfileEntryDeleter::ProfileEntryDeletedCallback
,
159 AsWeakPtr(), profile_path
, entry_path
),
160 base::Bind(&ProfileEntryDeleter::ShillErrorCallback
,
161 AsWeakPtr(), profile_path
, entry_path
));
165 void ProfileEntryDeletedCallback(const std::string
& profile_path
,
166 const std::string
& entry
) {
167 NET_LOG_DEBUG("Profile Entry Deleted", base::StringPrintf(
168 "%s: %s", profile_path
.c_str(), entry
.c_str()));
169 profile_delete_entries_
.erase(profile_path
);
170 if (!profile_delete_entries_
.empty())
172 // Run the callback if this is the last pending deletion.
173 if (!callback_
.is_null())
175 // Request NetworkStateHandler manager update to update ServiceCompleteList.
176 owner_
->network_state_handler_
->UpdateManagerProperties();
177 owner_
->ProfileEntryDeleterCompleted(service_path_
); // Deletes this.
180 void ShillErrorCallback(const std::string
& profile_path
,
181 const std::string
& entry
,
182 const std::string
& dbus_error_name
,
183 const std::string
& dbus_error_message
) {
184 // Any Shill Error triggers a failure / error.
185 network_handler::ShillErrorCallbackFunction(
186 "GetLoadableProfileEntries Failed", profile_path
, error_callback_
,
187 dbus_error_name
, dbus_error_message
);
188 // Delete this even if there are pending deletions; any callbacks will
189 // safely become no-ops (by invalidating the WeakPtrs).
190 owner_
->ProfileEntryDeleterCompleted(service_path_
); // Deletes this.
193 NetworkConfigurationHandler
* owner_
; // Unowned
194 std::string service_path_
;
195 base::Closure callback_
;
196 network_handler::ErrorCallback error_callback_
;
198 // Map of pending profile entry deletions, indexed by profile path.
199 std::map
<std::string
, std::string
> profile_delete_entries_
;
201 DISALLOW_COPY_AND_ASSIGN(ProfileEntryDeleter
);
204 // NetworkConfigurationHandler
206 void NetworkConfigurationHandler::GetProperties(
207 const std::string
& service_path
,
208 const network_handler::DictionaryResultCallback
& callback
,
209 const network_handler::ErrorCallback
& error_callback
) const {
210 DBusThreadManager::Get()->GetShillServiceClient()->GetProperties(
211 dbus::ObjectPath(service_path
),
212 base::Bind(&GetPropertiesCallback
,
213 callback
, error_callback
, service_path
));
216 void NetworkConfigurationHandler::SetProperties(
217 const std::string
& service_path
,
218 const base::DictionaryValue
& properties
,
219 const base::Closure
& callback
,
220 const network_handler::ErrorCallback
& error_callback
) {
221 if (properties
.empty()) {
222 if (!callback
.is_null())
226 NET_LOG_USER("SetProperties", service_path
);
227 LogConfigProperties("SetProperty", service_path
, properties
);
229 DBusThreadManager::Get()->GetShillServiceClient()->SetProperties(
230 dbus::ObjectPath(service_path
),
232 base::Bind(&NetworkConfigurationHandler::SetPropertiesSuccessCallback
,
233 AsWeakPtr(), service_path
, callback
),
234 base::Bind(&NetworkConfigurationHandler::SetPropertiesErrorCallback
,
235 AsWeakPtr(), service_path
, error_callback
));
238 void NetworkConfigurationHandler::ClearProperties(
239 const std::string
& service_path
,
240 const std::vector
<std::string
>& names
,
241 const base::Closure
& callback
,
242 const network_handler::ErrorCallback
& error_callback
) {
244 if (!callback
.is_null())
248 NET_LOG_USER("ClearProperties", service_path
);
249 for (std::vector
<std::string
>::const_iterator iter
= names
.begin();
250 iter
!= names
.end(); ++iter
) {
251 NET_LOG_DEBUG("ClearProperty", service_path
+ "." + *iter
);
253 DBusThreadManager::Get()->GetShillServiceClient()->ClearProperties(
254 dbus::ObjectPath(service_path
),
256 base::Bind(&NetworkConfigurationHandler::ClearPropertiesSuccessCallback
,
257 AsWeakPtr(), service_path
, names
, callback
, error_callback
),
258 base::Bind(&NetworkConfigurationHandler::ClearPropertiesErrorCallback
,
259 AsWeakPtr(), service_path
, error_callback
));
262 void NetworkConfigurationHandler::CreateConfiguration(
263 const base::DictionaryValue
& properties
,
264 const network_handler::StringResultCallback
& callback
,
265 const network_handler::ErrorCallback
& error_callback
) {
266 ShillManagerClient
* manager
=
267 DBusThreadManager::Get()->GetShillManagerClient();
269 properties
.GetStringWithoutPathExpansion(shill::kTypeProperty
, &type
);
270 if (NetworkTypePattern::Ethernet().MatchesType(type
)) {
272 "" /* no service path */,
274 "ConfigureServiceForProfile is not implemented for Ethernet");
278 NET_LOG_USER("CreateConfiguration", type
);
279 LogConfigProperties("Configure", type
, properties
);
282 properties
.GetStringWithoutPathExpansion(shill::kProfileProperty
,
284 DCHECK(!profile
.empty());
285 manager
->ConfigureServiceForProfile(
286 dbus::ObjectPath(profile
),
288 base::Bind(&NetworkConfigurationHandler::RunCreateNetworkCallback
,
291 base::Bind(&network_handler::ShillErrorCallbackFunction
,
292 "Config.CreateConfiguration Failed",
297 void NetworkConfigurationHandler::RemoveConfiguration(
298 const std::string
& service_path
,
299 const base::Closure
& callback
,
300 const network_handler::ErrorCallback
& error_callback
) {
301 // Service.Remove is not reliable. Instead, request the profile entries
302 // for the service and remove each entry.
303 if (ContainsKey(profile_entry_deleters_
,service_path
)) {
305 service_path
, error_callback
, "RemoveConfigurationInProgress");
308 NET_LOG_USER("Remove Configuration", service_path
);
309 ProfileEntryDeleter
* deleter
=
310 new ProfileEntryDeleter(this, service_path
, callback
, error_callback
);
311 profile_entry_deleters_
[service_path
] = deleter
;
315 void NetworkConfigurationHandler::SetNetworkProfile(
316 const std::string
& service_path
,
317 const std::string
& profile_path
,
318 const base::Closure
& callback
,
319 const network_handler::ErrorCallback
& error_callback
) {
320 NET_LOG_USER("SetNetworkProfile", service_path
+ ": " + profile_path
);
321 base::StringValue
profile_path_value(profile_path
);
322 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
323 dbus::ObjectPath(service_path
),
324 shill::kProfileProperty
,
327 base::Bind(&SetNetworkProfileErrorCallback
,
328 service_path
, profile_path
, error_callback
));
331 // NetworkConfigurationHandler Private methods
333 NetworkConfigurationHandler::NetworkConfigurationHandler()
334 : network_state_handler_(NULL
) {
337 NetworkConfigurationHandler::~NetworkConfigurationHandler() {
338 STLDeleteContainerPairSecondPointers(
339 profile_entry_deleters_
.begin(), profile_entry_deleters_
.end());
342 void NetworkConfigurationHandler::Init(
343 NetworkStateHandler
* network_state_handler
) {
344 network_state_handler_
= network_state_handler
;
347 void NetworkConfigurationHandler::RunCreateNetworkCallback(
348 const network_handler::StringResultCallback
& callback
,
349 const dbus::ObjectPath
& service_path
) {
350 if (!callback
.is_null())
351 callback
.Run(service_path
.value());
352 // This may also get called when CreateConfiguration is used to update an
353 // existing configuration, so request a service update just in case.
354 // TODO(pneubeck): Separate 'Create' and 'Update' calls and only trigger
355 // this on an update.
356 network_state_handler_
->RequestUpdateForNetwork(service_path
.value());
359 void NetworkConfigurationHandler::ProfileEntryDeleterCompleted(
360 const std::string
& service_path
) {
361 std::map
<std::string
, ProfileEntryDeleter
*>::iterator iter
=
362 profile_entry_deleters_
.find(service_path
);
363 DCHECK(iter
!= profile_entry_deleters_
.end());
365 profile_entry_deleters_
.erase(iter
);
368 void NetworkConfigurationHandler::SetPropertiesSuccessCallback(
369 const std::string
& service_path
,
370 const base::Closure
& callback
) {
371 if (!callback
.is_null())
373 network_state_handler_
->RequestUpdateForNetwork(service_path
);
376 void NetworkConfigurationHandler::SetPropertiesErrorCallback(
377 const std::string
& service_path
,
378 const network_handler::ErrorCallback
& error_callback
,
379 const std::string
& dbus_error_name
,
380 const std::string
& dbus_error_message
) {
381 network_handler::ShillErrorCallbackFunction(
382 "Config.SetProperties Failed",
383 service_path
, error_callback
,
384 dbus_error_name
, dbus_error_message
);
385 // Some properties may have changed so request an update regardless.
386 network_state_handler_
->RequestUpdateForNetwork(service_path
);
389 void NetworkConfigurationHandler::ClearPropertiesSuccessCallback(
390 const std::string
& service_path
,
391 const std::vector
<std::string
>& names
,
392 const base::Closure
& callback
,
393 const network_handler::ErrorCallback
& error_callback
,
394 const base::ListValue
& result
) {
395 const std::string
kClearPropertiesFailedError("Error.ClearPropertiesFailed");
396 DCHECK(names
.size() == result
.GetSize())
397 << "Incorrect result size from ClearProperties.";
399 bool some_failed
= false;
400 for (size_t i
= 0; i
< result
.GetSize(); ++i
) {
401 bool success
= false;
402 result
.GetBoolean(i
, &success
);
404 NET_LOG_ERROR("ClearProperties Failed: " + names
[i
], service_path
);
410 if (!error_callback
.is_null()) {
411 scoped_ptr
<base::DictionaryValue
> error_data(
412 network_handler::CreateErrorData(
413 service_path
, kClearPropertiesFailedError
,
414 base::StringPrintf("Errors: %" PRIuS
, result
.GetSize())));
415 error_data
->Set("errors", result
.DeepCopy());
416 scoped_ptr
<base::ListValue
> name_list(new base::ListValue
);
417 name_list
->AppendStrings(names
);
418 error_data
->Set("names", name_list
.release());
419 error_callback
.Run(kClearPropertiesFailedError
, error_data
.Pass());
421 } else if (!callback
.is_null()) {
424 network_state_handler_
->RequestUpdateForNetwork(service_path
);
427 void NetworkConfigurationHandler::ClearPropertiesErrorCallback(
428 const std::string
& service_path
,
429 const network_handler::ErrorCallback
& error_callback
,
430 const std::string
& dbus_error_name
,
431 const std::string
& dbus_error_message
) {
432 network_handler::ShillErrorCallbackFunction(
433 "Config.ClearProperties Failed",
434 service_path
, error_callback
,
435 dbus_error_name
, dbus_error_message
);
436 // Some properties may have changed so request an update regardless.
437 network_state_handler_
->RequestUpdateForNetwork(service_path
);
441 NetworkConfigurationHandler
* NetworkConfigurationHandler::InitializeForTest(
442 NetworkStateHandler
* network_state_handler
) {
443 NetworkConfigurationHandler
* handler
= new NetworkConfigurationHandler();
444 handler
->Init(network_state_handler
);
448 } // namespace chromeos