ml: Add mojo API of TextClassifier/Annotator.
This CL implements the mojo API of text classifier.
BUG=chromium:1020419
TEST=pass the existing unit tests.
TEST=on device (eve), 1+2=3 works.
TEST=on device (eve), Annotate() and SuggestSelection() work.
Change-Id: Iadfe5747f3b2d1f8c8a8d9f0b975e2e8cda50726
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/1951350
Tested-by: Honglin Yu <honglinyu@chromium.org>
Commit-Queue: Honglin Yu <honglinyu@chromium.org>
Reviewed-by: Sam McNally <sammc@chromium.org>
Reviewed-by: Andrew Moylan <amoylan@chromium.org>
diff --git a/ml/text_classifier_impl.h b/ml/text_classifier_impl.h
new file mode 100644
index 0000000..1dafbb0
--- /dev/null
+++ b/ml/text_classifier_impl.h
@@ -0,0 +1,65 @@
+// Copyright 2020 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef ML_TEXT_CLASSIFIER_IMPL_H_
+#define ML_TEXT_CLASSIFIER_IMPL_H_
+
+#include <list>
+#include <map>
+#include <memory>
+#include <string>
+
+#include <annotator/annotator.h>
+#include <base/macros.h>
+#include <mojo/public/cpp/bindings/binding.h>
+#include <tensorflow/lite/model.h>
+#include <utils/memory/mmap.h>
+
+#include "ml/graph_executor_impl.h"
+#include "ml/mojom/model.mojom.h"
+#include "ml/mojom/text_classifier.mojom.h"
+
+namespace ml {
+
+class TextClassifierImpl
+ : public chromeos::machine_learning::mojom::TextClassifier {
+ public:
+ // Interface to create new |TextClassifierImpl| object. This function will
+ // automatically achieve strong binding.The model object will be deleted when
+ // the corresponding mojo connection meets error.
+ // Will return false if it fails to create the annotator object, otherwise
+ // return true.
+ static bool Create(
+ std::unique_ptr<libtextclassifier3::ScopedMmap>* mmap,
+ chromeos::machine_learning::mojom::TextClassifierRequest request);
+
+ private:
+ // A private constructor, call |TextClassifierImpl::Create| to create new
+ // objects.
+ explicit TextClassifierImpl(
+ std::unique_ptr<libtextclassifier3::ScopedMmap>* mmap,
+ chromeos::machine_learning::mojom::TextClassifierRequest request);
+
+ void SetConnectionErrorHandler(base::Closure connection_error_handler);
+
+ // chromeos::machine_learning::mojom::TextClassifier:
+ void Annotate(
+ chromeos::machine_learning::mojom::TextAnnotationRequestPtr request,
+ AnnotateCallback callback) override;
+
+ // chromeos::machine_learning::mojom::TextClassifier:
+ void SuggestSelection(
+ chromeos::machine_learning::mojom::TextSuggestSelectionRequestPtr request,
+ SuggestSelectionCallback callback) override;
+
+ std::unique_ptr<libtextclassifier3::Annotator> annotator_;
+
+ mojo::Binding<chromeos::machine_learning::mojom::TextClassifier> binding_;
+
+ DISALLOW_COPY_AND_ASSIGN(TextClassifierImpl);
+};
+
+} // namespace ml
+
+#endif // ML_TEXT_CLASSIFIER_IMPL_H_