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 "log_print.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_status status = napi_get_cb_info(env, info, &argc, argv, &self, nullptr);
29 napi_valuetype valueType = napi_undefined;
30 if (status == napi_ok && argc > 0) {
31 napi_typeof(env, argv[argc - 1], &valueType);
32 if (valueType == napi_function) {
33 status = napi_create_reference(env, argv[argc - 1], 1, &callback_);
34 argc = argc - 1;
35 }
36 }
37
38 // int -->input_(env, argc, argv, self)
39 if (status == napi_ok) {
40 input(env, argc, argv, self);
41 } else {
42 error = std::make_shared<InnerError>(NativePreferences::E_ERROR);
43 }
44
45 // if input return is not ok, then napi_throw_error context error
46 PRE_NAPI_ASSERT_RETURN_VOID(env, error == nullptr, error);
47
48 output_ = std::move(output);
49 exec_ = std::move(exec);
50
51 napi_create_reference(env, self, 1, &self_);
52 }
53
SetError(std::shared_ptr<JSError> err)54 void BaseContext::SetError(std::shared_ptr<JSError> err)
55 {
56 error = err;
57 }
58
~BaseContext()59 BaseContext::~BaseContext()
60 {
61 if (env_ == nullptr) {
62 return;
63 }
64 if (work_ != nullptr) {
65 napi_delete_async_work(env_, work_);
66 }
67 if (callback_ != nullptr) {
68 napi_delete_reference(env_, callback_);
69 }
70 napi_delete_reference(env_, self_);
71 env_ = nullptr;
72 }
73
SetBusinessError(napi_env env,napi_value * businessError,std::shared_ptr<JSError> error)74 void AsyncCall::SetBusinessError(napi_env env, napi_value *businessError, std::shared_ptr<JSError> error)
75 {
76 napi_value code = nullptr;
77 napi_value msg = nullptr;
78 napi_create_object(env, businessError);
79 // if error is not inner error
80 if (error != nullptr && error->GetCode() != E_INVALID_PARAM) {
81 napi_create_int32(env, error->GetCode(), &code);
82 napi_create_string_utf8(env, error->GetMsg().c_str(), NAPI_AUTO_LENGTH, &msg);
83 napi_set_named_property(env, *businessError, "code", code);
84 napi_set_named_property(env, *businessError, "message", msg);
85 }
86 }
87
Call(napi_env env,std::shared_ptr<BaseContext> context)88 napi_value AsyncCall::Call(napi_env env, std::shared_ptr<BaseContext> context)
89 {
90 napi_value promise = nullptr;
91
92 if (context->callback_ == nullptr) {
93 napi_create_promise(env, &context->defer_, &promise);
94 } else {
95 napi_get_undefined(env, &promise);
96 }
97 context->keep_ = context;
98 napi_value resource = nullptr;
99 napi_create_string_utf8(env, "PreferencesAsyncCall", NAPI_AUTO_LENGTH, &resource);
100 // create async work, execute function is OnExecute, complete function is OnComplete
101 napi_create_async_work(env, nullptr, resource, AsyncCall::OnExecute, AsyncCall::OnComplete,
102 reinterpret_cast<void *>(context.get()), &context->work_);
103 // add async work to execute queue
104 napi_queue_async_work(env, context->work_);
105 return promise;
106 }
107
OnExecute(napi_env env,void * data)108 void AsyncCall::OnExecute(napi_env env, void *data)
109 {
110 BaseContext *context = reinterpret_cast<BaseContext *>(data);
111 if (context->exec_) {
112 context->execCode_ = context->exec_();
113 }
114 context->exec_ = nullptr;
115 }
116
OnComplete(napi_env env,napi_status status,void * data)117 void AsyncCall::OnComplete(napi_env env, napi_status status, void *data)
118 {
119 BaseContext *context = reinterpret_cast<BaseContext *>(data);
120 if (context->execCode_ != NativePreferences::E_OK) {
121 context->SetError(std::make_shared<InnerError>(context->execCode_));
122 }
123 napi_value output = nullptr;
124 // if async execute status is not napi_ok then un-execute out function
125 if ((context->error == nullptr) && context->output_) {
126 context->output_(env, output);
127 }
128 context->output_ = nullptr;
129 napi_value result[ARG_BUTT] = { 0 };
130 // if out function status is ok then async renturn output data, else return error.
131 if (context->error == nullptr) {
132 napi_get_undefined(env, &result[ARG_ERROR]);
133 if (output != nullptr) {
134 result[ARG_DATA] = output;
135 } else {
136 napi_get_undefined(env, &result[ARG_DATA]);
137 }
138 } else {
139 SetBusinessError(env, &result[ARG_ERROR], context->error);
140 napi_get_undefined(env, &result[ARG_DATA]);
141 }
142 if (context->defer_ != nullptr) {
143 // promise
144 if (status == napi_ok && (context->error == nullptr)) {
145 napi_resolve_deferred(env, context->defer_, result[ARG_DATA]);
146 } else {
147 napi_reject_deferred(env, context->defer_, result[ARG_ERROR]);
148 }
149 } else {
150 // callback
151 napi_value callback = nullptr;
152 napi_get_reference_value(env, context->callback_, &callback);
153 napi_value returnValue;
154 napi_call_function(env, nullptr, callback, ARG_BUTT, result, &returnValue);
155 }
156 context->keep_.reset();
157 }
158 } // namespace PreferencesJsKit
159 } // namespace OHOS