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 PostSuccessVoidCallback(callback); |
| 210 } |
| 211 |
| 212 // FlimflamDeviceClient override. |
| 213 virtual void SetProperty(const dbus::ObjectPath& device_path, |
| 214 const std::string& name, |
| 215 const base::Value& value, |
| 216 const VoidCallback& callback) OVERRIDE { |
| 217 PostSuccessVoidCallback(callback); |
| 218 } |
| 219 |
| 220 // FlimflamDeviceClient override. |
| 221 virtual void ClearProperty(const dbus::ObjectPath& device_path, |
| 222 const std::string& name, |
| 223 const VoidCallback& callback) OVERRIDE { |
| 224 PostSuccessVoidCallback(callback); |
| 225 } |
| 226 |
| 227 // FlimflamDeviceClient override. |
| 228 virtual void AddIPConfig(const dbus::ObjectPath& device_path, |
| 229 const std::string& method, |
| 230 const ObjectPathCallback& callback) OVERRIDE { |
| 231 MessageLoop::current()->PostTask(FROM_HERE, |
| 232 base::Bind(callback, |
| 233 DBUS_METHOD_CALL_SUCCESS, |
| 234 dbus::ObjectPath())); |
| 235 } |
| 236 |
| 237 // FlimflamDeviceClient override. |
| 238 virtual void RequirePin(const dbus::ObjectPath& device_path, |
| 239 const std::string& pin, |
| 240 bool require, |
| 241 const VoidCallback& callback) OVERRIDE { |
| 242 PostSuccessVoidCallback(callback); |
| 243 } |
| 244 |
| 245 // FlimflamDeviceClient override. |
| 246 virtual void EnterPin(const dbus::ObjectPath& device_path, |
| 247 const std::string& pin, |
| 248 const VoidCallback& callback) OVERRIDE { |
| 249 PostSuccessVoidCallback(callback); |
| 250 } |
| 251 |
| 252 // FlimflamDeviceClient override. |
| 253 virtual void UnblockPin(const dbus::ObjectPath& device_path, |
| 254 const std::string& puk, |
| 255 const std::string& pin, |
| 256 const VoidCallback& callback) OVERRIDE { |
| 257 PostSuccessVoidCallback(callback); |
| 258 } |
| 259 |
| 260 // FlimflamDeviceClient override. |
| 261 virtual void ChangePin(const dbus::ObjectPath& device_path, |
| 262 const std::string& old_pin, |
| 263 const std::string& new_pin, |
| 264 const VoidCallback& callback) OVERRIDE { |
| 265 PostSuccessVoidCallback(callback); |
| 266 } |
| 267 |
| 268 // FlimflamDeviceClient override. |
| 269 virtual void Register(const dbus::ObjectPath& device_path, |
| 270 const std::string& network_id, |
| 271 const VoidCallback& callback) OVERRIDE { |
| 272 PostSuccessVoidCallback(callback); |
| 273 } |
| 274 |
| 275 private: |
| 276 void PassEmptyDictionaryValue(const DictionaryValueCallback& callback) const { |
| 277 base::DictionaryValue dictionary; |
| 278 callback.Run(DBUS_METHOD_CALL_SUCCESS, dictionary); |
| 279 } |
| 280 |
| 281 // Posts a task to run a void callback with success status code. |
| 282 void PostSuccessVoidCallback(const VoidCallback& callback) { |
| 283 MessageLoop::current()->PostTask(FROM_HERE, |
| 284 base::Bind(callback, |
| 285 DBUS_METHOD_CALL_SUCCESS)); |
| 286 } |
| 287 |
| 288 base::WeakPtrFactory<FlimflamDeviceClientStubImpl> weak_ptr_factory_; |
| 289 |
| 290 DISALLOW_COPY_AND_ASSIGN(FlimflamDeviceClientStubImpl); |
| 291 }; |
| 292 |
| 293 } // namespace |
| 294 |
| 295 FlimflamDeviceClient::FlimflamDeviceClient() {} |
| 296 |
| 297 FlimflamDeviceClient::~FlimflamDeviceClient() {} |
| 298 |
| 299 // static |
| 300 FlimflamDeviceClient* FlimflamDeviceClient::Create( |
| 301 DBusClientImplementationType type, |
| 302 dbus::Bus* bus) { |
| 303 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
| 304 return new FlimflamDeviceClientImpl(bus); |
| 305 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
| 306 return new FlimflamDeviceClientStubImpl(); |
| 307 } |
| 308 |
| 309 } // namespace chromeos |
OLD | NEW |