Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 1 | // Copyright 2020 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_TEXT_CLASSIFIER_IMPL_H_ |
| 6 | #define ML_TEXT_CLASSIFIER_IMPL_H_ |
| 7 | |
| 8 | #include <list> |
| 9 | #include <map> |
| 10 | #include <memory> |
| 11 | #include <string> |
| 12 | |
| 13 | #include <annotator/annotator.h> |
| 14 | #include <base/macros.h> |
| 15 | #include <mojo/public/cpp/bindings/binding.h> |
| 16 | #include <tensorflow/lite/model.h> |
| 17 | #include <utils/memory/mmap.h> |
| 18 | |
| 19 | #include "ml/graph_executor_impl.h" |
| 20 | #include "ml/mojom/model.mojom.h" |
| 21 | #include "ml/mojom/text_classifier.mojom.h" |
| 22 | |
| 23 | namespace ml { |
| 24 | |
| 25 | class TextClassifierImpl |
| 26 | : public chromeos::machine_learning::mojom::TextClassifier { |
| 27 | public: |
| 28 | // Interface to create new |TextClassifierImpl| object. This function will |
| 29 | // automatically achieve strong binding.The model object will be deleted when |
| 30 | // the corresponding mojo connection meets error. |
| 31 | // Will return false if it fails to create the annotator object, otherwise |
| 32 | // return true. |
| 33 | static bool Create( |
| 34 | std::unique_ptr<libtextclassifier3::ScopedMmap>* mmap, |
| 35 | chromeos::machine_learning::mojom::TextClassifierRequest request); |
| 36 | |
| 37 | private: |
| 38 | // A private constructor, call |TextClassifierImpl::Create| to create new |
| 39 | // objects. |
| 40 | explicit TextClassifierImpl( |
| 41 | std::unique_ptr<libtextclassifier3::ScopedMmap>* mmap, |
| 42 | chromeos::machine_learning::mojom::TextClassifierRequest request); |
| 43 | |
| 44 | void SetConnectionErrorHandler(base::Closure connection_error_handler); |
| 45 | |
| 46 | // chromeos::machine_learning::mojom::TextClassifier: |
| 47 | void Annotate( |
| 48 | chromeos::machine_learning::mojom::TextAnnotationRequestPtr request, |
| 49 | AnnotateCallback callback) override; |
| 50 | |
| 51 | // chromeos::machine_learning::mojom::TextClassifier: |
| 52 | void SuggestSelection( |
| 53 | chromeos::machine_learning::mojom::TextSuggestSelectionRequestPtr request, |
| 54 | SuggestSelectionCallback callback) override; |
| 55 | |
| 56 | std::unique_ptr<libtextclassifier3::Annotator> annotator_; |
| 57 | |
| 58 | mojo::Binding<chromeos::machine_learning::mojom::TextClassifier> binding_; |
| 59 | |
| 60 | DISALLOW_COPY_AND_ASSIGN(TextClassifierImpl); |
| 61 | }; |
| 62 | |
| 63 | } // namespace ml |
| 64 | |
| 65 | #endif // ML_TEXT_CLASSIFIER_IMPL_H_ |