blob: b7c736957cfcf85580a5faa8a9b5aa12a4318641 [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 Malani6789d662017-07-28 15:03:40 -07005#include <base/bind.h>
Prashant Malanid91f4892017-03-22 14:06:44 -07006#include <base/memory/ptr_util.h>
Prashant Malani6789d662017-07-28 15:03:40 -07007#include <dbus/bus.h>
8#include <dbus/message.h>
Prashant Malanid91f4892017-03-22 14:06:44 -07009
Prashant Malani224d3ca2017-03-14 12:44:11 -070010#include "midis/daemon.h"
11
Prashant Malani6789d662017-07-28 15:03:40 -070012namespace {
13
14// TODO(pmalani): Move these into system_api once names are finalized.
15constexpr char kMidisServiceName[] = "org.chromium.Midis";
16constexpr char kMidisServicePath[] = "/org/chromium/Midis";
17constexpr char kMidisInterfaceName[] = "org.chromium.Midis";
18constexpr char kBootstrapMojoConnectionMethod[] = "BootstrapMojoConnection";
19
20} // namespace
21
Prashant Malani224d3ca2017-03-14 12:44:11 -070022namespace midis {
23
Prashant Malanif07c9522017-04-11 14:54:29 -070024Daemon::Daemon()
25 : device_tracker_(base::MakeUnique<DeviceTracker>()),
Prashant Malani6789d662017-07-28 15:03:40 -070026 client_tracker_(base::MakeUnique<ClientTracker>()),
27 weak_factory_(this) {}
Prashant Malani224d3ca2017-03-14 12:44:11 -070028
29Daemon::~Daemon() {}
30
31int Daemon::OnInit() {
32 if (!device_tracker_->InitDeviceTracker()) {
33 return -1;
34 }
35
Prashant Malani3de29872017-04-28 16:01:56 -070036 if (!client_tracker_->InitClientTracker(device_tracker_.get())) {
Prashant Malanif07c9522017-04-11 14:54:29 -070037 return -1;
38 }
39
Prashant Malani6789d662017-07-28 15:03:40 -070040 InitDBus();
Prashant Malani224d3ca2017-03-14 12:44:11 -070041 return 0;
42}
43
Prashant Malani6789d662017-07-28 15:03:40 -070044void Daemon::InitDBus() {
45 dbus::Bus::Options options;
46 options.bus_type = dbus::Bus::SYSTEM;
47 scoped_refptr<dbus::Bus> bus(new dbus::Bus(options));
48 CHECK(bus->Connect());
49 dbus::ExportedObject* exported_object =
50 bus->GetExportedObject(dbus::ObjectPath(kMidisServicePath));
51
52 CHECK(exported_object);
53 CHECK(exported_object->ExportMethodAndBlock(
54 kMidisInterfaceName,
55 kBootstrapMojoConnectionMethod,
56 base::Bind(&Daemon::BootstrapMojoConnection,
57 weak_factory_.GetWeakPtr())));
58 CHECK(bus->RequestOwnershipAndBlock(kMidisServiceName,
59 dbus::Bus::REQUIRE_PRIMARY));
60 LOG(INFO) << "D-Bus Registration succeeded";
61}
62
63void Daemon::BootstrapMojoConnection(
64 dbus::MethodCall* method_call,
65 dbus::ExportedObject::ResponseSender response_sender) {
66 LOG(INFO) << "Successfully received call from D-Bus client";
67 // TODO(pmalani): Actually bootstrap mojo interface in ClientTracker
68 // handle.value() contains the good stuff.
69}
70
Prashant Malani224d3ca2017-03-14 12:44:11 -070071} // namespace midis