1 /*
2 * Copyright (c) 2023 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 #include "service_change_callback_napi.h"
16
17 #include <uv.h>
18 #include "intell_voice_info.h"
19 #include "intell_voice_log.h"
20 #include "intell_voice_napi_util.h"
21 #include "scope_guard.h"
22 #include "intell_voice_common_napi.h"
23
24
25 #define LOG_TAG "ServiceChangeCallbackNapi"
26
27 using namespace std;
28 using namespace OHOS::IntellVoice;
29
30 namespace OHOS {
31 namespace IntellVoiceNapi {
ServiceChangeCallbackNapi(napi_env env)32 ServiceChangeCallbackNapi::ServiceChangeCallbackNapi(napi_env env) : env_(env)
33 {
34 INTELL_VOICE_LOG_INFO("enter");
35 if (env_ != nullptr) {
36 napi_get_uv_event_loop(env_, &loop_);
37 }
38 }
39
SaveCallbackReference(napi_value callback)40 void ServiceChangeCallbackNapi::SaveCallbackReference(napi_value callback)
41 {
42 std::lock_guard<std::mutex> lock(mutex_);
43 napi_ref cbRef = nullptr;
44 constexpr int32_t refCount = 1;
45 napi_status status = napi_create_reference(env_, callback, refCount, &cbRef);
46 CHECK_CONDITION_RETURN_VOID(((status != napi_ok) || (cbRef == nullptr)), "creating reference for callback failed");
47 callbackRef_ = std::make_shared<IntellVoiceRef>(env_, cbRef);
48 }
49
OnRemoteDied(const wptr<IRemoteObject> & remote)50 void ServiceChangeCallbackNapi::OnRemoteDied(const wptr <IRemoteObject> &remote)
51 {
52 std::lock_guard<std::mutex> lock(mutex_);
53 INTELL_VOICE_LOG_INFO("receive sa death callback");
54 (void)remote;
55 int32_t status = ServiceChangeType::SERVICE_UNAVAILABLE;
56
57 OnJsCallbackServiceChange(status);
58 }
59
OnJsCallbackServiceChange(int32_t status)60 void ServiceChangeCallbackNapi::OnJsCallbackServiceChange(int32_t status)
61 {
62 CHECK_CONDITION_RETURN_VOID((loop_ == nullptr), "loop is nullptr");
63 uv_work_t *work = new (std::nothrow) uv_work_t;
64 CHECK_CONDITION_RETURN_VOID((work == nullptr), "Create uv work failed, no memory");
65 work->data = new ServiceChangeCallbackData { status, callbackRef_ };
66 CHECK_CONDITION_RETURN_VOID((work->data == nullptr), "Create uv work data failed, no memory");
67
68 int32_t ret = uv_queue_work_with_qos(loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int uvStatus) {
69 ServiceChangeCallbackData *callbackData = reinterpret_cast<ServiceChangeCallbackData *>(work->data);
70 ON_SCOPE_EXIT {
71 INTELL_VOICE_LOG_INFO("clear momery");
72 delete callbackData;
73 delete work;
74 };
75
76 CHECK_CONDITION_RETURN_VOID((callbackData->callback == nullptr), "callback is nullptr");
77 napi_env env = callbackData->callback->env_;
78 napi_value jsCallback = callbackData->callback->GetRefValue();
79 CHECK_CONDITION_RETURN_VOID((jsCallback == nullptr), "callback ref is nullptr");
80
81 const size_t argc = ARGC_ONE;
82 napi_value args[argc] = { SetValue(env, callbackData->status) };
83 CHECK_CONDITION_RETURN_VOID((args[ARG_INDEX_0] == nullptr), "arg is nullptr");
84
85 INTELL_VOICE_LOG_INFO("uv_queue_work start");
86 napi_value result = nullptr;
87 napi_status status = napi_call_function(env, nullptr, jsCallback, argc, args, &result);
88 if (status != napi_ok) {
89 INTELL_VOICE_LOG_ERROR("failed to call engine event callback, error: %{public}d", status);
90 }
91 }, uv_qos_default);
92 if (ret != 0) {
93 INTELL_VOICE_LOG_ERROR("failed to execute libuv work queue");
94 delete reinterpret_cast<ServiceChangeCallbackData *>(work->data);
95 delete work;
96 }
97 }
98 } // namespace IntellVoiceNapi
99 } // namespace OHOS
100