1 /*
2 * Copyright (c) 2021 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 "async_call.h"
17
18 #include "global.h"
19 #include "js_utils.h"
20
21 namespace OHOS {
22 namespace MiscServices {
AsyncCall(napi_env env,napi_callback_info info,std::shared_ptr<Context> context,size_t pos)23 AsyncCall::AsyncCall(napi_env env, napi_callback_info info, std::shared_ptr<Context> context, size_t pos)
24 : env_(env)
25 {
26 context_ = new AsyncContext();
27 size_t argc = ARGC_MAX;
28 napi_value self = nullptr;
29 napi_value argv[ARGC_MAX] = {nullptr};
30 NAPI_CALL_RETURN_VOID(env, napi_get_cb_info(env, info, &argc, argv, &self, nullptr));
31 pos = ((pos == ASYNC_DEFAULT_POS) ? (argc - 1) : pos);
32 if (pos >= 0 && pos < argc) {
33 napi_valuetype valueType = napi_undefined;
34 napi_typeof(env, argv[pos], &valueType);
35 if (valueType == napi_function) {
36 napi_create_reference(env, argv[pos], 1, &context_->callback);
37 argc = pos;
38 }
39 }
40 NAPI_CALL_RETURN_VOID(env, (*context)(env, argc, argv, self));
41 context_->ctx = std::move(context);
42 napi_create_reference(env, self, 1, &context_->self);
43 }
44
~AsyncCall()45 AsyncCall::~AsyncCall()
46 {
47 if (context_ == nullptr) {
48 return;
49 }
50
51 DeleteContext(env_, context_);
52 }
53
Call(napi_env env,Context::ExecAction exec)54 napi_value AsyncCall::Call(napi_env env, Context::ExecAction exec)
55 {
56 if (context_ == nullptr) {
57 IMSA_HILOGE("context_ is null");
58 return nullptr;
59 }
60 if (context_->ctx == nullptr) {
61 IMSA_HILOGE("context_->ctx is null");
62 return nullptr;
63 }
64 context_->ctx->exec_ = std::move(exec);
65 napi_value promise = nullptr;
66 if (context_->callback == nullptr) {
67 napi_create_promise(env, &context_->defer, &promise);
68 } else {
69 napi_get_undefined(env, &promise);
70 }
71 napi_async_work work = context_->work;
72 napi_value resource = nullptr;
73 napi_create_string_utf8(env, "AsyncCall", NAPI_AUTO_LENGTH, &resource);
74 napi_create_async_work(env, nullptr, resource, AsyncCall::OnExecute, AsyncCall::OnComplete, context_, &work);
75 context_->work = work;
76 context_ = nullptr;
77 napi_queue_async_work(env, work);
78 return promise;
79 }
80
SyncCall(napi_env env,AsyncCall::Context::ExecAction exec)81 napi_value AsyncCall::SyncCall(napi_env env, AsyncCall::Context::ExecAction exec)
82 {
83 if ((context_ == nullptr) || (context_->ctx == nullptr)) {
84 IMSA_HILOGE("context_ or context_->ctx is null");
85 return nullptr;
86 }
87 context_->ctx->exec_ = std::move(exec);
88 napi_value promise = nullptr;
89 if (context_->callback == nullptr) {
90 napi_create_promise(env, &context_->defer, &promise);
91 } else {
92 napi_get_undefined(env, &promise);
93 }
94 AsyncCall::OnExecute(env, context_);
95 AsyncCall::OnComplete(env, context_->ctx->status_, context_);
96 return promise;
97 }
98
OnExecute(napi_env env,void * data)99 void AsyncCall::OnExecute(napi_env env, void *data)
100 {
101 AsyncContext *context = reinterpret_cast<AsyncContext *>(data);
102 context->ctx->Exec();
103 }
104
OnComplete(napi_env env,napi_status status,void * data)105 void AsyncCall::OnComplete(napi_env env, napi_status status, void *data)
106 {
107 AsyncContext *context = reinterpret_cast<AsyncContext *>(data);
108 napi_value output = nullptr;
109 napi_status runStatus = (*context->ctx)(env, &output);
110 napi_value result[ARG_BUTT] = { 0 };
111 IMSA_HILOGE("run the js callback function:status[%{public}d]runStatus[%{public}d]", status, runStatus);
112 if (status == napi_ok && runStatus == napi_ok) {
113 napi_get_undefined(env, &result[ARG_ERROR]);
114 if (output != nullptr) {
115 IMSA_HILOGE("AsyncCall::OnComplete output != nullptr");
116 result[ARG_DATA] = output;
117 } else {
118 IMSA_HILOGE("AsyncCall::OnComplete output == nullptr");
119 napi_get_undefined(env, &result[ARG_DATA]);
120 }
121 } else {
122 result[ARG_ERROR] = JsUtils::ToError(env, context->ctx->errorCode_);
123 napi_get_undefined(env, &result[ARG_DATA]);
124 }
125 if (context->defer != nullptr) {
126 if (status == napi_ok && runStatus == napi_ok) {
127 napi_resolve_deferred(env, context->defer, result[ARG_DATA]);
128 } else {
129 napi_reject_deferred(env, context->defer, result[ARG_ERROR]);
130 }
131 } else {
132 napi_value callback = nullptr;
133 napi_get_reference_value(env, context->callback, &callback);
134 napi_value returnValue;
135 napi_call_function(env, nullptr, callback, ARG_BUTT, result, &returnValue);
136 }
137 DeleteContext(env, context);
138 }
139
DeleteContext(napi_env env,AsyncContext * context)140 void AsyncCall::DeleteContext(napi_env env, AsyncContext *context)
141 {
142 if (env != nullptr) {
143 napi_delete_reference(env, context->callback);
144 napi_delete_reference(env, context->self);
145 napi_delete_async_work(env, context->work);
146 }
147 delete context;
148 }
149 } // namespace MiscServices
150 } // namespace OHOS
151