-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdbus_wrapper.h
88 lines (72 loc) · 2.69 KB
/
dbus_wrapper.h
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 2017 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.
//
// This file implements DBus functionality.
// Because hammerd is not a daemon, it can only send signals to other
// processes or call methods provided by others.
#ifndef HAMMERD_DBUS_WRAPPER_H_
#define HAMMERD_DBUS_WRAPPER_H_
#include <memory>
#include <string>
#include <base/macros.h>
#include <base/memory/ptr_util.h>
#include <base/memory/ref_counted.h>
#include <dbus/bus.h>
#include <dbus/exported_object.h>
#include <dbus/message.h>
namespace hammerd {
class DBusWrapperInterface {
public:
virtual ~DBusWrapperInterface() = default;
// Send a signal without argument.
virtual void SendSignal(const std::string& signal_name) = 0;
// Send a signal with a binary-blob argument.
// Currently we only have one signal with a binary-blob argument. If we need
// other kind of arguments in the future, then switch to protobuf.
virtual void SendSignalWithArg(const std::string& signal_name,
const uint8_t* values,
size_t length) = 0;
};
class DBusWrapper : public DBusWrapperInterface {
public:
DBusWrapper();
DBusWrapper(const DBusWrapper&) = delete;
DBusWrapper& operator=(const DBusWrapper&) = delete;
virtual ~DBusWrapper() = default;
void SendSignal(const std::string& signal_name) override;
void SendSignalWithArg(const std::string& signal_name,
const uint8_t* values,
size_t length) override;
protected:
scoped_refptr<dbus::Bus> bus_;
dbus::ExportedObject* exported_object_;
};
// The dummy class used in hammerd API.
class DummyDBusWrapper : public DBusWrapperInterface {
public:
DummyDBusWrapper() {}
DummyDBusWrapper(const DummyDBusWrapper&) = delete;
DummyDBusWrapper& operator=(const DummyDBusWrapper&) = delete;
virtual ~DummyDBusWrapper() = default;
void SendSignal(const std::string& signal_name) override {
SendSignalWithArg(signal_name, NULL, 0);
}
void SendSignalWithArg(const std::string& signal_name,
const uint8_t* values,
size_t length) override {
last_signal_name_ = std::string(signal_name);
if (values == NULL)
last_value_ = "";
else
last_value_ = std::string(reinterpret_cast<const char*>(values), length);
}
std::string GetLastSignalName() { return last_signal_name_; }
std::string GetLastValue() { return last_value_; }
private:
// Record the last signal that was "sent"
std::string last_signal_name_;
std::string last_value_;
};
} // namespace hammerd
#endif // HAMMERD_DBUS_WRAPPER_H_