| 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_ipconfig_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 FlimflamIPConfigClient implementation. | |
| 23 class FlimflamIPConfigClientImpl : public FlimflamIPConfigClient { | |
| 24 public: | |
| 25 explicit FlimflamIPConfigClientImpl(dbus::Bus* bus); | |
| 26 | |
| 27 // FlimflamIPConfigClient overrides: | |
| 28 virtual void SetPropertyChangedHandler( | |
| 29 const dbus::ObjectPath& ipconfig_path, | |
| 30 const PropertyChangedHandler& handler) OVERRIDE; | |
| 31 virtual void ResetPropertyChangedHandler( | |
| 32 const dbus::ObjectPath& ipconfig_path) OVERRIDE; | |
| 33 virtual void Refresh(const dbus::ObjectPath& ipconfig_path, | |
| 34 const VoidDBusMethodCallback& callback) OVERRIDE; | |
| 35 virtual void GetProperties(const dbus::ObjectPath& ipconfig_path, | |
| 36 const DictionaryValueCallback& callback) OVERRIDE; | |
| 37 virtual base::DictionaryValue* CallGetPropertiesAndBlock( | |
| 38 const dbus::ObjectPath& ipconfig_path) OVERRIDE; | |
| 39 virtual void SetProperty(const dbus::ObjectPath& ipconfig_path, | |
| 40 const std::string& name, | |
| 41 const base::Value& value, | |
| 42 const VoidDBusMethodCallback& callback) OVERRIDE; | |
| 43 virtual void ClearProperty(const dbus::ObjectPath& ipconfig_path, | |
| 44 const std::string& name, | |
| 45 const VoidDBusMethodCallback& callback) OVERRIDE; | |
| 46 virtual void Remove(const dbus::ObjectPath& ipconfig_path, | |
| 47 const VoidDBusMethodCallback& callback) OVERRIDE; | |
| 48 virtual bool CallRemoveAndBlock( | |
| 49 const dbus::ObjectPath& ipconfig_path) OVERRIDE; | |
| 50 | |
| 51 private: | |
| 52 typedef std::map<std::string, FlimflamClientHelper*> HelperMap; | |
| 53 | |
| 54 // Returns the corresponding FlimflamClientHelper for the profile. | |
| 55 FlimflamClientHelper* GetHelper(const dbus::ObjectPath& ipconfig_path) { | |
| 56 HelperMap::iterator it = helpers_.find(ipconfig_path.value()); | |
| 57 if (it != helpers_.end()) | |
| 58 return it->second; | |
| 59 | |
| 60 // There is no helper for the profile, create it. | |
| 61 dbus::ObjectProxy* object_proxy = | |
| 62 bus_->GetObjectProxy(flimflam::kFlimflamServiceName, ipconfig_path); | |
| 63 FlimflamClientHelper* helper = new FlimflamClientHelper(bus_, object_proxy); | |
| 64 helper->MonitorPropertyChanged(flimflam::kFlimflamIPConfigInterface); | |
| 65 helpers_.insert(HelperMap::value_type(ipconfig_path.value(), helper)); | |
| 66 return helper; | |
| 67 } | |
| 68 | |
| 69 dbus::Bus* bus_; | |
| 70 HelperMap helpers_; | |
| 71 STLValueDeleter<HelperMap> helpers_deleter_; | |
| 72 | |
| 73 DISALLOW_COPY_AND_ASSIGN(FlimflamIPConfigClientImpl); | |
| 74 }; | |
| 75 | |
| 76 FlimflamIPConfigClientImpl::FlimflamIPConfigClientImpl(dbus::Bus* bus) | |
| 77 : bus_(bus), | |
| 78 helpers_deleter_(&helpers_) { | |
| 79 } | |
| 80 | |
| 81 void FlimflamIPConfigClientImpl::SetPropertyChangedHandler( | |
| 82 const dbus::ObjectPath& ipconfig_path, | |
| 83 const PropertyChangedHandler& handler) { | |
| 84 GetHelper(ipconfig_path)->SetPropertyChangedHandler(handler); | |
| 85 } | |
| 86 | |
| 87 void FlimflamIPConfigClientImpl::ResetPropertyChangedHandler( | |
| 88 const dbus::ObjectPath& ipconfig_path) { | |
| 89 GetHelper(ipconfig_path)->ResetPropertyChangedHandler(); | |
| 90 } | |
| 91 | |
| 92 void FlimflamIPConfigClientImpl::GetProperties( | |
| 93 const dbus::ObjectPath& ipconfig_path, | |
| 94 const DictionaryValueCallback& callback) { | |
| 95 dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface, | |
| 96 flimflam::kGetPropertiesFunction); | |
| 97 GetHelper(ipconfig_path)->CallDictionaryValueMethod(&method_call, callback); | |
| 98 } | |
| 99 | |
| 100 base::DictionaryValue* FlimflamIPConfigClientImpl::CallGetPropertiesAndBlock( | |
| 101 const dbus::ObjectPath& ipconfig_path) { | |
| 102 dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface, | |
| 103 flimflam::kGetPropertiesFunction); | |
| 104 return GetHelper(ipconfig_path)->CallDictionaryValueMethodAndBlock( | |
| 105 &method_call); | |
| 106 } | |
| 107 | |
| 108 void FlimflamIPConfigClientImpl::Refresh( | |
| 109 const dbus::ObjectPath& ipconfig_path, | |
| 110 const VoidDBusMethodCallback& callback) { | |
| 111 dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface, | |
| 112 shill::kRefreshFunction); | |
| 113 GetHelper(ipconfig_path)->CallVoidMethod(&method_call, callback); | |
| 114 } | |
| 115 | |
| 116 void FlimflamIPConfigClientImpl::SetProperty( | |
| 117 const dbus::ObjectPath& ipconfig_path, | |
| 118 const std::string& name, | |
| 119 const base::Value& value, | |
| 120 const VoidDBusMethodCallback& callback) { | |
| 121 dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface, | |
| 122 flimflam::kSetPropertyFunction); | |
| 123 dbus::MessageWriter writer(&method_call); | |
| 124 writer.AppendString(name); | |
| 125 // IPConfig supports writing basic type and string array properties. | |
| 126 switch (value.GetType()) { | |
| 127 case base::Value::TYPE_LIST: { | |
| 128 const base::ListValue* list_value = NULL; | |
| 129 value.GetAsList(&list_value); | |
| 130 dbus::MessageWriter variant_writer(NULL); | |
| 131 writer.OpenVariant("as", &variant_writer); | |
| 132 dbus::MessageWriter array_writer(NULL); | |
| 133 variant_writer.OpenArray("s", &array_writer); | |
| 134 for (base::ListValue::const_iterator it = list_value->begin(); | |
| 135 it != list_value->end(); | |
| 136 ++it) { | |
| 137 DLOG_IF(ERROR, (*it)->GetType() != base::Value::TYPE_STRING) | |
| 138 << "Unexpected type " << (*it)->GetType(); | |
| 139 std::string str; | |
| 140 (*it)->GetAsString(&str); | |
| 141 array_writer.AppendString(str); | |
| 142 } | |
| 143 variant_writer.CloseContainer(&array_writer); | |
| 144 writer.CloseContainer(&variant_writer); | |
| 145 } | |
| 146 case base::Value::TYPE_BOOLEAN: | |
| 147 case base::Value::TYPE_INTEGER: | |
| 148 case base::Value::TYPE_DOUBLE: | |
| 149 case base::Value::TYPE_STRING: | |
| 150 dbus::AppendBasicTypeValueDataAsVariant(&writer, value); | |
| 151 break; | |
| 152 default: | |
| 153 DLOG(ERROR) << "Unexpected type " << value.GetType(); | |
| 154 } | |
| 155 GetHelper(ipconfig_path)->CallVoidMethod(&method_call, callback); | |
| 156 } | |
| 157 | |
| 158 void FlimflamIPConfigClientImpl::ClearProperty( | |
| 159 const dbus::ObjectPath& ipconfig_path, | |
| 160 const std::string& name, | |
| 161 const VoidDBusMethodCallback& callback) { | |
| 162 dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface, | |
| 163 flimflam::kClearPropertyFunction); | |
| 164 dbus::MessageWriter writer(&method_call); | |
| 165 writer.AppendString(name); | |
| 166 GetHelper(ipconfig_path)->CallVoidMethod(&method_call, callback); | |
| 167 } | |
| 168 | |
| 169 void FlimflamIPConfigClientImpl::Remove( | |
| 170 const dbus::ObjectPath& ipconfig_path, | |
| 171 const VoidDBusMethodCallback& callback) { | |
| 172 dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface, | |
| 173 flimflam::kRemoveConfigFunction); | |
| 174 GetHelper(ipconfig_path)->CallVoidMethod(&method_call, callback); | |
| 175 } | |
| 176 | |
| 177 bool FlimflamIPConfigClientImpl::CallRemoveAndBlock( | |
| 178 const dbus::ObjectPath& ipconfig_path) { | |
| 179 dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface, | |
| 180 flimflam::kRemoveConfigFunction); | |
| 181 return GetHelper(ipconfig_path)->CallVoidMethodAndBlock(&method_call); | |
| 182 } | |
| 183 | |
| 184 // A stub implementation of FlimflamIPConfigClient. | |
| 185 class FlimflamIPConfigClientStubImpl : public FlimflamIPConfigClient { | |
| 186 public: | |
| 187 FlimflamIPConfigClientStubImpl() : weak_ptr_factory_(this) {} | |
| 188 | |
| 189 virtual ~FlimflamIPConfigClientStubImpl() {} | |
| 190 | |
| 191 /////////////////////////////////////////////// | |
| 192 // FlimflamIPConfigClient overrides: | |
| 193 virtual void SetPropertyChangedHandler( | |
| 194 const dbus::ObjectPath& ipconfig_path, | |
| 195 const PropertyChangedHandler& handler) OVERRIDE {} | |
| 196 | |
| 197 virtual void ResetPropertyChangedHandler( | |
| 198 const dbus::ObjectPath& ipconfig_path) OVERRIDE {} | |
| 199 | |
| 200 virtual void Refresh(const dbus::ObjectPath& ipconfig_path, | |
| 201 const VoidDBusMethodCallback& callback) OVERRIDE {} | |
| 202 | |
| 203 virtual void GetProperties(const dbus::ObjectPath& ipconfig_path, | |
| 204 const DictionaryValueCallback& callback) OVERRIDE { | |
| 205 MessageLoop::current()->PostTask( | |
| 206 FROM_HERE, base::Bind(&FlimflamIPConfigClientStubImpl::PassProperties, | |
| 207 weak_ptr_factory_.GetWeakPtr(), | |
| 208 callback)); | |
| 209 } | |
| 210 | |
| 211 virtual base::DictionaryValue* CallGetPropertiesAndBlock( | |
| 212 const dbus::ObjectPath& ipconfig_path) OVERRIDE { | |
| 213 return new base::DictionaryValue; | |
| 214 } | |
| 215 | |
| 216 virtual void SetProperty(const dbus::ObjectPath& ipconfig_path, | |
| 217 const std::string& name, | |
| 218 const base::Value& value, | |
| 219 const VoidDBusMethodCallback& callback) OVERRIDE { | |
| 220 MessageLoop::current()->PostTask( | |
| 221 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS)); | |
| 222 } | |
| 223 | |
| 224 virtual void ClearProperty(const dbus::ObjectPath& ipconfig_path, | |
| 225 const std::string& name, | |
| 226 const VoidDBusMethodCallback& callback) OVERRIDE { | |
| 227 MessageLoop::current()->PostTask( | |
| 228 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS)); | |
| 229 } | |
| 230 | |
| 231 virtual void Remove(const dbus::ObjectPath& ipconfig_path, | |
| 232 const VoidDBusMethodCallback& callback) OVERRIDE { | |
| 233 MessageLoop::current()->PostTask( | |
| 234 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS)); | |
| 235 } | |
| 236 | |
| 237 virtual bool CallRemoveAndBlock( | |
| 238 const dbus::ObjectPath& ipconfig_path) OVERRIDE { | |
| 239 return true; | |
| 240 } | |
| 241 | |
| 242 private: | |
| 243 // Runs callback with |properties_|. | |
| 244 void PassProperties(const DictionaryValueCallback& callback) const { | |
| 245 callback.Run(DBUS_METHOD_CALL_SUCCESS, properties_); | |
| 246 } | |
| 247 | |
| 248 base::DictionaryValue properties_; | |
| 249 | |
| 250 // Note: This should remain the last member so it'll be destroyed and | |
| 251 // invalidate its weak pointers before any other members are destroyed. | |
| 252 base::WeakPtrFactory<FlimflamIPConfigClientStubImpl> weak_ptr_factory_; | |
| 253 | |
| 254 DISALLOW_COPY_AND_ASSIGN(FlimflamIPConfigClientStubImpl); | |
| 255 }; | |
| 256 | |
| 257 } // namespace | |
| 258 | |
| 259 FlimflamIPConfigClient::FlimflamIPConfigClient() {} | |
| 260 | |
| 261 FlimflamIPConfigClient::~FlimflamIPConfigClient() {} | |
| 262 | |
| 263 // static | |
| 264 FlimflamIPConfigClient* FlimflamIPConfigClient::Create( | |
| 265 DBusClientImplementationType type, | |
| 266 dbus::Bus* bus) { | |
| 267 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | |
| 268 return new FlimflamIPConfigClientImpl(bus); | |
| 269 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | |
| 270 return new FlimflamIPConfigClientStubImpl(); | |
| 271 } | |
| 272 | |
| 273 } // namespace chromeos | |
| OLD | NEW |