1 // Copyright 2018 The Chromium 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 CHROMEOS_SERVICES_MACHINE_LEARNING_PUBLIC_CPP_SERVICE_CONNECTION_H_
6 #define CHROMEOS_SERVICES_MACHINE_LEARNING_PUBLIC_CPP_SERVICE_CONNECTION_H_
7 
8 #include "chromeos/services/machine_learning/public/mojom/machine_learning_service.mojom.h"
9 #include "mojo/public/cpp/bindings/pending_receiver.h"
10 
11 namespace chromeos {
12 namespace machine_learning {
13 
14 // Encapsulates a connection to the Chrome OS ML Service daemon via its Mojo
15 // interface.
16 // Usage for Built-in models:
17 //   mojo::Remote<chromeos::machine_learning::mojom::Model> model;
18 //   chromeos::machine_learning::mojom::BuiltinModelSpecPtr spec =
19 //       chromeos::machine_learning::mojom::BuiltinModelSpec::New();
20 //   spec->id = ...;
21 //   chromeos::machine_learning::ServiceConnection::GetInstance()
22 //       ->LoadBuiltinModel(std::move(spec), model.BindNewPipeAndPassReceiver(),
23 //                          base::BindOnce(&MyCallBack));
24 //   // Use |model| or wait for |MyCallBack|.
25 // Usage for Flatbuffer models:
26 //   mojo::Remote<chromeos::machine_learning::mojom::Model> model;
27 //   chromeos::machine_learning::mojom::FlatBufferModelSpecPtr spec =
28 //       chromeos::machine_learning::mojom::FlatBufferModelSpec::New();
29 //   spec->model_string = ...;
30 //   spec->inputs = ...;
31 //   spec->outputs = ...;
32 //   spec->metrics_model_name = ...;
33 //   chromeos::machine_learning::ServiceConnection::GetInstance()
34 //       ->LoadFlatBufferModel(std::move(spec),
35 //                             model.BindNewPipeAndPassReceiver(),
36 //                             base::BindOnce(&MyCallBack));
37 //
38 // Sequencing: Must be used on a single sequence (may be created on another).
39 class ServiceConnection {
40  public:
41   static ServiceConnection* GetInstance();
42   // Overrides the result of GetInstance() for use in tests.
43   // Does not take ownership of |fake_service_connection|.
44   static void UseFakeServiceConnectionForTesting(
45       ServiceConnection* fake_service_connection);
46 
47   // Instruct ML daemon to load the builtin model specified in |spec|, binding a
48   // Model implementation to |receiver|. Bootstraps the initial Mojo connection
49   // to the daemon if necessary.
50   virtual void LoadBuiltinModel(
51       mojom::BuiltinModelSpecPtr spec,
52       mojo::PendingReceiver<mojom::Model> receiver,
53       mojom::MachineLearningService::LoadBuiltinModelCallback
54           result_callback) = 0;
55 
56   // Instruct ML daemon to load the flatbuffer model specified in |spec|,
57   // binding a Model implementation to |receiver|. Bootstraps the initial Mojo
58   // connection to the daemon if necessary.
59   virtual void LoadFlatBufferModel(
60       mojom::FlatBufferModelSpecPtr spec,
61       mojo::PendingReceiver<mojom::Model> receiver,
62       mojom::MachineLearningService::LoadFlatBufferModelCallback
63           result_callback) = 0;
64 
65   // Instruct ML daemon to load the TextClassifier model, binding a
66   // TextClassifier implementation to |receiver|. Bootstraps the initial Mojo
67   // connection to the daemon if necessary.
68   virtual void LoadTextClassifier(
69       mojo::PendingReceiver<mojom::TextClassifier> receiver,
70       mojom::MachineLearningService::LoadTextClassifierCallback
71           result_callback) = 0;
72 
73   // Instruct ML daemon to load the Handwriting model with the given |spec|,
74   // binding a Handwriting implementation to |receiver|. Bootstraps the initial
75   // Mojo connection to the daemon if necessary.
76   virtual void LoadHandwritingModel(
77       mojom::HandwritingRecognizerSpecPtr spec,
78       mojo::PendingReceiver<mojom::HandwritingRecognizer> receiver,
79       mojom::MachineLearningService::LoadHandwritingModelCallback
80           result_callback) = 0;
81 
82   // Same as LoadHandwritingModel, but will be deprecated and removed soon.
83   virtual void LoadHandwritingModelWithSpec(
84       mojom::HandwritingRecognizerSpecPtr spec,
85       mojo::PendingReceiver<mojom::HandwritingRecognizer> receiver,
86       mojom::MachineLearningService::LoadHandwritingModelWithSpecCallback
87           result_callback) = 0;
88 
89   // Instruct ML daemon to load the Grammar model, binding a GrammarChecker
90   // implementation to |receiver|. Bootstraps the initial Mojo connection to the
91   // daemon if necessary.
92   virtual void LoadGrammarChecker(
93       mojo::PendingReceiver<mojom::GrammarChecker> receiver,
94       mojom::MachineLearningService::LoadGrammarCheckerCallback
95           result_callback) = 0;
96 
97  protected:
98   ServiceConnection() = default;
~ServiceConnection()99   virtual ~ServiceConnection() {}
100 };
101 
102 }  // namespace machine_learning
103 }  // namespace chromeos
104 
105 #endif  // CHROMEOS_SERVICES_MACHINE_LEARNING_PUBLIC_CPP_SERVICE_CONNECTION_H_
106