Chromium Code Reviews| 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() { | |
|
hashimoto
2012/11/01 04:54:34
nit: OVERRIDE
stevenjb
2012/11/01 18:45:59
Done.
| |
| 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 { |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 359 MessageLoop::current()->PostTask(FROM_HERE, callback); | 335 MessageLoop::current()->PostTask(FROM_HERE, callback); |
| 360 } | 336 } |
| 361 | 337 |
| 362 virtual void Register(const dbus::ObjectPath& device_path, | 338 virtual void Register(const dbus::ObjectPath& device_path, |
| 363 const std::string& network_id, | 339 const std::string& network_id, |
| 364 const base::Closure& callback, | 340 const base::Closure& callback, |
| 365 const ErrorCallback& error_callback) OVERRIDE { | 341 const ErrorCallback& error_callback) OVERRIDE { |
| 366 MessageLoop::current()->PostTask(FROM_HERE, callback); | 342 MessageLoop::current()->PostTask(FROM_HERE, callback); |
| 367 } | 343 } |
| 368 | 344 |
| 369 // ShillDeviceClient override. | |
| 370 virtual void SetCarrier(const dbus::ObjectPath& device_path, | 345 virtual void SetCarrier(const dbus::ObjectPath& device_path, |
| 371 const std::string& carrier, | 346 const std::string& carrier, |
| 372 const base::Closure& callback, | 347 const base::Closure& callback, |
| 373 const ErrorCallback& error_callback) OVERRIDE { | 348 const ErrorCallback& error_callback) OVERRIDE { |
| 374 MessageLoop::current()->PostTask(FROM_HERE, callback); | 349 MessageLoop::current()->PostTask(FROM_HERE, callback); |
| 375 } | 350 } |
| 376 | 351 |
| 352 virtual ShillDeviceClient::TestInterface* GetTestInterface() { | |
|
hashimoto
2012/11/01 04:54:34
nit: OVERRIDE
stevenjb
2012/11/01 18:45:59
Done.
| |
| 353 return this; | |
| 354 } | |
| 355 | |
| 356 // ShillDeviceClient::TestInterface overrides. | |
| 357 | |
| 358 virtual void AddDevice(const std::string& device_path, | |
| 359 const std::string& type, | |
| 360 const std::string& object_path, | |
| 361 const std::string& connection_path) OVERRIDE { | |
| 362 base::DictionaryValue* properties = GetDeviceProperties(device_path); | |
| 363 properties->SetWithoutPathExpansion( | |
| 364 flimflam::kTypeProperty, | |
| 365 base::Value::CreateStringValue(type)); | |
| 366 properties->SetWithoutPathExpansion( | |
| 367 flimflam::kDBusObjectProperty, | |
| 368 base::Value::CreateStringValue(object_path)); | |
| 369 properties->SetWithoutPathExpansion( | |
| 370 flimflam::kDBusConnectionProperty, | |
| 371 base::Value::CreateStringValue(connection_path)); | |
| 372 } | |
| 373 | |
| 374 virtual void RemoveDevice(const std::string& device_path) { | |
|
hashimoto
2012/11/01 04:54:34
nit: OVERRIDE
stevenjb
2012/11/01 18:45:59
Done.
| |
| 375 stub_devices_.RemoveWithoutPathExpansion(device_path, NULL); | |
| 376 } | |
| 377 | |
| 378 virtual void ClearDevices() OVERRIDE { | |
| 379 stub_devices_.Clear(); | |
| 380 } | |
| 381 | |
| 377 private: | 382 private: |
| 383 void SetDefaultProperties() { | |
| 384 // Add a wifi device. Note: path matches Manager entry. | |
| 385 AddDevice("stub_wifi_device1", flimflam::kTypeWifi, | |
| 386 "/device/wifi1", "/stub"); | |
| 387 | |
| 388 // Add a cellular device. Used in SMS stub. Note: path matches | |
| 389 // Manager entry. | |
| 390 AddDevice("stub_cellular_device1", flimflam::kTypeCellular, | |
| 391 "/device/cellular1", "/stub"); | |
| 392 } | |
| 393 | |
| 378 void PassStubDeviceProperties(const dbus::ObjectPath& device_path, | 394 void PassStubDeviceProperties(const dbus::ObjectPath& device_path, |
| 379 const DictionaryValueCallback& callback) const { | 395 const DictionaryValueCallback& callback) const { |
|
hashimoto
2012/11/01 04:54:34
nit: Please fix this indent while you are here.
stevenjb
2012/11/01 18:45:59
Done.
| |
| 380 const base::DictionaryValue* device_properties = NULL; | 396 const base::DictionaryValue* device_properties = NULL; |
| 381 if (!stub_devices_.GetDictionary(device_path.value(), &device_properties)) { | 397 if (!stub_devices_.GetDictionaryWithoutPathExpansion( |
| 398 device_path.value(), &device_properties)) { | |
| 382 base::DictionaryValue empty_dictionary; | 399 base::DictionaryValue empty_dictionary; |
| 383 callback.Run(DBUS_METHOD_CALL_FAILURE, empty_dictionary); | 400 callback.Run(DBUS_METHOD_CALL_FAILURE, empty_dictionary); |
| 384 return; | 401 return; |
| 385 } | 402 } |
| 386 callback.Run(DBUS_METHOD_CALL_SUCCESS, *device_properties); | 403 callback.Run(DBUS_METHOD_CALL_SUCCESS, *device_properties); |
| 387 } | 404 } |
| 388 | 405 |
| 389 // Posts a task to run a void callback with status code |status|. | 406 // Posts a task to run a void callback with status code |status|. |
| 390 void PostVoidCallback(const VoidDBusMethodCallback& callback, | 407 void PostVoidCallback(const VoidDBusMethodCallback& callback, |
| 391 DBusMethodCallStatus status) { | 408 DBusMethodCallStatus status) { |
| 392 MessageLoop::current()->PostTask(FROM_HERE, | 409 MessageLoop::current()->PostTask(FROM_HERE, |
| 393 base::Bind(callback, status)); | 410 base::Bind(callback, status)); |
| 394 } | 411 } |
| 395 | 412 |
| 413 base::DictionaryValue* GetDeviceProperties(const std::string& device_path) { | |
| 414 base::DictionaryValue* properties; | |
|
hashimoto
2012/11/01 04:54:34
nit: = NULL;
stevenjb
2012/11/01 18:45:59
Good catch. Done.
| |
| 415 if (!stub_devices_.GetDictionaryWithoutPathExpansion( | |
| 416 device_path, &properties)) { | |
| 417 properties = new base::DictionaryValue; | |
| 418 stub_devices_.Set(device_path, properties); | |
| 419 } | |
| 420 return properties; | |
| 421 } | |
| 422 | |
| 396 // Dictionary of <device_name, Dictionary>. | 423 // Dictionary of <device_name, Dictionary>. |
| 397 base::DictionaryValue stub_devices_; | 424 base::DictionaryValue stub_devices_; |
| 398 | 425 |
| 399 // Note: This should remain the last member so it'll be destroyed and | 426 // Note: This should remain the last member so it'll be destroyed and |
| 400 // invalidate its weak pointers before any other members are destroyed. | 427 // invalidate its weak pointers before any other members are destroyed. |
| 401 base::WeakPtrFactory<ShillDeviceClientStubImpl> weak_ptr_factory_; | 428 base::WeakPtrFactory<ShillDeviceClientStubImpl> weak_ptr_factory_; |
| 402 | 429 |
| 403 DISALLOW_COPY_AND_ASSIGN(ShillDeviceClientStubImpl); | 430 DISALLOW_COPY_AND_ASSIGN(ShillDeviceClientStubImpl); |
| 404 }; | 431 }; |
| 405 | 432 |
| 406 } // namespace | 433 } // namespace |
| 407 | 434 |
| 408 ShillDeviceClient::ShillDeviceClient() {} | 435 ShillDeviceClient::ShillDeviceClient() {} |
| 409 | 436 |
| 410 ShillDeviceClient::~ShillDeviceClient() {} | 437 ShillDeviceClient::~ShillDeviceClient() {} |
| 411 | 438 |
| 412 // static | 439 // static |
| 413 ShillDeviceClient* ShillDeviceClient::Create( | 440 ShillDeviceClient* ShillDeviceClient::Create( |
| 414 DBusClientImplementationType type, | 441 DBusClientImplementationType type, |
| 415 dbus::Bus* bus) { | 442 dbus::Bus* bus) { |
| 416 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | 443 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
| 417 return new ShillDeviceClientImpl(bus); | 444 return new ShillDeviceClientImpl(bus); |
| 418 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | 445 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
| 419 return new ShillDeviceClientStubImpl(); | 446 return new ShillDeviceClientStubImpl(); |
| 420 } | 447 } |
| 421 | 448 |
| 422 } // namespace chromeos | 449 } // namespace chromeos |
| OLD | NEW |