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_edm_common.h"
17
18 #include "edm_errors.h"
19 #include "edm_log.h"
20 #include "js_native_api.h"
21 #include "js_native_api_types.h"
22 #include "napi_edm_error.h"
23 #include "napi/native_api.h"
24 #include "napi/native_common.h"
25 #include "napi/native_node_api.h"
26
27 namespace OHOS {
28 namespace EDM {
MatchValueType(napi_env env,napi_value value,napi_valuetype targetType)29 bool MatchValueType(napi_env env, napi_value value, napi_valuetype targetType)
30 {
31 napi_valuetype valueType = napi_undefined;
32 napi_typeof(env, value, &valueType);
33 return valueType == targetType;
34 }
35
ParseElementName(napi_env env,AppExecFwk::ElementName & elementName,napi_value args)36 bool ParseElementName(napi_env env, AppExecFwk::ElementName &elementName, napi_value args)
37 {
38 napi_valuetype valueType;
39 NAPI_CALL_BASE(env, napi_typeof(env, args, &valueType), false);
40 if (valueType != napi_object) {
41 EDMLOGE("Parameter element valueType error");
42 return false;
43 }
44 std::string bundleName;
45 std::string abilityName;
46 napi_value prop = nullptr;
47 if (napi_get_named_property(env, args, "bundleName", &prop) != napi_ok ||
48 !GetStringFromNAPI(env, prop, bundleName)) {
49 EDMLOGE("Parameter element bundleName error");
50 return false;
51 }
52 EDMLOGD("ParseElementName bundleName %{public}s ", bundleName.c_str());
53
54 prop = nullptr;
55 if (napi_get_named_property(env, args, "abilityName", &prop) != napi_ok ||
56 !GetStringFromNAPI(env, prop, abilityName)) {
57 EDMLOGE("Parameter abilityname error");
58 return false;
59 }
60 EDMLOGD("ParseElementName abilityname %{public}s", abilityName.c_str());
61
62 elementName.SetBundleName(bundleName);
63 elementName.SetAbilityName(abilityName);
64 return true;
65 }
66
ParseLong(napi_env env,int64_t & param,napi_value args)67 bool ParseLong(napi_env env, int64_t ¶m, napi_value args)
68 {
69 napi_valuetype valueType = napi_undefined;
70 if (napi_typeof(env, args, &valueType)!= napi_ok ||
71 valueType != napi_number || napi_get_value_int64(env, args, ¶m) != napi_ok) {
72 EDMLOGE("Wrong argument type. int64 expected.");
73 return false;
74 }
75 return true;
76 }
77
ParseInt(napi_env env,int32_t & param,napi_value args)78 bool ParseInt(napi_env env, int32_t ¶m, napi_value args)
79 {
80 napi_valuetype valueType = napi_undefined;
81 if (napi_typeof(env, args, &valueType)!= napi_ok ||
82 valueType != napi_number || napi_get_value_int32(env, args, ¶m) != napi_ok) {
83 EDMLOGE("Wrong argument type. int32 expected.");
84 return false;
85 }
86 return true;
87 }
88
ParseString(napi_env env,std::string & param,napi_value args)89 bool ParseString(napi_env env, std::string ¶m, napi_value args)
90 {
91 napi_valuetype valuetype;
92 if (napi_typeof(env, args, &valuetype) != napi_ok || valuetype != napi_string ||
93 !GetStringFromNAPI(env, args, param)) {
94 EDMLOGE("can not get string value");
95 return false;
96 }
97 EDMLOGD("ParseString param = %{public}s.", param.c_str());
98 return true;
99 }
100
GetStringFromNAPI(napi_env env,napi_value value,std::string & resultStr)101 bool GetStringFromNAPI(napi_env env, napi_value value, std::string &resultStr)
102 {
103 std::string result;
104 size_t size = 0;
105
106 if (napi_get_value_string_utf8(env, value, nullptr, NAPI_RETURN_ZERO, &size) != napi_ok) {
107 EDMLOGE("can not get string size");
108 return false;
109 }
110 result.reserve(size + NAPI_RETURN_ONE);
111 result.resize(size);
112 if (napi_get_value_string_utf8(env, value, result.data(), (size + NAPI_RETURN_ONE), &size) != napi_ok) {
113 EDMLOGE("can not get string value");
114 return false;
115 }
116 resultStr = result;
117 return true;
118 }
119
NativeVoidCallbackComplete(napi_env env,napi_status status,void * data)120 void NativeVoidCallbackComplete(napi_env env, napi_status status, void *data)
121 {
122 if (data == nullptr) {
123 EDMLOGE("data is nullptr");
124 return;
125 }
126 AsyncCallbackInfo *asyncCallbackInfo = static_cast<AsyncCallbackInfo *>(data);
127 napi_value error = nullptr;
128 if (asyncCallbackInfo->callback == nullptr) {
129 EDMLOGD("asyncCallbackInfo->deferred != nullptr");
130 if (asyncCallbackInfo->ret == ERR_OK) {
131 napi_get_null(env, &error);
132 napi_resolve_deferred(env, asyncCallbackInfo->deferred, error);
133 } else {
134 napi_reject_deferred(env, asyncCallbackInfo->deferred, CreateError(env, asyncCallbackInfo->ret));
135 }
136 } else {
137 if (asyncCallbackInfo->ret == ERR_OK) {
138 napi_get_null(env, &error);
139 } else {
140 error = CreateError(env, asyncCallbackInfo->ret);
141 }
142 napi_value callback = nullptr;
143 napi_value result = nullptr;
144 napi_get_reference_value(env, asyncCallbackInfo->callback, &callback);
145 napi_call_function(env, nullptr, callback, ARGS_SIZE_ONE, &error, &result);
146 napi_delete_reference(env, asyncCallbackInfo->callback);
147 }
148 napi_delete_async_work(env, asyncCallbackInfo->asyncWork);
149 delete asyncCallbackInfo;
150 }
151
HandleAsyncWork(napi_env env,AsyncCallbackInfo * context,const std::string & workName,napi_async_execute_callback execute,napi_async_complete_callback complete)152 napi_value HandleAsyncWork(napi_env env, AsyncCallbackInfo *context, const std::string &workName,
153 napi_async_execute_callback execute, napi_async_complete_callback complete)
154 {
155 napi_value result = nullptr;
156 if (context->callback == nullptr) {
157 napi_create_promise(env, &context->deferred, &result);
158 } else {
159 napi_get_undefined(env, &result);
160 }
161 napi_value resource = nullptr;
162 napi_get_undefined(env, &resource);
163 napi_value resourceName = nullptr;
164 napi_create_string_utf8(env, workName.data(), NAPI_AUTO_LENGTH, &resourceName);
165 napi_create_async_work(env, resource, resourceName, execute, complete,
166 static_cast<void *>(context), &context->asyncWork);
167 napi_queue_async_work(env, context->asyncWork);
168 return result;
169 }
170
NativeBoolCallbackComplete(napi_env env,napi_status status,void * data)171 void NativeBoolCallbackComplete(napi_env env, napi_status status, void *data)
172 {
173 if (data == nullptr) {
174 EDMLOGE("data is nullptr");
175 return;
176 }
177 AsyncCallbackInfo *asyncCallbackInfo = static_cast<AsyncCallbackInfo *>(data);
178 if (asyncCallbackInfo->deferred != nullptr) {
179 EDMLOGD("asyncCallbackInfo->deferred != nullptr");
180 if (asyncCallbackInfo->ret == ERR_OK) {
181 EDMLOGD("asyncCallbackInfo->boolRet = %{public}d", asyncCallbackInfo->boolRet);
182 napi_value result = nullptr;
183 napi_get_boolean(env, asyncCallbackInfo->boolRet, &result);
184 napi_resolve_deferred(env, asyncCallbackInfo->deferred, result);
185 } else {
186 napi_reject_deferred(env, asyncCallbackInfo->deferred, CreateError(env, asyncCallbackInfo->ret));
187 }
188 } else {
189 napi_value callbackValue[ARGS_SIZE_TWO] = { 0 };
190 if (asyncCallbackInfo->ret == ERR_OK) {
191 napi_get_null(env, &callbackValue[ARR_INDEX_ZERO]);
192 EDMLOGD("asyncCallbackInfo->boolRet = %{public}d", asyncCallbackInfo->boolRet);
193 napi_get_boolean(env, asyncCallbackInfo->boolRet, &callbackValue[ARR_INDEX_ONE]);
194 } else {
195 EDMLOGD("asyncCallbackInfo->first = %{public}u, second = %{public}s ",
196 GetMessageFromReturncode(asyncCallbackInfo->ret).first,
197 GetMessageFromReturncode(asyncCallbackInfo->ret).second.c_str());
198 callbackValue[ARR_INDEX_ZERO] = CreateError(env, asyncCallbackInfo->ret);
199 napi_get_null(env, &callbackValue[ARR_INDEX_ONE]);
200 }
201 napi_value callback = nullptr;
202 napi_value result = nullptr;
203 napi_get_reference_value(env, asyncCallbackInfo->callback, &callback);
204 napi_call_function(env, nullptr, callback, std::size(callbackValue), callbackValue, &result);
205 napi_delete_reference(env, asyncCallbackInfo->callback);
206 }
207 napi_delete_async_work(env, asyncCallbackInfo->asyncWork);
208 delete asyncCallbackInfo;
209 }
210
NativeStringCallbackComplete(napi_env env,napi_status status,void * data)211 void NativeStringCallbackComplete(napi_env env, napi_status status, void *data)
212 {
213 if (data == nullptr) {
214 EDMLOGE("data is nullptr");
215 return;
216 }
217 AsyncCallbackInfo *asyncCallbackInfo = static_cast<AsyncCallbackInfo *>(data);
218 if (asyncCallbackInfo->deferred != nullptr) {
219 EDMLOGD("asyncCallbackInfo->deferred != nullptr");
220 if (asyncCallbackInfo->ret == ERR_OK) {
221 EDMLOGD("asyncCallbackInfo->stringRet = %{public}s", asyncCallbackInfo->stringRet.c_str());
222 napi_value result = nullptr;
223 napi_create_string_utf8(env, asyncCallbackInfo->stringRet.c_str(), NAPI_AUTO_LENGTH, &result);
224 napi_resolve_deferred(env, asyncCallbackInfo->deferred, result);
225 } else {
226 napi_reject_deferred(env, asyncCallbackInfo->deferred, CreateError(env, asyncCallbackInfo->ret));
227 }
228 } else {
229 napi_value callbackValue[ARGS_SIZE_TWO] = { 0 };
230 if (asyncCallbackInfo->ret == ERR_OK) {
231 napi_get_null(env, &callbackValue[ARR_INDEX_ZERO]);
232 EDMLOGD("asyncCallbackInfo->stringRet = %{public}s", asyncCallbackInfo->stringRet.c_str());
233 napi_create_string_utf8(env, asyncCallbackInfo->stringRet.c_str(), NAPI_AUTO_LENGTH,
234 &callbackValue[ARR_INDEX_ONE]);
235 } else {
236 EDMLOGD("asyncCallbackInfo->first = %{public}u, second = %{public}s ",
237 GetMessageFromReturncode(asyncCallbackInfo->ret).first,
238 GetMessageFromReturncode(asyncCallbackInfo->ret).second.c_str());
239 callbackValue[ARR_INDEX_ZERO] = CreateError(env, asyncCallbackInfo->ret);
240 napi_get_null(env, &callbackValue[ARR_INDEX_ONE]);
241 }
242 napi_value callback = nullptr;
243 napi_value result = nullptr;
244 napi_get_reference_value(env, asyncCallbackInfo->callback, &callback);
245 napi_call_function(env, nullptr, callback, std::size(callbackValue), callbackValue, &result);
246 napi_delete_reference(env, asyncCallbackInfo->callback);
247 }
248 napi_delete_async_work(env, asyncCallbackInfo->asyncWork);
249 delete asyncCallbackInfo;
250 }
251 } // namespace EDM
252 } // namespace OHOS