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 "third_party/cros_system_api/dbus/service_constants.h"
25 void ErrorFunction(const std::string& device_path,
26 const std::string& error_name,
27 const std::string& error_message) {
28 LOG(ERROR) << "Shill Error for: " << device_path
29 << ": " << error_name << " : " << error_message;
34 FakeShillDeviceClient::FakeShillDeviceClient() : weak_ptr_factory_(this) {
37 FakeShillDeviceClient::~FakeShillDeviceClient() {
38 STLDeleteContainerPairSecondPointers(
39 observer_list_.begin(), observer_list_.end());
42 // ShillDeviceClient overrides.
44 void FakeShillDeviceClient::Init(dbus::Bus* bus) {}
46 void FakeShillDeviceClient::AddPropertyChangedObserver(
47 const dbus::ObjectPath& device_path,
48 ShillPropertyChangedObserver* observer) {
49 GetObserverList(device_path).AddObserver(observer);
52 void FakeShillDeviceClient::RemovePropertyChangedObserver(
53 const dbus::ObjectPath& device_path,
54 ShillPropertyChangedObserver* observer) {
55 GetObserverList(device_path).RemoveObserver(observer);
58 void FakeShillDeviceClient::GetProperties(
59 const dbus::ObjectPath& device_path,
60 const DictionaryValueCallback& callback) {
61 base::MessageLoop::current()->PostTask(
63 base::Bind(&FakeShillDeviceClient::PassStubDeviceProperties,
64 weak_ptr_factory_.GetWeakPtr(),
65 device_path, callback));
68 void FakeShillDeviceClient::ProposeScan(
69 const dbus::ObjectPath& device_path,
70 const VoidDBusMethodCallback& callback) {
71 PostVoidCallback(callback, DBUS_METHOD_CALL_SUCCESS);
74 void FakeShillDeviceClient::SetProperty(const dbus::ObjectPath& device_path,
75 const std::string& name,
76 const base::Value& value,
77 const base::Closure& callback,
78 const ErrorCallback& error_callback) {
79 base::DictionaryValue* device_properties = NULL;
80 if (!stub_devices_.GetDictionaryWithoutPathExpansion(device_path.value(),
81 &device_properties)) {
82 std::string error_name("org.chromium.flimflam.Error.Failure");
83 std::string error_message("Failed");
84 base::MessageLoop::current()->PostTask(FROM_HERE,
85 base::Bind(error_callback,
90 device_properties->SetWithoutPathExpansion(name, value.DeepCopy());
91 base::MessageLoop::current()->PostTask(
93 base::Bind(&FakeShillDeviceClient::NotifyObserversPropertyChanged,
94 weak_ptr_factory_.GetWeakPtr(), device_path, name));
95 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
98 void FakeShillDeviceClient::ClearProperty(
99 const dbus::ObjectPath& device_path,
100 const std::string& name,
101 const VoidDBusMethodCallback& callback) {
102 base::DictionaryValue* device_properties = NULL;
103 if (!stub_devices_.GetDictionaryWithoutPathExpansion(device_path.value(),
104 &device_properties)) {
105 PostVoidCallback(callback, DBUS_METHOD_CALL_FAILURE);
108 device_properties->RemoveWithoutPathExpansion(name, NULL);
109 PostVoidCallback(callback, DBUS_METHOD_CALL_SUCCESS);
112 void FakeShillDeviceClient::AddIPConfig(
113 const dbus::ObjectPath& device_path,
114 const std::string& method,
115 const ObjectPathDBusMethodCallback& callback) {
116 base::MessageLoop::current()->PostTask(FROM_HERE,
118 DBUS_METHOD_CALL_SUCCESS,
119 dbus::ObjectPath()));
122 void FakeShillDeviceClient::RequirePin(const dbus::ObjectPath& device_path,
123 const std::string& pin,
125 const base::Closure& callback,
126 const ErrorCallback& error_callback) {
127 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
130 void FakeShillDeviceClient::EnterPin(const dbus::ObjectPath& device_path,
131 const std::string& pin,
132 const base::Closure& callback,
133 const ErrorCallback& error_callback) {
134 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
137 void FakeShillDeviceClient::UnblockPin(const dbus::ObjectPath& device_path,
138 const std::string& puk,
139 const std::string& pin,
140 const base::Closure& callback,
141 const ErrorCallback& error_callback) {
142 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
145 void FakeShillDeviceClient::ChangePin(const dbus::ObjectPath& device_path,
146 const std::string& old_pin,
147 const std::string& new_pin,
148 const base::Closure& callback,
149 const ErrorCallback& error_callback) {
150 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
153 void FakeShillDeviceClient::Register(const dbus::ObjectPath& device_path,
154 const std::string& network_id,
155 const base::Closure& callback,
156 const ErrorCallback& error_callback) {
157 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
160 void FakeShillDeviceClient::SetCarrier(const dbus::ObjectPath& device_path,
161 const std::string& carrier,
162 const base::Closure& callback,
163 const ErrorCallback& error_callback) {
164 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
167 void FakeShillDeviceClient::Reset(const dbus::ObjectPath& device_path,
168 const base::Closure& callback,
169 const ErrorCallback& error_callback) {
170 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
173 ShillDeviceClient::TestInterface* FakeShillDeviceClient::GetTestInterface() {
177 // ShillDeviceClient::TestInterface overrides.
179 void FakeShillDeviceClient::AddDevice(const std::string& device_path,
180 const std::string& type,
181 const std::string& object_path) {
182 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
183 AddDevice(device_path);
185 base::DictionaryValue* properties = GetDeviceProperties(device_path);
186 properties->SetWithoutPathExpansion(
187 shill::kTypeProperty,
188 base::Value::CreateStringValue(type));
189 properties->SetWithoutPathExpansion(
190 shill::kDBusObjectProperty,
191 base::Value::CreateStringValue(object_path));
192 properties->SetWithoutPathExpansion(
193 shill::kDBusConnectionProperty,
194 base::Value::CreateStringValue("/stub"));
197 void FakeShillDeviceClient::RemoveDevice(const std::string& device_path) {
198 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
199 RemoveDevice(device_path);
201 stub_devices_.RemoveWithoutPathExpansion(device_path, NULL);
204 void FakeShillDeviceClient::ClearDevices() {
205 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
208 stub_devices_.Clear();
211 void FakeShillDeviceClient::SetDeviceProperty(const std::string& device_path,
212 const std::string& name,
213 const base::Value& value) {
214 VLOG(1) << "SetDeviceProperty: " << device_path
215 << ": " << name << " = " << value;
216 SetProperty(dbus::ObjectPath(device_path), name, value,
217 base::Bind(&base::DoNothing),
218 base::Bind(&ErrorFunction, device_path));
221 std::string FakeShillDeviceClient::GetDevicePathForType(
222 const std::string& type) {
223 for (base::DictionaryValue::Iterator iter(stub_devices_);
224 !iter.IsAtEnd(); iter.Advance()) {
225 const base::DictionaryValue* properties = NULL;
226 if (!iter.value().GetAsDictionary(&properties))
228 std::string prop_type;
229 if (!properties->GetStringWithoutPathExpansion(
230 shill::kTypeProperty, &prop_type) ||
235 return std::string();
238 void FakeShillDeviceClient::PassStubDeviceProperties(
239 const dbus::ObjectPath& device_path,
240 const DictionaryValueCallback& callback) const {
241 const base::DictionaryValue* device_properties = NULL;
242 if (!stub_devices_.GetDictionaryWithoutPathExpansion(
243 device_path.value(), &device_properties)) {
244 base::DictionaryValue empty_dictionary;
245 callback.Run(DBUS_METHOD_CALL_FAILURE, empty_dictionary);
248 callback.Run(DBUS_METHOD_CALL_SUCCESS, *device_properties);
251 // Posts a task to run a void callback with status code |status|.
252 void FakeShillDeviceClient::PostVoidCallback(
253 const VoidDBusMethodCallback& callback,
254 DBusMethodCallStatus status) {
255 base::MessageLoop::current()->PostTask(FROM_HERE,
256 base::Bind(callback, status));
259 void FakeShillDeviceClient::NotifyObserversPropertyChanged(
260 const dbus::ObjectPath& device_path,
261 const std::string& property) {
262 base::DictionaryValue* dict = NULL;
263 std::string path = device_path.value();
264 if (!stub_devices_.GetDictionaryWithoutPathExpansion(path, &dict)) {
265 LOG(ERROR) << "Notify for unknown service: " << path;
268 base::Value* value = NULL;
269 if (!dict->GetWithoutPathExpansion(property, &value)) {
270 LOG(ERROR) << "Notify for unknown property: "
271 << path << " : " << property;
274 FOR_EACH_OBSERVER(ShillPropertyChangedObserver,
275 GetObserverList(device_path),
276 OnPropertyChanged(property, *value));
279 base::DictionaryValue* FakeShillDeviceClient::GetDeviceProperties(
280 const std::string& device_path) {
281 base::DictionaryValue* properties = NULL;
282 if (!stub_devices_.GetDictionaryWithoutPathExpansion(
283 device_path, &properties)) {
284 properties = new base::DictionaryValue;
285 stub_devices_.SetWithoutPathExpansion(device_path, properties);
290 FakeShillDeviceClient::PropertyObserverList&
291 FakeShillDeviceClient::GetObserverList(const dbus::ObjectPath& device_path) {
292 std::map<dbus::ObjectPath, PropertyObserverList*>::iterator iter =
293 observer_list_.find(device_path);
294 if (iter != observer_list_.end())
295 return *(iter->second);
296 PropertyObserverList* observer_list = new PropertyObserverList();
297 observer_list_[device_path] = observer_list;
298 return *observer_list;
301 } // namespace chromeos