• 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 #include <brillo/daemons/dbus_daemon.h>
6 
7 #include <sysexits.h>
8 
9 #include <base/bind.h>
10 #include <brillo/dbus/async_event_sequencer.h>
11 #include <brillo/dbus/exported_object_manager.h>
12 
13 using brillo::dbus_utils::AsyncEventSequencer;
14 using brillo::dbus_utils::ExportedObjectManager;
15 
16 namespace brillo {
17 
DBusDaemon()18 DBusDaemon::DBusDaemon() {
19 }
20 
OnInit()21 int DBusDaemon::OnInit() {
22   int exit_code = Daemon::OnInit();
23   if (exit_code != EX_OK)
24     return exit_code;
25 
26   bus_ = dbus_connection_.Connect();
27   CHECK(bus_);
28 
29   return exit_code;
30 }
31 
DBusServiceDaemon(const std::string & service_name)32 DBusServiceDaemon::DBusServiceDaemon(const std::string& service_name)
33     : service_name_(service_name) {
34 }
35 
DBusServiceDaemon(const std::string & service_name,const dbus::ObjectPath & object_manager_path)36 DBusServiceDaemon::DBusServiceDaemon(
37     const std::string& service_name,
38     const dbus::ObjectPath& object_manager_path)
39     : service_name_(service_name), object_manager_path_(object_manager_path) {
40 }
41 
DBusServiceDaemon(const std::string & service_name,base::StringPiece object_manager_path)42 DBusServiceDaemon::DBusServiceDaemon(const std::string& service_name,
43                                      base::StringPiece object_manager_path)
44     : DBusServiceDaemon(service_name,
45                         dbus::ObjectPath(object_manager_path.as_string())) {
46 }
47 
OnInit()48 int DBusServiceDaemon::OnInit() {
49   int exit_code = DBusDaemon::OnInit();
50   if (exit_code != EX_OK)
51     return exit_code;
52 
53   scoped_refptr<AsyncEventSequencer> sequencer(new AsyncEventSequencer());
54   if (object_manager_path_.IsValid()) {
55     object_manager_.reset(
56         new ExportedObjectManager(bus_, object_manager_path_));
57     object_manager_->RegisterAsync(
58         sequencer->GetHandler("ObjectManager.RegisterAsync() failed.", true));
59   }
60   RegisterDBusObjectsAsync(sequencer.get());
61   sequencer->OnAllTasksCompletedCall({
62       base::Bind(&DBusServiceDaemon::TakeServiceOwnership,
63                  base::Unretained(this))
64   });
65   return EX_OK;
66 }
67 
RegisterDBusObjectsAsync(dbus_utils::AsyncEventSequencer *)68 void DBusServiceDaemon::RegisterDBusObjectsAsync(
69     dbus_utils::AsyncEventSequencer* /* sequencer */) {
70   // Do nothing here.
71   // Overload this method to export custom D-Bus objects at daemon startup.
72 }
73 
TakeServiceOwnership(bool success)74 void DBusServiceDaemon::TakeServiceOwnership(bool success) {
75   // Success should always be true since we've said that failures are fatal.
76   CHECK(success) << "Init of one or more objects has failed.";
77   CHECK(bus_->RequestOwnershipAndBlock(service_name_,
78                                        dbus::Bus::REQUIRE_PRIMARY))
79       << "Unable to take ownership of " << service_name_;
80 }
81 
82 }  // namespace brillo
83