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 "napi_async_call.h"
17 #include "js_logger.h"
18
19 namespace OHOS {
20 namespace PreferencesJsKit {
SetAction(napi_env env,napi_callback_info info,InputAction input,ExecuteAction exec,OutputAction output)21 void BaseContext::SetAction(
22 napi_env env, napi_callback_info info, InputAction input, ExecuteAction exec, OutputAction output)
23 {
24 env_ = env;
25 size_t argc = MAX_INPUT_COUNT;
26 napi_value self = nullptr;
27 napi_value argv[MAX_INPUT_COUNT] = { nullptr };
28 NAPI_CALL_RETURN_VOID(env, napi_get_cb_info(env, info, &argc, argv, &self, nullptr));
29
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_CALL_RETURN_VOID(env, napi_create_reference(env, argv[argc - 1], 1, &callback_));
35 argc = argc - 1;
36 }
37 }
38 // int -->input_(env, argc, argv, self)
39 int status = input(env, argc, argv, self);
40
41 // if input return is not ok, then napi_throw_error context error
42 PRE_NAPI_ASSERT_RETURN_VOID(env, status == OK, error);
43
44 output_ = std::move(output);
45 exec_ = std::move(exec);
46
47 napi_create_reference(env, self, 1, &self_);
48 }
49
SetError(std::shared_ptr<Error> err)50 void BaseContext::SetError(std::shared_ptr<Error> err)
51 {
52 error = err;
53 }
54
~BaseContext()55 BaseContext::~BaseContext()
56 {
57 if (env_ == nullptr) {
58 return;
59 }
60 if (work_ != nullptr) {
61 napi_delete_async_work(env_, work_);
62 }
63 if (callback_ != nullptr) {
64 napi_delete_reference(env_, callback_);
65 }
66 napi_delete_reference(env_, self_);
67 env_ = nullptr;
68 }
69
SetBusinessError(napi_env env,napi_value * businessError,std::shared_ptr<Error> error)70 void AsyncCall::SetBusinessError(napi_env env, napi_value *businessError, std::shared_ptr<Error> error)
71 {
72 LOG_DEBUG("SetBusinessError enter");
73 napi_value code = nullptr;
74 napi_value msg = nullptr;
75 napi_create_object(env, businessError);
76 // if error is not inner error
77 if (error != nullptr && error->GetCode() != E_PARAM_ERROR) {
78 napi_create_int32(env, error->GetCode(), &code);
79 napi_create_string_utf8(env, error->GetMessage().c_str(), NAPI_AUTO_LENGTH, &msg);
80 napi_set_named_property(env, *businessError, "code", code);
81 napi_set_named_property(env, *businessError, "message", msg);
82 }
83 }
84
Call(napi_env env,std::shared_ptr<BaseContext> context)85 napi_value AsyncCall::Call(napi_env env, std::shared_ptr<BaseContext> context)
86 {
87 napi_value promise = nullptr;
88 if (context->callback_ == nullptr) {
89 napi_create_promise(env, &context->defer_, &promise);
90 } else {
91 napi_get_undefined(env, &promise);
92 }
93 context->keep_ = context;
94 napi_value resource = nullptr;
95 napi_create_string_utf8(env, "AsyncCall", NAPI_AUTO_LENGTH, &resource);
96 // create async work, execute function is OnExecute, complete function is OnComplete
97 napi_create_async_work(env, nullptr, resource, AsyncCall::OnExecute, AsyncCall::OnComplete,
98 reinterpret_cast<void *>(context.get()), &context->work_);
99 // add async work to execute queue
100 napi_queue_async_work(env, context->work_);
101 return promise;
102 }
103
OnExecute(napi_env env,void * data)104 void AsyncCall::OnExecute(napi_env env, void *data)
105 {
106 BaseContext *context = reinterpret_cast<BaseContext *>(data);
107 if (context->exec_) {
108 context->execStatus = context->exec_();
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 BaseContext *context = reinterpret_cast<BaseContext *>(data);
115 napi_value output = nullptr;
116 int outStatus = ERR;
117 // if async execute status is not napi_ok then un-execute out function
118 if ((context->execStatus == OK) && context->output_) {
119 outStatus = context->output_(env, output);
120 }
121 napi_value result[ARG_BUTT] = { 0 };
122 // if out function status is ok then async renturn output data, else return error.
123 if (outStatus == OK) {
124 napi_get_undefined(env, &result[ARG_ERROR]);
125 if (output != nullptr) {
126 result[ARG_DATA] = output;
127 } else {
128 napi_get_undefined(env, &result[ARG_DATA]);
129 }
130 } else {
131 napi_value businessError = nullptr;
132 SetBusinessError(env, &businessError, context->error);
133 result[ARG_ERROR] = businessError;
134 napi_get_undefined(env, &result[ARG_DATA]);
135 }
136 if (context->defer_ != nullptr) {
137 // promise
138 if (status == napi_ok && outStatus == OK) {
139 napi_resolve_deferred(env, context->defer_, result[ARG_DATA]);
140 } else {
141 napi_reject_deferred(env, context->defer_, result[ARG_ERROR]);
142 }
143 } else {
144 // callback
145 napi_value callback = nullptr;
146 napi_get_reference_value(env, context->callback_, &callback);
147 napi_value returnValue;
148 napi_call_function(env, nullptr, callback, ARG_BUTT, result, &returnValue);
149 }
150 context->exec_ = nullptr;
151 context->output_ = nullptr;
152 context->keep_.reset();
153 }
154 } // namespace PreferencesJsKit
155 } // namespace OHOS