• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 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 #include <brillo/dbus/dbus_connection.h>
6 
7 namespace brillo {
8 
DBusConnection()9 DBusConnection::DBusConnection() {
10 }
11 
~DBusConnection()12 DBusConnection::~DBusConnection() {
13   if (bus_)
14     bus_->ShutdownAndBlock();
15 }
16 
Connect()17 scoped_refptr<dbus::Bus> DBusConnection::Connect() {
18   return ConnectWithTimeout(base::TimeDelta());
19 }
20 
ConnectWithTimeout(base::TimeDelta timeout)21 scoped_refptr<dbus::Bus> DBusConnection::ConnectWithTimeout(
22     base::TimeDelta timeout) {
23   if (bus_)
24     return bus_;
25 
26   base::TimeTicks deadline = base::TimeTicks::Now() + timeout;
27 
28   dbus::Bus::Options options;
29   options.bus_type = dbus::Bus::SYSTEM;
30 
31   scoped_refptr<dbus::Bus> bus = new dbus::Bus(options);
32 
33   do {
34     if (bus->Connect()) {
35       bus_ = bus;
36       return bus_;
37     }
38     LOG(WARNING) << "Failed to get system bus.";
39     // Wait 1 second to prevent trashing the device while waiting for the
40     // dbus-daemon to start.
41     sleep(1);
42   } while (base::TimeTicks::Now() < deadline);
43 
44   LOG(ERROR) << "Failed to get system bus after " << timeout.InSeconds()
45              << " seconds.";
46   return nullptr;
47 }
48 
49 }  // namespace brillo
50