blob: e93fcc1842908046d2df4ca4ed0525b0f8dd621a [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 Zhao6d467e62020-08-31 10:02:03 +100062 void LoadHandwritingModel(
63 chromeos::machine_learning::mojom::HandwritingRecognizerSpecPtr spec,
64 mojo::PendingReceiver<
65 chromeos::machine_learning::mojom::HandwritingRecognizer> receiver,
66 LoadHandwritingModelCallback callback) override;
charleszhao05c5a4a2020-06-09 16:49:54 +100067 void LoadHandwritingModelWithSpec(
68 chromeos::machine_learning::mojom::HandwritingRecognizerSpecPtr spec,
Andrew Moylanb481af72020-07-09 15:22:00 +100069 mojo::PendingReceiver<
70 chromeos::machine_learning::mojom::HandwritingRecognizer> receiver,
Charles Zhaoc882eb02020-07-27 10:02:35 +100071 LoadHandwritingModelWithSpecCallback callback) override;
charleszhao05c5a4a2020-06-09 16:49:54 +100072
Honglin Yuf33dce32019-12-05 15:10:39 +110073 // Init the icu data if it is not initialized yet.
74 void InitIcuIfNeeded();
75
76 // Used to hold the icu data read from file.
77 char* icu_data_;
78
79 std::string text_classifier_model_filename_;
80
Honglin Yu0ed72352019-08-27 17:42:01 +100081 // Metadata required to load builtin models. Initialized at construction.
82 const std::map<chromeos::machine_learning::mojom::BuiltinModelId,
83 BuiltinModelMetadata>
84 builtin_model_metadata_;
85
Michael Martisa74af932018-08-13 16:52:36 +100086 const std::string model_dir_;
Andrew Moylanff6be512018-07-03 11:05:01 +100087
Charles Zhaod4fb7b62020-08-25 17:21:58 +100088 // DlcserviceClient used to communicate with DlcService.
89 std::unique_ptr<DlcserviceClient> dlcservice_client_;
90
Andrew Moylanb481af72020-07-09 15:22:00 +100091 // Primordial receiver bootstrapped over D-Bus. Once opened, is never closed.
92 mojo::Receiver<chromeos::machine_learning::mojom::MachineLearningService>
93 receiver_;
Andrew Moylanff6be512018-07-03 11:05:01 +100094
Andrew Moylanb481af72020-07-09 15:22:00 +100095 // Additional receivers bound via `Clone`.
96 mojo::ReceiverSet<chromeos::machine_learning::mojom::MachineLearningService>
97 clone_receivers_;
Andrew Moylan2fb80af2020-07-08 10:52:08 +100098
Andrew Moylanff6be512018-07-03 11:05:01 +100099 DISALLOW_COPY_AND_ASSIGN(MachineLearningServiceImpl);
100};
101
102} // namespace ml
103
104#endif // ML_MACHINE_LEARNING_SERVICE_IMPL_H_