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