1 /*
2  * Copyright (c) 2022-2024 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_common_event.h"
17 #include "event_log_wrapper.h"
18 #include "ces_inner_error_code.h"
19 #include "napi_common_util.h"
20 #include "node_api.h"
21 
22 namespace OHOS {
23 namespace EventManagerFwkNapi {
24 using namespace OHOS::EventFwk;
25 using namespace OHOS::Notification;
26 
27 static const uint32_t SET_CODE_MAX_PARA = 2;
28 static const uint32_t SET_DATA_MAX_PARA = 2;
29 static const uint32_t SET_CODE_AND_DATA_MAX_PARA = 3;
30 static const int32_t ARGS_TWO_EVENT = 2;
31 static const int32_t INDEX_ZERO = 0;
32 static const uint32_t INDEX_ONE = 1;
33 static const uint32_t ARGC_ONE = 1;
34 static const uint32_t ARGC_TWO = 2;
35 
AsyncCompleteCallbackRemoveStickyCommonEvent(napi_env env,napi_status status,void * data)36 void AsyncCompleteCallbackRemoveStickyCommonEvent(napi_env env, napi_status status, void *data)
37 {
38     EVENT_LOGD("enter");
39     if (!data) {
40         EVENT_LOGE("Invalid async callback data");
41         return;
42     }
43     AsyncCallbackRemoveSticky *asyncCallbackInfo = static_cast<AsyncCallbackRemoveSticky *>(data);
44     ReturnCallbackPromise(env, asyncCallbackInfo->info, NapiGetNull(env));
45     if (asyncCallbackInfo->info.callback != nullptr) {
46         napi_delete_reference(env, asyncCallbackInfo->info.callback);
47     }
48 
49     napi_delete_async_work(env, asyncCallbackInfo->asyncWork);
50     delete asyncCallbackInfo;
51     asyncCallbackInfo = nullptr;
52 }
53 
SetPublisherPermissionResult(const napi_env & env,const std::string & permission,napi_value & commonEventSubscribeInfo)54 void SetPublisherPermissionResult(
55     const napi_env &env, const std::string &permission, napi_value &commonEventSubscribeInfo)
56 {
57     EVENT_LOGD("SetPublisherPermissionResult excute");
58 
59     napi_value value = nullptr;
60     napi_create_string_utf8(env, permission.c_str(), NAPI_AUTO_LENGTH, &value);
61 
62     napi_set_named_property(env, commonEventSubscribeInfo, "publisherPermission", value);
63 }
64 
SetPublisherDeviceIdResult(const napi_env & env,const std::string & deviceId,napi_value & commonEventSubscribeInfo)65 void SetPublisherDeviceIdResult(const napi_env &env, const std::string &deviceId, napi_value &commonEventSubscribeInfo)
66 {
67     EVENT_LOGD("SetPublisherDeviceIdResult excute");
68 
69     napi_value value = nullptr;
70     napi_create_string_utf8(env, deviceId.c_str(), NAPI_AUTO_LENGTH, &value);
71 
72     napi_set_named_property(env, commonEventSubscribeInfo, "publisherDeviceId", value);
73 }
74 
SetPublisherUserIdResult(const napi_env & env,const int32_t & userId,napi_value & commonEventSubscribeInfo)75 void SetPublisherUserIdResult(const napi_env &env, const int32_t &userId, napi_value &commonEventSubscribeInfo)
76 {
77     EVENT_LOGD("SetPublisherUserIdResult excute");
78 
79     napi_value value = nullptr;
80     napi_create_int32(env, userId, &value);
81 
82     napi_set_named_property(env, commonEventSubscribeInfo, "userId", value);
83 }
84 
SetPublisherPriorityResult(const napi_env & env,const int32_t & priority,napi_value & commonEventSubscribeInfo)85 void SetPublisherPriorityResult(const napi_env &env, const int32_t &priority, napi_value &commonEventSubscribeInfo)
86 {
87     EVENT_LOGD("SetPublisherPriorityResult excute");
88 
89     napi_value value = nullptr;
90     napi_create_int32(env, priority, &value);
91 
92     napi_set_named_property(env, commonEventSubscribeInfo, "priority", value);
93 }
94 
SetPublisherBundleNameResult(const napi_env & env,const std::string & publisherBundleName,napi_value & commonEventSubscribeInfo)95 void SetPublisherBundleNameResult(
96     const napi_env &env, const std::string &publisherBundleName, napi_value &commonEventSubscribeInfo)
97 {
98     EVENT_LOGD("Called.");
99     napi_value value = nullptr;
100     napi_create_string_utf8(env, publisherBundleName.c_str(), NAPI_AUTO_LENGTH, &value);
101 
102     napi_set_named_property(env, commonEventSubscribeInfo, "publisherBundleName", value);
103 }
104 
SetNapiResult(const napi_env & env,const AsyncCallbackInfoSubscribeInfo * asyncCallbackInfo,napi_value & result)105 void SetNapiResult(const napi_env &env, const AsyncCallbackInfoSubscribeInfo *asyncCallbackInfo, napi_value &result)
106 {
107     EVENT_LOGD("SetNapiResult excute");
108 
109     SetEventsResult(env, asyncCallbackInfo->events, result);
110     SetPublisherPermissionResult(env, asyncCallbackInfo->permission, result);
111     SetPublisherDeviceIdResult(env, asyncCallbackInfo->deviceId, result);
112     SetPublisherUserIdResult(env, asyncCallbackInfo->userId, result);
113     SetPublisherPriorityResult(env, asyncCallbackInfo->priority, result);
114     SetPublisherBundleNameResult(env, asyncCallbackInfo->publisherBundleName, result);
115 }
116 
SetCode(napi_env env,napi_callback_info info)117 napi_value SetCode(napi_env env, napi_callback_info info)
118 {
119     EVENT_LOGD("SetCode excute");
120     size_t argc = SET_CODE_MAX_PARA;
121     napi_value argv[SET_CODE_MAX_PARA] = {nullptr};
122     napi_value thisVar = nullptr;
123     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
124 
125     napi_ref callback = nullptr;
126     int32_t code = 0;
127     if (ParseParametersBySetCode(env, argv, argc, code, callback) == nullptr) {
128         NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID);
129         return NapiGetNull(env);
130     }
131 
132     AsyncCallbackInfoSetCode *asyncCallbackInfo = new (std::nothrow)
133         AsyncCallbackInfoSetCode {.env = env, .asyncWork = nullptr, .code = code};
134     if (asyncCallbackInfo == nullptr) {
135         EVENT_LOGE("Create asyncCallbackInfo is defeat.");
136         return NapiGetNull(env);
137     }
138 
139     asyncCallbackInfo->subscriber = GetSubscriber(env, thisVar);
140     if (asyncCallbackInfo->subscriber == nullptr) {
141         EVENT_LOGE("subscriber is null");
142         delete asyncCallbackInfo;
143         return NapiGetNull(env);
144     }
145 
146     napi_value promise = nullptr;
147     PaddingAsyncCallbackInfoSetCode(env, argc, asyncCallbackInfo, callback, promise);
148 
149     napi_value resourceName = nullptr;
150     napi_create_string_latin1(env, "setCode", NAPI_AUTO_LENGTH, &resourceName);
151     // Asynchronous function call
152     napi_create_async_work(env,
153         nullptr,
154         resourceName,
155         [](napi_env env, void *data) {
156             EVENT_LOGD("SetCode napi_create_async_work excute");
157             AsyncCallbackInfoSetCode *asyncCallbackInfo = static_cast<AsyncCallbackInfoSetCode *>(data);
158             if (asyncCallbackInfo) {
159                 std::shared_ptr<AsyncCommonEventResult> asyncResult = GetAsyncResult(
160                     asyncCallbackInfo->subscriber.get());
161                 if (asyncResult) {
162                     asyncCallbackInfo->info.errorCode = asyncResult->SetCode(asyncCallbackInfo->code) ?
163                         NO_ERROR : ERR_CES_FAILED;
164                 }
165             }
166         },
167         [](napi_env env, napi_status status, void *data) {
168             EVENT_LOGD("SetCode napi_create_async_work complete");
169             AsyncCallbackInfoSetCode *asyncCallbackInfo = static_cast<AsyncCallbackInfoSetCode *>(data);
170             if (asyncCallbackInfo) {
171                 ReturnCallbackPromise(env, asyncCallbackInfo->info, NapiGetNull(env));
172                 if (asyncCallbackInfo->info.callback != nullptr) {
173                     napi_delete_reference(env, asyncCallbackInfo->info.callback);
174                 }
175                 napi_delete_async_work(env, asyncCallbackInfo->asyncWork);
176                 delete asyncCallbackInfo;
177                 asyncCallbackInfo = nullptr;
178                 EVENT_LOGD("NapiSetCode work complete end.");
179             }
180         },
181         (void *)asyncCallbackInfo,
182         &asyncCallbackInfo->asyncWork);
183 
184     NAPI_CALL(env, napi_queue_async_work_with_qos(env, asyncCallbackInfo->asyncWork, napi_qos_user_initiated));
185 
186     if (asyncCallbackInfo->info.isCallback) {
187         EVENT_LOGD("Delete napiSetCode callback reference.");
188         return NapiGetNull(env);
189     } else {
190         return promise;
191     }
192 }
193 
SetData(napi_env env,napi_callback_info info)194 napi_value SetData(napi_env env, napi_callback_info info)
195 {
196     EVENT_LOGD("SetData excute");
197     size_t argc = SET_DATA_MAX_PARA;
198     napi_value argv[SET_DATA_MAX_PARA] = {nullptr};
199     napi_value thisVar = nullptr;
200     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
201 
202     napi_ref callback = nullptr;
203     std::string data;
204     if (ParseParametersBySetData(env, argv, argc, data, callback) == nullptr) {
205         NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID);
206         return NapiGetNull(env);
207     }
208 
209     AsyncCallbackInfoSetData *asyncCallbackInfo = new (std::nothrow)
210         AsyncCallbackInfoSetData {.env = env, .asyncWork = nullptr, .data = data};
211     if (asyncCallbackInfo == nullptr) {
212         EVENT_LOGE("asyncCallbackInfo is defeat");
213         return NapiGetNull(env);
214     }
215 
216     asyncCallbackInfo->subscriber = GetSubscriber(env, thisVar);
217     if (asyncCallbackInfo->subscriber == nullptr) {
218         EVENT_LOGE("subscriber is null");
219         delete asyncCallbackInfo;
220         return NapiGetNull(env);
221     }
222 
223     napi_value promise = nullptr;
224     PaddingAsyncCallbackInfoSetData(env, argc, asyncCallbackInfo, callback, promise);
225 
226     napi_value resourceName = nullptr;
227     napi_create_string_latin1(env, "setData", NAPI_AUTO_LENGTH, &resourceName);
228     // Asynchronous function call
229     napi_create_async_work(env,
230         nullptr,
231         resourceName,
232         [](napi_env env, void *data) {
233             EVENT_LOGD("SetData work excute");
234             AsyncCallbackInfoSetData *asyncCallbackInfo = static_cast<AsyncCallbackInfoSetData *>(data);
235             if (asyncCallbackInfo) {
236                 std::shared_ptr<AsyncCommonEventResult> asyncResult = GetAsyncResult(
237                     asyncCallbackInfo->subscriber.get());
238                 if (asyncResult) {
239                     asyncCallbackInfo->info.errorCode = asyncResult->SetData(asyncCallbackInfo->data) ?
240                         NO_ERROR : ERR_CES_FAILED;
241                 }
242             }
243         },
244         [](napi_env env, napi_status status, void *data) {
245             EVENT_LOGD("SetData work complete");
246             AsyncCallbackInfoSetData *asyncCallbackInfo = static_cast<AsyncCallbackInfoSetData *>(data);
247             if (asyncCallbackInfo) {
248                 ReturnCallbackPromise(env, asyncCallbackInfo->info, NapiGetNull(env));
249                 if (asyncCallbackInfo->info.callback != nullptr) {
250                     napi_delete_reference(env, asyncCallbackInfo->info.callback);
251                 }
252                 napi_delete_async_work(env, asyncCallbackInfo->asyncWork);
253                 delete asyncCallbackInfo;
254                 asyncCallbackInfo = nullptr;
255             }
256             EVENT_LOGD("NapiSetData work complete end.");
257         },
258         (void *)asyncCallbackInfo,
259         &asyncCallbackInfo->asyncWork);
260 
261     NAPI_CALL(env, napi_queue_async_work_with_qos(env, asyncCallbackInfo->asyncWork, napi_qos_user_initiated));
262 
263     if (asyncCallbackInfo->info.isCallback) {
264         EVENT_LOGD("Delete napiSetData callback reference.");
265         return NapiGetNull(env);
266     } else {
267         return promise;
268     }
269 }
270 
SetCodeAndData(napi_env env,napi_callback_info info)271 napi_value SetCodeAndData(napi_env env, napi_callback_info info)
272 {
273     EVENT_LOGD("SetCodeAndData excute");
274     size_t argc = SET_CODE_AND_DATA_MAX_PARA;
275     napi_value argv[SET_CODE_AND_DATA_MAX_PARA] = {nullptr};
276     napi_value thisVar = nullptr;
277     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
278 
279     napi_ref callback = nullptr;
280     int32_t code = 0;
281     std::string data;
282     if (ParseParametersBySetCodeAndData(env, argv, argc, code, data, callback) == nullptr) {
283         NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID);
284         return NapiGetNull(env);
285     }
286 
287     AsyncCallbackInfoSetCodeAndData *asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfoSetCodeAndData {
288         .env = env, .asyncWork = nullptr, .code = code, .data = data};
289     if (asyncCallbackInfo == nullptr) {
290         EVENT_LOGE("asyncCallbackInfo is nullptr");
291         return NapiGetNull(env);
292     }
293 
294     asyncCallbackInfo->subscriber = GetSubscriber(env, thisVar);
295     if (asyncCallbackInfo->subscriber == nullptr) {
296         EVENT_LOGE("subscriber is defeat");
297         delete asyncCallbackInfo;
298         return NapiGetNull(env);
299     }
300 
301     napi_value promise = nullptr;
302     PaddingAsyncCallbackInfoSetCodeAndData(env, argc, asyncCallbackInfo, callback, promise);
303 
304     napi_value resourceName = nullptr;
305     napi_create_string_latin1(env, "setCodeAndData", NAPI_AUTO_LENGTH, &resourceName);
306     // Calling Asynchronous functions
307     napi_create_async_work(env,
308         nullptr,
309         resourceName,
310         [](napi_env env, void *data) {
311             EVENT_LOGD("SetCodeAndData napi_create_async_work start");
312             AsyncCallbackInfoSetCodeAndData *asyncCallbackInfo = static_cast<AsyncCallbackInfoSetCodeAndData *>(data);
313             if (asyncCallbackInfo) {
314                 std::shared_ptr<AsyncCommonEventResult> asyncResult = GetAsyncResult(
315                     asyncCallbackInfo->subscriber.get());
316                 if (asyncResult) {
317                     asyncCallbackInfo->info.errorCode = asyncResult->SetCodeAndData(
318                         asyncCallbackInfo->code, asyncCallbackInfo->data) ? NO_ERROR : ERR_CES_FAILED;
319                 }
320             }
321         },
322         [](napi_env env, napi_status status, void *data) {
323             EVENT_LOGD("SetCodeAndData napi_create_async_work complete");
324             AsyncCallbackInfoSetCodeAndData *asyncCallbackInfo = static_cast<AsyncCallbackInfoSetCodeAndData *>(data);
325             if (asyncCallbackInfo) {
326                 ReturnCallbackPromise(env, asyncCallbackInfo->info, NapiGetNull(env));
327                 if (asyncCallbackInfo->info.callback != nullptr) {
328                     napi_delete_reference(env, asyncCallbackInfo->info.callback);
329                 }
330                 napi_delete_async_work(env, asyncCallbackInfo->asyncWork);
331                 delete asyncCallbackInfo;
332                 asyncCallbackInfo = nullptr;
333                 EVENT_LOGD("asyncCallbackInfo is null");
334             }
335         },
336         (void *)asyncCallbackInfo,
337         &asyncCallbackInfo->asyncWork);
338 
339     NAPI_CALL(env, napi_queue_async_work_with_qos(env, asyncCallbackInfo->asyncWork, napi_qos_user_initiated));
340 
341     if (asyncCallbackInfo->info.isCallback) {
342         EVENT_LOGD("Delete napiSetCodeAndData callback reference.");
343         return NapiGetNull(env);
344     } else {
345         return promise;
346     }
347 }
348 
SetStaticSubscriberState(napi_env env,napi_callback_info info)349 napi_value NapiStaticSubscribe::SetStaticSubscriberState(napi_env env, napi_callback_info info)
350 {
351     NapiStaticSubscribe *me = AbilityRuntime::CheckParamsAndGetThis<NapiStaticSubscribe>(env, info);
352     return (me != nullptr) ? me->OnSetStaticSubscriberState(env, info) : nullptr;
353 }
354 
SetEventsResult(const napi_env & env,const std::vector<std::string> & events,napi_value & commonEventSubscribeInfo)355 void SetEventsResult(const napi_env &env, const std::vector<std::string> &events, napi_value &commonEventSubscribeInfo)
356 {
357     EVENT_LOGD("SetEventsResult start");
358 
359     napi_value value = nullptr;
360 
361     if (events.size() > 0) {
362         napi_value nEvents = nullptr;
363         if (napi_create_array(env, &nEvents) != napi_ok) {
364             return;
365         }
366         size_t index = 0;
367         for (auto event : events) {
368             EVENT_LOGD("SetEventsResult event: %{public}s", event.c_str());
369             napi_create_string_utf8(env, event.c_str(), NAPI_AUTO_LENGTH, &value);
370             napi_set_element(env, nEvents, index, value);
371             index++;
372         }
373 
374         napi_set_named_property(env, commonEventSubscribeInfo, "events", nEvents);
375     }
376 
377     EVENT_LOGD("SetEventsResult end");
378 }
379 
SetCallback(const napi_env & env,const napi_ref & callbackIn,const int32_t & errorCode,const napi_value & result)380 void SetCallback(const napi_env &env, const napi_ref &callbackIn, const int32_t &errorCode, const napi_value &result)
381 {
382     EVENT_LOGD("Return error: %{public}d", errorCode);
383     napi_value undefined = nullptr;
384     napi_get_undefined(env, &undefined);
385 
386     napi_value callback = nullptr;
387     napi_value resultout = nullptr;
388     napi_get_reference_value(env, callbackIn, &callback);
389 
390     napi_value results[ARGS_TWO_EVENT] = {nullptr};
391     results[INDEX_ZERO] = GetCallbackErrorValue(env, errorCode);
392     results[INDEX_ONE] = result;
393 
394     NAPI_CALL_RETURN_VOID(env,
395         napi_call_function(env, undefined, callback, ARGS_TWO_EVENT, &results[INDEX_ZERO], &resultout));
396 }
397 
SetCallback(const napi_env & env,const napi_ref & callbackIn,const napi_value & result)398 void SetCallback(const napi_env &env, const napi_ref &callbackIn, const napi_value &result)
399 {
400     napi_value undefined = nullptr;
401     napi_get_undefined(env, &undefined);
402 
403     napi_value callback = nullptr;
404     napi_value resultout = nullptr;
405     napi_get_reference_value(env, callbackIn, &callback);
406 
407     napi_value results[ARGS_TWO_EVENT] = {nullptr};
408     results[INDEX_ZERO] = GetCallbackErrorValue(env, NO_ERROR);
409     results[INDEX_ONE] = result;
410 
411     NAPI_CALL_RETURN_VOID(env,
412         napi_call_function(env, undefined, callback, ARGS_TWO_EVENT, &results[INDEX_ZERO], &resultout));
413 }
414 
SetPromise(const napi_env & env,const napi_deferred & deferred,const int32_t & errorCode,const napi_value & result)415 void SetPromise(const napi_env &env, const napi_deferred &deferred, const int32_t &errorCode, const napi_value &result)
416 {
417     if (errorCode == NO_ERROR) {
418         napi_resolve_deferred(env, deferred, result);
419     } else {
420         napi_reject_deferred(env, deferred, GetCallbackErrorValue(env, errorCode));
421     }
422 }
423 
ReturnCallbackPromise(const napi_env & env,const CallbackPromiseInfo & info,const napi_value & result)424 void ReturnCallbackPromise(const napi_env &env, const CallbackPromiseInfo &info, const napi_value &result)
425 {
426     EVENT_LOGD("ReturnCallbackPromise excute");
427 
428     if (info.isCallback) {
429         SetCallback(env, info.callback, info.errorCode, result);
430     } else {
431         SetPromise(env, info.deferred, info.errorCode, result);
432     }
433 }
434 
ParseSetStaticSubscriberStateParam(napi_env env,const napi_callback_info info,std::vector<std::string> & events,bool & fromBundle,napi_value & lastParam)435 bool ParseSetStaticSubscriberStateParam(napi_env env, const napi_callback_info info, std::vector<std::string> &events,
436     bool &fromBundle, napi_value &lastParam)
437 {
438     EVENT_LOGD("Called.");
439     napi_value thisVar = nullptr;
440     size_t argc = ARGC_TWO;
441     napi_value argv[ARGC_TWO] = { nullptr };
442     NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL), false);
443     if (argc == ARGC_ONE) {
444         return true;
445     }
446     napi_valuetype valueType = napi_undefined;
447     NAPI_CALL_BASE(env, napi_typeof(env, argv[INDEX_ONE], &valueType), false);
448     if (valueType == napi_function) {
449         lastParam = argv[INDEX_ONE];
450         return true;
451     }
452     fromBundle = true;
453     uint32_t arraySize = 0;
454     NAPI_CALL_BASE(env, napi_get_array_length(env, argv[INDEX_ONE], &arraySize), false);
455     napi_value jsValue = nullptr;
456     for (uint32_t index = 0; index < arraySize; index++) {
457         if (napi_get_element(env, argv[INDEX_ONE], index, &jsValue) != napi_ok) {
458             EVENT_LOGE("Get element failed.");
459             std::string msg = "Mandatory parameters are left unspecified.";
460             NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
461             return false;
462         }
463         std::string event;
464         if (!AppExecFwk::UnwrapStringFromJS2(env, jsValue, event)) {
465             EVENT_LOGE("Failed to convert value to string.");
466             std::string msg = "Incorrect parameter types.The type of param must be string.";
467             NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
468             return false;
469         }
470         events.push_back(event);
471     }
472     return true;
473 }
474 
OnSetStaticSubscriberState(napi_env env,const napi_callback_info info)475 napi_value NapiStaticSubscribe::OnSetStaticSubscriberState(napi_env env, const napi_callback_info info)
476 {
477     EVENT_LOGD("Called.");
478     napi_value argv[ARGC_TWO] = { nullptr };
479     napi_value result = nullptr;
480     size_t argc = ARGC_TWO;
481     napi_value thisVar = nullptr;
482     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
483     if (argc < ARGC_ONE) {
484         EVENT_LOGE("The param is invalid.");
485         std::string msg = "Mandatory parameters are left unspecified.";
486         NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
487         napi_get_undefined(env, &result);
488         return result;
489     }
490     napi_valuetype valueType = napi_undefined;
491     NAPI_CALL(env, napi_typeof(env, argv[INDEX_ZERO], &valueType));
492     if (valueType != napi_boolean) {
493         EVENT_LOGE("Parse type failed.");
494         std::string msg = "Incorrect parameter types.The type of param must be boolean.";
495         NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
496         napi_get_undefined(env, &result);
497         return result;
498     }
499     bool enable;
500     NAPI_CALL(env, napi_get_value_bool(env, argv[INDEX_ZERO], &enable));
501     napi_value lastParam = nullptr;
502     std::vector<std::string> events;
503     bool fromBundle = false;
504     if (!ParseSetStaticSubscriberStateParam(env, info, events, fromBundle, lastParam)) {
505         EVENT_LOGE("Parameter judgment error.");
506         NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID);
507         napi_get_undefined(env, &result);
508         return result;
509     }
510     auto complete = [enable, events, fromBundle](napi_env env, AbilityRuntime::NapiAsyncTask &task, int32_t status) {
511         auto ret = fromBundle ? CommonEventManager::SetStaticSubscriberState(events, enable)
512                               : CommonEventManager::SetStaticSubscriberState(enable);
513         if (ret == ERR_OK) {
514             napi_value result = nullptr;
515             napi_get_undefined(env, &result);
516             task.Resolve(env, result);
517         } else {
518             if (ret != ERR_NOTIFICATION_CES_COMMON_NOT_SYSTEM_APP && ret != ERR_NOTIFICATION_SEND_ERROR) {
519                 ret = ERR_NOTIFICATION_CESM_ERROR;
520             }
521             task.Reject(env, AbilityRuntime::CreateJsError(env, ret, "SetStaticSubscriberState failed"));
522         }
523     };
524     AbilityRuntime::NapiAsyncTask::Schedule("NapiStaticSubscribe::OnSetStaticSubscriberState", env,
525         AbilityRuntime::CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
526     return result;
527 }
528 }
529 }
530