| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chromeos/dbus/flimflam_device_client.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/message_loop.h" | |
| 9 #include "base/stl_util.h" | |
| 10 #include "base/values.h" | |
| 11 #include "dbus/bus.h" | |
| 12 #include "dbus/message.h" | |
| 13 #include "dbus/object_path.h" | |
| 14 #include "dbus/object_proxy.h" | |
| 15 #include "dbus/values_util.h" | |
| 16 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 17 | |
| 18 namespace chromeos { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 // The FlimflamDeviceClient implementation. | |
| 23 class FlimflamDeviceClientImpl : public FlimflamDeviceClient { | |
| 24 public: | |
| 25 explicit FlimflamDeviceClientImpl(dbus::Bus* bus) | |
| 26 : bus_(bus), | |
| 27 helpers_deleter_(&helpers_) { | |
| 28 } | |
| 29 | |
| 30 /////////////////////////////////////// | |
| 31 // FlimflamDeviceClient overrides. | |
| 32 virtual void SetPropertyChangedHandler( | |
| 33 const dbus::ObjectPath& device_path, | |
| 34 const PropertyChangedHandler& handler) OVERRIDE { | |
| 35 GetHelper(device_path)->SetPropertyChangedHandler(handler); | |
| 36 } | |
| 37 | |
| 38 virtual void ResetPropertyChangedHandler( | |
| 39 const dbus::ObjectPath& device_path) OVERRIDE { | |
| 40 GetHelper(device_path)->ResetPropertyChangedHandler(); | |
| 41 } | |
| 42 | |
| 43 virtual void GetProperties(const dbus::ObjectPath& device_path, | |
| 44 const DictionaryValueCallback& callback) OVERRIDE { | |
| 45 dbus::MethodCall method_call(flimflam::kFlimflamDeviceInterface, | |
| 46 flimflam::kGetPropertiesFunction); | |
| 47 GetHelper(device_path)->CallDictionaryValueMethod(&method_call, callback); | |
| 48 } | |
| 49 | |
| 50 virtual base::DictionaryValue* CallGetPropertiesAndBlock( | |
| 51 const dbus::ObjectPath& device_path) OVERRIDE { | |
| 52 dbus::MethodCall method_call(flimflam::kFlimflamDeviceInterface, | |
| 53 flimflam::kGetPropertiesFunction); | |
| 54 return GetHelper(device_path)->CallDictionaryValueMethodAndBlock( | |
| 55 &method_call); | |
| 56 } | |
| 57 | |
| 58 virtual void ProposeScan(const dbus::ObjectPath& device_path, | |
| 59 const VoidDBusMethodCallback& callback) OVERRIDE { | |
| 60 dbus::MethodCall method_call(flimflam::kFlimflamDeviceInterface, | |
| 61 flimflam::kProposeScanFunction); | |
| 62 GetHelper(device_path)->CallVoidMethod(&method_call, callback); | |
| 63 } | |
| 64 | |
| 65 virtual void SetProperty(const dbus::ObjectPath& device_path, | |
| 66 const std::string& name, | |
| 67 const base::Value& value, | |
| 68 const VoidDBusMethodCallback& callback) OVERRIDE { | |
| 69 dbus::MethodCall method_call(flimflam::kFlimflamDeviceInterface, | |
| 70 flimflam::kSetPropertyFunction); | |
| 71 dbus::MessageWriter writer(&method_call); | |
| 72 writer.AppendString(name); | |
| 73 FlimflamClientHelper::AppendValueDataAsVariant(&writer, value); | |
| 74 GetHelper(device_path)->CallVoidMethod(&method_call, callback); | |
| 75 } | |
| 76 | |
| 77 virtual void ClearProperty(const dbus::ObjectPath& device_path, | |
| 78 const std::string& name, | |
| 79 const VoidDBusMethodCallback& callback) OVERRIDE { | |
| 80 dbus::MethodCall method_call(flimflam::kFlimflamDeviceInterface, | |
| 81 flimflam::kClearPropertyFunction); | |
| 82 dbus::MessageWriter writer(&method_call); | |
| 83 writer.AppendString(name); | |
| 84 GetHelper(device_path)->CallVoidMethod(&method_call, callback); | |
| 85 } | |
| 86 | |
| 87 virtual void AddIPConfig( | |
| 88 const dbus::ObjectPath& device_path, | |
| 89 const std::string& method, | |
| 90 const ObjectPathDBusMethodCallback& callback) OVERRIDE { | |
| 91 dbus::MethodCall method_call(flimflam::kFlimflamDeviceInterface, | |
| 92 flimflam::kAddIPConfigFunction); | |
| 93 dbus::MessageWriter writer(&method_call); | |
| 94 writer.AppendString(method); | |
| 95 GetHelper(device_path)->CallObjectPathMethod(&method_call, callback); | |
| 96 } | |
| 97 | |
| 98 virtual dbus::ObjectPath CallAddIPConfigAndBlock( | |
| 99 const dbus::ObjectPath& device_path, | |
| 100 const std::string& method) OVERRIDE { | |
| 101 dbus::MethodCall method_call(flimflam::kFlimflamDeviceInterface, | |
| 102 flimflam::kAddIPConfigFunction); | |
| 103 dbus::MessageWriter writer(&method_call); | |
| 104 writer.AppendString(method); | |
| 105 return GetHelper(device_path)->CallObjectPathMethodAndBlock(&method_call); | |
| 106 } | |
| 107 | |
| 108 virtual void RequirePin(const dbus::ObjectPath& device_path, | |
| 109 const std::string& pin, | |
| 110 bool require, | |
| 111 const base::Closure& callback, | |
| 112 const ErrorCallback& error_callback) OVERRIDE { | |
| 113 dbus::MethodCall method_call(flimflam::kFlimflamDeviceInterface, | |
| 114 flimflam::kRequirePinFunction); | |
| 115 dbus::MessageWriter writer(&method_call); | |
| 116 writer.AppendString(pin); | |
| 117 writer.AppendBool(require); | |
| 118 GetHelper(device_path)->CallVoidMethodWithErrorCallback( | |
| 119 &method_call, callback, error_callback); | |
| 120 } | |
| 121 | |
| 122 virtual void EnterPin(const dbus::ObjectPath& device_path, | |
| 123 const std::string& pin, | |
| 124 const base::Closure& callback, | |
| 125 const ErrorCallback& error_callback) OVERRIDE { | |
| 126 dbus::MethodCall method_call(flimflam::kFlimflamDeviceInterface, | |
| 127 flimflam::kEnterPinFunction); | |
| 128 dbus::MessageWriter writer(&method_call); | |
| 129 writer.AppendString(pin); | |
| 130 GetHelper(device_path)->CallVoidMethodWithErrorCallback( | |
| 131 &method_call, callback, error_callback); | |
| 132 } | |
| 133 | |
| 134 virtual void UnblockPin(const dbus::ObjectPath& device_path, | |
| 135 const std::string& puk, | |
| 136 const std::string& pin, | |
| 137 const base::Closure& callback, | |
| 138 const ErrorCallback& error_callback) OVERRIDE { | |
| 139 dbus::MethodCall method_call(flimflam::kFlimflamDeviceInterface, | |
| 140 flimflam::kUnblockPinFunction); | |
| 141 dbus::MessageWriter writer(&method_call); | |
| 142 writer.AppendString(puk); | |
| 143 writer.AppendString(pin); | |
| 144 GetHelper(device_path)->CallVoidMethodWithErrorCallback( | |
| 145 &method_call, callback, error_callback); | |
| 146 } | |
| 147 | |
| 148 virtual void ChangePin(const dbus::ObjectPath& device_path, | |
| 149 const std::string& old_pin, | |
| 150 const std::string& new_pin, | |
| 151 const base::Closure& callback, | |
| 152 const ErrorCallback& error_callback) OVERRIDE { | |
| 153 dbus::MethodCall method_call(flimflam::kFlimflamDeviceInterface, | |
| 154 flimflam::kChangePinFunction); | |
| 155 dbus::MessageWriter writer(&method_call); | |
| 156 writer.AppendString(old_pin); | |
| 157 writer.AppendString(new_pin); | |
| 158 GetHelper(device_path)->CallVoidMethodWithErrorCallback( | |
| 159 &method_call, callback, error_callback); | |
| 160 } | |
| 161 | |
| 162 virtual void Register(const dbus::ObjectPath& device_path, | |
| 163 const std::string& network_id, | |
| 164 const base::Closure& callback, | |
| 165 const ErrorCallback& error_callback) OVERRIDE { | |
| 166 dbus::MethodCall method_call(flimflam::kFlimflamDeviceInterface, | |
| 167 flimflam::kRegisterFunction); | |
| 168 dbus::MessageWriter writer(&method_call); | |
| 169 writer.AppendString(network_id); | |
| 170 GetHelper(device_path)->CallVoidMethodWithErrorCallback( | |
| 171 &method_call, callback, error_callback); | |
| 172 } | |
| 173 | |
| 174 private: | |
| 175 typedef std::map<std::string, FlimflamClientHelper*> HelperMap; | |
| 176 | |
| 177 // Returns the corresponding FlimflamClientHelper for the profile. | |
| 178 FlimflamClientHelper* GetHelper(const dbus::ObjectPath& device_path) { | |
| 179 HelperMap::iterator it = helpers_.find(device_path.value()); | |
| 180 if (it != helpers_.end()) | |
| 181 return it->second; | |
| 182 | |
| 183 // There is no helper for the profile, create it. | |
| 184 dbus::ObjectProxy* object_proxy = | |
| 185 bus_->GetObjectProxy(flimflam::kFlimflamServiceName, device_path); | |
| 186 FlimflamClientHelper* helper = new FlimflamClientHelper(bus_, object_proxy); | |
| 187 helper->MonitorPropertyChanged(flimflam::kFlimflamDeviceInterface); | |
| 188 helpers_.insert(HelperMap::value_type(device_path.value(), helper)); | |
| 189 return helper; | |
| 190 } | |
| 191 | |
| 192 dbus::Bus* bus_; | |
| 193 HelperMap helpers_; | |
| 194 STLValueDeleter<HelperMap> helpers_deleter_; | |
| 195 | |
| 196 DISALLOW_COPY_AND_ASSIGN(FlimflamDeviceClientImpl); | |
| 197 }; | |
| 198 | |
| 199 // A stub implementation of FlimflamDeviceClient. | |
| 200 // Implemented: Stub cellular device for SMS testing. | |
| 201 class FlimflamDeviceClientStubImpl : public FlimflamDeviceClient { | |
| 202 public: | |
| 203 FlimflamDeviceClientStubImpl() : weak_ptr_factory_(this) { | |
| 204 // Add a cellular device for SMS. Note: name matches Manager entry. | |
| 205 const char kStubCellular1[] = "stub_cellular1"; | |
| 206 base::DictionaryValue* cellular_properties = new base::DictionaryValue; | |
| 207 cellular_properties->SetWithoutPathExpansion( | |
| 208 flimflam::kTypeProperty, | |
| 209 base::Value::CreateStringValue(flimflam::kTypeCellular)); | |
| 210 cellular_properties->SetWithoutPathExpansion( | |
| 211 flimflam::kDBusConnectionProperty, | |
| 212 base::Value::CreateStringValue("/stub")); | |
| 213 cellular_properties->SetWithoutPathExpansion( | |
| 214 flimflam::kDBusObjectProperty, | |
| 215 base::Value::CreateStringValue("/device/cellular1")); | |
| 216 stub_devices_.Set(kStubCellular1, cellular_properties); | |
| 217 | |
| 218 // Create a second device stubbing a modem managed by | |
| 219 // ModemManager1 interfaces. | |
| 220 // Note: name matches Manager entry. | |
| 221 const char kStubCellular2[] = "stub_cellular2"; | |
| 222 cellular_properties = new base::DictionaryValue; | |
| 223 cellular_properties->SetWithoutPathExpansion( | |
| 224 flimflam::kTypeProperty, | |
| 225 base::Value::CreateStringValue(flimflam::kTypeCellular)); | |
| 226 cellular_properties->SetWithoutPathExpansion( | |
| 227 flimflam::kDBusConnectionProperty, | |
| 228 base::Value::CreateStringValue(":stub.0")); | |
| 229 cellular_properties->SetWithoutPathExpansion( | |
| 230 flimflam::kDBusObjectProperty, | |
| 231 base::Value::CreateStringValue( | |
| 232 "/org/freedesktop/ModemManager1/stub/0")); | |
| 233 stub_devices_.Set(kStubCellular2, cellular_properties); | |
| 234 } | |
| 235 | |
| 236 virtual ~FlimflamDeviceClientStubImpl() {} | |
| 237 | |
| 238 // FlimflamDeviceClient override. | |
| 239 virtual void SetPropertyChangedHandler( | |
| 240 const dbus::ObjectPath& device_path, | |
| 241 const PropertyChangedHandler& handler) OVERRIDE {} | |
| 242 | |
| 243 // FlimflamDeviceClient override. | |
| 244 virtual void ResetPropertyChangedHandler( | |
| 245 const dbus::ObjectPath& device_path) OVERRIDE {} | |
| 246 | |
| 247 // FlimflamDeviceClient override. | |
| 248 virtual void GetProperties(const dbus::ObjectPath& device_path, | |
| 249 const DictionaryValueCallback& callback) OVERRIDE { | |
| 250 MessageLoop::current()->PostTask( | |
| 251 FROM_HERE, | |
| 252 base::Bind(&FlimflamDeviceClientStubImpl::PassStubDevicePrperties, | |
| 253 weak_ptr_factory_.GetWeakPtr(), | |
| 254 device_path, callback)); | |
| 255 } | |
| 256 | |
| 257 // FlimflamDeviceClient override. | |
| 258 virtual base::DictionaryValue* CallGetPropertiesAndBlock( | |
| 259 const dbus::ObjectPath& device_path) OVERRIDE { | |
| 260 return new base::DictionaryValue; | |
| 261 } | |
| 262 | |
| 263 // FlimflamProfileClient override. | |
| 264 virtual void ProposeScan(const dbus::ObjectPath& device_path, | |
| 265 const VoidDBusMethodCallback& callback) OVERRIDE { | |
| 266 PostVoidCallback(callback, DBUS_METHOD_CALL_SUCCESS); | |
| 267 } | |
| 268 | |
| 269 // FlimflamDeviceClient override. | |
| 270 virtual void SetProperty(const dbus::ObjectPath& device_path, | |
| 271 const std::string& name, | |
| 272 const base::Value& value, | |
| 273 const VoidDBusMethodCallback& callback) OVERRIDE { | |
| 274 base::DictionaryValue* device_properties = NULL; | |
| 275 if (!stub_devices_.GetDictionary(device_path.value(), &device_properties)) { | |
| 276 PostVoidCallback(callback, DBUS_METHOD_CALL_FAILURE); | |
| 277 return; | |
| 278 } | |
| 279 device_properties->Set(name, value.DeepCopy()); | |
| 280 PostVoidCallback(callback, DBUS_METHOD_CALL_SUCCESS); | |
| 281 } | |
| 282 | |
| 283 // FlimflamDeviceClient override. | |
| 284 virtual void ClearProperty(const dbus::ObjectPath& device_path, | |
| 285 const std::string& name, | |
| 286 const VoidDBusMethodCallback& callback) OVERRIDE { | |
| 287 base::DictionaryValue* device_properties = NULL; | |
| 288 if (!stub_devices_.GetDictionary(device_path.value(), &device_properties)) { | |
| 289 PostVoidCallback(callback, DBUS_METHOD_CALL_FAILURE); | |
| 290 return; | |
| 291 } | |
| 292 device_properties->Remove(name, NULL); | |
| 293 PostVoidCallback(callback, DBUS_METHOD_CALL_SUCCESS); | |
| 294 } | |
| 295 | |
| 296 // FlimflamDeviceClient override. | |
| 297 virtual void AddIPConfig( | |
| 298 const dbus::ObjectPath& device_path, | |
| 299 const std::string& method, | |
| 300 const ObjectPathDBusMethodCallback& callback) OVERRIDE { | |
| 301 MessageLoop::current()->PostTask(FROM_HERE, | |
| 302 base::Bind(callback, | |
| 303 DBUS_METHOD_CALL_SUCCESS, | |
| 304 dbus::ObjectPath())); | |
| 305 } | |
| 306 | |
| 307 // FlimflamDeviceClient override. | |
| 308 virtual dbus::ObjectPath CallAddIPConfigAndBlock( | |
| 309 const dbus::ObjectPath& device_path, | |
| 310 const std::string& method) OVERRIDE { | |
| 311 return dbus::ObjectPath(); | |
| 312 } | |
| 313 | |
| 314 // FlimflamDeviceClient override. | |
| 315 virtual void RequirePin(const dbus::ObjectPath& device_path, | |
| 316 const std::string& pin, | |
| 317 bool require, | |
| 318 const base::Closure& callback, | |
| 319 const ErrorCallback& error_callback) OVERRIDE { | |
| 320 MessageLoop::current()->PostTask(FROM_HERE, callback); | |
| 321 } | |
| 322 | |
| 323 // FlimflamDeviceClient override. | |
| 324 virtual void EnterPin(const dbus::ObjectPath& device_path, | |
| 325 const std::string& pin, | |
| 326 const base::Closure& callback, | |
| 327 const ErrorCallback& error_callback) OVERRIDE { | |
| 328 MessageLoop::current()->PostTask(FROM_HERE, callback); | |
| 329 } | |
| 330 | |
| 331 // FlimflamDeviceClient override. | |
| 332 virtual void UnblockPin(const dbus::ObjectPath& device_path, | |
| 333 const std::string& puk, | |
| 334 const std::string& pin, | |
| 335 const base::Closure& callback, | |
| 336 const ErrorCallback& error_callback) OVERRIDE { | |
| 337 MessageLoop::current()->PostTask(FROM_HERE, callback); | |
| 338 } | |
| 339 | |
| 340 // FlimflamDeviceClient override. | |
| 341 virtual void ChangePin(const dbus::ObjectPath& device_path, | |
| 342 const std::string& old_pin, | |
| 343 const std::string& new_pin, | |
| 344 const base::Closure& callback, | |
| 345 const ErrorCallback& error_callback) OVERRIDE { | |
| 346 MessageLoop::current()->PostTask(FROM_HERE, callback); | |
| 347 } | |
| 348 | |
| 349 // FlimflamDeviceClient override. | |
| 350 virtual void Register(const dbus::ObjectPath& device_path, | |
| 351 const std::string& network_id, | |
| 352 const base::Closure& callback, | |
| 353 const ErrorCallback& error_callback) OVERRIDE { | |
| 354 MessageLoop::current()->PostTask(FROM_HERE, callback); | |
| 355 } | |
| 356 | |
| 357 private: | |
| 358 void PassStubDevicePrperties(const dbus::ObjectPath& device_path, | |
| 359 const DictionaryValueCallback& callback) const { | |
| 360 const base::DictionaryValue* device_properties = NULL; | |
| 361 if (!stub_devices_.GetDictionary(device_path.value(), &device_properties)) { | |
| 362 base::DictionaryValue empty_dictionary; | |
| 363 callback.Run(DBUS_METHOD_CALL_FAILURE, empty_dictionary); | |
| 364 return; | |
| 365 } | |
| 366 callback.Run(DBUS_METHOD_CALL_SUCCESS, *device_properties); | |
| 367 } | |
| 368 | |
| 369 // Posts a task to run a void callback with status code |status|. | |
| 370 void PostVoidCallback(const VoidDBusMethodCallback& callback, | |
| 371 DBusMethodCallStatus status) { | |
| 372 MessageLoop::current()->PostTask(FROM_HERE, | |
| 373 base::Bind(callback, status)); | |
| 374 } | |
| 375 | |
| 376 // Dictionary of <device_name, Dictionary>. | |
| 377 base::DictionaryValue stub_devices_; | |
| 378 | |
| 379 // Note: This should remain the last member so it'll be destroyed and | |
| 380 // invalidate its weak pointers before any other members are destroyed. | |
| 381 base::WeakPtrFactory<FlimflamDeviceClientStubImpl> weak_ptr_factory_; | |
| 382 | |
| 383 DISALLOW_COPY_AND_ASSIGN(FlimflamDeviceClientStubImpl); | |
| 384 }; | |
| 385 | |
| 386 } // namespace | |
| 387 | |
| 388 FlimflamDeviceClient::FlimflamDeviceClient() {} | |
| 389 | |
| 390 FlimflamDeviceClient::~FlimflamDeviceClient() {} | |
| 391 | |
| 392 // static | |
| 393 FlimflamDeviceClient* FlimflamDeviceClient::Create( | |
| 394 DBusClientImplementationType type, | |
| 395 dbus::Bus* bus) { | |
| 396 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | |
| 397 return new FlimflamDeviceClientImpl(bus); | |
| 398 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | |
| 399 return new FlimflamDeviceClientStubImpl(); | |
| 400 } | |
| 401 | |
| 402 } // namespace chromeos | |
| OLD | NEW |