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_async_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
Enqueue(napi_env env,ContextBase * ctxt,const std::string & name,NapiAsyncExecute execute,NapiAsyncComplete complete)74 napi_value NapiAsyncWork::Enqueue(napi_env env, ContextBase *ctxt, const std::string &name,
75 NapiAsyncExecute execute, NapiAsyncComplete complete)
76 {
77 if (ctxt->status != napi_ok) {
78 NapiUtils::ThrowError(env, CODE_TO_MESSAGE.find(ctxt->errCode)->second.c_str(), ctxt->errCode);
79 return NapiUtils::GetUndefinedValue(env);
80 }
81 ctxt->execute = std::move(execute);
82 ctxt->complete = std::move(complete);
83 napi_value promise = nullptr;
84 if (ctxt->callbackRef == nullptr) {
85 napi_create_promise(ctxt->env, &ctxt->deferred, &promise);
86 TIME_HILOGD(TIME_MODULE_JS_NAPI, "create deferred promise");
87 } else {
88 napi_get_undefined(ctxt->env, &promise);
89 }
90 napi_value resource = nullptr;
91 napi_create_string_utf8(ctxt->env, name.c_str(), NAPI_AUTO_LENGTH, &resource);
92 napi_create_async_work(
93 ctxt->env, nullptr, resource,
94 [](napi_env env, void *data) {
95 CHECK_RETURN_VOID(TIME_MODULE_JS_NAPI, data != nullptr, "napi_async_execute_callback nullptr");
96 auto ctxt = reinterpret_cast<ContextBase *>(data);
97 TIME_HILOGD(TIME_MODULE_JS_NAPI, "napi_async_execute_callback ctxt->status=%{public}d", ctxt->status);
98 if (ctxt->execute != nullptr && ctxt->status == napi_ok) {
99 ctxt->execute();
100 }
101 },
102 [](napi_env env, napi_status status, void *data) {
103 CHECK_RETURN_VOID(TIME_MODULE_JS_NAPI, data != nullptr, "napi_async_complete_callback nullptr");
104 auto ctxt = reinterpret_cast<ContextBase *>(data);
105 TIME_HILOGD(TIME_MODULE_JS_NAPI, "napi_async_complete_callback status=%{public}d, ctxt->status=%{public}d",
106 status, ctxt->status);
107 if ((status != napi_ok) && (ctxt->status == napi_ok)) {
108 ctxt->status = status;
109 }
110 if ((ctxt->complete) && (status == napi_ok) && (ctxt->status == napi_ok)) {
111 ctxt->complete(ctxt->output);
112 }
113 GenerateOutput(ctxt);
114 delete ctxt;
115 },
116 reinterpret_cast<void *>(ctxt), &ctxt->work);
117 napi_queue_async_work(ctxt->env, ctxt->work);
118 return promise;
119 }
120
GenerateOutput(ContextBase * ctxt)121 void NapiAsyncWork::GenerateOutput(ContextBase *ctxt)
122 {
123 napi_value result[RESULT_ALL] = { nullptr };
124 if (ctxt->status == napi_ok) {
125 napi_get_undefined(ctxt->env, &result[RESULT_ERROR]);
126 if (ctxt->output == nullptr) {
127 napi_get_undefined(ctxt->env, &ctxt->output);
128 }
129 result[RESULT_DATA] = ctxt->output;
130 } else {
131 napi_value message = nullptr;
132 napi_value code = nullptr;
133 int32_t jsErrorCode = NapiUtils::ConvertErrorCode(ctxt->errCode);
134 napi_create_string_utf8(ctxt->env, CODE_TO_MESSAGE.find(jsErrorCode)->second.c_str(), NAPI_AUTO_LENGTH,
135 &message);
136 napi_create_error(ctxt->env, nullptr, message, &result[RESULT_ERROR]);
137 if (jsErrorCode != JsErrorCode::ERROR) {
138 napi_create_int32(ctxt->env, jsErrorCode, &code);
139 napi_set_named_property(ctxt->env, result[RESULT_ERROR], "code", code);
140 }
141 napi_get_undefined(ctxt->env, &result[RESULT_DATA]);
142 }
143 if (ctxt->deferred != nullptr) {
144 if (ctxt->status == napi_ok) {
145 TIME_HILOGD(TIME_MODULE_JS_NAPI, "deferred promise resolved");
146 napi_resolve_deferred(ctxt->env, ctxt->deferred, result[RESULT_DATA]);
147 } else {
148 TIME_HILOGD(TIME_MODULE_JS_NAPI, "deferred promise rejected");
149 napi_reject_deferred(ctxt->env, ctxt->deferred, result[RESULT_ERROR]);
150 }
151 } else {
152 napi_value callback = nullptr;
153 napi_get_reference_value(ctxt->env, ctxt->callbackRef, &callback);
154 napi_value callbackResult = nullptr;
155 TIME_HILOGD(TIME_MODULE_JS_NAPI, "call callback function");
156 napi_call_function(ctxt->env, nullptr, callback, RESULT_ALL, result, &callbackResult);
157 }
158 }
159 } // namespace Time
160 } // namespace MiscServices
161 } // namespace OHOS