1 /*
2  * Copyright (c) 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 "notification_napi.h"
17 #include "ans_inner_errors.h"
18 #include "location_log.h"
19 #include "js_native_api.h"
20 #include "js_native_api_types.h"
21 #include "napi_common.h"
22 #include "napi_common_util.h"
23 #include "notification_action_button.h"
24 #include "notification_capsule.h"
25 #include "notification_constant.h"
26 #include "notification_local_live_view_content.h"
27 #include "notification_progress.h"
28 #include "notification_time.h"
29 #include "pixel_map_napi.h"
30 
31 namespace OHOS {
32 namespace Location {
GetNotificationRequestByNumber(const napi_env & env,const napi_value & value,NotificationRequest & request)33 napi_value NotificationNapi::GetNotificationRequestByNumber(
34     const napi_env &env, const napi_value &value, NotificationRequest &request)
35 {
36     LBSLOGD(NAPI_UTILS, "enter");
37     // id?: number
38     if (GetNotificationId(env, value, request) == nullptr) {
39         return nullptr;
40     }
41     // deliveryTime?: number
42     if (GetNotificationDeliveryTime(env, value, request) == nullptr) {
43         return nullptr;
44     }
45     // autoDeletedTime?: number
46     if (GetNotificationAutoDeletedTime(env, value, request) == nullptr) {
47         return nullptr;
48     }
49     // color?: number
50     if (GetNotificationColor(env, value, request) == nullptr) {
51         return nullptr;
52     }
53     // badgeIconStyle?: number
54     if (GetNotificationBadgeIconStyle(env, value, request) == nullptr) {
55         return nullptr;
56     }
57     // badgeNumber?: number
58     if (GetNotificationBadgeNumber(env, value, request) == nullptr) {
59         return nullptr;
60     }
61     // notificationControlFlags?: number
62     if (GetNotificationControlFlags(env, value, request) == nullptr) {
63         return nullptr;
64     }
65 
66     return NapiGetNull(env);
67 }
68 
GetNotificationRequestByString(const napi_env & env,const napi_value & value,NotificationRequest & request)69 napi_value NotificationNapi::GetNotificationRequestByString(
70     const napi_env &env, const napi_value &value, NotificationRequest &request)
71 {
72     LBSLOGD(NAPI_UTILS, "enter");
73     // classification?: string
74     if (GetNotificationClassification(env, value, request) == nullptr) {
75         return nullptr;
76     }
77     // statusBarText?: string
78     if (GetNotificationStatusBarText(env, value, request) == nullptr) {
79         return nullptr;
80     }
81     // label?: string
82     if (GetNotificationLabel(env, value, request) == nullptr) {
83         return nullptr;
84     }
85     // groupName?: string
86     if (GetNotificationGroupName(env, value, request) == nullptr) {
87         return nullptr;
88     }
89     // appMessageId?: string
90     if (GetNotificationAppMessageId(env, value, request) == nullptr) {
91         return nullptr;
92     }
93     return NapiGetNull(env);
94 }
95 
GetNotificationRequestByBool(const napi_env & env,const napi_value & value,NotificationRequest & request)96 napi_value NotificationNapi::GetNotificationRequestByBool(
97     const napi_env &env, const napi_value &value, NotificationRequest &request)
98 {
99     LBSLOGD(NAPI_UTILS, "enter");
100     // isOngoing?: boolean
101     if (GetNotificationIsOngoing(env, value, request) == nullptr) {
102         return nullptr;
103     }
104     // isUnremovable?: boolean
105     if (GetNotificationIsUnremovable(env, value, request) == nullptr) {
106         return nullptr;
107     }
108     // tapDismissed?: boolean
109     if (GetNotificationtapDismissed(env, value, request) == nullptr) {
110         return nullptr;
111     }
112     // colorEnabled?: boolean
113     if (GetNotificationColorEnabled(env, value, request) == nullptr) {
114         return nullptr;
115     }
116     // isAlertOnce?: boolean
117     if (GetNotificationIsAlertOnce(env, value, request) == nullptr) {
118         return nullptr;
119     }
120     // isStopwatch?: boolean
121     if (GetNotificationIsStopwatch(env, value, request) == nullptr) {
122         return nullptr;
123     }
124     // isCountDown?: boolean
125     if (GetNotificationIsCountDown(env, value, request) == nullptr) {
126         return nullptr;
127     }
128     // showDeliveryTime?: boolean
129     if (GetNotificationShowDeliveryTime(env, value, request) == nullptr) {
130         return nullptr;
131     }
132 
133     GetNotificationIsRemoveAllowed(env, value, request);
134 
135     return NapiGetNull(env);
136 }
137 
GetNotificationRequestByCustom(const napi_env & env,const napi_value & value,NotificationRequest & request)138 napi_value NotificationNapi::GetNotificationRequestByCustom(
139     const napi_env &env, const napi_value &value, NotificationRequest &request)
140 {
141     LBSLOGD(NAPI_UTILS, "enter");
142     // content: NotificationContent
143     if (GetNotificationContent(env, value, request) == nullptr) {
144         return nullptr;
145     }
146     // slotType?: notification.SlotType
147     if (GetNotificationSlotType(env, value, request) == nullptr) {
148         return nullptr;
149     }
150     // wantAgent?: WantAgent
151     if (GetNotificationWantAgent(env, value, request) == nullptr) {
152         return nullptr;
153     }
154     // extraInfo?: {[key: string]: any}
155     if (GetNotificationExtraInfo(env, value, request) == nullptr) {
156         return nullptr;
157     }
158     // removalWantAgent?: WantAgent
159     if (GetNotificationRemovalWantAgent(env, value, request) == nullptr) {
160         return nullptr;
161     }
162     // maxScreenWantAgent?: WantAgent
163     if (GetNotificationMaxScreenWantAgent(env, value, request) == nullptr) {
164         return nullptr;
165     }
166     // actionButtons?: Array<NotificationActionButton>
167     if (GetNotificationActionButtons(env, value, request) == nullptr) {
168         return nullptr;
169     }
170     // smallIcon?: image.PixelMap
171     if (GetNotificationSmallIcon(env, value, request) == nullptr) {
172         return nullptr;
173     }
174     // largeIcon?: image.PixelMap
175     if (GetNotificationLargeIcon(env, value, request) == nullptr) {
176         return nullptr;
177     }
178     // overlayIcon?: image.PixelMap
179     if (GetNotificationOverlayIcon(env, value, request) == nullptr) {
180         return nullptr;
181     }
182     // distributedOption?:DistributedOptions
183     if (GetNotificationRequestDistributedOptions(env, value, request) == nullptr) {
184         return nullptr;
185     }
186     // template?: NotificationTemplate
187     if (GetNotificationTemplate(env, value, request) == nullptr) {
188         return nullptr;
189     }
190     // representativeBundle?: BundleOption
191     if (GetNotificationBundleOption(env, value, request) == nullptr) {
192         return nullptr;
193     }
194     // unifiedGroupInfo?: NotificationUnifiedGroupInfo
195     if (GetNotificationUnifiedGroupInfo(env, value, request) == nullptr) {
196         return nullptr;
197     }
198     return NapiGetNull(env);
199 }
200 
GetNotificationRequest(const napi_env & env,const napi_value & value,NotificationRequest & request)201 napi_value NotificationNapi::GetNotificationRequest(
202     const napi_env &env, const napi_value &value, NotificationRequest &request)
203 {
204     LBSLOGD(NAPI_UTILS, "enter");
205     if (!GetNotificationRequestByNumber(env, value, request)) {
206         return nullptr;
207     }
208     if (!GetNotificationRequestByString(env, value, request)) {
209         return nullptr;
210     }
211     if (!GetNotificationRequestByBool(env, value, request)) {
212         return nullptr;
213     }
214     if (!GetNotificationRequestByCustom(env, value, request)) {
215         return nullptr;
216     }
217     return NapiGetNull(env);
218 }
219 
GetNotificationSmallIcon(const napi_env & env,const napi_value & value,NotificationRequest & request)220 napi_value NotificationNapi::GetNotificationSmallIcon(
221     const napi_env &env, const napi_value &value, NotificationRequest &request)
222 {
223     LBSLOGD(NAPI_UTILS, "enter");
224 
225     napi_valuetype valuetype = napi_undefined;
226     napi_value result = nullptr;
227     bool hasProperty = false;
228 
229     NAPI_CALL(env, napi_has_named_property(env, value, "smallIcon", &hasProperty));
230     if (hasProperty) {
231         napi_get_named_property(env, value, "smallIcon", &result);
232         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
233         if (valuetype != napi_object) {
234             LBSLOGE(NAPI_UTILS, "Argument type is not object.");
235             return nullptr;
236         }
237         std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
238         pixelMap = Media::PixelMapNapi::GetPixelMap(env, result);
239         if (pixelMap == nullptr) {
240             LBSLOGE(NAPI_UTILS, "Invalid object pixelMap");
241             return nullptr;
242         }
243         request.SetLittleIcon(pixelMap);
244     }
245 
246     return NapiGetNull(env);
247 }
248 
GetNotificationLargeIcon(const napi_env & env,const napi_value & value,NotificationRequest & request)249 napi_value NotificationNapi::GetNotificationLargeIcon(
250     const napi_env &env, const napi_value &value, NotificationRequest &request)
251 {
252     LBSLOGD(NAPI_UTILS, "enter");
253 
254     napi_valuetype valuetype = napi_undefined;
255     napi_value result = nullptr;
256     bool hasProperty = false;
257 
258     NAPI_CALL(env, napi_has_named_property(env, value, "largeIcon", &hasProperty));
259     if (hasProperty) {
260         napi_get_named_property(env, value, "largeIcon", &result);
261         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
262         if (valuetype != napi_object) {
263             LBSLOGE(NAPI_UTILS, "Wrong argument type. Object expected.");
264             return nullptr;
265         }
266         std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
267         pixelMap = Media::PixelMapNapi::GetPixelMap(env, result);
268         if (pixelMap == nullptr) {
269             LBSLOGE(NAPI_UTILS, "Invalid object pixelMap");
270             return nullptr;
271         }
272         request.SetBigIcon(pixelMap);
273     }
274 
275     return NapiGetNull(env);
276 }
277 
GetNotificationOverlayIcon(const napi_env & env,const napi_value & value,NotificationRequest & request)278 napi_value NotificationNapi::GetNotificationOverlayIcon(
279     const napi_env &env, const napi_value &value, NotificationRequest &request)
280 {
281     LBSLOGD(NAPI_UTILS, "enter");
282 
283     napi_valuetype valuetype = napi_undefined;
284     napi_value result = nullptr;
285     bool hasProperty = false;
286 
287     NAPI_CALL(env, napi_has_named_property(env, value, "overlayIcon", &hasProperty));
288     if (hasProperty) {
289         napi_get_named_property(env, value, "overlayIcon", &result);
290         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
291         if (valuetype != napi_object) {
292             LBSLOGE(NAPI_UTILS, "Wrong argument type. Object expected.");
293             return nullptr;
294         }
295         std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
296         pixelMap = Media::PixelMapNapi::GetPixelMap(env, result);
297         if (pixelMap == nullptr) {
298             LBSLOGE(NAPI_UTILS, "Invalid object pixelMap");
299             return nullptr;
300         }
301         request.SetOverlayIcon(pixelMap);
302     }
303 
304     return NapiGetNull(env);
305 }
306 
GetNotificationSupportDisplayDevices(const napi_env & env,const napi_value & value,NotificationRequest & request)307 napi_value NotificationNapi::GetNotificationSupportDisplayDevices(
308     const napi_env &env, const napi_value &value, NotificationRequest &request)
309 {
310     LBSLOGD(NAPI_UTILS, "enter");
311 
312     bool isArray = false;
313     bool hasProperty = false;
314     napi_valuetype valuetype = napi_undefined;
315     napi_value supportDisplayDevices = nullptr;
316     size_t strLen = 0;
317     uint32_t length = 0;
318 
319     NAPI_CALL(env, napi_has_named_property(env, value, "supportDisplayDevices", &hasProperty));
320     if (hasProperty) {
321         napi_get_named_property(env, value, "supportDisplayDevices", &supportDisplayDevices);
322         napi_is_array(env, supportDisplayDevices, &isArray);
323         if (!isArray) {
324             LBSLOGE(NAPI_UTILS, "Property supportDisplayDevices is expected to be an array.");
325             return nullptr;
326         }
327 
328         napi_get_array_length(env, supportDisplayDevices, &length);
329         if (length == 0) {
330             LBSLOGE(NAPI_UTILS, "The array is empty.");
331             return nullptr;
332         }
333         std::vector<std::string> devices;
334         for (size_t i = 0; i < length; i++) {
335             napi_value line = nullptr;
336             napi_get_element(env, supportDisplayDevices, i, &line);
337             NAPI_CALL(env, napi_typeof(env, line, &valuetype));
338             if (valuetype != napi_string) {
339                 LBSLOGE(NAPI_UTILS, "Wrong argument type. String expected.");
340                 return nullptr;
341             }
342             char str[STR_MAX_SIZE] = {0};
343             NAPI_CALL(env, napi_get_value_string_utf8(env, line, str, STR_MAX_SIZE - 1, &strLen));
344             devices.emplace_back(str);
345             LBSLOGI(NAPI_UTILS, "supportDisplayDevices = %{public}s", str);
346         }
347         request.SetDevicesSupportDisplay(devices);
348     }
349     return NapiGetNull(env);
350 }
351 
GetNotificationSupportOperateDevices(const napi_env & env,const napi_value & value,NotificationRequest & request)352 napi_value NotificationNapi::GetNotificationSupportOperateDevices(
353     const napi_env &env, const napi_value &value, NotificationRequest &request)
354 {
355     LBSLOGD(NAPI_UTILS, "enter");
356 
357     bool isArray = false;
358     bool hasProperty = false;
359     napi_valuetype valuetype = napi_undefined;
360     napi_value supportOperateDevices = nullptr;
361     size_t strLen = 0;
362     uint32_t length = 0;
363 
364     NAPI_CALL(env, napi_has_named_property(env, value, "supportOperateDevices", &hasProperty));
365     if (hasProperty) {
366         napi_get_named_property(env, value, "supportOperateDevices", &supportOperateDevices);
367         napi_is_array(env, supportOperateDevices, &isArray);
368         if (!isArray) {
369             LBSLOGE(NAPI_UTILS, "Property supportOperateDevices is expected to be an array.");
370             return nullptr;
371         }
372 
373         napi_get_array_length(env, supportOperateDevices, &length);
374         if (length == 0) {
375             LBSLOGE(NAPI_UTILS, "The array is empty.");
376             return nullptr;
377         }
378         std::vector<std::string> devices;
379         for (size_t i = 0; i < length; i++) {
380             napi_value line = nullptr;
381             napi_get_element(env, supportOperateDevices, i, &line);
382             NAPI_CALL(env, napi_typeof(env, line, &valuetype));
383             if (valuetype != napi_string) {
384                 LBSLOGE(NAPI_UTILS, "Wrong argument type. String expected.");
385                 return nullptr;
386             }
387             char str[STR_MAX_SIZE] = {0};
388             NAPI_CALL(env, napi_get_value_string_utf8(env, line, str, STR_MAX_SIZE - 1, &strLen));
389             devices.emplace_back(str);
390             LBSLOGI(NAPI_UTILS, "supportOperateDevices = %{public}s", str);
391         }
392         request.SetDevicesSupportOperate(devices);
393     }
394 
395     return NapiGetNull(env);
396 }
397 
GetNotificationId(const napi_env & env,const napi_value & value,NotificationRequest & request)398 napi_value NotificationNapi::GetNotificationId(
399     const napi_env &env, const napi_value &value, NotificationRequest &request)
400 {
401     LBSLOGD(NAPI_UTILS, "enter");
402 
403     napi_valuetype valuetype = napi_undefined;
404     napi_value result = nullptr;
405     bool hasProperty = false;
406     int32_t notificationId = 0;
407 
408     NAPI_CALL(env, napi_has_named_property(env, value, "id", &hasProperty));
409     if (hasProperty) {
410         napi_get_named_property(env, value, "id", &result);
411         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
412         if (valuetype != napi_number) {
413             LBSLOGE(NAPI_UTILS, "Wrong argument type. Number expected.");
414             return nullptr;
415         }
416         napi_get_value_int32(env, result, &notificationId);
417         request.SetNotificationId(notificationId);
418         LBSLOGI(NAPI_UTILS, "notificationId = %{public}d", notificationId);
419     } else {
420         LBSLOGI(NAPI_UTILS, "default notificationId = 0");
421         request.SetNotificationId(0);
422     }
423 
424     return NapiGetNull(env);
425 }
426 
GetNotificationSlotType(const napi_env & env,const napi_value & value,NotificationRequest & request)427 napi_value NotificationNapi::GetNotificationSlotType(
428     const napi_env &env, const napi_value &value, NotificationRequest &request)
429 {
430     LBSLOGD(NAPI_UTILS, "enter");
431 
432     napi_valuetype valuetype = napi_undefined;
433     napi_value result = nullptr;
434     bool hasSlotType = false;
435     bool hasNotificationSlotType = false;
436     int32_t slotType = 0;
437 
438     NAPI_CALL(env, napi_has_named_property(env, value, "notificationSlotType", &hasNotificationSlotType));
439     if (hasNotificationSlotType) {
440         napi_get_named_property(env, value, "notificationSlotType", &result);
441         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
442         if (valuetype != napi_number) {
443             LBSLOGE(NAPI_UTILS, "Wrong argument type. Number expected.");
444             return nullptr;
445         }
446         napi_get_value_int32(env, result, &slotType);
447 
448         NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER;
449         if (!AnsEnumUtil::SlotTypeJSToC(SlotType(slotType), outType)) {
450             return nullptr;
451         }
452         request.SetSlotType(outType);
453         LBSLOGI(NAPI_UTILS, "notificationSlotType = %{public}d", slotType);
454         return NapiGetNull(env);
455     }
456 
457     NAPI_CALL(env, napi_has_named_property(env, value, "slotType", &hasSlotType));
458     if (hasSlotType) {
459         napi_get_named_property(env, value, "slotType", &result);
460         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
461         if (valuetype != napi_number) {
462             LBSLOGE(NAPI_UTILS, "Wrong argument type. Number expected.");
463             return nullptr;
464         }
465         napi_get_value_int32(env, result, &slotType);
466 
467         NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER;
468         if (!AnsEnumUtil::SlotTypeJSToC(SlotType(slotType), outType)) {
469             return nullptr;
470         }
471         request.SetSlotType(outType);
472         LBSLOGI(NAPI_UTILS, "slotType = %{public}d", slotType);
473     } else {
474         LBSLOGI(NAPI_UTILS, "default slotType = OTHER");
475         request.SetSlotType(NotificationConstant::OTHER);
476     }
477 
478     return NapiGetNull(env);
479 }
480 
GetNotificationIsOngoing(const napi_env & env,const napi_value & value,NotificationRequest & request)481 napi_value NotificationNapi::GetNotificationIsOngoing(
482     const napi_env &env, const napi_value &value, NotificationRequest &request)
483 {
484     LBSLOGD(NAPI_UTILS, "enter");
485 
486     napi_valuetype valuetype = napi_undefined;
487     napi_value result = nullptr;
488     bool hasProperty = false;
489     bool isOngoing = false;
490 
491     NAPI_CALL(env, napi_has_named_property(env, value, "isOngoing", &hasProperty));
492     if (hasProperty) {
493         napi_get_named_property(env, value, "isOngoing", &result);
494         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
495         if (valuetype != napi_boolean) {
496             LBSLOGE(NAPI_UTILS, "Wrong argument type. Bool expected.");
497             return nullptr;
498         }
499         napi_get_value_bool(env, result, &isOngoing);
500         request.SetInProgress(isOngoing);
501     }
502 
503     return NapiGetNull(env);
504 }
505 
GetNotificationIsUnremovable(const napi_env & env,const napi_value & value,NotificationRequest & request)506 napi_value NotificationNapi::GetNotificationIsUnremovable(
507     const napi_env &env, const napi_value &value, NotificationRequest &request)
508 {
509     LBSLOGD(NAPI_UTILS, "enter");
510 
511     napi_valuetype valuetype = napi_undefined;
512     napi_value result = nullptr;
513     bool hasProperty = false;
514     bool isUnremovable = false;
515 
516     NAPI_CALL(env, napi_has_named_property(env, value, "isUnremovable", &hasProperty));
517     if (hasProperty) {
518         napi_get_named_property(env, value, "isUnremovable", &result);
519         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
520         if (valuetype != napi_boolean) {
521             LBSLOGE(NAPI_UTILS, "Wrong argument type. Bool expected.");
522             return nullptr;
523         }
524         napi_get_value_bool(env, result, &isUnremovable);
525         request.SetUnremovable(isUnremovable);
526     }
527 
528     return NapiGetNull(env);
529 }
530 
GetNotificationDeliveryTime(const napi_env & env,const napi_value & value,NotificationRequest & request)531 napi_value NotificationNapi::GetNotificationDeliveryTime(
532     const napi_env &env, const napi_value &value, NotificationRequest &request)
533 {
534     LBSLOGD(NAPI_UTILS, "enter");
535 
536     napi_valuetype valuetype = napi_undefined;
537     napi_value result = nullptr;
538     bool hasProperty = false;
539     int64_t deliveryTime = 0;
540 
541     NAPI_CALL(env, napi_has_named_property(env, value, "deliveryTime", &hasProperty));
542     if (hasProperty) {
543         napi_get_named_property(env, value, "deliveryTime", &result);
544         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
545         if (valuetype != napi_number) {
546             LBSLOGE(NAPI_UTILS, "Wrong argument type. Number expected.");
547             return nullptr;
548         }
549         napi_get_value_int64(env, result, &deliveryTime);
550         request.SetDeliveryTime(deliveryTime);
551     }
552 
553     return NapiGetNull(env);
554 }
555 
GetNotificationtapDismissed(const napi_env & env,const napi_value & value,NotificationRequest & request)556 napi_value NotificationNapi::GetNotificationtapDismissed(
557     const napi_env &env, const napi_value &value, NotificationRequest &request)
558 {
559     LBSLOGD(NAPI_UTILS, "enter");
560 
561     napi_valuetype valuetype = napi_undefined;
562     napi_value result = nullptr;
563     bool hasProperty = false;
564     bool tapDismissed = true;
565 
566     NAPI_CALL(env, napi_has_named_property(env, value, "tapDismissed", &hasProperty));
567     if (hasProperty) {
568         napi_get_named_property(env, value, "tapDismissed", &result);
569         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
570         if (valuetype != napi_boolean) {
571             LBSLOGE(NAPI_UTILS, "Wrong argument type. Bool expected.");
572             return nullptr;
573         }
574         napi_get_value_bool(env, result, &tapDismissed);
575         request.SetTapDismissed(tapDismissed);
576     }
577 
578     return NapiGetNull(env);
579 }
580 
GetNotificationWantAgent(const napi_env & env,const napi_value & value,NotificationRequest & request)581 napi_value NotificationNapi::GetNotificationWantAgent(
582     const napi_env &env, const napi_value &value, NotificationRequest &request)
583 {
584     LBSLOGD(NAPI_UTILS, "enter");
585 
586     bool hasProperty = false;
587     AbilityRuntime::WantAgent::WantAgent *wantAgent = nullptr;
588     napi_value result = nullptr;
589     napi_valuetype valuetype = napi_undefined;
590 
591     NAPI_CALL(env, napi_has_named_property(env, value, "wantAgent", &hasProperty));
592     if (hasProperty) {
593         napi_get_named_property(env, value, "wantAgent", &result);
594         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
595         if (valuetype != napi_object) {
596             LBSLOGE(NAPI_UTILS, "Wrong argument type. Object expected.");
597             return nullptr;
598         }
599         napi_unwrap(env, result, (void **)&wantAgent);
600         if (wantAgent == nullptr) {
601             LBSLOGE(NAPI_UTILS, "Invalid object wantAgent");
602             return nullptr;
603         }
604         std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> sWantAgent =
605             std::make_shared<AbilityRuntime::WantAgent::WantAgent>(*wantAgent);
606         request.SetWantAgent(sWantAgent);
607     }
608 
609     return NapiGetNull(env);
610 }
611 
GetNotificationExtraInfo(const napi_env & env,const napi_value & value,NotificationRequest & request)612 napi_value NotificationNapi::GetNotificationExtraInfo(
613     const napi_env &env, const napi_value &value, NotificationRequest &request)
614 {
615     LBSLOGD(NAPI_UTILS, "enter");
616 
617     napi_valuetype valuetype = napi_undefined;
618     napi_value result = nullptr;
619     bool hasProperty = false;
620 
621     NAPI_CALL(env, napi_has_named_property(env, value, "extraInfo", &hasProperty));
622     if (hasProperty) {
623         napi_get_named_property(env, value, "extraInfo", &result);
624         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
625         if (valuetype != napi_object) {
626             LBSLOGE(NAPI_UTILS, "Wrong argument type. Object expected.");
627             return nullptr;
628         }
629         AAFwk::WantParams wantParams;
630         if (!OHOS::AppExecFwk::UnwrapWantParams(env, result, wantParams)) {
631             return nullptr;
632         }
633 
634         std::shared_ptr<AAFwk::WantParams> extras = std::make_shared<AAFwk::WantParams>(wantParams);
635         request.SetAdditionalData(extras);
636     }
637 
638     return NapiGetNull(env);
639 }
640 
GetNotificationGroupName(const napi_env & env,const napi_value & value,NotificationRequest & request)641 napi_value NotificationNapi::GetNotificationGroupName(
642     const napi_env &env, const napi_value &value, NotificationRequest &request)
643 {
644     LBSLOGD(NAPI_UTILS, "enter");
645 
646     napi_valuetype valuetype = napi_undefined;
647     napi_value result = nullptr;
648     bool hasProperty = false;
649     size_t strLen = 0;
650 
651     NAPI_CALL(env, napi_has_named_property(env, value, "groupName", &hasProperty));
652     if (hasProperty) {
653         napi_get_named_property(env, value, "groupName", &result);
654         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
655         if (valuetype != napi_string) {
656             LBSLOGE(NAPI_UTILS, "Wrong argument type. String expected.");
657             return nullptr;
658         }
659         char str[STR_MAX_SIZE] = {0};
660         NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
661         request.SetGroupName(str);
662     }
663 
664     return NapiGetNull(env);
665 }
666 
GetNotificationRemovalWantAgent(const napi_env & env,const napi_value & value,NotificationRequest & request)667 napi_value NotificationNapi::GetNotificationRemovalWantAgent(
668     const napi_env &env, const napi_value &value, NotificationRequest &request)
669 {
670     LBSLOGD(NAPI_UTILS, "enter");
671 
672     bool hasProperty = false;
673     AbilityRuntime::WantAgent::WantAgent *wantAgent = nullptr;
674     napi_value result = nullptr;
675     napi_valuetype valuetype = napi_undefined;
676 
677     NAPI_CALL(env, napi_has_named_property(env, value, "removalWantAgent", &hasProperty));
678     if (hasProperty) {
679         napi_get_named_property(env, value, "removalWantAgent", &result);
680         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
681         if (valuetype != napi_object) {
682             LBSLOGE(NAPI_UTILS, "Wrong argument type. Object expected.");
683             return nullptr;
684         }
685         napi_unwrap(env, result, (void **)&wantAgent);
686         if (wantAgent == nullptr) {
687             LBSLOGE(NAPI_UTILS, "Invalid object removalWantAgent");
688             return nullptr;
689         }
690         std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> removeWantAgent =
691             std::make_shared<AbilityRuntime::WantAgent::WantAgent>(*wantAgent);
692         if ((uint32_t)removeWantAgent->GetPendingWant()->GetType(
693             removeWantAgent->GetPendingWant()->GetTarget()) >= OPERATION_MAX_TYPE) {
694             request.SetRemovalWantAgent(removeWantAgent);
695         }
696     }
697 
698     return NapiGetNull(env);
699 }
700 
GetNotificationMaxScreenWantAgent(const napi_env & env,const napi_value & value,NotificationRequest & request)701 napi_value NotificationNapi::GetNotificationMaxScreenWantAgent(
702     const napi_env &env, const napi_value &value, NotificationRequest &request)
703 {
704     LBSLOGD(NAPI_UTILS, "enter");
705 
706     bool hasProperty = false;
707     AbilityRuntime::WantAgent::WantAgent *wantAgent = nullptr;
708     napi_value result = nullptr;
709     napi_valuetype valuetype = napi_undefined;
710 
711     NAPI_CALL(env, napi_has_named_property(env, value, "maxScreenWantAgent", &hasProperty));
712     if (hasProperty) {
713         napi_get_named_property(env, value, "maxScreenWantAgent", &result);
714         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
715         if (valuetype != napi_object) {
716             LBSLOGE(NAPI_UTILS, "Wrong argument type. Object expected.");
717             return nullptr;
718         }
719         napi_unwrap(env, result, (void **)&wantAgent);
720         if (wantAgent == nullptr) {
721             LBSLOGE(NAPI_UTILS, "Invalid object maxScreenWantAgent");
722             return nullptr;
723         }
724         std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> maxScreenWantAgent =
725             std::make_shared<AbilityRuntime::WantAgent::WantAgent>(*wantAgent);
726         request.SetMaxScreenWantAgent(maxScreenWantAgent);
727     }
728 
729     return NapiGetNull(env);
730 }
731 
GetNotificationAutoDeletedTime(const napi_env & env,const napi_value & value,NotificationRequest & request)732 napi_value NotificationNapi::GetNotificationAutoDeletedTime(
733     const napi_env &env, const napi_value &value, NotificationRequest &request)
734 {
735     LBSLOGD(NAPI_UTILS, "enter");
736 
737     napi_valuetype valuetype = napi_undefined;
738     napi_value result = nullptr;
739     bool hasProperty = false;
740     int64_t autoDeletedTime = 0;
741 
742     NAPI_CALL(env, napi_has_named_property(env, value, "autoDeletedTime", &hasProperty));
743     if (hasProperty) {
744         napi_get_named_property(env, value, "autoDeletedTime", &result);
745         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
746         if (valuetype != napi_number) {
747             LBSLOGE(NAPI_UTILS, "Wrong argument type. Number expected.");
748             return nullptr;
749         }
750         napi_get_value_int64(env, result, &autoDeletedTime);
751         request.SetAutoDeletedTime(autoDeletedTime);
752     }
753 
754     return NapiGetNull(env);
755 }
756 
GetNotificationClassification(const napi_env & env,const napi_value & value,NotificationRequest & request)757 napi_value NotificationNapi::GetNotificationClassification(
758     const napi_env &env, const napi_value &value, NotificationRequest &request)
759 {
760     LBSLOGD(NAPI_UTILS, "enter");
761 
762     napi_valuetype valuetype = napi_undefined;
763     napi_value result = nullptr;
764     bool hasProperty = false;
765     size_t strLen = 0;
766 
767     NAPI_CALL(env, napi_has_named_property(env, value, "classification", &hasProperty));
768     if (hasProperty) {
769         napi_get_named_property(env, value, "classification", &result);
770         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
771         if (valuetype != napi_string) {
772             LBSLOGE(NAPI_UTILS, "Wrong argument type. String expected.");
773             return nullptr;
774         }
775         char str[STR_MAX_SIZE] = {0};
776         NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
777         request.SetClassification(str);
778     }
779 
780     return NapiGetNull(env);
781 }
782 
GetNotificationAppMessageId(const napi_env & env,const napi_value & value,NotificationRequest & request)783 napi_value NotificationNapi::GetNotificationAppMessageId(
784     const napi_env &env, const napi_value &value, NotificationRequest &request)
785 {
786     bool hasProperty = false;
787     NAPI_CALL(env, napi_has_named_property(env, value, "appMessageId", &hasProperty));
788     if (!hasProperty) {
789         return NapiGetNull(env);
790     }
791 
792     auto appMessageIdValue = AppExecFwk::GetPropertyValueByPropertyName(env, value, "appMessageId", napi_string);
793     if (appMessageIdValue == nullptr) {
794         LBSLOGE(NAPI_UTILS, "Wrong argument type. String expected.");
795         return nullptr;
796     }
797 
798     std::string appMessageId = AppExecFwk::UnwrapStringFromJS(env, appMessageIdValue);
799     request.SetAppMessageId(appMessageId);
800     return NapiGetNull(env);
801 }
802 
GetNotificationColor(const napi_env & env,const napi_value & value,NotificationRequest & request)803 napi_value NotificationNapi::GetNotificationColor(
804     const napi_env &env, const napi_value &value, NotificationRequest &request)
805 {
806     LBSLOGD(NAPI_UTILS, "enter");
807 
808     napi_valuetype valuetype = napi_undefined;
809     napi_value result = nullptr;
810     bool hasProperty = false;
811     int32_t color = 0;
812 
813     NAPI_CALL(env, napi_has_named_property(env, value, "color", &hasProperty));
814     if (hasProperty) {
815         napi_get_named_property(env, value, "color", &result);
816         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
817         if (valuetype != napi_number) {
818             LBSLOGE(NAPI_UTILS, "Wrong argument type. Number expected.");
819             return nullptr;
820         }
821         napi_get_value_int32(env, result, &color);
822         if (color < 0) {
823             LBSLOGE(NAPI_UTILS, "Wrong argument type. Natural number expected.");
824             return nullptr;
825         }
826         request.SetColor(color);
827     }
828 
829     return NapiGetNull(env);
830 }
831 
GetNotificationColorEnabled(const napi_env & env,const napi_value & value,NotificationRequest & request)832 napi_value NotificationNapi::GetNotificationColorEnabled(
833     const napi_env &env, const napi_value &value, NotificationRequest &request)
834 {
835     LBSLOGD(NAPI_UTILS, "enter");
836 
837     napi_valuetype valuetype = napi_undefined;
838     napi_value result = nullptr;
839     bool hasProperty = false;
840     bool colorEnabled = false;
841 
842     NAPI_CALL(env, napi_has_named_property(env, value, "colorEnabled", &hasProperty));
843     if (hasProperty) {
844         napi_get_named_property(env, value, "colorEnabled", &result);
845         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
846         if (valuetype != napi_boolean) {
847             LBSLOGE(NAPI_UTILS, "Wrong argument type. Bool expected.");
848             return nullptr;
849         }
850         napi_get_value_bool(env, result, &colorEnabled);
851         request.SetColorEnabled(colorEnabled);
852     }
853 
854     return NapiGetNull(env);
855 }
856 
GetNotificationIsAlertOnce(const napi_env & env,const napi_value & value,NotificationRequest & request)857 napi_value NotificationNapi::GetNotificationIsAlertOnce(
858     const napi_env &env, const napi_value &value, NotificationRequest &request)
859 {
860     LBSLOGD(NAPI_UTILS, "enter");
861 
862     napi_valuetype valuetype = napi_undefined;
863     napi_value result = nullptr;
864     bool hasProperty = false;
865     bool isAlertOnce = false;
866 
867     NAPI_CALL(env, napi_has_named_property(env, value, "isAlertOnce", &hasProperty));
868     if (hasProperty) {
869         napi_get_named_property(env, value, "isAlertOnce", &result);
870         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
871         if (valuetype != napi_boolean) {
872             LBSLOGE(NAPI_UTILS, "Wrong argument type. Bool expected.");
873             return nullptr;
874         }
875         napi_get_value_bool(env, result, &isAlertOnce);
876         request.SetAlertOneTime(isAlertOnce);
877     }
878 
879     return NapiGetNull(env);
880 }
881 
GetNotificationIsStopwatch(const napi_env & env,const napi_value & value,NotificationRequest & request)882 napi_value NotificationNapi::GetNotificationIsStopwatch(
883     const napi_env &env, const napi_value &value, NotificationRequest &request)
884 {
885     LBSLOGD(NAPI_UTILS, "enter");
886 
887     napi_valuetype valuetype = napi_undefined;
888     napi_value result = nullptr;
889     bool hasProperty = false;
890     bool isStopwatch = false;
891 
892     NAPI_CALL(env, napi_has_named_property(env, value, "isStopwatch", &hasProperty));
893     if (hasProperty) {
894         napi_get_named_property(env, value, "isStopwatch", &result);
895         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
896         if (valuetype != napi_boolean) {
897             LBSLOGE(NAPI_UTILS, "Wrong argument type. Bool expected.");
898             return nullptr;
899         }
900         napi_get_value_bool(env, result, &isStopwatch);
901         request.SetShowStopwatch(isStopwatch);
902     }
903 
904     return NapiGetNull(env);
905 }
906 
GetNotificationIsCountDown(const napi_env & env,const napi_value & value,NotificationRequest & request)907 napi_value NotificationNapi::GetNotificationIsCountDown(
908     const napi_env &env, const napi_value &value, NotificationRequest &request)
909 {
910     LBSLOGD(NAPI_UTILS, "enter");
911 
912     napi_valuetype valuetype = napi_undefined;
913     napi_value result = nullptr;
914     bool hasProperty = false;
915     bool isCountDown = false;
916 
917     NAPI_CALL(env, napi_has_named_property(env, value, "isCountDown", &hasProperty));
918     if (hasProperty) {
919         napi_get_named_property(env, value, "isCountDown", &result);
920         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
921         if (valuetype != napi_boolean) {
922             LBSLOGE(NAPI_UTILS, "Wrong argument type. Bool expected.");
923             return nullptr;
924         }
925         napi_get_value_bool(env, result, &isCountDown);
926         request.SetCountdownTimer(isCountDown);
927     }
928 
929     return NapiGetNull(env);
930 }
931 
GetNotificationStatusBarText(const napi_env & env,const napi_value & value,NotificationRequest & request)932 napi_value NotificationNapi::GetNotificationStatusBarText(
933     const napi_env &env, const napi_value &value, NotificationRequest &request)
934 {
935     LBSLOGD(NAPI_UTILS, "enter");
936 
937     napi_valuetype valuetype = napi_undefined;
938     napi_value result = nullptr;
939     bool hasProperty = false;
940     size_t strLen = 0;
941 
942     NAPI_CALL(env, napi_has_named_property(env, value, "statusBarText", &hasProperty));
943     if (hasProperty) {
944         napi_get_named_property(env, value, "statusBarText", &result);
945         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
946         if (valuetype != napi_string) {
947             LBSLOGE(NAPI_UTILS, "Wrong argument type. String expected.");
948             return nullptr;
949         }
950         char str[STR_MAX_SIZE] = {0};
951         NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
952         request.SetStatusBarText(str);
953     }
954 
955     return NapiGetNull(env);
956 }
957 
GetNotificationLabel(const napi_env & env,const napi_value & value,NotificationRequest & request)958 napi_value NotificationNapi::GetNotificationLabel(
959     const napi_env &env, const napi_value &value, NotificationRequest &request)
960 {
961     LBSLOGD(NAPI_UTILS, "enter");
962 
963     napi_valuetype valuetype = napi_undefined;
964     napi_value result = nullptr;
965     bool hasProperty = false;
966     size_t strLen = 0;
967 
968     NAPI_CALL(env, napi_has_named_property(env, value, "label", &hasProperty));
969     if (hasProperty) {
970         napi_get_named_property(env, value, "label", &result);
971         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
972         if (valuetype != napi_string) {
973             LBSLOGE(NAPI_UTILS, "Wrong argument type. String expected.");
974             return nullptr;
975         }
976         char str[STR_MAX_SIZE] = {0};
977         NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
978         request.SetLabel(str);
979     }
980 
981     return NapiGetNull(env);
982 }
983 
GetNotificationBadgeIconStyle(const napi_env & env,const napi_value & value,NotificationRequest & request)984 napi_value NotificationNapi::GetNotificationBadgeIconStyle(
985     const napi_env &env, const napi_value &value, NotificationRequest &request)
986 {
987     LBSLOGD(NAPI_UTILS, "enter");
988 
989     napi_valuetype valuetype = napi_undefined;
990     napi_value result = nullptr;
991     bool hasProperty = false;
992     int32_t badgeIconStyle = 0;
993 
994     NAPI_CALL(env, napi_has_named_property(env, value, "badgeIconStyle", &hasProperty));
995     if (hasProperty) {
996         napi_get_named_property(env, value, "badgeIconStyle", &result);
997         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
998         if (valuetype != napi_number) {
999             LBSLOGE(NAPI_UTILS, "Wrong argument type. Number expected.");
1000             return nullptr;
1001         }
1002         napi_get_value_int32(env, result, &badgeIconStyle);
1003         request.SetBadgeIconStyle(static_cast<NotificationRequest::BadgeStyle>(badgeIconStyle));
1004     }
1005 
1006     return NapiGetNull(env);
1007 }
1008 
GetNotificationShowDeliveryTime(const napi_env & env,const napi_value & value,NotificationRequest & request)1009 napi_value NotificationNapi::GetNotificationShowDeliveryTime(
1010     const napi_env &env, const napi_value &value, NotificationRequest &request)
1011 {
1012     LBSLOGD(NAPI_UTILS, "enter");
1013 
1014     napi_valuetype valuetype = napi_undefined;
1015     napi_value result = nullptr;
1016     bool hasProperty = false;
1017     bool showDeliveryTime = false;
1018 
1019     NAPI_CALL(env, napi_has_named_property(env, value, "showDeliveryTime", &hasProperty));
1020     if (hasProperty) {
1021         napi_get_named_property(env, value, "showDeliveryTime", &result);
1022         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1023         if (valuetype != napi_boolean) {
1024             LBSLOGE(NAPI_UTILS, "Wrong argument type. Bool expected.");
1025             return nullptr;
1026         }
1027         napi_get_value_bool(env, result, &showDeliveryTime);
1028         request.SetShowDeliveryTime(showDeliveryTime);
1029     }
1030 
1031     return NapiGetNull(env);
1032 }
1033 
GetNotificationIsRemoveAllowed(const napi_env & env,const napi_value & value,NotificationRequest & request)1034 napi_value NotificationNapi::GetNotificationIsRemoveAllowed(
1035     const napi_env &env, const napi_value &value, NotificationRequest &request)
1036 {
1037     LBSLOGD(NAPI_UTILS, "enter");
1038 
1039     napi_valuetype valuetype = napi_undefined;
1040     napi_value result = nullptr;
1041     bool hasProperty = false;
1042     bool isRemoveAllowed = true;
1043 
1044     NAPI_CALL(env, napi_has_named_property(env, value, "isRemoveAllowed", &hasProperty));
1045     if (hasProperty) {
1046         napi_get_named_property(env, value, "isRemoveAllowed", &result);
1047         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1048         if (valuetype != napi_boolean) {
1049             LBSLOGE(NAPI_UTILS, "Wrong argument type. Bool expected.");
1050             return nullptr;
1051         }
1052         napi_get_value_bool(env, result, &isRemoveAllowed);
1053         request.SetRemoveAllowed(isRemoveAllowed);
1054     }
1055 
1056     return NapiGetNull(env);
1057 }
1058 
GetNotificationActionButtons(const napi_env & env,const napi_value & value,NotificationRequest & request)1059 napi_value NotificationNapi::GetNotificationActionButtons(
1060     const napi_env &env, const napi_value &value, NotificationRequest &request)
1061 {
1062     LBSLOGD(NAPI_UTILS, "enter");
1063 
1064     bool isArray = false;
1065     napi_valuetype valuetype = napi_undefined;
1066     napi_value actionButtons = nullptr;
1067     uint32_t length = 0;
1068     bool hasProperty = false;
1069 
1070     napi_has_named_property(env, value, "actionButtons", &hasProperty);
1071     if (!hasProperty) {
1072         return NotificationNapi::NapiGetNull(env);
1073     }
1074 
1075     request.SetIsCoverActionButtons(true);
1076     napi_get_named_property(env, value, "actionButtons", &actionButtons);
1077     napi_is_array(env, actionButtons, &isArray);
1078     if (!isArray) {
1079         LBSLOGE(NAPI_UTILS, "Property actionButtons is expected to be an array.");
1080         return nullptr;
1081     }
1082     napi_get_array_length(env, actionButtons, &length);
1083     if (length == 0) {
1084         LBSLOGI(NAPI_UTILS, "The array is empty.");
1085         return NotificationNapi::NapiGetNull(env);
1086     }
1087     for (size_t i = 0; i < length; i++) {
1088         napi_value actionButton = nullptr;
1089         napi_get_element(env, actionButtons, i, &actionButton);
1090         NAPI_CALL(env, napi_typeof(env, actionButton, &valuetype));
1091         if (valuetype != napi_object) {
1092             LBSLOGE(NAPI_UTILS, "Wrong argument type. Object expected.");
1093             return nullptr;
1094         }
1095 
1096         std::shared_ptr<NotificationActionButton> pActionButton = nullptr;
1097         if (GetNotificationActionButtonsDetailed(env, actionButton, pActionButton) == nullptr) {
1098             return nullptr;
1099         }
1100         request.AddActionButton(pActionButton);
1101     }
1102 
1103     return NapiGetNull(env);
1104 }
1105 
GetNotificationActionButtonsDetailed(const napi_env & env,const napi_value & actionButton,std::shared_ptr<NotificationActionButton> & pActionButton)1106 napi_value NotificationNapi::GetNotificationActionButtonsDetailed(
1107     const napi_env &env, const napi_value &actionButton, std::shared_ptr<NotificationActionButton> &pActionButton)
1108 {
1109     LBSLOGD(NAPI_UTILS, "enter");
1110 
1111     if (!GetNotificationActionButtonsDetailedBasicInfo(env, actionButton, pActionButton)) {
1112         return nullptr;
1113     }
1114     if (!GetNotificationActionButtonsDetailedByExtras(env, actionButton, pActionButton)) {
1115         return nullptr;
1116     }
1117     if (!GetNotificationUserInput(env, actionButton, pActionButton)) {
1118         return nullptr;
1119     }
1120     return NapiGetNull(env);
1121 }
1122 
GetNotificationActionButtonsDetailedBasicInfo(const napi_env & env,const napi_value & actionButton,std::shared_ptr<NotificationActionButton> & pActionButton)1123 napi_value NotificationNapi::GetNotificationActionButtonsDetailedBasicInfo(
1124     const napi_env &env, const napi_value &actionButton, std::shared_ptr<NotificationActionButton> &pActionButton)
1125 {
1126     LBSLOGD(NAPI_UTILS, "enter");
1127     napi_valuetype valuetype = napi_undefined;
1128     bool hasProperty = false;
1129     char str[STR_MAX_SIZE] = {0};
1130     size_t strLen = 0;
1131     napi_value value = nullptr;
1132     std::string title;
1133     AbilityRuntime::WantAgent::WantAgent *wantAgentPtr = nullptr;
1134     std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
1135     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> wantAgent;
1136 
1137     // title: string
1138     NAPI_CALL(env, napi_has_named_property(env, actionButton, "title", &hasProperty));
1139     if (!hasProperty) {
1140         LBSLOGE(NAPI_UTILS, "Property title expected.");
1141         return nullptr;
1142     }
1143     napi_get_named_property(env, actionButton, "title", &value);
1144     NAPI_CALL(env, napi_typeof(env, value, &valuetype));
1145     if (valuetype != napi_string) {
1146         LBSLOGE(NAPI_UTILS, "Wrong argument type. String expected.");
1147         return nullptr;
1148     }
1149     NAPI_CALL(env, napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen));
1150     title = str;
1151 
1152     // wantAgent: WantAgent
1153     NAPI_CALL(env, napi_has_named_property(env, actionButton, "wantAgent", &hasProperty));
1154     if (!hasProperty) {
1155         LBSLOGE(NAPI_UTILS, "Property wantAgent expected.");
1156         return nullptr;
1157     }
1158     napi_get_named_property(env, actionButton, "wantAgent", &value);
1159     NAPI_CALL(env, napi_typeof(env, value, &valuetype));
1160     if (valuetype != napi_object) {
1161         LBSLOGE(NAPI_UTILS, "Wrong argument type. Object expected.");
1162         return nullptr;
1163     }
1164     napi_unwrap(env, value, (void **)&wantAgentPtr);
1165     if (wantAgentPtr == nullptr) {
1166         LBSLOGE(NAPI_UTILS, "Invalid object wantAgent");
1167         return nullptr;
1168     }
1169     wantAgent = std::make_shared<AbilityRuntime::WantAgent::WantAgent>(*wantAgentPtr);
1170 
1171     // icon?: image.PixelMap
1172     NAPI_CALL(env, napi_has_named_property(env, actionButton, "icon", &hasProperty));
1173     if (hasProperty) {
1174         napi_get_named_property(env, actionButton, "icon", &value);
1175         NAPI_CALL(env, napi_typeof(env, value, &valuetype));
1176         if (valuetype != napi_object) {
1177             LBSLOGE(NAPI_UTILS, "Wrong argument type. Object expected.");
1178             return nullptr;
1179         }
1180         pixelMap = Media::PixelMapNapi::GetPixelMap(env, value);
1181         if (pixelMap == nullptr) {
1182             LBSLOGE(NAPI_UTILS, "Invalid object pixelMap");
1183             return nullptr;
1184         }
1185     }
1186     pActionButton = NotificationActionButton::Create(pixelMap, title, wantAgent);
1187 
1188     return NapiGetNull(env);
1189 }
1190 
GetNotificationActionButtonsDetailedByExtras(const napi_env & env,const napi_value & actionButton,std::shared_ptr<NotificationActionButton> & pActionButton)1191 napi_value NotificationNapi::GetNotificationActionButtonsDetailedByExtras(
1192     const napi_env &env, const napi_value &actionButton, std::shared_ptr<NotificationActionButton> &pActionButton)
1193 {
1194     LBSLOGD(NAPI_UTILS, "enter");
1195 
1196     napi_valuetype valuetype = napi_undefined;
1197     napi_value result = nullptr;
1198     bool hasProperty = false;
1199 
1200     if (!pActionButton) {
1201         LBSLOGE(NAPI_UTILS, "pActionButton is nullptr");
1202         return nullptr;
1203     }
1204 
1205     // extras?: {[key: string]: any}
1206     NAPI_CALL(env, napi_has_named_property(env, actionButton, "extras", &hasProperty));
1207     if (hasProperty) {
1208         napi_get_named_property(env, actionButton, "extras", &result);
1209         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1210         if (valuetype != napi_object) {
1211             LBSLOGE(NAPI_UTILS, "Wrong argument type. Object expected.");
1212             return nullptr;
1213         }
1214         AAFwk::WantParams wantParams;
1215         if (!OHOS::AppExecFwk::UnwrapWantParams(env, result, wantParams)) {
1216             return nullptr;
1217         }
1218         pActionButton->AddAdditionalData(wantParams);
1219     }
1220     return NapiGetNull(env);
1221 }
1222 
GetNotificationBadgeNumber(const napi_env & env,const napi_value & value,NotificationRequest & request)1223 napi_value NotificationNapi::GetNotificationBadgeNumber(
1224     const napi_env &env, const napi_value &value, NotificationRequest &request)
1225 {
1226     LBSLOGD(NAPI_UTILS, "enter");
1227 
1228     napi_valuetype valuetype = napi_undefined;
1229     napi_value result = nullptr;
1230     bool hasProperty = false;
1231     int32_t badgeNumber = 0;
1232 
1233     NAPI_CALL(env, napi_has_named_property(env, value, "badgeNumber", &hasProperty));
1234     if (hasProperty) {
1235         napi_get_named_property(env, value, "badgeNumber", &result);
1236         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1237         if (valuetype != napi_number) {
1238             LBSLOGE(NAPI_UTILS, "Wrong argument type. Number expected.");
1239             return nullptr;
1240         }
1241 
1242         napi_get_value_int32(env, result, &badgeNumber);
1243         if (badgeNumber < 0) {
1244             LBSLOGE(NAPI_UTILS, "Wrong badge number.");
1245             return nullptr;
1246         }
1247 
1248         request.SetBadgeNumber(badgeNumber);
1249     }
1250 
1251     return NapiGetNull(env);
1252 }
1253 
GetNotificationUnifiedGroupInfo(const napi_env & env,const napi_value & value,NotificationRequest & request)1254 napi_value NotificationNapi::GetNotificationUnifiedGroupInfo(
1255     const napi_env &env, const napi_value &value, NotificationRequest &request)
1256 {
1257     bool hasProperty = false;
1258     NAPI_CALL(env, napi_has_named_property(env, value, "unifiedGroupInfo", &hasProperty));
1259     if (!hasProperty) {
1260         return NapiGetNull(env);
1261     }
1262 
1263     auto info = AppExecFwk::GetPropertyValueByPropertyName(env, value, "unifiedGroupInfo", napi_object);
1264     if (info == nullptr) {
1265         LBSLOGE(NAPI_UTILS, "Wrong argument type. object expected.");
1266         return nullptr;
1267     }
1268     std::shared_ptr<NotificationUnifiedGroupInfo> unifiedGroupInfo = std::make_shared<NotificationUnifiedGroupInfo>();
1269     // key?: string
1270     auto jsValue = AppExecFwk::GetPropertyValueByPropertyName(env, info, "key", napi_string);
1271     if (jsValue != nullptr) {
1272         std::string key = AppExecFwk::UnwrapStringFromJS(env, jsValue);
1273         unifiedGroupInfo->SetKey(key);
1274     }
1275 
1276     // title?: string
1277     jsValue = AppExecFwk::GetPropertyValueByPropertyName(env, info, "title", napi_string);
1278     if (jsValue != nullptr) {
1279         std::string title = AppExecFwk::UnwrapStringFromJS(env, jsValue);
1280         unifiedGroupInfo->SetTitle(title);
1281     }
1282 
1283     // content?: string
1284     jsValue = AppExecFwk::GetPropertyValueByPropertyName(env, info, "content", napi_string);
1285     if (jsValue != nullptr) {
1286         std::string content = AppExecFwk::UnwrapStringFromJS(env, jsValue);
1287         unifiedGroupInfo->SetContent(content);
1288     }
1289 
1290     // sceneName?: string
1291     jsValue = AppExecFwk::GetPropertyValueByPropertyName(env, info, "sceneName", napi_string);
1292     if (jsValue != nullptr) {
1293         std::string sceneName = AppExecFwk::UnwrapStringFromJS(env, jsValue);
1294         unifiedGroupInfo->SetSceneName(sceneName);
1295     }
1296 
1297     // extraInfo?: {[key:string] : any}
1298     jsValue = AppExecFwk::GetPropertyValueByPropertyName(env, info, "extraInfo", napi_object);
1299     if (jsValue != nullptr) {
1300         std::shared_ptr<AAFwk::WantParams> extras = std::make_shared<AAFwk::WantParams>();
1301         if (!OHOS::AppExecFwk::UnwrapWantParams(env, jsValue, *extras)) {
1302             return nullptr;
1303         }
1304         unifiedGroupInfo->SetExtraInfo(extras);
1305     }
1306 
1307     request.SetUnifiedGroupInfo(unifiedGroupInfo);
1308     return NapiGetNull(env);
1309 }
1310 
GetNotificationControlFlags(const napi_env & env,const napi_value & value,NotificationRequest & request)1311 napi_value NotificationNapi::GetNotificationControlFlags(
1312     const napi_env &env, const napi_value &value, NotificationRequest &request)
1313 {
1314     LBSLOGD(NAPI_UTILS, "Called.");
1315 
1316     napi_valuetype valuetype = napi_undefined;
1317     napi_value result = nullptr;
1318     bool hasProperty = false;
1319     uint32_t notificationControlFlags = 0;
1320 
1321     NAPI_CALL(env, napi_has_named_property(env, value, "notificationControlFlags", &hasProperty));
1322     if (hasProperty) {
1323         napi_get_named_property(env, value, "notificationControlFlags", &result);
1324         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1325         if (valuetype != napi_number) {
1326             LBSLOGE(NAPI_UTILS, "Wrong argument type. Number expected.");
1327             return nullptr;
1328         }
1329 
1330         napi_get_value_uint32(env, result, &notificationControlFlags);
1331         if (notificationControlFlags == 0) {
1332             LBSLOGD(NAPI_UTILS, "Undefined notification control flags.");
1333             return nullptr;
1334         }
1335 
1336         request.SetNotificationControlFlags(notificationControlFlags);
1337     }
1338 
1339     return NapiGetNull(env);
1340 }
1341 }
1342 }
1343