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 #define LOG_TAG "AsyncCall"
16
17 #include "async_call.h"
18
19 #include "sclock_log.h"
20
21 namespace OHOS::ScreenLock {
AsyncCall(napi_env env,napi_callback_info info,Context * context,size_t pos)22 AsyncCall::AsyncCall(napi_env env, napi_callback_info info, Context *context, size_t pos)
23 {
24 context_ = new AsyncContext();
25 context_->env = env;
26 if (context_ == nullptr) {
27 SCLOCK_HILOGD("AsyncCall context_ is null");
28 return;
29 }
30 size_t argc = 6;
31 napi_value self = nullptr;
32 napi_value argv[6] = { nullptr };
33 NAPI_CALL_RETURN_VOID(env, napi_get_cb_info(env, info, &argc, argv, &self, nullptr));
34 pos = ((pos == ASYNC_DEFAULT_POS) ? (argc - 1) : pos);
35 if (pos >= 0 && pos < argc) {
36 napi_valuetype valueType = napi_undefined;
37 napi_typeof(env, argv[pos], &valueType);
38 if (valueType == napi_function) {
39 napi_create_reference(env, argv[pos], 1, &context_->callback);
40 argc = pos;
41 }
42 }
43 if (context == nullptr) {
44 SCLOCK_HILOGD("AsyncCall context is null");
45 return;
46 }
47 NAPI_CALL_RETURN_VOID(env, (*context)(env, argc, argv, self));
48 context_->ctx = context;
49 napi_create_reference(env, self, 1, &context_->self);
50 SCLOCK_HILOGD("AsyncCall");
51 }
52
~AsyncCall()53 AsyncCall::~AsyncCall()
54 {
55 if (context_ == nullptr) {
56 return;
57 }
58 delete context_;
59 }
60
Call(const napi_env env,Context::ExecAction exec,const std::string & resourceName)61 napi_value AsyncCall::Call(const napi_env env, Context::ExecAction exec, const std::string &resourceName)
62 {
63 if (context_ == nullptr) {
64 SCLOCK_HILOGD("context_ is null");
65 return nullptr;
66 }
67 if (context_->ctx == nullptr) {
68 SCLOCK_HILOGD("context_->ctx is null");
69 return nullptr;
70 }
71 context_->ctx->exec_ = std::move(exec);
72 napi_value promise = nullptr;
73 if (context_->callback == nullptr) {
74 napi_create_promise(env, &context_->defer, &promise);
75 } else {
76 napi_get_undefined(env, &promise);
77 }
78 napi_async_work work = context_->work;
79 napi_value resource = nullptr;
80 std::string name = "THEME_" + resourceName;
81 napi_create_string_utf8(env, name.c_str(), NAPI_AUTO_LENGTH, &resource);
82 napi_create_async_work(env, nullptr, resource, AsyncCall::OnExecute, AsyncCall::OnComplete, context_, &work);
83 context_->work = work;
84 context_ = nullptr;
85 napi_queue_async_work_with_qos(env, work, napi_qos_user_initiated);
86 SCLOCK_HILOGD("async call exec");
87 return promise;
88 }
89
SyncCall(const napi_env env,AsyncCall::Context::ExecAction exec)90 napi_value AsyncCall::SyncCall(const napi_env env, AsyncCall::Context::ExecAction exec)
91 {
92 if ((context_ == nullptr) || (context_->ctx == nullptr)) {
93 SCLOCK_HILOGD("context_ or context_->ctx is null");
94 return nullptr;
95 }
96 context_->ctx->exec_ = std::move(exec);
97 napi_value promise = nullptr;
98 if (context_->callback == nullptr) {
99 napi_create_promise(env, &context_->defer, &promise);
100 } else {
101 napi_get_undefined(env, &promise);
102 }
103 AsyncCall::OnExecute(env, context_);
104 AsyncCall::OnComplete(env, napi_ok, context_);
105 context_ = nullptr;
106 return promise;
107 }
108
OnExecute(const napi_env env,void * data)109 void AsyncCall::OnExecute(const napi_env env, void *data)
110 {
111 SCLOCK_HILOGD("run the async runnable");
112 AsyncContext *context = reinterpret_cast<AsyncContext *>(data);
113 context->ctx->Exec();
114 }
115
OnComplete(const napi_env env,napi_status status,void * data)116 void AsyncCall::OnComplete(const napi_env env, napi_status status, void *data)
117 {
118 AsyncContext *context = reinterpret_cast<AsyncContext *>(data);
119 napi_value output = nullptr;
120 napi_status runStatus = (*context->ctx)(env, &output);
121 napi_value result[static_cast<uint32_t>(ARG_INFO::ARG_BUTT)] = { nullptr };
122 SCLOCK_HILOGD("run the js callback function:status[%{public}d]runStatus[%{public}d]", status, runStatus);
123 if (status == napi_ok && runStatus == napi_ok) {
124 napi_get_null(env, &result[static_cast<uint32_t>(ARG_INFO::ARG_ERROR)]);
125 if (output != nullptr) {
126 SCLOCK_HILOGD("AsyncCall::OnComplete output != nullptr");
127 result[static_cast<uint32_t>(ARG_INFO::ARG_DATA)] = output;
128 } else {
129 SCLOCK_HILOGD("AsyncCall::OnComplete output == nullptr");
130 napi_get_undefined(env, &result[static_cast<uint32_t>(ARG_INFO::ARG_DATA)]);
131 }
132 } else {
133 napi_get_undefined(env, &result[static_cast<uint32_t>(ARG_INFO::ARG_DATA)]);
134 GenerateBusinessError(env, context->ctx->errorInfo_, &result[static_cast<uint32_t>(ARG_INFO::ARG_ERROR)]);
135 }
136 if (context->defer != nullptr) {
137 // promise
138 SCLOCK_HILOGD("Promise to do!");
139 if (status == napi_ok && runStatus == napi_ok) {
140 napi_resolve_deferred(env, context->defer, result[static_cast<uint32_t>(ARG_INFO::ARG_DATA)]);
141 } else {
142 napi_reject_deferred(env, context->defer, result[static_cast<uint32_t>(ARG_INFO::ARG_ERROR)]);
143 }
144 } else {
145 // callback
146 SCLOCK_HILOGD("Callback to do!");
147 napi_value callback = nullptr;
148 napi_get_reference_value(env, context->callback, &callback);
149 napi_value returnValue;
150 napi_call_function(env, nullptr, callback, static_cast<size_t>(ARG_INFO::ARG_BUTT), result, &returnValue);
151 }
152 delete context;
153 }
154
~AsyncContext()155 AsyncCall::AsyncContext::~AsyncContext()
156 {
157 if (env != nullptr) {
158 napi_delete_reference(env, callback);
159 napi_delete_reference(env, self);
160 napi_delete_async_work(env, work);
161 }
162 delete ctx;
163 }
164
GenerateBusinessError(napi_env env,const ErrorInfo & errorInfo,napi_value * result)165 void AsyncCall::GenerateBusinessError(napi_env env, const ErrorInfo &errorInfo, napi_value *result)
166 {
167 napi_value message = nullptr;
168 napi_value code = nullptr;
169 if (errorInfo.errorCode_ != 0) {
170 napi_create_int32(env, errorInfo.errorCode_, &code);
171 }
172 napi_create_string_utf8(env, errorInfo.message_.c_str(), NAPI_AUTO_LENGTH, &message);
173 napi_create_object(env, result);
174 napi_set_named_property(env, *result, "code", code);
175 napi_set_named_property(env, *result, "message", message);
176 }
177 } // namespace OHOS::ScreenLock