• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "slot.h"
17 
18 namespace OHOS {
19 namespace NotificationNapi {
20 const int32_t ADD_SLOT_MAX_PARA = 2;
21 const int32_t ADD_SLOTS_MAX_PARA = 2;
22 const int32_t SET_SLOT_AS_BUNDLE_MAX_PARA = 3;
23 const int32_t GET_SLOT_MAX_PARA = 2;
24 const int32_t GET_SLOT_NUM_AS_BUNDLE_MAX_PARA = 2;
25 const int32_t GET_SLOTS_AS_BUNDLE_MAX_PARA = 2;
26 const int32_t REMOVE_SLOT_MAX_PARA = 2;
27 const int32_t GET_ENABLE_SLOT_MAX_PARA = 3;
28 const int32_t SET_ENABLE_SLOT_MAX_PARA = 4;
29 
30 struct ParametersInfoAddSlot {
31     NotificationSlot slot;
32     NotificationConstant::SlotType inType = NotificationConstant::SlotType::OTHER;
33     bool isAddSlotByType = false;
34     napi_ref callback = nullptr;
35 };
36 
37 struct AsyncCallbackInfoAddSlot {
38     napi_env env = nullptr;
39     napi_async_work asyncWork = nullptr;
40     NotificationSlot slot;
41     NotificationConstant::SlotType inType = NotificationConstant::SlotType::OTHER;
42     bool isAddSlotByType = false;
43     CallbackPromiseInfo info;
44 };
45 
46 struct ParametersInfoAddSlots {
47     std::vector<NotificationSlot> slots;
48     napi_ref callback = nullptr;
49 };
50 
51 struct AsyncCallbackInfoAddSlots {
52     napi_env env = nullptr;
53     napi_async_work asyncWork = nullptr;
54     std::vector<NotificationSlot> slots;
55     CallbackPromiseInfo info;
56 };
57 
58 struct ParametersInfoSetSlotByBundle {
59     NotificationBundleOption option;
60     std::vector<sptr<NotificationSlot>> slots;
61     napi_ref callback = nullptr;
62 };
63 
64 struct AsyncCallbackInfoSetSlotByBundle {
65     napi_env env = nullptr;
66     napi_async_work asyncWork = nullptr;
67     ParametersInfoSetSlotByBundle params;
68     CallbackPromiseInfo info;
69 };
70 
71 struct ParametersInfoGetSlot {
72     NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER;
73     napi_ref callback = nullptr;
74 };
75 
76 struct AsyncCallbackInfoGetSlot {
77     napi_env env = nullptr;
78     napi_async_work asyncWork = nullptr;
79     NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER;
80     CallbackPromiseInfo info;
81     sptr<NotificationSlot> slot = nullptr;
82 };
83 
84 struct ParametersInfoGetSlotNumByBundle {
85     NotificationBundleOption option;
86     napi_ref callback = nullptr;
87 };
88 
89 struct AsyncCallbackInfoGetSlotNumByBundle {
90     napi_env env = nullptr;
91     napi_async_work asyncWork = nullptr;
92     ParametersInfoGetSlotNumByBundle params;
93     CallbackPromiseInfo info;
94     uint64_t num = 0;
95 };
96 
97 struct AsyncCallbackInfoGetSlots {
98     napi_env env = nullptr;
99     napi_async_work asyncWork = nullptr;
100     CallbackPromiseInfo info;
101     std::vector<sptr<NotificationSlot>> slots;
102 };
103 
104 struct ParametersInfoGetSlotsByBundle {
105     NotificationBundleOption option;
106     napi_ref callback = nullptr;
107 };
108 
109 struct AsyncCallbackInfoGetSlotsByBundle {
110     napi_env env = nullptr;
111     napi_async_work asyncWork = nullptr;
112     ParametersInfoGetSlotsByBundle params;
113     CallbackPromiseInfo info;
114     std::vector<sptr<NotificationSlot>> slots;
115 };
116 
117 struct ParametersInfoRemoveSlot {
118     NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER;
119     napi_ref callback = nullptr;
120 };
121 
122 struct AsyncCallbackInfoRemoveSlot {
123     napi_env env = nullptr;
124     napi_async_work asyncWork = nullptr;
125     NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER;
126     CallbackPromiseInfo info;
127 };
128 
129 struct AsyncCallbackInfoRemoveAllSlots {
130     napi_env env = nullptr;
131     napi_async_work asyncWork = nullptr;
132     CallbackPromiseInfo info;
133 };
134 
135 struct ParametersInfoEnableSlot {
136     NotificationBundleOption option;
137     NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER;
138     bool enable = false;
139     napi_ref callback = nullptr;
140 };
141 
142 struct AsyncCallbackInfoInfoEnableSlot {
143     napi_env env = nullptr;
144     napi_async_work asyncWork = nullptr;
145     ParametersInfoEnableSlot params;
146     CallbackPromiseInfo info;
147 };
148 
149 struct ParametersInfoIsEnableSlot {
150     NotificationBundleOption option;
151     NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER;
152     napi_ref callback = nullptr;
153 };
154 
155 struct AsyncCallbackInfoInfoIsEnableSlot {
156     napi_env env = nullptr;
157     napi_async_work asyncWork = nullptr;
158     ParametersInfoIsEnableSlot params;
159     bool isEnable = false;
160     CallbackPromiseInfo info;
161 };
162 
ParseParametersByAddSlot(const napi_env & env,const napi_callback_info & info,ParametersInfoAddSlot & paras)163 napi_value ParseParametersByAddSlot(const napi_env &env, const napi_callback_info &info, ParametersInfoAddSlot &paras)
164 {
165     ANS_LOGI("enter");
166     size_t argc = ADD_SLOT_MAX_PARA;
167     napi_value argv[ADD_SLOT_MAX_PARA] = {nullptr};
168     napi_value thisVar = nullptr;
169     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
170     if (argc < 1) {
171         ANS_LOGE("Wrong number of arguments");
172         return nullptr;
173     }
174 
175     napi_valuetype valuetype = napi_undefined;
176     // argv[0]: NotificationSlot
177     NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
178     if (valuetype != napi_object && valuetype != napi_number) {
179         ANS_LOGE("Wrong argument type. Object or number expected.");
180         return nullptr;
181     }
182     if (valuetype == napi_number) {
183         paras.isAddSlotByType = true;
184         int32_t slotType = 0;
185         napi_get_value_int32(env, argv[PARAM0], &slotType);
186         if (!Common::SlotTypeJSToC(SlotType(slotType), paras.inType)) {
187             return nullptr;
188         }
189     } else {
190         paras.isAddSlotByType = false;
191         if (!Common::GetNotificationSlot(env, argv[PARAM0], paras.slot)) {
192             return nullptr;
193         }
194     }
195 
196     // argv[1]:callback
197     if (argc >= ADD_SLOT_MAX_PARA) {
198         NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
199         if (valuetype != napi_function) {
200             ANS_LOGE("Wrong argument type. Function expected.");
201             return nullptr;
202         }
203         napi_create_reference(env, argv[PARAM1], 1, &paras.callback);
204     }
205     return Common::NapiGetNull(env);
206 }
207 
ParseParametersByAddSlots(const napi_env & env,const napi_callback_info & info,ParametersInfoAddSlots & paras)208 napi_value ParseParametersByAddSlots(const napi_env &env, const napi_callback_info &info, ParametersInfoAddSlots &paras)
209 {
210     ANS_LOGI("enter");
211     size_t argc = ADD_SLOTS_MAX_PARA;
212     napi_value argv[ADD_SLOTS_MAX_PARA] = {nullptr};
213     napi_value thisVar = nullptr;
214     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
215     if (argc < 1) {
216         ANS_LOGE("Wrong number of arguments");
217         return nullptr;
218     }
219 
220     napi_valuetype valuetype = napi_undefined;
221     // argv[0]: Array<NotificationSlot>
222     bool isArray = false;
223     napi_is_array(env, argv[PARAM0], &isArray);
224     if (!isArray) {
225         ANS_LOGE("Wrong argument type. Array expected.");
226         return nullptr;
227     }
228     uint32_t length = 0;
229     napi_get_array_length(env, argv[PARAM0], &length);
230     if (length == 0) {
231         ANS_LOGE("The array is empty.");
232         return nullptr;
233     }
234     for (size_t i = 0; i < length; i++) {
235         napi_value nSlot = nullptr;
236         napi_get_element(env, argv[PARAM0], i, &nSlot);
237         NAPI_CALL(env, napi_typeof(env, nSlot, &valuetype));
238         if (valuetype != napi_object) {
239             ANS_LOGE("Wrong argument type. Object expected.");
240             return nullptr;
241         }
242         NotificationSlot slot;
243         if (!Common::GetNotificationSlot(env, nSlot, slot)) {
244             return nullptr;
245         }
246         paras.slots.emplace_back(slot);
247     }
248 
249     // argv[1]:callback
250     if (argc >= ADD_SLOTS_MAX_PARA) {
251         NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
252         if (valuetype != napi_function) {
253             ANS_LOGE("Wrong argument type. Function expected.");
254             return nullptr;
255         }
256         napi_create_reference(env, argv[PARAM1], 1, &paras.callback);
257     }
258     return Common::NapiGetNull(env);
259 }
260 
ParseParametersSetSlotByBundle(const napi_env & env,const napi_callback_info & info,ParametersInfoSetSlotByBundle & params)261 napi_value ParseParametersSetSlotByBundle(
262     const napi_env &env, const napi_callback_info &info, ParametersInfoSetSlotByBundle &params)
263 {
264     ANS_LOGI("enter");
265 
266     size_t argc = SET_SLOT_AS_BUNDLE_MAX_PARA;
267     napi_value argv[SET_SLOT_AS_BUNDLE_MAX_PARA] = {nullptr};
268     napi_value thisVar = nullptr;
269     napi_valuetype valuetype = napi_undefined;
270     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
271     if (argc < SET_SLOT_AS_BUNDLE_MAX_PARA - 1) {
272         ANS_LOGE("Wrong number of arguments");
273         return nullptr;
274     }
275 
276     // argv[0]: bundle
277     NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
278     if (valuetype != napi_object) {
279         ANS_LOGE("Wrong argument type. Object expected.");
280         return nullptr;
281     }
282     auto retValue = Common::GetBundleOption(env, argv[PARAM0], params.option);
283     if (retValue == nullptr) {
284         ANS_LOGE("GetBundleOption failed.");
285         return nullptr;
286     }
287 
288     // argv[1]: slot
289     NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
290     if (valuetype != napi_object) {
291         ANS_LOGE("Wrong argument type. Object expected.");
292         return nullptr;
293     }
294     NotificationSlot slot;
295     if (!Common::GetNotificationSlot(env, argv[PARAM1], slot)) {
296         return nullptr;
297     }
298     std::vector<NotificationSlot> slots;
299     slots.emplace_back(slot);
300 
301     for (auto vec : slots) {
302         sptr<NotificationSlot> slotPtr = new (std::nothrow) NotificationSlot(vec);
303         if (slotPtr == nullptr) {
304             ANS_LOGE("Failed to create NotificationSlot ptr");
305             return nullptr;
306         }
307         params.slots.emplace_back(slotPtr);
308     }
309 
310     // argv[2]:callback
311     if (argc >= SET_SLOT_AS_BUNDLE_MAX_PARA) {
312         NAPI_CALL(env, napi_typeof(env, argv[PARAM2], &valuetype));
313         if (valuetype != napi_function) {
314             ANS_LOGE("Wrong argument type. Function expected.");
315             return nullptr;
316         }
317         napi_create_reference(env, argv[PARAM2], 1, &params.callback);
318     }
319 
320     return Common::NapiGetNull(env);
321 }
322 
ParseParametersByGetSlot(const napi_env & env,const napi_callback_info & info,ParametersInfoGetSlot & paras)323 napi_value ParseParametersByGetSlot(const napi_env &env, const napi_callback_info &info, ParametersInfoGetSlot &paras)
324 {
325     ANS_LOGI("enter");
326     size_t argc = GET_SLOT_MAX_PARA;
327     napi_value argv[GET_SLOT_MAX_PARA] = {nullptr};
328     napi_value thisVar = nullptr;
329     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
330     if (argc < 1) {
331         ANS_LOGE("Wrong number of arguments");
332         return nullptr;
333     }
334 
335     napi_valuetype valuetype = napi_undefined;
336     // argv[0]: SlotType
337     NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
338     if (valuetype != napi_number) {
339         ANS_LOGE("Wrong argument type. Number expected.");
340         return nullptr;
341     }
342     int32_t slotType = 0;
343     napi_get_value_int32(env, argv[PARAM0], &slotType);
344     if (!Common::SlotTypeJSToC(SlotType(slotType), paras.outType)) {
345         return nullptr;
346     }
347 
348     // argv[1]:callback
349     if (argc >= GET_SLOT_MAX_PARA) {
350         NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
351         if (valuetype != napi_function) {
352             ANS_LOGE("Wrong argument type. Function expected.");
353             return nullptr;
354         }
355         napi_create_reference(env, argv[PARAM1], 1, &paras.callback);
356     }
357     return Common::NapiGetNull(env);
358 }
359 
ParseParametersGetSlotNumByBundle(const napi_env & env,const napi_callback_info & info,ParametersInfoGetSlotNumByBundle & params)360 napi_value ParseParametersGetSlotNumByBundle(
361     const napi_env &env, const napi_callback_info &info, ParametersInfoGetSlotNumByBundle &params)
362 {
363     ANS_LOGI("enter");
364 
365     size_t argc = GET_SLOT_NUM_AS_BUNDLE_MAX_PARA;
366     napi_value argv[GET_SLOT_NUM_AS_BUNDLE_MAX_PARA] = {nullptr};
367     napi_value thisVar = nullptr;
368     napi_valuetype valuetype = napi_undefined;
369     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
370     if (argc < GET_SLOT_NUM_AS_BUNDLE_MAX_PARA - 1) {
371         ANS_LOGE("Wrong number of arguments");
372         return nullptr;
373     }
374 
375     // argv[0]: bundle
376     NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
377     if (valuetype != napi_object) {
378         ANS_LOGE("Wrong argument type. Object expected.");
379         return nullptr;
380     }
381     auto retValue = Common::GetBundleOption(env, argv[PARAM0], params.option);
382     if (retValue == nullptr) {
383         ANS_LOGE("GetBundleOption failed.");
384         return nullptr;
385     }
386 
387     // argv[1]:callback
388     if (argc >= GET_SLOT_NUM_AS_BUNDLE_MAX_PARA) {
389         NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
390         if (valuetype != napi_function) {
391             ANS_LOGE("Wrong argument type. Function expected.");
392             return nullptr;
393         }
394         napi_create_reference(env, argv[PARAM1], 1, &params.callback);
395     }
396 
397     return Common::NapiGetNull(env);
398 }
399 
ParseParametersGetSlotsByBundle(const napi_env & env,const napi_callback_info & info,ParametersInfoGetSlotsByBundle & params)400 napi_value ParseParametersGetSlotsByBundle(
401     const napi_env &env, const napi_callback_info &info, ParametersInfoGetSlotsByBundle &params)
402 {
403     ANS_LOGI("enter");
404 
405     size_t argc = GET_SLOTS_AS_BUNDLE_MAX_PARA;
406     napi_value argv[GET_SLOTS_AS_BUNDLE_MAX_PARA] = {nullptr};
407     napi_value thisVar = nullptr;
408     napi_valuetype valuetype = napi_undefined;
409     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
410     if (argc < GET_SLOTS_AS_BUNDLE_MAX_PARA - 1) {
411         ANS_LOGE("Wrong number of arguments");
412         return nullptr;
413     }
414 
415     // argv[0]: bundle
416     NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
417     if (valuetype != napi_object) {
418         ANS_LOGE("Wrong argument type. Object expected.");
419         return nullptr;
420     }
421     auto retValue = Common::GetBundleOption(env, argv[PARAM0], params.option);
422     if (retValue == nullptr) {
423         ANS_LOGE("GetBundleOption failed.");
424         return nullptr;
425     }
426 
427     // argv[1]:callback
428     if (argc >= GET_SLOTS_AS_BUNDLE_MAX_PARA) {
429         NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
430         if (valuetype != napi_function) {
431             ANS_LOGE("Wrong argument type. Function expected.");
432             return nullptr;
433         }
434         napi_create_reference(env, argv[PARAM1], 1, &params.callback);
435     }
436 
437     return Common::NapiGetNull(env);
438 }
439 
ParseParametersByRemoveSlot(const napi_env & env,const napi_callback_info & info,ParametersInfoRemoveSlot & paras)440 napi_value ParseParametersByRemoveSlot(
441     const napi_env &env, const napi_callback_info &info, ParametersInfoRemoveSlot &paras)
442 {
443     ANS_LOGI("enter");
444     size_t argc = REMOVE_SLOT_MAX_PARA;
445     napi_value argv[REMOVE_SLOT_MAX_PARA] = {nullptr};
446     napi_value thisVar = nullptr;
447     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
448     if (argc < 1) {
449         ANS_LOGE("Wrong number of arguments");
450         return nullptr;
451     }
452 
453     napi_valuetype valuetype = napi_undefined;
454     // argv[0]: SlotType
455     NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
456     if (valuetype != napi_number) {
457         ANS_LOGE("Wrong argument type. Number expected.");
458         return nullptr;
459     }
460     int32_t slotType = 0;
461     napi_get_value_int32(env, argv[PARAM0], &slotType);
462     if (!Common::SlotTypeJSToC(SlotType(slotType), paras.outType)) {
463         return nullptr;
464     }
465 
466     // argv[1]:callback
467     if (argc >= REMOVE_SLOT_MAX_PARA) {
468         NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
469         if (valuetype != napi_function) {
470             ANS_LOGE("Wrong argument type. Function expected.");
471             return nullptr;
472         }
473         napi_create_reference(env, argv[PARAM1], 1, &paras.callback);
474     }
475     return Common::NapiGetNull(env);
476 }
477 
AddSlot(napi_env env,napi_callback_info info)478 napi_value AddSlot(napi_env env, napi_callback_info info)
479 {
480     ANS_LOGI("enter");
481 
482     ParametersInfoAddSlot paras;
483     if (ParseParametersByAddSlot(env, info, paras) == nullptr) {
484         return Common::NapiGetUndefined(env);
485     }
486 
487     AsyncCallbackInfoAddSlot *asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoAddSlot {
488         .env = env,
489         .asyncWork = nullptr,
490         .slot = paras.slot,
491         .inType = paras.inType,
492         .isAddSlotByType = paras.isAddSlotByType
493     };
494     if (!asynccallbackinfo) {
495         return Common::JSParaError(env, paras.callback);
496     }
497     napi_value promise = nullptr;
498     Common::PaddingCallbackPromiseInfo(env, paras.callback, asynccallbackinfo->info, promise);
499 
500     napi_value resourceName = nullptr;
501     napi_create_string_latin1(env, "addSlot", NAPI_AUTO_LENGTH, &resourceName);
502     // Asynchronous function call
503     napi_create_async_work(env,
504         nullptr,
505         resourceName,
506         [](napi_env env, void *data) {
507             ANS_LOGI("AddSlot napi_create_async_work start");
508             auto asynccallbackinfo = static_cast<AsyncCallbackInfoAddSlot *>(data);
509             if (asynccallbackinfo) {
510                 if (asynccallbackinfo->isAddSlotByType) {
511                     asynccallbackinfo->info.errorCode = NotificationHelper::AddSlotByType(asynccallbackinfo->inType);
512                 } else {
513                     asynccallbackinfo->info.errorCode = NotificationHelper::AddNotificationSlot(
514                         asynccallbackinfo->slot);
515                 }
516             }
517         },
518         [](napi_env env, napi_status status, void *data) {
519             ANS_LOGI("AddSlot napi_create_async_work end");
520             auto asynccallbackinfo = static_cast<AsyncCallbackInfoAddSlot *>(data);
521             if (asynccallbackinfo) {
522                 Common::ReturnCallbackPromise(env, asynccallbackinfo->info, Common::NapiGetNull(env));
523                 if (asynccallbackinfo->info.callback != nullptr) {
524                     napi_delete_reference(env, asynccallbackinfo->info.callback);
525                 }
526                 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
527                 delete asynccallbackinfo;
528                 asynccallbackinfo = nullptr;
529             }
530         },
531         (void *)asynccallbackinfo,
532         &asynccallbackinfo->asyncWork);
533 
534     napi_status status = napi_queue_async_work(env, asynccallbackinfo->asyncWork);
535     if (status != napi_ok) {
536         ANS_LOGE("napi_queue_async_work failed return: %{public}d", status);
537         if (asynccallbackinfo->info.callback != nullptr) {
538             napi_delete_reference(env, asynccallbackinfo->info.callback);
539         }
540         napi_delete_async_work(env, asynccallbackinfo->asyncWork);
541         delete asynccallbackinfo;
542         asynccallbackinfo = nullptr;
543         return Common::JSParaError(env, paras.callback);
544     }
545 
546     if (asynccallbackinfo->info.isCallback) {
547         return Common::NapiGetNull(env);
548     } else {
549         return promise;
550     }
551 }
552 
AddSlots(napi_env env,napi_callback_info info)553 napi_value AddSlots(napi_env env, napi_callback_info info)
554 {
555     ANS_LOGI("enter");
556 
557     ParametersInfoAddSlots paras;
558     if (ParseParametersByAddSlots(env, info, paras) == nullptr) {
559         return Common::NapiGetUndefined(env);
560     }
561 
562     AsyncCallbackInfoAddSlots *asynccallbackinfo =
563         new (std::nothrow) AsyncCallbackInfoAddSlots {.env = env, .asyncWork = nullptr, .slots = paras.slots};
564     if (!asynccallbackinfo) {
565         return Common::JSParaError(env, paras.callback);
566     }
567     napi_value promise = nullptr;
568     Common::PaddingCallbackPromiseInfo(env, paras.callback, asynccallbackinfo->info, promise);
569 
570     napi_value resourceName = nullptr;
571     napi_create_string_latin1(env, "addSlots", NAPI_AUTO_LENGTH, &resourceName);
572     // Asynchronous function call
573     napi_create_async_work(env,
574         nullptr,
575         resourceName,
576         [](napi_env env, void *data) {
577             ANS_LOGI("AddSlots napi_create_async_work start");
578             auto asynccallbackinfo = static_cast<AsyncCallbackInfoAddSlots *>(data);
579             if (asynccallbackinfo) {
580                 asynccallbackinfo->info.errorCode = NotificationHelper::AddNotificationSlots(asynccallbackinfo->slots);
581             }
582         },
583         [](napi_env env, napi_status status, void *data) {
584             ANS_LOGI("AddSlots napi_create_async_work end");
585             auto asynccallbackinfo = static_cast<AsyncCallbackInfoAddSlots *>(data);
586             if (asynccallbackinfo) {
587                 Common::ReturnCallbackPromise(env, asynccallbackinfo->info, Common::NapiGetNull(env));
588                 if (asynccallbackinfo->info.callback != nullptr) {
589                     napi_delete_reference(env, asynccallbackinfo->info.callback);
590                 }
591                 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
592                 delete asynccallbackinfo;
593                 asynccallbackinfo = nullptr;
594             }
595         },
596         (void *)asynccallbackinfo,
597         &asynccallbackinfo->asyncWork);
598 
599     napi_status status = napi_queue_async_work(env, asynccallbackinfo->asyncWork);
600     if (status != napi_ok) {
601         ANS_LOGE("napi_queue_async_work failed return: %{public}d", status);
602         if (asynccallbackinfo->info.callback != nullptr) {
603             napi_delete_reference(env, asynccallbackinfo->info.callback);
604         }
605         napi_delete_async_work(env, asynccallbackinfo->asyncWork);
606         delete asynccallbackinfo;
607         asynccallbackinfo = nullptr;
608         return Common::JSParaError(env, paras.callback);
609     }
610 
611     if (asynccallbackinfo->info.isCallback) {
612         return Common::NapiGetNull(env);
613     } else {
614         return promise;
615     }
616 }
617 
SetSlotByBundle(napi_env env,napi_callback_info info)618 napi_value SetSlotByBundle(napi_env env, napi_callback_info info)
619 {
620     ANS_LOGI("enter");
621 
622     ParametersInfoSetSlotByBundle params {};
623     if (ParseParametersSetSlotByBundle(env, info, params) == nullptr) {
624         return Common::NapiGetUndefined(env);
625     }
626 
627     AsyncCallbackInfoSetSlotByBundle *asynccallbackinfo =
628         new (std::nothrow) AsyncCallbackInfoSetSlotByBundle {.env = env, .asyncWork = nullptr, .params = params};
629     if (!asynccallbackinfo) {
630         return Common::JSParaError(env, params.callback);
631     }
632     napi_value promise = nullptr;
633     Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise);
634 
635     napi_value resourceName = nullptr;
636     napi_create_string_latin1(env, "setSlotByBundle", NAPI_AUTO_LENGTH, &resourceName);
637     // Asynchronous function call
638     napi_create_async_work(env,
639         nullptr,
640         resourceName,
641         [](napi_env env, void *data) {
642             ANS_LOGI("SetSlotByBundle napi_create_async_work start");
643             auto asynccallbackinfo = static_cast<AsyncCallbackInfoSetSlotByBundle *>(data);
644             if (asynccallbackinfo) {
645                 asynccallbackinfo->info.errorCode = NotificationHelper::UpdateNotificationSlots(
646                     asynccallbackinfo->params.option, asynccallbackinfo->params.slots);
647             }
648         },
649         [](napi_env env, napi_status status, void *data) {
650             ANS_LOGI("SetSlotByBundle napi_create_async_work end");
651             auto asynccallbackinfo = static_cast<AsyncCallbackInfoSetSlotByBundle *>(data);
652             if (asynccallbackinfo) {
653                 Common::ReturnCallbackPromise(env, asynccallbackinfo->info, Common::NapiGetNull(env));
654                 if (asynccallbackinfo->info.callback != nullptr) {
655                     napi_delete_reference(env, asynccallbackinfo->info.callback);
656                 }
657                 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
658                 delete asynccallbackinfo;
659                 asynccallbackinfo = nullptr;
660             }
661         },
662         (void *)asynccallbackinfo,
663         &asynccallbackinfo->asyncWork);
664 
665     napi_status status = napi_queue_async_work(env, asynccallbackinfo->asyncWork);
666     if (status != napi_ok) {
667         ANS_LOGE("napi_queue_async_work failed return: %{public}d", status);
668         if (asynccallbackinfo->info.callback != nullptr) {
669             napi_delete_reference(env, asynccallbackinfo->info.callback);
670         }
671         napi_delete_async_work(env, asynccallbackinfo->asyncWork);
672         delete asynccallbackinfo;
673         asynccallbackinfo = nullptr;
674         return Common::JSParaError(env, params.callback);
675     }
676 
677     if (asynccallbackinfo->info.isCallback) {
678         return Common::NapiGetNull(env);
679     } else {
680         return promise;
681     }
682 }
683 
684 
AsyncCompleteCallbackGetSlot(napi_env env,napi_status status,void * data)685 void AsyncCompleteCallbackGetSlot(napi_env env, napi_status status, void *data)
686 {
687     ANS_LOGI("GetSlot napi_create_async_work end");
688 
689     if (!data) {
690         ANS_LOGE("Invalid async callback data");
691         return;
692     }
693 
694     auto asynccallbackinfo = static_cast<AsyncCallbackInfoGetSlot *>(data);
695     if (asynccallbackinfo) {
696         napi_value result = Common::NapiGetNull(env);
697         if (asynccallbackinfo->info.errorCode == ERR_OK) {
698             if (asynccallbackinfo->slot != nullptr) {
699                 napi_create_object(env, &result);
700                 if (!Common::SetNotificationSlot(env, *asynccallbackinfo->slot, result)) {
701                     asynccallbackinfo->info.errorCode = ERROR;
702                     result = Common::NapiGetNull(env);
703                 }
704             }
705         }
706         Common::ReturnCallbackPromise(env, asynccallbackinfo->info, result);
707         if (asynccallbackinfo->info.callback != nullptr) {
708             napi_delete_reference(env, asynccallbackinfo->info.callback);
709         }
710         napi_delete_async_work(env, asynccallbackinfo->asyncWork);
711         delete asynccallbackinfo;
712         asynccallbackinfo = nullptr;
713     }
714 }
715 
GetSlot(napi_env env,napi_callback_info info)716 napi_value GetSlot(napi_env env, napi_callback_info info)
717 {
718     ANS_LOGI("enter");
719 
720     ParametersInfoGetSlot paras;
721     if (ParseParametersByGetSlot(env, info, paras) == nullptr) {
722         return Common::NapiGetUndefined(env);
723     }
724 
725     AsyncCallbackInfoGetSlot *asynccallbackinfo =
726         new (std::nothrow) AsyncCallbackInfoGetSlot {.env = env, .asyncWork = nullptr, .outType = paras.outType};
727     if (!asynccallbackinfo) {
728         return Common::JSParaError(env, paras.callback);
729     }
730     napi_value promise = nullptr;
731     Common::PaddingCallbackPromiseInfo(env, paras.callback, asynccallbackinfo->info, promise);
732 
733     napi_value resourceName = nullptr;
734     napi_create_string_latin1(env, "getSlot", NAPI_AUTO_LENGTH, &resourceName);
735     // Asynchronous function call
736     napi_create_async_work(env,
737         nullptr,
738         resourceName,
739         [](napi_env env, void *data) {
740             ANS_LOGI("GetSlot napi_create_async_work start");
741             auto asynccallbackinfo = static_cast<AsyncCallbackInfoGetSlot *>(data);
742             if (asynccallbackinfo) {
743                 asynccallbackinfo->info.errorCode =
744                     NotificationHelper::GetNotificationSlot(asynccallbackinfo->outType, asynccallbackinfo->slot);
745             }
746         },
747         AsyncCompleteCallbackGetSlot,
748         (void *)asynccallbackinfo,
749         &asynccallbackinfo->asyncWork);
750 
751     napi_status status = napi_queue_async_work(env, asynccallbackinfo->asyncWork);
752     if (status != napi_ok) {
753         ANS_LOGE("napi_queue_async_work failed return: %{public}d", status);
754         if (asynccallbackinfo->info.callback != nullptr) {
755             napi_delete_reference(env, asynccallbackinfo->info.callback);
756         }
757         napi_delete_async_work(env, asynccallbackinfo->asyncWork);
758         delete asynccallbackinfo;
759         asynccallbackinfo = nullptr;
760         return Common::JSParaError(env, paras.callback);
761     }
762 
763     if (asynccallbackinfo->info.isCallback) {
764         return Common::NapiGetNull(env);
765     } else {
766         return promise;
767     }
768 }
769 
GetSlotNumByBundle(napi_env env,napi_callback_info info)770 napi_value GetSlotNumByBundle(napi_env env, napi_callback_info info)
771 {
772     ANS_LOGI("enter");
773 
774     ParametersInfoGetSlotNumByBundle params {};
775     if (ParseParametersGetSlotNumByBundle(env, info, params) == nullptr) {
776         return Common::NapiGetUndefined(env);
777     }
778 
779     AsyncCallbackInfoGetSlotNumByBundle *asynccallbackinfo =
780         new (std::nothrow) AsyncCallbackInfoGetSlotNumByBundle {.env = env, .asyncWork = nullptr, .params = params};
781     if (!asynccallbackinfo) {
782         return Common::JSParaError(env, params.callback);
783     }
784     napi_value promise = nullptr;
785     Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise);
786 
787     napi_value resourceName = nullptr;
788     napi_create_string_latin1(env, "getSlotNumByBundle", NAPI_AUTO_LENGTH, &resourceName);
789     // Asynchronous function call
790     napi_create_async_work(env,
791         nullptr,
792         resourceName,
793         [](napi_env env, void *data) {
794             ANS_LOGI("GetSlotNumByBundle napi_create_async_work start");
795             auto asynccallbackinfo = reinterpret_cast<AsyncCallbackInfoGetSlotNumByBundle *>(data);
796             if (asynccallbackinfo) {
797                 asynccallbackinfo->info.errorCode = NotificationHelper::GetNotificationSlotNumAsBundle(
798                     asynccallbackinfo->params.option, asynccallbackinfo->num);
799             }
800         },
801         [](napi_env env, napi_status status, void *data) {
802             ANS_LOGI("GetSlotNumByBundle napi_create_async_work end");
803             auto asynccallbackinfo = static_cast<AsyncCallbackInfoGetSlotNumByBundle *>(data);
804             if (asynccallbackinfo) {
805                 napi_value result = nullptr;
806                 napi_create_uint32(env, asynccallbackinfo->num, &result);
807                 Common::ReturnCallbackPromise(env, asynccallbackinfo->info, result);
808                 if (asynccallbackinfo->info.callback != nullptr) {
809                     napi_delete_reference(env, asynccallbackinfo->info.callback);
810                 }
811                 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
812                 delete asynccallbackinfo;
813                 asynccallbackinfo = nullptr;
814             }
815         },
816         (void *)asynccallbackinfo,
817         &asynccallbackinfo->asyncWork);
818 
819     napi_status status = napi_queue_async_work(env, asynccallbackinfo->asyncWork);
820     if (status != napi_ok) {
821         ANS_LOGE("napi_queue_async_work failed return: %{public}d", status);
822         if (asynccallbackinfo->info.callback != nullptr) {
823             napi_delete_reference(env, asynccallbackinfo->info.callback);
824         }
825         napi_delete_async_work(env, asynccallbackinfo->asyncWork);
826         delete asynccallbackinfo;
827         asynccallbackinfo = nullptr;
828         return Common::JSParaError(env, params.callback);
829     }
830 
831     if (asynccallbackinfo->info.isCallback) {
832         return Common::NapiGetNull(env);
833     } else {
834         return promise;
835     }
836 }
837 
AsyncCompleteCallbackGetSlots(napi_env env,napi_status status,void * data)838 void AsyncCompleteCallbackGetSlots(napi_env env, napi_status status, void *data)
839 {
840     ANS_LOGI("enter");
841     if (!data) {
842         ANS_LOGE("Invalid async callback data");
843         return;
844     }
845     napi_value result = nullptr;
846     auto asynccallbackinfo = reinterpret_cast<AsyncCallbackInfoGetSlots *>(data);
847     if (asynccallbackinfo) {
848         if (asynccallbackinfo->info.errorCode != ERR_OK) {
849             result = Common::NapiGetNull(env);
850         } else {
851             napi_value arr = nullptr;
852             napi_create_array(env, &arr);
853             size_t count = 0;
854             for (auto vec : asynccallbackinfo->slots) {
855                 if (!vec) {
856                     ANS_LOGW("Invalid NotificationSlot object ptr");
857                     continue;
858                 }
859                 napi_value nSlot = nullptr;
860                 napi_create_object(env, &nSlot);
861                 if (!Common::SetNotificationSlot(env, *vec, nSlot)) {
862                     continue;
863                 }
864                 napi_set_element(env, arr, count, nSlot);
865                 count++;
866             }
867             ANS_LOGI("getSlots count = %{public}zu", count);
868             result = arr;
869             if ((count == 0) && (asynccallbackinfo->slots.size() > 0)) {
870                 asynccallbackinfo->info.errorCode = ERROR;
871                 result = Common::NapiGetNull(env);
872             }
873         }
874         Common::ReturnCallbackPromise(env, asynccallbackinfo->info, result);
875         if (asynccallbackinfo->info.callback != nullptr) {
876             napi_delete_reference(env, asynccallbackinfo->info.callback);
877         }
878         napi_delete_async_work(env, asynccallbackinfo->asyncWork);
879         delete asynccallbackinfo;
880         asynccallbackinfo = nullptr;
881     }
882 }
883 
GetSlots(napi_env env,napi_callback_info info)884 napi_value GetSlots(napi_env env, napi_callback_info info)
885 {
886     ANS_LOGI("enter");
887 
888     napi_ref callback = nullptr;
889     if (Common::ParseParaOnlyCallback(env, info, callback) == nullptr) {
890         return Common::NapiGetUndefined(env);
891     }
892 
893     auto asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoGetSlots {.env = env, .asyncWork = nullptr};
894     if (!asynccallbackinfo) {
895         return Common::JSParaError(env, callback);
896     }
897     napi_value promise = nullptr;
898     Common::PaddingCallbackPromiseInfo(env, callback, asynccallbackinfo->info, promise);
899 
900     napi_value resourceName = nullptr;
901     napi_create_string_latin1(env, "getSlots", NAPI_AUTO_LENGTH, &resourceName);
902     // Asynchronous function call
903     napi_create_async_work(env,
904         nullptr,
905         resourceName,
906         [](napi_env env, void *data) {
907             ANS_LOGI("GetSlots napi_create_async_work start");
908             auto asynccallbackinfo = reinterpret_cast<AsyncCallbackInfoGetSlots *>(data);
909             if (asynccallbackinfo) {
910                 asynccallbackinfo->info.errorCode = NotificationHelper::GetNotificationSlots(asynccallbackinfo->slots);
911             }
912         },
913         AsyncCompleteCallbackGetSlots,
914         (void *)asynccallbackinfo,
915         &asynccallbackinfo->asyncWork);
916 
917     napi_status status = napi_queue_async_work(env, asynccallbackinfo->asyncWork);
918     if (status != napi_ok) {
919         ANS_LOGE("napi_queue_async_work failed return: %{public}d", status);
920         if (asynccallbackinfo->info.callback != nullptr) {
921             napi_delete_reference(env, asynccallbackinfo->info.callback);
922         }
923         napi_delete_async_work(env, asynccallbackinfo->asyncWork);
924         delete asynccallbackinfo;
925         asynccallbackinfo = nullptr;
926         return Common::JSParaError(env, callback);
927     }
928 
929     if (asynccallbackinfo->info.isCallback) {
930         return Common::NapiGetNull(env);
931     } else {
932         return promise;
933     }
934 }
935 
AsyncCompleteCallbackGetSlotsByBundle(napi_env env,napi_status status,void * data)936 void AsyncCompleteCallbackGetSlotsByBundle(napi_env env, napi_status status, void *data)
937 {
938     ANS_LOGI("enter");
939     if (!data) {
940         ANS_LOGE("Invalid async callback data");
941         return;
942     }
943     napi_value result = nullptr;
944     auto asynccallbackinfo = reinterpret_cast<AsyncCallbackInfoGetSlotsByBundle *>(data);
945     if (asynccallbackinfo) {
946         if (asynccallbackinfo->info.errorCode != ERR_OK) {
947             result = Common::NapiGetNull(env);
948         } else {
949             napi_value arr = nullptr;
950             napi_create_array(env, &arr);
951             size_t count = 0;
952             for (auto vec : asynccallbackinfo->slots) {
953                 if (!vec) {
954                     ANS_LOGW("Invalid NotificationSlot object ptr");
955                     continue;
956                 }
957                 napi_value nSlot = nullptr;
958                 napi_create_object(env, &nSlot);
959                 if (!Common::SetNotificationSlot(env, *vec, nSlot)) {
960                     continue;
961                 }
962                 napi_set_element(env, arr, count, nSlot);
963                 count++;
964             }
965             ANS_LOGI("getSlots count = %{public}zu", count);
966             result = arr;
967             if ((count == 0) && (asynccallbackinfo->slots.size() > 0)) {
968                 asynccallbackinfo->info.errorCode = ERROR;
969                 result = Common::NapiGetNull(env);
970             }
971         }
972         Common::ReturnCallbackPromise(env, asynccallbackinfo->info, result);
973         if (asynccallbackinfo->info.callback != nullptr) {
974             napi_delete_reference(env, asynccallbackinfo->info.callback);
975         }
976         napi_delete_async_work(env, asynccallbackinfo->asyncWork);
977         delete asynccallbackinfo;
978         asynccallbackinfo = nullptr;
979     }
980 }
981 
GetSlotsByBundle(napi_env env,napi_callback_info info)982 napi_value GetSlotsByBundle(napi_env env, napi_callback_info info)
983 {
984     ANS_LOGI("enter");
985 
986     ParametersInfoGetSlotsByBundle params {};
987     if (ParseParametersGetSlotsByBundle(env, info, params) == nullptr) {
988         return Common::NapiGetUndefined(env);
989     }
990 
991     AsyncCallbackInfoGetSlotsByBundle *asynccallbackinfo =
992         new (std::nothrow) AsyncCallbackInfoGetSlotsByBundle {.env = env, .asyncWork = nullptr, .params = params};
993     if (!asynccallbackinfo) {
994         return Common::JSParaError(env, params.callback);
995     }
996     napi_value promise = nullptr;
997     Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise);
998 
999     napi_value resourceName = nullptr;
1000     napi_create_string_latin1(env, "getSlotsByBundle", NAPI_AUTO_LENGTH, &resourceName);
1001     // Asynchronous function call
1002     napi_create_async_work(env,
1003         nullptr,
1004         resourceName,
1005         [](napi_env env, void *data) {
1006             ANS_LOGI("GetSlotsByBundle napi_create_async_work start");
1007             auto asynccallbackinfo = reinterpret_cast<AsyncCallbackInfoGetSlotsByBundle *>(data);
1008             if (asynccallbackinfo) {
1009             asynccallbackinfo->info.errorCode = NotificationHelper::GetNotificationSlotsForBundle(
1010                 asynccallbackinfo->params.option, asynccallbackinfo->slots);
1011             }
1012         },
1013         AsyncCompleteCallbackGetSlotsByBundle,
1014         (void *)asynccallbackinfo,
1015         &asynccallbackinfo->asyncWork);
1016 
1017     napi_status status = napi_queue_async_work(env, asynccallbackinfo->asyncWork);
1018     if (status != napi_ok) {
1019         ANS_LOGE("napi_queue_async_work failed return: %{public}d", status);
1020         if (asynccallbackinfo->info.callback != nullptr) {
1021             napi_delete_reference(env, asynccallbackinfo->info.callback);
1022         }
1023         napi_delete_async_work(env, asynccallbackinfo->asyncWork);
1024         delete asynccallbackinfo;
1025         asynccallbackinfo = nullptr;
1026         return Common::JSParaError(env, params.callback);
1027     }
1028 
1029     if (asynccallbackinfo->info.isCallback) {
1030         return Common::NapiGetNull(env);
1031     } else {
1032         return promise;
1033     }
1034 }
1035 
RemoveSlot(napi_env env,napi_callback_info info)1036 napi_value RemoveSlot(napi_env env, napi_callback_info info)
1037 {
1038     ANS_LOGI("enter");
1039 
1040     ParametersInfoRemoveSlot paras;
1041     if (ParseParametersByRemoveSlot(env, info, paras) == nullptr) {
1042         return Common::NapiGetUndefined(env);
1043     }
1044 
1045     AsyncCallbackInfoRemoveSlot *asynccallbackinfo =
1046         new (std::nothrow) AsyncCallbackInfoRemoveSlot {.env = env, .asyncWork = nullptr, .outType = paras.outType};
1047     if (!asynccallbackinfo) {
1048         return Common::JSParaError(env, paras.callback);
1049     }
1050     napi_value promise = nullptr;
1051     Common::PaddingCallbackPromiseInfo(env, paras.callback, asynccallbackinfo->info, promise);
1052 
1053     napi_value resourceName = nullptr;
1054     napi_create_string_latin1(env, "removeSlot", NAPI_AUTO_LENGTH, &resourceName);
1055     // Asynchronous function call
1056     napi_create_async_work(env,
1057         nullptr,
1058         resourceName,
1059         [](napi_env env, void *data) {
1060             ANS_LOGI("removeSlot napi_create_async_work start");
1061             auto asynccallbackinfo = reinterpret_cast<AsyncCallbackInfoRemoveSlot *>(data);
1062             if (asynccallbackinfo) {
1063                 asynccallbackinfo->info.errorCode = NotificationHelper::RemoveNotificationSlot(
1064                     asynccallbackinfo->outType);
1065             }
1066         },
1067         [](napi_env env, napi_status status, void *data) {
1068             ANS_LOGI("removeSlot napi_create_async_work end");
1069             auto asynccallbackinfo = reinterpret_cast<AsyncCallbackInfoRemoveSlot *>(data);
1070             if (asynccallbackinfo) {
1071                 Common::ReturnCallbackPromise(env, asynccallbackinfo->info, Common::NapiGetNull(env));
1072                 if (asynccallbackinfo->info.callback != nullptr) {
1073                     napi_delete_reference(env, asynccallbackinfo->info.callback);
1074                 }
1075                 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
1076                 delete asynccallbackinfo;
1077                 asynccallbackinfo = nullptr;
1078             }
1079         },
1080         (void *)asynccallbackinfo,
1081         &asynccallbackinfo->asyncWork);
1082 
1083     napi_status status = napi_queue_async_work(env, asynccallbackinfo->asyncWork);
1084     if (status != napi_ok) {
1085         ANS_LOGE("napi_queue_async_work failed return: %{public}d", status);
1086         if (asynccallbackinfo->info.callback != nullptr) {
1087             napi_delete_reference(env, asynccallbackinfo->info.callback);
1088         }
1089         napi_delete_async_work(env, asynccallbackinfo->asyncWork);
1090         delete asynccallbackinfo;
1091         asynccallbackinfo = nullptr;
1092         return Common::JSParaError(env, paras.callback);
1093     }
1094 
1095     if (asynccallbackinfo->info.isCallback) {
1096         return Common::NapiGetNull(env);
1097     } else {
1098         return promise;
1099     }
1100 }
1101 
RemoveAllSlots(napi_env env,napi_callback_info info)1102 napi_value RemoveAllSlots(napi_env env, napi_callback_info info)
1103 {
1104     ANS_LOGI("enter");
1105 
1106     napi_ref callback = nullptr;
1107     if (Common::ParseParaOnlyCallback(env, info, callback) == nullptr) {
1108         return Common::NapiGetUndefined(env);
1109     }
1110 
1111     auto *asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoRemoveAllSlots {.env = env, .asyncWork = nullptr};
1112     if (!asynccallbackinfo) {
1113         return Common::JSParaError(env, callback);
1114     }
1115     napi_value promise = nullptr;
1116     Common::PaddingCallbackPromiseInfo(env, callback, asynccallbackinfo->info, promise);
1117 
1118     napi_value resourceName = nullptr;
1119     napi_create_string_latin1(env, "removeAll", NAPI_AUTO_LENGTH, &resourceName);
1120     // Asynchronous function call
1121     napi_create_async_work(env,
1122         nullptr,
1123         resourceName,
1124         [](napi_env env, void *data) {
1125             ANS_LOGI("RemoveAllSlots napi_create_async_work start");
1126             auto asynccallbackinfo = reinterpret_cast<AsyncCallbackInfoRemoveAllSlots *>(data);
1127             if (asynccallbackinfo) {
1128                 asynccallbackinfo->info.errorCode = NotificationHelper::RemoveAllSlots();
1129             }
1130         },
1131         [](napi_env env, napi_status status, void *data) {
1132             ANS_LOGI("RemoveAllSlots napi_create_async_work end");
1133             auto asynccallbackinfo = reinterpret_cast<AsyncCallbackInfoRemoveAllSlots *>(data);
1134             if (asynccallbackinfo) {
1135                 Common::ReturnCallbackPromise(env, asynccallbackinfo->info, Common::NapiGetNull(env));
1136                 if (asynccallbackinfo->info.callback != nullptr) {
1137                     napi_delete_reference(env, asynccallbackinfo->info.callback);
1138                 }
1139                 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
1140                 delete asynccallbackinfo;
1141                 asynccallbackinfo = nullptr;
1142             }
1143         },
1144         (void *)asynccallbackinfo,
1145         &asynccallbackinfo->asyncWork);
1146 
1147     napi_status status = napi_queue_async_work(env, asynccallbackinfo->asyncWork);
1148     if (status != napi_ok) {
1149         ANS_LOGE("napi_queue_async_work failed return: %{public}d", status);
1150         if (asynccallbackinfo->info.callback != nullptr) {
1151             napi_delete_reference(env, asynccallbackinfo->info.callback);
1152         }
1153         napi_delete_async_work(env, asynccallbackinfo->asyncWork);
1154         delete asynccallbackinfo;
1155         asynccallbackinfo = nullptr;
1156         return Common::JSParaError(env, callback);
1157     }
1158 
1159     if (asynccallbackinfo->info.isCallback) {
1160         return Common::NapiGetNull(env);
1161     } else {
1162         return promise;
1163     }
1164 }
1165 
ParseParametersEnableSlot(const napi_env & env,const napi_callback_info & info,ParametersInfoEnableSlot & params)1166 napi_value ParseParametersEnableSlot(
1167     const napi_env &env, const napi_callback_info &info, ParametersInfoEnableSlot &params)
1168 {
1169     ANS_LOGI("enter");
1170 
1171     size_t argc = SET_ENABLE_SLOT_MAX_PARA;
1172     napi_value argv[SET_ENABLE_SLOT_MAX_PARA] = {nullptr};
1173     napi_value thisVar = nullptr;
1174     napi_valuetype valuetype = napi_undefined;
1175     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
1176     if (argc < SET_ENABLE_SLOT_MAX_PARA - 1) {
1177         ANS_LOGW("Wrong number of arguments.");
1178         return nullptr;
1179     }
1180 
1181     // argv[0]: bundle
1182     NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
1183     if (valuetype != napi_object) {
1184         ANS_LOGW("Wrong argument type. Object expected.");
1185         return nullptr;
1186     }
1187     auto retValue = Common::GetBundleOption(env, argv[PARAM0], params.option);
1188     if (retValue == nullptr) {
1189         ANS_LOGE("GetBundleOption failed.");
1190         return nullptr;
1191     }
1192 
1193     // argv[1]: SlotType
1194     NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
1195     if (valuetype != napi_number) {
1196         ANS_LOGW("Wrong argument type. Number expected.");
1197         return nullptr;
1198     }
1199     int slotType = 0;
1200     napi_get_value_int32(env, argv[PARAM1], &slotType);
1201     if (!Common::SlotTypeJSToC(SlotType(slotType), params.outType)) {
1202         return nullptr;
1203     }
1204 
1205     // argv[2]: enable
1206     NAPI_CALL(env, napi_typeof(env, argv[PARAM2], &valuetype));
1207     if (valuetype != napi_boolean) {
1208         ANS_LOGW("Wrong argument type. Bool expected.");
1209         return nullptr;
1210     }
1211     napi_get_value_bool(env, argv[PARAM2], &params.enable);
1212 
1213     // argv[3]:callback
1214     if (argc >= SET_ENABLE_SLOT_MAX_PARA) {
1215         NAPI_CALL(env, napi_typeof(env, argv[PARAM3], &valuetype));
1216         if (valuetype != napi_function) {
1217             ANS_LOGW("Wrong argument type. Function expected.");
1218             return nullptr;
1219         }
1220         napi_create_reference(env, argv[PARAM3], 1, &params.callback);
1221     }
1222 
1223     return Common::NapiGetNull(env);
1224 }
1225 
EnableNotificationSlot(napi_env env,napi_callback_info info)1226 napi_value EnableNotificationSlot(napi_env env, napi_callback_info info)
1227 {
1228     ANS_LOGI("enter");
1229 
1230     ParametersInfoEnableSlot params {};
1231     if (ParseParametersEnableSlot(env, info, params) == nullptr) {
1232         return Common::NapiGetUndefined(env);
1233     }
1234 
1235     AsyncCallbackInfoInfoEnableSlot *asynccallbackinfo =
1236         new (std::nothrow) AsyncCallbackInfoInfoEnableSlot {.env = env, .asyncWork = nullptr, .params = params};
1237     if (!asynccallbackinfo) {
1238         return Common::JSParaError(env, params.callback);
1239     }
1240     napi_value promise = nullptr;
1241     Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise);
1242 
1243     napi_value resourceName = nullptr;
1244     napi_create_string_latin1(env, "EnableNotificationSlot", NAPI_AUTO_LENGTH, &resourceName);
1245     // Asynchronous function call
1246     napi_create_async_work(env,
1247         nullptr,
1248         resourceName,
1249         [](napi_env env, void *data) {
1250             ANS_LOGI("EnableNotificationSlot napi_create_async_work start");
1251             auto asynccallbackinfo = static_cast<AsyncCallbackInfoInfoEnableSlot *>(data);
1252             if (asynccallbackinfo) {
1253                 asynccallbackinfo->info.errorCode = NotificationHelper::SetEnabledForBundleSlot(
1254                     asynccallbackinfo->params.option, asynccallbackinfo->params.outType,
1255                     asynccallbackinfo->params.enable);
1256             }
1257         },
1258         [](napi_env env, napi_status status, void *data) {
1259             ANS_LOGI("EnableNotificationSlot napi_create_async_work end");
1260             auto asynccallbackinfo = static_cast<AsyncCallbackInfoInfoEnableSlot *>(data);
1261             if (asynccallbackinfo) {
1262                 Common::ReturnCallbackPromise(env, asynccallbackinfo->info, Common::NapiGetNull(env));
1263                 if (asynccallbackinfo->info.callback != nullptr) {
1264                     napi_delete_reference(env, asynccallbackinfo->info.callback);
1265                 }
1266                 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
1267                 delete asynccallbackinfo;
1268                 asynccallbackinfo = nullptr;
1269             }
1270         },
1271         (void *)asynccallbackinfo,
1272         &asynccallbackinfo->asyncWork);
1273 
1274     napi_status status = napi_queue_async_work(env, asynccallbackinfo->asyncWork);
1275     if (status != napi_ok) {
1276         ANS_LOGE("napi_queue_async_work failed return: %{public}d", status);
1277         if (asynccallbackinfo->info.callback != nullptr) {
1278             napi_delete_reference(env, asynccallbackinfo->info.callback);
1279         }
1280         napi_delete_async_work(env, asynccallbackinfo->asyncWork);
1281         delete asynccallbackinfo;
1282         asynccallbackinfo = nullptr;
1283         return Common::JSParaError(env, params.callback);
1284     }
1285 
1286     if (asynccallbackinfo->info.isCallback) {
1287         return Common::NapiGetNull(env);
1288     } else {
1289         return promise;
1290     }
1291 }
1292 
ParseParametersIsEnableSlot(const napi_env & env,const napi_callback_info & info,ParametersInfoIsEnableSlot & params)1293 napi_value ParseParametersIsEnableSlot(
1294     const napi_env &env, const napi_callback_info &info, ParametersInfoIsEnableSlot &params)
1295 {
1296     ANS_LOGI("enter");
1297 
1298     size_t argc = GET_ENABLE_SLOT_MAX_PARA;
1299     napi_value argv[GET_ENABLE_SLOT_MAX_PARA] = {nullptr};
1300     napi_value thisVar = nullptr;
1301     napi_valuetype valuetype = napi_undefined;
1302     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
1303     if (argc < GET_ENABLE_SLOT_MAX_PARA - 1) {
1304         ANS_LOGW("Wrong number of arguments.");
1305         return nullptr;
1306     }
1307 
1308     // argv[0]: bundle
1309     NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
1310     if (valuetype != napi_object) {
1311         ANS_LOGW("Wrong argument type. Object expected.");
1312         return nullptr;
1313     }
1314     auto retValue = Common::GetBundleOption(env, argv[PARAM0], params.option);
1315     if (retValue == nullptr) {
1316         ANS_LOGE("GetBundleOption failed.");
1317         return nullptr;
1318     }
1319 
1320     // argv[1]: SlotType
1321     NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
1322     if (valuetype != napi_number) {
1323         ANS_LOGW("Wrong argument type. Number expected.");
1324         return nullptr;
1325     }
1326     int slotType = 0;
1327     napi_get_value_int32(env, argv[PARAM1], &slotType);
1328     if (!Common::SlotTypeJSToC(SlotType(slotType), params.outType)) {
1329         return nullptr;
1330     }
1331 
1332     // argv[2]:callback
1333     if (argc >= GET_ENABLE_SLOT_MAX_PARA) {
1334         NAPI_CALL(env, napi_typeof(env, argv[PARAM2], &valuetype));
1335         if (valuetype != napi_function) {
1336             ANS_LOGW("Wrong argument type. Function expected.");
1337             return nullptr;
1338         }
1339         napi_create_reference(env, argv[PARAM2], 1, &params.callback);
1340     }
1341 
1342     return Common::NapiGetNull(env);
1343 }
1344 
IsEnableNotificationSlot(napi_env env,napi_callback_info info)1345 napi_value IsEnableNotificationSlot(napi_env env, napi_callback_info info)
1346 {
1347     ANS_LOGI("enter");
1348 
1349     ParametersInfoIsEnableSlot params {};
1350     if (ParseParametersIsEnableSlot(env, info, params) == nullptr) {
1351         return Common::NapiGetUndefined(env);
1352     }
1353 
1354     AsyncCallbackInfoInfoIsEnableSlot *asynccallbackinfo =
1355         new (std::nothrow) AsyncCallbackInfoInfoIsEnableSlot {.env = env, .asyncWork = nullptr, .params = params};
1356     if (!asynccallbackinfo) {
1357         return Common::JSParaError(env, params.callback);
1358     }
1359     napi_value promise = nullptr;
1360     Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise);
1361 
1362     napi_value resourceName = nullptr;
1363     napi_create_string_latin1(env, "IsEnableNotificationSlot", NAPI_AUTO_LENGTH, &resourceName);
1364     // Asynchronous function call
1365     napi_create_async_work(env,
1366         nullptr,
1367         resourceName,
1368         [](napi_env env, void *data) {
1369             ANS_LOGI("IsEnableNotificationSlot napi_create_async_work start");
1370             auto asynccallbackinfo = static_cast<AsyncCallbackInfoInfoIsEnableSlot *>(data);
1371             if (asynccallbackinfo) {
1372                 asynccallbackinfo->info.errorCode = NotificationHelper::GetEnabledForBundleSlot(
1373                     asynccallbackinfo->params.option, asynccallbackinfo->params.outType, asynccallbackinfo->isEnable);
1374             }
1375         },
1376         [](napi_env env, napi_status status, void *data) {
1377             ANS_LOGI("IsEnableNotificationSlot napi_create_async_work end");
1378             auto asynccallbackinfo = static_cast<AsyncCallbackInfoInfoIsEnableSlot *>(data);
1379             if (asynccallbackinfo) {
1380                 napi_value result = nullptr;
1381                 napi_get_boolean(env, asynccallbackinfo->isEnable, &result);
1382                 Common::ReturnCallbackPromise(env, asynccallbackinfo->info, result);
1383                 if (asynccallbackinfo->info.callback != nullptr) {
1384                     napi_delete_reference(env, asynccallbackinfo->info.callback);
1385                 }
1386                 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
1387                 delete asynccallbackinfo;
1388                 asynccallbackinfo = nullptr;
1389             }
1390         },
1391         (void *)asynccallbackinfo,
1392         &asynccallbackinfo->asyncWork);
1393 
1394     napi_status status = napi_queue_async_work(env, asynccallbackinfo->asyncWork);
1395     if (status != napi_ok) {
1396         ANS_LOGE("napi_queue_async_work failed return: %{public}d", status);
1397         if (asynccallbackinfo->info.callback != nullptr) {
1398             napi_delete_reference(env, asynccallbackinfo->info.callback);
1399         }
1400         napi_delete_async_work(env, asynccallbackinfo->asyncWork);
1401         delete asynccallbackinfo;
1402         asynccallbackinfo = nullptr;
1403         return Common::JSParaError(env, params.callback);
1404     }
1405 
1406     if (asynccallbackinfo->info.isCallback) {
1407         return Common::NapiGetNull(env);
1408     } else {
1409         return promise;
1410     }
1411 }
1412 }  // namespace NotificationNapi
1413 }  // namespace OHOS