// Copyright 2014 The Chromium OS Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef LIBCHROMEOS_CHROMEOS_DBUS_EXPORTED_OBJECT_MANAGER_H_ #define LIBCHROMEOS_CHROMEOS_DBUS_EXPORTED_OBJECT_MANAGER_H_ #include #include #include #include #include #include #include #include #include #include #include #include #include namespace chromeos { namespace dbus_utils { // ExportedObjectManager is a delegate that implements the // org.freedesktop.DBus.ObjectManager interface on behalf of another // object. It handles sending signals when new interfaces are added. // // This class is very similar to the ExportedPropertySet class, except that // it allows objects to expose an object manager interface rather than the // properties interface. // // Example usage: // // class ExampleObjectManager { // public: // ExampleObjectManager(dbus::Bus* bus) // : object_manager_(bus, "/my/objects/path") { } // // void RegisterAsync(const CompletionAction& cb) { // object_manager_.RegisterAsync(cb); // } // void ClaimInterface(const dbus::ObjectPath& path, // const std::string& interface_name, // const ExportedPropertySet::PropertyWriter& writer) { // object_manager_->ClaimInterface(...); // } // void ReleaseInterface(const dbus::ObjectPath& path, // const std::string& interface_name) { // object_manager_->ReleaseInterface(...); // } // // private: // ExportedObjectManager object_manager_; // }; // // class MyObjectClaimingAnInterface { // public: // MyObjectClaimingAnInterface(ExampleObjectManager* object_manager) // : object_manager_(object_manager) {} // // void OnInitFinish(bool success) { // if (!success) { /* handle that */ } // object_manager_->ClaimInterface( // my_path_, my_interface_, my_properties_.GetWriter()); // } // // private: // struct Properties : public ExportedPropertySet { // public: // /* Lots of interesting properties. */ // }; // // Properties my_properties_; // ExampleObjectManager* object_manager_; // }; class CHROMEOS_EXPORT ExportedObjectManager : public base::SupportsWeakPtr { public: using ObjectMap = std::map>; using InterfaceProperties = std::map; ExportedObjectManager(scoped_refptr bus, const dbus::ObjectPath& path); virtual ~ExportedObjectManager() = default; // Registers methods implementing the ObjectManager interface on the object // exported on the path given in the constructor. Must be called on the // origin thread. virtual void RegisterAsync( const chromeos::dbus_utils::AsyncEventSequencer::CompletionAction& completion_callback); // Trigger a signal that |path| has added an interface |interface_name| // with properties as given by |writer|. virtual void ClaimInterface( const dbus::ObjectPath& path, const std::string& interface_name, const ExportedPropertySet::PropertyWriter& writer); // Trigger a signal that |path| has removed an interface |interface_name|. virtual void ReleaseInterface(const dbus::ObjectPath& path, const std::string& interface_name); const scoped_refptr& GetBus() const { return bus_; } private: CHROMEOS_PRIVATE ObjectMap HandleGetManagedObjects(); scoped_refptr bus_; chromeos::dbus_utils::DBusObject dbus_object_; // Tracks all objects currently known to the ExportedObjectManager. std::map registered_objects_; using SignalInterfacesAdded = DBusSignal>; using SignalInterfacesRemoved = DBusSignal>; std::weak_ptr signal_itf_added_; std::weak_ptr signal_itf_removed_; friend class ExportedObjectManagerTest; DISALLOW_COPY_AND_ASSIGN(ExportedObjectManager); }; } // namespace dbus_utils } // namespace chromeos #endif // LIBCHROMEOS_CHROMEOS_DBUS_EXPORTED_OBJECT_MANAGER_H_