Prashant Malani | 224d3ca | 2017-03-14 12:44:11 -0700 | [diff] [blame] | 1 | // 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 Malani | 6789d66 | 2017-07-28 15:03:40 -0700 | [diff] [blame] | 5 | #include <base/bind.h> |
Prashant Malani | d91f489 | 2017-03-22 14:06:44 -0700 | [diff] [blame] | 6 | #include <base/memory/ptr_util.h> |
Prashant Malani | 6789d66 | 2017-07-28 15:03:40 -0700 | [diff] [blame] | 7 | #include <dbus/bus.h> |
| 8 | #include <dbus/message.h> |
Prashant Malani | d91f489 | 2017-03-22 14:06:44 -0700 | [diff] [blame] | 9 | |
Prashant Malani | 224d3ca | 2017-03-14 12:44:11 -0700 | [diff] [blame] | 10 | #include "midis/daemon.h" |
| 11 | |
Prashant Malani | 6789d66 | 2017-07-28 15:03:40 -0700 | [diff] [blame] | 12 | namespace { |
| 13 | |
| 14 | // TODO(pmalani): Move these into system_api once names are finalized. |
| 15 | constexpr char kMidisServiceName[] = "org.chromium.Midis"; |
| 16 | constexpr char kMidisServicePath[] = "/org/chromium/Midis"; |
| 17 | constexpr char kMidisInterfaceName[] = "org.chromium.Midis"; |
| 18 | constexpr char kBootstrapMojoConnectionMethod[] = "BootstrapMojoConnection"; |
| 19 | |
| 20 | } // namespace |
| 21 | |
Prashant Malani | 224d3ca | 2017-03-14 12:44:11 -0700 | [diff] [blame] | 22 | namespace midis { |
| 23 | |
Prashant Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 24 | Daemon::Daemon() |
| 25 | : device_tracker_(base::MakeUnique<DeviceTracker>()), |
Prashant Malani | 6789d66 | 2017-07-28 15:03:40 -0700 | [diff] [blame] | 26 | client_tracker_(base::MakeUnique<ClientTracker>()), |
| 27 | weak_factory_(this) {} |
Prashant Malani | 224d3ca | 2017-03-14 12:44:11 -0700 | [diff] [blame] | 28 | |
| 29 | Daemon::~Daemon() {} |
| 30 | |
| 31 | int Daemon::OnInit() { |
| 32 | if (!device_tracker_->InitDeviceTracker()) { |
| 33 | return -1; |
| 34 | } |
| 35 | |
Prashant Malani | 3de2987 | 2017-04-28 16:01:56 -0700 | [diff] [blame] | 36 | if (!client_tracker_->InitClientTracker(device_tracker_.get())) { |
Prashant Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 37 | return -1; |
| 38 | } |
| 39 | |
Prashant Malani | 6789d66 | 2017-07-28 15:03:40 -0700 | [diff] [blame] | 40 | InitDBus(); |
Prashant Malani | 224d3ca | 2017-03-14 12:44:11 -0700 | [diff] [blame] | 41 | return 0; |
| 42 | } |
| 43 | |
Prashant Malani | 6789d66 | 2017-07-28 15:03:40 -0700 | [diff] [blame] | 44 | void 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 | |
| 63 | void 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 Malani | 224d3ca | 2017-03-14 12:44:11 -0700 | [diff] [blame] | 71 | } // namespace midis |