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_message("Failed");
36 base::MessageLoop::current()->PostTask(
38 base::Bind(error_callback, shill::kErrorResultNotFound, error_message));
43 FakeShillDeviceClient::FakeShillDeviceClient()
44 : tdls_busy_count_(0),
45 weak_ptr_factory_(this) {
48 FakeShillDeviceClient::~FakeShillDeviceClient() {
49 STLDeleteContainerPairSecondPointers(
50 observer_list_.begin(), observer_list_.end());
53 // ShillDeviceClient overrides.
55 void FakeShillDeviceClient::Init(dbus::Bus* bus) {}
57 void FakeShillDeviceClient::AddPropertyChangedObserver(
58 const dbus::ObjectPath& device_path,
59 ShillPropertyChangedObserver* observer) {
60 GetObserverList(device_path).AddObserver(observer);
63 void FakeShillDeviceClient::RemovePropertyChangedObserver(
64 const dbus::ObjectPath& device_path,
65 ShillPropertyChangedObserver* observer) {
66 GetObserverList(device_path).RemoveObserver(observer);
69 void FakeShillDeviceClient::GetProperties(
70 const dbus::ObjectPath& device_path,
71 const DictionaryValueCallback& callback) {
72 base::MessageLoop::current()->PostTask(
74 base::Bind(&FakeShillDeviceClient::PassStubDeviceProperties,
75 weak_ptr_factory_.GetWeakPtr(),
76 device_path, callback));
79 void FakeShillDeviceClient::ProposeScan(
80 const dbus::ObjectPath& device_path,
81 const VoidDBusMethodCallback& callback) {
82 PostVoidCallback(callback, DBUS_METHOD_CALL_SUCCESS);
85 void FakeShillDeviceClient::SetProperty(const dbus::ObjectPath& device_path,
86 const std::string& name,
87 const base::Value& value,
88 const base::Closure& callback,
89 const ErrorCallback& error_callback) {
90 base::DictionaryValue* device_properties = NULL;
91 if (!stub_devices_.GetDictionaryWithoutPathExpansion(device_path.value(),
92 &device_properties)) {
93 PostDeviceNotFoundError(error_callback);
96 device_properties->SetWithoutPathExpansion(name, value.DeepCopy());
97 base::MessageLoop::current()->PostTask(
99 base::Bind(&FakeShillDeviceClient::NotifyObserversPropertyChanged,
100 weak_ptr_factory_.GetWeakPtr(), device_path, name));
101 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
104 void FakeShillDeviceClient::ClearProperty(
105 const dbus::ObjectPath& device_path,
106 const std::string& name,
107 const VoidDBusMethodCallback& callback) {
108 base::DictionaryValue* device_properties = NULL;
109 if (!stub_devices_.GetDictionaryWithoutPathExpansion(device_path.value(),
110 &device_properties)) {
111 PostVoidCallback(callback, DBUS_METHOD_CALL_FAILURE);
114 device_properties->RemoveWithoutPathExpansion(name, NULL);
115 PostVoidCallback(callback, DBUS_METHOD_CALL_SUCCESS);
118 void FakeShillDeviceClient::AddIPConfig(
119 const dbus::ObjectPath& device_path,
120 const std::string& method,
121 const ObjectPathDBusMethodCallback& callback) {
122 base::MessageLoop::current()->PostTask(FROM_HERE,
124 DBUS_METHOD_CALL_SUCCESS,
125 dbus::ObjectPath()));
128 void FakeShillDeviceClient::RequirePin(const dbus::ObjectPath& device_path,
129 const std::string& pin,
131 const base::Closure& callback,
132 const ErrorCallback& error_callback) {
133 if (!stub_devices_.HasKey(device_path.value())) {
134 PostDeviceNotFoundError(error_callback);
137 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
140 void FakeShillDeviceClient::EnterPin(const dbus::ObjectPath& device_path,
141 const std::string& pin,
142 const base::Closure& callback,
143 const ErrorCallback& error_callback) {
144 if (!stub_devices_.HasKey(device_path.value())) {
145 PostDeviceNotFoundError(error_callback);
148 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
151 void FakeShillDeviceClient::UnblockPin(const dbus::ObjectPath& device_path,
152 const std::string& puk,
153 const std::string& pin,
154 const base::Closure& callback,
155 const ErrorCallback& error_callback) {
156 if (!stub_devices_.HasKey(device_path.value())) {
157 PostDeviceNotFoundError(error_callback);
160 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
163 void FakeShillDeviceClient::ChangePin(const dbus::ObjectPath& device_path,
164 const std::string& old_pin,
165 const std::string& new_pin,
166 const base::Closure& callback,
167 const ErrorCallback& error_callback) {
168 if (!stub_devices_.HasKey(device_path.value())) {
169 PostDeviceNotFoundError(error_callback);
172 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
175 void FakeShillDeviceClient::Register(const dbus::ObjectPath& device_path,
176 const std::string& network_id,
177 const base::Closure& callback,
178 const ErrorCallback& error_callback) {
179 if (!stub_devices_.HasKey(device_path.value())) {
180 PostDeviceNotFoundError(error_callback);
183 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
186 void FakeShillDeviceClient::SetCarrier(const dbus::ObjectPath& device_path,
187 const std::string& carrier,
188 const base::Closure& callback,
189 const ErrorCallback& error_callback) {
190 if (!stub_devices_.HasKey(device_path.value())) {
191 PostDeviceNotFoundError(error_callback);
194 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
197 void FakeShillDeviceClient::Reset(const dbus::ObjectPath& device_path,
198 const base::Closure& callback,
199 const ErrorCallback& error_callback) {
200 if (!stub_devices_.HasKey(device_path.value())) {
201 PostDeviceNotFoundError(error_callback);
204 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
207 void FakeShillDeviceClient::PerformTDLSOperation(
208 const dbus::ObjectPath& device_path,
209 const std::string& operation,
210 const std::string& peer,
211 const StringCallback& callback,
212 const ErrorCallback& error_callback) {
213 if (!stub_devices_.HasKey(device_path.value())) {
214 PostDeviceNotFoundError(error_callback);
217 if (tdls_busy_count_) {
219 std::string error_message("In-Progress");
220 base::MessageLoop::current()->PostTask(
222 base::Bind(error_callback,
223 shill::kErrorResultInProgress, error_message));
227 if (operation == shill::kTDLSStatusOperation)
228 result = shill::kTDLSConnectedState;
229 base::MessageLoop::current()->PostTask(FROM_HERE,
230 base::Bind(callback, result));
233 ShillDeviceClient::TestInterface* FakeShillDeviceClient::GetTestInterface() {
237 // ShillDeviceClient::TestInterface overrides.
239 void FakeShillDeviceClient::AddDevice(const std::string& device_path,
240 const std::string& type,
241 const std::string& object_path) {
242 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
243 AddDevice(device_path);
245 base::DictionaryValue* properties = GetDeviceProperties(device_path);
246 properties->SetWithoutPathExpansion(shill::kTypeProperty,
247 base::Value::CreateStringValue(type));
248 properties->SetWithoutPathExpansion(
249 shill::kDBusObjectProperty, base::Value::CreateStringValue(object_path));
250 properties->SetWithoutPathExpansion(
251 shill::kDBusServiceProperty,
252 base::Value::CreateStringValue(modemmanager::kModemManager1));
253 if (NetworkTypePattern::Cellular().MatchesType(type)) {
254 properties->SetWithoutPathExpansion(shill::kCellularAllowRoamingProperty,
255 new base::FundamentalValue(false));
259 void FakeShillDeviceClient::RemoveDevice(const std::string& device_path) {
260 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
261 RemoveDevice(device_path);
263 stub_devices_.RemoveWithoutPathExpansion(device_path, NULL);
266 void FakeShillDeviceClient::ClearDevices() {
267 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
270 stub_devices_.Clear();
273 void FakeShillDeviceClient::SetDeviceProperty(const std::string& device_path,
274 const std::string& name,
275 const base::Value& value) {
276 VLOG(1) << "SetDeviceProperty: " << device_path
277 << ": " << name << " = " << value;
278 SetProperty(dbus::ObjectPath(device_path), name, value,
279 base::Bind(&base::DoNothing),
280 base::Bind(&ErrorFunction, device_path));
283 std::string FakeShillDeviceClient::GetDevicePathForType(
284 const std::string& type) {
285 for (base::DictionaryValue::Iterator iter(stub_devices_);
286 !iter.IsAtEnd(); iter.Advance()) {
287 const base::DictionaryValue* properties = NULL;
288 if (!iter.value().GetAsDictionary(&properties))
290 std::string prop_type;
291 if (!properties->GetStringWithoutPathExpansion(
292 shill::kTypeProperty, &prop_type) ||
297 return std::string();
300 void FakeShillDeviceClient::PassStubDeviceProperties(
301 const dbus::ObjectPath& device_path,
302 const DictionaryValueCallback& callback) const {
303 const base::DictionaryValue* device_properties = NULL;
304 if (!stub_devices_.GetDictionaryWithoutPathExpansion(
305 device_path.value(), &device_properties)) {
306 base::DictionaryValue empty_dictionary;
307 callback.Run(DBUS_METHOD_CALL_FAILURE, empty_dictionary);
310 callback.Run(DBUS_METHOD_CALL_SUCCESS, *device_properties);
313 // Posts a task to run a void callback with status code |status|.
314 void FakeShillDeviceClient::PostVoidCallback(
315 const VoidDBusMethodCallback& callback,
316 DBusMethodCallStatus status) {
317 base::MessageLoop::current()->PostTask(FROM_HERE,
318 base::Bind(callback, status));
321 void FakeShillDeviceClient::NotifyObserversPropertyChanged(
322 const dbus::ObjectPath& device_path,
323 const std::string& property) {
324 base::DictionaryValue* dict = NULL;
325 std::string path = device_path.value();
326 if (!stub_devices_.GetDictionaryWithoutPathExpansion(path, &dict)) {
327 LOG(ERROR) << "Notify for unknown service: " << path;
330 base::Value* value = NULL;
331 if (!dict->GetWithoutPathExpansion(property, &value)) {
332 LOG(ERROR) << "Notify for unknown property: "
333 << path << " : " << property;
336 FOR_EACH_OBSERVER(ShillPropertyChangedObserver,
337 GetObserverList(device_path),
338 OnPropertyChanged(property, *value));
341 base::DictionaryValue* FakeShillDeviceClient::GetDeviceProperties(
342 const std::string& device_path) {
343 base::DictionaryValue* properties = NULL;
344 if (!stub_devices_.GetDictionaryWithoutPathExpansion(
345 device_path, &properties)) {
346 properties = new base::DictionaryValue;
347 stub_devices_.SetWithoutPathExpansion(device_path, properties);
352 FakeShillDeviceClient::PropertyObserverList&
353 FakeShillDeviceClient::GetObserverList(const dbus::ObjectPath& device_path) {
354 std::map<dbus::ObjectPath, PropertyObserverList*>::iterator iter =
355 observer_list_.find(device_path);
356 if (iter != observer_list_.end())
357 return *(iter->second);
358 PropertyObserverList* observer_list = new PropertyObserverList();
359 observer_list_[device_path] = observer_list;
360 return *observer_list;
363 } // namespace chromeos