Chromium Code Reviews| 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 // FlimflamProfileClient override. | |
| 31 virtual void SetPropertyChangedHandler( | |
| 32 const dbus::ObjectPath& device_path, | |
| 33 const PropertyChangedHandler& handler) OVERRIDE { | |
| 34 GetHelper(device_path)->SetPropertyChangedHandler(handler); | |
| 35 } | |
| 36 | |
| 37 // FlimflamProfileClient override. | |
| 38 virtual void ResetPropertyChangedHandler( | |
| 39 const dbus::ObjectPath& device_path) OVERRIDE { | |
| 40 GetHelper(device_path)->ResetPropertyChangedHandler(); | |
| 41 } | |
| 42 | |
| 43 // FlimflamProfileClient override. | |
| 44 virtual void GetProperties(const dbus::ObjectPath& device_path, | |
| 45 const DictionaryValueCallback& callback) OVERRIDE { | |
| 46 dbus::MethodCall method_call(flimflam::kFlimflamDeviceInterface, | |
| 47 flimflam::kGetPropertiesFunction); | |
| 48 GetHelper(device_path)->CallDictionaryValueMethod(&method_call, callback); | |
| 49 } | |
| 50 | |
| 51 // FlimflamProfileClient override. | |
| 52 virtual void ProposeScan(const dbus::ObjectPath& device_path, | |
| 53 const VoidCallback& callback) OVERRIDE { | |
| 54 dbus::MethodCall method_call(flimflam::kFlimflamDeviceInterface, | |
| 55 flimflam::kProposeScanFunction); | |
| 56 GetHelper(device_path)->CallVoidMethod(&method_call, callback); | |
| 57 } | |
| 58 | |
| 59 // FlimflamProfileClient override. | |
| 60 virtual void SetProperty(const dbus::ObjectPath& device_path, | |
| 61 const std::string& name, | |
| 62 const base::Value& value, | |
| 63 const VoidCallback& callback) OVERRIDE { | |
| 64 dbus::MethodCall method_call(flimflam::kFlimflamDeviceInterface, | |
| 65 flimflam::kSetPropertyFunction); | |
| 66 dbus::MessageWriter writer(&method_call); | |
| 67 writer.AppendString(name); | |
| 68 FlimflamClientHelper::AppendValueDataAsVariant(&writer, value); | |
| 69 GetHelper(device_path)->CallVoidMethod(&method_call, callback); | |
| 70 } | |
| 71 | |
| 72 // FlimflamProfileClient override. | |
| 73 virtual void ClearProperty(const dbus::ObjectPath& device_path, | |
| 74 const std::string& name, | |
| 75 const VoidCallback& callback) OVERRIDE { | |
| 76 dbus::MethodCall method_call(flimflam::kFlimflamDeviceInterface, | |
| 77 flimflam::kClearPropertyFunction); | |
| 78 dbus::MessageWriter writer(&method_call); | |
| 79 writer.AppendString(name); | |
| 80 GetHelper(device_path)->CallVoidMethod(&method_call, callback); | |
| 81 } | |
| 82 | |
| 83 // FlimflamProfileClient override. | |
| 84 virtual void AddIPConfig(const dbus::ObjectPath& device_path, | |
| 85 const std::string& method, | |
| 86 const ObjectPathCallback& callback) OVERRIDE { | |
| 87 dbus::MethodCall method_call(flimflam::kFlimflamDeviceInterface, | |
| 88 flimflam::kAddIPConfigFunction); | |
| 89 dbus::MessageWriter writer(&method_call); | |
| 90 writer.AppendString(method); | |
| 91 GetHelper(device_path)->CallObjectPathMethod(&method_call, callback); | |
| 92 } | |
| 93 | |
| 94 // FlimflamProfileClient override. | |
| 95 virtual void RequirePin(const dbus::ObjectPath& device_path, | |
| 96 const std::string& pin, | |
| 97 bool require, | |
| 98 const VoidCallback& callback) OVERRIDE { | |
| 99 dbus::MethodCall method_call(flimflam::kFlimflamDeviceInterface, | |
| 100 flimflam::kRequirePinFunction); | |
| 101 dbus::MessageWriter writer(&method_call); | |
| 102 writer.AppendString(pin); | |
| 103 writer.AppendBool(require); | |
| 104 GetHelper(device_path)->CallVoidMethod(&method_call, callback); | |
| 105 } | |
| 106 | |
| 107 // FlimflamProfileClient override. | |
| 108 virtual void EnterPin(const dbus::ObjectPath& device_path, | |
| 109 const std::string& pin, | |
| 110 const VoidCallback& callback) OVERRIDE { | |
| 111 dbus::MethodCall method_call(flimflam::kFlimflamDeviceInterface, | |
| 112 flimflam::kEnterPinFunction); | |
| 113 dbus::MessageWriter writer(&method_call); | |
| 114 writer.AppendString(pin); | |
| 115 GetHelper(device_path)->CallVoidMethod(&method_call, callback); | |
| 116 } | |
| 117 | |
| 118 // FlimflamProfileClient override. | |
| 119 virtual void UnblockPin(const dbus::ObjectPath& device_path, | |
| 120 const std::string& puk, | |
| 121 const std::string& pin, | |
| 122 const VoidCallback& callback) OVERRIDE { | |
| 123 dbus::MethodCall method_call(flimflam::kFlimflamDeviceInterface, | |
| 124 flimflam::kUnblockPinFunction); | |
| 125 dbus::MessageWriter writer(&method_call); | |
| 126 writer.AppendString(puk); | |
| 127 writer.AppendString(pin); | |
| 128 GetHelper(device_path)->CallVoidMethod(&method_call, callback); | |
| 129 } | |
| 130 | |
| 131 // FlimflamProfileClient override. | |
| 132 virtual void ChangePin(const dbus::ObjectPath& device_path, | |
| 133 const std::string& old_pin, | |
| 134 const std::string& new_pin, | |
| 135 const VoidCallback& callback) OVERRIDE { | |
| 136 dbus::MethodCall method_call(flimflam::kFlimflamDeviceInterface, | |
| 137 flimflam::kChangePinFunction); | |
| 138 dbus::MessageWriter writer(&method_call); | |
| 139 writer.AppendString(old_pin); | |
| 140 writer.AppendString(new_pin); | |
| 141 GetHelper(device_path)->CallVoidMethod(&method_call, callback); | |
| 142 } | |
| 143 | |
| 144 // FlimflamProfileClient override. | |
| 145 virtual void Register(const dbus::ObjectPath& device_path, | |
| 146 const std::string& network_id, | |
| 147 const VoidCallback& callback) OVERRIDE { | |
| 148 dbus::MethodCall method_call(flimflam::kFlimflamDeviceInterface, | |
| 149 flimflam::kRegisterFunction); | |
| 150 dbus::MessageWriter writer(&method_call); | |
| 151 writer.AppendString(network_id); | |
| 152 GetHelper(device_path)->CallVoidMethod(&method_call, callback); | |
| 153 } | |
| 154 | |
| 155 private: | |
| 156 typedef std::map<std::string, FlimflamClientHelper*> HelperMap; | |
| 157 | |
| 158 // Returns the corresponding FlimflamClientHelper for the profile. | |
| 159 FlimflamClientHelper* GetHelper(const dbus::ObjectPath& device_path) { | |
| 160 HelperMap::iterator it = helpers_.find(device_path.value()); | |
| 161 if (it != helpers_.end()) | |
| 162 return it->second; | |
| 163 | |
| 164 // There is no helper for the profile, create it. | |
| 165 dbus::ObjectProxy* object_proxy = | |
| 166 bus_->GetObjectProxy(flimflam::kFlimflamServiceName, device_path); | |
| 167 FlimflamClientHelper* helper = new FlimflamClientHelper(object_proxy); | |
| 168 helper->MonitorPropertyChanged(flimflam::kFlimflamDeviceInterface); | |
| 169 helpers_.insert(HelperMap::value_type(device_path.value(), helper)); | |
| 170 return helper; | |
| 171 } | |
| 172 | |
| 173 dbus::Bus* bus_; | |
| 174 HelperMap helpers_; | |
| 175 STLValueDeleter<HelperMap> helpers_deleter_; | |
| 176 | |
| 177 DISALLOW_COPY_AND_ASSIGN(FlimflamDeviceClientImpl); | |
| 178 }; | |
| 179 | |
| 180 // A stub implementation of FlimflamDeviceClient. | |
| 181 class FlimflamDeviceClientStubImpl : public FlimflamDeviceClient { | |
| 182 public: | |
| 183 FlimflamDeviceClientStubImpl() : weak_ptr_factory_(this) {} | |
| 184 | |
| 185 virtual ~FlimflamDeviceClientStubImpl() {} | |
| 186 | |
| 187 // FlimflamDeviceClient override. | |
| 188 virtual void SetPropertyChangedHandler( | |
| 189 const dbus::ObjectPath& device_path, | |
| 190 const PropertyChangedHandler& handler) OVERRIDE {} | |
| 191 | |
| 192 // FlimflamDeviceClient override. | |
| 193 virtual void ResetPropertyChangedHandler( | |
| 194 const dbus::ObjectPath& device_path) OVERRIDE {} | |
| 195 | |
| 196 // FlimflamDeviceClient override. | |
| 197 virtual void GetProperties(const dbus::ObjectPath& device_path, | |
| 198 const DictionaryValueCallback& callback) OVERRIDE { | |
| 199 MessageLoop::current()->PostTask( | |
| 200 FROM_HERE, | |
| 201 base::Bind(&FlimflamDeviceClientStubImpl::PassEmptyDictionaryValue, | |
| 202 weak_ptr_factory_.GetWeakPtr(), | |
| 203 callback)); | |
| 204 } | |
| 205 | |
| 206 // FlimflamProfileClient override. | |
| 207 virtual void ProposeScan(const dbus::ObjectPath& device_path, | |
| 208 const VoidCallback& callback) OVERRIDE { | |
| 209 MessageLoop::current()->PostTask(FROM_HERE, | |
| 210 base::Bind(callback, | |
| 211 DBUS_METHOD_CALL_SUCCESS)); | |
| 212 } | |
| 213 | |
| 214 // FlimflamDeviceClient override. | |
| 215 virtual void SetProperty(const dbus::ObjectPath& device_path, | |
| 216 const std::string& name, | |
| 217 const base::Value& value, | |
| 218 const VoidCallback& callback) OVERRIDE { | |
| 219 MessageLoop::current()->PostTask(FROM_HERE, | |
| 220 base::Bind(callback, | |
| 221 DBUS_METHOD_CALL_SUCCESS)); | |
| 222 } | |
| 223 | |
| 224 // FlimflamDeviceClient override. | |
| 225 virtual void ClearProperty(const dbus::ObjectPath& device_path, | |
| 226 const std::string& name, | |
| 227 const VoidCallback& callback) OVERRIDE { | |
| 228 MessageLoop::current()->PostTask(FROM_HERE, | |
| 229 base::Bind(callback, | |
| 230 DBUS_METHOD_CALL_SUCCESS)); | |
| 231 } | |
| 232 | |
| 233 // FlimflamDeviceClient override. | |
| 234 virtual void AddIPConfig(const dbus::ObjectPath& device_path, | |
| 235 const std::string& method, | |
| 236 const ObjectPathCallback& callback) OVERRIDE { | |
| 237 MessageLoop::current()->PostTask(FROM_HERE, | |
| 238 base::Bind(callback, | |
| 239 DBUS_METHOD_CALL_SUCCESS, | |
| 240 dbus::ObjectPath())); | |
| 241 } | |
| 242 | |
| 243 // FlimflamDeviceClient override. | |
| 244 virtual void RequirePin(const dbus::ObjectPath& device_path, | |
| 245 const std::string& pin, | |
| 246 bool require, | |
| 247 const VoidCallback& callback) OVERRIDE { | |
| 248 MessageLoop::current()->PostTask(FROM_HERE, | |
| 249 base::Bind(callback, | |
| 250 DBUS_METHOD_CALL_SUCCESS)); | |
| 251 } | |
| 252 | |
| 253 // FlimflamDeviceClient override. | |
| 254 virtual void EnterPin(const dbus::ObjectPath& device_path, | |
| 255 const std::string& pin, | |
| 256 const VoidCallback& callback) OVERRIDE { | |
| 257 MessageLoop::current()->PostTask(FROM_HERE, | |
| 258 base::Bind(callback, | |
| 259 DBUS_METHOD_CALL_SUCCESS)); | |
| 260 } | |
| 261 | |
| 262 // FlimflamDeviceClient override. | |
| 263 virtual void UnblockPin(const dbus::ObjectPath& device_path, | |
| 264 const std::string& puk, | |
| 265 const std::string& pin, | |
| 266 const VoidCallback& callback) OVERRIDE { | |
| 267 MessageLoop::current()->PostTask(FROM_HERE, | |
| 268 base::Bind(callback, | |
| 269 DBUS_METHOD_CALL_SUCCESS)); | |
| 270 } | |
| 271 | |
| 272 // FlimflamDeviceClient override. | |
| 273 virtual void ChangePin(const dbus::ObjectPath& device_path, | |
| 274 const std::string& old_pin, | |
| 275 const std::string& new_pin, | |
| 276 const VoidCallback& callback) OVERRIDE { | |
| 277 MessageLoop::current()->PostTask(FROM_HERE, | |
| 278 base::Bind(callback, | |
| 279 DBUS_METHOD_CALL_SUCCESS)); | |
| 280 } | |
| 281 | |
| 282 // FlimflamDeviceClient override. | |
| 283 virtual void Register(const dbus::ObjectPath& device_path, | |
| 284 const std::string& network_id, | |
| 285 const VoidCallback& callback) OVERRIDE { | |
| 286 MessageLoop::current()->PostTask(FROM_HERE, | |
| 287 base::Bind(callback, | |
| 288 DBUS_METHOD_CALL_SUCCESS)); | |
|
stevenjb
2012/04/17 00:25:52
nit: Since these all do the exact same thing, woul
hashimoto
2012/04/17 02:26:06
Added PostSuccessVoidCallabck
| |
| 289 } | |
| 290 | |
| 291 private: | |
| 292 void PassEmptyDictionaryValue(const DictionaryValueCallback& callback) const { | |
| 293 base::DictionaryValue dictionary; | |
| 294 callback.Run(DBUS_METHOD_CALL_SUCCESS, dictionary); | |
| 295 } | |
| 296 | |
| 297 base::WeakPtrFactory<FlimflamDeviceClientStubImpl> weak_ptr_factory_; | |
| 298 | |
| 299 DISALLOW_COPY_AND_ASSIGN(FlimflamDeviceClientStubImpl); | |
| 300 }; | |
| 301 | |
| 302 } // namespace | |
| 303 | |
| 304 FlimflamDeviceClient::FlimflamDeviceClient() {} | |
| 305 | |
| 306 FlimflamDeviceClient::~FlimflamDeviceClient() {} | |
| 307 | |
| 308 // static | |
| 309 FlimflamDeviceClient* FlimflamDeviceClient::Create( | |
| 310 DBusClientImplementationType type, | |
| 311 dbus::Bus* bus) { | |
| 312 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | |
| 313 return new FlimflamDeviceClientImpl(bus); | |
| 314 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | |
| 315 return new FlimflamDeviceClientStubImpl(); | |
| 316 } | |
| 317 | |
| 318 } // namespace chromeos | |
| OLD | NEW |