aboutsummaryrefslogtreecommitdiffstats
path: root/brillo/daemons/dbus_daemon.h
blob: c2352c1aba48255fe53477d745ebea1d554b9d10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// 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_BRILLO_DAEMONS_DBUS_DAEMON_H_
#define LIBCHROMEOS_BRILLO_DAEMONS_DBUS_DAEMON_H_

#include <memory>
#include <string>

#include <base/strings/string_piece.h>
#include <base/memory/ref_counted.h>
#include <brillo/brillo_export.h>
#include <brillo/daemons/daemon.h>
#include <brillo/dbus/exported_object_manager.h>
#include <dbus/bus.h>

namespace brillo {

namespace dbus_utils {
class AsyncEventSequencer;
}  // namespace dbus_utils

// DBusDaemon adds D-Bus support to Daemon.
// Derive your daemon from this class if you want D-Bus client services in your
// daemon (consuming other D-Bus objects). Currently uses a SYSTEM bus.
class BRILLO_EXPORT DBusDaemon : public Daemon {
 public:
  DBusDaemon();
  ~DBusDaemon() override;

 protected:
  // Calls the base OnInit() and then instantiates dbus::Bus and establishes
  // a D-Bus connection.
  int OnInit() override;

  scoped_refptr<dbus::Bus> bus_;

 private:
  DISALLOW_COPY_AND_ASSIGN(DBusDaemon);
};

// DBusServiceDaemon adds D-Bus service support to DBusDaemon.
// Derive your daemon from this class if your daemon exposes D-Bus objects.
// Provides an ExportedObjectManager to announce your object/interface creation
// and destruction.
class BRILLO_EXPORT DBusServiceDaemon : public DBusDaemon {
 public:
  // Constructs the daemon.
  // |service_name| is the name of D-Bus service provided by the daemon.
  // |object_manager_path_| is a well-known D-Bus object path for
  // ExportedObjectManager object.
  // If |object_manager_path_| is not specified, then ExportedObjectManager is
  // not created and is not available as part of the D-Bus service.
  explicit DBusServiceDaemon(const std::string& service_name);
  DBusServiceDaemon(const std::string& service_name,
                    const dbus::ObjectPath& object_manager_path);
  DBusServiceDaemon(const std::string& service_name,
                    base::StringPiece object_manager_path);

 protected:
  // OnInit() overload exporting D-Bus objects. Exports the contained
  // ExportedObjectManager object and calls RegisterDBusObjectsAsync() to let
  // you provide additional D-Bus objects.
  int OnInit() override;

  // Overload this method to export your custom D-Bus objects at startup.
  // Objects exported in this way will finish exporting before we claim the
  // daemon's service name on DBus.
  virtual void RegisterDBusObjectsAsync(
      dbus_utils::AsyncEventSequencer* sequencer);

  std::string service_name_;
  dbus::ObjectPath object_manager_path_;
  std::unique_ptr<dbus_utils::ExportedObjectManager> object_manager_;

 private:
  // A callback that will be called when all the D-Bus objects/interfaces are
  // exported successfully and the daemon is ready to claim the D-Bus service
  // ownership.
  void TakeServiceOwnership(bool success);

  DISALLOW_COPY_AND_ASSIGN(DBusServiceDaemon);
};

}  // namespace brillo

#endif  // LIBCHROMEOS_BRILLO_DAEMONS_DBUS_DAEMON_H_