OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/dbus/fake_shill_device_client.h" | 5 #include "chromeos/dbus/fake_shill_device_client.h" |
6 | 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 |
7 namespace chromeos { | 21 namespace chromeos { |
8 | 22 |
9 FakeShillDeviceClient::FakeShillDeviceClient() { | 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 FakeShillDeviceClient::FakeShillDeviceClient() : weak_ptr_factory_(this) { |
10 } | 35 } |
11 | 36 |
12 FakeShillDeviceClient::~FakeShillDeviceClient() { | 37 FakeShillDeviceClient::~FakeShillDeviceClient() { |
| 38 STLDeleteContainerPairSecondPointers( |
| 39 observer_list_.begin(), observer_list_.end()); |
13 } | 40 } |
14 | 41 |
15 void FakeShillDeviceClient::Init(dbus::Bus* bus) { | 42 // ShillDeviceClient overrides. |
16 } | 43 |
| 44 void FakeShillDeviceClient::Init(dbus::Bus* bus) {} |
17 | 45 |
18 void FakeShillDeviceClient::AddPropertyChangedObserver( | 46 void FakeShillDeviceClient::AddPropertyChangedObserver( |
19 const dbus::ObjectPath& device_path, | 47 const dbus::ObjectPath& device_path, |
20 ShillPropertyChangedObserver* observer) { | 48 ShillPropertyChangedObserver* observer) { |
| 49 GetObserverList(device_path).AddObserver(observer); |
21 } | 50 } |
22 | 51 |
23 void FakeShillDeviceClient::RemovePropertyChangedObserver( | 52 void FakeShillDeviceClient::RemovePropertyChangedObserver( |
24 const dbus::ObjectPath& device_path, | 53 const dbus::ObjectPath& device_path, |
25 ShillPropertyChangedObserver* observer) { | 54 ShillPropertyChangedObserver* observer) { |
| 55 GetObserverList(device_path).RemoveObserver(observer); |
26 } | 56 } |
27 | 57 |
28 void FakeShillDeviceClient::GetProperties( | 58 void FakeShillDeviceClient::GetProperties( |
29 const dbus::ObjectPath& device_path, | 59 const dbus::ObjectPath& device_path, |
30 const DictionaryValueCallback& callback) { | 60 const DictionaryValueCallback& callback) { |
| 61 base::MessageLoop::current()->PostTask( |
| 62 FROM_HERE, |
| 63 base::Bind(&FakeShillDeviceClient::PassStubDeviceProperties, |
| 64 weak_ptr_factory_.GetWeakPtr(), |
| 65 device_path, callback)); |
31 } | 66 } |
32 | 67 |
33 void FakeShillDeviceClient::ProposeScan( | 68 void FakeShillDeviceClient::ProposeScan( |
34 const dbus::ObjectPath& device_path, | 69 const dbus::ObjectPath& device_path, |
35 const VoidDBusMethodCallback& callback) { | 70 const VoidDBusMethodCallback& callback) { |
| 71 PostVoidCallback(callback, DBUS_METHOD_CALL_SUCCESS); |
36 } | 72 } |
37 | 73 |
38 void FakeShillDeviceClient::SetProperty(const dbus::ObjectPath& device_path, | 74 void FakeShillDeviceClient::SetProperty(const dbus::ObjectPath& device_path, |
39 const std::string& name, | 75 const std::string& name, |
40 const base::Value& value, | 76 const base::Value& value, |
41 const base::Closure& callback, | 77 const base::Closure& callback, |
42 const ErrorCallback& error_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, |
| 86 error_name, |
| 87 error_message)); |
| 88 return; |
| 89 } |
| 90 device_properties->SetWithoutPathExpansion(name, value.DeepCopy()); |
| 91 base::MessageLoop::current()->PostTask( |
| 92 FROM_HERE, |
| 93 base::Bind(&FakeShillDeviceClient::NotifyObserversPropertyChanged, |
| 94 weak_ptr_factory_.GetWeakPtr(), device_path, name)); |
| 95 base::MessageLoop::current()->PostTask(FROM_HERE, callback); |
43 } | 96 } |
44 | 97 |
45 void FakeShillDeviceClient::ClearProperty( | 98 void FakeShillDeviceClient::ClearProperty( |
46 const dbus::ObjectPath& device_path, | 99 const dbus::ObjectPath& device_path, |
47 const std::string& name, | 100 const std::string& name, |
48 const VoidDBusMethodCallback& callback) { | 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); |
| 106 return; |
| 107 } |
| 108 device_properties->RemoveWithoutPathExpansion(name, NULL); |
| 109 PostVoidCallback(callback, DBUS_METHOD_CALL_SUCCESS); |
49 } | 110 } |
50 | 111 |
51 void FakeShillDeviceClient::AddIPConfig( | 112 void FakeShillDeviceClient::AddIPConfig( |
52 const dbus::ObjectPath& device_path, | 113 const dbus::ObjectPath& device_path, |
53 const std::string& method, | 114 const std::string& method, |
54 const ObjectPathDBusMethodCallback& callback) { | 115 const ObjectPathDBusMethodCallback& callback) { |
| 116 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 117 base::Bind(callback, |
| 118 DBUS_METHOD_CALL_SUCCESS, |
| 119 dbus::ObjectPath())); |
55 } | 120 } |
56 | 121 |
57 void FakeShillDeviceClient::RequirePin(const dbus::ObjectPath& device_path, | 122 void FakeShillDeviceClient::RequirePin(const dbus::ObjectPath& device_path, |
58 const std::string& pin, | 123 const std::string& pin, |
59 bool require, | 124 bool require, |
60 const base::Closure& callback, | 125 const base::Closure& callback, |
61 const ErrorCallback& error_callback) { | 126 const ErrorCallback& error_callback) { |
| 127 base::MessageLoop::current()->PostTask(FROM_HERE, callback); |
62 } | 128 } |
63 | 129 |
64 void FakeShillDeviceClient::EnterPin(const dbus::ObjectPath& device_path, | 130 void FakeShillDeviceClient::EnterPin(const dbus::ObjectPath& device_path, |
65 const std::string& pin, | 131 const std::string& pin, |
66 const base::Closure& callback, | 132 const base::Closure& callback, |
67 const ErrorCallback& error_callback) { | 133 const ErrorCallback& error_callback) { |
| 134 base::MessageLoop::current()->PostTask(FROM_HERE, callback); |
68 } | 135 } |
69 | 136 |
70 void FakeShillDeviceClient::UnblockPin(const dbus::ObjectPath& device_path, | 137 void FakeShillDeviceClient::UnblockPin(const dbus::ObjectPath& device_path, |
71 const std::string& puk, | 138 const std::string& puk, |
72 const std::string& pin, | 139 const std::string& pin, |
73 const base::Closure& callback, | 140 const base::Closure& callback, |
74 const ErrorCallback& error_callback) { | 141 const ErrorCallback& error_callback) { |
| 142 base::MessageLoop::current()->PostTask(FROM_HERE, callback); |
75 } | 143 } |
76 | 144 |
77 void FakeShillDeviceClient::ChangePin(const dbus::ObjectPath& device_path, | 145 void FakeShillDeviceClient::ChangePin(const dbus::ObjectPath& device_path, |
78 const std::string& old_pin, | 146 const std::string& old_pin, |
79 const std::string& new_pin, | 147 const std::string& new_pin, |
80 const base::Closure& callback, | 148 const base::Closure& callback, |
81 const ErrorCallback& error_callback) { | 149 const ErrorCallback& error_callback) { |
| 150 base::MessageLoop::current()->PostTask(FROM_HERE, callback); |
82 } | 151 } |
83 | 152 |
84 void FakeShillDeviceClient::Register(const dbus::ObjectPath& device_path, | 153 void FakeShillDeviceClient::Register(const dbus::ObjectPath& device_path, |
85 const std::string& network_id, | 154 const std::string& network_id, |
86 const base::Closure& callback, | 155 const base::Closure& callback, |
87 const ErrorCallback& error_callback) { | 156 const ErrorCallback& error_callback) { |
| 157 base::MessageLoop::current()->PostTask(FROM_HERE, callback); |
88 } | 158 } |
89 | 159 |
90 void FakeShillDeviceClient::SetCarrier(const dbus::ObjectPath& device_path, | 160 void FakeShillDeviceClient::SetCarrier(const dbus::ObjectPath& device_path, |
91 const std::string& carrier, | 161 const std::string& carrier, |
92 const base::Closure& callback, | 162 const base::Closure& callback, |
93 const ErrorCallback& error_callback) { | 163 const ErrorCallback& error_callback) { |
| 164 base::MessageLoop::current()->PostTask(FROM_HERE, callback); |
94 } | 165 } |
95 | 166 |
96 void FakeShillDeviceClient::Reset(const dbus::ObjectPath& device_path, | 167 void FakeShillDeviceClient::Reset(const dbus::ObjectPath& device_path, |
97 const base::Closure& callback, | 168 const base::Closure& callback, |
98 const ErrorCallback& error_callback) { | 169 const ErrorCallback& error_callback) { |
| 170 base::MessageLoop::current()->PostTask(FROM_HERE, callback); |
99 } | 171 } |
100 | 172 |
101 FakeShillDeviceClient::TestInterface* | 173 ShillDeviceClient::TestInterface* FakeShillDeviceClient::GetTestInterface() { |
102 FakeShillDeviceClient::GetTestInterface() { | 174 return this; |
103 return NULL; | 175 } |
| 176 |
| 177 // ShillDeviceClient::TestInterface overrides. |
| 178 |
| 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); |
| 184 |
| 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")); |
| 195 } |
| 196 |
| 197 void FakeShillDeviceClient::RemoveDevice(const std::string& device_path) { |
| 198 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()-> |
| 199 RemoveDevice(device_path); |
| 200 |
| 201 stub_devices_.RemoveWithoutPathExpansion(device_path, NULL); |
| 202 } |
| 203 |
| 204 void FakeShillDeviceClient::ClearDevices() { |
| 205 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()-> |
| 206 ClearDevices(); |
| 207 |
| 208 stub_devices_.Clear(); |
| 209 } |
| 210 |
| 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)); |
| 219 } |
| 220 |
| 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)) |
| 227 continue; |
| 228 std::string prop_type; |
| 229 if (!properties->GetStringWithoutPathExpansion( |
| 230 shill::kTypeProperty, &prop_type) || |
| 231 prop_type != type) |
| 232 continue; |
| 233 return iter.key(); |
| 234 } |
| 235 return std::string(); |
| 236 } |
| 237 |
| 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); |
| 246 return; |
| 247 } |
| 248 callback.Run(DBUS_METHOD_CALL_SUCCESS, *device_properties); |
| 249 } |
| 250 |
| 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)); |
| 257 } |
| 258 |
| 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; |
| 266 return; |
| 267 } |
| 268 base::Value* value = NULL; |
| 269 if (!dict->GetWithoutPathExpansion(property, &value)) { |
| 270 LOG(ERROR) << "Notify for unknown property: " |
| 271 << path << " : " << property; |
| 272 return; |
| 273 } |
| 274 FOR_EACH_OBSERVER(ShillPropertyChangedObserver, |
| 275 GetObserverList(device_path), |
| 276 OnPropertyChanged(property, *value)); |
| 277 } |
| 278 |
| 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); |
| 286 } |
| 287 return properties; |
| 288 } |
| 289 |
| 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; |
104 } | 299 } |
105 | 300 |
106 } // namespace chromeos | 301 } // namespace chromeos |
OLD | NEW |