blob: eeaafad6223468b33eeea312d577dd9b4ce29b12 [file] [log] [blame]
Ken Turneredb93932018-02-14 05:27:31 +11001// 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 Moylanef116f92018-04-11 15:12:38 +10007#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 Turneredb93932018-02-14 05:27:31 +110018namespace ml {
19
Andrew Moylanef116f92018-04-11 15:12:38 +100020Daemon::Daemon() : weak_ptr_factory_(this) {}
Ken Turneredb93932018-02-14 05:27:31 +110021
22Daemon::~Daemon() {}
23
24int Daemon::OnInit() {
Andrew Moylanef116f92018-04-11 15:12:38 +100025 int exit_code = DBusDaemon::OnInit();
26 if (exit_code != EX_OK)
27 return exit_code;
28
Ken Turneredb93932018-02-14 05:27:31 +110029 InitDBus();
30 return 0;
31}
32
33void Daemon::InitDBus() {
Andrew Moylanef116f92018-04-11 15:12:38 +100034 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 Turneredb93932018-02-14 05:27:31 +110052}
53
54void Daemon::BootstrapMojoConnection(
55 dbus::MethodCall* method_call,
56 dbus::ExportedObject::ResponseSender response_sender) {
Andrew Moylanef116f92018-04-11 15:12:38 +100057 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 Turneredb93932018-02-14 05:27:31 +110065}
66
67} // namespace ml