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