blob: 759c776d91775bd84311fd209e69993dfb5866aa [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>
15
Michael Martisa74af932018-08-13 16:52:36 +100016#include "ml/model_metadata.h"
Hidehiko Abeaa488c32018-08-31 23:49:41 +090017#include "ml/mojom/machine_learning_service.mojom.h"
Andrew Moylanff6be512018-07-03 11:05:01 +100018
19namespace ml {
20
21class MachineLearningServiceImpl
22 : public chromeos::machine_learning::mojom::MachineLearningService {
23 public:
24 // Creates an instance bound to |pipe|. The specified
25 // |connection_error_handler| will be invoked if the binding encounters a
26 // connection error.
27 MachineLearningServiceImpl(mojo::ScopedMessagePipeHandle pipe,
28 base::Closure connection_error_handler);
29
Honglin Yuf33dce32019-12-05 15:10:39 +110030 // A interface to change |text_classifier_model_filename_| for testing. Should
31 // not be used outside of tests.
32 void SetTextClassifierModelFilenameForTesting(const std::string& filename);
33
Michael Martisa74af932018-08-13 16:52:36 +100034 protected:
35 // Testing constructor that allows overriding of the model dir. Should not be
36 // used outside of tests.
37 MachineLearningServiceImpl(mojo::ScopedMessagePipeHandle pipe,
38 base::Closure connection_error_handler,
39 const std::string& model_dir);
40
Andrew Moylanff6be512018-07-03 11:05:01 +100041 private:
42 // chromeos::machine_learning::mojom::MachineLearningService:
Honglin Yu0ed72352019-08-27 17:42:01 +100043 void LoadBuiltinModel(
44 chromeos::machine_learning::mojom::BuiltinModelSpecPtr spec,
45 chromeos::machine_learning::mojom::ModelRequest request,
Qijiang Fan5d381a02020-04-19 23:42:37 +090046 LoadBuiltinModelCallback callback) override;
Honglin Yu0ed72352019-08-27 17:42:01 +100047
48 // chromeos::machine_learning::mojom::MachineLearningService:
49 void LoadFlatBufferModel(
50 chromeos::machine_learning::mojom::FlatBufferModelSpecPtr spec,
51 chromeos::machine_learning::mojom::ModelRequest request,
Qijiang Fan5d381a02020-04-19 23:42:37 +090052 LoadFlatBufferModelCallback callback) override;
Honglin Yu0ed72352019-08-27 17:42:01 +100053
Honglin Yuf33dce32019-12-05 15:10:39 +110054 // chromeos::machine_learning::mojom::MachineLearningService:
55 void LoadTextClassifier(
56 chromeos::machine_learning::mojom::TextClassifierRequest request,
57 LoadTextClassifierCallback callback) override;
58
59 // Init the icu data if it is not initialized yet.
60 void InitIcuIfNeeded();
61
62 // Used to hold the icu data read from file.
63 char* icu_data_;
64
65 std::string text_classifier_model_filename_;
66
Honglin Yu0ed72352019-08-27 17:42:01 +100067 // Metadata required to load builtin models. Initialized at construction.
68 const std::map<chromeos::machine_learning::mojom::BuiltinModelId,
69 BuiltinModelMetadata>
70 builtin_model_metadata_;
71
Michael Martisa74af932018-08-13 16:52:36 +100072 const std::string model_dir_;
Andrew Moylanff6be512018-07-03 11:05:01 +100073
74 mojo::Binding<chromeos::machine_learning::mojom::MachineLearningService>
75 binding_;
76
77 DISALLOW_COPY_AND_ASSIGN(MachineLearningServiceImpl);
78};
79
80} // namespace ml
81
82#endif // ML_MACHINE_LEARNING_SERVICE_IMPL_H_