Chromium Code Reviews| 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/shill_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 ShillClientHelper::ShillClientHelper(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 ShillClientHelper::~ShillClientHelper() { | 23 ShillClientHelper::~ShillClientHelper() { |
| 24 } | 24 } |
| 25 | 25 |
| 26 void ShillClientHelper::SetPropertyChangedHandler( | 26 void ShillClientHelper::AddPropertyChangedObserver( |
| 27 const PropertyChangedHandler& handler) { | 27 PropertyChangedObserver* obs) { |
|
hashimoto
2012/09/21 11:52:01
Rename this to |observer|
Greg Spencer (Chromium)
2012/09/21 22:03:47
Done.
| |
| 28 property_changed_handler_ = handler; | 28 observer_list_.AddObserver(obs); |
| 29 } | 29 } |
| 30 | 30 |
| 31 void ShillClientHelper::ResetPropertyChangedHandler() { | 31 void ShillClientHelper::RemovePropertyChangedObserver( |
| 32 property_changed_handler_.Reset(); | 32 PropertyChangedObserver* obs) { |
|
hashimoto
2012/09/21 11:52:01
ditto.
Greg Spencer (Chromium)
2012/09/21 22:03:47
Done.
| |
| 33 observer_list_.RemoveObserver(obs); | |
| 33 } | 34 } |
| 34 | 35 |
| 35 void ShillClientHelper::MonitorPropertyChanged( | 36 void ShillClientHelper::MonitorPropertyChanged( |
| 36 const std::string& interface_name) { | 37 const std::string& interface_name) { |
| 37 // We are not using dbus::PropertySet to monitor PropertyChanged signal | 38 // We are not using dbus::PropertySet to monitor PropertyChanged signal |
| 38 // because the interface is not "org.freedesktop.DBus.Properties". | 39 // because the interface is not "org.freedesktop.DBus.Properties". |
| 39 proxy_->ConnectToSignal(interface_name, | 40 proxy_->ConnectToSignal(interface_name, |
| 40 flimflam::kMonitorPropertyChanged, | 41 flimflam::kMonitorPropertyChanged, |
| 41 base::Bind(&ShillClientHelper::OnPropertyChanged, | 42 base::Bind(&ShillClientHelper::OnPropertyChanged, |
| 42 weak_ptr_factory_.GetWeakPtr()), | 43 weak_ptr_factory_.GetWeakPtr()), |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 55 | 56 |
| 56 void ShillClientHelper::CallObjectPathMethod( | 57 void ShillClientHelper::CallObjectPathMethod( |
| 57 dbus::MethodCall* method_call, | 58 dbus::MethodCall* method_call, |
| 58 const ObjectPathDBusMethodCallback& callback) { | 59 const ObjectPathDBusMethodCallback& callback) { |
| 59 proxy_->CallMethod(method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 60 proxy_->CallMethod(method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 60 base::Bind(&ShillClientHelper::OnObjectPathMethod, | 61 base::Bind(&ShillClientHelper::OnObjectPathMethod, |
| 61 weak_ptr_factory_.GetWeakPtr(), | 62 weak_ptr_factory_.GetWeakPtr(), |
| 62 callback)); | 63 callback)); |
| 63 } | 64 } |
| 64 | 65 |
| 66 void ShillClientHelper::CallObjectPathMethodWithErrorCallback( | |
| 67 dbus::MethodCall* method_call, | |
| 68 const ObjectPathDBusMethodCallback& callback, | |
| 69 const ErrorCallback& error_callback) { | |
| 70 proxy_->CallMethodWithErrorCallback( | |
| 71 method_call, | |
| 72 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 73 base::Bind(&ShillClientHelper::OnObjectPathMethod, | |
| 74 weak_ptr_factory_.GetWeakPtr(), | |
| 75 callback), | |
| 76 base::Bind(&ShillClientHelper::OnError, | |
| 77 weak_ptr_factory_.GetWeakPtr(), | |
| 78 error_callback)); | |
| 79 } | |
| 80 | |
| 65 void ShillClientHelper::CallDictionaryValueMethod( | 81 void ShillClientHelper::CallDictionaryValueMethod( |
| 66 dbus::MethodCall* method_call, | 82 dbus::MethodCall* method_call, |
| 67 const DictionaryValueCallback& callback) { | 83 const DictionaryValueCallback& callback) { |
| 68 proxy_->CallMethod(method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 84 proxy_->CallMethod(method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 69 base::Bind(&ShillClientHelper::OnDictionaryValueMethod, | 85 base::Bind(&ShillClientHelper::OnDictionaryValueMethod, |
| 70 weak_ptr_factory_.GetWeakPtr(), | 86 weak_ptr_factory_.GetWeakPtr(), |
| 71 callback)); | 87 callback)); |
| 72 } | 88 } |
| 73 | 89 |
| 74 void ShillClientHelper::CallVoidMethodWithErrorCallback( | 90 void ShillClientHelper::CallVoidMethodWithErrorCallback( |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 137 base::DictionaryValue* result = NULL; | 153 base::DictionaryValue* result = NULL; |
| 138 if (!value || !value->GetAsDictionary(&result)) { | 154 if (!value || !value->GetAsDictionary(&result)) { |
| 139 delete value; | 155 delete value; |
| 140 return NULL; | 156 return NULL; |
| 141 } | 157 } |
| 142 return result; | 158 return result; |
| 143 } | 159 } |
| 144 | 160 |
| 145 // static | 161 // static |
| 146 void ShillClientHelper::AppendValueDataAsVariant(dbus::MessageWriter* writer, | 162 void ShillClientHelper::AppendValueDataAsVariant(dbus::MessageWriter* writer, |
| 147 const base::Value& value) { | 163 const base::Value& value) { |
| 148 // Support basic types and string-to-string dictionary. | 164 // Support basic types and string-to-string dictionary. |
| 149 switch (value.GetType()) { | 165 switch (value.GetType()) { |
| 150 case base::Value::TYPE_DICTIONARY: { | 166 case base::Value::TYPE_DICTIONARY: { |
| 151 const base::DictionaryValue* dictionary = NULL; | 167 const base::DictionaryValue* dictionary = NULL; |
| 152 value.GetAsDictionary(&dictionary); | 168 value.GetAsDictionary(&dictionary); |
| 153 dbus::MessageWriter variant_writer(NULL); | 169 dbus::MessageWriter variant_writer(NULL); |
| 154 writer->OpenVariant("a{ss}", &variant_writer); | 170 writer->OpenVariant("a{ss}", &variant_writer); |
| 155 dbus::MessageWriter array_writer(NULL); | 171 dbus::MessageWriter array_writer(NULL); |
| 156 variant_writer.OpenArray("{ss}", &array_writer); | 172 variant_writer.OpenArray("{ss}", &array_writer); |
| 157 for (base::DictionaryValue::Iterator it(*dictionary); | 173 for (base::DictionaryValue::Iterator it(*dictionary); |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 178 case base::Value::TYPE_STRING: | 194 case base::Value::TYPE_STRING: |
| 179 dbus::AppendBasicTypeValueDataAsVariant(writer, value); | 195 dbus::AppendBasicTypeValueDataAsVariant(writer, value); |
| 180 break; | 196 break; |
| 181 default: | 197 default: |
| 182 DLOG(ERROR) << "Unexpected type " << value.GetType(); | 198 DLOG(ERROR) << "Unexpected type " << value.GetType(); |
| 183 } | 199 } |
| 184 | 200 |
| 185 } | 201 } |
| 186 | 202 |
| 187 void ShillClientHelper::OnSignalConnected(const std::string& interface, | 203 void ShillClientHelper::OnSignalConnected(const std::string& interface, |
| 188 const std::string& signal, | 204 const std::string& signal, |
| 189 bool success) { | 205 bool success) { |
| 190 LOG_IF(ERROR, !success) << "Connect to " << interface << " " << signal | 206 LOG_IF(ERROR, !success) << "Connect to " << interface << " " << signal |
| 191 << " failed."; | 207 << " failed."; |
| 192 } | 208 } |
| 193 | 209 |
| 194 void ShillClientHelper::OnPropertyChanged(dbus::Signal* signal) { | 210 void ShillClientHelper::OnPropertyChanged(dbus::Signal* signal) { |
| 195 if (property_changed_handler_.is_null()) | 211 if (!observer_list_.might_have_observers()) |
|
hashimoto
2012/09/21 11:52:01
This early exit was necessary since calling Run()
Greg Spencer (Chromium)
2012/09/21 22:03:47
Sure, but why not early exit anyhow, to avoid havi
| |
| 196 return; | 212 return; |
| 197 | 213 |
| 198 dbus::MessageReader reader(signal); | 214 dbus::MessageReader reader(signal); |
| 199 std::string name; | 215 std::string name; |
| 200 if (!reader.PopString(&name)) | 216 if (!reader.PopString(&name)) |
| 201 return; | 217 return; |
| 202 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); | 218 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); |
| 203 if (!value.get()) | 219 if (!value.get()) |
| 204 return; | 220 return; |
| 205 property_changed_handler_.Run(name, *value); | 221 |
| 222 FOR_EACH_OBSERVER(PropertyChangedObserver, observer_list_, | |
| 223 OnPropertyChanged(name, *value)); | |
| 224 | |
| 206 } | 225 } |
| 207 | 226 |
| 208 void ShillClientHelper::OnVoidMethod(const VoidDBusMethodCallback& callback, | 227 void ShillClientHelper::OnVoidMethod(const VoidDBusMethodCallback& callback, |
| 209 dbus::Response* response) { | 228 dbus::Response* response) { |
| 210 if (!response) { | 229 if (!response) { |
| 211 callback.Run(DBUS_METHOD_CALL_FAILURE); | 230 callback.Run(DBUS_METHOD_CALL_FAILURE); |
| 212 return; | 231 return; |
| 213 } | 232 } |
| 214 callback.Run(DBUS_METHOD_CALL_SUCCESS); | 233 callback.Run(DBUS_METHOD_CALL_SUCCESS); |
| 215 } | 234 } |
| 216 | 235 |
| 217 void ShillClientHelper::OnObjectPathMethod( | 236 void ShillClientHelper::OnObjectPathMethod( |
| 218 const ObjectPathDBusMethodCallback& callback, | 237 const ObjectPathDBusMethodCallback& callback, |
| 219 dbus::Response* response) { | 238 dbus::Response* response) { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 265 if (!value.get() || !value->GetAsDictionary(&result)) { | 284 if (!value.get() || !value->GetAsDictionary(&result)) { |
| 266 const std::string error_name; // No error name. | 285 const std::string error_name; // No error name. |
| 267 const std::string error_message = "Invalid response."; | 286 const std::string error_message = "Invalid response."; |
| 268 error_callback.Run(error_name, error_message); | 287 error_callback.Run(error_name, error_message); |
| 269 return; | 288 return; |
| 270 } | 289 } |
| 271 callback.Run(*result); | 290 callback.Run(*result); |
| 272 } | 291 } |
| 273 | 292 |
| 274 void ShillClientHelper::OnError(const ErrorCallback& error_callback, | 293 void ShillClientHelper::OnError(const ErrorCallback& error_callback, |
| 275 dbus::ErrorResponse* response) { | 294 dbus::ErrorResponse* response) { |
| 276 std::string error_name; | 295 std::string error_name; |
| 277 std::string error_message; | 296 std::string error_message; |
| 278 if (response) { | 297 if (response) { |
| 279 // Error message may contain the error message as string. | 298 // Error message may contain the error message as string. |
| 280 dbus::MessageReader reader(response); | 299 dbus::MessageReader reader(response); |
| 281 error_name = response->GetErrorName(); | 300 error_name = response->GetErrorName(); |
| 282 reader.PopString(&error_message); | 301 reader.PopString(&error_message); |
| 283 } | 302 } |
| 284 error_callback.Run(error_name, error_message); | 303 error_callback.Run(error_name, error_message); |
| 285 } | 304 } |
| 286 | 305 |
| 287 } // namespace chromeos | 306 } // namespace chromeos |
| OLD | NEW |