aboutsummaryrefslogtreecommitdiffstats
path: root/brillo/dbus
diff options
context:
space:
mode:
authorAlex Deymo <deymo@google.com>2016-01-20 17:04:51 -0800
committerAlex Deymo <deymo@google.com>2016-01-21 14:58:16 -0800
commit2a25190ea741023a3753e7f381a8a2b1465c43c1 (patch)
tree03b3f32aead9095c2b8e07997a947c57d72d90b2 /brillo/dbus
parent037abdbfc6e180b0db2624c958000ea9388516c0 (diff)
downloadplatform_external_libbrillo-2a25190ea741023a3753e7f381a8a2b1465c43c1.tar.gz
platform_external_libbrillo-2a25190ea741023a3753e7f381a8a2b1465c43c1.tar.bz2
platform_external_libbrillo-2a25190ea741023a3753e7f381a8a2b1465c43c1.zip
Add DBusConnection helper class to use instead of DBusDaemon.
This CL moves the logic in DBusDaemon to a helper class that can be included in another class inheriting from Daemon without needed to use multiple inheritance nor forcing a DBus connection to succeed. DBusDaemon was modified to re-use this class without changing its behaviour to be compatible with existing code. Bug: 26690086 TEST=mm; Update update_engine to use this class and tested it on edison. Change-Id: If05a5582158552b6aeebf86dbde2aaa161f5a29f
Diffstat (limited to 'brillo/dbus')
-rw-r--r--brillo/dbus/dbus_connection.cc58
-rw-r--r--brillo/dbus/dbus_connection.h40
2 files changed, 98 insertions, 0 deletions
diff --git a/brillo/dbus/dbus_connection.cc b/brillo/dbus/dbus_connection.cc
new file mode 100644
index 0000000..b60cf44
--- /dev/null
+++ b/brillo/dbus/dbus_connection.cc
@@ -0,0 +1,58 @@
+// Copyright 2016 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.
+
+#include <brillo/dbus/dbus_connection.h>
+
+#include <sysexits.h>
+
+#include <base/bind.h>
+#include <brillo/dbus/async_event_sequencer.h>
+#include <brillo/dbus/exported_object_manager.h>
+
+using brillo::dbus_utils::AsyncEventSequencer;
+using brillo::dbus_utils::ExportedObjectManager;
+
+namespace brillo {
+
+DBusConnection::DBusConnection() {
+}
+
+DBusConnection::~DBusConnection() {
+ if (bus_)
+ bus_->ShutdownAndBlock();
+}
+
+scoped_refptr<dbus::Bus> DBusConnection::Connect() {
+ return ConnectWithTimeout(base::TimeDelta());
+}
+
+scoped_refptr<dbus::Bus> DBusConnection::ConnectWithTimeout(
+ base::TimeDelta timeout) {
+ if (bus_)
+ return bus_;
+
+ base::TimeTicks deadline = base::TimeTicks::Now() + timeout;
+
+ dbus::Bus::Options options;
+ options.bus_type = dbus::Bus::SYSTEM;
+
+ scoped_refptr<dbus::Bus> bus = new dbus::Bus(options);
+
+ do {
+ if (bus->Connect()) {
+ bus_ = bus;
+ return bus_;
+ }
+ LOG(WARNING) << "Failed to get system bus.";
+ // Wait 1 second to prevent trashing the device while waiting for the
+ // dbus-daemon to start.
+ sleep(1);
+ } while (base::TimeTicks::Now() < deadline);
+
+ LOG(ERROR) << "Failed to get system bus after " << timeout.InSeconds()
+ << " seconds.";
+ return nullptr;
+}
+
+} // namespace brillo
diff --git a/brillo/dbus/dbus_connection.h b/brillo/dbus/dbus_connection.h
new file mode 100644
index 0000000..aecf434
--- /dev/null
+++ b/brillo/dbus/dbus_connection.h
@@ -0,0 +1,40 @@
+// Copyright 2016 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 LIBBRILLO_BRILLO_DBUS_DBUS_CONNECTION_H_
+#define LIBBRILLO_BRILLO_DBUS_DBUS_CONNECTION_H_
+
+#include <base/memory/ref_counted.h>
+#include <base/time/time.h>
+#include <dbus/bus.h>
+
+#include <brillo/brillo_export.h>
+
+namespace brillo {
+
+// DBusConnection adds D-Bus support to Daemon.
+class BRILLO_EXPORT DBusConnection final {
+ public:
+ DBusConnection();
+ ~DBusConnection();
+
+ // Instantiates dbus::Bus and establishes a D-Bus connection. Returns a
+ // reference to the connected bus, or an empty pointer in case of error.
+ scoped_refptr<dbus::Bus> Connect();
+
+ // Instantiates dbus::Bus and tries to establish a D-Bus connection for up to
+ // |timeout|. If the connection can't be established after the timeout, fails
+ // returning an empty pointer.
+ scoped_refptr<dbus::Bus> ConnectWithTimeout(base::TimeDelta timeout);
+
+ private:
+ scoped_refptr<dbus::Bus> bus_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(DBusConnection);
+};
+
+} // namespace brillo
+
+#endif // LIBBRILLO_BRILLO_DAEMONS_DBUS_DAEMON_H_