OLD | NEW |
| (Empty) |
1 // Copyright (c) 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. | |
4 | |
5 #include "chromeos/dbus/shill_device_client_stub.h" | |
6 | |
7 #include "base/bind.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 "dbus/bus.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" | |
20 | |
21 namespace chromeos { | |
22 | |
23 namespace { | |
24 | |
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; | |
30 } | |
31 | |
32 } // namespace | |
33 | |
34 ShillDeviceClientStub::ShillDeviceClientStub() : weak_ptr_factory_(this) { | |
35 } | |
36 | |
37 ShillDeviceClientStub::~ShillDeviceClientStub() { | |
38 STLDeleteContainerPairSecondPointers( | |
39 observer_list_.begin(), observer_list_.end()); | |
40 } | |
41 | |
42 // ShillDeviceClient overrides. | |
43 | |
44 void ShillDeviceClientStub::Init(dbus::Bus* bus) {} | |
45 | |
46 void ShillDeviceClientStub::AddPropertyChangedObserver( | |
47 const dbus::ObjectPath& device_path, | |
48 ShillPropertyChangedObserver* observer){ | |
49 GetObserverList(device_path).AddObserver(observer); | |
50 } | |
51 | |
52 void ShillDeviceClientStub::RemovePropertyChangedObserver( | |
53 const dbus::ObjectPath& device_path, | |
54 ShillPropertyChangedObserver* observer){ | |
55 GetObserverList(device_path).RemoveObserver(observer); | |
56 } | |
57 | |
58 void ShillDeviceClientStub::GetProperties( | |
59 const dbus::ObjectPath& device_path, | |
60 const DictionaryValueCallback& callback){ | |
61 base::MessageLoop::current()->PostTask( | |
62 FROM_HERE, | |
63 base::Bind(&ShillDeviceClientStub::PassStubDeviceProperties, | |
64 weak_ptr_factory_.GetWeakPtr(), | |
65 device_path, callback)); | |
66 } | |
67 | |
68 void ShillDeviceClientStub::ProposeScan(const dbus::ObjectPath& device_path, | |
69 const VoidDBusMethodCallback& callback){ | |
70 PostVoidCallback(callback, DBUS_METHOD_CALL_SUCCESS); | |
71 } | |
72 | |
73 void ShillDeviceClientStub::SetProperty(const dbus::ObjectPath& device_path, | |
74 const std::string& name, | |
75 const base::Value& value, | |
76 const base::Closure& callback, | |
77 const ErrorCallback& error_callback){ | |
78 base::DictionaryValue* device_properties = NULL; | |
79 if (!stub_devices_.GetDictionaryWithoutPathExpansion(device_path.value(), | |
80 &device_properties)) { | |
81 std::string error_name("org.chromium.flimflam.Error.Failure"); | |
82 std::string error_message("Failed"); | |
83 base::MessageLoop::current()->PostTask(FROM_HERE, | |
84 base::Bind(error_callback, | |
85 error_name, | |
86 error_message)); | |
87 return; | |
88 } | |
89 device_properties->SetWithoutPathExpansion(name, value.DeepCopy()); | |
90 base::MessageLoop::current()->PostTask( | |
91 FROM_HERE, | |
92 base::Bind(&ShillDeviceClientStub::NotifyObserversPropertyChanged, | |
93 weak_ptr_factory_.GetWeakPtr(), device_path, name)); | |
94 base::MessageLoop::current()->PostTask(FROM_HERE, callback); | |
95 } | |
96 | |
97 void ShillDeviceClientStub::ClearProperty( | |
98 const dbus::ObjectPath& device_path, | |
99 const std::string& name, | |
100 const VoidDBusMethodCallback& callback){ | |
101 base::DictionaryValue* device_properties = NULL; | |
102 if (!stub_devices_.GetDictionaryWithoutPathExpansion(device_path.value(), | |
103 &device_properties)) { | |
104 PostVoidCallback(callback, DBUS_METHOD_CALL_FAILURE); | |
105 return; | |
106 } | |
107 device_properties->RemoveWithoutPathExpansion(name, NULL); | |
108 PostVoidCallback(callback, DBUS_METHOD_CALL_SUCCESS); | |
109 } | |
110 | |
111 void ShillDeviceClientStub::AddIPConfig( | |
112 const dbus::ObjectPath& device_path, | |
113 const std::string& method, | |
114 const ObjectPathDBusMethodCallback& callback){ | |
115 base::MessageLoop::current()->PostTask(FROM_HERE, | |
116 base::Bind(callback, | |
117 DBUS_METHOD_CALL_SUCCESS, | |
118 dbus::ObjectPath())); | |
119 } | |
120 | |
121 void ShillDeviceClientStub::RequirePin(const dbus::ObjectPath& device_path, | |
122 const std::string& pin, | |
123 bool require, | |
124 const base::Closure& callback, | |
125 const ErrorCallback& error_callback){ | |
126 base::MessageLoop::current()->PostTask(FROM_HERE, callback); | |
127 } | |
128 | |
129 void ShillDeviceClientStub::EnterPin(const dbus::ObjectPath& device_path, | |
130 const std::string& pin, | |
131 const base::Closure& callback, | |
132 const ErrorCallback& error_callback){ | |
133 base::MessageLoop::current()->PostTask(FROM_HERE, callback); | |
134 } | |
135 | |
136 void ShillDeviceClientStub::UnblockPin(const dbus::ObjectPath& device_path, | |
137 const std::string& puk, | |
138 const std::string& pin, | |
139 const base::Closure& callback, | |
140 const ErrorCallback& error_callback){ | |
141 base::MessageLoop::current()->PostTask(FROM_HERE, callback); | |
142 } | |
143 | |
144 void ShillDeviceClientStub::ChangePin(const dbus::ObjectPath& device_path, | |
145 const std::string& old_pin, | |
146 const std::string& new_pin, | |
147 const base::Closure& callback, | |
148 const ErrorCallback& error_callback){ | |
149 base::MessageLoop::current()->PostTask(FROM_HERE, callback); | |
150 } | |
151 | |
152 void ShillDeviceClientStub::Register(const dbus::ObjectPath& device_path, | |
153 const std::string& network_id, | |
154 const base::Closure& callback, | |
155 const ErrorCallback& error_callback){ | |
156 base::MessageLoop::current()->PostTask(FROM_HERE, callback); | |
157 } | |
158 | |
159 void ShillDeviceClientStub::SetCarrier(const dbus::ObjectPath& device_path, | |
160 const std::string& carrier, | |
161 const base::Closure& callback, | |
162 const ErrorCallback& error_callback){ | |
163 base::MessageLoop::current()->PostTask(FROM_HERE, callback); | |
164 } | |
165 | |
166 void ShillDeviceClientStub::Reset(const dbus::ObjectPath& device_path, | |
167 const base::Closure& callback, | |
168 const ErrorCallback& error_callback){ | |
169 base::MessageLoop::current()->PostTask(FROM_HERE, callback); | |
170 } | |
171 | |
172 ShillDeviceClient::TestInterface* ShillDeviceClientStub::GetTestInterface(){ | |
173 return this; | |
174 } | |
175 | |
176 // ShillDeviceClient::TestInterface overrides. | |
177 | |
178 void ShillDeviceClientStub::AddDevice(const std::string& device_path, | |
179 const std::string& type, | |
180 const std::string& object_path){ | |
181 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()-> | |
182 AddDevice(device_path); | |
183 | |
184 base::DictionaryValue* properties = GetDeviceProperties(device_path); | |
185 properties->SetWithoutPathExpansion( | |
186 shill::kTypeProperty, | |
187 base::Value::CreateStringValue(type)); | |
188 properties->SetWithoutPathExpansion( | |
189 shill::kDBusObjectProperty, | |
190 base::Value::CreateStringValue(object_path)); | |
191 properties->SetWithoutPathExpansion( | |
192 shill::kDBusConnectionProperty, | |
193 base::Value::CreateStringValue("/stub")); | |
194 } | |
195 | |
196 void ShillDeviceClientStub::RemoveDevice(const std::string& device_path){ | |
197 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()-> | |
198 RemoveDevice(device_path); | |
199 | |
200 stub_devices_.RemoveWithoutPathExpansion(device_path, NULL); | |
201 } | |
202 | |
203 void ShillDeviceClientStub::ClearDevices(){ | |
204 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()-> | |
205 ClearDevices(); | |
206 | |
207 stub_devices_.Clear(); | |
208 } | |
209 | |
210 void ShillDeviceClientStub::SetDeviceProperty(const std::string& device_path, | |
211 const std::string& name, | |
212 const base::Value& value){ | |
213 VLOG(1) << "SetDeviceProperty: " << device_path | |
214 << ": " << name << " = " << value; | |
215 SetProperty(dbus::ObjectPath(device_path), name, value, | |
216 base::Bind(&base::DoNothing), | |
217 base::Bind(&ErrorFunction, device_path)); | |
218 } | |
219 | |
220 std::string ShillDeviceClientStub::GetDevicePathForType( | |
221 const std::string& type) { | |
222 for (base::DictionaryValue::Iterator iter(stub_devices_); | |
223 !iter.IsAtEnd(); iter.Advance()) { | |
224 const base::DictionaryValue* properties = NULL; | |
225 if (!iter.value().GetAsDictionary(&properties)) | |
226 continue; | |
227 std::string prop_type; | |
228 if (!properties->GetStringWithoutPathExpansion( | |
229 shill::kTypeProperty, &prop_type) || | |
230 prop_type != type) | |
231 continue; | |
232 return iter.key(); | |
233 } | |
234 return std::string(); | |
235 } | |
236 | |
237 void ShillDeviceClientStub::PassStubDeviceProperties( | |
238 const dbus::ObjectPath& device_path, | |
239 const DictionaryValueCallback& callback) const { | |
240 const base::DictionaryValue* device_properties = NULL; | |
241 if (!stub_devices_.GetDictionaryWithoutPathExpansion( | |
242 device_path.value(), &device_properties)) { | |
243 base::DictionaryValue empty_dictionary; | |
244 callback.Run(DBUS_METHOD_CALL_FAILURE, empty_dictionary); | |
245 return; | |
246 } | |
247 callback.Run(DBUS_METHOD_CALL_SUCCESS, *device_properties); | |
248 } | |
249 | |
250 // Posts a task to run a void callback with status code |status|. | |
251 void ShillDeviceClientStub::PostVoidCallback( | |
252 const VoidDBusMethodCallback& callback, | |
253 DBusMethodCallStatus status) { | |
254 base::MessageLoop::current()->PostTask(FROM_HERE, | |
255 base::Bind(callback, status)); | |
256 } | |
257 | |
258 void ShillDeviceClientStub::NotifyObserversPropertyChanged( | |
259 const dbus::ObjectPath& device_path, | |
260 const std::string& property) { | |
261 base::DictionaryValue* dict = NULL; | |
262 std::string path = device_path.value(); | |
263 if (!stub_devices_.GetDictionaryWithoutPathExpansion(path, &dict)) { | |
264 LOG(ERROR) << "Notify for unknown service: " << path; | |
265 return; | |
266 } | |
267 base::Value* value = NULL; | |
268 if (!dict->GetWithoutPathExpansion(property, &value)) { | |
269 LOG(ERROR) << "Notify for unknown property: " | |
270 << path << " : " << property; | |
271 return; | |
272 } | |
273 FOR_EACH_OBSERVER(ShillPropertyChangedObserver, | |
274 GetObserverList(device_path), | |
275 OnPropertyChanged(property, *value)); | |
276 } | |
277 | |
278 base::DictionaryValue* ShillDeviceClientStub::GetDeviceProperties( | |
279 const std::string& device_path) { | |
280 base::DictionaryValue* properties = NULL; | |
281 if (!stub_devices_.GetDictionaryWithoutPathExpansion( | |
282 device_path, &properties)) { | |
283 properties = new base::DictionaryValue; | |
284 stub_devices_.SetWithoutPathExpansion(device_path, properties); | |
285 } | |
286 return properties; | |
287 } | |
288 | |
289 ShillDeviceClientStub::PropertyObserverList& | |
290 ShillDeviceClientStub::GetObserverList(const dbus::ObjectPath& device_path) { | |
291 std::map<dbus::ObjectPath, PropertyObserverList*>::iterator iter = | |
292 observer_list_.find(device_path); | |
293 if (iter != observer_list_.end()) | |
294 return *(iter->second); | |
295 PropertyObserverList* observer_list = new PropertyObserverList(); | |
296 observer_list_[device_path] = observer_list; | |
297 return *observer_list; | |
298 } | |
299 | |
300 } // namespace chromeos | |
OLD | NEW |