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 #include "napi_common_event.h"
16 #include "ces_inner_error_code.h"
17 #include "event_log_wrapper.h"
18 #include "napi_common.h"
19 namespace OHOS {
20 namespace EventManagerFwkNapi {
21 using namespace OHOS::Notification;
22
23 static const std::unordered_map<int32_t, std::string> ErrorCodeToMsg {
24 {ERR_NOTIFICATION_CES_COMMON_PERMISSION_DENIED,
25 "Permission verification failed, usually the result returned by VerifyAccessToken."},
26 {ERR_NOTIFICATION_CES_COMMON_NOT_SYSTEM_APP, "The application isn't system application."},
27 {ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, "Parameter error."},
28 {ERR_NOTIFICATION_CES_COMMON_SYSTEMCAP_NOT_SUPPORT, "Capability not supported."},
29 {ERR_NOTIFICATION_CES_WANT_ACTION_IS_NULL, "The action field in the want parameter is null."},
30 {ERR_NOTIFICATION_CES_SANDBOX_NOT_SUPPORT, "A sandbox application cannot send common events."},
31 {ERR_NOTIFICATION_CES_EVENT_FREQ_TOO_HIGH, "Too many common events are sent in a short period of time."},
32 {ERR_NOTIFICATION_CES_NOT_SA_SYSTEM_APP, "A third-party application cannot send system common events."},
33 {ERR_NOTIFICATION_CES_NO_SUBSCRIBER, "The subscriber is not found."},
34 {ERR_NOTIFICATION_CES_USERID_INVALID, "Invalid userId."},
35 {ERR_NOTIFICATION_SEND_ERROR, "Failed to send the message."},
36 {ERR_NOTIFICATION_CESM_ERROR, "Failed to read the data."},
37 {ERR_NOTIFICATION_SYS_ERROR, "System error."}
38 };
39
40 static const int32_t STR_MAX_SIZE = 256;
41 static const int32_t STR_DATA_MAX_SIZE = 64 * 1024; // 64KB
42 static const int32_t PUBLISH_MAX_PARA = 2;
43 static const int32_t GETSUBSCREBEINFO_MAX_PARA = 1;
44 static const int32_t ISORDEREDCOMMONEVENT_MAX_PARA = 1;
45 static const int32_t ISSTICKYCOMMONEVENT_MAX_PARA = 1;
46 static const int32_t GET_CODE_MAX_PARA = 1;
47 static const int32_t SET_CODE_MAX_PARA = 2;
48 static const int32_t GET_DATA_MAX_PARA = 1;
49 static const int32_t SET_DATA_MAX_PARA = 2;
50 static const int32_t SET_CODE_AND_DATA_MAX_PARA = 3;
51 static const int32_t ABORT_MAX_PARA = 1;
52 static const int32_t CLEAR_ABORT_MAX_PARA = 1;
53 static const int32_t GET_ABORT_MAX_PARA = 1;
54 static const int32_t FINISH_MAX_PARA = 1;
55 static const int32_t PUBLISH_MAX_PARA_AS_USER = 3;
56 static const int32_t ARGS_DATA_TWO = 2;
57
NapiThrow(napi_env env,int32_t errCode)58 void NapiThrow(napi_env env, int32_t errCode)
59 {
60 EVENT_LOGD("enter");
61
62 napi_value code = nullptr;
63 napi_create_int32(env, errCode, &code);
64
65 auto iter = ErrorCodeToMsg.find(errCode);
66 std::string errMsg = iter != ErrorCodeToMsg.end() ? iter->second : "";
67 napi_value message = nullptr;
68 napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &message);
69
70 napi_value error = nullptr;
71 napi_create_error(env, nullptr, message, &error);
72 napi_set_named_property(env, error, "code", code);
73 napi_throw(env, error);
74 }
75
NapiThrow(napi_env env,int32_t errCode,std::string & msg)76 void NapiThrow(napi_env env, int32_t errCode, std::string &msg)
77 {
78 EVENT_LOGD("enter");
79
80 napi_value code = nullptr;
81 napi_create_int32(env, errCode, &code);
82
83 auto iter = ErrorCodeToMsg.find(errCode);
84 std::string errMsg = iter != ErrorCodeToMsg.end() ? iter->second : "";
85 napi_value message = nullptr;
86 napi_create_string_utf8(env, errMsg.append(" ").append(msg).c_str(), NAPI_AUTO_LENGTH, &message);
87
88 napi_value error = nullptr;
89 napi_create_error(env, nullptr, message, &error);
90 napi_set_named_property(env, error, "code", code);
91 napi_throw(env, error);
92 }
93
NapiGetNull(napi_env env)94 napi_value NapiGetNull(napi_env env)
95 {
96 napi_value result = nullptr;
97 napi_get_null(env, &result);
98
99 return result;
100 }
101
GetCallbackErrorValue(napi_env env,int32_t errorCode)102 napi_value GetCallbackErrorValue(napi_env env, int32_t errorCode)
103 {
104 napi_value result = NapiGetNull(env);
105 napi_value eCode = NapiGetNull(env);
106 if (errorCode == ERR_OK) {
107 return result;
108 }
109 NAPI_CALL(env, napi_create_int32(env, errorCode, &eCode));
110 NAPI_CALL(env, napi_create_object(env, &result));
111 NAPI_CALL(env, napi_set_named_property(env, result, "code", eCode));
112
113 auto iter = ErrorCodeToMsg.find(errorCode);
114 std::string errMsg = iter != ErrorCodeToMsg.end() ? iter->second : "";
115 napi_value message = nullptr;
116 napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &message);
117 napi_set_named_property(env, result, "message", message);
118 return result;
119 }
120
ParseParametersByCreateSubscriber(const napi_env & env,const napi_value (& argv)[CREATE_MAX_PARA],const size_t & argc,napi_ref & callback)121 napi_value ParseParametersByCreateSubscriber(
122 const napi_env &env, const napi_value (&argv)[CREATE_MAX_PARA], const size_t &argc, napi_ref &callback)
123 {
124 napi_valuetype valuetype;
125
126 // argv[0]:CommonEventSubscribeInfo
127 NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype));
128 if (valuetype != napi_object) {
129 EVENT_LOGE("Parameter type error. object expected.");
130 std::string msg = "Incorrect parameter types.The type of param must be object.";
131 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
132 return nullptr;
133 }
134
135 // argv[1]:callback
136 if (argc >= CREATE_MAX_PARA) {
137 NAPI_CALL(env, napi_typeof(env, argv[1], &valuetype));
138 if (valuetype != napi_function) {
139 EVENT_LOGE("Callback is not function. Execute promise.");
140 return NapiGetNull(env);
141 }
142 napi_create_reference(env, argv[1], 1, &callback);
143 }
144
145 return NapiGetNull(env);
146 }
147
PaddingAsyncCallbackInfoCreateSubscriber(const napi_env & env,AsyncCallbackInfoCreate * & asyncCallbackInfo,const napi_ref & callback,napi_value & promise)148 void PaddingAsyncCallbackInfoCreateSubscriber(const napi_env &env,
149 AsyncCallbackInfoCreate *&asyncCallbackInfo, const napi_ref &callback, napi_value &promise)
150 {
151 EVENT_LOGD("PaddingAsyncCallbackInfoCreateSubscriber excute");
152
153 if (callback) {
154 asyncCallbackInfo->info.callback = callback;
155 asyncCallbackInfo->info.isCallback = true;
156 } else {
157 napi_deferred deferred = nullptr;
158 napi_create_promise(env, &deferred, &promise);
159 asyncCallbackInfo->info.deferred = deferred;
160 asyncCallbackInfo->info.isCallback = false;
161 }
162 }
163
PaddingCallbackPromiseInfo(const napi_env & env,const napi_ref & callback,CallbackPromiseInfo & callbackInfo,napi_value & promise)164 void PaddingCallbackPromiseInfo(const napi_env &env, const napi_ref &callback,
165 CallbackPromiseInfo &callbackInfo, napi_value &promise)
166 {
167 EVENT_LOGD("PaddingCallbackPromiseInfo start");
168
169 if (callback) {
170 callbackInfo.callback = callback;
171 callbackInfo.isCallback = true;
172 } else {
173 napi_deferred deferred = nullptr;
174 napi_create_promise(env, &deferred, &promise);
175 callbackInfo.deferred = deferred;
176 callbackInfo.isCallback = false;
177 }
178 }
179
ParseParametersByGetSubscribeInfo(const napi_env & env,const size_t & argc,const napi_value (& argv)[1],napi_ref & callback)180 napi_value ParseParametersByGetSubscribeInfo(
181 const napi_env &env, const size_t &argc, const napi_value (&argv)[1], napi_ref &callback)
182 {
183 napi_valuetype valuetype;
184
185 // argv[0]:callback
186 if (argc >= GETSUBSCREBEINFO_MAX_PARA) {
187 NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype));
188 if (valuetype != napi_function) {
189 EVENT_LOGE("Parameter type error. Function expected.");
190 std::string msg = "Incorrect parameter types.The type of param must be function.";
191 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
192 return nullptr;
193 }
194
195 napi_create_reference(env, argv[0], 1, &callback);
196 }
197
198 return NapiGetNull(env);
199 }
200
PaddingAsyncCallbackInfoGetSubscribeInfo(const napi_env & env,const size_t & argc,AsyncCallbackInfoSubscribeInfo * & asyncCallbackInfo,const napi_ref & callback,napi_value & promise)201 void PaddingAsyncCallbackInfoGetSubscribeInfo(const napi_env &env, const size_t &argc,
202 AsyncCallbackInfoSubscribeInfo *&asyncCallbackInfo, const napi_ref &callback, napi_value &promise)
203 {
204 EVENT_LOGD("PaddingAsyncCallbackInfoGetSubscribeInfo excute");
205
206 if (argc >= GETSUBSCREBEINFO_MAX_PARA) {
207 asyncCallbackInfo->info.callback = callback;
208 asyncCallbackInfo->info.isCallback = true;
209 } else {
210 napi_deferred deferred = nullptr;
211 NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise));
212 asyncCallbackInfo->info.deferred = deferred;
213 asyncCallbackInfo->info.isCallback = false;
214 }
215 }
216
ParseParametersByIsOrderedCommonEvent(const napi_env & env,const napi_value (& argv)[1],size_t argc,napi_ref & callback)217 napi_value ParseParametersByIsOrderedCommonEvent(
218 const napi_env &env, const napi_value (&argv)[1], size_t argc, napi_ref &callback)
219 {
220 napi_valuetype valuetype;
221
222 // argv[0]:callback
223 if (argc >= ISORDEREDCOMMONEVENT_MAX_PARA) {
224 NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype));
225 if (valuetype != napi_function) {
226 EVENT_LOGE("Wrong Parameter type. Function expected.");
227 std::string msg = "Incorrect parameter types.The type of param must be function.";
228 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
229 return nullptr;
230 }
231 napi_create_reference(env, argv[0], 1, &callback);
232 }
233
234 return NapiGetNull(env);
235 }
236
PaddingAsyncCallbackInfoIsOrderedCommonEvent(const napi_env & env,const size_t & argc,AsyncCallbackInfoOrderedCommonEvent * & asyncCallbackInfo,const napi_ref & callback,napi_value & promise)237 void PaddingAsyncCallbackInfoIsOrderedCommonEvent(const napi_env &env, const size_t &argc,
238 AsyncCallbackInfoOrderedCommonEvent *&asyncCallbackInfo, const napi_ref &callback, napi_value &promise)
239 {
240 EVENT_LOGD("PaddingAsyncCallbackInfoIsOrderedCommonEvent excute");
241
242 if (argc >= ISORDEREDCOMMONEVENT_MAX_PARA) {
243 asyncCallbackInfo->info.callback = callback;
244 asyncCallbackInfo->info.isCallback = true;
245 } else {
246 napi_deferred deferred = nullptr;
247 NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise));
248 asyncCallbackInfo->info.deferred = deferred;
249 asyncCallbackInfo->info.isCallback = false;
250 }
251 }
252
ParseParametersByIsStickyCommonEvent(const napi_env & env,const napi_value (& argv)[1],size_t argc,napi_ref & callback)253 napi_value ParseParametersByIsStickyCommonEvent(
254 const napi_env &env, const napi_value (&argv)[1], size_t argc, napi_ref &callback)
255 {
256 napi_valuetype valuetype;
257
258 // argv[0]:callback
259 if (argc >= ISSTICKYCOMMONEVENT_MAX_PARA) {
260 NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype));
261 if (valuetype != napi_function) {
262 EVENT_LOGE("Parameter type error. Function expected.");
263 std::string msg = "Incorrect parameter types.The type of param must be function.";
264 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
265 return nullptr;
266 }
267 napi_create_reference(env, argv[0], 1, &callback);
268 }
269
270 return NapiGetNull(env);
271 }
272
PaddingAsyncCallbackInfoIsStickyCommonEvent(const napi_env & env,const size_t & argc,AsyncCallbackInfoStickyCommonEvent * & asyncCallbackInfo,const napi_ref & callback,napi_value & promise)273 void PaddingAsyncCallbackInfoIsStickyCommonEvent(const napi_env &env, const size_t &argc,
274 AsyncCallbackInfoStickyCommonEvent *&asyncCallbackInfo, const napi_ref &callback, napi_value &promise)
275 {
276 EVENT_LOGD("PaddingAsyncCallbackInfoIsStickyCommonEvent excute");
277
278 if (argc >= ISSTICKYCOMMONEVENT_MAX_PARA) {
279 asyncCallbackInfo->info.callback = callback;
280 asyncCallbackInfo->info.isCallback = true;
281 } else {
282 napi_deferred deferred = nullptr;
283 NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise));
284 asyncCallbackInfo->info.deferred = deferred;
285 asyncCallbackInfo->info.isCallback = false;
286 }
287
288 EVENT_LOGD("PaddingAsyncCallbackInfoIsStickyCommonEvent complete");
289 }
290
ParseParametersByGetCode(const napi_env & env,const napi_value (& argv)[1],size_t argc,napi_ref & callback)291 napi_value ParseParametersByGetCode(const napi_env &env, const napi_value (&argv)[1], size_t argc, napi_ref &callback)
292 {
293 napi_valuetype valuetype;
294
295 // argv[0]:callback
296 if (argc >= GET_CODE_MAX_PARA) {
297 NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype));
298 if (valuetype != napi_function) {
299 EVENT_LOGE("Parameter type error. Function expected.");
300 std::string msg = "Incorrect parameter types.The type of param must be function.";
301 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
302 return nullptr;
303 }
304 napi_create_reference(env, argv[0], 1, &callback);
305 }
306
307 return NapiGetNull(env);
308 }
309
PaddingAsyncCallbackInfoGetCode(const napi_env & env,const size_t & argc,AsyncCallbackInfoGetCode * & asyncCallbackInfo,const napi_ref & callback,napi_value & promise)310 void PaddingAsyncCallbackInfoGetCode(const napi_env &env, const size_t &argc,
311 AsyncCallbackInfoGetCode *&asyncCallbackInfo, const napi_ref &callback, napi_value &promise)
312 {
313 EVENT_LOGD("PaddingAsyncCallbackInfoGetCode excute");
314
315 if (argc >= GET_CODE_MAX_PARA) {
316 asyncCallbackInfo->info.callback = callback;
317 asyncCallbackInfo->info.isCallback = true;
318 } else {
319 napi_deferred deferred = nullptr;
320 NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise));
321 asyncCallbackInfo->info.deferred = deferred;
322 asyncCallbackInfo->info.isCallback = false;
323 }
324
325 EVENT_LOGD("PaddingAsyncCallbackInfoGetCode complete");
326 }
327
ParseParametersBySetCode(const napi_env & env,const napi_value (& argv)[SET_CODE_MAX_PARA],size_t argc,int32_t & code,napi_ref & callback)328 napi_value ParseParametersBySetCode(
329 const napi_env &env, const napi_value (&argv)[SET_CODE_MAX_PARA], size_t argc, int32_t &code, napi_ref &callback)
330 {
331 napi_valuetype valuetype;
332
333 // argv[0]:code
334 NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype));
335 if (valuetype != napi_number) {
336 EVENT_LOGE("Parameter type error. Number expected.");
337 std::string msg = "Incorrect parameter types.The type of param must be number.";
338 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
339 return nullptr;
340 }
341 NAPI_CALL(env, napi_get_value_int32(env, argv[0], &code));
342
343 // argv[1]:callback
344 if (argc >= SET_CODE_MAX_PARA) {
345 NAPI_CALL(env, napi_typeof(env, argv[1], &valuetype));
346 if (valuetype != napi_function) {
347 EVENT_LOGE("Parameter type is error. Function expected.");
348 std::string msg = "Incorrect parameter types.The type of param must be function.";
349 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
350 return nullptr;
351 }
352 napi_create_reference(env, argv[1], 1, &callback);
353 }
354 return NapiGetNull(env);
355 }
356
PaddingAsyncCallbackInfoSetCode(const napi_env & env,const size_t & argc,AsyncCallbackInfoSetCode * & asyncCallbackInfo,const napi_ref & callback,napi_value & promise)357 void PaddingAsyncCallbackInfoSetCode(const napi_env &env, const size_t &argc,
358 AsyncCallbackInfoSetCode *&asyncCallbackInfo, const napi_ref &callback, napi_value &promise)
359 {
360 EVENT_LOGD("PaddingAsyncCallbackInfoSetCode excute");
361
362 if (argc >= SET_CODE_MAX_PARA) {
363 asyncCallbackInfo->info.callback = callback;
364 asyncCallbackInfo->info.isCallback = true;
365 } else {
366 napi_deferred deferred = nullptr;
367 NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise));
368 asyncCallbackInfo->info.deferred = deferred;
369 asyncCallbackInfo->info.isCallback = false;
370 }
371
372 EVENT_LOGD("PaddingAsyncCallbackInfoSetCode complete");
373 }
374
PaddingAsyncCallbackInfoSetData(const napi_env & env,const size_t & argc,AsyncCallbackInfoSetData * & asyncCallbackInfo,const napi_ref & callback,napi_value & promise)375 void PaddingAsyncCallbackInfoSetData(const napi_env &env, const size_t &argc,
376 AsyncCallbackInfoSetData *&asyncCallbackInfo, const napi_ref &callback, napi_value &promise)
377 {
378 EVENT_LOGD("PaddingAsyncCallbackInfoSetData excute");
379
380 if (argc >= SET_DATA_MAX_PARA) {
381 asyncCallbackInfo->info.callback = callback;
382 asyncCallbackInfo->info.isCallback = true;
383 } else {
384 napi_deferred deferred = nullptr;
385 NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise));
386 asyncCallbackInfo->info.deferred = deferred;
387 asyncCallbackInfo->info.isCallback = false;
388 }
389
390 EVENT_LOGD("PaddingAsyncCallbackInfoSetData complete");
391 }
392
PaddingAsyncCallbackInfoSetCodeAndData(const napi_env & env,const size_t & argc,AsyncCallbackInfoSetCodeAndData * & asyncCallbackInfo,const napi_ref & callback,napi_value & promise)393 void PaddingAsyncCallbackInfoSetCodeAndData(const napi_env &env, const size_t &argc,
394 AsyncCallbackInfoSetCodeAndData *&asyncCallbackInfo, const napi_ref &callback, napi_value &promise)
395 {
396 EVENT_LOGD("PaddingAsyncCallbackInfoSetCodeAndData excute");
397
398 if (argc >= SET_CODE_AND_DATA_MAX_PARA) {
399 asyncCallbackInfo->info.callback = callback;
400 asyncCallbackInfo->info.isCallback = true;
401 } else {
402 napi_deferred deferred = nullptr;
403 NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise));
404 asyncCallbackInfo->info.deferred = deferred;
405 asyncCallbackInfo->info.isCallback = false;
406 }
407
408 EVENT_LOGD("PaddingAsyncCallbackInfoSetCodeAndData complete");
409 }
410
PaddingNapiCreateAsyncWorkCallbackInfo(AsyncCallbackInfoSubscribeInfo * & asyncCallbackInfo)411 void PaddingNapiCreateAsyncWorkCallbackInfo(AsyncCallbackInfoSubscribeInfo *&asyncCallbackInfo)
412 {
413 EVENT_LOGD("PaddingNapiCreateAsyncWorkCallbackInfo excute");
414
415 asyncCallbackInfo->events = asyncCallbackInfo->subscriber->GetSubscribeInfo().GetMatchingSkills().GetEvents();
416 asyncCallbackInfo->permission = asyncCallbackInfo->subscriber->GetSubscribeInfo().GetPermission();
417 asyncCallbackInfo->deviceId = asyncCallbackInfo->subscriber->GetSubscribeInfo().GetDeviceId();
418 asyncCallbackInfo->userId = asyncCallbackInfo->subscriber->GetSubscribeInfo().GetUserId();
419 asyncCallbackInfo->priority = asyncCallbackInfo->subscriber->GetSubscribeInfo().GetPriority();
420 asyncCallbackInfo->publisherBundleName = asyncCallbackInfo->subscriber->GetSubscribeInfo().GetPublisherBundleName();
421 }
422
ParseParametersByGetData(const napi_env & env,const napi_value (& argv)[1],size_t argc,napi_ref & callback)423 napi_value ParseParametersByGetData(const napi_env &env, const napi_value (&argv)[1], size_t argc, napi_ref &callback)
424 {
425 napi_valuetype valuetype;
426
427 // argv[0]:callback
428 if (argc >= GET_DATA_MAX_PARA) {
429 NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype));
430 if (valuetype != napi_function) {
431 EVENT_LOGE("Parameter type error. Function expected.");
432 std::string msg = "Incorrect parameter types.The type of param must be function.";
433 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
434 return nullptr;
435 }
436 napi_create_reference(env, argv[0], 1, &callback);
437 }
438
439 return NapiGetNull(env);
440 }
441
PaddingAsyncCallbackInfoGetData(const napi_env & env,const size_t & argc,AsyncCallbackInfoGetData * & asyncCallbackInfo,const napi_ref & callback,napi_value & promise)442 void PaddingAsyncCallbackInfoGetData(const napi_env &env, const size_t &argc,
443 AsyncCallbackInfoGetData *&asyncCallbackInfo, const napi_ref &callback, napi_value &promise)
444 {
445 EVENT_LOGD("PaddingAsyncCallbackInfoGetData excute");
446
447 if (argc >= GET_DATA_MAX_PARA) {
448 asyncCallbackInfo->info.callback = callback;
449 asyncCallbackInfo->info.isCallback = true;
450 } else {
451 napi_deferred deferred = nullptr;
452 NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise));
453 asyncCallbackInfo->info.deferred = deferred;
454 asyncCallbackInfo->info.isCallback = false;
455 }
456
457 EVENT_LOGD("PaddingAsyncCallbackInfoGetData complete");
458 }
459
ParseParametersBySetData(const napi_env & env,const napi_value (& argv)[SET_DATA_MAX_PARA],size_t argc,std::string & data,napi_ref & callback)460 napi_value ParseParametersBySetData(
461 const napi_env &env, const napi_value (&argv)[SET_DATA_MAX_PARA], size_t argc,
462 std::string &data, napi_ref &callback)
463 {
464 napi_valuetype valuetype;
465 size_t strLen = 0;
466 char str[STR_DATA_MAX_SIZE] = {0};
467 // argv[0]:data
468 NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype));
469 if (valuetype != napi_string) {
470 EVENT_LOGE("Wrong argument type. String expected.");
471 std::string msg = "Incorrect parameter types.The type of param must be string.";
472 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
473 return nullptr;
474 }
475 NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], str, STR_DATA_MAX_SIZE, &strLen));
476
477 if (strLen > STR_DATA_MAX_SIZE - 1) {
478 EVENT_LOGE("data over size");
479 std::string msg = "Parameter verification failed. cannot exceed ";
480 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID,
481 msg.append(std::to_string(STR_DATA_MAX_SIZE - 1)).append(" characters"));
482 return nullptr;
483 }
484
485 data = str;
486
487 // argv[1]:callback
488 if (argc >= SET_CODE_MAX_PARA) {
489 NAPI_CALL(env, napi_typeof(env, argv[1], &valuetype));
490 if (valuetype != napi_function) {
491 EVENT_LOGE("Parameter type error. Function expected.");
492 std::string msg = "Incorrect parameter types.The type of param must be function.";
493 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
494 return nullptr;
495 }
496 napi_create_reference(env, argv[1], 1, &callback);
497 }
498 return NapiGetNull(env);
499 }
500
ParseParametersBySetCodeAndData(const napi_env & env,const napi_value (& argv)[SET_CODE_AND_DATA_MAX_PARA],size_t argc,int32_t & code,std::string & data,napi_ref & callback)501 napi_value ParseParametersBySetCodeAndData(
502 const napi_env &env, const napi_value (&argv)[SET_CODE_AND_DATA_MAX_PARA],
503 size_t argc, int32_t &code, std::string &data, napi_ref &callback)
504 {
505 napi_valuetype valuetype;
506 size_t strLen = 0;
507 char str[STR_DATA_MAX_SIZE] = {0};
508
509 // argv[0]:code
510 NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype));
511 if (valuetype != napi_number) {
512 EVENT_LOGE("Parameter type error. Number expected.");
513 std::string msg = "Incorrect parameter types.The type of param must be function.";
514 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
515 return nullptr;
516 }
517 NAPI_CALL(env, napi_get_value_int32(env, argv[0], &code));
518
519 // argv[1]:data
520 NAPI_CALL(env, napi_typeof(env, argv[1], &valuetype));
521 if (valuetype != napi_string) {
522 EVENT_LOGE("Parameter type error. String expected.");
523 std::string msg = "Incorrect parameter types.The type of param must be string.";
524 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
525 return nullptr;
526 }
527 NAPI_CALL(env, napi_get_value_string_utf8(env, argv[1], str, STR_DATA_MAX_SIZE, &strLen));
528
529 if (strLen > STR_DATA_MAX_SIZE - 1) {
530 EVENT_LOGE("data exceed size");
531 std::string msg = "Parameter verification failed. cannot exceed ";
532 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID,
533 msg.append(std::to_string(STR_DATA_MAX_SIZE - 1)).append(" characters"));
534 return nullptr;
535 }
536
537 data = str;
538
539 // argv[2]:callback
540 if (argc >= SET_CODE_AND_DATA_MAX_PARA) {
541 NAPI_CALL(env, napi_typeof(env, argv[SET_CODE_AND_DATA_MAX_PARA - 1], &valuetype));
542 if (valuetype != napi_function) {
543 EVENT_LOGE("Parameter type error. Function expected.");
544 std::string msg = "Incorrect parameter types.The type of param must be function.";
545 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
546 return nullptr;
547 }
548 napi_create_reference(env, argv[SET_CODE_AND_DATA_MAX_PARA - 1], 1, &callback);
549 }
550 return NapiGetNull(env);
551 }
552
ParseParametersByClearAbort(const napi_env & env,const napi_value (& argv)[1],size_t argc,napi_ref & callback)553 napi_value ParseParametersByClearAbort(
554 const napi_env &env, const napi_value (&argv)[1], size_t argc, napi_ref &callback)
555 {
556 napi_valuetype valuetype;
557
558 // argv[0]:callback
559 if (argc >= CLEAR_ABORT_MAX_PARA) {
560 NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype));
561 if (valuetype != napi_function) {
562 EVENT_LOGE("Wrong argument type. Function expected.");
563 std::string msg = "Incorrect parameter types.The type of param must be function.";
564 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
565 return nullptr;
566 }
567 napi_create_reference(env, argv[0], 1, &callback);
568 }
569
570 return NapiGetNull(env);
571 }
572
PaddingAsyncCallbackInfoClearAbort(const napi_env & env,const size_t & argc,AsyncCallbackInfoClearAbort * & asyncCallbackInfo,const napi_ref & callback,napi_value & promise)573 void PaddingAsyncCallbackInfoClearAbort(const napi_env &env, const size_t &argc,
574 AsyncCallbackInfoClearAbort *&asyncCallbackInfo, const napi_ref &callback, napi_value &promise)
575 {
576 EVENT_LOGD("PaddingAsyncCallbackInfoClearAbort start");
577
578 if (argc >= CLEAR_ABORT_MAX_PARA) {
579 asyncCallbackInfo->info.callback = callback;
580 asyncCallbackInfo->info.isCallback = true;
581 } else {
582 napi_deferred deferred = nullptr;
583 NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise));
584 asyncCallbackInfo->info.deferred = deferred;
585 asyncCallbackInfo->info.isCallback = false;
586 }
587
588 EVENT_LOGD("PaddingAsyncCallbackInfoClearAbort end");
589 }
590
ParseParametersByAbort(const napi_env & env,const napi_value (& argv)[1],size_t argc,napi_ref & callback)591 napi_value ParseParametersByAbort(const napi_env &env, const napi_value (&argv)[1], size_t argc, napi_ref &callback)
592 {
593 napi_valuetype valuetype;
594
595 // argv[0]:callback
596 if (argc >= ABORT_MAX_PARA) {
597 NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype));
598 if (valuetype != napi_function) {
599 EVENT_LOGE("Function expected. Wrong argument type.");
600 std::string msg = "Incorrect parameter types.The type of param must be function.";
601 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
602 return nullptr;
603 }
604 napi_create_reference(env, argv[0], 1, &callback);
605 }
606
607 return NapiGetNull(env);
608 }
609
PaddingAsyncCallbackInfoAbort(const napi_env & env,const size_t & argc,AsyncCallbackInfoAbort * & asyncCallbackInfo,const napi_ref & callback,napi_value & promise)610 void PaddingAsyncCallbackInfoAbort(const napi_env &env, const size_t &argc, AsyncCallbackInfoAbort *&asyncCallbackInfo,
611 const napi_ref &callback, napi_value &promise)
612 {
613 EVENT_LOGD("PaddingAsyncCallbackInfoAbort excute");
614
615 if (argc >= ABORT_MAX_PARA) {
616 asyncCallbackInfo->info.callback = callback;
617 asyncCallbackInfo->info.isCallback = true;
618 } else {
619 napi_deferred deferred = nullptr;
620 NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise));
621 asyncCallbackInfo->info.deferred = deferred;
622 asyncCallbackInfo->info.isCallback = false;
623 }
624
625 EVENT_LOGD("PaddingAsyncCallbackInfoAbort complete");
626 }
627
ParseParametersByGetAbort(const napi_env & env,const napi_value (& argv)[1],size_t argc,napi_ref & callback)628 napi_value ParseParametersByGetAbort(const napi_env &env, const napi_value (&argv)[1], size_t argc, napi_ref &callback)
629 {
630 napi_valuetype valuetype;
631
632 // argv[0]:callback
633 if (argc >= GET_ABORT_MAX_PARA) {
634 NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype));
635 if (valuetype != napi_function) {
636 EVENT_LOGE("Parameter type error. Function expected.");
637 std::string msg = "Incorrect parameter types.The type of param must be function.";
638 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
639 return nullptr;
640 }
641 napi_create_reference(env, argv[0], 1, &callback);
642 }
643
644 return NapiGetNull(env);
645 }
646
PaddingAsyncCallbackInfoGetAbort(const napi_env & env,const size_t & argc,AsyncCallbackInfoGetAbort * & asyncCallbackInfo,const napi_ref & callback,napi_value & promise)647 void PaddingAsyncCallbackInfoGetAbort(const napi_env &env, const size_t &argc,
648 AsyncCallbackInfoGetAbort *&asyncCallbackInfo, const napi_ref &callback, napi_value &promise)
649 {
650 EVENT_LOGD("PaddingAsyncCallbackInfoGetAbort excute");
651
652 if (argc >= GET_ABORT_MAX_PARA) {
653 asyncCallbackInfo->info.callback = callback;
654 asyncCallbackInfo->info.isCallback = true;
655 } else {
656 napi_deferred deferred = nullptr;
657 NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise));
658 asyncCallbackInfo->info.deferred = deferred;
659 asyncCallbackInfo->info.isCallback = false;
660 }
661
662 EVENT_LOGD("PaddingAsyncCallbackInfoGetAbort complete");
663 }
664
ParseParametersByFinish(const napi_env & env,const napi_value (& argv)[1],size_t argc,napi_ref & callback)665 napi_value ParseParametersByFinish(const napi_env &env, const napi_value (&argv)[1], size_t argc, napi_ref &callback)
666 {
667 napi_valuetype valuetype;
668
669 // argv[0]:callback
670 if (argc >= FINISH_MAX_PARA) {
671 NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype));
672 if (valuetype != napi_function) {
673 EVENT_LOGE("Wrong parameter type. Function expected.");
674 std::string msg = "Incorrect parameter types.The type of param must be function.";
675 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
676 return nullptr;
677 }
678 napi_create_reference(env, argv[0], 1, &callback);
679 }
680
681 return NapiGetNull(env);
682 }
683
PaddingAsyncCallbackInfoFinish(const napi_env & env,const size_t & argc,AsyncCallbackInfoFinish * & asyncCallbackInfo,const napi_ref & callback,napi_value & promise)684 void PaddingAsyncCallbackInfoFinish(const napi_env &env, const size_t &argc,
685 AsyncCallbackInfoFinish *&asyncCallbackInfo, const napi_ref &callback, napi_value &promise)
686 {
687 EVENT_LOGD("PaddingAsyncCallbackInfoFinish excute");
688
689 if (argc >= FINISH_MAX_PARA) {
690 asyncCallbackInfo->info.callback = callback;
691 asyncCallbackInfo->info.isCallback = true;
692 } else {
693 napi_deferred deferred = nullptr;
694 NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise));
695 asyncCallbackInfo->info.deferred = deferred;
696 asyncCallbackInfo->info.isCallback = false;
697 }
698
699 EVENT_LOGD("PaddingAsyncCallbackInfoFinish complete");
700 }
701
ParseParametersBySubscribe(const napi_env & env,const napi_value (& argv)[SUBSCRIBE_MAX_PARA],std::shared_ptr<SubscriberInstance> & subscriber,napi_ref & callback)702 napi_value ParseParametersBySubscribe(const napi_env &env, const napi_value (&argv)[SUBSCRIBE_MAX_PARA],
703 std::shared_ptr<SubscriberInstance> &subscriber, napi_ref &callback)
704 {
705 EVENT_LOGD("ParseParametersBySubscribe excute");
706
707 napi_valuetype valuetype;
708 // argv[0]:subscriber
709 NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype));
710 if (valuetype != napi_object) {
711 EVENT_LOGE("Subscribe expected. Wrong argument type for arg0.");
712 std::string msg = "Incorrect parameter types.The type of param must be object.";
713 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
714 return nullptr;
715 }
716 subscriber = GetSubscriber(env, argv[0]);
717 if (subscriber == nullptr) {
718 EVENT_LOGE("subscriber is null");
719 std::string msg = "Mandatory parameters are left unspecified.";
720 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
721 return nullptr;
722 }
723
724 // argv[1]:callback
725 NAPI_CALL(env, napi_typeof(env, argv[1], &valuetype));
726 if (valuetype != napi_function) {
727 EVENT_LOGE("Parameter type error. Function expected.");
728 std::string msg = "Incorrect parameter types.The type of param must be function.";
729 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
730 return nullptr;
731 }
732 napi_create_reference(env, argv[1], 1, &callback);
733
734 return NapiGetNull(env);
735 }
736
GetBundlenameByPublish(const napi_env & env,const napi_value & value,std::string & bundleName)737 napi_value GetBundlenameByPublish(const napi_env &env, const napi_value &value, std::string &bundleName)
738 {
739 EVENT_LOGD("GetBundlenameByPublish excute");
740
741 napi_valuetype valuetype;
742 napi_value result = nullptr;
743 char str[STR_MAX_SIZE] = {0};
744 bool hasProperty = false;
745 size_t strLen = 0;
746
747 NAPI_CALL(env, napi_has_named_property(env, value, "bundleName", &hasProperty));
748 if (hasProperty) {
749 napi_get_named_property(env, value, "bundleName", &result);
750 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
751 if (valuetype != napi_string) {
752 EVENT_LOGE("Parameter type error. String expected.");
753 std::string msg = "Incorrect parameter types.The type of param must be string.";
754 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
755 return nullptr;
756 }
757 NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
758 bundleName = str;
759 }
760
761 return NapiGetNull(env);
762 }
763
GetDataByPublish(const napi_env & env,const napi_value & value,std::string & data)764 napi_value GetDataByPublish(const napi_env &env, const napi_value &value, std::string &data)
765 {
766 EVENT_LOGD("GetDataByPublish excute");
767
768 napi_valuetype valuetype;
769 napi_value result = nullptr;
770 char str[STR_DATA_MAX_SIZE] = {0};
771 bool hasProperty = false;
772 size_t strLen = 0;
773
774 NAPI_CALL(env, napi_has_named_property(env, value, "data", &hasProperty));
775 if (hasProperty) {
776 napi_get_named_property(env, value, "data", &result);
777 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
778 if (valuetype != napi_string) {
779 EVENT_LOGE("Parameter type error. String expected.");
780 std::string msg = "Incorrect parameter types.The type of param must be string.";
781 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
782 return nullptr;
783 }
784 NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_DATA_MAX_SIZE, &strLen));
785
786 if (strLen > STR_DATA_MAX_SIZE - 1) {
787 EVENT_LOGE("data exceed size");
788 std::string msg = "Parameter verification failed. cannot exceed ";
789 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID,
790 msg.append(std::to_string(STR_DATA_MAX_SIZE - 1)).append(" characters"));
791 return nullptr;
792 }
793
794 data = str;
795 }
796
797 return NapiGetNull(env);
798 }
799
GetCodeByPublish(const napi_env & env,const napi_value & value,int32_t & code)800 napi_value GetCodeByPublish(const napi_env &env, const napi_value &value, int32_t &code)
801 {
802 EVENT_LOGD("GetCodeByPublish excute");
803
804 napi_valuetype valuetype;
805 napi_value result = nullptr;
806 bool hasProperty = false;
807
808 NAPI_CALL(env, napi_has_named_property(env, value, "code", &hasProperty));
809 if (hasProperty) {
810 napi_get_named_property(env, value, "code", &result);
811 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
812 if (valuetype != napi_number) {
813 EVENT_LOGE("Parameter type error. Number expected.");
814 std::string msg = "Incorrect parameter types.The type of param must be number.";
815 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
816 return nullptr;
817 }
818 napi_get_value_int32(env, result, &code);
819 }
820
821 return NapiGetNull(env);
822 }
823
GetSubscriberPermissionsByPublish(const napi_env & env,const napi_value & value,std::vector<std::string> & subscriberPermissions)824 napi_value GetSubscriberPermissionsByPublish(
825 const napi_env &env, const napi_value &value, std::vector<std::string> &subscriberPermissions)
826 {
827 EVENT_LOGD("GetSubscriberPermissionsByPublish excute");
828
829 napi_valuetype valuetype;
830 napi_value result = nullptr;
831 bool isArray = false;
832 size_t strLen = 0;
833 bool hasProperty = false;
834 char str[STR_MAX_SIZE] = {0};
835
836 NAPI_CALL(env, napi_has_named_property(env, value, "subscriberPermissions", &hasProperty));
837 if (hasProperty) {
838 napi_get_named_property(env, value, "subscriberPermissions", &result);
839 napi_is_array(env, result, &isArray);
840 if (isArray) {
841 uint32_t length = 0;
842 napi_get_array_length(env, result, &length);
843 if (length > 0) {
844 for (uint32_t i = 0; i < length; ++i) {
845 napi_value nSubscriberPermission = nullptr;
846 napi_get_element(env, result, i, &nSubscriberPermission);
847 NAPI_CALL(env, napi_typeof(env, nSubscriberPermission, &valuetype));
848 if (valuetype != napi_string) {
849 EVENT_LOGE("Parameter type error. String expected.");
850 std::string msg = "Incorrect parameter types.The type of param must be string.";
851 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
852 return nullptr;
853 }
854 if (memset_s(str, STR_MAX_SIZE, 0, STR_MAX_SIZE) != 0) {
855 EVENT_LOGE("memset_s failed");
856 std::string msg = "Parameter verification failed.";
857 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
858 return nullptr;
859 }
860 NAPI_CALL(
861 env, napi_get_value_string_utf8(env, nSubscriberPermission, str, STR_MAX_SIZE - 1, &strLen));
862 subscriberPermissions.emplace_back(str);
863 }
864 }
865 }
866 }
867
868 return NapiGetNull(env);
869 }
870
GetIsOrderedByPublish(const napi_env & env,const napi_value & value,bool & isOrdered)871 napi_value GetIsOrderedByPublish(const napi_env &env, const napi_value &value, bool &isOrdered)
872 {
873 EVENT_LOGD("GetIsOrderedByPublish excute");
874
875 napi_valuetype valuetype;
876 napi_value result = nullptr;
877 bool hasProperty = false;
878
879 NAPI_CALL(env, napi_has_named_property(env, value, "isOrdered", &hasProperty));
880 if (hasProperty) {
881 napi_get_named_property(env, value, "isOrdered", &result);
882 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
883 if (valuetype != napi_boolean) {
884 EVENT_LOGE("Parameter type error. Boolean expected.");
885 std::string msg = "Incorrect parameter types.The type of param must be boolean.";
886 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
887 return nullptr;
888 }
889 napi_get_value_bool(env, result, &isOrdered);
890 }
891
892 return NapiGetNull(env);
893 }
894
GetIsStickyByPublish(const napi_env & env,const napi_value & value,bool & isSticky)895 napi_value GetIsStickyByPublish(const napi_env &env, const napi_value &value, bool &isSticky)
896 {
897 EVENT_LOGD("GetIsStickyByPublish excute");
898
899 napi_valuetype valuetype;
900 napi_value result = nullptr;
901 bool hasProperty = false;
902
903 NAPI_CALL(env, napi_has_named_property(env, value, "isSticky", &hasProperty));
904 if (hasProperty) {
905 napi_get_named_property(env, value, "isSticky", &result);
906 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
907 if (valuetype != napi_boolean) {
908 EVENT_LOGE("Parameter type error. Boolean expected.");
909 std::string msg = "Incorrect parameter types.The type of param must be boolean.";
910 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
911 return nullptr;
912 }
913 napi_get_value_bool(env, result, &isSticky);
914 }
915
916 return NapiGetNull(env);
917 }
918
GetParametersByPublish(const napi_env & env,const napi_value & value,AAFwk::WantParams & wantParams)919 napi_value GetParametersByPublish(const napi_env &env, const napi_value &value, AAFwk::WantParams &wantParams)
920 {
921 EVENT_LOGD("GetParametersByPublish excute");
922
923 napi_valuetype valuetype = napi_undefined;
924 napi_value result = nullptr;
925 bool hasProperty = false;
926
927 NAPI_CALL(env, napi_has_named_property(env, value, "parameters", &hasProperty));
928 if (hasProperty) {
929 napi_get_named_property(env, value, "parameters", &result);
930 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
931 if (valuetype != napi_object) {
932 EVENT_LOGE("Parameter type error. Object expected.");
933 std::string msg = "Incorrect parameter types.The type of param must be object.";
934 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
935 return nullptr;
936 }
937 if (!OHOS::AppExecFwk::UnwrapWantParams(env, result, wantParams)) {
938 std::string msg = "Parameter verification failed.";
939 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
940 return nullptr;
941 }
942 }
943
944 return NapiGetNull(env);
945 }
946
ParseParametersByPublish(const napi_env & env,const napi_value (& argv)[PUBLISH_MAX_PARA_BY_PUBLISHDATA],const size_t & argc,std::string & event,CommonEventPublishDataByjs & commonEventPublishData,napi_ref & callback)947 napi_value ParseParametersByPublish(const napi_env &env, const napi_value (&argv)[PUBLISH_MAX_PARA_BY_PUBLISHDATA],
948 const size_t &argc, std::string &event, CommonEventPublishDataByjs &commonEventPublishData, napi_ref &callback)
949 {
950 EVENT_LOGD("ParseParametersByPublish excute");
951
952 napi_valuetype valuetype;
953 // argv[0]: event
954 NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype));
955 if (valuetype != napi_string) {
956 EVENT_LOGE("Parameter type error. String expected.");
957 std::string msg = "Incorrect parameter types.The type of param must be string.";
958 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
959 return nullptr;
960 }
961
962 char str[STR_MAX_SIZE] = {0};
963 size_t strLen = 0;
964 napi_get_value_string_utf8(env, argv[0], str, STR_MAX_SIZE - 1, &strLen);
965 event = str;
966 EVENT_LOGD("ParseParametersByPublish event: %{public}s", str);
967
968 if (argc == PUBLISH_MAX_PARA_BY_PUBLISHDATA) {
969 // argv[1]: CommonEventPublishData
970 NAPI_CALL(env, napi_typeof(env, argv[1], &valuetype));
971 if (valuetype != napi_object) {
972 EVENT_LOGE("Wrong argument type. Object expected.");
973 std::string msg = "Incorrect parameter types.The type of param must be object.";
974 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
975 return nullptr;
976 }
977
978 // argv[1]: CommonEventPublishData:bundlename
979 if (GetBundlenameByPublish(env, argv[1], commonEventPublishData.bundleName) == nullptr) {
980 return nullptr;
981 }
982 // argv[1]: CommonEventPublishData:data
983 if (GetDataByPublish(env, argv[1], commonEventPublishData.data) == nullptr) {
984 return nullptr;
985 }
986 // argv[1]: CommonEventPublishData:code
987 if (GetCodeByPublish(env, argv[1], commonEventPublishData.code) == nullptr) {
988 return nullptr;
989 }
990 // argv[1]: CommonEventPublishData:permissions
991 if (GetSubscriberPermissionsByPublish(env, argv[1], commonEventPublishData.subscriberPermissions) == nullptr) {
992 return nullptr;
993 }
994 // argv[1]: CommonEventPublishData:isOrdered
995 if (GetIsOrderedByPublish(env, argv[1], commonEventPublishData.isOrdered) == nullptr) {
996 return nullptr;
997 }
998 // argv[1]: CommonEventPublishData:isSticky
999 if (GetIsStickyByPublish(env, argv[1], commonEventPublishData.isSticky) == nullptr) {
1000 return nullptr;
1001 }
1002 // argv[1]: CommonEventPublishData:parameters
1003 if (GetParametersByPublish(env, argv[1], commonEventPublishData.wantParams) == nullptr) {
1004 return nullptr;
1005 }
1006
1007 // argv[2]: callback
1008 NAPI_CALL(env, napi_typeof(env, argv[PUBLISH_MAX_PARA], &valuetype));
1009 if (valuetype != napi_function) {
1010 EVENT_LOGE("Wrong argument type. Function expected.");
1011 std::string msg = "Incorrect parameter types.The type of param must be function.";
1012 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
1013 return nullptr;
1014 }
1015 napi_create_reference(env, argv[PUBLISH_MAX_PARA], 1, &callback);
1016 } else {
1017 NAPI_CALL(env, napi_typeof(env, argv[1], &valuetype));
1018 if (valuetype != napi_function) {
1019 EVENT_LOGE("Wrong argument type. Function expected.");
1020 std::string msg = "Incorrect parameter types.The type of param must be function.";
1021 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
1022 return nullptr;
1023 }
1024 napi_create_reference(env, argv[1], 1, &callback);
1025 }
1026
1027 return NapiGetNull(env);
1028 }
1029
PaddingCallbackInfoPublish(Want & want,AsyncCallbackInfoPublish * & asyncCallbackInfo,const CommonEventPublishDataByjs & commonEventPublishDatajs)1030 void PaddingCallbackInfoPublish(Want &want, AsyncCallbackInfoPublish *&asyncCallbackInfo,
1031 const CommonEventPublishDataByjs &commonEventPublishDatajs)
1032 {
1033 EVENT_LOGD("NapiPaddingCallbackInfoPublish start");
1034 want.SetParams(commonEventPublishDatajs.wantParams);
1035 asyncCallbackInfo->commonEventData.SetCode(commonEventPublishDatajs.code);
1036 asyncCallbackInfo->commonEventData.SetData(commonEventPublishDatajs.data);
1037 asyncCallbackInfo->commonEventPublishInfo.SetSubscriberPermissions(commonEventPublishDatajs.subscriberPermissions);
1038 asyncCallbackInfo->commonEventPublishInfo.SetOrdered(commonEventPublishDatajs.isOrdered);
1039 asyncCallbackInfo->commonEventPublishInfo.SetSticky(commonEventPublishDatajs.isSticky);
1040 asyncCallbackInfo->commonEventPublishInfo.SetBundleName(commonEventPublishDatajs.bundleName);
1041 }
1042
ParseParametersByPublishAsUser(const napi_env & env,const napi_value (& argv)[PUBLISH_MAX_PARA_BY_USERID],const size_t & argc,std::string & event,int32_t & userId,CommonEventPublishDataByjs & commonEventPublishData,napi_ref & callback)1043 napi_value ParseParametersByPublishAsUser(const napi_env &env, const napi_value (&argv)[PUBLISH_MAX_PARA_BY_USERID],
1044 const size_t &argc, std::string &event, int32_t &userId, CommonEventPublishDataByjs &commonEventPublishData,
1045 napi_ref &callback)
1046 {
1047 EVENT_LOGD("ParseParametersByPublishAsUser start");
1048
1049 napi_valuetype valuetype;
1050 // argv[0]: event
1051 NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype));
1052 if (valuetype != napi_string) {
1053 EVENT_LOGE("Wrong Parameter type. String expected.");
1054 std::string msg = "Incorrect parameter types.The type of param must be string.";
1055 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
1056 return nullptr;
1057 }
1058
1059 char str[STR_MAX_SIZE] = {0};
1060 size_t strLen = 0;
1061 napi_get_value_string_utf8(env, argv[0], str, STR_MAX_SIZE - 1, &strLen);
1062 event = str;
1063 EVENT_LOGD("ParseParametersByPublishAsUser event = %{public}s", str);
1064
1065 // argv[1]: userId
1066 NAPI_CALL(env, napi_typeof(env, argv[1], &valuetype));
1067 if (valuetype != napi_number) {
1068 EVENT_LOGE("Wrong Parameter type. Number expected.");
1069 std::string msg = "Incorrect parameter types.The type of param must be number.";
1070 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
1071 return nullptr;
1072 }
1073 napi_get_value_int32(env, argv[1], &userId);
1074 EVENT_LOGD("ParseParametersByPublishAsUser userId = %{public}d", userId);
1075
1076 if (argc == PUBLISH_MAX_PARA_BY_USERID) {
1077 // argv[2]: CommonEventPublishData
1078 NAPI_CALL(env, napi_typeof(env, argv[ARGS_DATA_TWO], &valuetype));
1079 if (valuetype != napi_object) {
1080 EVENT_LOGE("Wrong argument type. Object expected.");
1081 std::string msg = "Incorrect parameter types.The type of param must be object.";
1082 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
1083 return nullptr;
1084 }
1085
1086 // argv[2]: CommonEventPublishData:bundlename
1087 if (GetBundlenameByPublish(env, argv[ARGS_DATA_TWO], commonEventPublishData.bundleName) == nullptr) {
1088 return nullptr;
1089 }
1090 // argv[2]: CommonEventPublishData:data
1091 if (GetDataByPublish(env, argv[ARGS_DATA_TWO], commonEventPublishData.data) == nullptr) {
1092 return nullptr;
1093 }
1094 // argv[2]: CommonEventPublishData:code
1095 if (GetCodeByPublish(env, argv[ARGS_DATA_TWO], commonEventPublishData.code) == nullptr) {
1096 return nullptr;
1097 }
1098 // argv[2]: CommonEventPublishData:permissions
1099 if (GetSubscriberPermissionsByPublish(env, argv[ARGS_DATA_TWO],
1100 commonEventPublishData.subscriberPermissions) == nullptr) {
1101 return nullptr;
1102 }
1103 // argv[2]: CommonEventPublishData:isOrdered
1104 if (GetIsOrderedByPublish(env, argv[ARGS_DATA_TWO], commonEventPublishData.isOrdered) == nullptr) {
1105 return nullptr;
1106 }
1107 // argv[2]: CommonEventPublishData:isSticky
1108 if (GetIsStickyByPublish(env, argv[ARGS_DATA_TWO], commonEventPublishData.isSticky) == nullptr) {
1109 return nullptr;
1110 }
1111 // argv[2]: CommonEventPublishData:parameters
1112 if (GetParametersByPublish(env, argv[ARGS_DATA_TWO], commonEventPublishData.wantParams) == nullptr) {
1113 return nullptr;
1114 }
1115
1116 // argv[3]: callback
1117 NAPI_CALL(env, napi_typeof(env, argv[PUBLISH_MAX_PARA_AS_USER], &valuetype));
1118 if (valuetype != napi_function) {
1119 EVENT_LOGE("Wrong argument type. Function expected.");
1120 std::string msg = "Incorrect parameter types.The type of param must be function.";
1121 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
1122 return nullptr;
1123 }
1124 napi_create_reference(env, argv[PUBLISH_MAX_PARA_AS_USER], 1, &callback);
1125 } else {
1126 NAPI_CALL(env, napi_typeof(env, argv[ARGS_DATA_TWO], &valuetype));
1127 if (valuetype != napi_function) {
1128 EVENT_LOGE("Wrong argument type. Function expected.");
1129 std::string msg = "Incorrect parameter types.The type of param must be function.";
1130 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
1131 return nullptr;
1132 }
1133 napi_create_reference(env, argv[ARGS_DATA_TWO], 1, &callback);
1134 }
1135
1136 return NapiGetNull(env);
1137 }
1138
ParseParametersByUnsubscribe(const napi_env & env,const size_t & argc,const napi_value (& argv)[UNSUBSCRIBE_MAX_PARA],std::shared_ptr<SubscriberInstance> & subscriber,napi_ref & callback)1139 napi_value ParseParametersByUnsubscribe(const napi_env &env, const size_t &argc,
1140 const napi_value (&argv)[UNSUBSCRIBE_MAX_PARA], std::shared_ptr<SubscriberInstance> &subscriber, napi_ref &callback)
1141 {
1142 EVENT_LOGD("ParseParametersByUnsubscribe excute");
1143
1144 napi_valuetype valuetype;
1145 napi_value result = nullptr;
1146 // argv[0]:subscriber
1147 NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype));
1148 if (valuetype != napi_object) {
1149 EVENT_LOGE("Parameter type error for arg0. Subscribe expected.");
1150 std::string msg = "Incorrect parameter types.The type of param must be subscribe.";
1151 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
1152 return nullptr;
1153 }
1154
1155 bool isFind = false;
1156 if (GetSubscriberByUnsubscribe(env, argv[0], subscriber, isFind) == nullptr) {
1157 return nullptr;
1158 }
1159
1160 // argv[1]:callback
1161 if (argc >= UNSUBSCRIBE_MAX_PARA) {
1162 NAPI_CALL(env, napi_typeof(env, argv[1], &valuetype));
1163 if (valuetype != napi_function) {
1164 EVENT_LOGE("Parameter type error. Function expected.");
1165 std::string msg = "Incorrect parameter types.The type of param must be function.";
1166 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
1167 return nullptr;
1168 }
1169 napi_create_reference(env, argv[1], 1, &callback);
1170 }
1171
1172 napi_get_boolean(env, isFind, &result);
1173
1174 return result;
1175 }
1176
ParseParametersByRemoveSticky(const napi_env & env,const napi_callback_info & info,std::string & event,CallbackPromiseInfo & params)1177 napi_value ParseParametersByRemoveSticky(const napi_env &env,
1178 const napi_callback_info &info, std::string &event, CallbackPromiseInfo ¶ms)
1179 {
1180 EVENT_LOGD("ParseParametersByRemoveSticky start");
1181
1182 size_t argc = REMOVE_STICKY_MAX_PARA;
1183 napi_value argv[REMOVE_STICKY_MAX_PARA] = {nullptr};
1184 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
1185 if (argc < REMOVE_STICKY_MAX_PARA - 1) {
1186 EVENT_LOGE("Wrong number of arguments.");
1187 std::string msg = "Incorrect parameter types.The type of param must be number.";
1188 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
1189 return nullptr;
1190 }
1191
1192 // argv[0]: event
1193 napi_valuetype valuetype = napi_undefined;
1194 NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
1195 if (valuetype != napi_string && valuetype != napi_number && valuetype != napi_boolean) {
1196 EVENT_LOGE("Wrong argument type. String expected.");
1197 std::string msg = "Incorrect parameter types.The type of param must be string.";
1198 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
1199 return nullptr;
1200 }
1201 if (valuetype == napi_string) {
1202 size_t strLen = 0;
1203 char str[STR_MAX_SIZE] = {0};
1204 NAPI_CALL(env, napi_get_value_string_utf8(env, argv[PARAM0], str, STR_MAX_SIZE - 1, &strLen));
1205 event = str;
1206 } else if (valuetype == napi_number) {
1207 int64_t number = 0;
1208 NAPI_CALL(env, napi_get_value_int64(env, argv[PARAM0], &number));
1209 event = std::to_string(number);
1210 } else {
1211 bool result = false;
1212 NAPI_CALL(env, napi_get_value_bool(env, argv[PARAM0], &result));
1213 event = std::to_string(result);
1214 }
1215
1216 // argv[1]:callback
1217 if (argc >= REMOVE_STICKY_MAX_PARA) {
1218 NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
1219 if (valuetype != napi_function) {
1220 EVENT_LOGE("Callback is not function excute promise.");
1221 return NapiGetNull(env);
1222 }
1223 napi_create_reference(env, argv[PARAM1], 1, ¶ms.callback);
1224 }
1225
1226 return NapiGetNull(env);
1227 }
1228
GetEventsByCreateSubscriber(const napi_env & env,const napi_value & argv,std::vector<std::string> & events)1229 napi_value GetEventsByCreateSubscriber(const napi_env &env, const napi_value &argv, std::vector<std::string> &events)
1230 {
1231 EVENT_LOGD("GetEventsByCreateSubscriber start");
1232 napi_valuetype valuetype;
1233 bool hasProperty = false;
1234 bool isArray = false;
1235 napi_value eventsNapi = nullptr;
1236 size_t strLen = 0;
1237 uint32_t length = 0;
1238 // get events
1239 NAPI_CALL(env, napi_has_named_property(env, argv, "events", &hasProperty));
1240 if (!hasProperty) {
1241 EVENT_LOGE("Property events expected");
1242 std::string msg = "Incorrect parameter types.The type of param must be events.";
1243 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
1244 return nullptr;
1245 }
1246 napi_get_named_property(env, argv, "events", &eventsNapi);
1247 napi_is_array(env, eventsNapi, &isArray);
1248 if (!isArray) {
1249 EVENT_LOGE("Parameter type error . Array expected.");
1250 std::string msg = "Incorrect parameter types.The type of param must be array.";
1251 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
1252 return nullptr;
1253 }
1254 napi_get_array_length(env, eventsNapi, &length);
1255 if (length == 0) {
1256 EVENT_LOGE("The array is empty.");
1257 std::string msg = "Parameter verification failed.The array is empty.";
1258 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
1259 return nullptr;
1260 }
1261 for (size_t i = 0; i < length; i++) {
1262 napi_value event = nullptr;
1263 napi_get_element(env, eventsNapi, i, &event);
1264 NAPI_CALL(env, napi_typeof(env, event, &valuetype));
1265 if (valuetype != napi_string) {
1266 EVENT_LOGE("Wrong argument type. String expected.");
1267 std::string msg = "Incorrect parameter types.The type of param must be string.";
1268 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
1269 return nullptr;
1270 }
1271 char str[STR_MAX_SIZE] = {0};
1272 NAPI_CALL(env, napi_get_value_string_utf8(env, event, str, STR_MAX_SIZE - 1, &strLen));
1273 EVENT_LOGD("event = %{public}s", str);
1274 events.emplace_back(str);
1275 }
1276
1277 return NapiGetNull(env);
1278 }
1279
GetPublisherPermissionByCreateSubscriber(const napi_env & env,const napi_value & argv,CommonEventSubscribeInfo & info)1280 napi_value GetPublisherPermissionByCreateSubscriber(
1281 const napi_env &env, const napi_value &argv, CommonEventSubscribeInfo &info)
1282 {
1283 EVENT_LOGD("enter");
1284
1285 bool hasProperty = false;
1286 napi_value result = nullptr;
1287 napi_valuetype valuetype = napi_undefined;
1288 size_t strLen = 0;
1289 char str[STR_MAX_SIZE] = {0};
1290
1291 // publisherPermission
1292 NAPI_CALL(env, napi_has_named_property(env, argv, "publisherPermission", &hasProperty));
1293 if (hasProperty) {
1294 napi_get_named_property(env, argv, "publisherPermission", &result);
1295 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1296 if (valuetype != napi_string) {
1297 EVENT_LOGE("Wrong argument type. String expected.");
1298 std::string msg = "Incorrect parameter types.The type of param must be string.";
1299 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
1300 return nullptr;
1301 }
1302 NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
1303 info.SetPermission(str);
1304 }
1305
1306 return NapiGetNull(env);
1307 }
1308
GetPublisherDeviceIdByCreateSubscriber(const napi_env & env,const napi_value & argv,CommonEventSubscribeInfo & info)1309 napi_value GetPublisherDeviceIdByCreateSubscriber(
1310 const napi_env &env, const napi_value &argv, CommonEventSubscribeInfo &info)
1311 {
1312 EVENT_LOGD("enter");
1313
1314 bool hasProperty = false;
1315 napi_value result = nullptr;
1316 napi_valuetype valuetype = napi_undefined;
1317 size_t strLen = 0;
1318 char str[STR_MAX_SIZE] = {0};
1319
1320 // publisherDeviceId
1321 NAPI_CALL(env, napi_has_named_property(env, argv, "publisherDeviceId", &hasProperty));
1322 if (hasProperty) {
1323 napi_get_named_property(env, argv, "publisherDeviceId", &result);
1324 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1325 if (valuetype != napi_string) {
1326 EVENT_LOGE("Wrong argument type. String expected.");
1327 std::string msg = "Incorrect parameter types.The type of param must be string.";
1328 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
1329 return nullptr;
1330 }
1331 NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
1332 info.SetDeviceId(str);
1333 }
1334
1335 return NapiGetNull(env);
1336 }
1337
GetUserIdByCreateSubscriber(const napi_env & env,const napi_value & argv,CommonEventSubscribeInfo & info)1338 napi_value GetUserIdByCreateSubscriber(const napi_env &env, const napi_value &argv, CommonEventSubscribeInfo &info)
1339 {
1340 EVENT_LOGD("enter");
1341
1342 bool hasUserId = false;
1343 napi_value result = nullptr;
1344 napi_valuetype valuetype = napi_undefined;
1345 int32_t value = 0;
1346
1347 // userId
1348 NAPI_CALL(env, napi_has_named_property(env, argv, "userId", &hasUserId));
1349 if (hasUserId) {
1350 napi_get_named_property(env, argv, "userId", &result);
1351 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1352 if (valuetype != napi_number) {
1353 EVENT_LOGE("Wrong argument type. Number expected.");
1354 std::string msg = "Incorrect parameter types.The type of param must be number.";
1355 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
1356 return nullptr;
1357 }
1358 NAPI_CALL(env, napi_get_value_int32(env, result, &value));
1359 info.SetUserId(value);
1360 }
1361
1362 return NapiGetNull(env);
1363 }
1364
GetPriorityByCreateSubscriber(const napi_env & env,const napi_value & argv,CommonEventSubscribeInfo & info)1365 napi_value GetPriorityByCreateSubscriber(const napi_env &env, const napi_value &argv, CommonEventSubscribeInfo &info)
1366 {
1367 EVENT_LOGD("enter");
1368
1369 bool hasProperty = false;
1370 napi_value result = nullptr;
1371 napi_valuetype valuetype = napi_undefined;
1372 int32_t value = 0;
1373
1374 // priority
1375 NAPI_CALL(env, napi_has_named_property(env, argv, "priority", &hasProperty));
1376 if (hasProperty) {
1377 napi_get_named_property(env, argv, "priority", &result);
1378 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1379 if (valuetype != napi_number) {
1380 EVENT_LOGE("Wrong argument type. Number expected.");
1381 std::string msg = "Incorrect parameter types.The type of param must be number.";
1382 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
1383 return nullptr;
1384 }
1385 NAPI_CALL(env, napi_get_value_int32(env, result, &value));
1386 info.SetPriority(value);
1387 }
1388
1389 return NapiGetNull(env);
1390 }
1391
GetPublisherBundleNameByCreateSubscriber(const napi_env & env,const napi_value & argv,CommonEventSubscribeInfo & info)1392 napi_value GetPublisherBundleNameByCreateSubscriber(
1393 const napi_env &env, const napi_value &argv, CommonEventSubscribeInfo &info)
1394 {
1395 EVENT_LOGD("Called.");
1396 bool hasPublisherBundleName = false;
1397 napi_value result = nullptr;
1398 napi_valuetype valuetype = napi_undefined;
1399 size_t strLen = 0;
1400 char str[STR_MAX_SIZE] = {0};
1401
1402 // publisherBundleName
1403 NAPI_CALL(env, napi_has_named_property(env, argv, "publisherBundleName", &hasPublisherBundleName));
1404 if (hasPublisherBundleName) {
1405 napi_get_named_property(env, argv, "publisherBundleName", &result);
1406 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1407 if (valuetype != napi_string) {
1408 EVENT_LOGE("Wrong argument type. String expected.");
1409 std::string msg = "Incorrect parameter types.The type of param must be string.";
1410 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
1411 return nullptr;
1412 }
1413 NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
1414 info.SetPublisherBundleName(str);
1415 }
1416
1417 return NapiGetNull(env);
1418 }
1419
ParseParametersConstructor(const napi_env & env,const napi_callback_info & info,napi_value & thisVar,CommonEventSubscribeInfo & params)1420 napi_value ParseParametersConstructor(
1421 const napi_env &env, const napi_callback_info &info, napi_value &thisVar, CommonEventSubscribeInfo ¶ms)
1422 {
1423 EVENT_LOGD("enter");
1424 size_t argc = 1;
1425 napi_value argv[1] = {nullptr};
1426 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
1427 if (argc < 1) {
1428 EVENT_LOGE("Wrong number of arguments");
1429 std::string msg = "Incorrect parameter types.The type of param must be number.";
1430 NapiThrow(env, ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, msg);
1431 return nullptr;
1432 }
1433
1434 // events: Array<string>
1435 std::vector<std::string> events;
1436 if (!GetEventsByCreateSubscriber(env, argv[0], events)) {
1437 return nullptr;
1438 }
1439 MatchingSkills matchingSkills;
1440 for (const auto &event : events) {
1441 matchingSkills.AddEvent(event);
1442 }
1443 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1444
1445 // publisherPermission?: string
1446 if (!GetPublisherPermissionByCreateSubscriber(env, argv[0], subscribeInfo)) {
1447 return nullptr;
1448 }
1449
1450 // publisherDeviceId?: string
1451 if (!GetPublisherDeviceIdByCreateSubscriber(env, argv[0], subscribeInfo)) {
1452 return nullptr;
1453 }
1454
1455 // userId?: number
1456 if (!GetUserIdByCreateSubscriber(env, argv[0], subscribeInfo)) {
1457 return nullptr;
1458 }
1459
1460 // priority?: number
1461 if (!GetPriorityByCreateSubscriber(env, argv[0], subscribeInfo)) {
1462 return nullptr;
1463 }
1464
1465 // publisherBundleName?: string
1466 if (!GetPublisherBundleNameByCreateSubscriber(env, argv[0], subscribeInfo)) {
1467 return nullptr;
1468 }
1469
1470 params = subscribeInfo;
1471 return NapiGetNull(env);
1472 }
1473
1474 }
1475 }
1476