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