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> |
| 14 | #include <mojo/public/cpp/bindings/binding.h> |
| 15 | |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 16 | #include "ml/model_metadata.h" |
Hidehiko Abe | aa488c3 | 2018-08-31 23:49:41 +0900 | [diff] [blame] | 17 | #include "ml/mojom/machine_learning_service.mojom.h" |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 18 | |
| 19 | namespace ml { |
| 20 | |
| 21 | class 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 | |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 30 | protected: |
| 31 | // Testing constructor that allows overriding of the model dir. Should not be |
| 32 | // used outside of tests. |
| 33 | MachineLearningServiceImpl(mojo::ScopedMessagePipeHandle pipe, |
| 34 | base::Closure connection_error_handler, |
| 35 | const std::string& model_dir); |
| 36 | |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 37 | private: |
| 38 | // chromeos::machine_learning::mojom::MachineLearningService: |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame^] | 39 | // TODO(crbug.com/990619): Remove this once clients migrate to |
| 40 | // |LoadBuiltinModel|. |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 41 | void LoadModel(chromeos::machine_learning::mojom::ModelSpecPtr spec, |
| 42 | chromeos::machine_learning::mojom::ModelRequest request, |
| 43 | const LoadModelCallback& callback) override; |
| 44 | |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame^] | 45 | // chromeos::machine_learning::mojom::MachineLearningService: |
| 46 | void LoadBuiltinModel( |
| 47 | chromeos::machine_learning::mojom::BuiltinModelSpecPtr spec, |
| 48 | chromeos::machine_learning::mojom::ModelRequest request, |
| 49 | const LoadBuiltinModelCallback& callback) override; |
| 50 | |
| 51 | // chromeos::machine_learning::mojom::MachineLearningService: |
| 52 | void LoadFlatBufferModel( |
| 53 | chromeos::machine_learning::mojom::FlatBufferModelSpecPtr spec, |
| 54 | chromeos::machine_learning::mojom::ModelRequest request, |
| 55 | const LoadFlatBufferModelCallback& callback) override; |
| 56 | |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 57 | // Metadata required to load models. Initialized at construction. |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame^] | 58 | // TODO(crbug.com/990619): Remove this once clients migrate to |
| 59 | // |LoadBuiltinModel|. |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 60 | const std::map<chromeos::machine_learning::mojom::ModelId, ModelMetadata> |
| 61 | model_metadata_; |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame^] | 62 | |
| 63 | // Metadata required to load builtin models. Initialized at construction. |
| 64 | const std::map<chromeos::machine_learning::mojom::BuiltinModelId, |
| 65 | BuiltinModelMetadata> |
| 66 | builtin_model_metadata_; |
| 67 | |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 68 | const std::string model_dir_; |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 69 | |
| 70 | mojo::Binding<chromeos::machine_learning::mojom::MachineLearningService> |
| 71 | binding_; |
| 72 | |
| 73 | DISALLOW_COPY_AND_ASSIGN(MachineLearningServiceImpl); |
| 74 | }; |
| 75 | |
| 76 | } // namespace ml |
| 77 | |
| 78 | #endif // ML_MACHINE_LEARNING_SERVICE_IMPL_H_ |