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