• 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 #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)62 napi_value AsyncCall::Call(const napi_env env, Context::ExecAction exec)
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     napi_create_string_utf8(env, "AsyncCall", NAPI_AUTO_LENGTH, &resource);
83     napi_create_async_work(env, nullptr, resource, AsyncCall::OnExecute, AsyncCall::OnComplete, context_, &work);
84     context_->work = work;
85     context_ = nullptr;
86     napi_queue_async_work(env, work);
87     SCLOCK_HILOGD("async call exec");
88     return promise;
89 }
90 
SyncCall(const napi_env env,AsyncCall::Context::ExecAction exec)91 napi_value AsyncCall::SyncCall(const napi_env env, AsyncCall::Context::ExecAction exec)
92 {
93     if ((context_ == nullptr) || (context_->ctx == nullptr)) {
94         SCLOCK_HILOGD("context_ or context_->ctx is null");
95         return nullptr;
96     }
97     context_->ctx->exec_ = std::move(exec);
98     napi_value promise = nullptr;
99     if (context_->callback == nullptr) {
100         napi_create_promise(env, &context_->defer, &promise);
101     } else {
102         napi_get_undefined(env, &promise);
103     }
104     AsyncCall::OnExecute(env, context_);
105     AsyncCall::OnComplete(env, napi_ok, context_);
106     context_ = nullptr;
107     return promise;
108 }
109 
OnExecute(const napi_env env,void * data)110 void AsyncCall::OnExecute(const napi_env env, void *data)
111 {
112     SCLOCK_HILOGD("run the async runnable");
113     AsyncContext *context = reinterpret_cast<AsyncContext *>(data);
114     context->ctx->Exec();
115 }
116 
OnComplete(const napi_env env,napi_status status,void * data)117 void AsyncCall::OnComplete(const napi_env env, napi_status status, void *data)
118 {
119     SCLOCK_HILOGD("run the js callback function");
120     AsyncContext *context = reinterpret_cast<AsyncContext *>(data);
121     napi_value output = nullptr;
122     napi_status runStatus = (*context->ctx)(env, &output);
123     napi_value result[static_cast<uint32_t>(ARG_INFO::ARG_BUTT)] = { nullptr };
124     SCLOCK_HILOGD("run the js callback function:status[%{public}d]runStatus[%{public}d]", status, runStatus);
125     if (status == napi_ok && runStatus == napi_ok) {
126         napi_get_undefined(env, &result[static_cast<uint32_t>(ARG_INFO::ARG_ERROR)]);
127         if (output != nullptr) {
128             SCLOCK_HILOGD("AsyncCall::OnComplete output != nullptr");
129             result[static_cast<uint32_t>(ARG_INFO::ARG_DATA)] = output;
130         } else {
131             SCLOCK_HILOGD("AsyncCall::OnComplete output == nullptr");
132             napi_get_undefined(env, &result[static_cast<uint32_t>(ARG_INFO::ARG_DATA)]);
133         }
134     } else {
135         napi_get_undefined(env, &result[static_cast<uint32_t>(ARG_INFO::ARG_DATA)]);
136         GenerateBusinessError(env, context->ctx->errorInfo_, &result[static_cast<uint32_t>(ARG_INFO::ARG_ERROR)]);
137     }
138     SCLOCK_HILOGD("run the js callback function:(context->defer != nullptr)?[%{public}d]", context->defer != nullptr);
139     if (context->defer != nullptr) {
140         // promise
141         SCLOCK_HILOGD("Promise to do!");
142         if (status == napi_ok && runStatus == napi_ok) {
143             napi_resolve_deferred(env, context->defer, result[static_cast<uint32_t>(ARG_INFO::ARG_DATA)]);
144         } else {
145             napi_reject_deferred(env, context->defer, result[static_cast<uint32_t>(ARG_INFO::ARG_ERROR)]);
146         }
147     } else {
148         // callback
149         SCLOCK_HILOGD("Callback to do!");
150         napi_value callback = nullptr;
151         napi_get_reference_value(env, context->callback, &callback);
152         napi_value returnValue;
153         napi_call_function(env, nullptr, callback, static_cast<size_t>(ARG_INFO::ARG_BUTT), result, &returnValue);
154     }
155     delete context;
156 }
157 
~AsyncContext()158 AsyncCall::AsyncContext::~AsyncContext()
159 {
160     if (env != nullptr) {
161         napi_delete_reference(env, callback);
162         napi_delete_reference(env, self);
163         napi_delete_async_work(env, work);
164     }
165     delete ctx;
166 }
167 
GenerateBusinessError(napi_env env,const ErrorInfo & errorInfo,napi_value * result)168 void AsyncCall::GenerateBusinessError(napi_env env, const ErrorInfo &errorInfo, napi_value *result)
169 {
170     napi_value message = nullptr;
171     napi_value code = nullptr;
172     napi_create_int32(env, errorInfo.errorCode_, &code);
173     napi_create_string_utf8(env, errorInfo.message_.c_str(), NAPI_AUTO_LENGTH, &message);
174     napi_create_object(env, result);
175     napi_set_named_property(env, *result, "code", code);
176     napi_set_named_property(env, *result, "message", message);
177 }
178 } // namespace OHOS::ScreenLock