| 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 { |
| 236 observer_list_.AddObserver(observer); |
| 237 } |
| 260 | 238 |
| 261 virtual void RemovePropertyChangedObserver( | 239 virtual void RemovePropertyChangedObserver( |
| 262 const dbus::ObjectPath& device_path, | 240 const dbus::ObjectPath& device_path, |
| 263 ShillPropertyChangedObserver* observer) OVERRIDE {} | 241 ShillPropertyChangedObserver* observer) OVERRIDE { |
| 242 observer_list_.RemoveObserver(observer); |
| 243 } |
| 264 | 244 |
| 265 virtual void GetProperties(const dbus::ObjectPath& device_path, | 245 virtual void GetProperties(const dbus::ObjectPath& device_path, |
| 266 const DictionaryValueCallback& callback) OVERRIDE { | 246 const DictionaryValueCallback& callback) OVERRIDE { |
| 267 MessageLoop::current()->PostTask( | 247 MessageLoop::current()->PostTask( |
| 268 FROM_HERE, | 248 FROM_HERE, |
| 269 base::Bind(&ShillDeviceClientStubImpl::PassStubDeviceProperties, | 249 base::Bind(&ShillDeviceClientStubImpl::PassStubDeviceProperties, |
| 270 weak_ptr_factory_.GetWeakPtr(), | 250 weak_ptr_factory_.GetWeakPtr(), |
| 271 device_path, callback)); | 251 device_path, callback)); |
| 272 } | 252 } |
| 273 | 253 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 291 std::string error_name("org.chromium.flimflam.Error.Failure"); | 271 std::string error_name("org.chromium.flimflam.Error.Failure"); |
| 292 std::string error_message("Failed"); | 272 std::string error_message("Failed"); |
| 293 MessageLoop::current()->PostTask(FROM_HERE, | 273 MessageLoop::current()->PostTask(FROM_HERE, |
| 294 base::Bind(error_callback, | 274 base::Bind(error_callback, |
| 295 error_name, | 275 error_name, |
| 296 error_message)); | 276 error_message)); |
| 297 return; | 277 return; |
| 298 } | 278 } |
| 299 device_properties->Set(name, value.DeepCopy()); | 279 device_properties->Set(name, value.DeepCopy()); |
| 300 MessageLoop::current()->PostTask(FROM_HERE, callback); | 280 MessageLoop::current()->PostTask(FROM_HERE, callback); |
| 281 MessageLoop::current()->PostTask( |
| 282 FROM_HERE, |
| 283 base::Bind(&ShillDeviceClientStubImpl::NotifyObserversPropertyChanged, |
| 284 weak_ptr_factory_.GetWeakPtr(), device_path, name)); |
| 301 } | 285 } |
| 302 | 286 |
| 303 virtual void ClearProperty(const dbus::ObjectPath& device_path, | 287 virtual void ClearProperty(const dbus::ObjectPath& device_path, |
| 304 const std::string& name, | 288 const std::string& name, |
| 305 const VoidDBusMethodCallback& callback) OVERRIDE { | 289 const VoidDBusMethodCallback& callback) OVERRIDE { |
| 306 base::DictionaryValue* device_properties = NULL; | 290 base::DictionaryValue* device_properties = NULL; |
| 307 if (!stub_devices_.GetDictionary(device_path.value(), &device_properties)) { | 291 if (!stub_devices_.GetDictionary(device_path.value(), &device_properties)) { |
| 308 PostVoidCallback(callback, DBUS_METHOD_CALL_FAILURE); | 292 PostVoidCallback(callback, DBUS_METHOD_CALL_FAILURE); |
| 309 return; | 293 return; |
| 310 } | 294 } |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 MessageLoop::current()->PostTask(FROM_HERE, callback); | 343 MessageLoop::current()->PostTask(FROM_HERE, callback); |
| 360 } | 344 } |
| 361 | 345 |
| 362 virtual void Register(const dbus::ObjectPath& device_path, | 346 virtual void Register(const dbus::ObjectPath& device_path, |
| 363 const std::string& network_id, | 347 const std::string& network_id, |
| 364 const base::Closure& callback, | 348 const base::Closure& callback, |
| 365 const ErrorCallback& error_callback) OVERRIDE { | 349 const ErrorCallback& error_callback) OVERRIDE { |
| 366 MessageLoop::current()->PostTask(FROM_HERE, callback); | 350 MessageLoop::current()->PostTask(FROM_HERE, callback); |
| 367 } | 351 } |
| 368 | 352 |
| 369 // ShillDeviceClient override. | |
| 370 virtual void SetCarrier(const dbus::ObjectPath& device_path, | 353 virtual void SetCarrier(const dbus::ObjectPath& device_path, |
| 371 const std::string& carrier, | 354 const std::string& carrier, |
| 372 const base::Closure& callback, | 355 const base::Closure& callback, |
| 373 const ErrorCallback& error_callback) OVERRIDE { | 356 const ErrorCallback& error_callback) OVERRIDE { |
| 374 MessageLoop::current()->PostTask(FROM_HERE, callback); | 357 MessageLoop::current()->PostTask(FROM_HERE, callback); |
| 375 } | 358 } |
| 376 | 359 |
| 360 virtual ShillDeviceClient::TestInterface* GetTestInterface() OVERRIDE { |
| 361 return this; |
| 362 } |
| 363 |
| 364 // ShillDeviceClient::TestInterface overrides. |
| 365 |
| 366 virtual void AddDevice(const std::string& device_path, |
| 367 const std::string& type, |
| 368 const std::string& object_path, |
| 369 const std::string& connection_path) OVERRIDE { |
| 370 base::DictionaryValue* properties = GetDeviceProperties(device_path); |
| 371 properties->SetWithoutPathExpansion( |
| 372 flimflam::kTypeProperty, |
| 373 base::Value::CreateStringValue(type)); |
| 374 properties->SetWithoutPathExpansion( |
| 375 flimflam::kDBusObjectProperty, |
| 376 base::Value::CreateStringValue(object_path)); |
| 377 properties->SetWithoutPathExpansion( |
| 378 flimflam::kDBusConnectionProperty, |
| 379 base::Value::CreateStringValue(connection_path)); |
| 380 } |
| 381 |
| 382 virtual void RemoveDevice(const std::string& device_path) OVERRIDE { |
| 383 stub_devices_.RemoveWithoutPathExpansion(device_path, NULL); |
| 384 } |
| 385 |
| 386 virtual void ClearDevices() OVERRIDE { |
| 387 stub_devices_.Clear(); |
| 388 } |
| 389 |
| 377 private: | 390 private: |
| 391 void SetDefaultProperties() { |
| 392 // Add a wifi device. Note: path matches Manager entry. |
| 393 AddDevice("stub_wifi_device1", flimflam::kTypeWifi, |
| 394 "/device/wifi1", "/stub"); |
| 395 |
| 396 // Add a cellular device. Used in SMS stub. Note: path matches |
| 397 // Manager entry. |
| 398 AddDevice("stub_cellular_device1", flimflam::kTypeCellular, |
| 399 "/device/cellular1", "/stub"); |
| 400 } |
| 401 |
| 378 void PassStubDeviceProperties(const dbus::ObjectPath& device_path, | 402 void PassStubDeviceProperties(const dbus::ObjectPath& device_path, |
| 379 const DictionaryValueCallback& callback) const { | 403 const DictionaryValueCallback& callback) const { |
| 380 const base::DictionaryValue* device_properties = NULL; | 404 const base::DictionaryValue* device_properties = NULL; |
| 381 if (!stub_devices_.GetDictionary(device_path.value(), &device_properties)) { | 405 if (!stub_devices_.GetDictionaryWithoutPathExpansion( |
| 406 device_path.value(), &device_properties)) { |
| 382 base::DictionaryValue empty_dictionary; | 407 base::DictionaryValue empty_dictionary; |
| 383 callback.Run(DBUS_METHOD_CALL_FAILURE, empty_dictionary); | 408 callback.Run(DBUS_METHOD_CALL_FAILURE, empty_dictionary); |
| 384 return; | 409 return; |
| 385 } | 410 } |
| 386 callback.Run(DBUS_METHOD_CALL_SUCCESS, *device_properties); | 411 callback.Run(DBUS_METHOD_CALL_SUCCESS, *device_properties); |
| 387 } | 412 } |
| 388 | 413 |
| 389 // Posts a task to run a void callback with status code |status|. | 414 // Posts a task to run a void callback with status code |status|. |
| 390 void PostVoidCallback(const VoidDBusMethodCallback& callback, | 415 void PostVoidCallback(const VoidDBusMethodCallback& callback, |
| 391 DBusMethodCallStatus status) { | 416 DBusMethodCallStatus status) { |
| 392 MessageLoop::current()->PostTask(FROM_HERE, | 417 MessageLoop::current()->PostTask(FROM_HERE, |
| 393 base::Bind(callback, status)); | 418 base::Bind(callback, status)); |
| 394 } | 419 } |
| 395 | 420 |
| 421 void NotifyObserversPropertyChanged(const dbus::ObjectPath& device_path, |
| 422 const std::string& property) { |
| 423 base::DictionaryValue* dict = NULL; |
| 424 std::string path = device_path.value(); |
| 425 if (!stub_devices_.GetDictionaryWithoutPathExpansion(path, &dict)) { |
| 426 LOG(ERROR) << "Notify for unknown service: " << path; |
| 427 return; |
| 428 } |
| 429 base::Value* value = NULL; |
| 430 if (!dict->GetWithoutPathExpansion(property, &value)) { |
| 431 LOG(ERROR) << "Notify for unknown property: " |
| 432 << path << " : " << property; |
| 433 return; |
| 434 } |
| 435 FOR_EACH_OBSERVER(ShillPropertyChangedObserver, |
| 436 observer_list_, |
| 437 OnPropertyChanged(property, *value)); |
| 438 } |
| 439 |
| 440 base::DictionaryValue* GetDeviceProperties(const std::string& device_path) { |
| 441 base::DictionaryValue* properties = NULL; |
| 442 if (!stub_devices_.GetDictionaryWithoutPathExpansion( |
| 443 device_path, &properties)) { |
| 444 properties = new base::DictionaryValue; |
| 445 stub_devices_.Set(device_path, properties); |
| 446 } |
| 447 return properties; |
| 448 } |
| 449 |
| 396 // Dictionary of <device_name, Dictionary>. | 450 // Dictionary of <device_name, Dictionary>. |
| 397 base::DictionaryValue stub_devices_; | 451 base::DictionaryValue stub_devices_; |
| 452 ObserverList<ShillPropertyChangedObserver> observer_list_; |
| 398 | 453 |
| 399 // Note: This should remain the last member so it'll be destroyed and | 454 // Note: This should remain the last member so it'll be destroyed and |
| 400 // invalidate its weak pointers before any other members are destroyed. | 455 // invalidate its weak pointers before any other members are destroyed. |
| 401 base::WeakPtrFactory<ShillDeviceClientStubImpl> weak_ptr_factory_; | 456 base::WeakPtrFactory<ShillDeviceClientStubImpl> weak_ptr_factory_; |
| 402 | 457 |
| 403 DISALLOW_COPY_AND_ASSIGN(ShillDeviceClientStubImpl); | 458 DISALLOW_COPY_AND_ASSIGN(ShillDeviceClientStubImpl); |
| 404 }; | 459 }; |
| 405 | 460 |
| 406 } // namespace | 461 } // namespace |
| 407 | 462 |
| 408 ShillDeviceClient::ShillDeviceClient() {} | 463 ShillDeviceClient::ShillDeviceClient() {} |
| 409 | 464 |
| 410 ShillDeviceClient::~ShillDeviceClient() {} | 465 ShillDeviceClient::~ShillDeviceClient() {} |
| 411 | 466 |
| 412 // static | 467 // static |
| 413 ShillDeviceClient* ShillDeviceClient::Create( | 468 ShillDeviceClient* ShillDeviceClient::Create( |
| 414 DBusClientImplementationType type, | 469 DBusClientImplementationType type, |
| 415 dbus::Bus* bus) { | 470 dbus::Bus* bus) { |
| 416 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | 471 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
| 417 return new ShillDeviceClientImpl(bus); | 472 return new ShillDeviceClientImpl(bus); |
| 418 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | 473 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
| 419 return new ShillDeviceClientStubImpl(); | 474 return new ShillDeviceClientStubImpl(); |
| 420 } | 475 } |
| 421 | 476 |
| 422 } // namespace chromeos | 477 } // namespace chromeos |
| OLD | NEW |