• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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");
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     if (!IntellVoiceCommonNapi::CheckIsSystemApp()) {
161         INTELL_VOICE_LOG_ERROR("Not system application!");
162         IntellVoiceCommonNapi::ThrowError(env, NAPI_INTELLIGENT_VOICE_NOT_SYSTEM_APPLICATION);
163         return nullptr;
164     }
165 
166     napi_status status;
167     size_t argCount = 0;
168 
169     status = napi_get_cb_info(env, info, &argCount, nullptr, nullptr, nullptr);
170     if (status != napi_ok || argCount != 0) {
171         INTELL_VOICE_LOG_ERROR("Invalid arguments");
172         IntellVoiceCommonNapi::ThrowError(env, NAPI_INTELLIGENT_VOICE_INVALID_PARAM);
173         return nullptr;
174     }
175 
176     return WakeupManagerNapi::GetWakeupManagerWrapper(env);
177 }
178 
GetWakeupManagerWrapper(napi_env env)179 napi_value WakeupManagerNapi::GetWakeupManagerWrapper(napi_env env)
180 {
181     napi_status status;
182     napi_value result = nullptr;
183     napi_value constructor;
184 
185     status = napi_get_reference_value(env, g_wakeupManagerConstructor, &constructor);
186     if (status == napi_ok) {
187         status = napi_new_instance(env, constructor, 0, nullptr, &result);
188         if (status == napi_ok) {
189             return result;
190         }
191     }
192 
193     INTELL_VOICE_LOG_ERROR("failed to get intell voice manager wrapper");
194     IntellVoiceCommonNapi::ThrowError(env, NAPI_INTELLIGENT_VOICE_NO_MEMORY);
195     napi_get_undefined(env, &result);
196 
197     return result;
198 }
199 
SetParameter(napi_env env,napi_callback_info info)200 napi_value WakeupManagerNapi::SetParameter(napi_env env, napi_callback_info info)
201 {
202     INTELL_VOICE_LOG_INFO("enter");
203     size_t cbIndex = ARG_INDEX_2;
204     class SetParameterContext : public AsyncContext {
205     public:
206         explicit SetParameterContext(napi_env env): AsyncContext(env) {};
207         string key;
208         string value;
209     };
210     shared_ptr<SetParameterContext> context = make_shared<SetParameterContext>(env);
211     if (context == nullptr) {
212         INTELL_VOICE_LOG_ERROR("create set parameter aync context failed, No memory");
213         return nullptr;
214     }
215 
216     CbInfoParser parser = [env, context](size_t argc, napi_value *argv) -> bool {
217         CHECK_CONDITION_RETURN_FALSE((argc < ARGC_TWO), "argc less than 2");
218         napi_status status = GetValue(env, argv[0], context->key);
219         CHECK_CONDITION_RETURN_FALSE((status != napi_ok), "Failed to get key");
220         status = GetValue(env, argv[1], context->value);
221         CHECK_CONDITION_RETURN_FALSE((status != napi_ok), "Failed to get value");
222         return true;
223     };
224 
225     context->result_ = (context->GetCbInfo(env, info, cbIndex, parser) ? NAPI_INTELLIGENT_VOICE_SUCCESS :
226         NAPI_INTELLIGENT_VOICE_INVALID_PARAM);
227 
228     AsyncExecute execute;
229     if (context->result_ == NAPI_INTELLIGENT_VOICE_SUCCESS) {
230         execute = [](napi_env env, void *data) {
231             CHECK_CONDITION_RETURN_VOID((data == nullptr), "data is nullptr");
232             auto setParamContext = reinterpret_cast<SetParameterContext *>(data);
233             IntellVoiceManager *manager = IntellVoiceManager::GetInstance();
234             if (manager == nullptr) {
235                 INTELL_VOICE_LOG_ERROR("manager is nullptr");
236                 setParamContext->result_ = NAPI_INTELLIGENT_VOICE_SYSTEM_ERROR;
237                 return;
238             }
239             setParamContext->result_ = IntellVoiceCommonNapi::ConvertResultCode(
240                 manager->SetParameter(setParamContext->key, setParamContext->value));
241             if (setParamContext->result_ != NAPI_INTELLIGENT_VOICE_SUCCESS) {
242                 INTELL_VOICE_LOG_ERROR("set parameter failed, ret:%{public}d", setParamContext->result_);
243             }
244         };
245     } else {
246         execute = [](napi_env env, void *data) {};
247     }
248 
249     return NapiAsync::AsyncWork(env, context, "SetParameter", execute);
250 }
251 
GetParameter(napi_env env,napi_callback_info info)252 napi_value WakeupManagerNapi::GetParameter(napi_env env, napi_callback_info info)
253 {
254     INTELL_VOICE_LOG_INFO("enter");
255     size_t cbIndex = ARG_INDEX_1;
256     class GetParamContext : public AsyncContext {
257     public:
258         explicit GetParamContext(napi_env env) : AsyncContext(env) {};
259         std::string key;
260         std::string val;
261     };
262     auto context = make_shared<GetParamContext>(env);
263     if (context == nullptr) {
264         INTELL_VOICE_LOG_ERROR("create AsyncContext failed, No memory");
265         return nullptr;
266     }
267 
268     CbInfoParser parser = [env, context](size_t argc, napi_value *argv) -> bool {
269         CHECK_CONDITION_RETURN_FALSE((argc < ARGC_ONE), "argc less than 1");
270         napi_status status = GetValue(env, argv[0], context->key);
271         CHECK_CONDITION_RETURN_FALSE((status != napi_ok), "Failed to get key");
272         return true;
273     };
274 
275     context->result_ = (context->GetCbInfo(env, info, cbIndex, parser) ? NAPI_INTELLIGENT_VOICE_SUCCESS :
276         NAPI_INTELLIGENT_VOICE_INVALID_PARAM);
277 
278     AsyncExecute execute;
279     if (context->result_ == NAPI_INTELLIGENT_VOICE_SUCCESS) {
280         execute = [](napi_env env, void *data) {
281             CHECK_CONDITION_RETURN_VOID((data == nullptr), "data is nullptr");
282             auto asyncContext = reinterpret_cast<GetParamContext *>(data);
283             IntellVoiceManager *manager = IntellVoiceManager::GetInstance();
284             if (manager == nullptr) {
285                 INTELL_VOICE_LOG_ERROR("manager is nullptr");
286                 asyncContext->result_ = NAPI_INTELLIGENT_VOICE_SYSTEM_ERROR;
287                 return;
288             }
289             asyncContext->val = manager->GetParameter(asyncContext->key);
290         };
291 
292         context->complete_ = [](napi_env env, AsyncContext *asyncContext, napi_value &result) {
293             auto getParamAsynContext = reinterpret_cast<GetParamContext *>(asyncContext);
294             if (getParamAsynContext != nullptr) {
295                 result = SetValue(env, getParamAsynContext->val);
296             }
297         };
298     } else {
299         execute = [](napi_env env, void *data) {};
300     }
301 
302     return NapiAsync::AsyncWork(env, context, "GetParameter", execute);
303 }
304 
GetUploadFiles(napi_env env,napi_callback_info info)305 napi_value WakeupManagerNapi::GetUploadFiles(napi_env env, napi_callback_info info)
306 {
307     INTELL_VOICE_LOG_INFO("enter");
308     auto context = std::make_shared<UploadFilesContext>(env);
309     CHECK_CONDITION_RETURN_RET(context == nullptr, nullptr, "create upload files context failed");
310 
311     CbInfoParser parser = [env, context](size_t argc, napi_value *argv) {
312         napi_status status = GetValue(env, argv[ARG_INDEX_0], context->numMax);
313         CHECK_CONDITION_RETURN_FALSE((status != napi_ok), "Failed to get numMax");
314         if ((context->numMax <= 0) || (context->numMax > UPLOAD_NUM_MAX)) {
315             INTELL_VOICE_LOG_ERROR("numMax:%{public}d is invalid", context->numMax);
316             return false;
317         }
318         return true;
319     };
320     context->result_ = (context->GetCbInfo(env, info, ARG_INDEX_1, parser) ? NAPI_INTELLIGENT_VOICE_SUCCESS :
321         NAPI_INTELLIGENT_VOICE_INVALID_PARAM);
322 
323     AsyncExecute execute;
324     if (context->result_ == NAPI_INTELLIGENT_VOICE_SUCCESS) {
325         execute = [](napi_env env, void *data) {
326             auto *context = static_cast<UploadFilesContext *>(data);
327             IntellVoiceManager *manager = IntellVoiceManager::GetInstance();
328             if (manager == nullptr) {
329                 context->result_ = NAPI_INTELLIGENT_VOICE_NO_MEMORY;
330                 return;
331             }
332 
333             std::vector<UploadFilesInfo>().swap(context->uploadFiles);
334             context->result_ = IntellVoiceCommonNapi::ConvertResultCode(
335                 manager->GetUploadFiles(context->numMax, context->uploadFiles));
336         };
337     } else {
338         execute = [](napi_env env, void *data) {};
339     }
340 
341     context->complete_ = [](napi_env env, AsyncContext *asyncContext, napi_value &result) {
342         GetUploadFilesComplete(env, asyncContext, result);
343     };
344     return NapiAsync::AsyncWork(env, context, "GetUploadFiles", execute);
345 }
346 
GetUploadFilesComplete(napi_env env,AsyncContext * data,napi_value & result)347 void WakeupManagerNapi::GetUploadFilesComplete(napi_env env, AsyncContext *data, napi_value &result)
348 {
349     INTELL_VOICE_LOG_INFO("enter");
350     auto *context = reinterpret_cast<UploadFilesContext *>(data);
351     napi_get_undefined(env, &result);
352     std::vector<UploadFilesInfo> &uploadFiles = context->uploadFiles;
353 
354     if (uploadFiles.size() == 0) {
355         context->result_ = NAPI_INTELLIGENT_VOICE_SYSTEM_ERROR;
356         goto EXIT;
357     }
358 
359     if (napi_create_array(env, &result) != napi_ok) {
360         context->result_ = NAPI_INTELLIGENT_VOICE_NO_MEMORY;
361         goto EXIT;
362     }
363 
364     for (uint32_t index = 0; index < uploadFiles.size(); index++) {
365         napi_value obj;
366         if (napi_create_object(env, &obj) != napi_ok) {
367             goto EXIT;
368         }
369 
370         napi_set_named_property(env, obj, "type", SetValue(env, uploadFiles[index].type));
371         napi_set_named_property(env, obj, "filesDescription", SetValue(env, uploadFiles[index].filesDescription));
372         napi_value arrayContents = nullptr;
373         if (napi_create_array(env, &arrayContents) != napi_ok) {
374             context->result_ = NAPI_INTELLIGENT_VOICE_NO_MEMORY;
375             goto EXIT;
376         }
377 
378         for (uint32_t j = 0; j < uploadFiles[index].filesContent.size(); j++) {
379             napi_value content = SetValue(env, uploadFiles[index].filesContent[j]);
380             if (content == nullptr) {
381                 context->result_ = NAPI_INTELLIGENT_VOICE_NO_MEMORY;
382                 goto EXIT;
383             }
384             napi_set_element(env, arrayContents, j, content);
385         }
386         napi_set_named_property(env, obj, "filesContent", arrayContents);
387         napi_set_element(env, result, index, obj);
388     }
389 
390 EXIT:
391     std::vector<UploadFilesInfo>().swap(uploadFiles);
392 }
393 
GetWakeupSourceFiles(napi_env env,napi_callback_info info)394 napi_value WakeupManagerNapi::GetWakeupSourceFiles(napi_env env, napi_callback_info info)
395 {
396     INTELL_VOICE_LOG_INFO("enter");
397     size_t cbIndex = ARG_INDEX_0;
398 
399     auto context = std::make_shared<WakeupSourceFilesContext>(env);
400     CHECK_CONDITION_RETURN_RET(context == nullptr, nullptr, "create clone file context failed");
401 
402     context->result_ = (context->GetCbInfo(env, info, cbIndex, nullptr) ? NAPI_INTELLIGENT_VOICE_SUCCESS :
403         NAPI_INTELLIGENT_VOICE_INVALID_PARAM);
404 
405     AsyncExecute execute;
406     if (context->result_ == NAPI_INTELLIGENT_VOICE_SUCCESS) {
407         execute = [](napi_env env, void *data) {
408             auto *context = static_cast<WakeupSourceFilesContext *>(data);
409             IntellVoiceManager *manager = IntellVoiceManager::GetInstance();
410             if (manager == nullptr) {
411                 context->result_ = NAPI_INTELLIGENT_VOICE_NO_MEMORY;
412                 return;
413             }
414 
415             context->result_ = IntellVoiceCommonNapi::ConvertResultCode(
416                 manager->GetWakeupSourceFiles(context->cloneFile));
417             if (context->result_ != 0) {
418                 INTELL_VOICE_LOG_ERROR("get clone files error, ret:%{public}d", context->result_);
419             }
420         };
421     } else {
422         execute = [](napi_env env, void *data) {};
423     }
424 
425     context->complete_ = [](napi_env env, AsyncContext *asyncContext, napi_value &result) {
426         GetCloneCompleteCallback(env, asyncContext, result);
427     };
428     return NapiAsync::AsyncWork(env, context, "GetWakeupSourceFiles", execute);
429 }
430 
GetCloneCompleteCallback(napi_env env,AsyncContext * data,napi_value & result)431 void WakeupManagerNapi::GetCloneCompleteCallback(napi_env env, AsyncContext *data, napi_value &result)
432 {
433     INTELL_VOICE_LOG_INFO("enter");
434     CHECK_CONDITION_RETURN_VOID(data == nullptr, "get clone file context null");
435     auto *context = reinterpret_cast<WakeupSourceFilesContext *>(data);
436     napi_get_undefined(env, &result);
437 
438     std::vector<WakeupSourceFile> &files = context->cloneFile;
439     if (files.size() == 0) {
440         context->result_ = NAPI_INTELLIGENT_VOICE_SYSTEM_ERROR;
441         goto EXIT;
442     }
443 
444     if (napi_create_array(env, &result) != napi_ok) {
445         context->result_ = NAPI_INTELLIGENT_VOICE_NO_MEMORY;
446         goto EXIT;
447     }
448 
449     for (uint32_t filesIndex = 0; filesIndex < files.size(); filesIndex++) {
450         napi_value obj;
451         if (napi_create_object(env, &obj) != napi_ok) {
452             context->result_ = NAPI_INTELLIGENT_VOICE_NO_MEMORY;
453             goto EXIT;
454         }
455         napi_set_named_property(env, obj, "filePath", SetValue(env, files[filesIndex].filePath));
456         napi_set_named_property(env, obj, "fileContent", SetValue(env, files[filesIndex].fileContent));
457         napi_set_element(env, result, filesIndex, obj);
458     }
459 
460 EXIT:
461     vector<WakeupSourceFile>().swap(files);
462 }
463 
CloneForResultCompleteCallback(napi_env env,napi_status status,void * data)464 void WakeupManagerNapi::CloneForResultCompleteCallback(napi_env env, napi_status status, void *data)
465 {
466     INTELL_VOICE_LOG_INFO("enter");
467     CHECK_CONDITION_RETURN_VOID((data == nullptr), "async complete callback data is nullptr");
468     napi_value result = nullptr;
469 
470     auto asyncContext = static_cast<UpdateAsyncContext *>(data);
471     if (asyncContext->result_ != NAPI_INTELLIGENT_VOICE_SUCCESS) {
472         napi_get_undefined(env, &result);
473         NapiAsync::CommonCallbackRoutine(env, asyncContext, result);
474         return;
475     }
476 
477     if (asyncContext->processWork == nullptr) {
478         INTELL_VOICE_LOG_ERROR("process work is nullptr");
479         napi_get_undefined(env, &result);
480         asyncContext->result_ = NAPI_INTELLIGENT_VOICE_INVALID_PARAM;
481         NapiAsync::CommonCallbackRoutine(env, asyncContext, result);
482         return;
483     }
484 
485     auto cb = reinterpret_cast<WakeupManagerNapi *>(asyncContext->instanceNapi_)->callbackNapi_;
486     if (cb == nullptr) {
487         INTELL_VOICE_LOG_ERROR("get callback napi failed");
488         napi_get_undefined(env, &result);
489         asyncContext->result_ = NAPI_INTELLIGENT_VOICE_INVALID_PARAM;
490         NapiAsync::CommonCallbackRoutine(env, asyncContext, result);
491         return;
492     }
493 
494     cb->QueueAsyncWork(asyncContext);
495     if (asyncContext->processWork(asyncContext) != 0) {
496         INTELL_VOICE_LOG_ERROR("clone process work failed");
497         cb->ClearAsyncWork(true, "the request was aborted because intelligent voice processWork error");
498     }
499 }
500 
WakeupFilesForResultParser(std::shared_ptr<UpdateAsyncContext> context,napi_env env,size_t argc,napi_value * argv)501 bool WakeupManagerNapi::WakeupFilesForResultParser(std::shared_ptr<UpdateAsyncContext> context,
502     napi_env env, size_t argc, napi_value *argv)
503 {
504     CHECK_CONDITION_RETURN_FALSE((argc < ARGC_TWO), "argc less than 2");
505 
506     bool isArray = false;
507     napi_status status = napi_is_array(env, argv[0], &isArray);
508     CHECK_CONDITION_RETURN_FALSE((status != napi_ok), "get property failed");
509     CHECK_CONDITION_RETURN_FALSE((!isArray), "argv 0 is not array");
510 
511     uint32_t arrayLength = 0;
512     status = napi_get_array_length(env, argv[0], &arrayLength);
513     CHECK_CONDITION_RETURN_FALSE((status != napi_ok), "get array length failed");
514     CHECK_CONDITION_RETURN_FALSE((arrayLength == 0), "array length is 0");
515 
516     napi_value value = nullptr;
517     napi_value tempResult = nullptr;
518 
519     context->cloneFiles.clear();
520     context->cloneFiles.reserve(arrayLength);
521     for (size_t index = 0; index < arrayLength; index++) {
522         status = napi_get_element(env, argv[0], index, &value);
523         CHECK_CONDITION_RETURN_FALSE((status != napi_ok), "get array element failed");
524         napi_valuetype valueType = napi_undefined;
525         napi_typeof(env, value, &valueType);
526         CHECK_CONDITION_RETURN_FALSE((valueType != napi_object), "get array element object mismatch");
527 
528         WakeupSourceFile fileInfo;
529         status = napi_get_named_property(env, value, "filePath", &tempResult);
530         CHECK_CONDITION_RETURN_FALSE((status != napi_ok), "get property failed");
531         status = GetValue(env, tempResult, fileInfo.filePath);
532         CHECK_CONDITION_RETURN_FALSE((status != napi_ok), "get filePath failed");
533 
534         status = napi_get_named_property(env, value, "fileContent", &tempResult);
535         CHECK_CONDITION_RETURN_FALSE((status != napi_ok), "get property failed");
536         status = GetValue(env, tempResult, fileInfo.fileContent);
537         CHECK_CONDITION_RETURN_FALSE((status != napi_ok), "get fileContent failed");
538         context->cloneFiles.push_back(fileInfo);
539     }
540 
541     status = GetValue(env, argv[1], context->wakeupInfo);
542     CHECK_CONDITION_RETURN_FALSE((status != napi_ok), "get clone info failed");
543     return true;
544 }
545 
EnrollWithWakeupFilesForResult(napi_env env,napi_callback_info info)546 napi_value WakeupManagerNapi::EnrollWithWakeupFilesForResult(napi_env env, napi_callback_info info)
547 {
548     INTELL_VOICE_LOG_INFO("enter");
549     size_t cbIndex = ARG_INDEX_2;
550     auto context = make_shared<UpdateAsyncContext>(env);
551     CHECK_CONDITION_RETURN_RET(context == nullptr, nullptr, "create context failed, no memory");
552     CbInfoParser parser = [env, context](size_t argc, napi_value *argv) -> bool {
553         return WakeupFilesForResultParser(context, env, argc, argv);
554     };
555 
556     context->result_ = (context->GetCbInfo(env, info, cbIndex, parser) ? NAPI_INTELLIGENT_VOICE_SUCCESS :
557         NAPI_INTELLIGENT_VOICE_INVALID_PARAM);
558     if (context->result_ == NAPI_INTELLIGENT_VOICE_SUCCESS) {
559         reinterpret_cast<WakeupManagerNapi *>(context->instanceNapi_)->callbackNapi_ =
560             std::make_shared<IntellVoiceUpdateCallbackNapi>(env);
561     }
562 
563     context->result = EnrollResult::UNKNOWN_ERROR;
564     context->processWork = [&](AsyncContext *asyncContext) -> int32_t {
565         auto context = reinterpret_cast<UpdateAsyncContext *>(asyncContext);
566         auto wakeupManagerNapi = reinterpret_cast<WakeupManagerNapi *>(context->instanceNapi_);
567         auto manager = IntellVoiceManager::GetInstance();
568         if (wakeupManagerNapi == nullptr) {
569             INTELL_VOICE_LOG_ERROR("get manager instance failed");
570             goto PROCESS_ERR_EXIT;
571         }
572         if (manager == nullptr) {
573             INTELL_VOICE_LOG_ERROR("manager null");
574             goto PROCESS_ERR_EXIT;
575         }
576 
577         if (wakeupManagerNapi->callbackNapi_ == nullptr) {
578             INTELL_VOICE_LOG_ERROR("callback napi null");
579             goto PROCESS_ERR_EXIT;
580         }
581 
582         if (manager->EnrollWithWakeupFilesForResult(context->cloneFiles, context->wakeupInfo,
583             wakeupManagerNapi->callbackNapi_) != 0) {
584             INTELL_VOICE_LOG_ERROR("clone for result failed");
585             wakeupManagerNapi->callbackNapi_ = nullptr;
586             goto PROCESS_ERR_EXIT;
587         }
588 
589         context->cloneFiles.clear();
590         return 0;
591 
592 PROCESS_ERR_EXIT:
593         context->cloneFiles.clear();
594         return -1;
595     };
596 
597     AsyncExecute execute = [](napi_env env, void *data) {};
598 
599     return NapiAsync::AsyncWork(env, context, "EnrollWithWakeupFilesForResult", execute,
600         CloneForResultCompleteCallback);
601 }
602 
ClearUserData(napi_env env,napi_callback_info info)603 napi_value WakeupManagerNapi::ClearUserData(napi_env env, napi_callback_info info)
604 {
605     INTELL_VOICE_LOG_INFO("enter");
606     size_t cbIndex = ARG_INDEX_0;
607     auto context = std::make_shared<AsyncContext>(env);
608     if (context == nullptr) {
609         INTELL_VOICE_LOG_ERROR("create AsyncContext failed, No memory");
610         return nullptr;
611     }
612     context->result_ = (context->GetCbInfo(env, info, cbIndex, nullptr) ? NAPI_INTELLIGENT_VOICE_SUCCESS :
613         NAPI_INTELLIGENT_VOICE_INVALID_PARAM);
614     AsyncExecute execute;
615     if (context->result_ == NAPI_INTELLIGENT_VOICE_SUCCESS) {
616         execute = [](napi_env env, void *data) {
617             CHECK_CONDITION_RETURN_VOID((data == nullptr), "data is nullptr");
618             auto asyncContext = static_cast<AsyncContext *>(data);
619             IntellVoiceManager *manager = IntellVoiceManager::GetInstance();
620             if (manager == nullptr) {
621                 INTELL_VOICE_LOG_ERROR("manager is nullptr");
622                 asyncContext->result_ = NAPI_INTELLIGENT_VOICE_SYSTEM_ERROR;
623                 return;
624             }
625             asyncContext->result_ = IntellVoiceCommonNapi::ConvertResultCode(
626                 manager->ClearUserData());
627         };
628     } else {
629         execute = [](napi_env env, void *data) {};
630     }
631 
632     return NapiAsync::AsyncWork(env, context, "ClearUserData", execute);
633 }
634 }  // namespace IntellVoiceNapi
635 }  // namespace OHOS
636