12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved. 22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file. 42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifndef DBUS_OBJECT_MANAGER_H_ 62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define DBUS_OBJECT_MANAGER_H_ 72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <map> 92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/memory/ref_counted.h" 112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/memory/weak_ptr.h" 122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "dbus/object_path.h" 132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "dbus/property.h" 142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Newer D-Bus services implement the Object Manager interface to inform other 162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// clients about the objects they export, the properties of those objects, and 172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// notification of changes in the set of available objects: 182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// http://dbus.freedesktop.org/doc/dbus-specification.html 192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// #standard-interfaces-objectmanager 202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This interface is very closely tied to the Properties interface, and uses 222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// even more levels of nested dictionaries and variants. In addition to 232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// simplifying implementation, since there tends to be a single object manager 242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// per service, spanning the complete set of objects an interfaces available, 252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// the classes implemented here make dealing with this interface simpler. 262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Except where noted, use of this class replaces the need for the code 282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// documented in dbus/property.h 292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Client implementation classes should begin by deriving from the 312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// dbus::ObjectManager::Interface class, and defining a Properties structure as 322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// documented in dbus/property.h. 332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Example: 352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// class ExampleClient : public dbus::ObjectManager::Interface { 362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// public: 372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// struct Properties : public dbus::PropertySet { 382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// dbus::Property<std::string> name; 392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// dbus::Property<uint16> version; 402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// dbus::Property<dbus::ObjectPath> parent; 412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// dbus::Property<std::vector<std::string> > children; 422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Properties(dbus::ObjectProxy* object_proxy, 442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// const PropertyChangedCallback callback) 452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// : dbus::PropertySet(object_proxy, kExampleInterface, callback) { 462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// RegisterProperty("Name", &name); 472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// RegisterProperty("Version", &version); 482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// RegisterProperty("Parent", &parent); 492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// RegisterProperty("Children", &children); 502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// } 512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// virtual ~Properties() {} 522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// }; 532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// The link between the implementation class and the object manager is set up 552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// in the constructor and removed in the destructor; the class should maintain 562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// a pointer to its object manager for use in other methods and establish 572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// itself as the implementation class for its interface. 582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Example: 602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// explicit ExampleClient::ExampleClient(dbus::Bus* bus) 612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// : bus_(bus), 622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// weak_ptr_factory_(this) { 632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// object_manager_ = bus_->GetObjectManager(kServiceName, kManagerPath); 642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// object_manager_->RegisterInterface(kInterface, this); 652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// } 662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// virtual ExampleClient::~ExampleClient() { 682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// object_manager_->UnregisterInterface(kInterface); 692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// } 702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// The D-Bus thread manager takes care of issuing the necessary call to 722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// GetManagedObjects() after the implementation classes have been set up. 732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// The object manager interface class has one abstract method that must be 752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// implemented by the class to create Properties structures on demand. As well 762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// as implementing this, you will want to implement a public GetProperties() 772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// method. 782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Example: 802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// dbus::PropertySet* CreateProperties(dbus::ObjectProxy* object_proxy, 812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// const std::string& interface_name) 822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// OVERRIDE { 832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Properties* properties = new Properties( 842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// object_proxy, interface_name, 852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// base::Bind(&PropertyChanged, 862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// weak_ptr_factory_.GetWeakPtr(), 872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// object_path)); 882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// return static_cast<dbus::PropertySet*>(properties); 892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// } 902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Properties* GetProperties(const dbus::ObjectPath& object_path) { 922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// return static_cast<Properties*>( 932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// object_manager_->GetProperties(object_path, kInterface)); 942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// } 952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Note that unlike classes that only use dbus/property.h there is no need 972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// to connect signals or obtain the initial values of properties. The object 982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// manager class handles that for you. 992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// PropertyChanged is a method of your own to notify your observers of a change 1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// in your properties, either as a result of a signal from the Properties 1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// interface or from the Object Manager interface. You may also wish to 1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// implement the optional ObjectAdded and ObjectRemoved methods of the class 1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// to likewise notify observers. 1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// When your class needs an object proxy for a given object path, it may 1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// obtain it from the object manager. Unlike the equivalent method on the bus 1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// this will return NULL if the object is not known. 1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// object_proxy = object_manager_->GetObjectProxy(object_path); 1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// if (object_proxy) { 1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// ... 1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// } 1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// There is no need for code using your implementation class to be aware of the 1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// use of object manager behind the scenes, the rules for updating properties 1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// documented in dbus/property.h still apply. 1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace dbus { 1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const char kObjectManagerInterface[] = "org.freedesktop.DBus.ObjectManager"; 1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const char kObjectManagerGetManagedObjects[] = "GetManagedObjects"; 1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const char kObjectManagerInterfacesAdded[] = "InterfacesAdded"; 1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const char kObjectManagerInterfacesRemoved[] = "InterfacesRemoved"; 1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class Bus; 1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class MessageReader; 1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class ObjectProxy; 1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class Response; 1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class Signal; 1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// ObjectManager implements both the D-Bus client components of the D-Bus 1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Object Manager interface, as internal methods, and a public API for 1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// client classes to utilize. 1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class CHROME_DBUS_EXPORT ObjectManager 1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) : public base::RefCountedThreadSafe<ObjectManager> { 1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)public: 1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // ObjectManager::Interface must be implemented by any class wishing to have 1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // its remote objects managed by an ObjectManager. 1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) class Interface { 1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public: 1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual ~Interface() {} 1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Called by ObjectManager to create a Properties structure for the remote 1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // D-Bus object identified by |object_path| and accessibile through 1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // |object_proxy|. The D-Bus interface name |interface_name| is that passed 1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // to RegisterInterface() by the implementation class. 1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // 1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // The implementation class should create and return an instance of its own 1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // subclass of dbus::PropertySet; ObjectManager will then connect signals 1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // and update the properties from its own internal message reader. 1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual PropertySet* CreateProperties( 1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ObjectProxy *object_proxy, 1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const dbus::ObjectPath& object_path, 1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& interface_name) = 0; 1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Called by ObjectManager to inform the implementation class that an 1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // object has been added with the path |object_path|. The D-Bus interface 1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // name |interface_name| is that passed to RegisterInterface() by the 1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // implementation class. 1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // 1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // If a new object implements multiple interfaces, this method will be 1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // called on each interface implementation with differing values of 1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // |interface_name| as appropriate. An implementation class will only 1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // receive multiple calls if it has registered for multiple interfaces. 1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual void ObjectAdded(const ObjectPath& object_path, 1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& interface_name) { } 1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Called by ObjectManager to inform the implementation class than an 1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // object with the path |object_path| has been removed. Ths D-Bus interface 1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // name |interface_name| is that passed to RegisterInterface() by the 1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // implementation class. Multiple interfaces are handled as with 1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // ObjectAdded(). 1742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // 1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // This method will be called before the Properties structure and the 1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // ObjectProxy object for the given interface are cleaned up, it is safe 1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // to retrieve them during removal to vary processing. 1782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual void ObjectRemoved(const ObjectPath& object_path, 1792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& interface_name) { } 1802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) }; 1812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Client code should use Bus::GetObjectManager() instead of this constructor. 1832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ObjectManager(Bus* bus, 1842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& service_name, 1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const ObjectPath& object_path); 1862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Register a client implementation class |interface| for the given D-Bus 1882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // interface named in |interface_name|. That object's CreateProperties() 1892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // method will be used to create instances of dbus::PropertySet* when 1902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // required. 1912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void RegisterInterface(const std::string& interface_name, 1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) Interface* interface); 1932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Unregister the implementation class for the D-Bus interface named in 1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // |interface_name|, objects and properties of this interface will be 1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // ignored. 1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void UnregisterInterface(const std::string& interface_name); 1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Returns a list of object paths, in an undefined order, of objects known 2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // to this manager. 2012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::vector<ObjectPath> GetObjects(); 2022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Returns the list of object paths, in an undefined order, of objects 2042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // implementing the interface named in |interface_name| known to this manager. 2052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::vector<ObjectPath> GetObjectsWithInterface( 2062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& interface_name); 2072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Returns a ObjectProxy pointer for the given |object_path|. Unlike 2092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // the equivalent method on Bus this will return NULL if the object 2102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // manager has not been informed of that object's existance. 2112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ObjectProxy* GetObjectProxy(const ObjectPath& object_path); 2122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Returns a PropertySet* pointer for the given |object_path| and 2142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // |interface_name|, or NULL if the object manager has not been informed of 2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // that object's existance or the interface's properties. The caller should 2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // cast the returned pointer to the appropriate type, e.g.: 2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // static_cast<Properties*>(GetProperties(object_path, my_interface)); 2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) PropertySet* GetProperties(const ObjectPath& object_path, 2192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& interface_name); 2202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Instructs the object manager to refresh its list of managed objects; 2222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // automatically called by the D-Bus thread manager, there should never be 2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // a need to call this manually. 2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void GetManagedObjects(); 2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // Cleans up any match rules and filter functions added by this ObjectManager. 2271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // The Bus object will take care of this so you don't have to do it manually. 2281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // 2291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // BLOCKING CALL. 2301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci void CleanUp(); 2311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 2322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) protected: 2332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual ~ObjectManager(); 2342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private: 2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) friend class base::RefCountedThreadSafe<ObjectManager>; 2372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // Connects the InterfacesAdded and InterfacesRemoved signals and calls 2391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // GetManagedObjects. Called from OnSetupMatchRuleAndFilterComplete. 2401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci void InitializeObjects(); 2411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 2421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // Called from the constructor to add a match rule for PropertiesChanged 2431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // signals on the DBus thread and set up a corresponding filter function. 2441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci bool SetupMatchRuleAndFilter(); 2451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 2461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // Called on the origin thread once the match rule and filter have been set 2471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // up. |success| is false, if an error occurred during set up; it's true 2481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // otherwise. 2491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci void OnSetupMatchRuleAndFilterComplete(bool success); 2501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 2511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // Called by dbus:: when a message is received. This is used to filter 2521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // PropertiesChanged signals from the correct sender and relay the event to 2531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // the correct PropertySet. 2541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci static DBusHandlerResult HandleMessageThunk(DBusConnection* connection, 2551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci DBusMessage* raw_message, 2561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci void* user_data); 2571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci DBusHandlerResult HandleMessage(DBusConnection* connection, 2581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci DBusMessage* raw_message); 2591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 2601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // Called when a PropertiesChanged signal is received from the sender. 2611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // This method notifies the relevant PropertySet that it should update its 2621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // properties based on the received signal. Called from HandleMessage. 2631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci void NotifyPropertiesChanged(const dbus::ObjectPath object_path, 2641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci Signal* signal); 2651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci void NotifyPropertiesChangedHelper(const dbus::ObjectPath object_path, 2661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci Signal* signal); 2672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Called by dbus:: in response to the GetManagedObjects() method call. 2692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void OnGetManagedObjects(Response* response); 2702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Called by dbus:: when an InterfacesAdded signal is received and initially 2722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // connected. 2732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void InterfacesAddedReceived(Signal* signal); 2742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void InterfacesAddedConnected(const std::string& interface_name, 2752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& signal_name, 2762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool success); 2772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Called by dbus:: when an InterfacesRemoved signal is received and 2792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // initially connected. 2802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void InterfacesRemovedReceived(Signal* signal); 2812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void InterfacesRemovedConnected(const std::string& interface_name, 2822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& signal_name, 2832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool success); 2842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Updates the map entry for the object with path |object_path| using the 2862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // D-Bus message in |reader|, which should consist of an dictionary mapping 2872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // interface names to properties dictionaries as recieved by both the 2882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // GetManagedObjects() method return and the InterfacesAdded() signal. 2892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void UpdateObject(const ObjectPath& object_path, MessageReader* reader); 2902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Updates the properties structure of the object with path |object_path| 2922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // for the interface named |interface_name| using the D-Bus message in 2932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // |reader| which should consist of the properties dictionary for that 2942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // interface. 2952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // 2962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Called by UpdateObjects() for each interface in the dictionary; this 2972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // method takes care of both creating the entry in the ObjectMap and 2982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // ObjectProxy if required, as well as the PropertySet instance for that 2992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // interface if necessary. 3002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void AddInterface(const ObjectPath& object_path, 3012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& interface_name, 3022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) MessageReader* reader); 3032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Removes the properties structure of the object with path |object_path| 3052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // for the interfaces named |interface_name|. 3062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // 3072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // If no further interfaces remain, the entry in the ObjectMap is discarded. 3082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void RemoveInterface(const ObjectPath& object_path, 3092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& interface_name); 3102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 311a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Removes all objects and interfaces from the object manager when 312a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // |old_owner| is not the empty string and/or re-requests the set of managed 313a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // objects when |new_owner| is not the empty string. 314a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) void NameOwnerChanged(const std::string& old_owner, 315a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) const std::string& new_owner); 316a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 3172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) Bus* bus_; 3182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::string service_name_; 3191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci std::string service_name_owner_; 3201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci std::string match_rule_; 3212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ObjectPath object_path_; 3222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ObjectProxy* object_proxy_; 3231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci bool setup_success_; 3241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci bool cleanup_called_; 3252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Maps the name of an interface to the implementation class used for 3272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // instantiating PropertySet structures for that interface's properties. 3282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) typedef std::map<std::string, Interface*> InterfaceMap; 3292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) InterfaceMap interface_map_; 3302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Each managed object consists of a ObjectProxy used to make calls 3322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // against that object and a collection of D-Bus interface names and their 3332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // associated PropertySet structures. 3342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) struct Object { 3352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) Object(); 3362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ~Object(); 3372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ObjectProxy* object_proxy; 3392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Maps the name of an interface to the specific PropertySet structure 3412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // of that interface's properties. 3422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) typedef std::map<const std::string, PropertySet*> PropertiesMap; 3432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) PropertiesMap properties_map; 3442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) }; 3452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Maps the object path of an object to the Object structure. 3472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) typedef std::map<const ObjectPath, Object*> ObjectMap; 3482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ObjectMap object_map_; 3492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Weak pointer factory for generating 'this' pointers that might live longer 3512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // than we do. 3522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Note: This should remain the last member so it'll be destroyed and 3532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // invalidate its weak pointers before any other members are destroyed. 3542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::WeakPtrFactory<ObjectManager> weak_ptr_factory_; 3552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(ObjectManager); 3572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}; 3582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} // namespace dbus 3602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif // DBUS_OBJECT_MANAGER_H_ 362