charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [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_HANDWRITING_RECOGNIZER_IMPL_H_ |
| 6 | #define ML_HANDWRITING_RECOGNIZER_IMPL_H_ |
| 7 | |
| 8 | #include <base/callback_forward.h> |
| 9 | #include <base/macros.h> |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 10 | #include <mojo/public/cpp/bindings/pending_receiver.h> |
| 11 | #include <mojo/public/cpp/bindings/receiver.h> |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 12 | |
Honglin Yu | cfb1930 | 2020-08-17 17:13:14 +1000 | [diff] [blame] | 13 | #include "chrome/knowledge/handwriting/handwriting_interface.pb.h" |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 14 | #include "ml/handwriting.h" |
| 15 | #include "ml/mojom/handwriting_recognizer.mojom.h" |
| 16 | |
| 17 | namespace ml { |
| 18 | |
| 19 | // The implementation of HandwritingRecognizer. |
| 20 | class HandwritingRecognizerImpl |
| 21 | : public chromeos::machine_learning::mojom::HandwritingRecognizer { |
| 22 | public: |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 23 | // Constructs a HandwritingRecognizerImpl; and set disconnect handler so |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 24 | // that the HandwritingRecognizerImpl will be deleted when the mojom |
| 25 | // connection is destroyed. |
| 26 | // Returns whether the object is create successfully. |
| 27 | static bool Create( |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 28 | chromeos::machine_learning::mojom::HandwritingRecognizerSpecPtr spec, |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 29 | mojo::PendingReceiver< |
| 30 | chromeos::machine_learning::mojom::HandwritingRecognizer> receiver); |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 31 | |
| 32 | // Called when mojom connection is destroyed. |
| 33 | ~HandwritingRecognizerImpl(); |
| 34 | |
| 35 | private: |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 36 | // Creates a HandwritingRecognizer and Binds to `receiver` inside so that |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 37 | // Recognize can be called on the other side for a particular handwriting |
| 38 | // reconition query. |
| 39 | HandwritingRecognizerImpl( |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 40 | chromeos::machine_learning::mojom::HandwritingRecognizerSpecPtr spec, |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 41 | mojo::PendingReceiver< |
| 42 | chromeos::machine_learning::mojom::HandwritingRecognizer> receiver); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 43 | HandwritingRecognizerImpl(const HandwritingRecognizerImpl&) = delete; |
| 44 | HandwritingRecognizerImpl& operator=(const HandwritingRecognizerImpl&) = |
| 45 | delete; |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 46 | |
| 47 | // mojom::HandwritingRecognizer: |
| 48 | void Recognize( |
| 49 | chromeos::machine_learning::mojom::HandwritingRecognitionQueryPtr query, |
| 50 | RecognizeCallback callback) override; |
| 51 | |
| 52 | bool successfully_loaded_; |
| 53 | // Pointer to the internal implementation of HandwritingRecognizer inside |
| 54 | // the HandwritingLibrary. |
| 55 | ::HandwritingRecognizer recognizer_; |
Charles Zhao | c882eb0 | 2020-07-27 10:02:35 +1000 | [diff] [blame] | 56 | const ml::HandwritingLibrary* const library_; |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 57 | |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 58 | mojo::Receiver<chromeos::machine_learning::mojom::HandwritingRecognizer> |
| 59 | receiver_; |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | } // namespace ml |
| 63 | |
| 64 | #endif // ML_HANDWRITING_RECOGNIZER_IMPL_H_ |