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 }
38 napi_status status = (*context)(env, argc, argv, self);
39 NAPI_ASSERT_ERRCODE(env, status == napi_ok, context->error);
40 context_->ctx = std::move(context);
41 napi_create_reference(env, self, 1, &context_->self);
42 }
43
~AsyncCall()44 AsyncCall::~AsyncCall()
45 {
46 if (context_ == nullptr) {
47 return;
48 }
49
50 DeleteContext(env_, context_);
51 context_ = nullptr;
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) || (context_->ctx == nullptr)) {
57 LOG_DEBUG("context_ or context_->ctx is null");
58 return nullptr;
59 }
60 context_->ctx->exec_ = std::move(exec);
61 napi_value promise = nullptr;
62 if (context_->callback == nullptr) {
63 napi_create_promise(env, &context_->defer, &promise);
64 } else {
65 napi_get_undefined(env, &promise);
66 }
67 napi_async_work work = context_->work;
68 napi_value resource = nullptr;
69 napi_create_string_utf8(env, "DataShareAsyncCall", NAPI_AUTO_LENGTH, &resource);
70 napi_create_async_work(env, nullptr, resource, AsyncCall::OnExecute, AsyncCall::OnComplete, context_, &work);
71 context_->work = work;
72 context_ = nullptr;
73 napi_queue_async_work_with_qos(env, work, napi_qos_user_initiated);
74 return promise;
75 }
76
SyncCall(napi_env env,AsyncCall::Context::ExecAction exec)77 napi_value AsyncCall::SyncCall(napi_env env, AsyncCall::Context::ExecAction exec)
78 {
79 if ((context_ == nullptr) || (context_->ctx == nullptr)) {
80 LOG_DEBUG("context_ or context_->ctx is null");
81 return nullptr;
82 }
83 context_->ctx->exec_ = std::move(exec);
84 napi_value promise = nullptr;
85 if (context_->callback == nullptr) {
86 napi_create_promise(env, &context_->defer, &promise);
87 } else {
88 napi_get_undefined(env, &promise);
89 }
90 AsyncCall::OnExecute(env, context_);
91 AsyncCall::OnComplete(env, napi_ok, context_);
92 return promise;
93 }
94
OnExecute(napi_env env,void * data)95 void AsyncCall::OnExecute(napi_env env, void *data)
96 {
97 AsyncContext *context = reinterpret_cast<AsyncContext *>(data);
98 context->ctx->Exec();
99 }
100
SetBusinessError(napi_env env,napi_value * businessError,std::shared_ptr<Error> error)101 void SetBusinessError(napi_env env, napi_value *businessError, std::shared_ptr<Error> error)
102 {
103 napi_create_object(env, businessError);
104 if (error != nullptr) {
105 napi_value code = nullptr;
106 napi_value msg = nullptr;
107 napi_create_int32(env, error->GetCode(), &code);
108 napi_create_string_utf8(env, error->GetMessage().c_str(), NAPI_AUTO_LENGTH, &msg);
109 napi_set_named_property(env, *businessError, "code", code);
110 napi_set_named_property(env, *businessError, "message", msg);
111 }
112 }
113
OnComplete(napi_env env,napi_status status,void * data)114 void AsyncCall::OnComplete(napi_env env, napi_status status, void *data)
115 {
116 AsyncContext *context = reinterpret_cast<AsyncContext *>(data);
117 napi_value output = nullptr;
118 napi_status runStatus = (*context->ctx)(env, &output);
119 napi_value result[ARG_BUTT] = { 0 };
120 if (status == napi_ok && runStatus == napi_ok) {
121 napi_get_undefined(env, &result[ARG_ERROR]);
122 if (output != nullptr) {
123 result[ARG_DATA] = output;
124 } else {
125 napi_get_undefined(env, &result[ARG_DATA]);
126 }
127 } else {
128 napi_value businessError = nullptr;
129 SetBusinessError(env, &businessError, context->ctx->error);
130 result[ARG_ERROR] = businessError;
131 napi_get_undefined(env, &result[ARG_DATA]);
132 }
133 if (context->defer != nullptr) {
134 // promise
135 if (status == napi_ok && runStatus == napi_ok) {
136 napi_resolve_deferred(env, context->defer, result[ARG_DATA]);
137 } else {
138 napi_reject_deferred(env, context->defer, result[ARG_ERROR]);
139 }
140 } else {
141 // callback
142 napi_value callback = nullptr;
143 napi_get_reference_value(env, context->callback, &callback);
144 napi_value returnValue;
145 napi_call_function(env, nullptr, callback, ARG_BUTT, result, &returnValue);
146 }
147 DeleteContext(env, context);
148 }
149
DeleteContext(napi_env env,AsyncContext * context)150 void AsyncCall::DeleteContext(napi_env env, AsyncContext *context)
151 {
152 if (env != nullptr && context != nullptr) {
153 napi_delete_reference(env, context->callback);
154 napi_delete_reference(env, context->self);
155 napi_delete_async_work(env, context->work);
156 }
157 if (context != nullptr && context->ctx != nullptr) {
158 context->ctx->exec_ = nullptr;
159 context->ctx->input_ = nullptr;
160 context->ctx->output_ = nullptr;
161 context->ctx = nullptr;
162 }
163 delete context;
164 }
165 }