OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/shill_device_client.h" | 5 #include "chromeos/dbus/shill_device_client.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 const base::Closure& callback, | 181 const base::Closure& callback, |
182 const ErrorCallback& error_callback) OVERRIDE { | 182 const ErrorCallback& error_callback) OVERRIDE { |
183 dbus::MethodCall method_call(flimflam::kFlimflamDeviceInterface, | 183 dbus::MethodCall method_call(flimflam::kFlimflamDeviceInterface, |
184 shill::kSetCarrierFunction); | 184 shill::kSetCarrierFunction); |
185 dbus::MessageWriter writer(&method_call); | 185 dbus::MessageWriter writer(&method_call); |
186 writer.AppendString(carrier); | 186 writer.AppendString(carrier); |
187 GetHelper(device_path)->CallVoidMethodWithErrorCallback( | 187 GetHelper(device_path)->CallVoidMethodWithErrorCallback( |
188 &method_call, callback, error_callback); | 188 &method_call, callback, error_callback); |
189 } | 189 } |
190 | 190 |
| 191 virtual TestInterface* GetTestInterface() OVERRIDE { |
| 192 return NULL; |
| 193 } |
| 194 |
191 private: | 195 private: |
192 typedef std::map<std::string, ShillClientHelper*> HelperMap; | 196 typedef std::map<std::string, ShillClientHelper*> HelperMap; |
193 | 197 |
194 // Returns the corresponding ShillClientHelper for the profile. | 198 // Returns the corresponding ShillClientHelper for the profile. |
195 ShillClientHelper* GetHelper(const dbus::ObjectPath& device_path) { | 199 ShillClientHelper* GetHelper(const dbus::ObjectPath& device_path) { |
196 HelperMap::iterator it = helpers_.find(device_path.value()); | 200 HelperMap::iterator it = helpers_.find(device_path.value()); |
197 if (it != helpers_.end()) | 201 if (it != helpers_.end()) |
198 return it->second; | 202 return it->second; |
199 | 203 |
200 // There is no helper for the profile, create it. | 204 // There is no helper for the profile, create it. |
201 dbus::ObjectProxy* object_proxy = | 205 dbus::ObjectProxy* object_proxy = |
202 bus_->GetObjectProxy(flimflam::kFlimflamServiceName, device_path); | 206 bus_->GetObjectProxy(flimflam::kFlimflamServiceName, device_path); |
203 ShillClientHelper* helper = new ShillClientHelper(bus_, object_proxy); | 207 ShillClientHelper* helper = new ShillClientHelper(bus_, object_proxy); |
204 helper->MonitorPropertyChanged(flimflam::kFlimflamDeviceInterface); | 208 helper->MonitorPropertyChanged(flimflam::kFlimflamDeviceInterface); |
205 helpers_.insert(HelperMap::value_type(device_path.value(), helper)); | 209 helpers_.insert(HelperMap::value_type(device_path.value(), helper)); |
206 return helper; | 210 return helper; |
207 } | 211 } |
208 | 212 |
209 dbus::Bus* bus_; | 213 dbus::Bus* bus_; |
210 HelperMap helpers_; | 214 HelperMap helpers_; |
211 STLValueDeleter<HelperMap> helpers_deleter_; | 215 STLValueDeleter<HelperMap> helpers_deleter_; |
212 | 216 |
213 DISALLOW_COPY_AND_ASSIGN(ShillDeviceClientImpl); | 217 DISALLOW_COPY_AND_ASSIGN(ShillDeviceClientImpl); |
214 }; | 218 }; |
215 | 219 |
216 // A stub implementation of ShillDeviceClient. | 220 // A stub implementation of ShillDeviceClient. |
217 // Implemented: Stub cellular device for SMS testing. | 221 // Implemented: Stub cellular device for SMS testing. |
218 class ShillDeviceClientStubImpl : public ShillDeviceClient { | 222 class ShillDeviceClientStubImpl : public ShillDeviceClient, |
| 223 public ShillDeviceClient::TestInterface { |
219 public: | 224 public: |
220 ShillDeviceClientStubImpl() : weak_ptr_factory_(this) { | 225 ShillDeviceClientStubImpl() : weak_ptr_factory_(this) { |
221 // Add a cellular device for SMS. Note: name matches Manager entry. | 226 SetDefaultProperties(); |
222 const char kStubCellular1[] = "stub_cellular1"; | |
223 base::DictionaryValue* cellular_properties = new base::DictionaryValue; | |
224 cellular_properties->SetWithoutPathExpansion( | |
225 flimflam::kTypeProperty, | |
226 base::Value::CreateStringValue(flimflam::kTypeCellular)); | |
227 cellular_properties->SetWithoutPathExpansion( | |
228 flimflam::kDBusConnectionProperty, | |
229 base::Value::CreateStringValue("/stub")); | |
230 cellular_properties->SetWithoutPathExpansion( | |
231 flimflam::kDBusObjectProperty, | |
232 base::Value::CreateStringValue("/device/cellular1")); | |
233 stub_devices_.Set(kStubCellular1, cellular_properties); | |
234 | |
235 // Create a second device stubbing a modem managed by | |
236 // ModemManager1 interfaces. | |
237 // Note: name matches Manager entry. | |
238 const char kStubCellular2[] = "stub_cellular2"; | |
239 cellular_properties = new base::DictionaryValue; | |
240 cellular_properties->SetWithoutPathExpansion( | |
241 flimflam::kTypeProperty, | |
242 base::Value::CreateStringValue(flimflam::kTypeCellular)); | |
243 cellular_properties->SetWithoutPathExpansion( | |
244 flimflam::kDBusConnectionProperty, | |
245 base::Value::CreateStringValue(":stub.0")); | |
246 cellular_properties->SetWithoutPathExpansion( | |
247 flimflam::kDBusObjectProperty, | |
248 base::Value::CreateStringValue( | |
249 "/org/freedesktop/ModemManager1/stub/0")); | |
250 stub_devices_.Set(kStubCellular2, cellular_properties); | |
251 } | 227 } |
252 | 228 |
253 virtual ~ShillDeviceClientStubImpl() {} | 229 virtual ~ShillDeviceClientStubImpl() {} |
254 | 230 |
255 /////////////////////////////////// | |
256 // ShillDeviceClient overrides. | 231 // ShillDeviceClient overrides. |
| 232 |
257 virtual void AddPropertyChangedObserver( | 233 virtual void AddPropertyChangedObserver( |
258 const dbus::ObjectPath& device_path, | 234 const dbus::ObjectPath& device_path, |
259 ShillPropertyChangedObserver* observer) OVERRIDE {} | 235 ShillPropertyChangedObserver* observer) OVERRIDE {} |
260 | 236 |
261 virtual void RemovePropertyChangedObserver( | 237 virtual void RemovePropertyChangedObserver( |
262 const dbus::ObjectPath& device_path, | 238 const dbus::ObjectPath& device_path, |
263 ShillPropertyChangedObserver* observer) OVERRIDE {} | 239 ShillPropertyChangedObserver* observer) OVERRIDE {} |
264 | 240 |
265 virtual void GetProperties(const dbus::ObjectPath& device_path, | 241 virtual void GetProperties(const dbus::ObjectPath& device_path, |
266 const DictionaryValueCallback& callback) OVERRIDE { | 242 const DictionaryValueCallback& callback) OVERRIDE { |
267 MessageLoop::current()->PostTask( | 243 MessageLoop::current()->PostTask( |
268 FROM_HERE, | 244 FROM_HERE, |
269 base::Bind(&ShillDeviceClientStubImpl::PassStubDeviceProperties, | 245 base::Bind(&ShillDeviceClientStubImpl::PassStubDeviceProperties, |
270 weak_ptr_factory_.GetWeakPtr(), | 246 weak_ptr_factory_.GetWeakPtr(), |
271 device_path, callback)); | 247 device_path, callback)); |
272 } | 248 } |
273 | 249 |
274 virtual base::DictionaryValue* CallGetPropertiesAndBlock( | 250 virtual base::DictionaryValue* CallGetPropertiesAndBlock( |
275 const dbus::ObjectPath& device_path) OVERRIDE { | 251 const dbus::ObjectPath& device_path) OVERRIDE { |
276 return new base::DictionaryValue; | 252 base::DictionaryValue* device_properties = NULL; |
| 253 stub_devices_.GetDictionaryWithoutPathExpansion( |
| 254 device_path.value(), &device_properties); |
| 255 return device_properties; |
277 } | 256 } |
278 | 257 |
279 virtual void ProposeScan(const dbus::ObjectPath& device_path, | 258 virtual void ProposeScan(const dbus::ObjectPath& device_path, |
280 const VoidDBusMethodCallback& callback) OVERRIDE { | 259 const VoidDBusMethodCallback& callback) OVERRIDE { |
281 PostVoidCallback(callback, DBUS_METHOD_CALL_SUCCESS); | 260 PostVoidCallback(callback, DBUS_METHOD_CALL_SUCCESS); |
282 } | 261 } |
283 | 262 |
284 virtual void SetProperty(const dbus::ObjectPath& device_path, | 263 virtual void SetProperty(const dbus::ObjectPath& device_path, |
285 const std::string& name, | 264 const std::string& name, |
286 const base::Value& value, | 265 const base::Value& value, |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 MessageLoop::current()->PostTask(FROM_HERE, callback); | 338 MessageLoop::current()->PostTask(FROM_HERE, callback); |
360 } | 339 } |
361 | 340 |
362 virtual void Register(const dbus::ObjectPath& device_path, | 341 virtual void Register(const dbus::ObjectPath& device_path, |
363 const std::string& network_id, | 342 const std::string& network_id, |
364 const base::Closure& callback, | 343 const base::Closure& callback, |
365 const ErrorCallback& error_callback) OVERRIDE { | 344 const ErrorCallback& error_callback) OVERRIDE { |
366 MessageLoop::current()->PostTask(FROM_HERE, callback); | 345 MessageLoop::current()->PostTask(FROM_HERE, callback); |
367 } | 346 } |
368 | 347 |
369 // ShillDeviceClient override. | |
370 virtual void SetCarrier(const dbus::ObjectPath& device_path, | 348 virtual void SetCarrier(const dbus::ObjectPath& device_path, |
371 const std::string& carrier, | 349 const std::string& carrier, |
372 const base::Closure& callback, | 350 const base::Closure& callback, |
373 const ErrorCallback& error_callback) OVERRIDE { | 351 const ErrorCallback& error_callback) OVERRIDE { |
374 MessageLoop::current()->PostTask(FROM_HERE, callback); | 352 MessageLoop::current()->PostTask(FROM_HERE, callback); |
375 } | 353 } |
376 | 354 |
| 355 virtual ShillDeviceClient::TestInterface* GetTestInterface() OVERRIDE { |
| 356 return this; |
| 357 } |
| 358 |
| 359 // ShillDeviceClient::TestInterface overrides. |
| 360 |
| 361 virtual void AddDevice(const std::string& device_path, |
| 362 const std::string& type, |
| 363 const std::string& object_path, |
| 364 const std::string& connection_path) OVERRIDE { |
| 365 base::DictionaryValue* properties = GetDeviceProperties(device_path); |
| 366 properties->SetWithoutPathExpansion( |
| 367 flimflam::kTypeProperty, |
| 368 base::Value::CreateStringValue(type)); |
| 369 properties->SetWithoutPathExpansion( |
| 370 flimflam::kDBusObjectProperty, |
| 371 base::Value::CreateStringValue(object_path)); |
| 372 properties->SetWithoutPathExpansion( |
| 373 flimflam::kDBusConnectionProperty, |
| 374 base::Value::CreateStringValue(connection_path)); |
| 375 } |
| 376 |
| 377 virtual void RemoveDevice(const std::string& device_path) OVERRIDE { |
| 378 stub_devices_.RemoveWithoutPathExpansion(device_path, NULL); |
| 379 } |
| 380 |
| 381 virtual void ClearDevices() OVERRIDE { |
| 382 stub_devices_.Clear(); |
| 383 } |
| 384 |
377 private: | 385 private: |
| 386 void SetDefaultProperties() { |
| 387 // Add a wifi device. Note: path matches Manager entry. |
| 388 AddDevice("stub_wifi_device1", flimflam::kTypeWifi, |
| 389 "/device/wifi1", "/stub"); |
| 390 |
| 391 // Add a cellular device. Used in SMS stub. Note: path matches |
| 392 // Manager entry. |
| 393 AddDevice("stub_cellular_device1", flimflam::kTypeCellular, |
| 394 "/device/cellular1", "/stub"); |
| 395 } |
| 396 |
378 void PassStubDeviceProperties(const dbus::ObjectPath& device_path, | 397 void PassStubDeviceProperties(const dbus::ObjectPath& device_path, |
379 const DictionaryValueCallback& callback) const { | 398 const DictionaryValueCallback& callback) const { |
380 const base::DictionaryValue* device_properties = NULL; | 399 const base::DictionaryValue* device_properties = NULL; |
381 if (!stub_devices_.GetDictionary(device_path.value(), &device_properties)) { | 400 if (!stub_devices_.GetDictionaryWithoutPathExpansion( |
| 401 device_path.value(), &device_properties)) { |
382 base::DictionaryValue empty_dictionary; | 402 base::DictionaryValue empty_dictionary; |
383 callback.Run(DBUS_METHOD_CALL_FAILURE, empty_dictionary); | 403 callback.Run(DBUS_METHOD_CALL_FAILURE, empty_dictionary); |
384 return; | 404 return; |
385 } | 405 } |
386 callback.Run(DBUS_METHOD_CALL_SUCCESS, *device_properties); | 406 callback.Run(DBUS_METHOD_CALL_SUCCESS, *device_properties); |
387 } | 407 } |
388 | 408 |
389 // Posts a task to run a void callback with status code |status|. | 409 // Posts a task to run a void callback with status code |status|. |
390 void PostVoidCallback(const VoidDBusMethodCallback& callback, | 410 void PostVoidCallback(const VoidDBusMethodCallback& callback, |
391 DBusMethodCallStatus status) { | 411 DBusMethodCallStatus status) { |
392 MessageLoop::current()->PostTask(FROM_HERE, | 412 MessageLoop::current()->PostTask(FROM_HERE, |
393 base::Bind(callback, status)); | 413 base::Bind(callback, status)); |
394 } | 414 } |
395 | 415 |
| 416 base::DictionaryValue* GetDeviceProperties(const std::string& device_path) { |
| 417 base::DictionaryValue* properties = NULL; |
| 418 if (!stub_devices_.GetDictionaryWithoutPathExpansion( |
| 419 device_path, &properties)) { |
| 420 properties = new base::DictionaryValue; |
| 421 stub_devices_.Set(device_path, properties); |
| 422 } |
| 423 return properties; |
| 424 } |
| 425 |
396 // Dictionary of <device_name, Dictionary>. | 426 // Dictionary of <device_name, Dictionary>. |
397 base::DictionaryValue stub_devices_; | 427 base::DictionaryValue stub_devices_; |
398 | 428 |
399 // Note: This should remain the last member so it'll be destroyed and | 429 // Note: This should remain the last member so it'll be destroyed and |
400 // invalidate its weak pointers before any other members are destroyed. | 430 // invalidate its weak pointers before any other members are destroyed. |
401 base::WeakPtrFactory<ShillDeviceClientStubImpl> weak_ptr_factory_; | 431 base::WeakPtrFactory<ShillDeviceClientStubImpl> weak_ptr_factory_; |
402 | 432 |
403 DISALLOW_COPY_AND_ASSIGN(ShillDeviceClientStubImpl); | 433 DISALLOW_COPY_AND_ASSIGN(ShillDeviceClientStubImpl); |
404 }; | 434 }; |
405 | 435 |
406 } // namespace | 436 } // namespace |
407 | 437 |
408 ShillDeviceClient::ShillDeviceClient() {} | 438 ShillDeviceClient::ShillDeviceClient() {} |
409 | 439 |
410 ShillDeviceClient::~ShillDeviceClient() {} | 440 ShillDeviceClient::~ShillDeviceClient() {} |
411 | 441 |
412 // static | 442 // static |
413 ShillDeviceClient* ShillDeviceClient::Create( | 443 ShillDeviceClient* ShillDeviceClient::Create( |
414 DBusClientImplementationType type, | 444 DBusClientImplementationType type, |
415 dbus::Bus* bus) { | 445 dbus::Bus* bus) { |
416 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | 446 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
417 return new ShillDeviceClientImpl(bus); | 447 return new ShillDeviceClientImpl(bus); |
418 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | 448 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
419 return new ShillDeviceClientStubImpl(); | 449 return new ShillDeviceClientStubImpl(); |
420 } | 450 } |
421 | 451 |
422 } // namespace chromeos | 452 } // namespace chromeos |
OLD | NEW |