blob: fef1193720fce2cb68027c82ba37e5d94f7ab01b [file] [log] [blame]
Andrew Moylanff6be512018-07-03 11:05:01 +10001// 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_MACHINE_LEARNING_SERVICE_IMPL_H_
6#define ML_MACHINE_LEARNING_SERVICE_IMPL_H_
7
Michael Martisa74af932018-08-13 16:52:36 +10008#include <map>
alanlxlcb1f8562018-11-01 15:16:11 +11009#include <memory>
Michael Martisa74af932018-08-13 16:52:36 +100010#include <string>
11
Andrew Moylanff6be512018-07-03 11:05:01 +100012#include <base/callback_forward.h>
13#include <base/macros.h>
Charles Zhaod4fb7b62020-08-25 17:21:58 +100014#include <dbus/bus.h>
Andrew Moylanb481af72020-07-09 15:22:00 +100015#include <mojo/public/cpp/bindings/pending_receiver.h>
16#include <mojo/public/cpp/bindings/receiver.h>
17#include <mojo/public/cpp/bindings/receiver_set.h>
Andrew Moylanff6be512018-07-03 11:05:01 +100018
Charles Zhaod4fb7b62020-08-25 17:21:58 +100019#include "ml/dlcservice_client.h"
Michael Martisa74af932018-08-13 16:52:36 +100020#include "ml/model_metadata.h"
Hidehiko Abeaa488c32018-08-31 23:49:41 +090021#include "ml/mojom/machine_learning_service.mojom.h"
Andrew Moylanff6be512018-07-03 11:05:01 +100022
23namespace ml {
24
25class MachineLearningServiceImpl
26 : public chromeos::machine_learning::mojom::MachineLearningService {
27 public:
Andrew Moylanb481af72020-07-09 15:22:00 +100028 // Creates an instance bound to `pipe`. The specified `disconnect_handler`
29 // will be invoked if the binding encounters a connection error or is closed.
Charles Zhaod4fb7b62020-08-25 17:21:58 +100030 // The `bus` is used to construct `dlcservice_client_` if it is not nullptr.
Andrew Moylanff6be512018-07-03 11:05:01 +100031 MachineLearningServiceImpl(mojo::ScopedMessagePipeHandle pipe,
Charles Zhaod4fb7b62020-08-25 17:21:58 +100032 base::Closure disconnect_handler,
33 dbus::Bus* bus = nullptr);
Andrew Moylanff6be512018-07-03 11:05:01 +100034
Andrew Moylan79b34a42020-07-08 11:13:11 +100035 // A interface to change `text_classifier_model_filename_` for testing. Should
Honglin Yuf33dce32019-12-05 15:10:39 +110036 // not be used outside of tests.
37 void SetTextClassifierModelFilenameForTesting(const std::string& filename);
Michael Martisa74af932018-08-13 16:52:36 +100038 protected:
39 // Testing constructor that allows overriding of the model dir. Should not be
40 // used outside of tests.
41 MachineLearningServiceImpl(mojo::ScopedMessagePipeHandle pipe,
Andrew Moylanb481af72020-07-09 15:22:00 +100042 base::Closure disconnect_handler,
Michael Martisa74af932018-08-13 16:52:36 +100043 const std::string& model_dir);
44
Andrew Moylanff6be512018-07-03 11:05:01 +100045 private:
46 // chromeos::machine_learning::mojom::MachineLearningService:
Andrew Moylanb481af72020-07-09 15:22:00 +100047 void Clone(mojo::PendingReceiver<
48 chromeos::machine_learning::mojom::MachineLearningService>
49 receiver) override;
Honglin Yu0ed72352019-08-27 17:42:01 +100050 void LoadBuiltinModel(
51 chromeos::machine_learning::mojom::BuiltinModelSpecPtr spec,
Andrew Moylanb481af72020-07-09 15:22:00 +100052 mojo::PendingReceiver<chromeos::machine_learning::mojom::Model> receiver,
Qijiang Fan5d381a02020-04-19 23:42:37 +090053 LoadBuiltinModelCallback callback) override;
Honglin Yu0ed72352019-08-27 17:42:01 +100054 void LoadFlatBufferModel(
55 chromeos::machine_learning::mojom::FlatBufferModelSpecPtr spec,
Andrew Moylanb481af72020-07-09 15:22:00 +100056 mojo::PendingReceiver<chromeos::machine_learning::mojom::Model> receiver,
Qijiang Fan5d381a02020-04-19 23:42:37 +090057 LoadFlatBufferModelCallback callback) override;
Honglin Yuf33dce32019-12-05 15:10:39 +110058 void LoadTextClassifier(
Andrew Moylanb481af72020-07-09 15:22:00 +100059 mojo::PendingReceiver<chromeos::machine_learning::mojom::TextClassifier>
60 receiver,
Honglin Yuf33dce32019-12-05 15:10:39 +110061 LoadTextClassifierCallback callback) override;
Charles Zhaoc882eb02020-07-27 10:02:35 +100062 void RemovedFunction_3() override;
charleszhao05c5a4a2020-06-09 16:49:54 +100063 void LoadHandwritingModelWithSpec(
64 chromeos::machine_learning::mojom::HandwritingRecognizerSpecPtr spec,
Andrew Moylanb481af72020-07-09 15:22:00 +100065 mojo::PendingReceiver<
66 chromeos::machine_learning::mojom::HandwritingRecognizer> receiver,
Charles Zhaoc882eb02020-07-27 10:02:35 +100067 LoadHandwritingModelWithSpecCallback callback) override;
charleszhao05c5a4a2020-06-09 16:49:54 +100068
Honglin Yuf33dce32019-12-05 15:10:39 +110069 // Init the icu data if it is not initialized yet.
70 void InitIcuIfNeeded();
71
72 // Used to hold the icu data read from file.
73 char* icu_data_;
74
75 std::string text_classifier_model_filename_;
76
Honglin Yu0ed72352019-08-27 17:42:01 +100077 // Metadata required to load builtin models. Initialized at construction.
78 const std::map<chromeos::machine_learning::mojom::BuiltinModelId,
79 BuiltinModelMetadata>
80 builtin_model_metadata_;
81
Michael Martisa74af932018-08-13 16:52:36 +100082 const std::string model_dir_;
Andrew Moylanff6be512018-07-03 11:05:01 +100083
Charles Zhaod4fb7b62020-08-25 17:21:58 +100084 // DlcserviceClient used to communicate with DlcService.
85 std::unique_ptr<DlcserviceClient> dlcservice_client_;
86
Andrew Moylanb481af72020-07-09 15:22:00 +100087 // Primordial receiver bootstrapped over D-Bus. Once opened, is never closed.
88 mojo::Receiver<chromeos::machine_learning::mojom::MachineLearningService>
89 receiver_;
Andrew Moylanff6be512018-07-03 11:05:01 +100090
Andrew Moylanb481af72020-07-09 15:22:00 +100091 // Additional receivers bound via `Clone`.
92 mojo::ReceiverSet<chromeos::machine_learning::mojom::MachineLearningService>
93 clone_receivers_;
Andrew Moylan2fb80af2020-07-08 10:52:08 +100094
Andrew Moylanff6be512018-07-03 11:05:01 +100095 DISALLOW_COPY_AND_ASSIGN(MachineLearningServiceImpl);
96};
97
98} // namespace ml
99
100#endif // ML_MACHINE_LEARNING_SERVICE_IMPL_H_