Ken Turner | edb9393 | 2018-02-14 05:27:31 +1100 | [diff] [blame] | 1 | // Copyright 2018 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 | |
| 5 | #include "ml/daemon.h" |
| 6 | |
Andrew Moylan | ef116f9 | 2018-04-11 15:12:38 +1000 | [diff] [blame^] | 7 | #include <sysexits.h> |
| 8 | |
| 9 | #include <memory> |
| 10 | #include <utility> |
| 11 | |
| 12 | #include <base/bind.h> |
| 13 | #include <base/memory/ref_counted.h> |
| 14 | #include <chromeos/dbus/service_constants.h> |
| 15 | #include <dbus/bus.h> |
| 16 | #include <dbus/message.h> |
| 17 | |
Ken Turner | edb9393 | 2018-02-14 05:27:31 +1100 | [diff] [blame] | 18 | namespace ml { |
| 19 | |
Andrew Moylan | ef116f9 | 2018-04-11 15:12:38 +1000 | [diff] [blame^] | 20 | Daemon::Daemon() : weak_ptr_factory_(this) {} |
Ken Turner | edb9393 | 2018-02-14 05:27:31 +1100 | [diff] [blame] | 21 | |
| 22 | Daemon::~Daemon() {} |
| 23 | |
| 24 | int Daemon::OnInit() { |
Andrew Moylan | ef116f9 | 2018-04-11 15:12:38 +1000 | [diff] [blame^] | 25 | int exit_code = DBusDaemon::OnInit(); |
| 26 | if (exit_code != EX_OK) |
| 27 | return exit_code; |
| 28 | |
Ken Turner | edb9393 | 2018-02-14 05:27:31 +1100 | [diff] [blame] | 29 | InitDBus(); |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | void Daemon::InitDBus() { |
Andrew Moylan | ef116f9 | 2018-04-11 15:12:38 +1000 | [diff] [blame^] | 34 | LOG(INFO) << "Registering as handler for ML service in D-Bus ..."; |
| 35 | |
| 36 | // Get or create the ExportedObject for the ML service. |
| 37 | dbus::ExportedObject* const ml_service_exported_object = |
| 38 | bus_->GetExportedObject(dbus::ObjectPath(kMlServicePath)); |
| 39 | CHECK(ml_service_exported_object); |
| 40 | |
| 41 | // Register a handler of the BootstrapMojoConnection method. |
| 42 | CHECK(ml_service_exported_object->ExportMethodAndBlock( |
| 43 | kMlInterfaceName, kBootstrapMojoConnectionMethod, |
| 44 | base::Bind(&Daemon::BootstrapMojoConnection, |
| 45 | weak_ptr_factory_.GetWeakPtr()))); |
| 46 | |
| 47 | // Take ownership of the ML service. |
| 48 | CHECK(bus_->RequestOwnershipAndBlock(kMlServiceName, |
| 49 | dbus::Bus::REQUIRE_PRIMARY)); |
| 50 | |
| 51 | LOG(INFO) << "D-Bus registration succeeded."; |
Ken Turner | edb9393 | 2018-02-14 05:27:31 +1100 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void Daemon::BootstrapMojoConnection( |
| 55 | dbus::MethodCall* method_call, |
| 56 | dbus::ExportedObject::ResponseSender response_sender) { |
Andrew Moylan | ef116f9 | 2018-04-11 15:12:38 +1000 | [diff] [blame^] | 57 | LOG(INFO) << "Received bootstrap request"; |
| 58 | |
| 59 | // TODO(amoylan): Extract file descriptor from method_call and bind a |
| 60 | // ModelProviderImpl to it. |
| 61 | |
| 62 | std::unique_ptr<dbus::Response> response = |
| 63 | dbus::Response::FromMethodCall(method_call); |
| 64 | response_sender.Run(std::move(response)); |
Ken Turner | edb9393 | 2018-02-14 05:27:31 +1100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | } // namespace ml |