1 /*
2 * Copyright (C) 2022 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_work.h"
17
18 #include "napi_utils.h"
19
20 namespace OHOS {
21 namespace MiscServices {
22 namespace Time {
~ContextBase()23 ContextBase::~ContextBase()
24 {
25 TIME_HILOGD(TIME_MODULE_JS_NAPI, "no memory leak after callback or promise[resolved/rejected]");
26 if (env != nullptr) {
27 if (work != nullptr) {
28 napi_delete_async_work(env, work);
29 }
30 if (callbackRef != nullptr) {
31 napi_delete_reference(env, callbackRef);
32 }
33 napi_delete_reference(env, selfRef);
34 env = nullptr;
35 }
36 }
37
GetCbInfo(napi_env envi,napi_callback_info info,NapiCbInfoParser parser,bool sync)38 void ContextBase::GetCbInfo(napi_env envi, napi_callback_info info, NapiCbInfoParser parser, bool sync)
39 {
40 env = envi;
41 size_t argc = ARGC_MAX;
42 napi_value argv[ARGC_MAX] = { nullptr };
43 status = napi_get_cb_info(env, info, &argc, argv, &self, nullptr);
44 CHECK_STATUS_RETURN_VOID(TIME_MODULE_JS_NAPI, this, "napi_get_cb_info failed!", JsErrorCode::PARAMETER_ERROR);
45 CHECK_ARGS_RETURN_VOID(TIME_MODULE_JS_NAPI, this, argc <= ARGC_MAX, "too many arguments!",
46 JsErrorCode::PARAMETER_ERROR);
47 CHECK_ARGS_RETURN_VOID(TIME_MODULE_JS_NAPI, this, self != nullptr, "no JavaScript this argument!",
48 JsErrorCode::PARAMETER_ERROR);
49 napi_create_reference(env, self, 1, &selfRef);
50
51 if (!sync && (argc > 0)) {
52 // get the last arguments :: <callback>
53 size_t index = argc - 1;
54 napi_valuetype type = napi_undefined;
55 napi_status tyst = napi_typeof(env, argv[index], &type);
56 if ((tyst == napi_ok) && (type == napi_function)) {
57 status = napi_create_reference(env, argv[index], 1, &callbackRef);
58 CHECK_STATUS_RETURN_VOID(TIME_MODULE_JS_NAPI, this, "ref callback failed!", JsErrorCode::PARAMETER_ERROR);
59 argc = index;
60 TIME_HILOGD(TIME_MODULE_JS_NAPI, "async callback, no promise");
61 } else {
62 TIME_HILOGD(TIME_MODULE_JS_NAPI, "no callback, async promise");
63 }
64 }
65
66 if (parser) {
67 parser(argc, argv);
68 } else {
69 CHECK_ARGS_RETURN_VOID(TIME_MODULE_JS_NAPI, this, argc == 0, "required no arguments!",
70 JsErrorCode::PARAMETER_ERROR);
71 }
72 }
73
AsyncEnqueue(napi_env env,ContextBase * ctxt,const std::string & name,NapiExecute execute,NapiComplete complete)74 napi_value NapiWork::AsyncEnqueue(napi_env env, ContextBase *ctxt, const std::string &name,
75 NapiExecute execute, NapiComplete complete)
76 {
77 if (ctxt->status != napi_ok) {
78 auto message = CODE_TO_MESSAGE.find(ctxt->errCode)->second + ". Error message: " + ctxt->errMessage;
79 NapiUtils::ThrowError(env, message.c_str(), ctxt->errCode);
80 delete ctxt;
81 return NapiUtils::GetUndefinedValue(env);
82 }
83 ctxt->execute = std::move(execute);
84 ctxt->complete = std::move(complete);
85 napi_value promise = nullptr;
86 if (ctxt->callbackRef == nullptr) {
87 napi_create_promise(ctxt->env, &ctxt->deferred, &promise);
88 TIME_HILOGD(TIME_MODULE_JS_NAPI, "create deferred promise");
89 } else {
90 napi_get_undefined(ctxt->env, &promise);
91 }
92 napi_value resource = nullptr;
93 napi_create_string_utf8(ctxt->env, name.c_str(), NAPI_AUTO_LENGTH, &resource);
94 napi_create_async_work(
95 ctxt->env, nullptr, resource,
96 [](napi_env env, void *data) {
97 CHECK_RETURN_VOID(TIME_MODULE_JS_NAPI, data != nullptr, "napi_async_execute_callback nullptr");
98 auto ctxt = reinterpret_cast<ContextBase *>(data);
99 TIME_HILOGD(TIME_MODULE_JS_NAPI, "napi_async_execute_callback ctxt->status=%{public}d", ctxt->status);
100 if (ctxt->execute != nullptr && ctxt->status == napi_ok) {
101 ctxt->execute();
102 }
103 },
104 [](napi_env env, napi_status status, void *data) {
105 CHECK_RETURN_VOID(TIME_MODULE_JS_NAPI, data != nullptr, "napi_async_complete_callback nullptr");
106 auto ctxt = reinterpret_cast<ContextBase *>(data);
107 TIME_HILOGD(TIME_MODULE_JS_NAPI, "status=%{public}d, ctxt->status=%{public}d", status, ctxt->status);
108 if ((status != napi_ok) && (ctxt->status == napi_ok)) {
109 ctxt->status = status;
110 }
111 if ((ctxt->complete) && (status == napi_ok) && (ctxt->status == napi_ok)) {
112 ctxt->complete(ctxt->output);
113 }
114 GenerateOutput(ctxt);
115 delete ctxt;
116 },
117 reinterpret_cast<void *>(ctxt), &ctxt->work);
118 auto ret = napi_queue_async_work_with_qos(ctxt->env, ctxt->work, napi_qos_user_initiated);
119 if (ret != napi_ok) {
120 napi_delete_async_work(env, ctxt->work);
121 delete ctxt;
122 NAPI_CALL(env, ret);
123 }
124 return promise;
125 }
126
GenerateOutput(ContextBase * ctxt)127 void NapiWork::GenerateOutput(ContextBase *ctxt)
128 {
129 napi_value result[RESULT_ALL] = { nullptr };
130 if (ctxt->status == napi_ok) {
131 napi_get_undefined(ctxt->env, &result[RESULT_ERROR]);
132 if (ctxt->output == nullptr) {
133 napi_get_undefined(ctxt->env, &ctxt->output);
134 }
135 result[RESULT_DATA] = ctxt->output;
136 } else {
137 napi_value message = nullptr;
138 napi_value code = nullptr;
139 int32_t jsErrorCode = NapiUtils::ConvertErrorCode(ctxt->errCode);
140 napi_create_string_utf8(ctxt->env, CODE_TO_MESSAGE.find(jsErrorCode)->second.c_str(), NAPI_AUTO_LENGTH,
141 &message);
142 napi_create_error(ctxt->env, nullptr, message, &result[RESULT_ERROR]);
143 if (jsErrorCode != JsErrorCode::ERROR) {
144 napi_create_int32(ctxt->env, jsErrorCode, &code);
145 napi_set_named_property(ctxt->env, result[RESULT_ERROR], "code", code);
146 }
147 napi_get_undefined(ctxt->env, &result[RESULT_DATA]);
148 }
149 if (ctxt->deferred != nullptr) {
150 if (ctxt->status == napi_ok) {
151 TIME_HILOGD(TIME_MODULE_JS_NAPI, "deferred promise resolved");
152 napi_resolve_deferred(ctxt->env, ctxt->deferred, result[RESULT_DATA]);
153 } else {
154 TIME_HILOGD(TIME_MODULE_JS_NAPI, "deferred promise rejected");
155 napi_reject_deferred(ctxt->env, ctxt->deferred, result[RESULT_ERROR]);
156 }
157 } else {
158 napi_value callback = nullptr;
159 napi_get_reference_value(ctxt->env, ctxt->callbackRef, &callback);
160 napi_value callbackResult = nullptr;
161 TIME_HILOGD(TIME_MODULE_JS_NAPI, "call callback function");
162 napi_call_function(ctxt->env, nullptr, callback, RESULT_ALL, result, &callbackResult);
163 }
164 }
165
SyncEnqueue(napi_env env,ContextBase * ctxt,const std::string & name,NapiExecute execute,NapiComplete complete)166 napi_value NapiWork::SyncEnqueue(napi_env env, ContextBase *ctxt, const std::string &name,
167 NapiExecute execute, NapiComplete complete)
168 {
169 if (ctxt->status != napi_ok) {
170 auto message = CODE_TO_MESSAGE.find(ctxt->errCode)->second + ". Error message: " + ctxt->errMessage;
171 NapiUtils::ThrowError(env, message.c_str(), ctxt->errCode);
172 delete ctxt;
173 return NapiUtils::GetUndefinedValue(env);
174 }
175
176 ctxt->execute = std::move(execute);
177 ctxt->complete = std::move(complete);
178
179 if (ctxt->execute != nullptr && ctxt->status == napi_ok) {
180 ctxt->execute();
181 }
182
183 if (ctxt->complete != nullptr && (ctxt->status == napi_ok)) {
184 ctxt->complete(ctxt->output);
185 }
186
187 return GenerateOutputSync(env, ctxt);
188 }
189
GenerateOutputSync(napi_env env,ContextBase * ctxt)190 napi_value NapiWork::GenerateOutputSync(napi_env env, ContextBase *ctxt)
191 {
192 napi_value result = nullptr;
193 if (ctxt->status == napi_ok) {
194 if (ctxt->output == nullptr) {
195 napi_get_undefined(ctxt->env, &ctxt->output);
196 }
197 result = ctxt->output;
198 } else {
199 napi_value error = nullptr;
200 napi_value message = nullptr;
201 int32_t jsErrorCode = NapiUtils::ConvertErrorCode(ctxt->errCode);
202 napi_create_string_utf8(ctxt->env, CODE_TO_MESSAGE.find(jsErrorCode)->second.c_str(), NAPI_AUTO_LENGTH,
203 &message);
204 napi_create_error(ctxt->env, nullptr, message, &error);
205 if (jsErrorCode != JsErrorCode::ERROR) {
206 napi_value code = nullptr;
207 napi_create_int32(ctxt->env, jsErrorCode, &code);
208 napi_set_named_property(ctxt->env, error, "code", code);
209 }
210 napi_throw(env, error);
211 }
212 delete ctxt;
213 return result;
214 }
215 } // namespace Time
216 } // namespace MiscServices
217 } // namespace OHOS