blob: c21b6fadd7f3baab3100e442872fdf6c2ff6f2b2 [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>
14#include <mojo/public/cpp/bindings/binding.h>
Andrew Moylan2fb80af2020-07-08 10:52:08 +100015#include <mojo/public/cpp/bindings/binding_set.h>
Andrew Moylanff6be512018-07-03 11:05:01 +100016
Michael Martisa74af932018-08-13 16:52:36 +100017#include "ml/model_metadata.h"
Hidehiko Abeaa488c32018-08-31 23:49:41 +090018#include "ml/mojom/machine_learning_service.mojom.h"
Andrew Moylanff6be512018-07-03 11:05:01 +100019
20namespace ml {
21
22class MachineLearningServiceImpl
23 : public chromeos::machine_learning::mojom::MachineLearningService {
24 public:
Andrew Moylan79b34a42020-07-08 11:13:11 +100025 // Creates an instance bound to `pipe`. The specified
26 // `connection_error_handler` will be invoked if the binding encounters a
Andrew Moylanff6be512018-07-03 11:05:01 +100027 // connection error.
28 MachineLearningServiceImpl(mojo::ScopedMessagePipeHandle pipe,
29 base::Closure connection_error_handler);
30
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,
39 base::Closure connection_error_handler,
40 const std::string& model_dir);
41
Andrew Moylanff6be512018-07-03 11:05:01 +100042 private:
43 // chromeos::machine_learning::mojom::MachineLearningService:
Andrew Moylan2fb80af2020-07-08 10:52:08 +100044 void Clone(chromeos::machine_learning::mojom::MachineLearningServiceRequest
45 request) override;
Honglin Yu0ed72352019-08-27 17:42:01 +100046 void LoadBuiltinModel(
47 chromeos::machine_learning::mojom::BuiltinModelSpecPtr spec,
48 chromeos::machine_learning::mojom::ModelRequest request,
Qijiang Fan5d381a02020-04-19 23:42:37 +090049 LoadBuiltinModelCallback callback) override;
Honglin Yu0ed72352019-08-27 17:42:01 +100050 void LoadFlatBufferModel(
51 chromeos::machine_learning::mojom::FlatBufferModelSpecPtr spec,
52 chromeos::machine_learning::mojom::ModelRequest request,
Qijiang Fan5d381a02020-04-19 23:42:37 +090053 LoadFlatBufferModelCallback callback) override;
Honglin Yuf33dce32019-12-05 15:10:39 +110054 void LoadTextClassifier(
55 chromeos::machine_learning::mojom::TextClassifierRequest request,
56 LoadTextClassifierCallback callback) override;
charleszhao17777f92020-04-23 12:53:11 +100057 void LoadHandwritingModel(
58 chromeos::machine_learning::mojom::HandwritingRecognizerRequest request,
59 LoadHandwritingModelCallback callback) override;
charleszhao05c5a4a2020-06-09 16:49:54 +100060 void LoadHandwritingModelWithSpec(
61 chromeos::machine_learning::mojom::HandwritingRecognizerSpecPtr spec,
62 chromeos::machine_learning::mojom::HandwritingRecognizerRequest request,
63 LoadHandwritingModelCallback callback) override;
64
Honglin Yuf33dce32019-12-05 15:10:39 +110065 // Init the icu data if it is not initialized yet.
66 void InitIcuIfNeeded();
67
68 // Used to hold the icu data read from file.
69 char* icu_data_;
70
71 std::string text_classifier_model_filename_;
72
Honglin Yu0ed72352019-08-27 17:42:01 +100073 // Metadata required to load builtin models. Initialized at construction.
74 const std::map<chromeos::machine_learning::mojom::BuiltinModelId,
75 BuiltinModelMetadata>
76 builtin_model_metadata_;
77
Michael Martisa74af932018-08-13 16:52:36 +100078 const std::string model_dir_;
Andrew Moylanff6be512018-07-03 11:05:01 +100079
Andrew Moylan2fb80af2020-07-08 10:52:08 +100080 // Primordial binding bootstrapped over D-Bus. Once opened, is never closed.
Andrew Moylanff6be512018-07-03 11:05:01 +100081 mojo::Binding<chromeos::machine_learning::mojom::MachineLearningService>
82 binding_;
83
Andrew Moylan2fb80af2020-07-08 10:52:08 +100084 // Additional bindings obtained via `Clone`.
85 mojo::BindingSet<chromeos::machine_learning::mojom::MachineLearningService>
86 clone_bindings_;
87
Andrew Moylanff6be512018-07-03 11:05:01 +100088 DISALLOW_COPY_AND_ASSIGN(MachineLearningServiceImpl);
89};
90
91} // namespace ml
92
93#endif // ML_MACHINE_LEARNING_SERVICE_IMPL_H_