keybuk@chromium.org | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "dbus/object_manager.h" |
| 6 | |
| 7 | #include "base/bind.h" |
armansito | ebff093d | 2014-09-05 17:49:34 | [diff] [blame] | 8 | #include "base/location.h" |
keybuk@chromium.org | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 9 | #include "base/logging.h" |
armansito | ebff093d | 2014-09-05 17:49:34 | [diff] [blame] | 10 | #include "base/metrics/histogram.h" |
| 11 | #include "base/strings/stringprintf.h" |
| 12 | #include "base/task_runner_util.h" |
keybuk@chromium.org | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 13 | #include "dbus/bus.h" |
armansito | ebff093d | 2014-09-05 17:49:34 | [diff] [blame] | 14 | #include "dbus/dbus_statistics.h" |
keybuk@chromium.org | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 15 | #include "dbus/message.h" |
| 16 | #include "dbus/object_proxy.h" |
| 17 | #include "dbus/property.h" |
armansito | ebff093d | 2014-09-05 17:49:34 | [diff] [blame] | 18 | #include "dbus/scoped_dbus_error.h" |
| 19 | #include "dbus/util.h" |
keybuk@chromium.org | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 20 | |
| 21 | namespace dbus { |
| 22 | |
| 23 | ObjectManager::Object::Object() |
| 24 | : object_proxy(NULL) { |
| 25 | } |
| 26 | |
| 27 | ObjectManager::Object::~Object() { |
| 28 | } |
| 29 | |
| 30 | ObjectManager::ObjectManager(Bus* bus, |
| 31 | const std::string& service_name, |
| 32 | const ObjectPath& object_path) |
| 33 | : bus_(bus), |
| 34 | service_name_(service_name), |
| 35 | object_path_(object_path), |
armansito | ebff093d | 2014-09-05 17:49:34 | [diff] [blame] | 36 | setup_success_(false), |
| 37 | cleanup_called_(false), |
keybuk@chromium.org | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 38 | weak_ptr_factory_(this) { |
| 39 | DVLOG(1) << "Creating ObjectManager for " << service_name_ |
| 40 | << " " << object_path_.value(); |
keybuk@chromium.org | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 41 | DCHECK(bus_); |
armansito | ebff093d | 2014-09-05 17:49:34 | [diff] [blame] | 42 | bus_->AssertOnOriginThread(); |
keybuk@chromium.org | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 43 | object_proxy_ = bus_->GetObjectProxy(service_name_, object_path_); |
keybuk@chromium.org | 043fb8c | 2014-03-07 02:24:33 | [diff] [blame] | 44 | object_proxy_->SetNameOwnerChangedCallback( |
| 45 | base::Bind(&ObjectManager::NameOwnerChanged, |
| 46 | weak_ptr_factory_.GetWeakPtr())); |
keybuk@chromium.org | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 47 | |
armansito | ebff093d | 2014-09-05 17:49:34 | [diff] [blame] | 48 | // Set up a match rule and a filter function to handle PropertiesChanged |
| 49 | // signals from the service. This is important to avoid any race conditions |
| 50 | // that might cause us to miss PropertiesChanged signals once all objects are |
| 51 | // initialized via GetManagedObjects. |
| 52 | base::PostTaskAndReplyWithResult( |
| 53 | bus_->GetDBusTaskRunner(), |
| 54 | FROM_HERE, |
| 55 | base::Bind(&ObjectManager::SetupMatchRuleAndFilter, this), |
| 56 | base::Bind(&ObjectManager::OnSetupMatchRuleAndFilterComplete, this)); |
keybuk@chromium.org | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | ObjectManager::~ObjectManager() { |
| 60 | // Clean up Object structures |
| 61 | for (ObjectMap::iterator iter = object_map_.begin(); |
| 62 | iter != object_map_.end(); ++iter) { |
| 63 | Object* object = iter->second; |
| 64 | |
| 65 | for (Object::PropertiesMap::iterator piter = object->properties_map.begin(); |
| 66 | piter != object->properties_map.end(); ++piter) { |
| 67 | PropertySet* properties = piter->second; |
| 68 | delete properties; |
| 69 | } |
| 70 | |
| 71 | delete object; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void ObjectManager::RegisterInterface(const std::string& interface_name, |
| 76 | Interface* interface) { |
| 77 | interface_map_[interface_name] = interface; |
| 78 | } |
| 79 | |
| 80 | void ObjectManager::UnregisterInterface(const std::string& interface_name) { |
| 81 | InterfaceMap::iterator iter = interface_map_.find(interface_name); |
| 82 | if (iter != interface_map_.end()) |
| 83 | interface_map_.erase(iter); |
| 84 | } |
| 85 | |
| 86 | std::vector<ObjectPath> ObjectManager::GetObjects() { |
| 87 | std::vector<ObjectPath> object_paths; |
| 88 | |
| 89 | for (ObjectMap::iterator iter = object_map_.begin(); |
| 90 | iter != object_map_.end(); ++iter) |
| 91 | object_paths.push_back(iter->first); |
| 92 | |
| 93 | return object_paths; |
| 94 | } |
| 95 | |
| 96 | std::vector<ObjectPath> ObjectManager::GetObjectsWithInterface( |
| 97 | const std::string& interface_name) { |
| 98 | std::vector<ObjectPath> object_paths; |
| 99 | |
| 100 | for (ObjectMap::iterator oiter = object_map_.begin(); |
| 101 | oiter != object_map_.end(); ++oiter) { |
| 102 | Object* object = oiter->second; |
| 103 | |
| 104 | Object::PropertiesMap::iterator piter = |
| 105 | object->properties_map.find(interface_name); |
| 106 | if (piter != object->properties_map.end()) |
| 107 | object_paths.push_back(oiter->first); |
| 108 | } |
| 109 | |
| 110 | return object_paths; |
| 111 | } |
| 112 | |
| 113 | ObjectProxy* ObjectManager::GetObjectProxy(const ObjectPath& object_path) { |
| 114 | ObjectMap::iterator iter = object_map_.find(object_path); |
| 115 | if (iter == object_map_.end()) |
| 116 | return NULL; |
| 117 | |
| 118 | Object* object = iter->second; |
| 119 | return object->object_proxy; |
| 120 | } |
| 121 | |
| 122 | PropertySet* ObjectManager::GetProperties(const ObjectPath& object_path, |
| 123 | const std::string& interface_name) { |
| 124 | ObjectMap::iterator iter = object_map_.find(object_path); |
| 125 | if (iter == object_map_.end()) |
| 126 | return NULL; |
| 127 | |
| 128 | Object* object = iter->second; |
| 129 | Object::PropertiesMap::iterator piter = |
| 130 | object->properties_map.find(interface_name); |
| 131 | if (piter == object->properties_map.end()) |
| 132 | return NULL; |
| 133 | |
| 134 | return piter->second; |
| 135 | } |
| 136 | |
| 137 | void ObjectManager::GetManagedObjects() { |
| 138 | MethodCall method_call(kObjectManagerInterface, |
| 139 | kObjectManagerGetManagedObjects); |
| 140 | |
| 141 | object_proxy_->CallMethod( |
| 142 | &method_call, |
| 143 | ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 144 | base::Bind(&ObjectManager::OnGetManagedObjects, |
| 145 | weak_ptr_factory_.GetWeakPtr())); |
armansito | ebff093d | 2014-09-05 17:49:34 | [diff] [blame] | 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 | |
hashimoto | b387031 | 2014-12-04 07:41:55 | [diff] [blame] | 158 | bus_->RemoveFilterFunction(&ObjectManager::HandleMessageThunk, this); |
armansito | ebff093d | 2014-09-05 17:49:34 | [diff] [blame] | 159 | |
| 160 | ScopedDBusError error; |
| 161 | bus_->RemoveMatch(match_rule_, error.get()); |
| 162 | if (error.is_set()) |
| 163 | LOG(ERROR) << "Failed to remove match rule: " << match_rule_; |
| 164 | |
| 165 | match_rule_.clear(); |
| 166 | } |
| 167 | |
| 168 | void ObjectManager::InitializeObjects() { |
| 169 | DCHECK(bus_); |
| 170 | DCHECK(object_proxy_); |
| 171 | DCHECK(setup_success_); |
| 172 | |
| 173 | // |object_proxy_| is no longer valid if the Bus was shut down before this |
| 174 | // call. Don't initiate any other action from the origin thread. |
| 175 | if (cleanup_called_) |
| 176 | return; |
| 177 | |
| 178 | object_proxy_->ConnectToSignal( |
| 179 | kObjectManagerInterface, |
| 180 | kObjectManagerInterfacesAdded, |
| 181 | base::Bind(&ObjectManager::InterfacesAddedReceived, |
| 182 | weak_ptr_factory_.GetWeakPtr()), |
| 183 | base::Bind(&ObjectManager::InterfacesAddedConnected, |
| 184 | weak_ptr_factory_.GetWeakPtr())); |
| 185 | |
| 186 | object_proxy_->ConnectToSignal( |
| 187 | kObjectManagerInterface, |
| 188 | kObjectManagerInterfacesRemoved, |
| 189 | base::Bind(&ObjectManager::InterfacesRemovedReceived, |
| 190 | weak_ptr_factory_.GetWeakPtr()), |
| 191 | base::Bind(&ObjectManager::InterfacesRemovedConnected, |
| 192 | weak_ptr_factory_.GetWeakPtr())); |
| 193 | |
| 194 | GetManagedObjects(); |
| 195 | } |
| 196 | |
| 197 | bool ObjectManager::SetupMatchRuleAndFilter() { |
| 198 | DCHECK(bus_); |
| 199 | DCHECK(!setup_success_); |
| 200 | bus_->AssertOnDBusThread(); |
| 201 | |
| 202 | if (cleanup_called_) |
| 203 | return false; |
| 204 | |
| 205 | if (!bus_->Connect() || !bus_->SetUpAsyncOperations()) |
| 206 | return false; |
| 207 | |
| 208 | service_name_owner_ = |
| 209 | bus_->GetServiceOwnerAndBlock(service_name_, Bus::SUPPRESS_ERRORS); |
| 210 | |
| 211 | const std::string match_rule = |
| 212 | base::StringPrintf( |
| 213 | "type='signal', sender='%s', interface='%s', member='%s'", |
| 214 | service_name_.c_str(), |
| 215 | kPropertiesInterface, |
| 216 | kPropertiesChanged); |
| 217 | |
hashimoto | b387031 | 2014-12-04 07:41:55 | [diff] [blame] | 218 | bus_->AddFilterFunction(&ObjectManager::HandleMessageThunk, this); |
armansito | ebff093d | 2014-09-05 17:49:34 | [diff] [blame] | 219 | |
| 220 | ScopedDBusError error; |
| 221 | bus_->AddMatch(match_rule, error.get()); |
| 222 | if (error.is_set()) { |
| 223 | LOG(ERROR) << "ObjectManager failed to add match rule \"" << match_rule |
| 224 | << "\". Got " << error.name() << ": " << error.message(); |
| 225 | bus_->RemoveFilterFunction(&ObjectManager::HandleMessageThunk, this); |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | match_rule_ = match_rule; |
| 230 | setup_success_ = true; |
| 231 | |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | void ObjectManager::OnSetupMatchRuleAndFilterComplete(bool success) { |
| 236 | LOG_IF(WARNING, !success) << service_name_ << " " << object_path_.value() |
| 237 | << ": Failed to set up match rule."; |
| 238 | if (success) |
| 239 | InitializeObjects(); |
| 240 | } |
| 241 | |
| 242 | // static |
| 243 | DBusHandlerResult ObjectManager::HandleMessageThunk(DBusConnection* connection, |
| 244 | DBusMessage* raw_message, |
| 245 | void* user_data) { |
| 246 | ObjectManager* self = reinterpret_cast<ObjectManager*>(user_data); |
| 247 | return self->HandleMessage(connection, raw_message); |
| 248 | } |
| 249 | |
| 250 | DBusHandlerResult ObjectManager::HandleMessage(DBusConnection* connection, |
| 251 | DBusMessage* raw_message) { |
| 252 | DCHECK(bus_); |
| 253 | bus_->AssertOnDBusThread(); |
| 254 | |
satorux | 9b780e6b | 2015-08-17 09:44:35 | [diff] [blame^] | 255 | // Handle the message only if it is a signal. |
| 256 | // Note that the match rule in SetupMatchRuleAndFilter() is configured to |
| 257 | // only accept signals, but we check here just in case. |
armansito | ebff093d | 2014-09-05 17:49:34 | [diff] [blame] | 258 | if (dbus_message_get_type(raw_message) != DBUS_MESSAGE_TYPE_SIGNAL) |
| 259 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 260 | |
| 261 | // raw_message will be unrefed on exit of the function. Increment the |
| 262 | // reference so we can use it in Signal. |
| 263 | dbus_message_ref(raw_message); |
| 264 | scoped_ptr<Signal> signal( |
| 265 | Signal::FromRawMessage(raw_message)); |
| 266 | |
| 267 | const std::string interface = signal->GetInterface(); |
| 268 | const std::string member = signal->GetMember(); |
| 269 | |
| 270 | statistics::AddReceivedSignal(service_name_, interface, member); |
| 271 | |
satorux | 9b780e6b | 2015-08-17 09:44:35 | [diff] [blame^] | 272 | // Handle the signal only if it is PropertiesChanged. |
| 273 | // Note that the match rule in SetupMatchRuleAndFilter() is configured to |
| 274 | // only accept PropertiesChanged signals, but we check here just in case. |
armansito | ebff093d | 2014-09-05 17:49:34 | [diff] [blame] | 275 | const std::string absolute_signal_name = |
| 276 | GetAbsoluteMemberName(interface, member); |
| 277 | const std::string properties_changed_signal_name = |
| 278 | GetAbsoluteMemberName(kPropertiesInterface, kPropertiesChanged); |
| 279 | if (absolute_signal_name != properties_changed_signal_name) |
| 280 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 281 | |
| 282 | VLOG(1) << "Signal received: " << signal->ToString(); |
| 283 | |
satorux | 9b780e6b | 2015-08-17 09:44:35 | [diff] [blame^] | 284 | // Handle the signal only if it is from the service that the ObjectManager |
| 285 | // instance is interested in. |
| 286 | // Note that the match rule in SetupMatchRuleAndFilter() is configured to |
| 287 | // only accept messages from the service name of our interest. However, the |
| 288 | // service='...' filter does not work as intended. See crbug.com/507206#14 |
| 289 | // and #15 for details, hence it's necessary to check the sender here. |
armansito | ebff093d | 2014-09-05 17:49:34 | [diff] [blame] | 290 | std::string sender = signal->GetSender(); |
satorux | 9b780e6b | 2015-08-17 09:44:35 | [diff] [blame^] | 291 | if (service_name_owner_ != sender) |
armansito | ebff093d | 2014-09-05 17:49:34 | [diff] [blame] | 292 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
armansito | ebff093d | 2014-09-05 17:49:34 | [diff] [blame] | 293 | |
| 294 | const ObjectPath path = signal->GetPath(); |
| 295 | |
| 296 | if (bus_->HasDBusThread()) { |
| 297 | // Post a task to run the method in the origin thread. Transfer ownership of |
| 298 | // |signal| to NotifyPropertiesChanged, which will handle the clean up. |
| 299 | Signal* released_signal = signal.release(); |
| 300 | bus_->GetOriginTaskRunner()->PostTask( |
| 301 | FROM_HERE, |
| 302 | base::Bind(&ObjectManager::NotifyPropertiesChanged, |
| 303 | this, path, |
| 304 | released_signal)); |
| 305 | } else { |
| 306 | // If the D-Bus thread is not used, just call the callback on the |
| 307 | // current thread. Transfer the ownership of |signal| to |
| 308 | // NotifyPropertiesChanged. |
| 309 | NotifyPropertiesChanged(path, signal.release()); |
| 310 | } |
| 311 | |
| 312 | // We don't return DBUS_HANDLER_RESULT_HANDLED for signals because other |
| 313 | // objects may be interested in them. (e.g. Signals from org.freedesktop.DBus) |
| 314 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 315 | } |
| 316 | |
| 317 | void ObjectManager::NotifyPropertiesChanged( |
| 318 | const dbus::ObjectPath object_path, |
| 319 | Signal* signal) { |
| 320 | DCHECK(bus_); |
| 321 | bus_->AssertOnOriginThread(); |
| 322 | |
| 323 | NotifyPropertiesChangedHelper(object_path, signal); |
| 324 | |
| 325 | // Delete the message on the D-Bus thread. See comments in HandleMessage. |
| 326 | bus_->GetDBusTaskRunner()->PostTask( |
| 327 | FROM_HERE, |
| 328 | base::Bind(&base::DeletePointer<Signal>, signal)); |
| 329 | } |
| 330 | |
| 331 | void ObjectManager::NotifyPropertiesChangedHelper( |
| 332 | const dbus::ObjectPath object_path, |
| 333 | Signal* signal) { |
| 334 | DCHECK(bus_); |
| 335 | bus_->AssertOnOriginThread(); |
| 336 | |
| 337 | MessageReader reader(signal); |
| 338 | std::string interface; |
| 339 | if (!reader.PopString(&interface)) { |
| 340 | LOG(WARNING) << "Property changed signal has wrong parameters: " |
| 341 | << "expected interface name: " << signal->ToString(); |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | PropertySet* properties = GetProperties(object_path, interface); |
| 346 | if (properties) |
| 347 | properties->ChangedReceived(signal); |
keybuk@chromium.org | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | void ObjectManager::OnGetManagedObjects(Response* response) { |
| 351 | if (response != NULL) { |
| 352 | MessageReader reader(response); |
| 353 | MessageReader array_reader(NULL); |
| 354 | if (!reader.PopArray(&array_reader)) |
| 355 | return; |
| 356 | |
| 357 | while (array_reader.HasMoreData()) { |
| 358 | MessageReader dict_entry_reader(NULL); |
| 359 | ObjectPath object_path; |
| 360 | if (!array_reader.PopDictEntry(&dict_entry_reader) || |
| 361 | !dict_entry_reader.PopObjectPath(&object_path)) |
| 362 | continue; |
| 363 | |
| 364 | UpdateObject(object_path, &dict_entry_reader); |
| 365 | } |
| 366 | |
| 367 | } else { |
| 368 | LOG(WARNING) << service_name_ << " " << object_path_.value() |
| 369 | << ": Failed to get managed objects"; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | void ObjectManager::InterfacesAddedReceived(Signal* signal) { |
| 374 | DCHECK(signal); |
| 375 | MessageReader reader(signal); |
| 376 | ObjectPath object_path; |
| 377 | if (!reader.PopObjectPath(&object_path)) { |
| 378 | LOG(WARNING) << service_name_ << " " << object_path_.value() |
| 379 | << ": InterfacesAdded signal has incorrect parameters: " |
| 380 | << signal->ToString(); |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | UpdateObject(object_path, &reader); |
| 385 | } |
| 386 | |
| 387 | void ObjectManager::InterfacesAddedConnected(const std::string& interface_name, |
| 388 | const std::string& signal_name, |
| 389 | bool success) { |
| 390 | LOG_IF(WARNING, !success) << service_name_ << " " << object_path_.value() |
| 391 | << ": Failed to connect to InterfacesAdded signal."; |
| 392 | } |
| 393 | |
| 394 | void ObjectManager::InterfacesRemovedReceived(Signal* signal) { |
| 395 | DCHECK(signal); |
| 396 | MessageReader reader(signal); |
| 397 | ObjectPath object_path; |
| 398 | std::vector<std::string> interface_names; |
| 399 | if (!reader.PopObjectPath(&object_path) || |
| 400 | !reader.PopArrayOfStrings(&interface_names)) { |
| 401 | LOG(WARNING) << service_name_ << " " << object_path_.value() |
| 402 | << ": InterfacesRemoved signal has incorrect parameters: " |
| 403 | << signal->ToString(); |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | for (size_t i = 0; i < interface_names.size(); ++i) |
| 408 | RemoveInterface(object_path, interface_names[i]); |
| 409 | } |
| 410 | |
| 411 | void ObjectManager::InterfacesRemovedConnected( |
| 412 | const std::string& interface_name, |
| 413 | const std::string& signal_name, |
| 414 | bool success) { |
| 415 | LOG_IF(WARNING, !success) << service_name_ << " " << object_path_.value() |
| 416 | << ": Failed to connect to " |
| 417 | << "InterfacesRemoved signal."; |
| 418 | } |
| 419 | |
| 420 | void ObjectManager::UpdateObject(const ObjectPath& object_path, |
| 421 | MessageReader* reader) { |
| 422 | DCHECK(reader); |
| 423 | MessageReader array_reader(NULL); |
| 424 | if (!reader->PopArray(&array_reader)) |
| 425 | return; |
| 426 | |
| 427 | while (array_reader.HasMoreData()) { |
| 428 | MessageReader dict_entry_reader(NULL); |
| 429 | std::string interface_name; |
| 430 | if (!array_reader.PopDictEntry(&dict_entry_reader) || |
| 431 | !dict_entry_reader.PopString(&interface_name)) |
| 432 | continue; |
| 433 | |
| 434 | AddInterface(object_path, interface_name, &dict_entry_reader); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | |
| 439 | void ObjectManager::AddInterface(const ObjectPath& object_path, |
| 440 | const std::string& interface_name, |
| 441 | MessageReader* reader) { |
| 442 | InterfaceMap::iterator iiter = interface_map_.find(interface_name); |
| 443 | if (iiter == interface_map_.end()) |
| 444 | return; |
| 445 | Interface* interface = iiter->second; |
| 446 | |
| 447 | ObjectMap::iterator oiter = object_map_.find(object_path); |
| 448 | Object* object; |
| 449 | if (oiter == object_map_.end()) { |
| 450 | object = object_map_[object_path] = new Object; |
| 451 | object->object_proxy = bus_->GetObjectProxy(service_name_, object_path); |
| 452 | } else |
| 453 | object = oiter->second; |
| 454 | |
| 455 | Object::PropertiesMap::iterator piter = |
| 456 | object->properties_map.find(interface_name); |
| 457 | PropertySet* property_set; |
| 458 | const bool interface_added = (piter == object->properties_map.end()); |
| 459 | if (interface_added) { |
| 460 | property_set = object->properties_map[interface_name] = |
| 461 | interface->CreateProperties(object->object_proxy, |
| 462 | object_path, interface_name); |
keybuk@chromium.org | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 463 | } else |
| 464 | property_set = piter->second; |
| 465 | |
| 466 | property_set->UpdatePropertiesFromReader(reader); |
| 467 | |
| 468 | if (interface_added) |
| 469 | interface->ObjectAdded(object_path, interface_name); |
| 470 | } |
| 471 | |
| 472 | void ObjectManager::RemoveInterface(const ObjectPath& object_path, |
| 473 | const std::string& interface_name) { |
| 474 | ObjectMap::iterator oiter = object_map_.find(object_path); |
| 475 | if (oiter == object_map_.end()) |
| 476 | return; |
| 477 | Object* object = oiter->second; |
| 478 | |
| 479 | Object::PropertiesMap::iterator piter = |
| 480 | object->properties_map.find(interface_name); |
| 481 | if (piter == object->properties_map.end()) |
| 482 | return; |
| 483 | |
| 484 | // Inform the interface before removing the properties structure or object |
| 485 | // in case it needs details from them to make its own decisions. |
| 486 | InterfaceMap::iterator iiter = interface_map_.find(interface_name); |
| 487 | if (iiter != interface_map_.end()) { |
| 488 | Interface* interface = iiter->second; |
| 489 | interface->ObjectRemoved(object_path, interface_name); |
| 490 | } |
| 491 | |
avakulenko | 9a99965a | 2015-04-02 18:51:32 | [diff] [blame] | 492 | delete piter->second; |
keybuk@chromium.org | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 493 | object->properties_map.erase(piter); |
| 494 | |
| 495 | if (object->properties_map.empty()) { |
| 496 | object_map_.erase(oiter); |
| 497 | delete object; |
| 498 | } |
| 499 | } |
| 500 | |
keybuk@chromium.org | 043fb8c | 2014-03-07 02:24:33 | [diff] [blame] | 501 | void ObjectManager::NameOwnerChanged(const std::string& old_owner, |
| 502 | const std::string& new_owner) { |
armansito | ebff093d | 2014-09-05 17:49:34 | [diff] [blame] | 503 | service_name_owner_ = new_owner; |
| 504 | |
keybuk@chromium.org | 043fb8c | 2014-03-07 02:24:33 | [diff] [blame] | 505 | if (!old_owner.empty()) { |
| 506 | ObjectMap::iterator iter = object_map_.begin(); |
| 507 | while (iter != object_map_.end()) { |
| 508 | ObjectMap::iterator tmp = iter; |
| 509 | ++iter; |
| 510 | |
| 511 | // PropertiesMap is mutated by RemoveInterface, and also Object is |
| 512 | // destroyed; easier to collect the object path and interface names |
| 513 | // and remove them safely. |
| 514 | const dbus::ObjectPath object_path = tmp->first; |
| 515 | Object* object = tmp->second; |
| 516 | std::vector<std::string> interfaces; |
| 517 | |
| 518 | for (Object::PropertiesMap::iterator piter = |
| 519 | object->properties_map.begin(); |
| 520 | piter != object->properties_map.end(); ++piter) |
| 521 | interfaces.push_back(piter->first); |
| 522 | |
| 523 | for (std::vector<std::string>::iterator iiter = interfaces.begin(); |
| 524 | iiter != interfaces.end(); ++iiter) |
| 525 | RemoveInterface(object_path, *iiter); |
| 526 | } |
| 527 | |
| 528 | } |
| 529 | |
| 530 | if (!new_owner.empty()) |
| 531 | GetManagedObjects(); |
| 532 | } |
| 533 | |
keybuk@chromium.org | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 534 | } // namespace dbus |