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"
14 #include "chromeos/network/shill_property_util.h"
16 #include "dbus/message.h"
17 #include "dbus/object_path.h"
18 #include "dbus/object_proxy.h"
19 #include "dbus/values_util.h"
20 #include "third_party/cros_system_api/dbus/service_constants.h"
26 void ErrorFunction(const std::string& device_path,
27 const std::string& error_name,
28 const std::string& error_message) {
29 LOG(ERROR) << "Shill Error for: " << device_path
30 << ": " << error_name << " : " << error_message;
33 void PostDeviceNotFoundError(
34 const ShillDeviceClient::ErrorCallback& error_callback) {
35 std::string error_name("org.chromium.flimflam.Error.Failure");
36 std::string error_message("Failed");
37 base::MessageLoop::current()->PostTask(
38 FROM_HERE, base::Bind(error_callback, error_name, error_message));
43 FakeShillDeviceClient::FakeShillDeviceClient() : weak_ptr_factory_(this) {
46 FakeShillDeviceClient::~FakeShillDeviceClient() {
47 STLDeleteContainerPairSecondPointers(
48 observer_list_.begin(), observer_list_.end());
51 // ShillDeviceClient overrides.
53 void FakeShillDeviceClient::Init(dbus::Bus* bus) {}
55 void FakeShillDeviceClient::AddPropertyChangedObserver(
56 const dbus::ObjectPath& device_path,
57 ShillPropertyChangedObserver* observer) {
58 GetObserverList(device_path).AddObserver(observer);
61 void FakeShillDeviceClient::RemovePropertyChangedObserver(
62 const dbus::ObjectPath& device_path,
63 ShillPropertyChangedObserver* observer) {
64 GetObserverList(device_path).RemoveObserver(observer);
67 void FakeShillDeviceClient::GetProperties(
68 const dbus::ObjectPath& device_path,
69 const DictionaryValueCallback& callback) {
70 base::MessageLoop::current()->PostTask(
72 base::Bind(&FakeShillDeviceClient::PassStubDeviceProperties,
73 weak_ptr_factory_.GetWeakPtr(),
74 device_path, callback));
77 void FakeShillDeviceClient::ProposeScan(
78 const dbus::ObjectPath& device_path,
79 const VoidDBusMethodCallback& callback) {
80 PostVoidCallback(callback, DBUS_METHOD_CALL_SUCCESS);
83 void FakeShillDeviceClient::SetProperty(const dbus::ObjectPath& device_path,
84 const std::string& name,
85 const base::Value& value,
86 const base::Closure& callback,
87 const ErrorCallback& error_callback) {
88 base::DictionaryValue* device_properties = NULL;
89 if (!stub_devices_.GetDictionaryWithoutPathExpansion(device_path.value(),
90 &device_properties)) {
91 PostDeviceNotFoundError(error_callback);
94 device_properties->SetWithoutPathExpansion(name, value.DeepCopy());
95 base::MessageLoop::current()->PostTask(
97 base::Bind(&FakeShillDeviceClient::NotifyObserversPropertyChanged,
98 weak_ptr_factory_.GetWeakPtr(), device_path, name));
99 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
102 void FakeShillDeviceClient::ClearProperty(
103 const dbus::ObjectPath& device_path,
104 const std::string& name,
105 const VoidDBusMethodCallback& callback) {
106 base::DictionaryValue* device_properties = NULL;
107 if (!stub_devices_.GetDictionaryWithoutPathExpansion(device_path.value(),
108 &device_properties)) {
109 PostVoidCallback(callback, DBUS_METHOD_CALL_FAILURE);
112 device_properties->RemoveWithoutPathExpansion(name, NULL);
113 PostVoidCallback(callback, DBUS_METHOD_CALL_SUCCESS);
116 void FakeShillDeviceClient::AddIPConfig(
117 const dbus::ObjectPath& device_path,
118 const std::string& method,
119 const ObjectPathDBusMethodCallback& callback) {
120 base::MessageLoop::current()->PostTask(FROM_HERE,
122 DBUS_METHOD_CALL_SUCCESS,
123 dbus::ObjectPath()));
126 void FakeShillDeviceClient::RequirePin(const dbus::ObjectPath& device_path,
127 const std::string& pin,
129 const base::Closure& callback,
130 const ErrorCallback& error_callback) {
131 if (!stub_devices_.HasKey(device_path.value())) {
132 PostDeviceNotFoundError(error_callback);
135 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
138 void FakeShillDeviceClient::EnterPin(const dbus::ObjectPath& device_path,
139 const std::string& pin,
140 const base::Closure& callback,
141 const ErrorCallback& error_callback) {
142 if (!stub_devices_.HasKey(device_path.value())) {
143 PostDeviceNotFoundError(error_callback);
146 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
149 void FakeShillDeviceClient::UnblockPin(const dbus::ObjectPath& device_path,
150 const std::string& puk,
151 const std::string& pin,
152 const base::Closure& callback,
153 const ErrorCallback& error_callback) {
154 if (!stub_devices_.HasKey(device_path.value())) {
155 PostDeviceNotFoundError(error_callback);
158 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
161 void FakeShillDeviceClient::ChangePin(const dbus::ObjectPath& device_path,
162 const std::string& old_pin,
163 const std::string& new_pin,
164 const base::Closure& callback,
165 const ErrorCallback& error_callback) {
166 if (!stub_devices_.HasKey(device_path.value())) {
167 PostDeviceNotFoundError(error_callback);
170 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
173 void FakeShillDeviceClient::Register(const dbus::ObjectPath& device_path,
174 const std::string& network_id,
175 const base::Closure& callback,
176 const ErrorCallback& error_callback) {
177 if (!stub_devices_.HasKey(device_path.value())) {
178 PostDeviceNotFoundError(error_callback);
181 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
184 void FakeShillDeviceClient::SetCarrier(const dbus::ObjectPath& device_path,
185 const std::string& carrier,
186 const base::Closure& callback,
187 const ErrorCallback& error_callback) {
188 if (!stub_devices_.HasKey(device_path.value())) {
189 PostDeviceNotFoundError(error_callback);
192 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
195 void FakeShillDeviceClient::Reset(const dbus::ObjectPath& device_path,
196 const base::Closure& callback,
197 const ErrorCallback& error_callback) {
198 if (!stub_devices_.HasKey(device_path.value())) {
199 PostDeviceNotFoundError(error_callback);
202 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
205 ShillDeviceClient::TestInterface* FakeShillDeviceClient::GetTestInterface() {
209 // ShillDeviceClient::TestInterface overrides.
211 void FakeShillDeviceClient::AddDevice(const std::string& device_path,
212 const std::string& type,
213 const std::string& object_path) {
214 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
215 AddDevice(device_path);
217 base::DictionaryValue* properties = GetDeviceProperties(device_path);
218 properties->SetWithoutPathExpansion(shill::kTypeProperty,
219 base::Value::CreateStringValue(type));
220 properties->SetWithoutPathExpansion(
221 shill::kDBusObjectProperty, base::Value::CreateStringValue(object_path));
222 properties->SetWithoutPathExpansion(
223 shill::kDBusServiceProperty,
224 base::Value::CreateStringValue(modemmanager::kModemManager1));
225 if (NetworkTypePattern::Cellular().MatchesType(type)) {
226 properties->SetWithoutPathExpansion(shill::kCellularAllowRoamingProperty,
227 new base::FundamentalValue(false));
231 void FakeShillDeviceClient::RemoveDevice(const std::string& device_path) {
232 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
233 RemoveDevice(device_path);
235 stub_devices_.RemoveWithoutPathExpansion(device_path, NULL);
238 void FakeShillDeviceClient::ClearDevices() {
239 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
242 stub_devices_.Clear();
245 void FakeShillDeviceClient::SetDeviceProperty(const std::string& device_path,
246 const std::string& name,
247 const base::Value& value) {
248 VLOG(1) << "SetDeviceProperty: " << device_path
249 << ": " << name << " = " << value;
250 SetProperty(dbus::ObjectPath(device_path), name, value,
251 base::Bind(&base::DoNothing),
252 base::Bind(&ErrorFunction, device_path));
255 std::string FakeShillDeviceClient::GetDevicePathForType(
256 const std::string& type) {
257 for (base::DictionaryValue::Iterator iter(stub_devices_);
258 !iter.IsAtEnd(); iter.Advance()) {
259 const base::DictionaryValue* properties = NULL;
260 if (!iter.value().GetAsDictionary(&properties))
262 std::string prop_type;
263 if (!properties->GetStringWithoutPathExpansion(
264 shill::kTypeProperty, &prop_type) ||
269 return std::string();
272 void FakeShillDeviceClient::PassStubDeviceProperties(
273 const dbus::ObjectPath& device_path,
274 const DictionaryValueCallback& callback) const {
275 const base::DictionaryValue* device_properties = NULL;
276 if (!stub_devices_.GetDictionaryWithoutPathExpansion(
277 device_path.value(), &device_properties)) {
278 base::DictionaryValue empty_dictionary;
279 callback.Run(DBUS_METHOD_CALL_FAILURE, empty_dictionary);
282 callback.Run(DBUS_METHOD_CALL_SUCCESS, *device_properties);
285 // Posts a task to run a void callback with status code |status|.
286 void FakeShillDeviceClient::PostVoidCallback(
287 const VoidDBusMethodCallback& callback,
288 DBusMethodCallStatus status) {
289 base::MessageLoop::current()->PostTask(FROM_HERE,
290 base::Bind(callback, status));
293 void FakeShillDeviceClient::NotifyObserversPropertyChanged(
294 const dbus::ObjectPath& device_path,
295 const std::string& property) {
296 base::DictionaryValue* dict = NULL;
297 std::string path = device_path.value();
298 if (!stub_devices_.GetDictionaryWithoutPathExpansion(path, &dict)) {
299 LOG(ERROR) << "Notify for unknown service: " << path;
302 base::Value* value = NULL;
303 if (!dict->GetWithoutPathExpansion(property, &value)) {
304 LOG(ERROR) << "Notify for unknown property: "
305 << path << " : " << property;
308 FOR_EACH_OBSERVER(ShillPropertyChangedObserver,
309 GetObserverList(device_path),
310 OnPropertyChanged(property, *value));
313 base::DictionaryValue* FakeShillDeviceClient::GetDeviceProperties(
314 const std::string& device_path) {
315 base::DictionaryValue* properties = NULL;
316 if (!stub_devices_.GetDictionaryWithoutPathExpansion(
317 device_path, &properties)) {
318 properties = new base::DictionaryValue;
319 stub_devices_.SetWithoutPathExpansion(device_path, properties);
324 FakeShillDeviceClient::PropertyObserverList&
325 FakeShillDeviceClient::GetObserverList(const dbus::ObjectPath& device_path) {
326 std::map<dbus::ObjectPath, PropertyObserverList*>::iterator iter =
327 observer_list_.find(device_path);
328 if (iter != observer_list_.end())
329 return *(iter->second);
330 PropertyObserverList* observer_list = new PropertyObserverList();
331 observer_list_[device_path] = observer_list;
332 return *observer_list;
335 } // namespace chromeos