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.
5 #include "chromeos/dbus/fake_shill_service_client.h"
8 #include "base/bind_helpers.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/stl_util.h"
11 #include "base/strings/string_util.h"
12 #include "base/values.h"
13 #include "chromeos/dbus/dbus_thread_manager.h"
14 #include "chromeos/dbus/shill_device_client.h"
15 #include "chromeos/dbus/shill_manager_client.h"
16 #include "chromeos/dbus/shill_profile_client.h"
17 #include "chromeos/dbus/shill_property_changed_observer.h"
18 #include "chromeos/network/shill_property_util.h"
20 #include "dbus/message.h"
21 #include "dbus/object_path.h"
22 #include "third_party/cros_system_api/dbus/service_constants.h"
28 void PassStubListValue(const ShillServiceClient::ListValueCallback
& callback
,
29 base::ListValue
* value
) {
33 void PassStubServiceProperties(
34 const ShillServiceClient::DictionaryValueCallback
& callback
,
35 DBusMethodCallStatus call_status
,
36 const base::DictionaryValue
* properties
) {
37 callback
.Run(call_status
, *properties
);
40 void CallSortManagerServices() {
41 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
42 SortManagerServices(true);
45 int GetInteractiveDelay() {
46 return DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
47 GetInteractiveDelay();
52 FakeShillServiceClient::FakeShillServiceClient() : weak_ptr_factory_(this) {
55 FakeShillServiceClient::~FakeShillServiceClient() {
56 STLDeleteContainerPairSecondPointers(
57 observer_list_
.begin(), observer_list_
.end());
61 // ShillServiceClient overrides.
63 void FakeShillServiceClient::Init(dbus::Bus
* bus
) {
66 void FakeShillServiceClient::AddPropertyChangedObserver(
67 const dbus::ObjectPath
& service_path
,
68 ShillPropertyChangedObserver
* observer
) {
69 GetObserverList(service_path
).AddObserver(observer
);
72 void FakeShillServiceClient::RemovePropertyChangedObserver(
73 const dbus::ObjectPath
& service_path
,
74 ShillPropertyChangedObserver
* observer
) {
75 GetObserverList(service_path
).RemoveObserver(observer
);
78 void FakeShillServiceClient::GetProperties(
79 const dbus::ObjectPath
& service_path
,
80 const DictionaryValueCallback
& callback
) {
81 base::DictionaryValue
* nested_dict
= NULL
;
82 scoped_ptr
<base::DictionaryValue
> result_properties
;
83 DBusMethodCallStatus call_status
;
84 stub_services_
.GetDictionaryWithoutPathExpansion(service_path
.value(),
87 result_properties
.reset(nested_dict
->DeepCopy());
88 // Remove credentials that Shill wouldn't send.
89 result_properties
->RemoveWithoutPathExpansion(shill::kPassphraseProperty
,
91 call_status
= DBUS_METHOD_CALL_SUCCESS
;
93 // This may happen if we remove services from the list.
94 VLOG(2) << "Properties not found for: " << service_path
.value();
95 result_properties
.reset(new base::DictionaryValue
);
96 call_status
= DBUS_METHOD_CALL_FAILURE
;
99 base::MessageLoop::current()->PostTask(
101 base::Bind(&PassStubServiceProperties
,
104 base::Owned(result_properties
.release())));
107 void FakeShillServiceClient::SetProperty(const dbus::ObjectPath
& service_path
,
108 const std::string
& name
,
109 const base::Value
& value
,
110 const base::Closure
& callback
,
111 const ErrorCallback
& error_callback
) {
112 if (!SetServiceProperty(service_path
.value(), name
, value
)) {
113 LOG(ERROR
) << "Service not found: " << service_path
.value();
114 error_callback
.Run("Error.InvalidService", "Invalid Service");
117 base::MessageLoop::current()->PostTask(FROM_HERE
, callback
);
120 void FakeShillServiceClient::SetProperties(
121 const dbus::ObjectPath
& service_path
,
122 const base::DictionaryValue
& properties
,
123 const base::Closure
& callback
,
124 const ErrorCallback
& error_callback
) {
125 for (base::DictionaryValue::Iterator
iter(properties
);
126 !iter
.IsAtEnd(); iter
.Advance()) {
127 if (!SetServiceProperty(service_path
.value(), iter
.key(), iter
.value())) {
128 LOG(ERROR
) << "Service not found: " << service_path
.value();
129 error_callback
.Run("Error.InvalidService", "Invalid Service");
133 base::MessageLoop::current()->PostTask(FROM_HERE
, callback
);
136 void FakeShillServiceClient::ClearProperty(
137 const dbus::ObjectPath
& service_path
,
138 const std::string
& name
,
139 const base::Closure
& callback
,
140 const ErrorCallback
& error_callback
) {
141 base::DictionaryValue
* dict
= NULL
;
142 if (!stub_services_
.GetDictionaryWithoutPathExpansion(
143 service_path
.value(), &dict
)) {
144 error_callback
.Run("Error.InvalidService", "Invalid Service");
147 dict
->RemoveWithoutPathExpansion(name
, NULL
);
148 // Note: Shill does not send notifications when properties are cleared.
149 base::MessageLoop::current()->PostTask(FROM_HERE
, callback
);
152 void FakeShillServiceClient::ClearProperties(
153 const dbus::ObjectPath
& service_path
,
154 const std::vector
<std::string
>& names
,
155 const ListValueCallback
& callback
,
156 const ErrorCallback
& error_callback
) {
157 base::DictionaryValue
* dict
= NULL
;
158 if (!stub_services_
.GetDictionaryWithoutPathExpansion(
159 service_path
.value(), &dict
)) {
160 error_callback
.Run("Error.InvalidService", "Invalid Service");
163 scoped_ptr
<base::ListValue
> results(new base::ListValue
);
164 for (std::vector
<std::string
>::const_iterator iter
= names
.begin();
165 iter
!= names
.end(); ++iter
) {
166 dict
->RemoveWithoutPathExpansion(*iter
, NULL
);
167 // Note: Shill does not send notifications when properties are cleared.
168 results
->AppendBoolean(true);
170 base::MessageLoop::current()->PostTask(
172 base::Bind(&PassStubListValue
,
173 callback
, base::Owned(results
.release())));
176 void FakeShillServiceClient::Connect(const dbus::ObjectPath
& service_path
,
177 const base::Closure
& callback
,
178 const ErrorCallback
& error_callback
) {
179 VLOG(1) << "FakeShillServiceClient::Connect: " << service_path
.value();
180 base::DictionaryValue
* service_properties
= NULL
;
181 if (!stub_services_
.GetDictionary(
182 service_path
.value(), &service_properties
)) {
183 LOG(ERROR
) << "Service not found: " << service_path
.value();
184 error_callback
.Run("Error.InvalidService", "Invalid Service");
188 // Set any other services of the same Type to 'offline' first, before setting
189 // State to Association which will trigger sorting Manager.Services and
190 // sending an update.
191 SetOtherServicesOffline(service_path
.value());
194 base::StringValue
associating_value(shill::kStateAssociation
);
195 SetServiceProperty(service_path
.value(),
196 shill::kStateProperty
,
199 // Stay Associating until the state is changed again after a delay.
200 base::MessageLoop::current()->PostDelayedTask(
202 base::Bind(&FakeShillServiceClient::ContinueConnect
,
203 weak_ptr_factory_
.GetWeakPtr(),
204 service_path
.value()),
205 base::TimeDelta::FromSeconds(GetInteractiveDelay()));
210 void FakeShillServiceClient::Disconnect(const dbus::ObjectPath
& service_path
,
211 const base::Closure
& callback
,
212 const ErrorCallback
& error_callback
) {
213 base::Value
* service
;
214 if (!stub_services_
.Get(service_path
.value(), &service
)) {
215 error_callback
.Run("Error.InvalidService", "Invalid Service");
218 // Set Idle after a delay
219 base::StringValue
idle_value(shill::kStateIdle
);
220 base::MessageLoop::current()->PostDelayedTask(
222 base::Bind(&FakeShillServiceClient::SetProperty
,
223 weak_ptr_factory_
.GetWeakPtr(),
225 shill::kStateProperty
,
227 base::Bind(&base::DoNothing
),
229 base::TimeDelta::FromSeconds(GetInteractiveDelay()));
233 void FakeShillServiceClient::Remove(const dbus::ObjectPath
& service_path
,
234 const base::Closure
& callback
,
235 const ErrorCallback
& error_callback
) {
236 base::MessageLoop::current()->PostTask(FROM_HERE
, callback
);
239 void FakeShillServiceClient::ActivateCellularModem(
240 const dbus::ObjectPath
& service_path
,
241 const std::string
& carrier
,
242 const base::Closure
& callback
,
243 const ErrorCallback
& error_callback
) {
244 base::DictionaryValue
* service_properties
=
245 GetModifiableServiceProperties(service_path
.value(), false);
246 if (!service_properties
) {
247 LOG(ERROR
) << "Service not found: " << service_path
.value();
248 error_callback
.Run("Error.InvalidService", "Invalid Service");
250 SetServiceProperty(service_path
.value(),
251 shill::kActivationStateProperty
,
252 base::StringValue(shill::kActivationStateActivating
));
253 // Set Activated after a delay
254 base::MessageLoop::current()->PostDelayedTask(
256 base::Bind(&FakeShillServiceClient::SetCellularActivated
,
257 weak_ptr_factory_
.GetWeakPtr(),
260 base::TimeDelta::FromSeconds(GetInteractiveDelay()));
262 base::MessageLoop::current()->PostTask(FROM_HERE
, callback
);
265 void FakeShillServiceClient::CompleteCellularActivation(
266 const dbus::ObjectPath
& service_path
,
267 const base::Closure
& callback
,
268 const ErrorCallback
& error_callback
) {
269 base::MessageLoop::current()->PostTask(FROM_HERE
, callback
);
272 void FakeShillServiceClient::GetLoadableProfileEntries(
273 const dbus::ObjectPath
& service_path
,
274 const DictionaryValueCallback
& callback
) {
275 // Provide a dictionary with a single { profile_path, service_path } entry
276 // if the Profile property is set, or an empty dictionary.
277 scoped_ptr
<base::DictionaryValue
> result_properties(
278 new base::DictionaryValue
);
279 base::DictionaryValue
* service_properties
=
280 GetModifiableServiceProperties(service_path
.value(), false);
281 if (service_properties
) {
282 std::string profile_path
;
283 if (service_properties
->GetStringWithoutPathExpansion(
284 shill::kProfileProperty
, &profile_path
)) {
285 result_properties
->SetStringWithoutPathExpansion(
286 profile_path
, service_path
.value());
289 LOG(WARNING
) << "Service not in profile: " << service_path
.value();
292 DBusMethodCallStatus call_status
= DBUS_METHOD_CALL_SUCCESS
;
293 base::MessageLoop::current()->PostTask(
295 base::Bind(&PassStubServiceProperties
,
298 base::Owned(result_properties
.release())));
301 ShillServiceClient::TestInterface
* FakeShillServiceClient::GetTestInterface() {
305 // ShillServiceClient::TestInterface overrides.
307 void FakeShillServiceClient::AddService(const std::string
& service_path
,
308 const std::string
& guid
,
309 const std::string
& name
,
310 const std::string
& type
,
311 const std::string
& state
,
313 AddServiceWithIPConfig(service_path
, guid
, name
,
314 type
, state
, "" /* ipconfig_path */,
318 void FakeShillServiceClient::AddServiceWithIPConfig(
319 const std::string
& service_path
,
320 const std::string
& guid
,
321 const std::string
& name
,
322 const std::string
& type
,
323 const std::string
& state
,
324 const std::string
& ipconfig_path
,
326 base::DictionaryValue
* properties
= SetServiceProperties(
327 service_path
, guid
, name
, type
, state
, visible
);
329 std::string profile_path
;
330 if (properties
->GetStringWithoutPathExpansion(shill::kProfileProperty
,
332 !profile_path
.empty()) {
333 DBusThreadManager::Get()->GetShillProfileClient()->GetTestInterface()->
334 UpdateService(profile_path
, service_path
);
337 if (!ipconfig_path
.empty()) {
338 properties
->SetWithoutPathExpansion(
339 shill::kIPConfigProperty
,
340 new base::StringValue(ipconfig_path
));
343 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
344 AddManagerService(service_path
, true);
347 base::DictionaryValue
* FakeShillServiceClient::SetServiceProperties(
348 const std::string
& service_path
,
349 const std::string
& guid
,
350 const std::string
& name
,
351 const std::string
& type
,
352 const std::string
& state
,
354 base::DictionaryValue
* properties
=
355 GetModifiableServiceProperties(service_path
, true);
356 connect_behavior_
.erase(service_path
);
358 std::string profile_path
;
359 base::DictionaryValue profile_properties
;
360 if (DBusThreadManager::Get()
361 ->GetShillProfileClient()
363 ->GetService(service_path
, &profile_path
, &profile_properties
)) {
364 properties
->SetStringWithoutPathExpansion(shill::kProfileProperty
,
368 // If |guid| is provided, set Service.GUID to that. Otherwise if a GUID is
369 // stored in a profile entry, use that. Otherwise leave it blank. Shill does
370 // not enforce a valid guid, we do that at the NetworkStateHandler layer.
371 std::string guid_to_set
= guid
;
372 if (guid_to_set
.empty()) {
373 profile_properties
.GetStringWithoutPathExpansion(shill::kGuidProperty
,
376 if (!guid_to_set
.empty()) {
377 properties
->SetStringWithoutPathExpansion(shill::kGuidProperty
,
380 properties
->SetStringWithoutPathExpansion(shill::kSSIDProperty
, name
);
381 shill_property_util::SetSSID(name
, properties
); // Sets kWifiHexSsid
382 properties
->SetStringWithoutPathExpansion(shill::kNameProperty
, name
);
383 std::string device_path
= DBusThreadManager::Get()
384 ->GetShillDeviceClient()
386 ->GetDevicePathForType(type
);
387 properties
->SetStringWithoutPathExpansion(shill::kDeviceProperty
,
389 properties
->SetStringWithoutPathExpansion(shill::kTypeProperty
, type
);
390 properties
->SetStringWithoutPathExpansion(shill::kStateProperty
, state
);
391 properties
->SetBooleanWithoutPathExpansion(shill::kVisibleProperty
, visible
);
392 if (type
== shill::kTypeWifi
) {
393 properties
->SetStringWithoutPathExpansion(shill::kSecurityClassProperty
,
394 shill::kSecurityNone
);
395 properties
->SetStringWithoutPathExpansion(shill::kModeProperty
,
396 shill::kModeManaged
);
401 void FakeShillServiceClient::RemoveService(const std::string
& service_path
) {
402 stub_services_
.RemoveWithoutPathExpansion(service_path
, NULL
);
403 connect_behavior_
.erase(service_path
);
404 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
405 RemoveManagerService(service_path
);
408 bool FakeShillServiceClient::SetServiceProperty(const std::string
& service_path
,
409 const std::string
& property
,
410 const base::Value
& value
) {
411 base::DictionaryValue
* dict
= NULL
;
412 if (!stub_services_
.GetDictionaryWithoutPathExpansion(service_path
, &dict
))
415 VLOG(1) << "Service.SetProperty: " << property
<< " = " << value
416 << " For: " << service_path
;
418 base::DictionaryValue new_properties
;
419 std::string changed_property
;
420 bool case_sensitive
= true;
421 if (StartsWithASCII(property
, "Provider.", case_sensitive
) ||
422 StartsWithASCII(property
, "OpenVPN.", case_sensitive
) ||
423 StartsWithASCII(property
, "L2TPIPsec.", case_sensitive
)) {
424 // These properties are only nested within the Provider dictionary if read
425 // from Shill. Properties that start with "Provider" need to have that
426 // stripped off, other properties are nested in the "Provider" dictionary
428 std::string key
= property
;
429 if (StartsWithASCII(property
, "Provider.", case_sensitive
))
430 key
= property
.substr(strlen("Provider."));
431 base::DictionaryValue
* provider
= new base::DictionaryValue
;
432 provider
->SetWithoutPathExpansion(key
, value
.DeepCopy());
433 new_properties
.SetWithoutPathExpansion(shill::kProviderProperty
, provider
);
434 changed_property
= shill::kProviderProperty
;
435 } else if (value
.GetType() == base::Value::TYPE_DICTIONARY
) {
436 const base::DictionaryValue
* new_dict
= NULL
;
437 value
.GetAsDictionary(&new_dict
);
439 scoped_ptr
<base::Value
> cur_value
;
440 base::DictionaryValue
* cur_dict
;
441 if (dict
->RemoveWithoutPathExpansion(property
, &cur_value
) &&
442 cur_value
->GetAsDictionary(&cur_dict
)) {
444 cur_dict
->MergeDictionary(new_dict
);
445 new_properties
.SetWithoutPathExpansion(property
, cur_value
.release());
447 new_properties
.SetWithoutPathExpansion(property
, value
.DeepCopy());
449 changed_property
= property
;
451 new_properties
.SetWithoutPathExpansion(property
, value
.DeepCopy());
452 changed_property
= property
;
455 dict
->MergeDictionary(&new_properties
);
457 // Add or update the profile entry.
458 ShillProfileClient::TestInterface
* profile_test
=
459 DBusThreadManager::Get()->GetShillProfileClient()->GetTestInterface();
460 if (property
== shill::kProfileProperty
) {
461 std::string profile_path
;
462 if (value
.GetAsString(&profile_path
)) {
463 if (!profile_path
.empty())
464 profile_test
->AddService(profile_path
, service_path
);
466 LOG(ERROR
) << "Profile value is not a String!";
469 std::string profile_path
;
470 if (dict
->GetStringWithoutPathExpansion(
471 shill::kProfileProperty
, &profile_path
) && !profile_path
.empty()) {
472 profile_test
->UpdateService(profile_path
, service_path
);
476 // Notify the Manager if the state changed (affects DefaultService).
477 if (property
== shill::kStateProperty
) {
479 value
.GetAsString(&state
);
480 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
481 ServiceStateChanged(service_path
, state
);
484 // If the State or Visibility changes, the sort order of service lists may
485 // change and the DefaultService property may change.
486 if (property
== shill::kStateProperty
||
487 property
== shill::kVisibleProperty
) {
488 base::MessageLoop::current()->PostTask(
489 FROM_HERE
, base::Bind(&CallSortManagerServices
));
492 // Notifiy Chrome of the property change.
493 base::MessageLoop::current()->PostTask(
495 base::Bind(&FakeShillServiceClient::NotifyObserversPropertyChanged
,
496 weak_ptr_factory_
.GetWeakPtr(),
497 dbus::ObjectPath(service_path
), changed_property
));
501 const base::DictionaryValue
* FakeShillServiceClient::GetServiceProperties(
502 const std::string
& service_path
) const {
503 const base::DictionaryValue
* properties
= NULL
;
504 stub_services_
.GetDictionaryWithoutPathExpansion(service_path
, &properties
);
508 void FakeShillServiceClient::ClearServices() {
509 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
510 ClearManagerServices();
512 stub_services_
.Clear();
513 connect_behavior_
.clear();
516 void FakeShillServiceClient::SetConnectBehavior(const std::string
& service_path
,
517 const base::Closure
& behavior
) {
518 connect_behavior_
[service_path
] = behavior
;
521 void FakeShillServiceClient::NotifyObserversPropertyChanged(
522 const dbus::ObjectPath
& service_path
,
523 const std::string
& property
) {
524 base::DictionaryValue
* dict
= NULL
;
525 std::string path
= service_path
.value();
526 if (!stub_services_
.GetDictionaryWithoutPathExpansion(path
, &dict
)) {
527 LOG(ERROR
) << "Notify for unknown service: " << path
;
530 base::Value
* value
= NULL
;
531 if (!dict
->GetWithoutPathExpansion(property
, &value
)) {
532 LOG(ERROR
) << "Notify for unknown property: "
533 << path
<< " : " << property
;
536 FOR_EACH_OBSERVER(ShillPropertyChangedObserver
,
537 GetObserverList(service_path
),
538 OnPropertyChanged(property
, *value
));
541 base::DictionaryValue
* FakeShillServiceClient::GetModifiableServiceProperties(
542 const std::string
& service_path
, bool create_if_missing
) {
543 base::DictionaryValue
* properties
= NULL
;
544 if (!stub_services_
.GetDictionaryWithoutPathExpansion(service_path
,
547 properties
= new base::DictionaryValue
;
548 stub_services_
.Set(service_path
, properties
);
553 FakeShillServiceClient::PropertyObserverList
&
554 FakeShillServiceClient::GetObserverList(const dbus::ObjectPath
& device_path
) {
555 std::map
<dbus::ObjectPath
, PropertyObserverList
*>::iterator iter
=
556 observer_list_
.find(device_path
);
557 if (iter
!= observer_list_
.end())
558 return *(iter
->second
);
559 PropertyObserverList
* observer_list
= new PropertyObserverList();
560 observer_list_
[device_path
] = observer_list
;
561 return *observer_list
;
564 void FakeShillServiceClient::SetOtherServicesOffline(
565 const std::string
& service_path
) {
566 const base::DictionaryValue
* service_properties
= GetServiceProperties(
568 if (!service_properties
) {
569 LOG(ERROR
) << "Missing service: " << service_path
;
572 std::string service_type
;
573 service_properties
->GetString(shill::kTypeProperty
, &service_type
);
574 // Set all other services of the same type to offline (Idle).
575 for (base::DictionaryValue::Iterator
iter(stub_services_
);
576 !iter
.IsAtEnd(); iter
.Advance()) {
577 std::string path
= iter
.key();
578 if (path
== service_path
)
580 base::DictionaryValue
* properties
;
581 if (!stub_services_
.GetDictionaryWithoutPathExpansion(path
, &properties
))
585 properties
->GetString(shill::kTypeProperty
, &type
);
586 if (type
!= service_type
)
588 properties
->SetWithoutPathExpansion(
589 shill::kStateProperty
,
590 new base::StringValue(shill::kStateIdle
));
594 void FakeShillServiceClient::SetCellularActivated(
595 const dbus::ObjectPath
& service_path
,
596 const ErrorCallback
& error_callback
) {
597 SetProperty(service_path
,
598 shill::kActivationStateProperty
,
599 base::StringValue(shill::kActivationStateActivated
),
600 base::Bind(&base::DoNothing
),
602 SetProperty(service_path
,
603 shill::kConnectableProperty
,
604 base::FundamentalValue(true),
605 base::Bind(&base::DoNothing
),
609 void FakeShillServiceClient::ContinueConnect(
610 const std::string
& service_path
) {
611 VLOG(1) << "FakeShillServiceClient::ContinueConnect: " << service_path
;
612 base::DictionaryValue
* service_properties
= NULL
;
613 if (!stub_services_
.GetDictionary(service_path
, &service_properties
)) {
614 LOG(ERROR
) << "Service not found: " << service_path
;
618 if (ContainsKey(connect_behavior_
, service_path
)) {
619 const base::Closure
& custom_connect_behavior
=
620 connect_behavior_
[service_path
];
621 VLOG(1) << "Running custom connect behavior for " << service_path
;
622 custom_connect_behavior
.Run();
626 // No custom connect behavior set, continue with the default connect behavior.
627 std::string passphrase
;
628 service_properties
->GetStringWithoutPathExpansion(
629 shill::kPassphraseProperty
, &passphrase
);
630 if (passphrase
== "failure") {
631 // Simulate a password failure.
632 SetServiceProperty(service_path
,
633 shill::kStateProperty
,
634 base::StringValue(shill::kStateFailure
));
635 base::MessageLoop::current()->PostTask(
638 base::IgnoreResult(&FakeShillServiceClient::SetServiceProperty
),
639 weak_ptr_factory_
.GetWeakPtr(),
641 shill::kErrorProperty
,
642 base::StringValue(shill::kErrorBadPassphrase
)));
645 VLOG(1) << "Setting state to Online " << service_path
;
646 SetServiceProperty(service_path
,
647 shill::kStateProperty
,
648 base::StringValue(shill::kStateOnline
));
652 } // namespace chromeos