1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "wakeup_manager_napi.h"
17
18 #include "intell_voice_common_napi.h"
19 #include "intell_voice_napi_util.h"
20 #include "intell_voice_napi_queue.h"
21 #include "intell_voice_update_callback_napi.h"
22
23 #include "intell_voice_log.h"
24
25 #define LOG_TAG "WakeupManagerNapi"
26
27 using namespace std;
28 using namespace OHOS::IntellVoice;
29
30 namespace OHOS {
31 namespace IntellVoiceNapi {
32 static constexpr int32_t UPLOAD_NUM_MAX = 100;
33
34 class WakeupSourceFilesContext : public AsyncContext {
35 public:
WakeupSourceFilesContext(napi_env env)36 explicit WakeupSourceFilesContext(napi_env env) : AsyncContext(env) {};
37 std::vector<WakeupSourceFile> cloneFile;
38 };
39
40 class UploadFilesContext : public AsyncContext {
41 public:
UploadFilesContext(napi_env env)42 explicit UploadFilesContext(napi_env env) : AsyncContext(env) {};
43 int numMax = 0;
44 std::vector<OHOS::IntellVoice::UploadFilesInfo> uploadFiles;
45 };
46
47 static __thread napi_ref g_wakeupManagerConstructor = nullptr;
48 int32_t WakeupManagerNapi::constructResult_ = NAPI_INTELLIGENT_VOICE_SUCCESS;
49
50 static const std::string WAKEUP_MANAGER_NAPI_CLASS_NAME = "WakeupManager";
51
WakeupManagerNapi()52 WakeupManagerNapi::WakeupManagerNapi()
53 {
54 INTELL_VOICE_LOG_INFO("enter, %{public}p", this);
55 }
56
~WakeupManagerNapi()57 WakeupManagerNapi::~WakeupManagerNapi()
58 {
59 INTELL_VOICE_LOG_INFO("enter");
60 if (wrapper_ != nullptr) {
61 napi_delete_reference(env_, wrapper_);
62 }
63 callbackNapi_ = nullptr;
64 }
65
Export(napi_env env,napi_value exports)66 napi_value WakeupManagerNapi::Export(napi_env env, napi_value exports)
67 {
68 napi_status status;
69 napi_value constructor;
70 napi_value result = nullptr;
71 const int32_t refCount = 1;
72 napi_get_undefined(env, &result);
73
74 napi_property_descriptor properties[] = {
75 DECLARE_NAPI_FUNCTION("setParameter", SetParameter),
76 DECLARE_NAPI_FUNCTION("getParameter", GetParameter),
77 DECLARE_NAPI_FUNCTION("getUploadFiles", GetUploadFiles),
78 DECLARE_NAPI_FUNCTION("getWakeupSourceFiles", GetWakeupSourceFiles),
79 DECLARE_NAPI_FUNCTION("enrollWithWakeupFilesForResult", EnrollWithWakeupFilesForResult),
80 DECLARE_NAPI_FUNCTION("clearUserData", ClearUserData),
81 };
82
83 napi_property_descriptor static_prop[] = {
84 DECLARE_NAPI_STATIC_FUNCTION(
85 "getWakeupManager", GetWakeupManager)
86 };
87
88 status = napi_define_class(env,
89 WAKEUP_MANAGER_NAPI_CLASS_NAME.c_str(),
90 NAPI_AUTO_LENGTH,
91 Construct,
92 nullptr,
93 sizeof(properties) / sizeof(properties[0]),
94 properties,
95 &constructor);
96 if (status != napi_ok) {
97 return result;
98 }
99
100 status = napi_create_reference(env, constructor, refCount, &g_wakeupManagerConstructor);
101 if (status == napi_ok) {
102 status = napi_set_named_property(env, exports, WAKEUP_MANAGER_NAPI_CLASS_NAME.c_str(), constructor);
103 if (status == napi_ok) {
104 status = napi_define_properties(env, exports,
105 sizeof(static_prop) / sizeof(static_prop[0]), static_prop);
106 if (status == napi_ok) {
107 return exports;
108 }
109 }
110 }
111
112 return result;
113 }
114
Destruct(napi_env env,void * nativeObject,void * finalizeHint)115 void WakeupManagerNapi::Destruct(napi_env env, void *nativeObject, void *finalizeHint)
116 {
117 if (nativeObject != nullptr) {
118 auto obj = static_cast<WakeupManagerNapi *>(nativeObject);
119 delete obj;
120 }
121 }
122
Construct(napi_env env,napi_callback_info info)123 napi_value WakeupManagerNapi::Construct(napi_env env, napi_callback_info info)
124 {
125 INTELL_VOICE_LOG_INFO("enter");
126 napi_status status;
127 size_t argc = 0;
128 napi_value jsThis = nullptr;
129 napi_value undefinedResult = nullptr;
130 napi_get_undefined(env, &undefinedResult);
131
132 status = napi_get_cb_info(env, info, &argc, nullptr, &jsThis, nullptr);
133 if (status != napi_ok) {
134 INTELL_VOICE_LOG_ERROR("invalid number of arguments");
135 return undefinedResult;
136 }
137
138 unique_ptr<WakeupManagerNapi> wakeupMangerNapi = make_unique<WakeupManagerNapi>();
139 if (wakeupMangerNapi == nullptr) {
140 INTELL_VOICE_LOG_ERROR("Failed to create WakeupManagerNapi, No memory");
141 return undefinedResult;
142 }
143
144 wakeupMangerNapi->env_ = env;
145 status = napi_wrap(env, jsThis, static_cast<void *>(wakeupMangerNapi.get()), WakeupManagerNapi::Destruct,
146 nullptr, &(wakeupMangerNapi->wrapper_));
147 if (status == napi_ok) {
148 wakeupMangerNapi.release();
149 return jsThis;
150 }
151
152 INTELL_VOICE_LOG_ERROR("wrap wakeup intell voice engine failed");
153 return undefinedResult;
154 }
155
GetWakeupManager(napi_env env,napi_callback_info info)156 napi_value WakeupManagerNapi::GetWakeupManager(napi_env env, napi_callback_info info)
157 {
158 INTELL_VOICE_LOG_INFO("enter");
159
160 napi_status status;
161 size_t argCount = 0;
162
163 status = napi_get_cb_info(env, info, &argCount, nullptr, nullptr, nullptr);
164 if (status != napi_ok || argCount != 0) {
165 INTELL_VOICE_LOG_ERROR("Invalid arguments");
166 IntellVoiceCommonNapi::ThrowError(env, NAPI_INTELLIGENT_VOICE_INVALID_PARAM);
167 return nullptr;
168 }
169
170 return WakeupManagerNapi::GetWakeupManagerWrapper(env);
171 }
172
GetWakeupManagerWrapper(napi_env env)173 napi_value WakeupManagerNapi::GetWakeupManagerWrapper(napi_env env)
174 {
175 napi_status status;
176 napi_value result = nullptr;
177 napi_value constructor;
178
179 status = napi_get_reference_value(env, g_wakeupManagerConstructor, &constructor);
180 if (status == napi_ok) {
181 status = napi_new_instance(env, constructor, 0, nullptr, &result);
182 if (status == napi_ok) {
183 return result;
184 }
185 }
186
187 INTELL_VOICE_LOG_ERROR("failed to get intell voice manager wrapper");
188 IntellVoiceCommonNapi::ThrowError(env, NAPI_INTELLIGENT_VOICE_NO_MEMORY);
189 napi_get_undefined(env, &result);
190
191 return result;
192 }
193
SetParameter(napi_env env,napi_callback_info info)194 napi_value WakeupManagerNapi::SetParameter(napi_env env, napi_callback_info info)
195 {
196 INTELL_VOICE_LOG_INFO("enter");
197 size_t cbIndex = ARG_INDEX_2;
198 class SetParameterContext : public AsyncContext {
199 public:
200 explicit SetParameterContext(napi_env env): AsyncContext(env) {};
201 string key;
202 string value;
203 };
204 shared_ptr<SetParameterContext> context = make_shared<SetParameterContext>(env);
205 if (context == nullptr) {
206 INTELL_VOICE_LOG_ERROR("create set parameter aync context failed, No memory");
207 return nullptr;
208 }
209
210 CbInfoParser parser = [env, context](size_t argc, napi_value *argv) -> bool {
211 CHECK_CONDITION_RETURN_FALSE((argc < ARGC_TWO), "argc less than 2");
212 napi_status status = GetValue(env, argv[0], context->key);
213 CHECK_CONDITION_RETURN_FALSE((status != napi_ok), "Failed to get key");
214 status = GetValue(env, argv[1], context->value);
215 CHECK_CONDITION_RETURN_FALSE((status != napi_ok), "Failed to get value");
216 return true;
217 };
218
219 context->result_ = (context->GetCbInfo(env, info, cbIndex, parser) ? NAPI_INTELLIGENT_VOICE_SUCCESS :
220 NAPI_INTELLIGENT_VOICE_INVALID_PARAM);
221
222 AsyncExecute execute;
223 if (context->result_ == NAPI_INTELLIGENT_VOICE_SUCCESS) {
224 execute = [](napi_env env, void *data) {
225 CHECK_CONDITION_RETURN_VOID((data == nullptr), "data is nullptr");
226 auto setParamContext = static_cast<SetParameterContext *>(data);
227 IntellVoiceManager *manager = IntellVoiceManager::GetInstance();
228 if (manager == nullptr) {
229 INTELL_VOICE_LOG_ERROR("manager is nullptr");
230 setParamContext->result_ = NAPI_INTELLIGENT_VOICE_SYSTEM_ERROR;
231 return;
232 }
233 auto ret = manager->SetParameter(setParamContext->key, setParamContext->value);
234 if (ret != 0) {
235 INTELL_VOICE_LOG_ERROR("set parameter failed, ret:%{public}d", ret);
236 setParamContext->result_ = NAPI_INTELLIGENT_VOICE_SYSTEM_ERROR;
237 }
238 };
239 } else {
240 execute = [](napi_env env, void *data) {};
241 }
242
243 return NapiAsync::AsyncWork(env, context, "SetParameter", execute);
244 }
245
GetParameter(napi_env env,napi_callback_info info)246 napi_value WakeupManagerNapi::GetParameter(napi_env env, napi_callback_info info)
247 {
248 INTELL_VOICE_LOG_INFO("enter");
249 size_t cbIndex = ARG_INDEX_1;
250 class GetParamContext : public AsyncContext {
251 public:
252 explicit GetParamContext(napi_env env) : AsyncContext(env) {};
253 std::string key;
254 std::string val;
255 };
256 auto context = make_shared<GetParamContext>(env);
257 if (context == nullptr) {
258 INTELL_VOICE_LOG_ERROR("create AsyncContext failed, No memory");
259 return nullptr;
260 }
261
262 CbInfoParser parser = [env, context](size_t argc, napi_value *argv) -> bool {
263 CHECK_CONDITION_RETURN_FALSE((argc < ARGC_ONE), "argc less than 1");
264 napi_status status = GetValue(env, argv[0], context->key);
265 CHECK_CONDITION_RETURN_FALSE((status != napi_ok), "Failed to get key");
266 return true;
267 };
268
269 context->result_ = (context->GetCbInfo(env, info, cbIndex, parser) ? NAPI_INTELLIGENT_VOICE_SUCCESS :
270 NAPI_INTELLIGENT_VOICE_INVALID_PARAM);
271
272 AsyncExecute execute;
273 if (context->result_ == NAPI_INTELLIGENT_VOICE_SUCCESS) {
274 execute = [](napi_env env, void *data) {
275 CHECK_CONDITION_RETURN_VOID((data == nullptr), "data is nullptr");
276 auto asyncContext = static_cast<GetParamContext *>(data);
277 IntellVoiceManager *manager = IntellVoiceManager::GetInstance();
278 if (manager == nullptr) {
279 INTELL_VOICE_LOG_ERROR("manager is nullptr");
280 asyncContext->result_ = NAPI_INTELLIGENT_VOICE_SYSTEM_ERROR;
281 return;
282 }
283 asyncContext->val = manager->GetParameter(asyncContext->key);
284 };
285
286 context->complete_ = [](napi_env env, AsyncContext *asyncContext, napi_value &result) {
287 auto getParamAsynContext = static_cast<GetParamContext *>(asyncContext);
288 result = SetValue(env, getParamAsynContext->val);
289 };
290 } else {
291 execute = [](napi_env env, void *data) {};
292 }
293
294 return NapiAsync::AsyncWork(env, context, "GetParameter", execute);
295 }
296
GetUploadFiles(napi_env env,napi_callback_info info)297 napi_value WakeupManagerNapi::GetUploadFiles(napi_env env, napi_callback_info info)
298 {
299 INTELL_VOICE_LOG_ERROR("enter");
300 auto context = std::make_shared<UploadFilesContext>(env);
301 CHECK_CONDITION_RETURN_RET(context == nullptr, nullptr, "create upload files context failed");
302
303 CbInfoParser parser = [env, context](size_t argc, napi_value *argv) {
304 napi_status status = GetValue(env, argv[ARG_INDEX_0], context->numMax);
305 CHECK_CONDITION_RETURN_FALSE((status != napi_ok), "Failed to get numMax");
306 if ((context->numMax <= 0) || (context->numMax > UPLOAD_NUM_MAX)) {
307 INTELL_VOICE_LOG_ERROR("numMax:%{public}d is invalid", context->numMax);
308 return false;
309 }
310 return true;
311 };
312 context->result_ = (context->GetCbInfo(env, info, ARG_INDEX_1, parser) ? NAPI_INTELLIGENT_VOICE_SUCCESS :
313 NAPI_INTELLIGENT_VOICE_INVALID_PARAM);
314
315 AsyncExecute execute;
316 if (context->result_ == NAPI_INTELLIGENT_VOICE_SUCCESS) {
317 execute = [](napi_env env, void *data) {
318 auto *context = static_cast<UploadFilesContext *>(data);
319 IntellVoiceManager *manager = IntellVoiceManager::GetInstance();
320 if (manager == nullptr) {
321 context->result_ = NAPI_INTELLIGENT_VOICE_NO_MEMORY;
322 return;
323 }
324
325 std::vector<UploadFilesInfo>().swap(context->uploadFiles);
326 if (manager->GetUploadFiles(context->numMax, context->uploadFiles) != 0) {
327 context->result_ = NAPI_INTELLIGENT_VOICE_SYSTEM_ERROR;
328 }
329 };
330 } else {
331 execute = [](napi_env env, void *data) {};
332 }
333
334 context->complete_ = [](napi_env env, AsyncContext *asyncContext, napi_value &result) {
335 GetUploadFilesComplete(env, asyncContext, result);
336 };
337 return NapiAsync::AsyncWork(env, context, "GetUploadFiles", execute);
338 }
339
GetUploadFilesComplete(napi_env env,AsyncContext * data,napi_value & result)340 void WakeupManagerNapi::GetUploadFilesComplete(napi_env env, AsyncContext *data, napi_value &result)
341 {
342 INTELL_VOICE_LOG_INFO("enter");
343 auto *context = reinterpret_cast<UploadFilesContext *>(data);
344 napi_get_undefined(env, &result);
345 std::vector<UploadFilesInfo> &uploadFiles = context->uploadFiles;
346
347 if (uploadFiles.size() == 0) {
348 context->result_ = NAPI_INTELLIGENT_VOICE_SYSTEM_ERROR;
349 goto EXIT;
350 }
351
352 if (napi_create_array(env, &result) != napi_ok) {
353 context->result_ = NAPI_INTELLIGENT_VOICE_NO_MEMORY;
354 goto EXIT;
355 }
356
357 for (uint32_t index = 0; index < uploadFiles.size(); index++) {
358 napi_value obj;
359 if (napi_create_object(env, &obj) != napi_ok) {
360 goto EXIT;
361 }
362
363 napi_set_named_property(env, obj, "type", SetValue(env, uploadFiles[index].type));
364 napi_set_named_property(env, obj, "filesDescription", SetValue(env, uploadFiles[index].filesDescription));
365 napi_value arrayContents = nullptr;
366 if (napi_create_array(env, &arrayContents) != napi_ok) {
367 context->result_ = NAPI_INTELLIGENT_VOICE_NO_MEMORY;
368 goto EXIT;
369 }
370
371 for (uint32_t j = 0; j < uploadFiles[index].filesContent.size(); j++) {
372 napi_value content = SetValue(env, uploadFiles[index].filesContent[j]);
373 if (content == nullptr) {
374 context->result_ = NAPI_INTELLIGENT_VOICE_NO_MEMORY;
375 goto EXIT;
376 }
377 napi_set_element(env, arrayContents, j, content);
378 }
379 napi_set_named_property(env, obj, "filesContent", arrayContents);
380 napi_set_element(env, result, index, obj);
381 }
382
383 EXIT:
384 std::vector<UploadFilesInfo>().swap(uploadFiles);
385 }
386
GetWakeupSourceFiles(napi_env env,napi_callback_info info)387 napi_value WakeupManagerNapi::GetWakeupSourceFiles(napi_env env, napi_callback_info info)
388 {
389 INTELL_VOICE_LOG_INFO("enter");
390 size_t cbIndex = ARG_INDEX_0;
391
392 auto context = std::make_shared<WakeupSourceFilesContext>(env);
393 CHECK_CONDITION_RETURN_RET(context == nullptr, nullptr, "create clone file context failed");
394
395 context->result_ = (context->GetCbInfo(env, info, cbIndex, nullptr) ? NAPI_INTELLIGENT_VOICE_SUCCESS :
396 NAPI_INTELLIGENT_VOICE_INVALID_PARAM);
397
398 AsyncExecute execute;
399 if (context->result_ == NAPI_INTELLIGENT_VOICE_SUCCESS) {
400 execute = [](napi_env env, void *data) {
401 auto *context = static_cast<WakeupSourceFilesContext *>(data);
402 IntellVoiceManager *manager = IntellVoiceManager::GetInstance();
403 if (manager == nullptr) {
404 context->result_ = NAPI_INTELLIGENT_VOICE_NO_MEMORY;
405 return;
406 }
407
408 int ret = manager->GetWakeupSourceFiles(context->cloneFile);
409 if (ret != 0) {
410 INTELL_VOICE_LOG_ERROR("get clone files error, ret:%{public}d", ret);
411 context->result_ = NAPI_INTELLIGENT_VOICE_SYSTEM_ERROR;
412 }
413 };
414 } else {
415 execute = [](napi_env env, void *data) {};
416 }
417
418 context->complete_ = [](napi_env env, AsyncContext *asyncContext, napi_value &result) {
419 GetCloneCompleteCallback(env, asyncContext, result);
420 };
421 return NapiAsync::AsyncWork(env, context, "GetWakeupSourceFiles", execute);
422 }
423
GetCloneCompleteCallback(napi_env env,AsyncContext * data,napi_value & result)424 void WakeupManagerNapi::GetCloneCompleteCallback(napi_env env, AsyncContext *data, napi_value &result)
425 {
426 INTELL_VOICE_LOG_INFO("enter");
427 CHECK_CONDITION_RETURN_VOID(data == nullptr, "get clone file context null");
428 auto *context = reinterpret_cast<WakeupSourceFilesContext *>(data);
429 napi_get_undefined(env, &result);
430
431 std::vector<WakeupSourceFile> &files = context->cloneFile;
432 if (files.size() == 0) {
433 context->result_ = NAPI_INTELLIGENT_VOICE_SYSTEM_ERROR;
434 goto EXIT;
435 }
436
437 if (napi_create_array(env, &result) != napi_ok) {
438 context->result_ = NAPI_INTELLIGENT_VOICE_NO_MEMORY;
439 goto EXIT;
440 }
441
442 for (uint32_t filesIndex = 0; filesIndex < files.size(); filesIndex++) {
443 napi_value obj;
444 if (napi_create_object(env, &obj) != napi_ok) {
445 context->result_ = NAPI_INTELLIGENT_VOICE_NO_MEMORY;
446 goto EXIT;
447 }
448 napi_set_named_property(env, obj, "filePath", SetValue(env, files[filesIndex].filePath));
449 napi_set_named_property(env, obj, "fileContent", SetValue(env, files[filesIndex].fileContent));
450 napi_set_element(env, result, filesIndex, obj);
451 }
452
453 EXIT:
454 vector<WakeupSourceFile>().swap(files);
455 }
456
CloneForResultCompleteCallback(napi_env env,napi_status status,void * data)457 void WakeupManagerNapi::CloneForResultCompleteCallback(napi_env env, napi_status status, void *data)
458 {
459 INTELL_VOICE_LOG_INFO("enter");
460 CHECK_CONDITION_RETURN_VOID((data == nullptr), "async complete callback data is nullptr");
461 napi_value result = nullptr;
462
463 auto asyncContext = static_cast<UpdateAsyncContext *>(data);
464 if (asyncContext->result_ != NAPI_INTELLIGENT_VOICE_SUCCESS) {
465 napi_get_undefined(env, &result);
466 NapiAsync::CommonCallbackRoutine(env, asyncContext, result);
467 return;
468 }
469
470 if (asyncContext->processWork == nullptr) {
471 INTELL_VOICE_LOG_ERROR("process work is nullptr");
472 napi_get_undefined(env, &result);
473 asyncContext->result_ = NAPI_INTELLIGENT_VOICE_INVALID_PARAM;
474 NapiAsync::CommonCallbackRoutine(env, asyncContext, result);
475 return;
476 }
477
478 auto cb = reinterpret_cast<WakeupManagerNapi *>(asyncContext->instanceNapi_)->callbackNapi_;
479 if (cb == nullptr) {
480 INTELL_VOICE_LOG_ERROR("get callback napi failed");
481 napi_get_undefined(env, &result);
482 asyncContext->result_ = NAPI_INTELLIGENT_VOICE_INVALID_PARAM;
483 NapiAsync::CommonCallbackRoutine(env, asyncContext, result);
484 return;
485 }
486
487 cb->QueueAsyncWork(asyncContext);
488 if (asyncContext->processWork(asyncContext) != 0) {
489 INTELL_VOICE_LOG_ERROR("clone process work failed");
490 cb->ClearAsyncWork(true, "the request was aborted because intelligent voice processWork error");
491 }
492 }
493
WakeupFilesForResultParser(std::shared_ptr<UpdateAsyncContext> context,napi_env env,size_t argc,napi_value * argv)494 bool WakeupManagerNapi::WakeupFilesForResultParser(std::shared_ptr<UpdateAsyncContext> context,
495 napi_env env, size_t argc, napi_value *argv)
496 {
497 CHECK_CONDITION_RETURN_FALSE((argc < ARGC_TWO), "argc less than 2");
498
499 bool isArray = false;
500 napi_status status = napi_is_array(env, argv[0], &isArray);
501 CHECK_CONDITION_RETURN_FALSE((status != napi_ok), "get property failed");
502 CHECK_CONDITION_RETURN_FALSE((!isArray), "argv 0 is not array");
503
504 uint32_t arrayLength = 0;
505 status = napi_get_array_length(env, argv[0], &arrayLength);
506 CHECK_CONDITION_RETURN_FALSE((status != napi_ok), "get array length failed");
507 CHECK_CONDITION_RETURN_FALSE((arrayLength == 0), "array length is 0");
508
509 napi_value value = nullptr;
510 napi_value tempResult = nullptr;
511
512 context->cloneFiles.clear();
513 context->cloneFiles.reserve(arrayLength);
514 for (size_t index = 0; index < arrayLength; index++) {
515 status = napi_get_element(env, argv[0], index, &value);
516 CHECK_CONDITION_RETURN_FALSE((status != napi_ok), "get array element failed");
517 napi_valuetype valueType = napi_undefined;
518 napi_typeof(env, value, &valueType);
519 CHECK_CONDITION_RETURN_FALSE((valueType != napi_object), "get array element object mismatch");
520
521 WakeupSourceFile fileInfo;
522 status = napi_get_named_property(env, value, "filePath", &tempResult);
523 CHECK_CONDITION_RETURN_FALSE((status != napi_ok), "get property failed");
524 status = GetValue(env, tempResult, fileInfo.filePath);
525 CHECK_CONDITION_RETURN_FALSE((status != napi_ok), "get filePath failed");
526
527 status = napi_get_named_property(env, value, "fileContent", &tempResult);
528 CHECK_CONDITION_RETURN_FALSE((status != napi_ok), "get property failed");
529 status = GetValue(env, tempResult, fileInfo.fileContent);
530 CHECK_CONDITION_RETURN_FALSE((status != napi_ok), "get fileContent failed");
531 context->cloneFiles.push_back(fileInfo);
532 }
533
534 status = GetValue(env, argv[1], context->wakeupInfo);
535 CHECK_CONDITION_RETURN_FALSE((status != napi_ok), "get clone info failed");
536 return true;
537 }
538
EnrollWithWakeupFilesForResult(napi_env env,napi_callback_info info)539 napi_value WakeupManagerNapi::EnrollWithWakeupFilesForResult(napi_env env, napi_callback_info info)
540 {
541 INTELL_VOICE_LOG_INFO("enter");
542 size_t cbIndex = ARG_INDEX_2;
543 auto context = make_shared<UpdateAsyncContext>(env);
544 CHECK_CONDITION_RETURN_RET(context == nullptr, nullptr, "create context failed, no memory");
545 CbInfoParser parser = [env, context](size_t argc, napi_value *argv) -> bool {
546 return WakeupFilesForResultParser(context, env, argc, argv);
547 };
548
549 context->result_ = (context->GetCbInfo(env, info, cbIndex, parser) ? NAPI_INTELLIGENT_VOICE_SUCCESS :
550 NAPI_INTELLIGENT_VOICE_INVALID_PARAM);
551 if (context->result_ == NAPI_INTELLIGENT_VOICE_SUCCESS) {
552 reinterpret_cast<WakeupManagerNapi *>(context->instanceNapi_)->callbackNapi_ =
553 std::make_shared<IntellVoiceUpdateCallbackNapi>(env);
554 }
555
556 context->result = EnrollResult::UNKNOWN_ERROR;
557 context->processWork = [&](AsyncContext *asyncContext) -> int32_t {
558 auto context = reinterpret_cast<UpdateAsyncContext *>(asyncContext);
559 auto wakeupManagerNapi = reinterpret_cast<WakeupManagerNapi *>(context->instanceNapi_);
560 auto manager = IntellVoiceManager::GetInstance();
561 if (wakeupManagerNapi == nullptr) {
562 INTELL_VOICE_LOG_ERROR("get manager instance failed");
563 goto PROCESS_ERR_EXIT;
564 }
565 if (manager == nullptr) {
566 INTELL_VOICE_LOG_ERROR("manager null");
567 goto PROCESS_ERR_EXIT;
568 }
569
570 if (wakeupManagerNapi->callbackNapi_ == nullptr) {
571 INTELL_VOICE_LOG_ERROR("callback napi null");
572 goto PROCESS_ERR_EXIT;
573 }
574
575 if (manager->EnrollWithWakeupFilesForResult(context->cloneFiles, context->wakeupInfo,
576 wakeupManagerNapi->callbackNapi_) != 0) {
577 INTELL_VOICE_LOG_ERROR("clone for result failed");
578 wakeupManagerNapi->callbackNapi_ = nullptr;
579 goto PROCESS_ERR_EXIT;
580 }
581
582 context->cloneFiles.clear();
583 return 0;
584
585 PROCESS_ERR_EXIT:
586 context->cloneFiles.clear();
587 return -1;
588 };
589
590 AsyncExecute execute = [](napi_env env, void *data) {};
591
592 return NapiAsync::AsyncWork(env, context, "EnrollWithWakeupFilesForResult", execute,
593 CloneForResultCompleteCallback);
594 }
595
ClearUserData(napi_env env,napi_callback_info info)596 napi_value WakeupManagerNapi::ClearUserData(napi_env env, napi_callback_info info)
597 {
598 INTELL_VOICE_LOG_INFO("enter");
599 size_t cbIndex = ARG_INDEX_0;
600 auto context = std::make_shared<AsyncContext>(env);
601 if (context == nullptr) {
602 INTELL_VOICE_LOG_ERROR("create AsyncContext failed, No memory");
603 return nullptr;
604 }
605 context->result_ = (context->GetCbInfo(env, info, cbIndex, nullptr) ? NAPI_INTELLIGENT_VOICE_SUCCESS :
606 NAPI_INTELLIGENT_VOICE_INVALID_PARAM);
607 AsyncExecute execute;
608 if (context->result_ == NAPI_INTELLIGENT_VOICE_SUCCESS) {
609 execute = [](napi_env env, void *data) {
610 CHECK_CONDITION_RETURN_VOID((data == nullptr), "data is nullptr");
611 auto asyncContext = static_cast<AsyncContext *>(data);
612 IntellVoiceManager *manager = IntellVoiceManager::GetInstance();
613 if (manager == nullptr) {
614 INTELL_VOICE_LOG_ERROR("manager is nullptr");
615 asyncContext->result_ = NAPI_INTELLIGENT_VOICE_SYSTEM_ERROR;
616 return;
617 }
618 manager->ClearUserData();
619 };
620 } else {
621 execute = [](napi_env env, void *data) {};
622 }
623
624 return NapiAsync::AsyncWork(env, context, "ClearUserData", execute);
625 }
626 } // namespace IntellVoiceNapi
627 } // namespace OHOS
628