• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         NapiUtils::ThrowError(env, CODE_TO_MESSAGE.find(ctxt->errCode)->second.c_str(), ctxt->errCode);
79         delete ctxt;
80         return NapiUtils::GetUndefinedValue(env);
81     }
82     ctxt->execute = std::move(execute);
83     ctxt->complete = std::move(complete);
84     napi_value promise = nullptr;
85     if (ctxt->callbackRef == nullptr) {
86         napi_create_promise(ctxt->env, &ctxt->deferred, &promise);
87         TIME_HILOGD(TIME_MODULE_JS_NAPI, "create deferred promise");
88     } else {
89         napi_get_undefined(ctxt->env, &promise);
90     }
91     napi_value resource = nullptr;
92     napi_create_string_utf8(ctxt->env, name.c_str(), NAPI_AUTO_LENGTH, &resource);
93     napi_create_async_work(
94         ctxt->env, nullptr, resource,
95         [](napi_env env, void *data) {
96             CHECK_RETURN_VOID(TIME_MODULE_JS_NAPI, data != nullptr, "napi_async_execute_callback nullptr");
97             auto ctxt = reinterpret_cast<ContextBase *>(data);
98             TIME_HILOGD(TIME_MODULE_JS_NAPI, "napi_async_execute_callback ctxt->status=%{public}d", ctxt->status);
99             if (ctxt->execute != nullptr && ctxt->status == napi_ok) {
100                 ctxt->execute();
101             }
102         },
103         [](napi_env env, napi_status status, void *data) {
104             CHECK_RETURN_VOID(TIME_MODULE_JS_NAPI, data != nullptr, "napi_async_complete_callback nullptr");
105             auto ctxt = reinterpret_cast<ContextBase *>(data);
106             TIME_HILOGD(TIME_MODULE_JS_NAPI, "status=%{public}d, ctxt->status=%{public}d", 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     auto ret = napi_queue_async_work_with_qos(ctxt->env, ctxt->work, napi_qos_user_initiated);
118     if (ret != napi_ok) {
119         napi_delete_async_work(env, ctxt->work);
120         delete ctxt;
121         NAPI_CALL(env, ret);
122     }
123     return promise;
124 }
125 
GenerateOutput(ContextBase * ctxt)126 void NapiWork::GenerateOutput(ContextBase *ctxt)
127 {
128     napi_value result[RESULT_ALL] = { nullptr };
129     if (ctxt->status == napi_ok) {
130         napi_get_undefined(ctxt->env, &result[RESULT_ERROR]);
131         if (ctxt->output == nullptr) {
132             napi_get_undefined(ctxt->env, &ctxt->output);
133         }
134         result[RESULT_DATA] = ctxt->output;
135     } else {
136         napi_value message = nullptr;
137         napi_value code = nullptr;
138         int32_t jsErrorCode = NapiUtils::ConvertErrorCode(ctxt->errCode);
139         napi_create_string_utf8(ctxt->env, CODE_TO_MESSAGE.find(jsErrorCode)->second.c_str(), NAPI_AUTO_LENGTH,
140             &message);
141         napi_create_error(ctxt->env, nullptr, message, &result[RESULT_ERROR]);
142         if (jsErrorCode != JsErrorCode::ERROR) {
143             napi_create_int32(ctxt->env, jsErrorCode, &code);
144             napi_set_named_property(ctxt->env, result[RESULT_ERROR], "code", code);
145         }
146         napi_get_undefined(ctxt->env, &result[RESULT_DATA]);
147     }
148     if (ctxt->deferred != nullptr) {
149         if (ctxt->status == napi_ok) {
150             TIME_HILOGD(TIME_MODULE_JS_NAPI, "deferred promise resolved");
151             napi_resolve_deferred(ctxt->env, ctxt->deferred, result[RESULT_DATA]);
152         } else {
153             TIME_HILOGD(TIME_MODULE_JS_NAPI, "deferred promise rejected");
154             napi_reject_deferred(ctxt->env, ctxt->deferred, result[RESULT_ERROR]);
155         }
156     } else {
157         napi_value callback = nullptr;
158         napi_get_reference_value(ctxt->env, ctxt->callbackRef, &callback);
159         napi_value callbackResult = nullptr;
160         TIME_HILOGD(TIME_MODULE_JS_NAPI, "call callback function");
161         napi_call_function(ctxt->env, nullptr, callback, RESULT_ALL, result, &callbackResult);
162     }
163 }
164 
SyncEnqueue(napi_env env,ContextBase * ctxt,const std::string & name,NapiExecute execute,NapiComplete complete)165 napi_value NapiWork::SyncEnqueue(napi_env env, ContextBase *ctxt, const std::string &name,
166     NapiExecute execute, NapiComplete complete)
167 {
168     if (ctxt->status != napi_ok) {
169         NapiUtils::ThrowError(env, CODE_TO_MESSAGE.find(ctxt->errCode)->second.c_str(), ctxt->errCode);
170         delete ctxt;
171         return NapiUtils::GetUndefinedValue(env);
172     }
173 
174     ctxt->execute = std::move(execute);
175     ctxt->complete = std::move(complete);
176 
177     if (ctxt->execute != nullptr && ctxt->status == napi_ok) {
178         ctxt->execute();
179     }
180 
181     if (ctxt->complete != nullptr && (ctxt->status == napi_ok)) {
182         ctxt->complete(ctxt->output);
183     }
184 
185     return GenerateOutputSync(env, ctxt);
186 }
187 
GenerateOutputSync(napi_env env,ContextBase * ctxt)188 napi_value NapiWork::GenerateOutputSync(napi_env env, ContextBase *ctxt)
189 {
190     napi_value result = nullptr;
191     if (ctxt->status == napi_ok) {
192         if (ctxt->output == nullptr) {
193             napi_get_undefined(ctxt->env, &ctxt->output);
194         }
195         result = ctxt->output;
196     } else {
197         napi_value error = nullptr;
198         napi_value message = nullptr;
199         int32_t jsErrorCode = NapiUtils::ConvertErrorCode(ctxt->errCode);
200         napi_create_string_utf8(ctxt->env, CODE_TO_MESSAGE.find(jsErrorCode)->second.c_str(), NAPI_AUTO_LENGTH,
201                                 &message);
202         napi_create_error(ctxt->env, nullptr, message, &error);
203         if (jsErrorCode != JsErrorCode::ERROR) {
204             napi_value code = nullptr;
205             napi_create_int32(ctxt->env, jsErrorCode, &code);
206             napi_set_named_property(ctxt->env, error, "code", code);
207         }
208         napi_throw(env, error);
209     }
210     delete ctxt;
211     return result;
212 }
213 } // namespace Time
214 } // namespace MiscServices
215 } // namespace OHOS