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
16 #include "async_call.h"
17
18 #include "datashare_log.h"
19 #include "napi_common_data.h"
20
21 namespace OHOS::DataShare {
AsyncCall(napi_env env,napi_callback_info info,std::shared_ptr<Context> context)22 __attribute__((no_sanitize("undefined"))) AsyncCall::AsyncCall(napi_env env, napi_callback_info info,
23 std::shared_ptr<Context> context) : env_(env)
24 {
25 context_ = new AsyncContext();
26 size_t argc = ARGS_MAX_COUNT;
27 napi_value self = nullptr;
28 napi_value argv[ARGS_MAX_COUNT] = {nullptr};
29 NAPI_CALL_RETURN_VOID(env, napi_get_cb_info(env, info, &argc, argv, &self, nullptr));
30 napi_valuetype valueType = napi_undefined;
31 if (argc > 0) {
32 napi_typeof(env, argv[argc - 1], &valueType);
33 if (valueType == napi_function) {
34 napi_create_reference(env, argv[argc - 1], 1, &context_->callback);
35 argc = argc - 1;
36 }
37 } else {
38 LOG_DEBUG("get argc value less than zero");
39 }
40 napi_status status = (*context)(env, argc, argv, self);
41 NAPI_ASSERT_ERRCODE(env, status == napi_ok, context->error);
42 context_->ctx = std::move(context);
43 napi_create_reference(env, self, 1, &context_->self);
44 }
45
~AsyncCall()46 AsyncCall::~AsyncCall()
47 {
48 if (context_ == nullptr) {
49 return;
50 }
51
52 DeleteContext(env_, context_);
53 context_ = nullptr;
54 }
55
Call(napi_env env,Context::ExecAction exec)56 napi_value AsyncCall::Call(napi_env env, Context::ExecAction exec)
57 {
58 if ((context_ == nullptr) || (context_->ctx == nullptr)) {
59 LOG_DEBUG("context_ or context_->ctx is null");
60 return nullptr;
61 }
62 context_->ctx->exec_ = std::move(exec);
63 napi_value promise = nullptr;
64 if (context_->callback == nullptr) {
65 napi_create_promise(env, &context_->defer, &promise);
66 } else {
67 napi_get_undefined(env, &promise);
68 }
69 napi_async_work work = context_->work;
70 napi_value resource = nullptr;
71 napi_create_string_utf8(env, "DataShareAsyncCall", NAPI_AUTO_LENGTH, &resource);
72 napi_create_async_work(env, nullptr, resource, AsyncCall::OnExecute, AsyncCall::OnComplete, context_, &work);
73 context_->work = work;
74 context_ = nullptr;
75 napi_queue_async_work_with_qos(env, work, napi_qos_user_initiated);
76 return promise;
77 }
78
SyncCall(napi_env env,AsyncCall::Context::ExecAction exec)79 napi_value AsyncCall::SyncCall(napi_env env, AsyncCall::Context::ExecAction exec)
80 {
81 if ((context_ == nullptr) || (context_->ctx == nullptr)) {
82 LOG_DEBUG("context_ or context_->ctx is null");
83 return nullptr;
84 }
85 context_->ctx->exec_ = std::move(exec);
86 napi_value promise = nullptr;
87 if (context_->callback == nullptr) {
88 napi_create_promise(env, &context_->defer, &promise);
89 } else {
90 napi_get_undefined(env, &promise);
91 }
92 AsyncCall::OnExecute(env, context_);
93 AsyncCall::OnComplete(env, napi_ok, context_);
94 return promise;
95 }
96
OnExecute(napi_env env,void * data)97 void AsyncCall::OnExecute(napi_env env, void *data)
98 {
99 AsyncContext *context = reinterpret_cast<AsyncContext *>(data);
100 context->ctx->Exec();
101 }
102
SetBusinessError(napi_env env,napi_value * businessError,std::shared_ptr<Error> error)103 void SetBusinessError(napi_env env, napi_value *businessError, std::shared_ptr<Error> error)
104 {
105 napi_create_object(env, businessError);
106 if (error != nullptr) {
107 napi_value code = nullptr;
108 napi_value msg = nullptr;
109 napi_create_int32(env, error->GetCode(), &code);
110 napi_create_string_utf8(env, error->GetMessage().c_str(), NAPI_AUTO_LENGTH, &msg);
111 napi_set_named_property(env, *businessError, "code", code);
112 napi_set_named_property(env, *businessError, "message", msg);
113 }
114 }
115
OnComplete(napi_env env,napi_status status,void * data)116 void AsyncCall::OnComplete(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[ARG_BUTT] = { 0 };
122 if (status == napi_ok && runStatus == napi_ok) {
123 napi_get_undefined(env, &result[ARG_ERROR]);
124 if (output != nullptr) {
125 result[ARG_DATA] = output;
126 } else {
127 napi_get_undefined(env, &result[ARG_DATA]);
128 }
129 } else {
130 napi_value businessError = nullptr;
131 SetBusinessError(env, &businessError, context->ctx->error);
132 result[ARG_ERROR] = businessError;
133 napi_get_undefined(env, &result[ARG_DATA]);
134 }
135 if (context->defer != nullptr) {
136 // promise
137 if (status == napi_ok && runStatus == napi_ok) {
138 napi_resolve_deferred(env, context->defer, result[ARG_DATA]);
139 } else {
140 napi_reject_deferred(env, context->defer, result[ARG_ERROR]);
141 }
142 } else {
143 // callback
144 napi_value callback = nullptr;
145 napi_get_reference_value(env, context->callback, &callback);
146 napi_value returnValue;
147 napi_call_function(env, nullptr, callback, ARG_BUTT, result, &returnValue);
148 }
149 DeleteContext(env, context);
150 }
151
DeleteContext(napi_env env,AsyncContext * context)152 void AsyncCall::DeleteContext(napi_env env, AsyncContext *context)
153 {
154 if (env != nullptr && context != nullptr) {
155 napi_delete_reference(env, context->callback);
156 napi_delete_reference(env, context->self);
157 napi_delete_async_work(env, context->work);
158 }
159 if (context != nullptr && context->ctx != nullptr) {
160 context->ctx->exec_ = nullptr;
161 context->ctx->input_ = nullptr;
162 context->ctx->output_ = nullptr;
163 context->ctx = nullptr;
164 }
165 delete context;
166 }
167 }