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