| 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 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 | 219 |
| 220 // A stub implementation of ShillDeviceClient. | 220 // A stub implementation of ShillDeviceClient. |
| 221 // Implemented: Stub cellular device for SMS testing. | 221 // Implemented: Stub cellular device for SMS testing. |
| 222 class ShillDeviceClientStubImpl : public ShillDeviceClient, | 222 class ShillDeviceClientStubImpl : public ShillDeviceClient, |
| 223 public ShillDeviceClient::TestInterface { | 223 public ShillDeviceClient::TestInterface { |
| 224 public: | 224 public: |
| 225 ShillDeviceClientStubImpl() : weak_ptr_factory_(this) { | 225 ShillDeviceClientStubImpl() : weak_ptr_factory_(this) { |
| 226 SetDefaultProperties(); | 226 SetDefaultProperties(); |
| 227 } | 227 } |
| 228 | 228 |
| 229 virtual ~ShillDeviceClientStubImpl() {} | 229 virtual ~ShillDeviceClientStubImpl() { |
| 230 STLDeleteContainerPairSecondPointers( |
| 231 observer_list_.begin(), observer_list_.end()); |
| 232 } |
| 230 | 233 |
| 231 // ShillDeviceClient overrides. | 234 // ShillDeviceClient overrides. |
| 232 | 235 |
| 233 virtual void AddPropertyChangedObserver( | 236 virtual void AddPropertyChangedObserver( |
| 234 const dbus::ObjectPath& device_path, | 237 const dbus::ObjectPath& device_path, |
| 235 ShillPropertyChangedObserver* observer) OVERRIDE {} | 238 ShillPropertyChangedObserver* observer) OVERRIDE { |
| 239 GetObserverList(device_path).AddObserver(observer); |
| 240 } |
| 236 | 241 |
| 237 virtual void RemovePropertyChangedObserver( | 242 virtual void RemovePropertyChangedObserver( |
| 238 const dbus::ObjectPath& device_path, | 243 const dbus::ObjectPath& device_path, |
| 239 ShillPropertyChangedObserver* observer) OVERRIDE {} | 244 ShillPropertyChangedObserver* observer) OVERRIDE { |
| 245 GetObserverList(device_path).RemoveObserver(observer); |
| 246 } |
| 240 | 247 |
| 241 virtual void GetProperties(const dbus::ObjectPath& device_path, | 248 virtual void GetProperties(const dbus::ObjectPath& device_path, |
| 242 const DictionaryValueCallback& callback) OVERRIDE { | 249 const DictionaryValueCallback& callback) OVERRIDE { |
| 243 MessageLoop::current()->PostTask( | 250 MessageLoop::current()->PostTask( |
| 244 FROM_HERE, | 251 FROM_HERE, |
| 245 base::Bind(&ShillDeviceClientStubImpl::PassStubDeviceProperties, | 252 base::Bind(&ShillDeviceClientStubImpl::PassStubDeviceProperties, |
| 246 weak_ptr_factory_.GetWeakPtr(), | 253 weak_ptr_factory_.GetWeakPtr(), |
| 247 device_path, callback)); | 254 device_path, callback)); |
| 248 } | 255 } |
| 249 | 256 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 270 std::string error_name("org.chromium.flimflam.Error.Failure"); | 277 std::string error_name("org.chromium.flimflam.Error.Failure"); |
| 271 std::string error_message("Failed"); | 278 std::string error_message("Failed"); |
| 272 MessageLoop::current()->PostTask(FROM_HERE, | 279 MessageLoop::current()->PostTask(FROM_HERE, |
| 273 base::Bind(error_callback, | 280 base::Bind(error_callback, |
| 274 error_name, | 281 error_name, |
| 275 error_message)); | 282 error_message)); |
| 276 return; | 283 return; |
| 277 } | 284 } |
| 278 device_properties->Set(name, value.DeepCopy()); | 285 device_properties->Set(name, value.DeepCopy()); |
| 279 MessageLoop::current()->PostTask(FROM_HERE, callback); | 286 MessageLoop::current()->PostTask(FROM_HERE, callback); |
| 287 MessageLoop::current()->PostTask( |
| 288 FROM_HERE, |
| 289 base::Bind(&ShillDeviceClientStubImpl::NotifyObserversPropertyChanged, |
| 290 weak_ptr_factory_.GetWeakPtr(), device_path, name)); |
| 280 } | 291 } |
| 281 | 292 |
| 282 virtual void ClearProperty(const dbus::ObjectPath& device_path, | 293 virtual void ClearProperty(const dbus::ObjectPath& device_path, |
| 283 const std::string& name, | 294 const std::string& name, |
| 284 const VoidDBusMethodCallback& callback) OVERRIDE { | 295 const VoidDBusMethodCallback& callback) OVERRIDE { |
| 285 base::DictionaryValue* device_properties = NULL; | 296 base::DictionaryValue* device_properties = NULL; |
| 286 if (!stub_devices_.GetDictionary(device_path.value(), &device_properties)) { | 297 if (!stub_devices_.GetDictionary(device_path.value(), &device_properties)) { |
| 287 PostVoidCallback(callback, DBUS_METHOD_CALL_FAILURE); | 298 PostVoidCallback(callback, DBUS_METHOD_CALL_FAILURE); |
| 288 return; | 299 return; |
| 289 } | 300 } |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 | 387 |
| 377 virtual void RemoveDevice(const std::string& device_path) OVERRIDE { | 388 virtual void RemoveDevice(const std::string& device_path) OVERRIDE { |
| 378 stub_devices_.RemoveWithoutPathExpansion(device_path, NULL); | 389 stub_devices_.RemoveWithoutPathExpansion(device_path, NULL); |
| 379 } | 390 } |
| 380 | 391 |
| 381 virtual void ClearDevices() OVERRIDE { | 392 virtual void ClearDevices() OVERRIDE { |
| 382 stub_devices_.Clear(); | 393 stub_devices_.Clear(); |
| 383 } | 394 } |
| 384 | 395 |
| 385 private: | 396 private: |
| 397 typedef ObserverList<ShillPropertyChangedObserver> PropertyObserverList; |
| 398 |
| 386 void SetDefaultProperties() { | 399 void SetDefaultProperties() { |
| 387 // Add a wifi device. Note: path matches Manager entry. | 400 // Add a wifi device. Note: path matches Manager entry. |
| 388 AddDevice("stub_wifi_device1", flimflam::kTypeWifi, | 401 AddDevice("stub_wifi_device1", flimflam::kTypeWifi, |
| 389 "/device/wifi1", "/stub"); | 402 "/device/wifi1", "/stub"); |
| 390 | 403 |
| 391 // Add a cellular device. Used in SMS stub. Note: path matches | 404 // Add a cellular device. Used in SMS stub. Note: path matches |
| 392 // Manager entry. | 405 // Manager entry. |
| 393 AddDevice("stub_cellular_device1", flimflam::kTypeCellular, | 406 AddDevice("stub_cellular_device1", flimflam::kTypeCellular, |
| 394 "/device/cellular1", "/stub"); | 407 "/device/cellular1", "/stub"); |
| 395 } | 408 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 406 callback.Run(DBUS_METHOD_CALL_SUCCESS, *device_properties); | 419 callback.Run(DBUS_METHOD_CALL_SUCCESS, *device_properties); |
| 407 } | 420 } |
| 408 | 421 |
| 409 // Posts a task to run a void callback with status code |status|. | 422 // Posts a task to run a void callback with status code |status|. |
| 410 void PostVoidCallback(const VoidDBusMethodCallback& callback, | 423 void PostVoidCallback(const VoidDBusMethodCallback& callback, |
| 411 DBusMethodCallStatus status) { | 424 DBusMethodCallStatus status) { |
| 412 MessageLoop::current()->PostTask(FROM_HERE, | 425 MessageLoop::current()->PostTask(FROM_HERE, |
| 413 base::Bind(callback, status)); | 426 base::Bind(callback, status)); |
| 414 } | 427 } |
| 415 | 428 |
| 429 void NotifyObserversPropertyChanged(const dbus::ObjectPath& device_path, |
| 430 const std::string& property) { |
| 431 base::DictionaryValue* dict = NULL; |
| 432 std::string path = device_path.value(); |
| 433 if (!stub_devices_.GetDictionaryWithoutPathExpansion(path, &dict)) { |
| 434 LOG(ERROR) << "Notify for unknown service: " << path; |
| 435 return; |
| 436 } |
| 437 base::Value* value = NULL; |
| 438 if (!dict->GetWithoutPathExpansion(property, &value)) { |
| 439 LOG(ERROR) << "Notify for unknown property: " |
| 440 << path << " : " << property; |
| 441 return; |
| 442 } |
| 443 FOR_EACH_OBSERVER(ShillPropertyChangedObserver, |
| 444 GetObserverList(device_path), |
| 445 OnPropertyChanged(property, *value)); |
| 446 } |
| 447 |
| 416 base::DictionaryValue* GetDeviceProperties(const std::string& device_path) { | 448 base::DictionaryValue* GetDeviceProperties(const std::string& device_path) { |
| 417 base::DictionaryValue* properties = NULL; | 449 base::DictionaryValue* properties = NULL; |
| 418 if (!stub_devices_.GetDictionaryWithoutPathExpansion( | 450 if (!stub_devices_.GetDictionaryWithoutPathExpansion( |
| 419 device_path, &properties)) { | 451 device_path, &properties)) { |
| 420 properties = new base::DictionaryValue; | 452 properties = new base::DictionaryValue; |
| 421 stub_devices_.Set(device_path, properties); | 453 stub_devices_.Set(device_path, properties); |
| 422 } | 454 } |
| 423 return properties; | 455 return properties; |
| 424 } | 456 } |
| 425 | 457 |
| 458 PropertyObserverList& GetObserverList(const dbus::ObjectPath& device_path) { |
| 459 std::map<dbus::ObjectPath, PropertyObserverList*>::iterator iter = |
| 460 observer_list_.find(device_path); |
| 461 if (iter != observer_list_.end()) |
| 462 return *(iter->second); |
| 463 PropertyObserverList* observer_list = new PropertyObserverList(); |
| 464 observer_list_[device_path] = observer_list; |
| 465 return *observer_list; |
| 466 } |
| 467 |
| 426 // Dictionary of <device_name, Dictionary>. | 468 // Dictionary of <device_name, Dictionary>. |
| 427 base::DictionaryValue stub_devices_; | 469 base::DictionaryValue stub_devices_; |
| 470 // Observer list for each device. |
| 471 std::map<dbus::ObjectPath, PropertyObserverList*> observer_list_; |
| 428 | 472 |
| 429 // Note: This should remain the last member so it'll be destroyed and | 473 // Note: This should remain the last member so it'll be destroyed and |
| 430 // invalidate its weak pointers before any other members are destroyed. | 474 // invalidate its weak pointers before any other members are destroyed. |
| 431 base::WeakPtrFactory<ShillDeviceClientStubImpl> weak_ptr_factory_; | 475 base::WeakPtrFactory<ShillDeviceClientStubImpl> weak_ptr_factory_; |
| 432 | 476 |
| 433 DISALLOW_COPY_AND_ASSIGN(ShillDeviceClientStubImpl); | 477 DISALLOW_COPY_AND_ASSIGN(ShillDeviceClientStubImpl); |
| 434 }; | 478 }; |
| 435 | 479 |
| 436 } // namespace | 480 } // namespace |
| 437 | 481 |
| 438 ShillDeviceClient::ShillDeviceClient() {} | 482 ShillDeviceClient::ShillDeviceClient() {} |
| 439 | 483 |
| 440 ShillDeviceClient::~ShillDeviceClient() {} | 484 ShillDeviceClient::~ShillDeviceClient() {} |
| 441 | 485 |
| 442 // static | 486 // static |
| 443 ShillDeviceClient* ShillDeviceClient::Create( | 487 ShillDeviceClient* ShillDeviceClient::Create( |
| 444 DBusClientImplementationType type, | 488 DBusClientImplementationType type, |
| 445 dbus::Bus* bus) { | 489 dbus::Bus* bus) { |
| 446 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | 490 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
| 447 return new ShillDeviceClientImpl(bus); | 491 return new ShillDeviceClientImpl(bus); |
| 448 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | 492 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
| 449 return new ShillDeviceClientStubImpl(); | 493 return new ShillDeviceClientStubImpl(); |
| 450 } | 494 } |
| 451 | 495 |
| 452 } // namespace chromeos | 496 } // namespace chromeos |
| OLD | NEW |