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 "intell_voice_update_callback_napi.h"
17 #include "intell_voice_log.h"
18 #include "intell_voice_common_napi.h"
19
20 #define LOG_TAG "UpdateCallbackNapi"
21
22 using namespace std;
23 using namespace OHOS::HDI::IntelligentVoice::Engine::V1_0;
24
25 namespace OHOS {
26 namespace IntellVoiceNapi {
IntellVoiceUpdateCallbackNapi(const napi_env env)27 IntellVoiceUpdateCallbackNapi::IntellVoiceUpdateCallbackNapi(const napi_env env) : env_(env)
28 {
29 if (env_ != nullptr) {
30 napi_get_uv_event_loop(env_, &loop_);
31 }
32 }
33
~IntellVoiceUpdateCallbackNapi()34 IntellVoiceUpdateCallbackNapi::~IntellVoiceUpdateCallbackNapi()
35 {
36 }
37
QueueAsyncWork(UpdateAsyncContext * context)38 void IntellVoiceUpdateCallbackNapi::QueueAsyncWork(UpdateAsyncContext *context)
39 {
40 std::lock_guard<std::mutex> lock(mutex_);
41 context_.push(context);
42 }
43
ClearAsyncWork(bool error,const std::string & msg)44 void IntellVoiceUpdateCallbackNapi::ClearAsyncWork(bool error, const std::string &msg)
45 {
46 INTELL_VOICE_LOG_INFO("%{public}s", msg.c_str());
47 std::lock_guard<std::mutex> lock(mutex_);
48 while (!context_.empty()) {
49 UpdateAsyncContext *context = context_.front();
50 context_.pop();
51 if (error) {
52 INTELL_VOICE_LOG_WARN("error occured");
53 }
54 if (context == nullptr) {
55 continue;
56 }
57
58 INTELL_VOICE_LOG_WARN("fail occured");
59 context->result_ = EnrollResult::UNKNOWN_ERROR;
60
61 OnJsCallBack(context);
62 }
63 }
64
OnUpdateComplete(const int result)65 void IntellVoiceUpdateCallbackNapi::OnUpdateComplete(const int result)
66 {
67 INTELL_VOICE_LOG_INFO("OnUpdateComplete: result: %{public}d", result);
68 std::lock_guard<std::mutex> lock(mutex_);
69 if (context_.empty()) {
70 INTELL_VOICE_LOG_WARN("queue is empty");
71 return;
72 }
73
74 UpdateAsyncContext *context = context_.front();
75 context_.pop();
76 if (context == nullptr) {
77 INTELL_VOICE_LOG_ERROR("context is nullptr");
78 return;
79 }
80
81 context->result = (result == 0) ? EnrollResult::SUCCESS : EnrollResult::UNKNOWN_ERROR;
82 OnJsCallBack(context);
83 }
84
UvWorkCallBack(uv_work_t * work,int status)85 void IntellVoiceUpdateCallbackNapi::UvWorkCallBack(uv_work_t *work, int status)
86 {
87 INTELL_VOICE_LOG_INFO("enter");
88 auto asyncContext = reinterpret_cast<UpdateAsyncContext *>(work->data);
89 napi_value result = nullptr;
90 if (asyncContext != nullptr) {
91 napi_env env = asyncContext->env_;
92 result = SetValue(env, asyncContext->result);
93 NapiAsync::CommonCallbackRoutine(env, asyncContext, result);
94 }
95
96 delete work;
97 }
98
OnJsCallBack(UpdateAsyncContext * context)99 void IntellVoiceUpdateCallbackNapi::OnJsCallBack(UpdateAsyncContext *context)
100 {
101 INTELL_VOICE_LOG_INFO("enter, result:%{public}d", context->result_);
102 if (loop_ != nullptr) {
103 uv_work_t *work = new (std::nothrow) uv_work_t;
104 if (work != nullptr) {
105 work->data = reinterpret_cast<void *>(context);
106 int ret = uv_queue_work(
107 loop_, work, [](uv_work_t *work) {}, UvWorkCallBack);
108 if (ret != 0) {
109 INTELL_VOICE_LOG_INFO("Failed to execute libuv work queue");
110 context->contextSp_.reset();
111 delete work;
112 }
113 } else {
114 context->contextSp_.reset();
115 }
116 } else {
117 context->contextSp_.reset();
118 }
119 }
120 } // namespace IntellVoiceNapi
121 } // namespace OHOS