blob: 942a9042c2679ac5fd57b0c8f0957d36eea1ed80 [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#ifndef ML_DAEMON_H_
6#define ML_DAEMON_H_
7
Andrew Moylanff6be512018-07-03 11:05:01 +10008#include <memory>
9
Andrew Moylan40ee4fc2018-08-24 15:46:09 +100010#include <base/memory/weak_ptr.h>
Andrew Moylanef116f92018-04-11 15:12:38 +100011#include <brillo/daemons/dbus_daemon.h>
Ken Turneredb93932018-02-14 05:27:31 +110012#include <dbus/exported_object.h>
Qijiang Fan7da812d2019-11-26 10:48:46 +090013#include <mojo/core/embedder/scoped_ipc_support.h>
Ken Turneredb93932018-02-14 05:27:31 +110014
Andrew Moylan40ee4fc2018-08-24 15:46:09 +100015#include "ml/metrics.h"
Hidehiko Abeaa488c32018-08-31 23:49:41 +090016#include "ml/mojom/machine_learning_service.mojom.h"
Andrew Moylanff6be512018-07-03 11:05:01 +100017
Ken Turneredb93932018-02-14 05:27:31 +110018namespace dbus {
19class MethodCall;
20} // namespace dbus
21
22namespace ml {
23
Andrew Moylanef116f92018-04-11 15:12:38 +100024class Daemon : public brillo::DBusDaemon {
Ken Turneredb93932018-02-14 05:27:31 +110025 public:
26 Daemon();
27 ~Daemon() override;
28
29 protected:
Andrew Moylanff6be512018-07-03 11:05:01 +100030 // brillo::DBusDaemon:
Ken Turneredb93932018-02-14 05:27:31 +110031 int OnInit() override;
32
33 private:
34 // This function initializes the D-Bus service. The primary function of the
35 // D-Bus interface is to receive a FD from the Chrome process so that we can
36 // bootstrap a Mojo IPC channel. Since we should expect requests for client
37 // registration to occur as soon as the D-Bus channel is up, this
38 // initialization should be the last thing that happens in Daemon::OnInit().
39 void InitDBus();
40
Andrew Moylanef116f92018-04-11 15:12:38 +100041 // Handles org.chromium.BootstrapMojoConnection D-Bus method calls.
Ken Turneredb93932018-02-14 05:27:31 +110042 void BootstrapMojoConnection(
43 dbus::MethodCall* method_call,
44 dbus::ExportedObject::ResponseSender response_sender);
45
Andrew Moylanff6be512018-07-03 11:05:01 +100046 // Responds to Mojo connection errors by quitting the daemon.
47 void OnConnectionError();
48
Qijiang Fan7da812d2019-11-26 10:48:46 +090049 // IPC Support
50 std::unique_ptr<mojo::core::ScopedIPCSupport> ipc_support_;
51
Andrew Moylanff6be512018-07-03 11:05:01 +100052 // The top-level interface. Empty until it is created & bound to a pipe by
53 // BootstrapMojoConnection.
54 std::unique_ptr<chromeos::machine_learning::mojom::MachineLearningService>
55 machine_learning_service_;
56
Andrew Moylan40ee4fc2018-08-24 15:46:09 +100057 // For periodic and on-demand UMA metrics logging.
58 Metrics metrics_;
59
Andrew Moylanef116f92018-04-11 15:12:38 +100060 // Must be last class member.
61 base::WeakPtrFactory<Daemon> weak_ptr_factory_;
Ken Turneredb93932018-02-14 05:27:31 +110062};
63
64} // namespace ml
65
66#endif // ML_DAEMON_H_