blob: 62b896676b7f593102cbdcf8caccf1b8f30b6203 [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>
Andrew Moylanb481af72020-07-09 15:22:00 +100014#include <mojo/public/cpp/bindings/pending_receiver.h>
15#include <mojo/public/cpp/bindings/receiver.h>
16#include <mojo/public/cpp/bindings/receiver_set.h>
Andrew Moylanff6be512018-07-03 11:05:01 +100017
Michael Martisa74af932018-08-13 16:52:36 +100018#include "ml/model_metadata.h"
Hidehiko Abeaa488c32018-08-31 23:49:41 +090019#include "ml/mojom/machine_learning_service.mojom.h"
Andrew Moylanff6be512018-07-03 11:05:01 +100020
21namespace ml {
22
23class MachineLearningServiceImpl
24 : public chromeos::machine_learning::mojom::MachineLearningService {
25 public:
Andrew Moylanb481af72020-07-09 15:22:00 +100026 // Creates an instance bound to `pipe`. The specified `disconnect_handler`
27 // will be invoked if the binding encounters a connection error or is closed.
Andrew Moylanff6be512018-07-03 11:05:01 +100028 MachineLearningServiceImpl(mojo::ScopedMessagePipeHandle pipe,
Andrew Moylanb481af72020-07-09 15:22:00 +100029 base::Closure disconnect_handler);
Andrew Moylanff6be512018-07-03 11:05:01 +100030
Andrew Moylan79b34a42020-07-08 11:13:11 +100031 // A interface to change `text_classifier_model_filename_` for testing. Should
Honglin Yuf33dce32019-12-05 15:10:39 +110032 // not be used outside of tests.
33 void SetTextClassifierModelFilenameForTesting(const std::string& filename);
34
Michael Martisa74af932018-08-13 16:52:36 +100035 protected:
36 // Testing constructor that allows overriding of the model dir. Should not be
37 // used outside of tests.
38 MachineLearningServiceImpl(mojo::ScopedMessagePipeHandle pipe,
Andrew Moylanb481af72020-07-09 15:22:00 +100039 base::Closure disconnect_handler,
Michael Martisa74af932018-08-13 16:52:36 +100040 const std::string& model_dir);
41
Andrew Moylanff6be512018-07-03 11:05:01 +100042 private:
43 // chromeos::machine_learning::mojom::MachineLearningService:
Andrew Moylanb481af72020-07-09 15:22:00 +100044 void Clone(mojo::PendingReceiver<
45 chromeos::machine_learning::mojom::MachineLearningService>
46 receiver) override;
Honglin Yu0ed72352019-08-27 17:42:01 +100047 void LoadBuiltinModel(
48 chromeos::machine_learning::mojom::BuiltinModelSpecPtr spec,
Andrew Moylanb481af72020-07-09 15:22:00 +100049 mojo::PendingReceiver<chromeos::machine_learning::mojom::Model> receiver,
Qijiang Fan5d381a02020-04-19 23:42:37 +090050 LoadBuiltinModelCallback callback) override;
Honglin Yu0ed72352019-08-27 17:42:01 +100051 void LoadFlatBufferModel(
52 chromeos::machine_learning::mojom::FlatBufferModelSpecPtr spec,
Andrew Moylanb481af72020-07-09 15:22:00 +100053 mojo::PendingReceiver<chromeos::machine_learning::mojom::Model> receiver,
Qijiang Fan5d381a02020-04-19 23:42:37 +090054 LoadFlatBufferModelCallback callback) override;
Honglin Yuf33dce32019-12-05 15:10:39 +110055 void LoadTextClassifier(
Andrew Moylanb481af72020-07-09 15:22:00 +100056 mojo::PendingReceiver<chromeos::machine_learning::mojom::TextClassifier>
57 receiver,
Honglin Yuf33dce32019-12-05 15:10:39 +110058 LoadTextClassifierCallback callback) override;
charleszhao17777f92020-04-23 12:53:11 +100059 void LoadHandwritingModel(
Andrew Moylanb481af72020-07-09 15:22:00 +100060 mojo::PendingReceiver<
61 chromeos::machine_learning::mojom::HandwritingRecognizer> receiver,
charleszhao17777f92020-04-23 12:53:11 +100062 LoadHandwritingModelCallback callback) 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,
charleszhao05c5a4a2020-06-09 16:49:54 +100067 LoadHandwritingModelCallback callback) override;
68
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
Andrew Moylanb481af72020-07-09 15:22:00 +100084 // Primordial receiver bootstrapped over D-Bus. Once opened, is never closed.
85 mojo::Receiver<chromeos::machine_learning::mojom::MachineLearningService>
86 receiver_;
Andrew Moylanff6be512018-07-03 11:05:01 +100087
Andrew Moylanb481af72020-07-09 15:22:00 +100088 // Additional receivers bound via `Clone`.
89 mojo::ReceiverSet<chromeos::machine_learning::mojom::MachineLearningService>
90 clone_receivers_;
Andrew Moylan2fb80af2020-07-08 10:52:08 +100091
Andrew Moylanff6be512018-07-03 11:05:01 +100092 DISALLOW_COPY_AND_ASSIGN(MachineLearningServiceImpl);
93};
94
95} // namespace ml
96
97#endif // ML_MACHINE_LEARNING_SERVICE_IMPL_H_