OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "dbus/object_manager.h" | 5 #include "dbus/object_manager.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" |
8 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/metrics/histogram.h" |
| 11 #include "base/strings/stringprintf.h" |
| 12 #include "base/task_runner_util.h" |
9 #include "dbus/bus.h" | 13 #include "dbus/bus.h" |
| 14 #include "dbus/dbus_statistics.h" |
10 #include "dbus/message.h" | 15 #include "dbus/message.h" |
11 #include "dbus/object_proxy.h" | 16 #include "dbus/object_proxy.h" |
12 #include "dbus/property.h" | 17 #include "dbus/property.h" |
| 18 #include "dbus/scoped_dbus_error.h" |
| 19 #include "dbus/util.h" |
13 | 20 |
14 namespace dbus { | 21 namespace dbus { |
15 | 22 |
16 ObjectManager::Object::Object() | 23 ObjectManager::Object::Object() |
17 : object_proxy(NULL) { | 24 : object_proxy(NULL) { |
18 } | 25 } |
19 | 26 |
20 ObjectManager::Object::~Object() { | 27 ObjectManager::Object::~Object() { |
21 } | 28 } |
22 | 29 |
23 ObjectManager::ObjectManager(Bus* bus, | 30 ObjectManager::ObjectManager(Bus* bus, |
24 const std::string& service_name, | 31 const std::string& service_name, |
25 const ObjectPath& object_path) | 32 const ObjectPath& object_path) |
26 : bus_(bus), | 33 : bus_(bus), |
27 service_name_(service_name), | 34 service_name_(service_name), |
28 object_path_(object_path), | 35 object_path_(object_path), |
| 36 setup_success_(false), |
| 37 cleanup_called_(false), |
29 weak_ptr_factory_(this) { | 38 weak_ptr_factory_(this) { |
30 DVLOG(1) << "Creating ObjectManager for " << service_name_ | 39 DVLOG(1) << "Creating ObjectManager for " << service_name_ |
31 << " " << object_path_.value(); | 40 << " " << object_path_.value(); |
32 | |
33 DCHECK(bus_); | 41 DCHECK(bus_); |
| 42 bus_->AssertOnOriginThread(); |
34 object_proxy_ = bus_->GetObjectProxy(service_name_, object_path_); | 43 object_proxy_ = bus_->GetObjectProxy(service_name_, object_path_); |
35 object_proxy_->SetNameOwnerChangedCallback( | 44 object_proxy_->SetNameOwnerChangedCallback( |
36 base::Bind(&ObjectManager::NameOwnerChanged, | 45 base::Bind(&ObjectManager::NameOwnerChanged, |
37 weak_ptr_factory_.GetWeakPtr())); | 46 weak_ptr_factory_.GetWeakPtr())); |
38 | 47 |
39 object_proxy_->ConnectToSignal( | 48 // Set up a match rule and a filter function to handle PropertiesChanged |
40 kObjectManagerInterface, | 49 // signals from the service. This is important to avoid any race conditions |
41 kObjectManagerInterfacesAdded, | 50 // that might cause us to miss PropertiesChanged signals once all objects are |
42 base::Bind(&ObjectManager::InterfacesAddedReceived, | 51 // initialized via GetManagedObjects. |
43 weak_ptr_factory_.GetWeakPtr()), | 52 base::PostTaskAndReplyWithResult( |
44 base::Bind(&ObjectManager::InterfacesAddedConnected, | 53 bus_->GetDBusTaskRunner(), |
45 weak_ptr_factory_.GetWeakPtr())); | 54 FROM_HERE, |
46 | 55 base::Bind(&ObjectManager::SetupMatchRuleAndFilter, this), |
47 object_proxy_->ConnectToSignal( | 56 base::Bind(&ObjectManager::OnSetupMatchRuleAndFilterComplete, this)); |
48 kObjectManagerInterface, | |
49 kObjectManagerInterfacesRemoved, | |
50 base::Bind(&ObjectManager::InterfacesRemovedReceived, | |
51 weak_ptr_factory_.GetWeakPtr()), | |
52 base::Bind(&ObjectManager::InterfacesRemovedConnected, | |
53 weak_ptr_factory_.GetWeakPtr())); | |
54 | |
55 GetManagedObjects(); | |
56 } | 57 } |
57 | 58 |
58 ObjectManager::~ObjectManager() { | 59 ObjectManager::~ObjectManager() { |
59 // Clean up Object structures | 60 // Clean up Object structures |
60 for (ObjectMap::iterator iter = object_map_.begin(); | 61 for (ObjectMap::iterator iter = object_map_.begin(); |
61 iter != object_map_.end(); ++iter) { | 62 iter != object_map_.end(); ++iter) { |
62 Object* object = iter->second; | 63 Object* object = iter->second; |
63 | 64 |
64 for (Object::PropertiesMap::iterator piter = object->properties_map.begin(); | 65 for (Object::PropertiesMap::iterator piter = object->properties_map.begin(); |
65 piter != object->properties_map.end(); ++piter) { | 66 piter != object->properties_map.end(); ++piter) { |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 MethodCall method_call(kObjectManagerInterface, | 138 MethodCall method_call(kObjectManagerInterface, |
138 kObjectManagerGetManagedObjects); | 139 kObjectManagerGetManagedObjects); |
139 | 140 |
140 object_proxy_->CallMethod( | 141 object_proxy_->CallMethod( |
141 &method_call, | 142 &method_call, |
142 ObjectProxy::TIMEOUT_USE_DEFAULT, | 143 ObjectProxy::TIMEOUT_USE_DEFAULT, |
143 base::Bind(&ObjectManager::OnGetManagedObjects, | 144 base::Bind(&ObjectManager::OnGetManagedObjects, |
144 weak_ptr_factory_.GetWeakPtr())); | 145 weak_ptr_factory_.GetWeakPtr())); |
145 } | 146 } |
146 | 147 |
| 148 void ObjectManager::CleanUp() { |
| 149 DCHECK(bus_); |
| 150 bus_->AssertOnDBusThread(); |
| 151 DCHECK(!cleanup_called_); |
| 152 |
| 153 cleanup_called_ = true; |
| 154 |
| 155 if (!setup_success_) |
| 156 return; |
| 157 |
| 158 if (!bus_->RemoveFilterFunction(&ObjectManager::HandleMessageThunk, this)) |
| 159 LOG(ERROR) << "Failed to remove filter function"; |
| 160 |
| 161 ScopedDBusError error; |
| 162 bus_->RemoveMatch(match_rule_, error.get()); |
| 163 if (error.is_set()) |
| 164 LOG(ERROR) << "Failed to remove match rule: " << match_rule_; |
| 165 |
| 166 match_rule_.clear(); |
| 167 } |
| 168 |
| 169 void ObjectManager::InitializeObjects() { |
| 170 DCHECK(bus_); |
| 171 DCHECK(object_proxy_); |
| 172 DCHECK(setup_success_); |
| 173 |
| 174 // |object_proxy_| is no longer valid if the Bus was shut down before this |
| 175 // call. Don't initiate any other action from the origin thread. |
| 176 if (cleanup_called_) |
| 177 return; |
| 178 |
| 179 object_proxy_->ConnectToSignal( |
| 180 kObjectManagerInterface, |
| 181 kObjectManagerInterfacesAdded, |
| 182 base::Bind(&ObjectManager::InterfacesAddedReceived, |
| 183 weak_ptr_factory_.GetWeakPtr()), |
| 184 base::Bind(&ObjectManager::InterfacesAddedConnected, |
| 185 weak_ptr_factory_.GetWeakPtr())); |
| 186 |
| 187 object_proxy_->ConnectToSignal( |
| 188 kObjectManagerInterface, |
| 189 kObjectManagerInterfacesRemoved, |
| 190 base::Bind(&ObjectManager::InterfacesRemovedReceived, |
| 191 weak_ptr_factory_.GetWeakPtr()), |
| 192 base::Bind(&ObjectManager::InterfacesRemovedConnected, |
| 193 weak_ptr_factory_.GetWeakPtr())); |
| 194 |
| 195 GetManagedObjects(); |
| 196 } |
| 197 |
| 198 bool ObjectManager::SetupMatchRuleAndFilter() { |
| 199 DCHECK(bus_); |
| 200 DCHECK(!setup_success_); |
| 201 bus_->AssertOnDBusThread(); |
| 202 |
| 203 if (cleanup_called_) |
| 204 return false; |
| 205 |
| 206 if (!bus_->Connect() || !bus_->SetUpAsyncOperations()) |
| 207 return false; |
| 208 |
| 209 service_name_owner_ = |
| 210 bus_->GetServiceOwnerAndBlock(service_name_, Bus::SUPPRESS_ERRORS); |
| 211 |
| 212 const std::string match_rule = |
| 213 base::StringPrintf( |
| 214 "type='signal', sender='%s', interface='%s', member='%s'", |
| 215 service_name_.c_str(), |
| 216 kPropertiesInterface, |
| 217 kPropertiesChanged); |
| 218 |
| 219 if (!bus_->AddFilterFunction(&ObjectManager::HandleMessageThunk, this)) { |
| 220 LOG(ERROR) << "ObjectManager failed to add filter function"; |
| 221 return false; |
| 222 } |
| 223 |
| 224 ScopedDBusError error; |
| 225 bus_->AddMatch(match_rule, error.get()); |
| 226 if (error.is_set()) { |
| 227 LOG(ERROR) << "ObjectManager failed to add match rule \"" << match_rule |
| 228 << "\". Got " << error.name() << ": " << error.message(); |
| 229 bus_->RemoveFilterFunction(&ObjectManager::HandleMessageThunk, this); |
| 230 return false; |
| 231 } |
| 232 |
| 233 match_rule_ = match_rule; |
| 234 setup_success_ = true; |
| 235 |
| 236 return true; |
| 237 } |
| 238 |
| 239 void ObjectManager::OnSetupMatchRuleAndFilterComplete(bool success) { |
| 240 LOG_IF(WARNING, !success) << service_name_ << " " << object_path_.value() |
| 241 << ": Failed to set up match rule."; |
| 242 if (success) |
| 243 InitializeObjects(); |
| 244 } |
| 245 |
| 246 // static |
| 247 DBusHandlerResult ObjectManager::HandleMessageThunk(DBusConnection* connection, |
| 248 DBusMessage* raw_message, |
| 249 void* user_data) { |
| 250 ObjectManager* self = reinterpret_cast<ObjectManager*>(user_data); |
| 251 return self->HandleMessage(connection, raw_message); |
| 252 } |
| 253 |
| 254 DBusHandlerResult ObjectManager::HandleMessage(DBusConnection* connection, |
| 255 DBusMessage* raw_message) { |
| 256 DCHECK(bus_); |
| 257 bus_->AssertOnDBusThread(); |
| 258 |
| 259 if (dbus_message_get_type(raw_message) != DBUS_MESSAGE_TYPE_SIGNAL) |
| 260 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 261 |
| 262 // raw_message will be unrefed on exit of the function. Increment the |
| 263 // reference so we can use it in Signal. |
| 264 dbus_message_ref(raw_message); |
| 265 scoped_ptr<Signal> signal( |
| 266 Signal::FromRawMessage(raw_message)); |
| 267 |
| 268 const std::string interface = signal->GetInterface(); |
| 269 const std::string member = signal->GetMember(); |
| 270 |
| 271 statistics::AddReceivedSignal(service_name_, interface, member); |
| 272 |
| 273 // Only handle the PropertiesChanged signal. |
| 274 const std::string absolute_signal_name = |
| 275 GetAbsoluteMemberName(interface, member); |
| 276 const std::string properties_changed_signal_name = |
| 277 GetAbsoluteMemberName(kPropertiesInterface, kPropertiesChanged); |
| 278 if (absolute_signal_name != properties_changed_signal_name) |
| 279 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 280 |
| 281 VLOG(1) << "Signal received: " << signal->ToString(); |
| 282 |
| 283 // Make sure that the signal originated from the correct sender. |
| 284 std::string sender = signal->GetSender(); |
| 285 if (service_name_owner_ != sender) { |
| 286 LOG(ERROR) << "Rejecting a message from a wrong sender."; |
| 287 UMA_HISTOGRAM_COUNTS("DBus.RejectedSignalCount", 1); |
| 288 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 289 } |
| 290 |
| 291 const ObjectPath path = signal->GetPath(); |
| 292 |
| 293 if (bus_->HasDBusThread()) { |
| 294 // Post a task to run the method in the origin thread. Transfer ownership of |
| 295 // |signal| to NotifyPropertiesChanged, which will handle the clean up. |
| 296 Signal* released_signal = signal.release(); |
| 297 bus_->GetOriginTaskRunner()->PostTask( |
| 298 FROM_HERE, |
| 299 base::Bind(&ObjectManager::NotifyPropertiesChanged, |
| 300 this, path, |
| 301 released_signal)); |
| 302 } else { |
| 303 // If the D-Bus thread is not used, just call the callback on the |
| 304 // current thread. Transfer the ownership of |signal| to |
| 305 // NotifyPropertiesChanged. |
| 306 NotifyPropertiesChanged(path, signal.release()); |
| 307 } |
| 308 |
| 309 // We don't return DBUS_HANDLER_RESULT_HANDLED for signals because other |
| 310 // objects may be interested in them. (e.g. Signals from org.freedesktop.DBus) |
| 311 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 312 } |
| 313 |
| 314 void ObjectManager::NotifyPropertiesChanged( |
| 315 const dbus::ObjectPath object_path, |
| 316 Signal* signal) { |
| 317 DCHECK(bus_); |
| 318 bus_->AssertOnOriginThread(); |
| 319 |
| 320 NotifyPropertiesChangedHelper(object_path, signal); |
| 321 |
| 322 // Delete the message on the D-Bus thread. See comments in HandleMessage. |
| 323 bus_->GetDBusTaskRunner()->PostTask( |
| 324 FROM_HERE, |
| 325 base::Bind(&base::DeletePointer<Signal>, signal)); |
| 326 } |
| 327 |
| 328 void ObjectManager::NotifyPropertiesChangedHelper( |
| 329 const dbus::ObjectPath object_path, |
| 330 Signal* signal) { |
| 331 DCHECK(bus_); |
| 332 bus_->AssertOnOriginThread(); |
| 333 |
| 334 MessageReader reader(signal); |
| 335 std::string interface; |
| 336 if (!reader.PopString(&interface)) { |
| 337 LOG(WARNING) << "Property changed signal has wrong parameters: " |
| 338 << "expected interface name: " << signal->ToString(); |
| 339 return; |
| 340 } |
| 341 |
| 342 PropertySet* properties = GetProperties(object_path, interface); |
| 343 if (properties) |
| 344 properties->ChangedReceived(signal); |
| 345 } |
| 346 |
147 void ObjectManager::OnGetManagedObjects(Response* response) { | 347 void ObjectManager::OnGetManagedObjects(Response* response) { |
148 if (response != NULL) { | 348 if (response != NULL) { |
149 MessageReader reader(response); | 349 MessageReader reader(response); |
150 MessageReader array_reader(NULL); | 350 MessageReader array_reader(NULL); |
151 if (!reader.PopArray(&array_reader)) | 351 if (!reader.PopArray(&array_reader)) |
152 return; | 352 return; |
153 | 353 |
154 while (array_reader.HasMoreData()) { | 354 while (array_reader.HasMoreData()) { |
155 MessageReader dict_entry_reader(NULL); | 355 MessageReader dict_entry_reader(NULL); |
156 ObjectPath object_path; | 356 ObjectPath object_path; |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 object = oiter->second; | 450 object = oiter->second; |
251 | 451 |
252 Object::PropertiesMap::iterator piter = | 452 Object::PropertiesMap::iterator piter = |
253 object->properties_map.find(interface_name); | 453 object->properties_map.find(interface_name); |
254 PropertySet* property_set; | 454 PropertySet* property_set; |
255 const bool interface_added = (piter == object->properties_map.end()); | 455 const bool interface_added = (piter == object->properties_map.end()); |
256 if (interface_added) { | 456 if (interface_added) { |
257 property_set = object->properties_map[interface_name] = | 457 property_set = object->properties_map[interface_name] = |
258 interface->CreateProperties(object->object_proxy, | 458 interface->CreateProperties(object->object_proxy, |
259 object_path, interface_name); | 459 object_path, interface_name); |
260 property_set->ConnectSignals(); | |
261 } else | 460 } else |
262 property_set = piter->second; | 461 property_set = piter->second; |
263 | 462 |
264 property_set->UpdatePropertiesFromReader(reader); | 463 property_set->UpdatePropertiesFromReader(reader); |
265 | 464 |
266 if (interface_added) | 465 if (interface_added) |
267 interface->ObjectAdded(object_path, interface_name); | 466 interface->ObjectAdded(object_path, interface_name); |
268 } | 467 } |
269 | 468 |
270 void ObjectManager::RemoveInterface(const ObjectPath& object_path, | 469 void ObjectManager::RemoveInterface(const ObjectPath& object_path, |
(...skipping 19 matching lines...) Expand all Loading... |
290 object->properties_map.erase(piter); | 489 object->properties_map.erase(piter); |
291 | 490 |
292 if (object->properties_map.empty()) { | 491 if (object->properties_map.empty()) { |
293 object_map_.erase(oiter); | 492 object_map_.erase(oiter); |
294 delete object; | 493 delete object; |
295 } | 494 } |
296 } | 495 } |
297 | 496 |
298 void ObjectManager::NameOwnerChanged(const std::string& old_owner, | 497 void ObjectManager::NameOwnerChanged(const std::string& old_owner, |
299 const std::string& new_owner) { | 498 const std::string& new_owner) { |
| 499 service_name_owner_ = new_owner; |
| 500 |
300 if (!old_owner.empty()) { | 501 if (!old_owner.empty()) { |
301 ObjectMap::iterator iter = object_map_.begin(); | 502 ObjectMap::iterator iter = object_map_.begin(); |
302 while (iter != object_map_.end()) { | 503 while (iter != object_map_.end()) { |
303 ObjectMap::iterator tmp = iter; | 504 ObjectMap::iterator tmp = iter; |
304 ++iter; | 505 ++iter; |
305 | 506 |
306 // PropertiesMap is mutated by RemoveInterface, and also Object is | 507 // PropertiesMap is mutated by RemoveInterface, and also Object is |
307 // destroyed; easier to collect the object path and interface names | 508 // destroyed; easier to collect the object path and interface names |
308 // and remove them safely. | 509 // and remove them safely. |
309 const dbus::ObjectPath object_path = tmp->first; | 510 const dbus::ObjectPath object_path = tmp->first; |
(...skipping 10 matching lines...) Expand all Loading... |
320 RemoveInterface(object_path, *iiter); | 521 RemoveInterface(object_path, *iiter); |
321 } | 522 } |
322 | 523 |
323 } | 524 } |
324 | 525 |
325 if (!new_owner.empty()) | 526 if (!new_owner.empty()) |
326 GetManagedObjects(); | 527 GetManagedObjects(); |
327 } | 528 } |
328 | 529 |
329 } // namespace dbus | 530 } // namespace dbus |
OLD | NEW |