• 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_DBUS_DBUS_SIGNAL_H_
6 #define LIBBRILLO_BRILLO_DBUS_DBUS_SIGNAL_H_
7 
8 #include <string>
9 #include <typeinfo>
10 
11 #include <base/bind.h>
12 #include <base/macros.h>
13 #include <brillo/brillo_export.h>
14 #include <brillo/dbus/dbus_param_writer.h>
15 #include <dbus/message.h>
16 
17 namespace brillo {
18 namespace dbus_utils {
19 
20 class DBusObject;
21 
22 // Base class for D-Bus signal proxy classes.
23 // Used mostly to store the polymorphic DBusSignal<...> in a single map
24 // container inside DBusInterface object.
25 class BRILLO_EXPORT DBusSignalBase {
26  public:
27   DBusSignalBase(DBusObject* dbus_object,
28                  const std::string& interface_name,
29                  const std::string& signal_name);
30   virtual ~DBusSignalBase() = default;
31 
32  protected:
33   bool SendSignal(::dbus::Signal* signal) const;
34 
35   std::string interface_name_;
36   std::string signal_name_;
37 
38  private:
39   DBusObject* dbus_object_;
40 
41   DISALLOW_COPY_AND_ASSIGN(DBusSignalBase);
42 };
43 
44 // DBusSignal<...> is a concrete signal proxy class that knows about the
45 // exact number of signal arguments and their types.
46 template<typename... Args>
47 class DBusSignal : public DBusSignalBase {
48  public:
49   // Expose the custom constructor from DBusSignalBase.
50   using DBusSignalBase::DBusSignalBase;
51   ~DBusSignal() override = default;
52 
53   // DBusSignal<...>::Send(...) dispatches the signal with the given arguments.
54   // Note: This function can be called from any thread/task runner, as it'll
55   // eventually post the actual signal sending to the DBus thread.
Send(const Args &...args)56   bool Send(const Args&... args) const {
57     ::dbus::Signal signal(interface_name_, signal_name_);
58     ::dbus::MessageWriter signal_writer(&signal);
59     DBusParamWriter::Append(&signal_writer, args...);
60     return SendSignal(&signal);
61   }
62 
63  private:
64   DISALLOW_COPY_AND_ASSIGN(DBusSignal);
65 };
66 
67 }  // namespace dbus_utils
68 }  // namespace brillo
69 
70 #endif  // LIBBRILLO_BRILLO_DBUS_DBUS_SIGNAL_H_
71