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 #define LOG_TAG "NapiQueue"
16 #include "napi_queue.h"
17
18 namespace OHOS {
19 namespace UDMF {
~ContextBase()20 ContextBase::~ContextBase()
21 {
22 LOG_DEBUG(UDMF_KITS_NAPI, "no memory leak after callback or promise[resolved/rejected]");
23 if (env != nullptr) {
24 napi_delete_reference(env, selfRef);
25 env = nullptr;
26 }
27 }
28
GetCbInfo(napi_env envi,napi_callback_info info,NapiCbInfoParser parse,bool sync)29 void ContextBase::GetCbInfo(napi_env envi, napi_callback_info info, NapiCbInfoParser parse, bool sync)
30 {
31 env = envi;
32 size_t argc = ARGC_MAX;
33 napi_value argv[ARGC_MAX] = { nullptr };
34 status = napi_get_cb_info(env, info, &argc, argv, &self, nullptr);
35 ASSERT_STATUS(this, "napi_get_cb_info failed!");
36 ASSERT_ARGS(this, argc <= ARGC_MAX, "too many arguments!");
37 ASSERT_ARGS(this, self != nullptr, "no JavaScript this argument!");
38 if (!sync) {
39 napi_create_reference(env, self, 1, &selfRef);
40 }
41 status = napi_unwrap(env, self, &native);
42 ASSERT_STATUS(this, "self unwrap failed!");
43
44 if (!sync && (argc > 0)) {
45 // get the last arguments :: <callback>
46 size_t index = argc - 1;
47 napi_valuetype type = napi_undefined;
48 napi_status tyst = napi_typeof(env, argv[index], &type);
49 if ((tyst == napi_ok) && (type == napi_function)) {
50 status = napi_create_reference(env, argv[index], 1, &callbackRef);
51 ASSERT_STATUS(this, "ref callback failed!");
52 argc = index;
53 LOG_DEBUG(UDMF_KITS_NAPI, "async callback, no promise");
54 } else {
55 LOG_DEBUG(UDMF_KITS_NAPI, "no callback, async pormose");
56 }
57 }
58
59 if (parse) {
60 parse(argc, argv);
61 } else {
62 ASSERT_ARGS(this, argc == 0, "required no arguments!");
63 }
64 }
65
AsyncWork(napi_env env,std::shared_ptr<ContextBase> contextBase,const std::string & name,NapiAsyncExecute execute,NapiAsyncComplete complete)66 napi_value NapiQueue::AsyncWork(napi_env env, std::shared_ptr<ContextBase> contextBase, const std::string &name,
67 NapiAsyncExecute execute, NapiAsyncComplete complete)
68 {
69 AsyncContext *aCtx = new (std::nothrow) AsyncContext;
70 if (aCtx == nullptr) {
71 LOG_ERROR(UDMF_KITS_NAPI, "aCtx is nullptr");
72 return nullptr;
73 }
74 aCtx->ctxt = std::move(contextBase);
75 aCtx->execute = std::move(execute);
76 aCtx->complete = std::move(complete);
77 LOG_DEBUG(UDMF_KITS_NAPI, "NapiQueue::AsyncWork move func finish");
78 auto ctxt = aCtx->ctxt;
79 napi_value promise = nullptr;
80 if (ctxt->callbackRef == nullptr) {
81 LOG_DEBUG(UDMF_KITS_NAPI, "NapiQueue::AsyncWork has promise");
82 napi_create_promise(ctxt->env, &ctxt->deferred, &promise);
83 LOG_DEBUG(UDMF_KITS_NAPI, "create deferred promise");
84 } else {
85 LOG_DEBUG(UDMF_KITS_NAPI, "NapiQueue::AsyncWork no promise");
86 napi_get_undefined(ctxt->env, &promise);
87 }
88 napi_value resource = nullptr;
89 LOG_DEBUG(UDMF_KITS_NAPI, "NapiQueue::AsyncWork create string start");
90 napi_create_string_utf8(ctxt->env, name.c_str(), NAPI_AUTO_LENGTH, &resource);
91 LOG_DEBUG(UDMF_KITS_NAPI, "NapiQueue::AsyncWork create string finish");
92 napi_create_async_work(ctxt->env, nullptr, resource, onExecute, onComplete, reinterpret_cast<void *>(aCtx),
93 &aCtx->work);
94 auto status = napi_queue_async_work(ctxt->env, aCtx->work);
95 if (status != napi_ok) {
96 LOG_ERROR(UDMF_KITS_NAPI, "async work failed: %{public}d", status);
97 napi_get_undefined(env, &promise);
98 delete aCtx;
99 }
100 return promise;
101 }
102
GenerateOutput(ContextBase * ctxt)103 void NapiQueue::GenerateOutput(ContextBase *ctxt)
104 {
105 LOG_DEBUG(UDMF_KITS_NAPI, "GenerateOutput start");
106 napi_value result[RESULT_ALL] = { nullptr };
107 LOG_DEBUG(UDMF_KITS_NAPI, "GenerateOutput ctxt->status = %{public}d", ctxt->status);
108 if (ctxt->status == napi_ok) {
109 napi_get_undefined(ctxt->env, &result[RESULT_ERROR]);
110 if (ctxt->output == nullptr) {
111 napi_get_undefined(ctxt->env, &ctxt->output);
112 }
113 result[RESULT_DATA] = ctxt->output;
114 } else {
115 napi_value code = nullptr;
116 napi_value message = nullptr;
117 if (ctxt->jsCode != 0 && ctxt->jsCode != -1) {
118 napi_create_string_utf8(ctxt->env, std::to_string(ctxt->jsCode).c_str(), NAPI_AUTO_LENGTH, &code);
119 }
120 if (ctxt->jsCode == -1) {
121 std::string jsCode;
122 napi_create_string_utf8(ctxt->env, jsCode.c_str(), NAPI_AUTO_LENGTH, &code);
123 }
124 napi_create_string_utf8(ctxt->env, ctxt->error.c_str(), NAPI_AUTO_LENGTH, &message);
125 napi_create_error(ctxt->env, code, message, &result[RESULT_ERROR]);
126 napi_get_undefined(ctxt->env, &result[RESULT_DATA]);
127 }
128 if (ctxt->deferred != nullptr) {
129 LOG_DEBUG(UDMF_KITS_NAPI, "GenerateOutput deferred branch");
130 if (ctxt->status == napi_ok) {
131 LOG_DEBUG(UDMF_KITS_NAPI, "deferred promise resolved");
132 napi_resolve_deferred(ctxt->env, ctxt->deferred, result[RESULT_DATA]);
133 } else {
134 LOG_DEBUG(UDMF_KITS_NAPI, "deferred promise rejected");
135 napi_reject_deferred(ctxt->env, ctxt->deferred, result[RESULT_ERROR]);
136 }
137 } else {
138 napi_value callback = nullptr;
139 napi_get_reference_value(ctxt->env, ctxt->callbackRef, &callback);
140 napi_value callbackResult = nullptr;
141 LOG_INFO(UDMF_KITS_NAPI, "GenerateOutput call callback function");
142 LOG_DEBUG(UDMF_KITS_NAPI, "call callback function");
143 napi_call_function(ctxt->env, nullptr, callback, RESULT_ALL, result, &callbackResult);
144 }
145 LOG_DEBUG(UDMF_KITS_NAPI, "GenerateOutput stop");
146 }
147
onExecute(napi_env env,void * data)148 void NapiQueue::onExecute(napi_env env, void *data)
149 {
150 LOG_DEBUG(UDMF_KITS_NAPI, "NapiQueue::AsyncWork start execute");
151 ASSERT_VOID(data != nullptr, "no data");
152 auto actxt = reinterpret_cast<AsyncContext *>(data);
153 LOG_DEBUG(UDMF_KITS_NAPI, "napi_async_execute_callback ctxt->status = %{public}d", actxt->ctxt->status);
154 if (actxt->execute && actxt->ctxt->status == napi_ok) {
155 LOG_DEBUG(UDMF_KITS_NAPI, "NapiQueue::AsyncWork do user design execute");
156 actxt->execute();
157 }
158 LOG_DEBUG(UDMF_KITS_NAPI, "NapiQueue::AsyncWork finish execute");
159 }
160
onComplete(napi_env env,napi_status status,void * data)161 void NapiQueue::onComplete(napi_env env, napi_status status, void *data)
162 {
163 LOG_DEBUG(UDMF_KITS_NAPI, "NapiQueue::AsyncWork start output");
164 ASSERT_VOID(data != nullptr, "no data");
165 auto actxt = reinterpret_cast<AsyncContext *>(data);
166 LOG_DEBUG(UDMF_KITS_NAPI, "napi_async_complete_callback status = %{public}d, ctxt->status = %{public}d", status,
167 actxt->ctxt->status);
168 if ((status != napi_ok) && (actxt->ctxt->status == napi_ok)) {
169 LOG_DEBUG(UDMF_KITS_NAPI, "NapiQueue::AsyncWork check status");
170 actxt->ctxt->status = status;
171 }
172 if ((actxt->complete) && (status == napi_ok) && (actxt->ctxt->status == napi_ok)) {
173 LOG_DEBUG(UDMF_KITS_NAPI, "NapiQueue::AsyncWork do user design output");
174 actxt->complete(actxt->ctxt->output);
175 }
176 GenerateOutput(actxt->ctxt.get());
177 LOG_DEBUG(UDMF_KITS_NAPI, "NapiQueue::AsyncWork finish output");
178 napi_delete_reference(env, actxt->ctxt->callbackRef);
179 napi_delete_async_work(env, actxt->work);
180 delete actxt;
181 }
182 } // namespace UDMF
183 } // namespace OHOS
184