1 /*
2 * Copyright (c) 2021 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 <uv.h>
16 #include "napi/native_common.h"
17 #include "permission_callback.h"
18 #include "hilog_wrapper.h"
19
20 namespace {
21 constexpr size_t ARGS_SIZE_TWO = 2;
22 }
23
PermissionCallback(napi_env env,napi_ref callback)24 PermissionCallback::PermissionCallback(napi_env env, napi_ref callback) : env_(env), callback_(callback)
25 {}
26
~PermissionCallback()27 PermissionCallback::~PermissionCallback()
28 {
29 if (callback_ != nullptr) {
30 napi_delete_reference(env_, callback_);
31 }
32 }
33
OnChanged(const int32_t uid)34 void PermissionCallback::OnChanged(const int32_t uid)
35 {
36 uv_loop_s *loop = nullptr;
37 #if NAPI_VERSION >= 2
38 napi_get_uv_event_loop(env_, &loop);
39 #endif // NAPI_VERSION >= 2
40 if (loop == nullptr) {
41 HILOG_INFO("loop instance is nullptr");
42 return;
43 }
44
45 uv_work_t *work = new (std::nothrow) uv_work_t;
46 if (work == nullptr) {
47 HILOG_ERROR("new uv_work_t failed");
48 return;
49 }
50 CallbackInfo *callbackInfo = new (std::nothrow) CallbackInfo{
51 .env = env_,
52 .callback = callback_,
53 .uid = uid,
54 };
55 if (callbackInfo == nullptr) {
56 HILOG_ERROR("new CallbackInfo failed");
57 return;
58 }
59 work->data = (void *)callbackInfo;
60
61 int rev = uv_queue_work(
62 loop,
63 work,
64 [](uv_work_t *work) {},
65 [](uv_work_t *work, int status) {
66 HILOG_INFO("CallOnPermissonChanged, uv_queue_work");
67 // JS Thread
68 CallbackInfo *event = (CallbackInfo *)work->data;
69 napi_value result[2] = {0};
70
71 napi_value callResult = nullptr;
72 napi_value eCode = nullptr;
73 NAPI_CALL_RETURN_VOID(event->env, napi_create_int32(event->env, 0, &eCode));
74 napi_create_object(event->env, &callResult);
75 napi_set_named_property(event->env, callResult, "code", eCode);
76 result[0] = callResult;
77 // create uid
78 NAPI_CALL_RETURN_VOID(event->env, napi_create_int32(event->env, event->uid, &result[1]));
79
80 napi_value callback = 0;
81 napi_value undefined = 0;
82 napi_get_undefined(event->env, &undefined);
83 napi_value callbackResult = 0;
84 napi_get_reference_value(event->env, event->callback, &callback);
85 napi_call_function(event->env, undefined, callback, ARGS_SIZE_TWO, &result[0], &callbackResult);
86 delete event;
87 event = nullptr;
88 delete work;
89 work = nullptr;
90 });
91 if (rev != 0) {
92 if (callbackInfo != nullptr) {
93 delete callbackInfo;
94 }
95 if (work != nullptr) {
96 delete work;
97 }
98 }
99 HILOG_INFO("OnChanged, end");
100 }