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 | #ifndef ML_DAEMON_H_ |
| 6 | #define ML_DAEMON_H_ |
| 7 | |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
Andrew Moylan | 40ee4fc | 2018-08-24 15:46:09 +1000 | [diff] [blame] | 10 | #include <base/memory/weak_ptr.h> |
Andrew Moylan | ef116f9 | 2018-04-11 15:12:38 +1000 | [diff] [blame] | 11 | #include <brillo/daemons/dbus_daemon.h> |
Ken Turner | edb9393 | 2018-02-14 05:27:31 +1100 | [diff] [blame] | 12 | #include <dbus/exported_object.h> |
Qijiang Fan | 7da812d | 2019-11-26 10:48:46 +0900 | [diff] [blame^] | 13 | #include <mojo/core/embedder/scoped_ipc_support.h> |
Ken Turner | edb9393 | 2018-02-14 05:27:31 +1100 | [diff] [blame] | 14 | |
Andrew Moylan | 40ee4fc | 2018-08-24 15:46:09 +1000 | [diff] [blame] | 15 | #include "ml/metrics.h" |
Hidehiko Abe | aa488c3 | 2018-08-31 23:49:41 +0900 | [diff] [blame] | 16 | #include "ml/mojom/machine_learning_service.mojom.h" |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 17 | |
Ken Turner | edb9393 | 2018-02-14 05:27:31 +1100 | [diff] [blame] | 18 | namespace dbus { |
| 19 | class MethodCall; |
| 20 | } // namespace dbus |
| 21 | |
| 22 | namespace ml { |
| 23 | |
Andrew Moylan | ef116f9 | 2018-04-11 15:12:38 +1000 | [diff] [blame] | 24 | class Daemon : public brillo::DBusDaemon { |
Ken Turner | edb9393 | 2018-02-14 05:27:31 +1100 | [diff] [blame] | 25 | public: |
| 26 | Daemon(); |
| 27 | ~Daemon() override; |
| 28 | |
| 29 | protected: |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 30 | // brillo::DBusDaemon: |
Ken Turner | edb9393 | 2018-02-14 05:27:31 +1100 | [diff] [blame] | 31 | 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 Moylan | ef116f9 | 2018-04-11 15:12:38 +1000 | [diff] [blame] | 41 | // Handles org.chromium.BootstrapMojoConnection D-Bus method calls. |
Ken Turner | edb9393 | 2018-02-14 05:27:31 +1100 | [diff] [blame] | 42 | void BootstrapMojoConnection( |
| 43 | dbus::MethodCall* method_call, |
| 44 | dbus::ExportedObject::ResponseSender response_sender); |
| 45 | |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 46 | // Responds to Mojo connection errors by quitting the daemon. |
| 47 | void OnConnectionError(); |
| 48 | |
Qijiang Fan | 7da812d | 2019-11-26 10:48:46 +0900 | [diff] [blame^] | 49 | // IPC Support |
| 50 | std::unique_ptr<mojo::core::ScopedIPCSupport> ipc_support_; |
| 51 | |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 52 | // 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 Moylan | 40ee4fc | 2018-08-24 15:46:09 +1000 | [diff] [blame] | 57 | // For periodic and on-demand UMA metrics logging. |
| 58 | Metrics metrics_; |
| 59 | |
Andrew Moylan | ef116f9 | 2018-04-11 15:12:38 +1000 | [diff] [blame] | 60 | // Must be last class member. |
| 61 | base::WeakPtrFactory<Daemon> weak_ptr_factory_; |
Ken Turner | edb9393 | 2018-02-14 05:27:31 +1100 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | } // namespace ml |
| 65 | |
| 66 | #endif // ML_DAEMON_H_ |