blob: be5ed8a8f8a695719e422abca6af324be8b1b2e5 [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>
13
Andrew Moylan40ee4fc2018-08-24 15:46:09 +100014#include "ml/metrics.h"
Hidehiko Abeaa488c32018-08-31 23:49:41 +090015#include "ml/mojom/machine_learning_service.mojom.h"
Andrew Moylanff6be512018-07-03 11:05:01 +100016
Ken Turneredb93932018-02-14 05:27:31 +110017namespace dbus {
18class MethodCall;
19} // namespace dbus
20
21namespace ml {
22
Andrew Moylanef116f92018-04-11 15:12:38 +100023class Daemon : public brillo::DBusDaemon {
Ken Turneredb93932018-02-14 05:27:31 +110024 public:
25 Daemon();
26 ~Daemon() override;
27
28 protected:
Andrew Moylanff6be512018-07-03 11:05:01 +100029 // brillo::DBusDaemon:
Ken Turneredb93932018-02-14 05:27:31 +110030 int OnInit() override;
31
32 private:
33 // This function initializes the D-Bus service. The primary function of the
34 // D-Bus interface is to receive a FD from the Chrome process so that we can
35 // bootstrap a Mojo IPC channel. Since we should expect requests for client
36 // registration to occur as soon as the D-Bus channel is up, this
37 // initialization should be the last thing that happens in Daemon::OnInit().
38 void InitDBus();
39
Andrew Moylanef116f92018-04-11 15:12:38 +100040 // Handles org.chromium.BootstrapMojoConnection D-Bus method calls.
Ken Turneredb93932018-02-14 05:27:31 +110041 void BootstrapMojoConnection(
42 dbus::MethodCall* method_call,
43 dbus::ExportedObject::ResponseSender response_sender);
44
Andrew Moylanff6be512018-07-03 11:05:01 +100045 // Responds to Mojo connection errors by quitting the daemon.
46 void OnConnectionError();
47
48 // The top-level interface. Empty until it is created & bound to a pipe by
49 // BootstrapMojoConnection.
50 std::unique_ptr<chromeos::machine_learning::mojom::MachineLearningService>
51 machine_learning_service_;
52
Andrew Moylan40ee4fc2018-08-24 15:46:09 +100053 // For periodic and on-demand UMA metrics logging.
54 Metrics metrics_;
55
Andrew Moylanef116f92018-04-11 15:12:38 +100056 // Must be last class member.
57 base::WeakPtrFactory<Daemon> weak_ptr_factory_;
Ken Turneredb93932018-02-14 05:27:31 +110058};
59
60} // namespace ml
61
62#endif // ML_DAEMON_H_