aboutsummaryrefslogtreecommitdiff
path: root/brillo/daemons/dbus_daemon.cc
blob: 6e9068668655a2c2e4f1e501c441721473c19171 (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
89
90
// 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.

#include <brillo/daemons/dbus_daemon.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 {

DBusDaemon::DBusDaemon() {
}

DBusDaemon::~DBusDaemon() {
  if (bus_)
    bus_->ShutdownAndBlock();
}

int DBusDaemon::OnInit() {
  int exit_code = Daemon::OnInit();
  if (exit_code != EX_OK)
    return exit_code;

  dbus::Bus::Options options;
  options.bus_type = dbus::Bus::SYSTEM;

  bus_ = new dbus::Bus(options);
  CHECK(bus_->Connect());

  return exit_code;
}

DBusServiceDaemon::DBusServiceDaemon(const std::string& service_name)
    : service_name_(service_name) {
}

DBusServiceDaemon::DBusServiceDaemon(
    const std::string& service_name,
    const dbus::ObjectPath& object_manager_path)
    : service_name_(service_name), object_manager_path_(object_manager_path) {
}

DBusServiceDaemon::DBusServiceDaemon(const std::string& service_name,
                                     base::StringPiece object_manager_path)
    : DBusServiceDaemon(service_name,
                        dbus::ObjectPath(object_manager_path.as_string())) {
}

int DBusServiceDaemon::OnInit() {
  int exit_code = DBusDaemon::OnInit();
  if (exit_code != EX_OK)
    return exit_code;

  scoped_refptr<AsyncEventSequencer> sequencer(new AsyncEventSequencer());
  if (object_manager_path_.IsValid()) {
    object_manager_.reset(
        new ExportedObjectManager(bus_, object_manager_path_));
    object_manager_->RegisterAsync(
        sequencer->GetHandler("ObjectManager.RegisterAsync() failed.", true));
  }
  RegisterDBusObjectsAsync(sequencer.get());
  sequencer->OnAllTasksCompletedCall({
      base::Bind(&DBusServiceDaemon::TakeServiceOwnership,
                 base::Unretained(this))
  });
  return EX_OK;
}

void DBusServiceDaemon::RegisterDBusObjectsAsync(
    dbus_utils::AsyncEventSequencer* /* sequencer */) {
  // Do nothing here.
  // Overload this method to export custom D-Bus objects at daemon startup.
}

void DBusServiceDaemon::TakeServiceOwnership(bool success) {
  // Success should always be true since we've said that failures are fatal.
  CHECK(success) << "Init of one or more objects has failed.";
  CHECK(bus_->RequestOwnershipAndBlock(service_name_,
                                       dbus::Bus::REQUIRE_PRIMARY))
      << "Unable to take ownership of " << service_name_;
}

}  // namespace brillo