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 AsyncCall::AsyncCall(napi_env env, napi_callback_info info, std::shared_ptr<Context> context)
23 : 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 napi_typeof(env, argv[argc - 1], &valueType);
32 if (valueType == napi_function) {
33 napi_create_reference(env, argv[argc - 1], 1, &context_->callback);
34 argc = argc - 1;
35 }
36 napi_status status = (*context)(env, argc, argv, self);
37 NAPI_ASSERT_ERRCODE(env, status == napi_ok, context->error);
38 context_->ctx = std::move(context);
39 napi_create_reference(env, self, 1, &context_->self);
40 }
41
~AsyncCall()42 AsyncCall::~AsyncCall()
43 {
44 if (context_ == nullptr) {
45 return;
46 }
47
48 DeleteContext(env_, context_);
49 context_ = nullptr;
50 }
51
Call(napi_env env,Context::ExecAction exec)52 napi_value AsyncCall::Call(napi_env env, Context::ExecAction exec)
53 {
54 if ((context_ == nullptr) || (context_->ctx == nullptr)) {
55 LOG_DEBUG("context_ or context_->ctx is null");
56 return nullptr;
57 }
58 LOG_DEBUG("async call exec");
59 context_->ctx->exec_ = std::move(exec);
60 napi_value promise = nullptr;
61 if (context_->callback == nullptr) {
62 napi_create_promise(env, &context_->defer, &promise);
63 } else {
64 napi_get_undefined(env, &promise);
65 }
66 napi_async_work work = context_->work;
67 napi_value resource = nullptr;
68 napi_create_string_utf8(env, "AsyncCall", NAPI_AUTO_LENGTH, &resource);
69 napi_create_async_work(env, nullptr, resource, AsyncCall::OnExecute, AsyncCall::OnComplete, context_, &work);
70 context_->work = work;
71 context_ = nullptr;
72 napi_queue_async_work(env, work);
73 LOG_DEBUG("async call exec");
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 LOG_DEBUG("run the async runnable");
98 AsyncContext *context = reinterpret_cast<AsyncContext *>(data);
99 context->ctx->Exec();
100 }
101
SetBusinessError(napi_env env,napi_value * businessError,std::shared_ptr<Error> error)102 void SetBusinessError(napi_env env, napi_value *businessError, std::shared_ptr<Error> error)
103 {
104 napi_create_object(env, businessError);
105 if (error->GetCode() != EXCEPTION_INNER) {
106 napi_value code = nullptr;
107 napi_value msg = nullptr;
108 napi_create_int32(env, error->GetCode(), &code);
109 napi_create_string_utf8(env, error->GetMessage().c_str(), NAPI_AUTO_LENGTH, &msg);
110 napi_set_named_property(env, *businessError, "code", code);
111 napi_set_named_property(env, *businessError, "message", msg);
112 }
113 }
114
OnComplete(napi_env env,napi_status status,void * data)115 void AsyncCall::OnComplete(napi_env env, napi_status status, void *data)
116 {
117 LOG_DEBUG("run the js callback function");
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) {
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 }