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_device_client.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/stl_util.h"
10 #include "base/values.h"
11 #include "chromeos/dbus/dbus_thread_manager.h"
12 #include "chromeos/dbus/shill_manager_client.h"
13 #include "chromeos/dbus/shill_property_changed_observer.h"
15 #include "dbus/message.h"
16 #include "dbus/object_path.h"
17 #include "dbus/object_proxy.h"
18 #include "dbus/values_util.h"
19 #include "net/base/ip_endpoint.h"
20 #include "third_party/cros_system_api/dbus/service_constants.h"
26 std::string kSimPin
= "1111";
28 void ErrorFunction(const std::string
& device_path
,
29 const std::string
& error_name
,
30 const std::string
& error_message
) {
31 LOG(ERROR
) << "Shill Error for: " << device_path
32 << ": " << error_name
<< " : " << error_message
;
35 void PostNotFoundError(
36 const ShillDeviceClient::ErrorCallback
& error_callback
) {
37 std::string
error_message("Failed");
38 base::MessageLoop::current()->PostTask(
40 base::Bind(error_callback
, shill::kErrorResultNotFound
, error_message
));
45 FakeShillDeviceClient::FakeShillDeviceClient()
46 : tdls_busy_count_(0),
47 weak_ptr_factory_(this) {
50 FakeShillDeviceClient::~FakeShillDeviceClient() {
51 STLDeleteContainerPairSecondPointers(
52 observer_list_
.begin(), observer_list_
.end());
55 // ShillDeviceClient overrides.
57 void FakeShillDeviceClient::Init(dbus::Bus
* bus
) {}
59 void FakeShillDeviceClient::AddPropertyChangedObserver(
60 const dbus::ObjectPath
& device_path
,
61 ShillPropertyChangedObserver
* observer
) {
62 GetObserverList(device_path
).AddObserver(observer
);
65 void FakeShillDeviceClient::RemovePropertyChangedObserver(
66 const dbus::ObjectPath
& device_path
,
67 ShillPropertyChangedObserver
* observer
) {
68 GetObserverList(device_path
).RemoveObserver(observer
);
71 void FakeShillDeviceClient::GetProperties(
72 const dbus::ObjectPath
& device_path
,
73 const DictionaryValueCallback
& callback
) {
74 base::MessageLoop::current()->PostTask(
76 base::Bind(&FakeShillDeviceClient::PassStubDeviceProperties
,
77 weak_ptr_factory_
.GetWeakPtr(),
78 device_path
, callback
));
81 void FakeShillDeviceClient::ProposeScan(
82 const dbus::ObjectPath
& device_path
,
83 const VoidDBusMethodCallback
& callback
) {
84 PostVoidCallback(callback
, DBUS_METHOD_CALL_SUCCESS
);
87 void FakeShillDeviceClient::SetProperty(const dbus::ObjectPath
& device_path
,
88 const std::string
& name
,
89 const base::Value
& value
,
90 const base::Closure
& callback
,
91 const ErrorCallback
& error_callback
) {
92 base::DictionaryValue
* device_properties
= NULL
;
93 if (!stub_devices_
.GetDictionaryWithoutPathExpansion(device_path
.value(),
94 &device_properties
)) {
95 PostNotFoundError(error_callback
);
98 device_properties
->SetWithoutPathExpansion(name
, value
.DeepCopy());
99 base::MessageLoop::current()->PostTask(
101 base::Bind(&FakeShillDeviceClient::NotifyObserversPropertyChanged
,
102 weak_ptr_factory_
.GetWeakPtr(), device_path
, name
));
103 base::MessageLoop::current()->PostTask(FROM_HERE
, callback
);
106 void FakeShillDeviceClient::ClearProperty(
107 const dbus::ObjectPath
& device_path
,
108 const std::string
& name
,
109 const VoidDBusMethodCallback
& callback
) {
110 base::DictionaryValue
* device_properties
= NULL
;
111 if (!stub_devices_
.GetDictionaryWithoutPathExpansion(device_path
.value(),
112 &device_properties
)) {
113 PostVoidCallback(callback
, DBUS_METHOD_CALL_FAILURE
);
116 device_properties
->RemoveWithoutPathExpansion(name
, NULL
);
117 PostVoidCallback(callback
, DBUS_METHOD_CALL_SUCCESS
);
120 void FakeShillDeviceClient::AddIPConfig(
121 const dbus::ObjectPath
& device_path
,
122 const std::string
& method
,
123 const ObjectPathDBusMethodCallback
& callback
) {
124 base::MessageLoop::current()->PostTask(FROM_HERE
,
126 DBUS_METHOD_CALL_SUCCESS
,
127 dbus::ObjectPath()));
130 void FakeShillDeviceClient::RequirePin(const dbus::ObjectPath
& device_path
,
131 const std::string
& pin
,
133 const base::Closure
& callback
,
134 const ErrorCallback
& error_callback
) {
135 VLOG(1) << "RequirePin: " << device_path
.value();
136 if (pin
!= kSimPin
) {
137 base::MessageLoop::current()->PostTask(
139 base::Bind(error_callback
, shill::kErrorResultIncorrectPin
, ""));
142 base::DictionaryValue
* device_properties
= NULL
;
143 if (!stub_devices_
.GetDictionaryWithoutPathExpansion(device_path
.value(),
144 &device_properties
)) {
145 PostNotFoundError(error_callback
);
148 base::DictionaryValue
* simlock_dict
= NULL
;
149 if (!device_properties
->GetDictionaryWithoutPathExpansion(
150 shill::kSIMLockStatusProperty
, &simlock_dict
)) {
151 simlock_dict
= new base::DictionaryValue
;
152 device_properties
->SetWithoutPathExpansion(
153 shill::kSIMLockStatusProperty
, simlock_dict
);
155 simlock_dict
->Clear();
156 simlock_dict
->SetBoolean(shill::kSIMLockEnabledProperty
, require
);
157 // TODO(stevenjb): Investigate why non-empty value breaks UI.
158 std::string lock_type
= ""; // shill::kSIMLockPin
159 simlock_dict
->SetString(shill::kSIMLockTypeProperty
, lock_type
);
160 simlock_dict
->SetInteger(shill::kSIMLockRetriesLeftProperty
, 5);
162 NotifyObserversPropertyChanged(device_path
, shill::kSIMLockStatusProperty
);
163 base::MessageLoop::current()->PostTask(FROM_HERE
, callback
);
166 void FakeShillDeviceClient::EnterPin(const dbus::ObjectPath
& device_path
,
167 const std::string
& pin
,
168 const base::Closure
& callback
,
169 const ErrorCallback
& error_callback
) {
170 VLOG(1) << "EnterPin: " << device_path
.value();
171 if (pin
!= kSimPin
) {
172 base::MessageLoop::current()->PostTask(
174 base::Bind(error_callback
, shill::kErrorResultIncorrectPin
, ""));
177 if (!stub_devices_
.HasKey(device_path
.value())) {
178 PostNotFoundError(error_callback
);
181 base::MessageLoop::current()->PostTask(FROM_HERE
, callback
);
184 void FakeShillDeviceClient::UnblockPin(const dbus::ObjectPath
& device_path
,
185 const std::string
& puk
,
186 const std::string
& pin
,
187 const base::Closure
& callback
,
188 const ErrorCallback
& error_callback
) {
189 VLOG(1) << "UnblockPin: " << device_path
.value();
190 if (!stub_devices_
.HasKey(device_path
.value())) {
191 PostNotFoundError(error_callback
);
194 base::MessageLoop::current()->PostTask(FROM_HERE
, callback
);
197 void FakeShillDeviceClient::ChangePin(const dbus::ObjectPath
& device_path
,
198 const std::string
& old_pin
,
199 const std::string
& new_pin
,
200 const base::Closure
& callback
,
201 const ErrorCallback
& error_callback
) {
202 VLOG(1) << "ChangePin: " << device_path
.value();
203 if (!stub_devices_
.HasKey(device_path
.value())) {
204 PostNotFoundError(error_callback
);
207 base::MessageLoop::current()->PostTask(FROM_HERE
, callback
);
210 void FakeShillDeviceClient::Register(const dbus::ObjectPath
& device_path
,
211 const std::string
& network_id
,
212 const base::Closure
& callback
,
213 const ErrorCallback
& error_callback
) {
214 if (!stub_devices_
.HasKey(device_path
.value())) {
215 PostNotFoundError(error_callback
);
218 base::MessageLoop::current()->PostTask(FROM_HERE
, callback
);
221 void FakeShillDeviceClient::SetCarrier(const dbus::ObjectPath
& device_path
,
222 const std::string
& carrier
,
223 const base::Closure
& callback
,
224 const ErrorCallback
& error_callback
) {
225 if (!stub_devices_
.HasKey(device_path
.value())) {
226 PostNotFoundError(error_callback
);
229 base::MessageLoop::current()->PostTask(FROM_HERE
, callback
);
232 void FakeShillDeviceClient::Reset(const dbus::ObjectPath
& device_path
,
233 const base::Closure
& callback
,
234 const ErrorCallback
& error_callback
) {
235 if (!stub_devices_
.HasKey(device_path
.value())) {
236 PostNotFoundError(error_callback
);
239 base::MessageLoop::current()->PostTask(FROM_HERE
, callback
);
242 void FakeShillDeviceClient::PerformTDLSOperation(
243 const dbus::ObjectPath
& device_path
,
244 const std::string
& operation
,
245 const std::string
& peer
,
246 const StringCallback
& callback
,
247 const ErrorCallback
& error_callback
) {
248 if (!stub_devices_
.HasKey(device_path
.value())) {
249 PostNotFoundError(error_callback
);
252 if (tdls_busy_count_
) {
254 std::string
error_message("In-Progress");
255 base::MessageLoop::current()->PostTask(
257 base::Bind(error_callback
,
258 shill::kErrorResultInProgress
, error_message
));
262 if (operation
== shill::kTDLSStatusOperation
)
263 result
= shill::kTDLSConnectedState
;
264 base::MessageLoop::current()->PostTask(FROM_HERE
,
265 base::Bind(callback
, result
));
268 void FakeShillDeviceClient::AddWakeOnPacketConnection(
269 const dbus::ObjectPath
& device_path
,
270 const net::IPEndPoint
& ip_endpoint
,
271 const base::Closure
& callback
,
272 const ErrorCallback
& error_callback
) {
273 if (!stub_devices_
.HasKey(device_path
.value())) {
274 PostNotFoundError(error_callback
);
278 wake_on_packet_connections_
[device_path
].insert(ip_endpoint
);
280 base::MessageLoop::current()->PostTask(FROM_HERE
, callback
);
283 void FakeShillDeviceClient::RemoveWakeOnPacketConnection(
284 const dbus::ObjectPath
& device_path
,
285 const net::IPEndPoint
& ip_endpoint
,
286 const base::Closure
& callback
,
287 const ErrorCallback
& error_callback
) {
288 const auto device_iter
= wake_on_packet_connections_
.find(device_path
);
289 if (!stub_devices_
.HasKey(device_path
.value()) ||
290 device_iter
== wake_on_packet_connections_
.end()) {
291 PostNotFoundError(error_callback
);
295 const auto endpoint_iter
= device_iter
->second
.find(ip_endpoint
);
296 if (endpoint_iter
== device_iter
->second
.end()) {
297 PostNotFoundError(error_callback
);
301 device_iter
->second
.erase(endpoint_iter
);
303 base::MessageLoop::current()->PostTask(FROM_HERE
, callback
);
306 void FakeShillDeviceClient::RemoveAllWakeOnPacketConnections(
307 const dbus::ObjectPath
& device_path
,
308 const base::Closure
& callback
,
309 const ErrorCallback
& error_callback
) {
310 const auto iter
= wake_on_packet_connections_
.find(device_path
);
311 if (!stub_devices_
.HasKey(device_path
.value()) ||
312 iter
== wake_on_packet_connections_
.end()) {
313 PostNotFoundError(error_callback
);
317 wake_on_packet_connections_
.erase(iter
);
319 base::MessageLoop::current()->PostTask(FROM_HERE
, callback
);
322 ShillDeviceClient::TestInterface
* FakeShillDeviceClient::GetTestInterface() {
326 // ShillDeviceClient::TestInterface overrides.
328 void FakeShillDeviceClient::AddDevice(const std::string
& device_path
,
329 const std::string
& type
,
330 const std::string
& name
) {
331 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
332 AddDevice(device_path
);
334 base::DictionaryValue
* properties
= GetDeviceProperties(device_path
);
335 properties
->SetStringWithoutPathExpansion(shill::kTypeProperty
, type
);
336 properties
->SetStringWithoutPathExpansion(shill::kNameProperty
, name
);
337 properties
->SetStringWithoutPathExpansion(shill::kDBusObjectProperty
,
339 properties
->SetStringWithoutPathExpansion(
340 shill::kDBusServiceProperty
, modemmanager::kModemManager1ServiceName
);
341 if (type
== shill::kTypeCellular
) {
342 properties
->SetBooleanWithoutPathExpansion(
343 shill::kCellularAllowRoamingProperty
, false);
347 void FakeShillDeviceClient::RemoveDevice(const std::string
& device_path
) {
348 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
349 RemoveDevice(device_path
);
351 stub_devices_
.RemoveWithoutPathExpansion(device_path
, NULL
);
354 void FakeShillDeviceClient::ClearDevices() {
355 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
358 stub_devices_
.Clear();
361 void FakeShillDeviceClient::SetDeviceProperty(const std::string
& device_path
,
362 const std::string
& name
,
363 const base::Value
& value
) {
364 VLOG(1) << "SetDeviceProperty: " << device_path
365 << ": " << name
<< " = " << value
;
366 SetProperty(dbus::ObjectPath(device_path
), name
, value
,
367 base::Bind(&base::DoNothing
),
368 base::Bind(&ErrorFunction
, device_path
));
371 std::string
FakeShillDeviceClient::GetDevicePathForType(
372 const std::string
& type
) {
373 for (base::DictionaryValue::Iterator
iter(stub_devices_
);
374 !iter
.IsAtEnd(); iter
.Advance()) {
375 const base::DictionaryValue
* properties
= NULL
;
376 if (!iter
.value().GetAsDictionary(&properties
))
378 std::string prop_type
;
379 if (!properties
->GetStringWithoutPathExpansion(
380 shill::kTypeProperty
, &prop_type
) ||
385 return std::string();
388 void FakeShillDeviceClient::PassStubDeviceProperties(
389 const dbus::ObjectPath
& device_path
,
390 const DictionaryValueCallback
& callback
) const {
391 const base::DictionaryValue
* device_properties
= NULL
;
392 if (!stub_devices_
.GetDictionaryWithoutPathExpansion(
393 device_path
.value(), &device_properties
)) {
394 base::DictionaryValue empty_dictionary
;
395 callback
.Run(DBUS_METHOD_CALL_FAILURE
, empty_dictionary
);
398 callback
.Run(DBUS_METHOD_CALL_SUCCESS
, *device_properties
);
401 // Posts a task to run a void callback with status code |status|.
402 void FakeShillDeviceClient::PostVoidCallback(
403 const VoidDBusMethodCallback
& callback
,
404 DBusMethodCallStatus status
) {
405 base::MessageLoop::current()->PostTask(FROM_HERE
,
406 base::Bind(callback
, status
));
409 void FakeShillDeviceClient::NotifyObserversPropertyChanged(
410 const dbus::ObjectPath
& device_path
,
411 const std::string
& property
) {
412 base::DictionaryValue
* dict
= NULL
;
413 std::string path
= device_path
.value();
414 if (!stub_devices_
.GetDictionaryWithoutPathExpansion(path
, &dict
)) {
415 LOG(ERROR
) << "Notify for unknown service: " << path
;
418 base::Value
* value
= NULL
;
419 if (!dict
->GetWithoutPathExpansion(property
, &value
)) {
420 LOG(ERROR
) << "Notify for unknown property: "
421 << path
<< " : " << property
;
424 FOR_EACH_OBSERVER(ShillPropertyChangedObserver
,
425 GetObserverList(device_path
),
426 OnPropertyChanged(property
, *value
));
429 base::DictionaryValue
* FakeShillDeviceClient::GetDeviceProperties(
430 const std::string
& device_path
) {
431 base::DictionaryValue
* properties
= NULL
;
432 if (!stub_devices_
.GetDictionaryWithoutPathExpansion(
433 device_path
, &properties
)) {
434 properties
= new base::DictionaryValue
;
435 stub_devices_
.SetWithoutPathExpansion(device_path
, properties
);
440 FakeShillDeviceClient::PropertyObserverList
&
441 FakeShillDeviceClient::GetObserverList(const dbus::ObjectPath
& device_path
) {
442 std::map
<dbus::ObjectPath
, PropertyObserverList
*>::iterator iter
=
443 observer_list_
.find(device_path
);
444 if (iter
!= observer_list_
.end())
445 return *(iter
->second
);
446 PropertyObserverList
* observer_list
= new PropertyObserverList();
447 observer_list_
[device_path
] = observer_list
;
448 return *observer_list
;
451 } // namespace chromeos