1 /*
2 * Copyright (c) 2022-2023 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_distributed_enable.h"
17
18 #include "ans_inner_errors.h"
19 #include "js_native_api.h"
20 #include "js_native_api_types.h"
21
22 namespace OHOS {
23 namespace NotificationNapi {
24 const int SET_DISTRIBUTED_ENABLE_MAX_PARA = 3;
25 const int SET_DISTRIBUTED_ENABLE_MIN_PARA = 2;
26 const int SET_SMART_REMINDER_ENABLE_MAX_PARA = 2;
27 const int SET_SMART_REMINDER_ENABLE_MIN_PARA = 1;
28 const int SET_DISTRIBUTED_ENABLE_BY_SLOT_PARA = 3;
29 const int GET_DISTRIBUTED_ENABLE_BY_SLOT_PARA = 2;
30 static const std::set<std::string> DEVICE_TYPES = {"headset", "liteWearable", "wearable"};
31
ParseParameters(const napi_env & env,const napi_callback_info & info,DistributedEnableParams & params)32 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, DistributedEnableParams ¶ms)
33 {
34 ANS_LOGD("enter");
35
36 size_t argc = SET_DISTRIBUTED_ENABLE_MAX_PARA;
37 napi_value argv[SET_DISTRIBUTED_ENABLE_MAX_PARA] = {nullptr};
38 napi_value thisVar = nullptr;
39 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
40 if (argc < SET_DISTRIBUTED_ENABLE_MIN_PARA) {
41 ANS_LOGW("Wrong number of arguments.");
42 Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
43 return nullptr;
44 }
45
46 // argv[0]: bundleOption
47 napi_valuetype valuetype = napi_undefined;
48 NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
49 if (valuetype != napi_object) {
50 ANS_LOGW("Parameter type error. Object expected.");
51 std::string msg = "Incorrect parameter types.The type of param must be object.";
52 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
53 return nullptr;
54 }
55 auto retValue = Common::GetBundleOption(env, argv[PARAM0], params.option);
56 if (retValue == nullptr) {
57 ANS_LOGE("GetBundleOption failed.");
58 Common::NapiThrow(env, ERROR_PARAM_INVALID, PARAMETER_VERIFICATION_FAILED);
59 return nullptr;
60 }
61
62 // argv[1]: deviceType
63 NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
64 if (valuetype != napi_string) {
65 ANS_LOGW("Wrong argument type. Bool expected.");
66 std::string msg = "Incorrect parameter types.The type of param must be boolean.";
67 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
68 return nullptr;
69 }
70 char str[STR_MAX_SIZE] = {0};
71 size_t strLen = 0;
72 napi_get_value_string_utf8(env, argv[PARAM1], str, STR_MAX_SIZE - 1, &strLen);
73 if (std::strlen(str) == 0) {
74 ANS_LOGE("Property deviceType is empty");
75 Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
76 return nullptr;
77 }
78 params.deviceType = str;
79
80 if (argc > SET_DISTRIBUTED_ENABLE_MIN_PARA) {
81 // argv[2]: enable
82 NAPI_CALL(env, napi_typeof(env, argv[PARAM2], &valuetype));
83 if (valuetype != napi_boolean) {
84 ANS_LOGW("Wrong argument type. Bool expected.");
85 std::string msg = "Incorrect parameter types.The type of param must be boolean.";
86 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
87 return nullptr;
88 }
89 napi_get_value_bool(env, argv[PARAM2], ¶ms.enable);
90 }
91
92 return Common::NapiGetNull(env);
93 }
94
AsyncCompleteCallbackNapiSetDistributedEnabledByBundle(napi_env env,napi_status status,void * data)95 void AsyncCompleteCallbackNapiSetDistributedEnabledByBundle(napi_env env, napi_status status, void *data)
96 {
97 ANS_LOGD("enter");
98 if (!data) {
99 ANS_LOGE("Invalid async callback data");
100 return;
101 }
102 AsyncCallbackDistributedEnable *asynccallbackinfo = static_cast<AsyncCallbackDistributedEnable *>(data);
103 if (asynccallbackinfo) {
104 Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env));
105 if (asynccallbackinfo->info.callback != nullptr) {
106 ANS_LOGD("Delete NapiSetDistributedEnableByBundle callback reference.");
107 napi_delete_reference(env, asynccallbackinfo->info.callback);
108 }
109 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
110 delete asynccallbackinfo;
111 asynccallbackinfo = nullptr;
112 }
113 }
114
NapiSetDistributedEnabledByBundle(napi_env env,napi_callback_info info)115 napi_value NapiSetDistributedEnabledByBundle(napi_env env, napi_callback_info info)
116 {
117 ANS_LOGD("enter");
118 DistributedEnableParams params {};
119 if (ParseParameters(env, info, params) == nullptr) {
120 Common::NapiThrow(env, ERROR_PARAM_INVALID);
121 return Common::NapiGetUndefined(env);
122 }
123
124 AsyncCallbackDistributedEnable *asynccallbackinfo =
125 new (std::nothrow) AsyncCallbackDistributedEnable {.env = env, .asyncWork = nullptr, .params = params};
126 if (!asynccallbackinfo) {
127 Common::NapiThrow(env, ERROR_INTERNAL_ERROR);
128 return Common::JSParaError(env, nullptr);
129 }
130 napi_value promise = nullptr;
131 Common::PaddingCallbackPromiseInfo(env, nullptr, asynccallbackinfo->info, promise);
132
133 napi_value resourceName = nullptr;
134 napi_create_string_latin1(env, "distributedEnableByBundle", NAPI_AUTO_LENGTH, &resourceName);
135 // Asynchronous function call
136 napi_create_async_work(env,
137 nullptr,
138 resourceName,
139 [](napi_env env, void *data) {
140 ANS_LOGD("NapiSetDistributedEnableByBundle work excute.");
141 AsyncCallbackDistributedEnable *asynccallbackinfo = static_cast<AsyncCallbackDistributedEnable *>(data);
142 if (asynccallbackinfo) {
143 std::string deviceType = asynccallbackinfo->params.deviceType;
144 asynccallbackinfo->info.errorCode = NotificationHelper::SetDistributedEnabledByBundle(
145 asynccallbackinfo->params.option, deviceType, asynccallbackinfo->params.enable);
146 ANS_LOGI("asynccallbackinfo->info.errorCode = %{public}d", asynccallbackinfo->info.errorCode);
147 }
148 },
149 AsyncCompleteCallbackNapiSetDistributedEnabledByBundle,
150 (void *)asynccallbackinfo,
151 &asynccallbackinfo->asyncWork);
152
153 napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated);
154
155 return promise;
156 }
157
AsyncCompleteCallbackNapiSetSmartReminderEnabled(napi_env env,napi_status status,void * data)158 void AsyncCompleteCallbackNapiSetSmartReminderEnabled(napi_env env, napi_status status, void *data)
159 {
160 ANS_LOGD("enter");
161 if (!data) {
162 ANS_LOGE("Invalid async callback data");
163 return;
164 }
165 AsyncCallbackSmartReminderEnabled *asynccallbackinfo = static_cast<AsyncCallbackSmartReminderEnabled *>(data);
166 if (asynccallbackinfo) {
167 Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env));
168 if (asynccallbackinfo->info.callback != nullptr) {
169 ANS_LOGD("Delete NapiSetSmartReminderEnabled callback reference.");
170 napi_delete_reference(env, asynccallbackinfo->info.callback);
171 }
172 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
173 delete asynccallbackinfo;
174 asynccallbackinfo = nullptr;
175 }
176 }
177
ParseParameters(const napi_env & env,const napi_callback_info & info,SmartReminderEnabledParams & params)178 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, SmartReminderEnabledParams ¶ms)
179 {
180 ANS_LOGD("enter");
181
182 size_t argc = SET_SMART_REMINDER_ENABLE_MAX_PARA;
183 napi_value argv[SET_SMART_REMINDER_ENABLE_MAX_PARA] = {nullptr};
184 napi_value thisVar = nullptr;
185 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
186 if (argc < SET_SMART_REMINDER_ENABLE_MIN_PARA) {
187 ANS_LOGW("Wrong number of arguments.");
188 Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
189 return nullptr;
190 }
191
192 napi_valuetype valuetype = napi_undefined;
193 // argv[0]: deviceType
194 NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
195 if (valuetype != napi_string) {
196 ANS_LOGW("Wrong argument type. String expected.");
197 std::string msg = "Incorrect parameter types.The type of param must be string.";
198 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
199 return nullptr;
200 }
201 char str[STR_MAX_SIZE] = {0};
202 size_t strLen = 0;
203 napi_get_value_string_utf8(env, argv[PARAM0], str, STR_MAX_SIZE - 1, &strLen);
204 if (std::strlen(str) == 0) {
205 ANS_LOGE("Property deviceType is empty");
206 Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
207 return nullptr;
208 }
209 params.deviceType = str;
210
211 if (argc > SET_SMART_REMINDER_ENABLE_MIN_PARA) {
212 // argv[1]: enable
213 NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
214 if (valuetype != napi_boolean) {
215 ANS_LOGW("Wrong argument type. Bool expected.");
216 std::string msg = "Incorrect parameter types.The type of param must be boolean.";
217 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
218 return nullptr;
219 }
220 napi_get_value_bool(env, argv[PARAM1], ¶ms.enable);
221 }
222
223 return Common::NapiGetNull(env);
224 }
225
NapiSetSmartReminderEnabled(napi_env env,napi_callback_info info)226 napi_value NapiSetSmartReminderEnabled(napi_env env, napi_callback_info info)
227 {
228 ANS_LOGD("enter");
229 SmartReminderEnabledParams params {};
230 if (ParseParameters(env, info, params) == nullptr) {
231 Common::NapiThrow(env, ERROR_PARAM_INVALID);
232 return Common::NapiGetUndefined(env);
233 }
234
235 AsyncCallbackSmartReminderEnabled *asynccallbackinfo =
236 new (std::nothrow) AsyncCallbackSmartReminderEnabled {.env = env, .asyncWork = nullptr, .params = params};
237 if (!asynccallbackinfo) {
238 Common::NapiThrow(env, ERROR_INTERNAL_ERROR);
239 return Common::JSParaError(env, nullptr);
240 }
241 napi_value promise = nullptr;
242 Common::PaddingCallbackPromiseInfo(env, nullptr, asynccallbackinfo->info, promise);
243
244 napi_value resourceName = nullptr;
245 napi_create_string_latin1(env, "setSmartReminderEnabled", NAPI_AUTO_LENGTH, &resourceName);
246 // Asynchronous function call
247 napi_create_async_work(env,
248 nullptr,
249 resourceName,
250 [](napi_env env, void *data) {
251 ANS_LOGD("NapiSetSmartReminderEnabled work excute.");
252 AsyncCallbackSmartReminderEnabled *asynccallbackinfo =
253 static_cast<AsyncCallbackSmartReminderEnabled *>(data);
254 if (asynccallbackinfo) {
255 asynccallbackinfo->info.errorCode = NotificationHelper::SetSmartReminderEnabled(
256 asynccallbackinfo->params.deviceType, asynccallbackinfo->params.enable);
257 ANS_LOGD("asynccallbackinfo->info.errorCode = %{public}d", asynccallbackinfo->info.errorCode);
258 }
259 },
260 AsyncCompleteCallbackNapiSetSmartReminderEnabled,
261 (void *)asynccallbackinfo,
262 &asynccallbackinfo->asyncWork);
263
264 napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated);
265 return promise;
266 }
267
AsyncCompleteCallbackNapiIsSmartReminderEnabled(napi_env env,napi_status status,void * data)268 void AsyncCompleteCallbackNapiIsSmartReminderEnabled(napi_env env, napi_status status, void *data)
269 {
270 ANS_LOGD("enter");
271 if (!data) {
272 ANS_LOGE("Invalid async callback data");
273 return;
274 }
275 ANS_LOGI("IsSmartReminderEnabled napi_create_async_work end");
276 AsyncCallbackSmartReminderEnabled *asynccallbackinfo = static_cast<AsyncCallbackSmartReminderEnabled *>(data);
277 if (asynccallbackinfo) {
278 napi_value result = nullptr;
279 if (asynccallbackinfo->info.errorCode != ERR_OK) {
280 result = Common::NapiGetNull(env);
281 } else {
282 napi_get_boolean(env, asynccallbackinfo->params.enable, &result);
283 }
284 Common::CreateReturnValue(env, asynccallbackinfo->info, result);
285 if (asynccallbackinfo->info.callback != nullptr) {
286 ANS_LOGD("Delete NapiIsSmartReminderEnabled callback reference.");
287 napi_delete_reference(env, asynccallbackinfo->info.callback);
288 }
289 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
290 delete asynccallbackinfo;
291 asynccallbackinfo = nullptr;
292 }
293 }
294
NapiIsSmartReminderEnabled(napi_env env,napi_callback_info info)295 napi_value NapiIsSmartReminderEnabled(napi_env env, napi_callback_info info)
296 {
297 ANS_LOGD("enter");
298 SmartReminderEnabledParams params {};
299 if (ParseParameters(env, info, params) == nullptr) {
300 Common::NapiThrow(env, ERROR_PARAM_INVALID);
301 return Common::NapiGetUndefined(env);
302 }
303
304 AsyncCallbackSmartReminderEnabled *asynccallbackinfo =
305 new (std::nothrow) AsyncCallbackSmartReminderEnabled {.env = env, .asyncWork = nullptr, .params = params};
306 if (!asynccallbackinfo) {
307 Common::NapiThrow(env, ERROR_INTERNAL_ERROR);
308 return Common::JSParaError(env, nullptr);
309 }
310 napi_value promise = nullptr;
311 Common::PaddingCallbackPromiseInfo(env, nullptr, asynccallbackinfo->info, promise);
312
313 napi_value resourceName = nullptr;
314 napi_create_string_latin1(env, "isSmartReminderEnabled", NAPI_AUTO_LENGTH, &resourceName);
315 // Asynchronous function call
316 napi_create_async_work(env,
317 nullptr,
318 resourceName,
319 [](napi_env env, void *data) {
320 ANS_LOGD("NapiIsSmartReminderEnabled work excute.");
321 AsyncCallbackSmartReminderEnabled *asynccallbackinfo =
322 static_cast<AsyncCallbackSmartReminderEnabled *>(data);
323 if (asynccallbackinfo) {
324 asynccallbackinfo->info.errorCode = NotificationHelper::IsSmartReminderEnabled(
325 asynccallbackinfo->params.deviceType, asynccallbackinfo->params.enable);
326 ANS_LOGD("asynccallbackinfo->info.errorCode = %{public}d", asynccallbackinfo->info.errorCode);
327 }
328 },
329 AsyncCompleteCallbackNapiIsSmartReminderEnabled,
330 (void *)asynccallbackinfo,
331 &asynccallbackinfo->asyncWork);
332
333 napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated);
334 return promise;
335 }
336
ParseParameters(const napi_env & env,const napi_callback_info & info,DistributedEnableBySlotParams & params,bool setOperation)337 napi_value ParseParameters(const napi_env &env,
338 const napi_callback_info &info, DistributedEnableBySlotParams ¶ms, bool setOperation)
339 {
340 ANS_LOGD("enter");
341 size_t argc = SET_DISTRIBUTED_ENABLE_BY_SLOT_PARA;
342 napi_value argv[SET_DISTRIBUTED_ENABLE_BY_SLOT_PARA] = {nullptr};
343 napi_value thisVar = nullptr;
344 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
345 if ((setOperation && argc != SET_DISTRIBUTED_ENABLE_BY_SLOT_PARA) ||
346 (!setOperation && argc != GET_DISTRIBUTED_ENABLE_BY_SLOT_PARA)) {
347 ANS_LOGW("Wrong number of arguments.");
348 Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
349 return nullptr;
350 }
351
352 // argv[0]: slot
353 napi_valuetype valuetype = napi_undefined;
354 NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
355 if (valuetype != napi_number) {
356 ANS_LOGW("Parameter type error. Object expected.");
357 std::string msg = "Incorrect parameter types.The type of param must be object.";
358 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
359 return nullptr;
360 }
361 int32_t slotType;
362 NAPI_CALL(env, napi_get_value_int32(env, argv[PARAM0], &slotType));
363 NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER;
364 if (!AnsEnumUtil::SlotTypeJSToC(SlotType(slotType), outType)) {
365 std::string msg = "Incorrect parameter slot.";
366 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
367 return nullptr;
368 }
369 params.slot = outType;
370
371 // argv[1]: deviceType
372 NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
373 if (valuetype != napi_string) {
374 ANS_LOGW("Wrong argument type. Bool expected.");
375 std::string msg = "Incorrect parameter types.The type of param must be string.";
376 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
377 return nullptr;
378 }
379 char str[STR_MAX_SIZE] = {0};
380 size_t strLen = 0;
381 napi_get_value_string_utf8(env, argv[PARAM1], str, STR_MAX_SIZE - 1, &strLen);
382 if (std::strlen(str) == 0) {
383 ANS_LOGE("Property deviceType is empty");
384 Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
385 return nullptr;
386 }
387 if (DEVICE_TYPES.find(str) == DEVICE_TYPES.end()) {
388 ANS_LOGE("Property deviceType is not allowed");
389 Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
390 return nullptr;
391 }
392 params.deviceType = str;
393
394 // argv[2]: enable
395 if (setOperation) {
396 NAPI_CALL(env, napi_typeof(env, argv[PARAM2], &valuetype));
397 if (valuetype != napi_boolean) {
398 ANS_LOGW("Wrong argument type. Bool expected.");
399 std::string msg = "Incorrect parameter types.The type of param must be boolean.";
400 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
401 return nullptr;
402 }
403 napi_get_value_bool(env, argv[PARAM2], ¶ms.enable);
404 }
405
406 return Common::NapiGetNull(env);
407 }
408
AsyncCompleteCallbackNapiSetDistributedEnabledBySlot(napi_env env,napi_status status,void * data)409 void AsyncCompleteCallbackNapiSetDistributedEnabledBySlot(napi_env env, napi_status status, void *data)
410 {
411 ANS_LOGD("enter");
412 if (!data) {
413 ANS_LOGE("Invalid async callback data");
414 return;
415 }
416 AsyncCallbackDistributedEnableBySlot *asynccallbackinfo = static_cast<AsyncCallbackDistributedEnableBySlot *>(data);
417 if (asynccallbackinfo) {
418 Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env));
419 if (asynccallbackinfo->info.callback != nullptr) {
420 ANS_LOGD("Delete NapiSetDistributedEnabledBySlot callback reference.");
421 napi_delete_reference(env, asynccallbackinfo->info.callback);
422 }
423 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
424 delete asynccallbackinfo;
425 asynccallbackinfo = nullptr;
426 }
427 }
428
NapiSetDistributedEnabledBySlot(napi_env env,napi_callback_info info)429 napi_value NapiSetDistributedEnabledBySlot(napi_env env, napi_callback_info info)
430 {
431 ANS_LOGD("enter");
432 DistributedEnableBySlotParams params {};
433 if (ParseParameters(env, info, params, true) == nullptr) {
434 Common::NapiThrow(env, ERROR_PARAM_INVALID);
435 return Common::NapiGetUndefined(env);
436 }
437
438 AsyncCallbackDistributedEnableBySlot *asynccallbackinfo =
439 new (std::nothrow) AsyncCallbackDistributedEnableBySlot {.env = env, .asyncWork = nullptr, .params = params};
440 if (!asynccallbackinfo) {
441 Common::NapiThrow(env, ERROR_INTERNAL_ERROR);
442 return Common::JSParaError(env, nullptr);
443 }
444 napi_value promise = nullptr;
445 Common::PaddingCallbackPromiseInfo(env, nullptr, asynccallbackinfo->info, promise);
446
447 napi_value resourceName = nullptr;
448 napi_create_string_latin1(env, "setDistributedEnabledBySlot", NAPI_AUTO_LENGTH, &resourceName);
449 // Asynchronous function call
450 napi_create_async_work(env,
451 nullptr,
452 resourceName,
453 [](napi_env env, void *data) {
454 ANS_LOGD("NapiSetDistributedEnabledBySlot work excute.");
455 AsyncCallbackDistributedEnableBySlot *asynccallbackinfo =
456 static_cast<AsyncCallbackDistributedEnableBySlot *>(data);
457 if (asynccallbackinfo) {
458 asynccallbackinfo->info.errorCode = NotificationHelper::SetDistributedEnabledBySlot(
459 asynccallbackinfo->params.slot,
460 asynccallbackinfo->params.deviceType,
461 asynccallbackinfo->params.enable);
462 ANS_LOGI("asynccallbackinfo->info.errorCode = %{public}d", asynccallbackinfo->info.errorCode);
463 }
464 },
465 AsyncCompleteCallbackNapiSetDistributedEnabledBySlot,
466 (void *)asynccallbackinfo,
467 &asynccallbackinfo->asyncWork);
468
469 napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated);
470
471 return promise;
472 }
473
AsyncCompleteCallbackNapiIsDistributedEnabledBySlot(napi_env env,napi_status status,void * data)474 void AsyncCompleteCallbackNapiIsDistributedEnabledBySlot(napi_env env, napi_status status, void *data)
475 {
476 ANS_LOGD("enter");
477 if (!data) {
478 ANS_LOGE("Invalid async callback data");
479 return;
480 }
481 ANS_LOGI("IsDistributedEnabledBySlot napi_create_async_work end");
482 AsyncCallbackDistributedEnableBySlot *asynccallbackinfo = static_cast<AsyncCallbackDistributedEnableBySlot *>(data);
483 if (asynccallbackinfo) {
484 napi_value result = nullptr;
485 if (asynccallbackinfo->info.errorCode != ERR_OK) {
486 result = Common::NapiGetNull(env);
487 } else {
488 napi_get_boolean(env, asynccallbackinfo->params.enable, &result);
489 }
490 Common::CreateReturnValue(env, asynccallbackinfo->info, result);
491 if (asynccallbackinfo->info.callback != nullptr) {
492 ANS_LOGD("Delete NapiIsDistributedEnabledBySlot callback reference.");
493 napi_delete_reference(env, asynccallbackinfo->info.callback);
494 }
495 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
496 delete asynccallbackinfo;
497 asynccallbackinfo = nullptr;
498 }
499 }
500
NapiIsDistributedEnabledBySlot(napi_env env,napi_callback_info info)501 napi_value NapiIsDistributedEnabledBySlot(napi_env env, napi_callback_info info)
502 {
503 ANS_LOGD("enter");
504 DistributedEnableBySlotParams params {};
505 if (ParseParameters(env, info, params, false) == nullptr) {
506 Common::NapiThrow(env, ERROR_PARAM_INVALID);
507 return Common::NapiGetUndefined(env);
508 }
509
510 AsyncCallbackDistributedEnableBySlot *asynccallbackinfo =
511 new (std::nothrow) AsyncCallbackDistributedEnableBySlot {.env = env, .asyncWork = nullptr, .params = params};
512 if (!asynccallbackinfo) {
513 Common::NapiThrow(env, ERROR_INTERNAL_ERROR);
514 return Common::JSParaError(env, nullptr);
515 }
516 napi_value promise = nullptr;
517 Common::PaddingCallbackPromiseInfo(env, nullptr, asynccallbackinfo->info, promise);
518
519 napi_value resourceName = nullptr;
520 napi_create_string_latin1(env, "isDistributedEnabledBySlot", NAPI_AUTO_LENGTH, &resourceName);
521 // Asynchronous function call
522 napi_create_async_work(env,
523 nullptr,
524 resourceName,
525 [](napi_env env, void *data) {
526 ANS_LOGD("NapiIsDistributedEnabledBySlot work excute.");
527 AsyncCallbackDistributedEnableBySlot *asynccallbackinfo =
528 static_cast<AsyncCallbackDistributedEnableBySlot *>(data);
529 if (asynccallbackinfo) {
530 asynccallbackinfo->info.errorCode = NotificationHelper::IsDistributedEnabledBySlot(
531 asynccallbackinfo->params.slot,
532 asynccallbackinfo->params.deviceType,
533 asynccallbackinfo->params.enable);
534 ANS_LOGD("asynccallbackinfo->info.errorCode = %{public}d", asynccallbackinfo->info.errorCode);
535 }
536 },
537 AsyncCompleteCallbackNapiIsDistributedEnabledBySlot,
538 (void *)asynccallbackinfo,
539 &asynccallbackinfo->asyncWork);
540
541 napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated);
542 return promise;
543 }
544 } // namespace NotificationNapi
545 } // namespace OHOS
546