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