Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 1 | // 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 Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 8 | #include <map> |
alanlxl | cb1f856 | 2018-11-01 15:16:11 +1100 | [diff] [blame] | 9 | #include <memory> |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 10 | #include <string> |
| 11 | |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 12 | #include <base/callback_forward.h> |
| 13 | #include <base/macros.h> |
Charles Zhao | d4fb7b6 | 2020-08-25 17:21:58 +1000 | [diff] [blame^] | 14 | #include <dbus/bus.h> |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 15 | #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 Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 18 | |
Charles Zhao | d4fb7b6 | 2020-08-25 17:21:58 +1000 | [diff] [blame^] | 19 | #include "ml/dlcservice_client.h" |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 20 | #include "ml/model_metadata.h" |
Hidehiko Abe | aa488c3 | 2018-08-31 23:49:41 +0900 | [diff] [blame] | 21 | #include "ml/mojom/machine_learning_service.mojom.h" |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 22 | |
| 23 | namespace ml { |
| 24 | |
| 25 | class MachineLearningServiceImpl |
| 26 | : public chromeos::machine_learning::mojom::MachineLearningService { |
| 27 | public: |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 28 | // 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 Zhao | d4fb7b6 | 2020-08-25 17:21:58 +1000 | [diff] [blame^] | 30 | // The `bus` is used to construct `dlcservice_client_` if it is not nullptr. |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 31 | MachineLearningServiceImpl(mojo::ScopedMessagePipeHandle pipe, |
Charles Zhao | d4fb7b6 | 2020-08-25 17:21:58 +1000 | [diff] [blame^] | 32 | base::Closure disconnect_handler, |
| 33 | dbus::Bus* bus = nullptr); |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 34 | |
Andrew Moylan | 79b34a4 | 2020-07-08 11:13:11 +1000 | [diff] [blame] | 35 | // A interface to change `text_classifier_model_filename_` for testing. Should |
Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 36 | // not be used outside of tests. |
| 37 | void SetTextClassifierModelFilenameForTesting(const std::string& filename); |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 38 | 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 Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 42 | base::Closure disconnect_handler, |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 43 | const std::string& model_dir); |
| 44 | |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 45 | private: |
| 46 | // chromeos::machine_learning::mojom::MachineLearningService: |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 47 | void Clone(mojo::PendingReceiver< |
| 48 | chromeos::machine_learning::mojom::MachineLearningService> |
| 49 | receiver) override; |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 50 | void LoadBuiltinModel( |
| 51 | chromeos::machine_learning::mojom::BuiltinModelSpecPtr spec, |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 52 | mojo::PendingReceiver<chromeos::machine_learning::mojom::Model> receiver, |
Qijiang Fan | 5d381a0 | 2020-04-19 23:42:37 +0900 | [diff] [blame] | 53 | LoadBuiltinModelCallback callback) override; |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 54 | void LoadFlatBufferModel( |
| 55 | chromeos::machine_learning::mojom::FlatBufferModelSpecPtr spec, |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 56 | mojo::PendingReceiver<chromeos::machine_learning::mojom::Model> receiver, |
Qijiang Fan | 5d381a0 | 2020-04-19 23:42:37 +0900 | [diff] [blame] | 57 | LoadFlatBufferModelCallback callback) override; |
Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 58 | void LoadTextClassifier( |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 59 | mojo::PendingReceiver<chromeos::machine_learning::mojom::TextClassifier> |
| 60 | receiver, |
Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 61 | LoadTextClassifierCallback callback) override; |
Charles Zhao | c882eb0 | 2020-07-27 10:02:35 +1000 | [diff] [blame] | 62 | void RemovedFunction_3() override; |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 63 | void LoadHandwritingModelWithSpec( |
| 64 | chromeos::machine_learning::mojom::HandwritingRecognizerSpecPtr spec, |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 65 | mojo::PendingReceiver< |
| 66 | chromeos::machine_learning::mojom::HandwritingRecognizer> receiver, |
Charles Zhao | c882eb0 | 2020-07-27 10:02:35 +1000 | [diff] [blame] | 67 | LoadHandwritingModelWithSpecCallback callback) override; |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 68 | |
Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 69 | // 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 Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 77 | // 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 Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 82 | const std::string model_dir_; |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 83 | |
Charles Zhao | d4fb7b6 | 2020-08-25 17:21:58 +1000 | [diff] [blame^] | 84 | // DlcserviceClient used to communicate with DlcService. |
| 85 | std::unique_ptr<DlcserviceClient> dlcservice_client_; |
| 86 | |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 87 | // Primordial receiver bootstrapped over D-Bus. Once opened, is never closed. |
| 88 | mojo::Receiver<chromeos::machine_learning::mojom::MachineLearningService> |
| 89 | receiver_; |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 90 | |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 91 | // Additional receivers bound via `Clone`. |
| 92 | mojo::ReceiverSet<chromeos::machine_learning::mojom::MachineLearningService> |
| 93 | clone_receivers_; |
Andrew Moylan | 2fb80af | 2020-07-08 10:52:08 +1000 | [diff] [blame] | 94 | |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 95 | DISALLOW_COPY_AND_ASSIGN(MachineLearningServiceImpl); |
| 96 | }; |
| 97 | |
| 98 | } // namespace ml |
| 99 | |
| 100 | #endif // ML_MACHINE_LEARNING_SERVICE_IMPL_H_ |