blob: 679b8316d822686180ae6fe24f69a376972da095 [file] [log] [blame]
Prashant Malani224d3ca2017-03-14 12:44:11 -07001// Copyright 2017 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
Prashant Malani06558552017-08-21 13:45:38 -07005#include "midis/daemon.h"
6
Prashant Malani6789d662017-07-28 15:03:40 -07007#include <base/bind.h>
Prashant Malanid91f4892017-03-22 14:06:44 -07008#include <base/memory/ptr_util.h>
Prashant Malani06558552017-08-21 13:45:38 -07009#include <chromeos/dbus/service_constants.h>
Prashant Malani6789d662017-07-28 15:03:40 -070010#include <dbus/bus.h>
11#include <dbus/message.h>
Prashant Malanid91f4892017-03-22 14:06:44 -070012
Prashant Malani6789d662017-07-28 15:03:40 -070013
Prashant Malani224d3ca2017-03-14 12:44:11 -070014namespace midis {
15
Prashant Malanif07c9522017-04-11 14:54:29 -070016Daemon::Daemon()
17 : device_tracker_(base::MakeUnique<DeviceTracker>()),
Prashant Malani6789d662017-07-28 15:03:40 -070018 client_tracker_(base::MakeUnique<ClientTracker>()),
19 weak_factory_(this) {}
Prashant Malani224d3ca2017-03-14 12:44:11 -070020
21Daemon::~Daemon() {}
22
23int Daemon::OnInit() {
24 if (!device_tracker_->InitDeviceTracker()) {
25 return -1;
26 }
27
Prashant Malani3de29872017-04-28 16:01:56 -070028 if (!client_tracker_->InitClientTracker(device_tracker_.get())) {
Prashant Malanif07c9522017-04-11 14:54:29 -070029 return -1;
30 }
31
Prashant Malani6789d662017-07-28 15:03:40 -070032 InitDBus();
Prashant Malani224d3ca2017-03-14 12:44:11 -070033 return 0;
34}
35
Prashant Malani6789d662017-07-28 15:03:40 -070036void Daemon::InitDBus() {
37 dbus::Bus::Options options;
38 options.bus_type = dbus::Bus::SYSTEM;
39 scoped_refptr<dbus::Bus> bus(new dbus::Bus(options));
40 CHECK(bus->Connect());
41 dbus::ExportedObject* exported_object =
42 bus->GetExportedObject(dbus::ObjectPath(kMidisServicePath));
43
44 CHECK(exported_object);
45 CHECK(exported_object->ExportMethodAndBlock(
46 kMidisInterfaceName,
47 kBootstrapMojoConnectionMethod,
48 base::Bind(&Daemon::BootstrapMojoConnection,
49 weak_factory_.GetWeakPtr())));
50 CHECK(bus->RequestOwnershipAndBlock(kMidisServiceName,
51 dbus::Bus::REQUIRE_PRIMARY));
52 LOG(INFO) << "D-Bus Registration succeeded";
53}
54
55void Daemon::BootstrapMojoConnection(
56 dbus::MethodCall* method_call,
57 dbus::ExportedObject::ResponseSender response_sender) {
58 LOG(INFO) << "Successfully received call from D-Bus client";
59 // TODO(pmalani): Actually bootstrap mojo interface in ClientTracker
60 // handle.value() contains the good stuff.
61}
62
Prashant Malani224d3ca2017-03-14 12:44:11 -070063} // namespace midis