| 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_client_helper.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/values.h" | |
| 9 #include "dbus/message.h" | |
| 10 #include "dbus/object_proxy.h" | |
| 11 #include "dbus/values_util.h" | |
| 12 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 13 | |
| 14 namespace chromeos { | |
| 15 | |
| 16 FlimflamClientHelper::FlimflamClientHelper(dbus::Bus* bus, | |
| 17 dbus::ObjectProxy* proxy) | |
| 18 : blocking_method_caller_(bus, proxy), | |
| 19 proxy_(proxy), | |
| 20 weak_ptr_factory_(this) { | |
| 21 } | |
| 22 | |
| 23 FlimflamClientHelper::~FlimflamClientHelper() { | |
| 24 } | |
| 25 | |
| 26 void FlimflamClientHelper::SetPropertyChangedHandler( | |
| 27 const PropertyChangedHandler& handler) { | |
| 28 property_changed_handler_ = handler; | |
| 29 } | |
| 30 | |
| 31 void FlimflamClientHelper::ResetPropertyChangedHandler() { | |
| 32 property_changed_handler_.Reset(); | |
| 33 } | |
| 34 | |
| 35 void FlimflamClientHelper::MonitorPropertyChanged( | |
| 36 const std::string& interface_name) { | |
| 37 // We are not using dbus::PropertySet to monitor PropertyChanged signal | |
| 38 // because the interface is not "org.freedesktop.DBus.Properties". | |
| 39 proxy_->ConnectToSignal(interface_name, | |
| 40 flimflam::kMonitorPropertyChanged, | |
| 41 base::Bind(&FlimflamClientHelper::OnPropertyChanged, | |
| 42 weak_ptr_factory_.GetWeakPtr()), | |
| 43 base::Bind(&FlimflamClientHelper::OnSignalConnected, | |
| 44 weak_ptr_factory_.GetWeakPtr())); | |
| 45 } | |
| 46 | |
| 47 void FlimflamClientHelper::CallVoidMethod( | |
| 48 dbus::MethodCall* method_call, | |
| 49 const VoidDBusMethodCallback& callback) { | |
| 50 proxy_->CallMethod(method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 51 base::Bind(&FlimflamClientHelper::OnVoidMethod, | |
| 52 weak_ptr_factory_.GetWeakPtr(), | |
| 53 callback)); | |
| 54 } | |
| 55 | |
| 56 void FlimflamClientHelper::CallObjectPathMethod( | |
| 57 dbus::MethodCall* method_call, | |
| 58 const ObjectPathDBusMethodCallback& callback) { | |
| 59 proxy_->CallMethod(method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 60 base::Bind(&FlimflamClientHelper::OnObjectPathMethod, | |
| 61 weak_ptr_factory_.GetWeakPtr(), | |
| 62 callback)); | |
| 63 } | |
| 64 | |
| 65 void FlimflamClientHelper::CallDictionaryValueMethod( | |
| 66 dbus::MethodCall* method_call, | |
| 67 const DictionaryValueCallback& callback) { | |
| 68 proxy_->CallMethod(method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 69 base::Bind(&FlimflamClientHelper::OnDictionaryValueMethod, | |
| 70 weak_ptr_factory_.GetWeakPtr(), | |
| 71 callback)); | |
| 72 } | |
| 73 | |
| 74 void FlimflamClientHelper::CallVoidMethodWithErrorCallback( | |
| 75 dbus::MethodCall* method_call, | |
| 76 const base::Closure& callback, | |
| 77 const ErrorCallback& error_callback) { | |
| 78 proxy_->CallMethodWithErrorCallback( | |
| 79 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 80 base::Bind(&FlimflamClientHelper::OnVoidMethodWithErrorCallback, | |
| 81 weak_ptr_factory_.GetWeakPtr(), | |
| 82 callback), | |
| 83 base::Bind(&FlimflamClientHelper::OnError, | |
| 84 weak_ptr_factory_.GetWeakPtr(), | |
| 85 error_callback)); | |
| 86 } | |
| 87 | |
| 88 void FlimflamClientHelper::CallDictionaryValueMethodWithErrorCallback( | |
| 89 dbus::MethodCall* method_call, | |
| 90 const DictionaryValueCallbackWithoutStatus& callback, | |
| 91 const ErrorCallback& error_callback) { | |
| 92 proxy_->CallMethodWithErrorCallback( | |
| 93 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 94 base::Bind( | |
| 95 &FlimflamClientHelper::OnDictionaryValueMethodWithErrorCallback, | |
| 96 weak_ptr_factory_.GetWeakPtr(), | |
| 97 callback, | |
| 98 error_callback), | |
| 99 base::Bind(&FlimflamClientHelper::OnError, | |
| 100 weak_ptr_factory_.GetWeakPtr(), | |
| 101 error_callback)); | |
| 102 } | |
| 103 | |
| 104 bool FlimflamClientHelper::CallVoidMethodAndBlock( | |
| 105 dbus::MethodCall* method_call) { | |
| 106 scoped_ptr<dbus::Response> response( | |
| 107 blocking_method_caller_.CallMethodAndBlock(method_call)); | |
| 108 if (!response.get()) | |
| 109 return false; | |
| 110 return true; | |
| 111 } | |
| 112 | |
| 113 dbus::ObjectPath FlimflamClientHelper::CallObjectPathMethodAndBlock( | |
| 114 dbus::MethodCall* method_call) { | |
| 115 scoped_ptr<dbus::Response> response( | |
| 116 blocking_method_caller_.CallMethodAndBlock(method_call)); | |
| 117 if (!response.get()) | |
| 118 return dbus::ObjectPath(); | |
| 119 | |
| 120 dbus::MessageReader reader(response.get()); | |
| 121 dbus::ObjectPath result; | |
| 122 if (!reader.PopObjectPath(&result)) | |
| 123 return dbus::ObjectPath(); | |
| 124 | |
| 125 return result; | |
| 126 } | |
| 127 | |
| 128 base::DictionaryValue* FlimflamClientHelper::CallDictionaryValueMethodAndBlock( | |
| 129 dbus::MethodCall* method_call) { | |
| 130 scoped_ptr<dbus::Response> response( | |
| 131 blocking_method_caller_.CallMethodAndBlock(method_call)); | |
| 132 if (!response.get()) | |
| 133 return NULL; | |
| 134 | |
| 135 dbus::MessageReader reader(response.get()); | |
| 136 base::Value* value = dbus::PopDataAsValue(&reader); | |
| 137 base::DictionaryValue* result = NULL; | |
| 138 if (!value || !value->GetAsDictionary(&result)) { | |
| 139 delete value; | |
| 140 return NULL; | |
| 141 } | |
| 142 return result; | |
| 143 } | |
| 144 | |
| 145 // static | |
| 146 void FlimflamClientHelper::AppendValueDataAsVariant(dbus::MessageWriter* writer, | |
| 147 const base::Value& value) { | |
| 148 // Support basic types and string-to-string dictionary. | |
| 149 switch (value.GetType()) { | |
| 150 case base::Value::TYPE_DICTIONARY: { | |
| 151 const base::DictionaryValue* dictionary = NULL; | |
| 152 value.GetAsDictionary(&dictionary); | |
| 153 dbus::MessageWriter variant_writer(NULL); | |
| 154 writer->OpenVariant("a{ss}", &variant_writer); | |
| 155 dbus::MessageWriter array_writer(NULL); | |
| 156 variant_writer.OpenArray("{ss}", &array_writer); | |
| 157 for (base::DictionaryValue::Iterator it(*dictionary); | |
| 158 it.HasNext(); | |
| 159 it.Advance()) { | |
| 160 dbus::MessageWriter entry_writer(NULL); | |
| 161 array_writer.OpenDictEntry(&entry_writer); | |
| 162 entry_writer.AppendString(it.key()); | |
| 163 const base::Value& value = it.value(); | |
| 164 std::string value_string; | |
| 165 DLOG_IF(ERROR, value.GetType() != base::Value::TYPE_STRING) | |
| 166 << "Unexpected type " << value.GetType(); | |
| 167 value.GetAsString(&value_string); | |
| 168 entry_writer.AppendString(value_string); | |
| 169 array_writer.CloseContainer(&entry_writer); | |
| 170 } | |
| 171 variant_writer.CloseContainer(&array_writer); | |
| 172 writer->CloseContainer(&variant_writer); | |
| 173 break; | |
| 174 } | |
| 175 case base::Value::TYPE_BOOLEAN: | |
| 176 case base::Value::TYPE_INTEGER: | |
| 177 case base::Value::TYPE_DOUBLE: | |
| 178 case base::Value::TYPE_STRING: | |
| 179 dbus::AppendBasicTypeValueDataAsVariant(writer, value); | |
| 180 break; | |
| 181 default: | |
| 182 DLOG(ERROR) << "Unexpected type " << value.GetType(); | |
| 183 } | |
| 184 | |
| 185 } | |
| 186 | |
| 187 void FlimflamClientHelper::OnSignalConnected(const std::string& interface, | |
| 188 const std::string& signal, | |
| 189 bool success) { | |
| 190 LOG_IF(ERROR, !success) << "Connect to " << interface << " " << signal | |
| 191 << " failed."; | |
| 192 } | |
| 193 | |
| 194 void FlimflamClientHelper::OnPropertyChanged(dbus::Signal* signal) { | |
| 195 if (property_changed_handler_.is_null()) | |
| 196 return; | |
| 197 | |
| 198 dbus::MessageReader reader(signal); | |
| 199 std::string name; | |
| 200 if (!reader.PopString(&name)) | |
| 201 return; | |
| 202 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); | |
| 203 if (!value.get()) | |
| 204 return; | |
| 205 property_changed_handler_.Run(name, *value); | |
| 206 } | |
| 207 | |
| 208 void FlimflamClientHelper::OnVoidMethod(const VoidDBusMethodCallback& callback, | |
| 209 dbus::Response* response) { | |
| 210 if (!response) { | |
| 211 callback.Run(DBUS_METHOD_CALL_FAILURE); | |
| 212 return; | |
| 213 } | |
| 214 callback.Run(DBUS_METHOD_CALL_SUCCESS); | |
| 215 } | |
| 216 | |
| 217 void FlimflamClientHelper::OnObjectPathMethod( | |
| 218 const ObjectPathDBusMethodCallback& callback, | |
| 219 dbus::Response* response) { | |
| 220 if (!response) { | |
| 221 callback.Run(DBUS_METHOD_CALL_FAILURE, dbus::ObjectPath()); | |
| 222 return; | |
| 223 } | |
| 224 dbus::MessageReader reader(response); | |
| 225 dbus::ObjectPath result; | |
| 226 if (!reader.PopObjectPath(&result)) { | |
| 227 callback.Run(DBUS_METHOD_CALL_FAILURE, dbus::ObjectPath()); | |
| 228 return; | |
| 229 } | |
| 230 callback.Run(DBUS_METHOD_CALL_SUCCESS, result); | |
| 231 } | |
| 232 | |
| 233 void FlimflamClientHelper::OnDictionaryValueMethod( | |
| 234 const DictionaryValueCallback& callback, | |
| 235 dbus::Response* response) { | |
| 236 if (!response) { | |
| 237 base::DictionaryValue result; | |
| 238 callback.Run(DBUS_METHOD_CALL_FAILURE, result); | |
| 239 return; | |
| 240 } | |
| 241 dbus::MessageReader reader(response); | |
| 242 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); | |
| 243 base::DictionaryValue* result = NULL; | |
| 244 if (!value.get() || !value->GetAsDictionary(&result)) { | |
| 245 base::DictionaryValue result; | |
| 246 callback.Run(DBUS_METHOD_CALL_FAILURE, result); | |
| 247 return; | |
| 248 } | |
| 249 callback.Run(DBUS_METHOD_CALL_SUCCESS, *result); | |
| 250 } | |
| 251 | |
| 252 void FlimflamClientHelper::OnVoidMethodWithErrorCallback( | |
| 253 const base::Closure& callback, | |
| 254 dbus::Response* response) { | |
| 255 callback.Run(); | |
| 256 } | |
| 257 | |
| 258 void FlimflamClientHelper::OnDictionaryValueMethodWithErrorCallback( | |
| 259 const DictionaryValueCallbackWithoutStatus& callback, | |
| 260 const ErrorCallback& error_callback, | |
| 261 dbus::Response* response) { | |
| 262 dbus::MessageReader reader(response); | |
| 263 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); | |
| 264 base::DictionaryValue* result = NULL; | |
| 265 if (!value.get() || !value->GetAsDictionary(&result)) { | |
| 266 const std::string error_name; // No error name. | |
| 267 const std::string error_message = "Invalid response."; | |
| 268 error_callback.Run(error_name, error_message); | |
| 269 return; | |
| 270 } | |
| 271 callback.Run(*result); | |
| 272 } | |
| 273 | |
| 274 void FlimflamClientHelper::OnError(const ErrorCallback& error_callback, | |
| 275 dbus::ErrorResponse* response) { | |
| 276 std::string error_name; | |
| 277 std::string error_message; | |
| 278 if (response) { | |
| 279 // Error message may contain the error message as string. | |
| 280 dbus::MessageReader reader(response); | |
| 281 error_name = response->GetErrorName(); | |
| 282 reader.PopString(&error_message); | |
| 283 } | |
| 284 error_callback.Run(error_name, error_message); | |
| 285 } | |
| 286 | |
| 287 } // namespace chromeos | |
| OLD | NEW |