blob: 5e008390acdcf6d2530b835e27aa314c5779f9ee [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 Malani112cc7a2017-07-28 18:48:54 -07007#include <fcntl.h>
8
Ben Chand496e612017-09-29 00:20:50 -07009#include <memory>
Prashant Malani112cc7a2017-07-28 18:48:54 -070010#include <utility>
11
Prashant Malani6789d662017-07-28 15:03:40 -070012#include <base/bind.h>
Prashant Malani112cc7a2017-07-28 18:48:54 -070013#include <base/files/file_util.h>
Prashant Malani06558552017-08-21 13:45:38 -070014#include <chromeos/dbus/service_constants.h>
Prashant Malani6789d662017-07-28 15:03:40 -070015#include <dbus/bus.h>
16#include <dbus/message.h>
Prashant Malanid91f4892017-03-22 14:06:44 -070017
Prashant Malani112cc7a2017-07-28 18:48:54 -070018#include "midis/client_tracker.h"
19#include "midis/device_tracker.h"
Prashant Malani6789d662017-07-28 15:03:40 -070020
Prashant Malani224d3ca2017-03-14 12:44:11 -070021namespace midis {
22
Prashant Malanif07c9522017-04-11 14:54:29 -070023Daemon::Daemon()
Ben Chand496e612017-09-29 00:20:50 -070024 : device_tracker_(std::make_unique<DeviceTracker>()),
25 client_tracker_(std::make_unique<ClientTracker>()),
Prashant Malani6789d662017-07-28 15:03:40 -070026 weak_factory_(this) {}
Prashant Malani224d3ca2017-03-14 12:44:11 -070027
28Daemon::~Daemon() {}
29
30int Daemon::OnInit() {
31 if (!device_tracker_->InitDeviceTracker()) {
32 return -1;
33 }
34
Prashant Malani9f15b9e2017-09-19 18:15:39 -070035 // It's OK to set |device_tracker_| here because the D-Bus connection
36 // has not yet been initialized, so it is impossible for clients to be
37 // registered.
38 client_tracker_->SetDeviceTracker(device_tracker_.get());
39 client_tracker_->InitClientTracker();
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(
Tom Hughesaebcdce2020-08-27 15:18:39 -070054 kMidisInterfaceName, kBootstrapMojoConnectionMethod,
Prashant Malani6789d662017-07-28 15:03:40 -070055 base::Bind(&Daemon::BootstrapMojoConnection,
56 weak_factory_.GetWeakPtr())));
57 CHECK(bus->RequestOwnershipAndBlock(kMidisServiceName,
58 dbus::Bus::REQUIRE_PRIMARY));
Prashant Malani112cc7a2017-07-28 18:48:54 -070059 VLOG(1) << "D-Bus Registration succeeded";
Prashant Malani6789d662017-07-28 15:03:40 -070060}
61
62void Daemon::BootstrapMojoConnection(
63 dbus::MethodCall* method_call,
64 dbus::ExportedObject::ResponseSender response_sender) {
Prashant Malani112cc7a2017-07-28 18:48:54 -070065 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
Eric Caruso92f668e2018-03-30 15:12:41 -070071 base::ScopedFD file_handle;
Prashant Malani112cc7a2017-07-28 18:48:54 -070072 dbus::MessageReader reader(method_call);
73
74 if (!reader.PopFileDescriptor(&file_handle)) {
75 LOG(ERROR) << "Couldn't extract Mojo IPC handle.";
76 return;
77 }
Prashant Malani112cc7a2017-07-28 18:48:54 -070078
Eric Caruso92f668e2018-03-30 15:12:41 -070079 if (!file_handle.is_valid()) {
80 LOG(ERROR) << "Couldn't get file handle sent over D-Bus.";
Prashant Malani112cc7a2017-07-28 18:48:54 -070081 return;
82 }
83
Eric Caruso92f668e2018-03-30 15:12:41 -070084 if (!base::SetCloseOnExec(file_handle.get())) {
Prashant Malani112cc7a2017-07-28 18:48:54 -070085 PLOG(ERROR) << "Failed setting FD_CLOEXEC on fd.";
86 return;
87 }
88
Eric Caruso92f668e2018-03-30 15:12:41 -070089 client_tracker_->AcceptProxyConnection(std::move(file_handle));
Prashant Malani112cc7a2017-07-28 18:48:54 -070090 LOG(INFO) << "MojoBridger connection established.";
Prashant Malani73c1ea12017-09-06 13:28:53 -070091 std::unique_ptr<dbus::Response> response =
92 dbus::Response::FromMethodCall(method_call);
Qijiang Fanaffa5802020-08-03 13:57:01 +090093 std::move(response_sender).Run(std::move(response));
Prashant Malani6789d662017-07-28 15:03:40 -070094}
95
Prashant Malani224d3ca2017-03-14 12:44:11 -070096} // namespace midis