| 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 "chrome/browser/chromeos/dbus/flimflam_ipconfig_client.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/chromeos/chromeos_version.h" |
| 9 #include "base/message_loop.h" |
| 10 #include "dbus/bus.h" |
| 11 #include "dbus/message.h" |
| 12 #include "dbus/object_path.h" |
| 13 #include "dbus/object_proxy.h" |
| 14 #include "dbus/values_util.h" |
| 15 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 16 |
| 17 namespace chromeos { |
| 18 |
| 19 namespace { |
| 20 |
| 21 // The FlimflamIPConfigClient implementation. |
| 22 class FlimflamIPConfigClientImpl : public FlimflamIPConfigClient { |
| 23 public: |
| 24 explicit FlimflamIPConfigClientImpl(dbus::Bus* bus); |
| 25 |
| 26 // FlimflamIPConfigClient override. |
| 27 virtual void SetPropertyChangedHandler( |
| 28 const PropertyChangedHandler& handler) OVERRIDE; |
| 29 |
| 30 // FlimflamIPConfigClient override. |
| 31 virtual void ResetPropertyChangedHandler() OVERRIDE; |
| 32 // FlimflamIPConfigClient override. |
| 33 virtual void GetProperties(const DictionaryValueCallback& callback) OVERRIDE; |
| 34 // FlimflamIPConfigClient override. |
| 35 virtual void SetProperty(const std::string& name, |
| 36 const base::Value& value, |
| 37 const VoidCallback& callback) OVERRIDE; |
| 38 // FlimflamIPConfigClient override. |
| 39 virtual void ClearProperty(const std::string& name, |
| 40 const VoidCallback& callback) OVERRIDE; |
| 41 // FlimflamIPConfigClient override. |
| 42 virtual void Remove(const VoidCallback& callback) OVERRIDE; |
| 43 |
| 44 private: |
| 45 // Handles the result of signal connection setup. |
| 46 void OnSignalConnected(const std::string& interface, |
| 47 const std::string& signal, |
| 48 bool success); |
| 49 // Handles PropertyChanged signal. |
| 50 void OnPropertyChanged(dbus::Signal* signal); |
| 51 // Handles responses for methods without results. |
| 52 void OnVoidMethod(const VoidCallback& callback, dbus::Response* response); |
| 53 // Handles responses for methods with DictionaryValue results. |
| 54 void OnDictionaryValueMethod(const DictionaryValueCallback& callback, |
| 55 dbus::Response* response); |
| 56 dbus::ObjectProxy* proxy_; |
| 57 base::WeakPtrFactory<FlimflamIPConfigClientImpl> weak_ptr_factory_; |
| 58 PropertyChangedHandler property_changed_handler_; |
| 59 |
| 60 DISALLOW_COPY_AND_ASSIGN(FlimflamIPConfigClientImpl); |
| 61 }; |
| 62 |
| 63 FlimflamIPConfigClientImpl::FlimflamIPConfigClientImpl(dbus::Bus* bus) |
| 64 : proxy_(bus->GetObjectProxy( |
| 65 flimflam::kFlimflamServiceName, |
| 66 dbus::ObjectPath(flimflam::kFlimflamServicePath))), |
| 67 weak_ptr_factory_(this) { |
| 68 proxy_->ConnectToSignal( |
| 69 flimflam::kFlimflamIPConfigInterface, |
| 70 flimflam::kMonitorPropertyChanged, |
| 71 base::Bind(&FlimflamIPConfigClientImpl::OnPropertyChanged, |
| 72 weak_ptr_factory_.GetWeakPtr()), |
| 73 base::Bind(&FlimflamIPConfigClientImpl::OnSignalConnected, |
| 74 weak_ptr_factory_.GetWeakPtr())); |
| 75 } |
| 76 |
| 77 void FlimflamIPConfigClientImpl::SetPropertyChangedHandler( |
| 78 const PropertyChangedHandler& handler) { |
| 79 property_changed_handler_ = handler; |
| 80 } |
| 81 |
| 82 void FlimflamIPConfigClientImpl::ResetPropertyChangedHandler() { |
| 83 property_changed_handler_.Reset(); |
| 84 } |
| 85 |
| 86 // FlimflamIPConfigClient override. |
| 87 void FlimflamIPConfigClientImpl::GetProperties( |
| 88 const DictionaryValueCallback& callback) { |
| 89 dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface, |
| 90 flimflam::kGetPropertiesFunction); |
| 91 proxy_->CallMethod( |
| 92 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 93 base::Bind(&FlimflamIPConfigClientImpl::OnDictionaryValueMethod, |
| 94 weak_ptr_factory_.GetWeakPtr(), |
| 95 callback)); |
| 96 } |
| 97 |
| 98 // FlimflamIPConfigClient override. |
| 99 void FlimflamIPConfigClientImpl::SetProperty(const std::string& name, |
| 100 const base::Value& value, |
| 101 const VoidCallback& callback) { |
| 102 dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface, |
| 103 flimflam::kSetPropertyFunction); |
| 104 dbus::MessageWriter writer(&method_call); |
| 105 writer.AppendString(name); |
| 106 // IPConfig supports writing basic type and string array properties. |
| 107 switch (value.GetType()) { |
| 108 case base::Value::TYPE_LIST: { |
| 109 const base::ListValue* list_value = NULL; |
| 110 value.GetAsList(&list_value); |
| 111 dbus::MessageWriter variant_writer(NULL); |
| 112 writer.OpenVariant("as", &variant_writer); |
| 113 dbus::MessageWriter array_writer(NULL); |
| 114 variant_writer.OpenArray("s", &array_writer); |
| 115 for (base::ListValue::const_iterator it = list_value->begin(); |
| 116 it != list_value->end(); |
| 117 ++it) { |
| 118 DLOG_IF(ERROR, (*it)->GetType() != base::Value::TYPE_STRING) |
| 119 << "Unexpected type " << (*it)->GetType(); |
| 120 std::string str; |
| 121 (*it)->GetAsString(&str); |
| 122 array_writer.AppendString(str); |
| 123 } |
| 124 variant_writer.CloseContainer(&array_writer); |
| 125 writer.CloseContainer(&variant_writer); |
| 126 } |
| 127 case base::Value::TYPE_BOOLEAN: |
| 128 case base::Value::TYPE_INTEGER: |
| 129 case base::Value::TYPE_DOUBLE: |
| 130 case base::Value::TYPE_STRING: |
| 131 dbus::AppendBasicTypeValueDataAsVariant(&writer, value); |
| 132 break; |
| 133 default: |
| 134 DLOG(ERROR) << "Unexpected type " << value.GetType(); |
| 135 } |
| 136 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 137 base::Bind(&FlimflamIPConfigClientImpl::OnVoidMethod, |
| 138 weak_ptr_factory_.GetWeakPtr(), |
| 139 callback)); |
| 140 } |
| 141 |
| 142 // FlimflamIPConfigClient override. |
| 143 void FlimflamIPConfigClientImpl::ClearProperty(const std::string& name, |
| 144 const VoidCallback& callback) { |
| 145 dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface, |
| 146 flimflam::kClearPropertyFunction); |
| 147 dbus::MessageWriter writer(&method_call); |
| 148 writer.AppendString(name); |
| 149 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 150 base::Bind(&FlimflamIPConfigClientImpl::OnVoidMethod, |
| 151 weak_ptr_factory_.GetWeakPtr(), |
| 152 callback)); |
| 153 } |
| 154 |
| 155 // FlimflamIPConfigClient override. |
| 156 void FlimflamIPConfigClientImpl::Remove(const VoidCallback& callback) { |
| 157 dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface, |
| 158 flimflam::kRemoveConfigFunction); |
| 159 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 160 base::Bind(&FlimflamIPConfigClientImpl::OnVoidMethod, |
| 161 weak_ptr_factory_.GetWeakPtr(), |
| 162 callback)); |
| 163 } |
| 164 |
| 165 void FlimflamIPConfigClientImpl::OnSignalConnected(const std::string& interface, |
| 166 const std::string& signal, |
| 167 bool success) { |
| 168 LOG_IF(ERROR, !success) << "Connect to " << interface << " " |
| 169 << signal << " failed."; |
| 170 } |
| 171 |
| 172 void FlimflamIPConfigClientImpl::OnPropertyChanged(dbus::Signal* signal) { |
| 173 dbus::MessageReader reader(signal); |
| 174 std::string name; |
| 175 if (!reader.PopString(&name)) |
| 176 return; |
| 177 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); |
| 178 if (!value.get()) |
| 179 return; |
| 180 |
| 181 if (!property_changed_handler_.is_null()) |
| 182 property_changed_handler_.Run(name, *value); |
| 183 } |
| 184 |
| 185 void FlimflamIPConfigClientImpl::OnVoidMethod(const VoidCallback& callback, |
| 186 dbus::Response* response) { |
| 187 if (!response) { |
| 188 callback.Run(FAILURE); |
| 189 return; |
| 190 } |
| 191 callback.Run(SUCCESS); |
| 192 } |
| 193 |
| 194 void FlimflamIPConfigClientImpl::OnDictionaryValueMethod( |
| 195 const DictionaryValueCallback& callback, |
| 196 dbus::Response* response) { |
| 197 if (!response) { |
| 198 base::DictionaryValue result; |
| 199 callback.Run(FAILURE, result); |
| 200 return; |
| 201 } |
| 202 dbus::MessageReader reader(response); |
| 203 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); |
| 204 base::DictionaryValue* result = NULL; |
| 205 if (!value.get() || !value->GetAsDictionary(&result)) { |
| 206 base::DictionaryValue result; |
| 207 callback.Run(FAILURE, result); |
| 208 return; |
| 209 } |
| 210 callback.Run(SUCCESS, *result); |
| 211 } |
| 212 |
| 213 // A stub implementation of FlimflamIPConfigClient. |
| 214 class FlimflamIPConfigClientStubImpl : public FlimflamIPConfigClient { |
| 215 public: |
| 216 FlimflamIPConfigClientStubImpl() : weak_ptr_factory_(this) {} |
| 217 |
| 218 virtual ~FlimflamIPConfigClientStubImpl() {} |
| 219 |
| 220 // FlimflamIPConfigClient override. |
| 221 virtual void SetPropertyChangedHandler( |
| 222 const PropertyChangedHandler& handler) OVERRIDE {} |
| 223 |
| 224 // FlimflamIPConfigClient override. |
| 225 virtual void ResetPropertyChangedHandler() OVERRIDE {} |
| 226 |
| 227 // FlimflamIPConfigClient override. |
| 228 virtual void GetProperties(const DictionaryValueCallback& callback) OVERRIDE { |
| 229 MessageLoop::current()->PostTask( |
| 230 FROM_HERE, base::Bind(&FlimflamIPConfigClientStubImpl::PassProperties, |
| 231 weak_ptr_factory_.GetWeakPtr(), |
| 232 callback)); |
| 233 } |
| 234 |
| 235 // FlimflamIPConfigClient override. |
| 236 virtual void SetProperty(const std::string& name, |
| 237 const base::Value& value, |
| 238 const VoidCallback& callback) OVERRIDE { |
| 239 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, SUCCESS)); |
| 240 } |
| 241 |
| 242 // FlimflamIPConfigClient override. |
| 243 virtual void ClearProperty(const std::string& name, |
| 244 const VoidCallback& callback) OVERRIDE { |
| 245 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, SUCCESS)); |
| 246 } |
| 247 |
| 248 // FlimflamIPConfigClient override. |
| 249 virtual void Remove(const VoidCallback& callback) OVERRIDE { |
| 250 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, SUCCESS)); |
| 251 } |
| 252 |
| 253 private: |
| 254 // Runs callback with |properties_|. |
| 255 void PassProperties(const DictionaryValueCallback& callback) const { |
| 256 callback.Run(SUCCESS, properties_); |
| 257 } |
| 258 |
| 259 base::WeakPtrFactory<FlimflamIPConfigClientStubImpl> weak_ptr_factory_; |
| 260 base::DictionaryValue properties_; |
| 261 |
| 262 DISALLOW_COPY_AND_ASSIGN(FlimflamIPConfigClientStubImpl); |
| 263 }; |
| 264 |
| 265 } // namespace |
| 266 |
| 267 FlimflamIPConfigClient::FlimflamIPConfigClient() {} |
| 268 |
| 269 FlimflamIPConfigClient::~FlimflamIPConfigClient() {} |
| 270 |
| 271 // static |
| 272 FlimflamIPConfigClient* FlimflamIPConfigClient::Create(dbus::Bus* bus) { |
| 273 if (base::chromeos::IsRunningOnChromeOS()) |
| 274 return new FlimflamIPConfigClientImpl(bus); |
| 275 else |
| 276 return new FlimflamIPConfigClientStubImpl(); |
| 277 } |
| 278 |
| 279 } // namespace chromeos |
| OLD | NEW |