1*1a96fba6SXin Li // Copyright 2014 The Chromium OS Authors. All rights reserved. 2*1a96fba6SXin Li // Use of this source code is governed by a BSD-style license that can be 3*1a96fba6SXin Li // found in the LICENSE file. 4*1a96fba6SXin Li 5*1a96fba6SXin Li #ifndef LIBBRILLO_BRILLO_DAEMONS_DBUS_DAEMON_H_ 6*1a96fba6SXin Li #define LIBBRILLO_BRILLO_DAEMONS_DBUS_DAEMON_H_ 7*1a96fba6SXin Li 8*1a96fba6SXin Li #include <memory> 9*1a96fba6SXin Li #include <string> 10*1a96fba6SXin Li 11*1a96fba6SXin Li #include <base/strings/string_piece.h> 12*1a96fba6SXin Li #include <base/memory/ref_counted.h> 13*1a96fba6SXin Li #include <brillo/brillo_export.h> 14*1a96fba6SXin Li #include <brillo/daemons/daemon.h> 15*1a96fba6SXin Li #include <brillo/dbus/dbus_connection.h> 16*1a96fba6SXin Li #include <brillo/dbus/exported_object_manager.h> 17*1a96fba6SXin Li #include <dbus/bus.h> 18*1a96fba6SXin Li 19*1a96fba6SXin Li namespace brillo { 20*1a96fba6SXin Li 21*1a96fba6SXin Li namespace dbus_utils { 22*1a96fba6SXin Li class AsyncEventSequencer; 23*1a96fba6SXin Li } // namespace dbus_utils 24*1a96fba6SXin Li 25*1a96fba6SXin Li // DBusDaemon adds D-Bus support to Daemon. 26*1a96fba6SXin Li // Derive your daemon from this class if you want D-Bus client services in your 27*1a96fba6SXin Li // daemon (consuming other D-Bus objects). Currently uses a SYSTEM bus. 28*1a96fba6SXin Li class BRILLO_EXPORT DBusDaemon : public Daemon { 29*1a96fba6SXin Li public: 30*1a96fba6SXin Li DBusDaemon(); 31*1a96fba6SXin Li ~DBusDaemon() override = default; 32*1a96fba6SXin Li 33*1a96fba6SXin Li protected: 34*1a96fba6SXin Li // Calls the base OnInit() and then instantiates dbus::Bus and establishes 35*1a96fba6SXin Li // a D-Bus connection. 36*1a96fba6SXin Li int OnInit() override; 37*1a96fba6SXin Li 38*1a96fba6SXin Li // A reference to the |dbus_connection_| bus object often used by derived 39*1a96fba6SXin Li // classes. 40*1a96fba6SXin Li scoped_refptr<::dbus::Bus> bus_; 41*1a96fba6SXin Li 42*1a96fba6SXin Li private: 43*1a96fba6SXin Li DBusConnection dbus_connection_; 44*1a96fba6SXin Li 45*1a96fba6SXin Li DISALLOW_COPY_AND_ASSIGN(DBusDaemon); 46*1a96fba6SXin Li }; 47*1a96fba6SXin Li 48*1a96fba6SXin Li // DBusServiceDaemon adds D-Bus service support to DBusDaemon. 49*1a96fba6SXin Li // Derive your daemon from this class if your daemon exposes D-Bus objects. 50*1a96fba6SXin Li // Provides an ExportedObjectManager to announce your object/interface creation 51*1a96fba6SXin Li // and destruction. 52*1a96fba6SXin Li class BRILLO_EXPORT DBusServiceDaemon : public DBusDaemon { 53*1a96fba6SXin Li public: 54*1a96fba6SXin Li // Constructs the daemon. 55*1a96fba6SXin Li // |service_name| is the name of D-Bus service provided by the daemon. 56*1a96fba6SXin Li // |object_manager_path_| is a well-known D-Bus object path for 57*1a96fba6SXin Li // ExportedObjectManager object. 58*1a96fba6SXin Li // If |object_manager_path_| is not specified, then ExportedObjectManager is 59*1a96fba6SXin Li // not created and is not available as part of the D-Bus service. 60*1a96fba6SXin Li explicit DBusServiceDaemon(const std::string& service_name); 61*1a96fba6SXin Li DBusServiceDaemon(const std::string& service_name, 62*1a96fba6SXin Li const ::dbus::ObjectPath& object_manager_path); 63*1a96fba6SXin Li DBusServiceDaemon(const std::string& service_name, 64*1a96fba6SXin Li base::StringPiece object_manager_path); 65*1a96fba6SXin Li 66*1a96fba6SXin Li protected: 67*1a96fba6SXin Li // OnInit() overload exporting D-Bus objects. Exports the contained 68*1a96fba6SXin Li // ExportedObjectManager object and calls RegisterDBusObjectsAsync() to let 69*1a96fba6SXin Li // you provide additional D-Bus objects. 70*1a96fba6SXin Li int OnInit() override; 71*1a96fba6SXin Li 72*1a96fba6SXin Li // Overload this method to export your custom D-Bus objects at startup. 73*1a96fba6SXin Li // Objects exported in this way will finish exporting before we claim the 74*1a96fba6SXin Li // daemon's service name on DBus. 75*1a96fba6SXin Li virtual void RegisterDBusObjectsAsync( 76*1a96fba6SXin Li dbus_utils::AsyncEventSequencer* sequencer); 77*1a96fba6SXin Li 78*1a96fba6SXin Li std::string service_name_; 79*1a96fba6SXin Li ::dbus::ObjectPath object_manager_path_; 80*1a96fba6SXin Li std::unique_ptr<dbus_utils::ExportedObjectManager> object_manager_; 81*1a96fba6SXin Li 82*1a96fba6SXin Li private: 83*1a96fba6SXin Li // A callback that will be called when all the D-Bus objects/interfaces are 84*1a96fba6SXin Li // exported successfully and the daemon is ready to claim the D-Bus service 85*1a96fba6SXin Li // ownership. 86*1a96fba6SXin Li void TakeServiceOwnership(bool success); 87*1a96fba6SXin Li 88*1a96fba6SXin Li DISALLOW_COPY_AND_ASSIGN(DBusServiceDaemon); 89*1a96fba6SXin Li }; 90*1a96fba6SXin Li 91*1a96fba6SXin Li } // namespace brillo 92*1a96fba6SXin Li 93*1a96fba6SXin Li #endif // LIBBRILLO_BRILLO_DAEMONS_DBUS_DAEMON_H_ 94