| 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_manager_client.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/chromeos/chromeos_version.h" | |
| 9 #include "base/message_loop.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 // Returns whether the properties have the required keys or not. | |
| 23 bool AreServicePropertiesValid(const base::DictionaryValue& properties) { | |
| 24 if (properties.HasKey(flimflam::kGuidProperty)) | |
| 25 return true; | |
| 26 return properties.HasKey(flimflam::kTypeProperty) && | |
| 27 properties.HasKey(flimflam::kSecurityProperty) && | |
| 28 properties.HasKey(flimflam::kSSIDProperty); | |
| 29 } | |
| 30 | |
| 31 // Appends a string-to-variant dictionary to the writer. | |
| 32 void AppendServicePropertiesDictionary( | |
| 33 dbus::MessageWriter* writer, | |
| 34 const base::DictionaryValue& dictionary) { | |
| 35 dbus::MessageWriter array_writer(NULL); | |
| 36 writer->OpenArray("{sv}", &array_writer); | |
| 37 for (base::DictionaryValue::Iterator it(dictionary); | |
| 38 it.HasNext(); | |
| 39 it.Advance()) { | |
| 40 dbus::MessageWriter entry_writer(NULL); | |
| 41 array_writer.OpenDictEntry(&entry_writer); | |
| 42 entry_writer.AppendString(it.key()); | |
| 43 FlimflamClientHelper::AppendValueDataAsVariant(&entry_writer, it.value()); | |
| 44 array_writer.CloseContainer(&entry_writer); | |
| 45 } | |
| 46 writer->CloseContainer(&array_writer); | |
| 47 } | |
| 48 | |
| 49 // The FlimflamManagerClient implementation. | |
| 50 class FlimflamManagerClientImpl : public FlimflamManagerClient { | |
| 51 public: | |
| 52 explicit FlimflamManagerClientImpl(dbus::Bus* bus) | |
| 53 : proxy_(bus->GetObjectProxy( | |
| 54 flimflam::kFlimflamServiceName, | |
| 55 dbus::ObjectPath(flimflam::kFlimflamServicePath))), | |
| 56 helper_(bus, proxy_) { | |
| 57 helper_.MonitorPropertyChanged(flimflam::kFlimflamManagerInterface); | |
| 58 } | |
| 59 | |
| 60 // FlimflamManagerClient overrides: | |
| 61 virtual void SetPropertyChangedHandler( | |
| 62 const PropertyChangedHandler& handler) OVERRIDE { | |
| 63 helper_.SetPropertyChangedHandler(handler); | |
| 64 } | |
| 65 | |
| 66 virtual void ResetPropertyChangedHandler() OVERRIDE { | |
| 67 helper_.ResetPropertyChangedHandler(); | |
| 68 } | |
| 69 | |
| 70 virtual void GetProperties(const DictionaryValueCallback& callback) OVERRIDE { | |
| 71 dbus::MethodCall method_call(flimflam::kFlimflamManagerInterface, | |
| 72 flimflam::kGetPropertiesFunction); | |
| 73 helper_.CallDictionaryValueMethod(&method_call, callback); | |
| 74 } | |
| 75 | |
| 76 virtual base::DictionaryValue* CallGetPropertiesAndBlock() OVERRIDE { | |
| 77 dbus::MethodCall method_call(flimflam::kFlimflamManagerInterface, | |
| 78 flimflam::kGetPropertiesFunction); | |
| 79 return helper_.CallDictionaryValueMethodAndBlock(&method_call); | |
| 80 } | |
| 81 | |
| 82 virtual void SetProperty(const std::string& name, | |
| 83 const base::Value& value, | |
| 84 const VoidDBusMethodCallback& callback) OVERRIDE { | |
| 85 dbus::MethodCall method_call(flimflam::kFlimflamManagerInterface, | |
| 86 flimflam::kSetPropertyFunction); | |
| 87 dbus::MessageWriter writer(&method_call); | |
| 88 writer.AppendString(name); | |
| 89 FlimflamClientHelper::AppendValueDataAsVariant(&writer, value); | |
| 90 helper_.CallVoidMethod(&method_call, callback); | |
| 91 } | |
| 92 | |
| 93 virtual void RequestScan(const std::string& type, | |
| 94 const VoidDBusMethodCallback& callback) OVERRIDE { | |
| 95 dbus::MethodCall method_call(flimflam::kFlimflamManagerInterface, | |
| 96 flimflam::kRequestScanFunction); | |
| 97 dbus::MessageWriter writer(&method_call); | |
| 98 writer.AppendString(type); | |
| 99 helper_.CallVoidMethod(&method_call, callback); | |
| 100 } | |
| 101 | |
| 102 virtual void EnableTechnology( | |
| 103 const std::string& type, | |
| 104 const VoidDBusMethodCallback& callback) OVERRIDE { | |
| 105 dbus::MethodCall method_call(flimflam::kFlimflamManagerInterface, | |
| 106 flimflam::kEnableTechnologyFunction); | |
| 107 dbus::MessageWriter writer(&method_call); | |
| 108 writer.AppendString(type); | |
| 109 helper_.CallVoidMethod(&method_call, callback); | |
| 110 } | |
| 111 | |
| 112 virtual void DisableTechnology( | |
| 113 const std::string& type, | |
| 114 const VoidDBusMethodCallback& callback) OVERRIDE { | |
| 115 dbus::MethodCall method_call(flimflam::kFlimflamManagerInterface, | |
| 116 flimflam::kDisableTechnologyFunction); | |
| 117 dbus::MessageWriter writer(&method_call); | |
| 118 writer.AppendString(type); | |
| 119 helper_.CallVoidMethod(&method_call, callback); | |
| 120 } | |
| 121 | |
| 122 virtual void ConfigureService( | |
| 123 const base::DictionaryValue& properties, | |
| 124 const VoidDBusMethodCallback& callback) OVERRIDE { | |
| 125 DCHECK(AreServicePropertiesValid(properties)); | |
| 126 dbus::MethodCall method_call(flimflam::kFlimflamManagerInterface, | |
| 127 flimflam::kConfigureServiceFunction); | |
| 128 dbus::MessageWriter writer(&method_call); | |
| 129 AppendServicePropertiesDictionary(&writer, properties); | |
| 130 helper_.CallVoidMethod(&method_call, callback); | |
| 131 } | |
| 132 | |
| 133 virtual void GetService( | |
| 134 const base::DictionaryValue& properties, | |
| 135 const ObjectPathDBusMethodCallback& callback) OVERRIDE { | |
| 136 dbus::MethodCall method_call(flimflam::kFlimflamManagerInterface, | |
| 137 flimflam::kGetServiceFunction); | |
| 138 dbus::MessageWriter writer(&method_call); | |
| 139 AppendServicePropertiesDictionary(&writer, properties); | |
| 140 helper_.CallObjectPathMethod(&method_call, callback); | |
| 141 } | |
| 142 | |
| 143 private: | |
| 144 dbus::ObjectProxy* proxy_; | |
| 145 FlimflamClientHelper helper_; | |
| 146 | |
| 147 DISALLOW_COPY_AND_ASSIGN(FlimflamManagerClientImpl); | |
| 148 }; | |
| 149 | |
| 150 // A stub implementation of FlimflamManagerClient. | |
| 151 // Implemented: Stub cellular DeviceList entry for SMS testing. | |
| 152 class FlimflamManagerClientStubImpl : public FlimflamManagerClient { | |
| 153 public: | |
| 154 FlimflamManagerClientStubImpl() : weak_ptr_factory_(this) { | |
| 155 base::ListValue* device_list = new base::ListValue; | |
| 156 // Note: names match Device stub map. | |
| 157 const char kStubCellular1[] = "stub_cellular1"; | |
| 158 const char kStubCellular2[] = "stub_cellular2"; | |
| 159 device_list->Append(base::Value::CreateStringValue(kStubCellular1)); | |
| 160 device_list->Append(base::Value::CreateStringValue(kStubCellular2)); | |
| 161 stub_properties_.Set(flimflam::kDevicesProperty, device_list); | |
| 162 } | |
| 163 | |
| 164 virtual ~FlimflamManagerClientStubImpl() {} | |
| 165 | |
| 166 // FlimflamManagerClient override. | |
| 167 virtual void SetPropertyChangedHandler( | |
| 168 const PropertyChangedHandler& handler) OVERRIDE {} | |
| 169 | |
| 170 // FlimflamManagerClient override. | |
| 171 virtual void ResetPropertyChangedHandler() OVERRIDE {} | |
| 172 | |
| 173 // FlimflamManagerClient override. | |
| 174 virtual void GetProperties(const DictionaryValueCallback& callback) OVERRIDE { | |
| 175 MessageLoop::current()->PostTask( | |
| 176 FROM_HERE, base::Bind( | |
| 177 &FlimflamManagerClientStubImpl::PassStubProperties, | |
| 178 weak_ptr_factory_.GetWeakPtr(), | |
| 179 callback)); | |
| 180 } | |
| 181 | |
| 182 // FlimflamManagerClient override. | |
| 183 virtual base::DictionaryValue* CallGetPropertiesAndBlock() OVERRIDE { | |
| 184 return new base::DictionaryValue; | |
| 185 } | |
| 186 | |
| 187 // FlimflamManagerClient override. | |
| 188 virtual void SetProperty(const std::string& name, | |
| 189 const base::Value& value, | |
| 190 const VoidDBusMethodCallback& callback) OVERRIDE { | |
| 191 stub_properties_.Set(name, value.DeepCopy()); | |
| 192 MessageLoop::current()->PostTask(FROM_HERE, | |
| 193 base::Bind(callback, | |
| 194 DBUS_METHOD_CALL_SUCCESS)); | |
| 195 } | |
| 196 | |
| 197 // FlimflamManagerClient override. | |
| 198 virtual void RequestScan(const std::string& type, | |
| 199 const VoidDBusMethodCallback& callback) OVERRIDE { | |
| 200 MessageLoop::current()->PostTask(FROM_HERE, | |
| 201 base::Bind(callback, | |
| 202 DBUS_METHOD_CALL_SUCCESS)); | |
| 203 } | |
| 204 | |
| 205 // FlimflamManagerClient override. | |
| 206 virtual void EnableTechnology( | |
| 207 const std::string& type, | |
| 208 const VoidDBusMethodCallback& callback) OVERRIDE { | |
| 209 MessageLoop::current()->PostTask(FROM_HERE, | |
| 210 base::Bind(callback, | |
| 211 DBUS_METHOD_CALL_SUCCESS)); | |
| 212 } | |
| 213 | |
| 214 // FlimflamManagerClient override. | |
| 215 virtual void DisableTechnology( | |
| 216 const std::string& type, | |
| 217 const VoidDBusMethodCallback& callback) OVERRIDE { | |
| 218 MessageLoop::current()->PostTask(FROM_HERE, | |
| 219 base::Bind(callback, | |
| 220 DBUS_METHOD_CALL_SUCCESS)); | |
| 221 } | |
| 222 | |
| 223 // FlimflamManagerClient override. | |
| 224 virtual void ConfigureService( | |
| 225 const base::DictionaryValue& properties, | |
| 226 const VoidDBusMethodCallback& callback) OVERRIDE { | |
| 227 MessageLoop::current()->PostTask(FROM_HERE, | |
| 228 base::Bind(callback, | |
| 229 DBUS_METHOD_CALL_SUCCESS)); | |
| 230 } | |
| 231 | |
| 232 // FlimflamManagerClient override. | |
| 233 virtual void GetService( | |
| 234 const base::DictionaryValue& properties, | |
| 235 const ObjectPathDBusMethodCallback& callback) OVERRIDE { | |
| 236 MessageLoop::current()->PostTask(FROM_HERE, | |
| 237 base::Bind(callback, | |
| 238 DBUS_METHOD_CALL_SUCCESS, | |
| 239 dbus::ObjectPath())); | |
| 240 } | |
| 241 | |
| 242 private: | |
| 243 void PassStubProperties(const DictionaryValueCallback& callback) const { | |
| 244 callback.Run(DBUS_METHOD_CALL_SUCCESS, stub_properties_); | |
| 245 } | |
| 246 | |
| 247 base::DictionaryValue stub_properties_; | |
| 248 | |
| 249 // Note: This should remain the last member so it'll be destroyed and | |
| 250 // invalidate its weak pointers before any other members are destroyed. | |
| 251 base::WeakPtrFactory<FlimflamManagerClientStubImpl> weak_ptr_factory_; | |
| 252 | |
| 253 DISALLOW_COPY_AND_ASSIGN(FlimflamManagerClientStubImpl); | |
| 254 }; | |
| 255 | |
| 256 } // namespace | |
| 257 | |
| 258 FlimflamManagerClient::FlimflamManagerClient() {} | |
| 259 | |
| 260 FlimflamManagerClient::~FlimflamManagerClient() {} | |
| 261 | |
| 262 // static | |
| 263 FlimflamManagerClient* FlimflamManagerClient::Create( | |
| 264 DBusClientImplementationType type, | |
| 265 dbus::Bus* bus) { | |
| 266 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | |
| 267 return new FlimflamManagerClientImpl(bus); | |
| 268 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | |
| 269 return new FlimflamManagerClientStubImpl(); | |
| 270 } | |
| 271 | |
| 272 } // namespace chromeos | |
| OLD | NEW |