@@ -13,7 +13,12 @@ namespace chromeos {
13
13
namespace machine_learning {
14
14
namespace {
15
15
16
- using chromeos::machine_learning::mojom::LoadHandwritingModelResult;
16
+ using ::chromeos::machine_learning::mojom::HandwritingRecognizerSpecPtr;
17
+ using ::chromeos::machine_learning::mojom::LoadHandwritingModelResult;
18
+ using HandwritingRecognizer =
19
+ mojo::PendingReceiver<mojom::HandwritingRecognizer>;
20
+ using LoadHandwritingModelCallback = ::chromeos::machine_learning::mojom::
21
+ MachineLearningService::LoadHandwritingModelCallback;
17
22
18
23
// Records CrOSActionRecorder event.
19
24
void RecordLoadHandwritingModelResult (const LoadHandwritingModelResult val) {
@@ -22,6 +27,8 @@ void RecordLoadHandwritingModelResult(const LoadHandwritingModelResult val) {
22
27
LoadHandwritingModelResult::LOAD_MODEL_FILES_ERROR);
23
28
}
24
29
30
+ constexpr char kOndeviceHandwritingSwitch [] = " ondevice_handwriting" ;
31
+ constexpr char kLibHandwritingDlcId [] = " libhandwriting" ;
25
32
// A list of supported language code.
26
33
constexpr char kLanguageCodeEn [] = " en" ;
27
34
constexpr char kLanguageCodeGesture [] = " gesture_in_context" ;
@@ -30,10 +37,8 @@ constexpr char kLanguageCodeGesture[] = "gesture_in_context";
30
37
// kOndeviceHandwritingSwitch.
31
38
bool HandwritingSwitchHasValue (const std::string& value) {
32
39
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess ();
33
- return command_line->HasSwitch (
34
- HandwritingModelLoader::kOndeviceHandwritingSwitch ) &&
35
- command_line->GetSwitchValueASCII (
36
- HandwritingModelLoader::kOndeviceHandwritingSwitch ) == value;
40
+ return command_line->HasSwitch (kOndeviceHandwritingSwitch ) &&
41
+ command_line->GetSwitchValueASCII (kOndeviceHandwritingSwitch ) == value;
37
42
}
38
43
39
44
// Returns true if switch kOndeviceHandwritingSwitch is set to use_rootfs.
@@ -46,69 +51,45 @@ bool IsLibHandwritingDlcEnabled() {
46
51
return HandwritingSwitchHasValue (" use_dlc" );
47
52
}
48
53
49
- } // namespace
50
-
51
- constexpr char HandwritingModelLoader::kOndeviceHandwritingSwitch [];
52
- constexpr char HandwritingModelLoader::kLibHandwritingDlcId [];
53
-
54
- HandwritingModelLoader::HandwritingModelLoader (
55
- mojom::HandwritingRecognizerSpecPtr spec,
56
- mojo::PendingReceiver<mojom::HandwritingRecognizer> receiver,
57
- mojom::MachineLearningService::LoadHandwritingModelCallback callback)
58
- : dlc_client_(chromeos::DlcserviceClient::Get()),
59
- spec_ (std::move(spec)),
60
- receiver_(std::move(receiver)),
61
- callback_(std::move(callback)),
62
- weak_ptr_factory_(this ) {}
63
-
64
- HandwritingModelLoader::~HandwritingModelLoader () = default ;
65
-
66
- void HandwritingModelLoader::Load () {
67
- // Returns FEATURE_NOT_SUPPORTED_ERROR if both rootfs and dlc are not enabled.
68
- if (!IsLibHandwritingRootfsEnabled () && !IsLibHandwritingDlcEnabled ()) {
69
- RecordLoadHandwritingModelResult (
70
- LoadHandwritingModelResult::FEATURE_NOT_SUPPORTED_ERROR);
71
- std::move (callback_).Run (
72
- LoadHandwritingModelResult::FEATURE_NOT_SUPPORTED_ERROR);
73
- return ;
74
- }
75
-
76
- // Returns LANGUAGE_NOT_SUPPORTED_ERROR if the language is not supported yet.
77
- if (spec_->language != kLanguageCodeEn &&
78
- spec_->language != kLanguageCodeGesture ) {
79
- RecordLoadHandwritingModelResult (
80
- LoadHandwritingModelResult::LANGUAGE_NOT_SUPPORTED_ERROR);
81
- std::move (callback_).Run (
82
- LoadHandwritingModelResult::LANGUAGE_NOT_SUPPORTED_ERROR);
83
- return ;
84
- }
85
-
86
- // Load from rootfs if enabled.
87
- if (IsLibHandwritingRootfsEnabled ()) {
54
+ // Called when InstallDlc completes.
55
+ // Returns an error if the `result.error` is not dlcservice::kErrorNone.
56
+ // Calls mlservice to LoadHandwritingModel otherwise.
57
+ void OnInstallDlcComplete (
58
+ HandwritingRecognizerSpecPtr spec,
59
+ HandwritingRecognizer receiver,
60
+ LoadHandwritingModelCallback callback,
61
+ const chromeos::DlcserviceClient::InstallResult& result) {
62
+ // Call LoadHandwritingModelWithSpec if no error was found.
63
+ if (result.error == dlcservice::kErrorNone ) {
88
64
ServiceConnection::GetInstance ()->LoadHandwritingModel (
89
- std::move (spec_ ), std::move (receiver_ ), std::move (callback_ ));
65
+ std::move (spec ), std::move (receiver ), std::move (callback ));
90
66
return ;
91
67
}
92
68
93
- // Gets existing dlc list and based on the presence of libhandwriting
94
- // either returns an error or installs the libhandwriting dlc.
95
- dlc_client_->GetExistingDlcs (
96
- base::BindOnce (&HandwritingModelLoader::OnGetExistingDlcsComplete,
97
- weak_ptr_factory_.GetWeakPtr ()));
69
+ RecordLoadHandwritingModelResult (
70
+ LoadHandwritingModelResult::DLC_INSTALL_ERROR);
71
+ std::move (callback).Run (LoadHandwritingModelResult::DLC_INSTALL_ERROR);
98
72
}
99
73
100
- void HandwritingModelLoader::OnGetExistingDlcsComplete (
74
+ // Called when the existing-dlc-list is returned.
75
+ // Returns an error if libhandwriting is not in the existing-dlc-list.
76
+ // Calls InstallDlc otherwise.
77
+ void OnGetExistingDlcsComplete (
78
+ HandwritingRecognizerSpecPtr spec,
79
+ HandwritingRecognizer receiver,
80
+ LoadHandwritingModelCallback callback,
81
+ DlcserviceClient* const dlc_client,
101
82
const std::string& err,
102
83
const dlcservice::DlcsWithContent& dlcs_with_content) {
103
84
// Loop over dlcs_with_content, and installs libhandwriting if already exists.
104
85
// Since we don't want to trigger downloading here, we only install(mount)
105
86
// the handwriting dlc if it is already on device.
106
87
for (const auto & dlc_info : dlcs_with_content.dlc_infos ()) {
107
- if (dlc_info.id () == HandwritingModelLoader:: kLibHandwritingDlcId ) {
108
- dlc_client_ ->Install (
88
+ if (dlc_info.id () == kLibHandwritingDlcId ) {
89
+ dlc_client ->Install (
109
90
kLibHandwritingDlcId ,
110
- base::BindOnce (&HandwritingModelLoader::OnInstallDlcComplete ,
111
- weak_ptr_factory_. GetWeakPtr ( )),
91
+ base::BindOnce (&OnInstallDlcComplete, std::move (spec) ,
92
+ std::move (receiver), std::move (callback )),
112
93
chromeos::DlcserviceClient::IgnoreProgress);
113
94
return ;
114
95
}
@@ -117,21 +98,46 @@ void HandwritingModelLoader::OnGetExistingDlcsComplete(
117
98
// Returns error if the handwriting dlc is not on the device.
118
99
RecordLoadHandwritingModelResult (
119
100
LoadHandwritingModelResult::DLC_DOES_NOT_EXIST);
120
- std::move (callback_ ).Run (LoadHandwritingModelResult::DLC_DOES_NOT_EXIST);
101
+ std::move (callback ).Run (LoadHandwritingModelResult::DLC_DOES_NOT_EXIST);
121
102
}
122
103
123
- void HandwritingModelLoader::OnInstallDlcComplete (
124
- const chromeos::DlcserviceClient::InstallResult& result) {
125
- // Call LoadHandwritingModelWithSpec if no error was found.
126
- if (result.error == dlcservice::kErrorNone ) {
104
+ } // namespace
105
+
106
+ void LoadHandwritingModelFromRootfsOrDlc (HandwritingRecognizerSpecPtr spec,
107
+ HandwritingRecognizer receiver,
108
+ LoadHandwritingModelCallback callback,
109
+ DlcserviceClient* const dlc_client) {
110
+ // Returns FEATURE_NOT_SUPPORTED_ERROR if both rootfs and dlc are not enabled.
111
+ if (!IsLibHandwritingRootfsEnabled () && !IsLibHandwritingDlcEnabled ()) {
112
+ RecordLoadHandwritingModelResult (
113
+ LoadHandwritingModelResult::FEATURE_NOT_SUPPORTED_ERROR);
114
+ std::move (callback).Run (
115
+ LoadHandwritingModelResult::FEATURE_NOT_SUPPORTED_ERROR);
116
+ return ;
117
+ }
118
+
119
+ // Returns LANGUAGE_NOT_SUPPORTED_ERROR if the language is not supported yet.
120
+ if (spec->language != kLanguageCodeEn &&
121
+ spec->language != kLanguageCodeGesture ) {
122
+ RecordLoadHandwritingModelResult (
123
+ LoadHandwritingModelResult::LANGUAGE_NOT_SUPPORTED_ERROR);
124
+ std::move (callback).Run (
125
+ LoadHandwritingModelResult::LANGUAGE_NOT_SUPPORTED_ERROR);
126
+ return ;
127
+ }
128
+
129
+ // Load from rootfs if enabled.
130
+ if (IsLibHandwritingRootfsEnabled ()) {
127
131
ServiceConnection::GetInstance ()->LoadHandwritingModel (
128
- std::move (spec_ ), std::move (receiver_ ), std::move (callback_ ));
132
+ std::move (spec ), std::move (receiver ), std::move (callback ));
129
133
return ;
130
134
}
131
135
132
- RecordLoadHandwritingModelResult (
133
- LoadHandwritingModelResult::DLC_INSTALL_ERROR);
134
- std::move (callback_).Run (LoadHandwritingModelResult::DLC_INSTALL_ERROR);
136
+ // Gets existing dlc list and based on the presence of libhandwriting
137
+ // either returns an error or installs the libhandwriting dlc.
138
+ dlc_client->GetExistingDlcs (
139
+ base::BindOnce (&OnGetExistingDlcsComplete, std::move (spec),
140
+ std::move (receiver), std::move (callback), dlc_client));
135
141
}
136
142
137
143
} // namespace machine_learning
0 commit comments