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 | 0655855 | 2017-08-21 13:45:38 -0700 | [diff] [blame] | 5 | #include "midis/daemon.h" |
| 6 | |
Prashant Malani | 112cc7a | 2017-07-28 18:48:54 -0700 | [diff] [blame^] | 7 | #include <fcntl.h> |
| 8 | |
| 9 | #include <utility> |
| 10 | |
Prashant Malani | 6789d66 | 2017-07-28 15:03:40 -0700 | [diff] [blame] | 11 | #include <base/bind.h> |
Prashant Malani | 112cc7a | 2017-07-28 18:48:54 -0700 | [diff] [blame^] | 12 | #include <base/files/file_util.h> |
Prashant Malani | d91f489 | 2017-03-22 14:06:44 -0700 | [diff] [blame] | 13 | #include <base/memory/ptr_util.h> |
Prashant Malani | 0655855 | 2017-08-21 13:45:38 -0700 | [diff] [blame] | 14 | #include <chromeos/dbus/service_constants.h> |
Prashant Malani | 6789d66 | 2017-07-28 15:03:40 -0700 | [diff] [blame] | 15 | #include <dbus/bus.h> |
| 16 | #include <dbus/message.h> |
Prashant Malani | d91f489 | 2017-03-22 14:06:44 -0700 | [diff] [blame] | 17 | |
Prashant Malani | 112cc7a | 2017-07-28 18:48:54 -0700 | [diff] [blame^] | 18 | #include "midis/client_tracker.h" |
| 19 | #include "midis/device_tracker.h" |
Prashant Malani | 6789d66 | 2017-07-28 15:03:40 -0700 | [diff] [blame] | 20 | |
Prashant Malani | 224d3ca | 2017-03-14 12:44:11 -0700 | [diff] [blame] | 21 | namespace midis { |
| 22 | |
Prashant Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 23 | Daemon::Daemon() |
| 24 | : device_tracker_(base::MakeUnique<DeviceTracker>()), |
Prashant Malani | 6789d66 | 2017-07-28 15:03:40 -0700 | [diff] [blame] | 25 | client_tracker_(base::MakeUnique<ClientTracker>()), |
| 26 | weak_factory_(this) {} |
Prashant Malani | 224d3ca | 2017-03-14 12:44:11 -0700 | [diff] [blame] | 27 | |
| 28 | Daemon::~Daemon() {} |
| 29 | |
| 30 | int Daemon::OnInit() { |
| 31 | if (!device_tracker_->InitDeviceTracker()) { |
| 32 | return -1; |
| 33 | } |
| 34 | |
Prashant Malani | 3de2987 | 2017-04-28 16:01:56 -0700 | [diff] [blame] | 35 | if (!client_tracker_->InitClientTracker(device_tracker_.get())) { |
Prashant Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 36 | return -1; |
| 37 | } |
| 38 | |
Prashant Malani | 6789d66 | 2017-07-28 15:03:40 -0700 | [diff] [blame] | 39 | InitDBus(); |
Prashant Malani | 224d3ca | 2017-03-14 12:44:11 -0700 | [diff] [blame] | 40 | return 0; |
| 41 | } |
| 42 | |
Prashant Malani | 6789d66 | 2017-07-28 15:03:40 -0700 | [diff] [blame] | 43 | void Daemon::InitDBus() { |
| 44 | dbus::Bus::Options options; |
| 45 | options.bus_type = dbus::Bus::SYSTEM; |
| 46 | scoped_refptr<dbus::Bus> bus(new dbus::Bus(options)); |
| 47 | CHECK(bus->Connect()); |
| 48 | dbus::ExportedObject* exported_object = |
| 49 | bus->GetExportedObject(dbus::ObjectPath(kMidisServicePath)); |
| 50 | |
| 51 | CHECK(exported_object); |
| 52 | CHECK(exported_object->ExportMethodAndBlock( |
| 53 | kMidisInterfaceName, |
| 54 | kBootstrapMojoConnectionMethod, |
| 55 | base::Bind(&Daemon::BootstrapMojoConnection, |
| 56 | weak_factory_.GetWeakPtr()))); |
| 57 | CHECK(bus->RequestOwnershipAndBlock(kMidisServiceName, |
| 58 | dbus::Bus::REQUIRE_PRIMARY)); |
Prashant Malani | 112cc7a | 2017-07-28 18:48:54 -0700 | [diff] [blame^] | 59 | VLOG(1) << "D-Bus Registration succeeded"; |
Prashant Malani | 6789d66 | 2017-07-28 15:03:40 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void Daemon::BootstrapMojoConnection( |
| 63 | dbus::MethodCall* method_call, |
| 64 | dbus::ExportedObject::ResponseSender response_sender) { |
Prashant Malani | 112cc7a | 2017-07-28 18:48:54 -0700 | [diff] [blame^] | 65 | LOG(INFO) << "Successfully received call from D-Bus client."; |
| 66 | if (client_tracker_->IsProxyConnected()) { |
| 67 | LOG(WARNING) << "Midis can only instantiate one Mojo Proxy instance."; |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | dbus::FileDescriptor file_handle; |
| 72 | dbus::MessageReader reader(method_call); |
| 73 | |
| 74 | if (!reader.PopFileDescriptor(&file_handle)) { |
| 75 | LOG(ERROR) << "Couldn't extract Mojo IPC handle."; |
| 76 | return; |
| 77 | } |
| 78 | file_handle.CheckValidity(); |
| 79 | base::ScopedFD fd(file_handle.TakeValue()); |
| 80 | |
| 81 | if (!fd.is_valid()) { |
| 82 | LOG(ERROR) << "Couldn't copy file handle sent over D-Bus."; |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | if (!base::SetCloseOnExec(fd.get())) { |
| 87 | PLOG(ERROR) << "Failed setting FD_CLOEXEC on fd."; |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | client_tracker_->AcceptProxyConnection(std::move(fd)); |
| 92 | LOG(INFO) << "MojoBridger connection established."; |
Prashant Malani | 6789d66 | 2017-07-28 15:03:40 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Prashant Malani | 224d3ca | 2017-03-14 12:44:11 -0700 | [diff] [blame] | 95 | } // namespace midis |