1 /*
2  * Copyright (c) 2023-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 "common.h"
17 #include "ans_inner_errors.h"
18 #include "ans_log_wrapper.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 NotificationNapi {
SetNotificationRequestByString(const napi_env & env,const OHOS::Notification::NotificationRequest * request,napi_value & result)33 napi_value Common::SetNotificationRequestByString(
34     const napi_env &env, const OHOS::Notification::NotificationRequest *request, napi_value &result)
35 {
36     ANS_LOGD("enter");
37 
38     napi_value value = nullptr;
39 
40     if (request == nullptr) {
41         ANS_LOGE("request is nullptr");
42         return NapiGetBoolean(env, false);
43     }
44 
45     // classification?: string
46     napi_create_string_utf8(env, request->GetClassification().c_str(), NAPI_AUTO_LENGTH, &value);
47     napi_set_named_property(env, result, "classification", value);
48 
49     // statusBarText?: string
50     napi_create_string_utf8(env, request->GetStatusBarText().c_str(), NAPI_AUTO_LENGTH, &value);
51     napi_set_named_property(env, result, "statusBarText", value);
52 
53     // label?: string
54     napi_create_string_utf8(env, request->GetLabel().c_str(), NAPI_AUTO_LENGTH, &value);
55     napi_set_named_property(env, result, "label", value);
56 
57     // groupName?: string
58     napi_create_string_utf8(env, request->GetGroupName().c_str(), NAPI_AUTO_LENGTH, &value);
59     napi_set_named_property(env, result, "groupName", value);
60 
61     // readonly creatorBundleName?: string
62     napi_create_string_utf8(env, request->GetCreatorBundleName().c_str(), NAPI_AUTO_LENGTH, &value);
63     napi_set_named_property(env, result, "creatorBundleName", value);
64 
65     // readonly sound?: string
66     napi_create_string_utf8(env, request->GetSound().c_str(), NAPI_AUTO_LENGTH, &value);
67     napi_set_named_property(env, result, "sound", value);
68 
69     return NapiGetBoolean(env, true);
70 }
71 
SetNotificationRequestByNumber(const napi_env & env,const OHOS::Notification::NotificationRequest * request,napi_value & result)72 napi_value Common::SetNotificationRequestByNumber(
73     const napi_env &env, const OHOS::Notification::NotificationRequest *request, napi_value &result)
74 {
75     ANS_LOGD("enter");
76 
77     napi_value value = nullptr;
78 
79     if (request == nullptr) {
80         ANS_LOGE("request is nullptr");
81         return NapiGetBoolean(env, false);
82     }
83 
84     // id?: number
85     napi_create_int32(env, request->GetNotificationId(), &value);
86     napi_set_named_property(env, result, "id", value);
87 
88     // slotType?: SlotType
89     SlotType outType = SlotType::UNKNOWN_TYPE;
90     if (!AnsEnumUtil::SlotTypeCToJS(request->GetSlotType(), outType)) {
91         return NapiGetBoolean(env, false);
92     }
93     napi_create_int32(env, static_cast<int32_t>(outType), &value);
94     napi_set_named_property(env, result, "slotType", value);
95     napi_set_named_property(env, result, "notificationSlotType", value);
96 
97     // deliveryTime?: number
98     napi_create_int64(env, request->GetDeliveryTime(), &value);
99     napi_set_named_property(env, result, "deliveryTime", value);
100 
101     // autoDeletedTime?: number
102     napi_create_int64(env, request->GetAutoDeletedTime(), &value);
103     napi_set_named_property(env, result, "autoDeletedTime", value);
104 
105     // color ?: number
106     napi_create_uint32(env, request->GetColor(), &value);
107     napi_set_named_property(env, result, "color", value);
108 
109     // badgeIconStyle ?: number
110     auto badgeIconStyle = static_cast<int32_t>(request->GetBadgeIconStyle());
111     napi_create_int32(env, badgeIconStyle, &value);
112     napi_set_named_property(env, result, "badgeIconStyle", value);
113 
114     // readonly creatorUid?: number
115     napi_create_int32(env, request->GetCreatorUid(), &value);
116     napi_set_named_property(env, result, "creatorUid", value);
117 
118     // readonly creatorPid?: number
119     napi_create_int32(env, request->GetCreatorPid(), &value);
120     napi_set_named_property(env, result, "creatorPid", value);
121 
122     // badgeNumber?: number
123     napi_create_uint32(env, request->GetBadgeNumber(), &value);
124     napi_set_named_property(env, result, "badgeNumber", value);
125 
126     // readonly creatorInstanceKey?: number
127     napi_create_int32(env, request->GetCreatorInstanceKey(), &value);
128     napi_set_named_property(env, result, "creatorInstanceKey", value);
129 
130     return NapiGetBoolean(env, true);
131 }
132 
SetNotificationRequestByBool(const napi_env & env,const OHOS::Notification::NotificationRequest * request,napi_value & result)133 napi_value Common::SetNotificationRequestByBool(
134     const napi_env &env, const OHOS::Notification::NotificationRequest *request, napi_value &result)
135 {
136     ANS_LOGD("enter");
137 
138     napi_value value = nullptr;
139 
140     if (request == nullptr) {
141         ANS_LOGE("request is nullptr");
142         return NapiGetBoolean(env, false);
143     }
144     // isOngoing?: boolean
145     napi_get_boolean(env, request->IsInProgress(), &value);
146     napi_set_named_property(env, result, "isOngoing", value);
147 
148     // isUnremovable?: boolean
149     napi_get_boolean(env, request->IsUnremovable(), &value);
150     napi_set_named_property(env, result, "isUnremovable", value);
151 
152     // tapDismissed?: boolean
153     napi_get_boolean(env, request->IsTapDismissed(), &value);
154     napi_set_named_property(env, result, "tapDismissed", value);
155 
156     // colorEnabled?: boolean
157     napi_get_boolean(env, request->IsColorEnabled(), &value);
158     napi_set_named_property(env, result, "colorEnabled", value);
159 
160     // isAlertOnce?: boolean
161     napi_get_boolean(env, request->IsAlertOneTime(), &value);
162     napi_set_named_property(env, result, "isAlertOnce", value);
163 
164     // isStopwatch?: boolean
165     napi_get_boolean(env, request->IsShowStopwatch(), &value);
166     napi_set_named_property(env, result, "isStopwatch", value);
167 
168     // isCountDown?: boolean
169     napi_get_boolean(env, request->IsCountdownTimer(), &value);
170     napi_set_named_property(env, result, "isCountDown", value);
171 
172     // isFloatingIcon?: boolean
173     napi_get_boolean(env, request->IsFloatingIcon(), &value);
174     napi_set_named_property(env, result, "isFloatingIcon", value);
175 
176     // showDeliveryTime?: boolean
177     napi_get_boolean(env, request->IsShowDeliveryTime(), &value);
178     napi_set_named_property(env, result, "showDeliveryTime", value);
179 
180     return NapiGetBoolean(env, true);
181 }
182 
SetNotificationRequestByWantAgent(const napi_env & env,const OHOS::Notification::NotificationRequest * request,napi_value & result)183 napi_value Common::SetNotificationRequestByWantAgent(
184     const napi_env &env, const OHOS::Notification::NotificationRequest *request, napi_value &result)
185 {
186     ANS_LOGD("enter");
187     if (request == nullptr) {
188         ANS_LOGE("request is nullptr");
189         return NapiGetBoolean(env, false);
190     }
191     // wantAgent?: WantAgent
192     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> agent = request->GetWantAgent();
193     if (agent) {
194         napi_value wantAgent = nullptr;
195         wantAgent = CreateWantAgentByJS(env, agent);
196         napi_set_named_property(env, result, "wantAgent", wantAgent);
197     } else {
198         napi_set_named_property(env, result, "wantAgent", NapiGetNull(env));
199     }
200 
201     // removalWantAgent?: WantAgent
202     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> removalAgent = request->GetRemovalWantAgent();
203     if (removalAgent) {
204         napi_value wantAgent = nullptr;
205         wantAgent = CreateWantAgentByJS(env, removalAgent);
206         napi_set_named_property(env, result, "removalWantAgent", wantAgent);
207     } else {
208         napi_set_named_property(env, result, "removalWantAgent", NapiGetNull(env));
209     }
210 
211     // maxScreenWantAgent?: WantAgent
212     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> maxScreenAgent = request->GetMaxScreenWantAgent();
213     if (maxScreenAgent) {
214         napi_value wantAgent = nullptr;
215         wantAgent = CreateWantAgentByJS(env, maxScreenAgent);
216         napi_set_named_property(env, result, "maxScreenWantAgent", wantAgent);
217     } else {
218         napi_set_named_property(env, result, "maxScreenWantAgent", NapiGetNull(env));
219     }
220 
221     return NapiGetBoolean(env, true);
222 }
223 
SetNotificationRequestByPixelMap(const napi_env & env,const OHOS::Notification::NotificationRequest * request,napi_value & result)224 napi_value Common::SetNotificationRequestByPixelMap(
225     const napi_env &env, const OHOS::Notification::NotificationRequest *request, napi_value &result)
226 {
227     ANS_LOGD("enter");
228 
229     if (request == nullptr) {
230         ANS_LOGE("request is nullptr");
231         return NapiGetBoolean(env, false);
232     }
233 
234     // smallIcon?: image.PixelMap
235     std::shared_ptr<Media::PixelMap> littleIcon = request->GetLittleIcon();
236     if (littleIcon) {
237         napi_value smallIconResult = nullptr;
238         napi_valuetype valuetype = napi_undefined;
239         smallIconResult = Media::PixelMapNapi::CreatePixelMap(env, littleIcon);
240         NAPI_CALL(env, napi_typeof(env, smallIconResult, &valuetype));
241         if (valuetype == napi_undefined) {
242             ANS_LOGI("smallIconResult is undefined");
243             napi_set_named_property(env, result, "smallIcon", NapiGetNull(env));
244         } else {
245             napi_set_named_property(env, result, "smallIcon", smallIconResult);
246         }
247     }
248 
249     // largeIcon?: image.PixelMap
250     std::shared_ptr<Media::PixelMap> largeIcon = request->GetBigIcon();
251     if (largeIcon) {
252         napi_value largeIconResult = nullptr;
253         napi_valuetype valuetype = napi_undefined;
254         largeIconResult = Media::PixelMapNapi::CreatePixelMap(env, largeIcon);
255         NAPI_CALL(env, napi_typeof(env, largeIconResult, &valuetype));
256         if (valuetype == napi_undefined) {
257             ANS_LOGI("largeIconResult is undefined");
258             napi_set_named_property(env, result, "largeIcon", NapiGetNull(env));
259         } else {
260             napi_set_named_property(env, result, "largeIcon", largeIconResult);
261         }
262     }
263 
264     // overlayIcon?: image.PixelMap
265     std::shared_ptr<Media::PixelMap> overlayIcon = request->GetOverlayIcon();
266     if (overlayIcon) {
267         napi_value overlayIconResult = nullptr;
268         napi_valuetype valuetype = napi_undefined;
269         overlayIconResult = Media::PixelMapNapi::CreatePixelMap(env, overlayIcon);
270         NAPI_CALL(env, napi_typeof(env, overlayIconResult, &valuetype));
271         if (valuetype == napi_undefined) {
272             ANS_LOGI("overlayIconResult is undefined");
273             napi_set_named_property(env, result, "overlayIcon", NapiGetNull(env));
274         } else {
275             napi_set_named_property(env, result, "overlayIcon", overlayIconResult);
276         }
277     }
278 
279     return NapiGetBoolean(env, true);
280 }
281 
SetNotificationRequestByCustom(const napi_env & env,const OHOS::Notification::NotificationRequest * request,napi_value & result)282 napi_value Common::SetNotificationRequestByCustom(
283     const napi_env &env, const OHOS::Notification::NotificationRequest *request, napi_value &result)
284 {
285     ANS_LOGD("enter");
286 
287     if (request == nullptr) {
288         ANS_LOGE("request is nullptr");
289         return NapiGetBoolean(env, false);
290     }
291 
292     // content: NotificationContent
293     std::shared_ptr<NotificationContent> content = request->GetContent();
294     if (content) {
295         napi_value contentResult = nullptr;
296         napi_create_object(env, &contentResult);
297         if (!SetNotificationContent(env, content, contentResult)) {
298             ANS_LOGE("SetNotificationContent call failed");
299             return NapiGetBoolean(env, false);
300         }
301         napi_set_named_property(env, result, "content", contentResult);
302     } else {
303         ANS_LOGE("content is nullptr");
304         return NapiGetBoolean(env, false);
305     }
306 
307     // extraInfo?: {[key:string] : any}
308     std::shared_ptr<AAFwk::WantParams> additionalData = request->GetAdditionalData();
309     if (additionalData) {
310         napi_value extraInfo = nullptr;
311         extraInfo = OHOS::AppExecFwk::WrapWantParams(env, *additionalData);
312         napi_set_named_property(env, result, "extraInfo", extraInfo);
313     }
314 
315     // actionButtons?: Array<NotificationActionButton>
316     napi_value arr = nullptr;
317     uint32_t count = 0;
318     napi_create_array(env, &arr);
319     for (auto vec : request->GetActionButtons()) {
320         if (vec) {
321             napi_value actionButtonResult = nullptr;
322             napi_create_object(env, &actionButtonResult);
323             if (SetNotificationActionButton(env, vec, actionButtonResult)) {
324                 napi_set_element(env, arr, count, actionButtonResult);
325                 count++;
326             }
327         }
328     }
329     if (count != 0) {
330         napi_set_named_property(env, result, "actionButtons", arr);
331     }
332 
333     // template?: NotificationTemplate
334     std::shared_ptr<NotificationTemplate> templ = request->GetTemplate();
335     if (templ) {
336         napi_value templateResult = nullptr;
337         napi_create_object(env, &templateResult);
338         if (!SetNotificationTemplateInfo(env, templ, templateResult)) {
339             ANS_LOGE("SetNotificationTemplate call failed");
340             return NapiGetBoolean(env, false);
341         }
342         napi_set_named_property(env, result, "template", templateResult);
343     }
344 
345     // readonly notificationFlags?: NotificationFlags
346     std::shared_ptr<NotificationFlags> flags = request->GetFlags();
347     if (flags) {
348         napi_value flagsResult = nullptr;
349         napi_create_object(env, &flagsResult);
350         if (!SetNotificationFlags(env, flags, flagsResult)) {
351             ANS_LOGE("SetNotificationFlags call failed");
352             return NapiGetBoolean(env, false);
353         }
354         napi_set_named_property(env, result, "notificationFlags", flagsResult);
355     }
356 
357     // readonly agentBundle?: agentBundle
358     std::shared_ptr<NotificationBundleOption> agentBundle = request->GetAgentBundle();
359     if (agentBundle) {
360         napi_value agentBundleResult = nullptr;
361         napi_create_object(env, &agentBundleResult);
362         if (!SetAgentBundle(env, agentBundle, agentBundleResult)) {
363             ANS_LOGE("SetAgentBundle call failed");
364             return NapiGetBoolean(env, false);
365         }
366         napi_set_named_property(env, result, "agentBundle", agentBundleResult);
367     }
368 
369     // unifiedGroupInfo?: unifiedGroupInfo
370     std::shared_ptr<NotificationUnifiedGroupInfo> groupInfo = request->GetUnifiedGroupInfo();
371     if (groupInfo) {
372         napi_value groupInfoResult = nullptr;
373         napi_create_object(env, &groupInfoResult);
374         if (!SetNotificationUnifiedGroupInfo(env, groupInfo, groupInfoResult)) {
375             ANS_LOGE("SetNotificationUnifiedGroupInfo call failed");
376             return NapiGetBoolean(env, false);
377         }
378         napi_set_named_property(env, result, "unifiedGroupInfo", groupInfoResult);
379     }
380 
381     return NapiGetBoolean(env, true);
382 }
383 
SetNotificationActionButton(const napi_env & env,const std::shared_ptr<NotificationActionButton> & actionButton,napi_value & result)384 napi_value Common::SetNotificationActionButton(
385     const napi_env &env, const std::shared_ptr<NotificationActionButton> &actionButton, napi_value &result)
386 {
387     ANS_LOGD("enter");
388     if (actionButton == nullptr) {
389         ANS_LOGE("actionButton is null");
390         return NapiGetBoolean(env, false);
391     }
392 
393     napi_value value = nullptr;
394 
395     // title: string
396     napi_create_string_utf8(env, actionButton->GetTitle().c_str(), NAPI_AUTO_LENGTH, &value);
397     napi_set_named_property(env, result, "title", value);
398 
399     // wantAgent: WantAgent
400     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> agent = actionButton->GetWantAgent();
401     if (agent == nullptr) {
402         ANS_LOGI("wantAgent is null");
403         napi_set_named_property(env, result, "wantAgent", NapiGetNull(env));
404         return NapiGetBoolean(env, false);
405     }
406     napi_value wantAgent = nullptr;
407     wantAgent = CreateWantAgentByJS(env, agent);
408     napi_set_named_property(env, result, "wantAgent", wantAgent);
409 
410     // icon?: image.PixelMap
411     std::shared_ptr<Media::PixelMap> icon = actionButton->GetIcon();
412     if (icon) {
413         napi_value iconResult = nullptr;
414         napi_valuetype valuetype = napi_undefined;
415         iconResult = Media::PixelMapNapi::CreatePixelMap(env, icon);
416         NAPI_CALL(env, napi_typeof(env, iconResult, &valuetype));
417         if (valuetype == napi_undefined) {
418             ANS_LOGW("icon result is undefined");
419             napi_set_named_property(env, result, "icon", NapiGetNull(env));
420         } else {
421             napi_set_named_property(env, result, "icon", iconResult);
422         }
423     }
424 
425     if (!SetNotificationActionButtonByExtras(env, actionButton, result)) {
426         return NapiGetBoolean(env, false);
427     }
428 
429     // userInput?: NotificationUserInput
430     napi_value userInputResult = nullptr;
431     napi_create_object(env, &userInputResult);
432     if (!SetNotificationActionButtonByUserInput(env, actionButton->GetUserInput(), userInputResult)) {
433         return NapiGetBoolean(env, false);
434     }
435     napi_set_named_property(env, result, "userInput", userInputResult);
436 
437     return NapiGetBoolean(env, true);
438 }
439 
SetNotificationActionButtonByExtras(const napi_env & env,const std::shared_ptr<NotificationActionButton> & actionButton,napi_value & result)440 napi_value Common::SetNotificationActionButtonByExtras(
441     const napi_env &env, const std::shared_ptr<NotificationActionButton> &actionButton, napi_value &result)
442 {
443     ANS_LOGD("enter");
444     if (!actionButton) {
445         ANS_LOGE("actionButton is null");
446         return NapiGetBoolean(env, false);
447     }
448     // extras?: {[key: string]: any}
449     auto extras = actionButton->GetAdditionalData();
450     if (extras) {
451         napi_value nExtras = nullptr;
452         nExtras = OHOS::AppExecFwk::WrapWantParams(env, *extras);
453         napi_set_named_property(env, result, "extras", nExtras);
454     }
455     return NapiGetBoolean(env, true);
456 }
457 
SetNotificationActionButtonByUserInput(const napi_env & env,const std::shared_ptr<NotificationUserInput> & userInput,napi_value & result)458 napi_value Common::SetNotificationActionButtonByUserInput(
459     const napi_env &env, const std::shared_ptr<NotificationUserInput> &userInput, napi_value &result)
460 {
461     ANS_LOGD("enter");
462 
463     if (!userInput) {
464         ANS_LOGE("userInput is null");
465         return NapiGetBoolean(env, false);
466     }
467 
468     napi_value value = nullptr;
469     napi_value arr = nullptr;
470     int count = 0;
471 
472     // inputKey: string
473     napi_create_string_utf8(env, userInput->GetInputKey().c_str(), NAPI_AUTO_LENGTH, &value);
474     napi_set_named_property(env, result, "inputKey", value);
475 
476     // tag: string
477     napi_create_string_utf8(env, userInput->GetTag().c_str(), NAPI_AUTO_LENGTH, &value);
478     napi_set_named_property(env, result, "tag", value);
479 
480     // options: Array<string>
481     napi_create_array(env, &arr);
482     for (auto vec : userInput->GetOptions()) {
483         napi_create_string_utf8(env, vec.c_str(), NAPI_AUTO_LENGTH, &value);
484         napi_set_element(env, arr, count, value);
485         count++;
486     }
487     if (count > 0) {
488         napi_set_named_property(env, result, "options", arr);
489     }
490 
491     // permitFreeFormInput?: boolean
492     napi_get_boolean(env, userInput->IsPermitFreeFormInput(), &value);
493     napi_set_named_property(env, result, "permitFreeFormInput", value);
494 
495     // permitMimeTypes?: Array<string>
496     count = 0;
497     napi_create_array(env, &arr);
498     for (auto vec : userInput->GetPermitMimeTypes()) {
499         napi_create_string_utf8(env, vec.c_str(), NAPI_AUTO_LENGTH, &value);
500         napi_set_element(env, arr, count, value);
501         count++;
502     }
503     if (count > 0) {
504         napi_set_named_property(env, result, "permitMimeTypes", arr);
505     }
506 
507     // editType?: number
508     napi_create_int64(env, userInput->GetEditType(), &value);
509     napi_set_named_property(env, result, "editType", value);
510 
511     // additionalData?: {[key: string]: Object}
512     auto additionalData = userInput->GetAdditionalData();
513     if (additionalData) {
514         napi_value nAdditionalData = nullptr;
515         nAdditionalData = OHOS::AppExecFwk::WrapWantParams(env, *additionalData);
516         napi_set_named_property(env, result, "additionalData", nAdditionalData);
517     }
518 
519     return NapiGetBoolean(env, true);
520 }
521 
SetNotificationRequest(const napi_env & env,const OHOS::Notification::NotificationRequest * request,napi_value & result)522 napi_value Common::SetNotificationRequest(
523     const napi_env &env, const OHOS::Notification::NotificationRequest *request, napi_value &result)
524 {
525     ANS_LOGD("enter");
526 
527     if (request == nullptr) {
528         ANS_LOGE("request is nullptr");
529         return NapiGetBoolean(env, false);
530     }
531 
532     if (!SetNotificationRequestByString(env, request, result)) {
533         return NapiGetBoolean(env, false);
534     }
535     if (!SetNotificationRequestByNumber(env, request, result)) {
536         return NapiGetBoolean(env, false);
537     }
538     if (!SetNotificationRequestByBool(env, request, result)) {
539         return NapiGetBoolean(env, false);
540     }
541     if (!SetNotificationRequestByWantAgent(env, request, result)) {
542         return NapiGetBoolean(env, false);
543     }
544     if (!SetNotificationRequestByPixelMap(env, request, result)) {
545         return NapiGetBoolean(env, false);
546     }
547     if (!SetNotificationRequestByCustom(env, request, result)) {
548         return NapiGetBoolean(env, false);
549     }
550 
551     return NapiGetBoolean(env, true);
552 }
553 
554 
GetNotificationRequestByNumber(const napi_env & env,const napi_value & value,NotificationRequest & request)555 napi_value Common::GetNotificationRequestByNumber(
556     const napi_env &env, const napi_value &value, NotificationRequest &request)
557 {
558     ANS_LOGD("enter");
559     // id?: number
560     if (GetNotificationId(env, value, request) == nullptr) {
561         return nullptr;
562     }
563     // deliveryTime?: number
564     if (GetNotificationDeliveryTime(env, value, request) == nullptr) {
565         return nullptr;
566     }
567     // autoDeletedTime?: number
568     if (GetNotificationAutoDeletedTime(env, value, request) == nullptr) {
569         return nullptr;
570     }
571     // color?: number
572     if (GetNotificationColor(env, value, request) == nullptr) {
573         return nullptr;
574     }
575     // badgeIconStyle?: number
576     if (GetNotificationBadgeIconStyle(env, value, request) == nullptr) {
577         return nullptr;
578     }
579     // badgeNumber?: number
580     if (GetNotificationBadgeNumber(env, value, request) == nullptr) {
581         return nullptr;
582     }
583     // notificationControlFlags?: number
584     if (GetNotificationControlFlags(env, value, request) == nullptr) {
585         return nullptr;
586     }
587 
588     return NapiGetNull(env);
589 }
590 
GetNotificationRequestByString(const napi_env & env,const napi_value & value,NotificationRequest & request)591 napi_value Common::GetNotificationRequestByString(
592     const napi_env &env, const napi_value &value, NotificationRequest &request)
593 {
594     ANS_LOGD("enter");
595     // classification?: string
596     if (GetNotificationClassification(env, value, request) == nullptr) {
597         return nullptr;
598     }
599     // statusBarText?: string
600     if (GetNotificationStatusBarText(env, value, request) == nullptr) {
601         return nullptr;
602     }
603     // label?: string
604     if (GetNotificationLabel(env, value, request) == nullptr) {
605         return nullptr;
606     }
607     // groupName?: string
608     if (GetNotificationGroupName(env, value, request) == nullptr) {
609         return nullptr;
610     }
611     // appMessageId?: string
612     if (GetNotificationAppMessageId(env, value, request) == nullptr) {
613         return nullptr;
614     }
615     // sound?: string
616     if (GetNotificationSound(env, value, request) == nullptr) {
617         return nullptr;
618     }
619     return NapiGetNull(env);
620 }
621 
GetNotificationRequestByBool(const napi_env & env,const napi_value & value,NotificationRequest & request)622 napi_value Common::GetNotificationRequestByBool(
623     const napi_env &env, const napi_value &value, NotificationRequest &request)
624 {
625     ANS_LOGD("enter");
626     // isOngoing?: boolean
627     if (GetNotificationIsOngoing(env, value, request) == nullptr) {
628         return nullptr;
629     }
630     // isUnremovable?: boolean
631     if (GetNotificationIsUnremovable(env, value, request) == nullptr) {
632         return nullptr;
633     }
634     // tapDismissed?: boolean
635     if (GetNotificationtapDismissed(env, value, request) == nullptr) {
636         return nullptr;
637     }
638     // colorEnabled?: boolean
639     if (GetNotificationColorEnabled(env, value, request) == nullptr) {
640         return nullptr;
641     }
642     // isAlertOnce?: boolean
643     if (GetNotificationIsAlertOnce(env, value, request) == nullptr) {
644         return nullptr;
645     }
646     // isStopwatch?: boolean
647     if (GetNotificationIsStopwatch(env, value, request) == nullptr) {
648         return nullptr;
649     }
650     // isCountDown?: boolean
651     if (GetNotificationIsCountDown(env, value, request) == nullptr) {
652         return nullptr;
653     }
654     // showDeliveryTime?: boolean
655     if (GetNotificationShowDeliveryTime(env, value, request) == nullptr) {
656         return nullptr;
657     }
658 
659     GetNotificationIsRemoveAllowed(env, value, request);
660 
661     return NapiGetNull(env);
662 }
663 
GetNotificationRequestByCustom(const napi_env & env,const napi_value & value,NotificationRequest & request)664 napi_value Common::GetNotificationRequestByCustom(
665     const napi_env &env, const napi_value &value, NotificationRequest &request)
666 {
667     ANS_LOGD("enter");
668     // content: NotificationContent
669     if (GetNotificationContent(env, value, request) == nullptr) {
670         return nullptr;
671     }
672     // slotType?: notification.SlotType
673     if (GetNotificationSlotType(env, value, request) == nullptr) {
674         return nullptr;
675     }
676     // wantAgent?: WantAgent
677     if (GetNotificationWantAgent(env, value, request) == nullptr) {
678         return nullptr;
679     }
680     // extraInfo?: {[key: string]: any}
681     if (GetNotificationExtraInfo(env, value, request) == nullptr) {
682         return nullptr;
683     }
684     // removalWantAgent?: WantAgent
685     if (GetNotificationRemovalWantAgent(env, value, request) == nullptr) {
686         return nullptr;
687     }
688     // maxScreenWantAgent?: WantAgent
689     if (GetNotificationMaxScreenWantAgent(env, value, request) == nullptr) {
690         return nullptr;
691     }
692     // actionButtons?: Array<NotificationActionButton>
693     if (GetNotificationActionButtons(env, value, request) == nullptr) {
694         return nullptr;
695     }
696     // smallIcon?: image.PixelMap
697     if (GetNotificationSmallIcon(env, value, request) == nullptr) {
698         return nullptr;
699     }
700     // largeIcon?: image.PixelMap
701     if (GetNotificationLargeIcon(env, value, request) == nullptr) {
702         return nullptr;
703     }
704     // overlayIcon?: image.PixelMap
705     if (GetNotificationOverlayIcon(env, value, request) == nullptr) {
706         return nullptr;
707     }
708     // distributedOption?:DistributedOptions
709     if (GetNotificationRequestDistributedOptions(env, value, request) == nullptr) {
710         return nullptr;
711     }
712     // template?: NotificationTemplate
713     if (GetNotificationTemplate(env, value, request) == nullptr) {
714         return nullptr;
715     }
716     // unifiedGroupInfo?: NotificationUnifiedGroupInfo
717     if (GetNotificationUnifiedGroupInfo(env, value, request) == nullptr) {
718         return nullptr;
719     }
720     // representativeBundle?: BundleOption
721     if (GetNotificationBundleOption(env, value, request) == nullptr) {
722         return nullptr;
723     }
724     return NapiGetNull(env);
725 }
726 
GetNotificationRequest(const napi_env & env,const napi_value & value,NotificationRequest & request)727 napi_value Common::GetNotificationRequest(const napi_env &env, const napi_value &value, NotificationRequest &request)
728 {
729     ANS_LOGD("enter");
730     if (!GetNotificationRequestByNumber(env, value, request)) {
731         return nullptr;
732     }
733     if (!GetNotificationRequestByString(env, value, request)) {
734         return nullptr;
735     }
736     if (!GetNotificationRequestByBool(env, value, request)) {
737         return nullptr;
738     }
739     if (!GetNotificationRequestByCustom(env, value, request)) {
740         return nullptr;
741     }
742     return NapiGetNull(env);
743 }
744 
GetNotificationSmallIcon(const napi_env & env,const napi_value & value,NotificationRequest & request)745 napi_value Common::GetNotificationSmallIcon(const napi_env &env, const napi_value &value, NotificationRequest &request)
746 {
747     ANS_LOGD("enter");
748 
749     napi_valuetype valuetype = napi_undefined;
750     napi_value result = nullptr;
751     bool hasProperty = false;
752 
753     NAPI_CALL(env, napi_has_named_property(env, value, "smallIcon", &hasProperty));
754     if (hasProperty) {
755         napi_get_named_property(env, value, "smallIcon", &result);
756         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
757         if (valuetype != napi_object) {
758             ANS_LOGE("Argument type is not object.");
759             return nullptr;
760         }
761         std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
762         pixelMap = Media::PixelMapNapi::GetPixelMap(env, result);
763         if (pixelMap == nullptr) {
764             ANS_LOGE("Invalid object pixelMap");
765             return nullptr;
766         }
767         request.SetLittleIcon(pixelMap);
768     }
769 
770     return NapiGetNull(env);
771 }
772 
GetNotificationLargeIcon(const napi_env & env,const napi_value & value,NotificationRequest & request)773 napi_value Common::GetNotificationLargeIcon(const napi_env &env, const napi_value &value, NotificationRequest &request)
774 {
775     ANS_LOGD("enter");
776 
777     napi_valuetype valuetype = napi_undefined;
778     napi_value result = nullptr;
779     bool hasProperty = false;
780 
781     NAPI_CALL(env, napi_has_named_property(env, value, "largeIcon", &hasProperty));
782     if (hasProperty) {
783         napi_get_named_property(env, value, "largeIcon", &result);
784         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
785         if (valuetype != napi_object) {
786             ANS_LOGE("Wrong argument type. Object expected.");
787             return nullptr;
788         }
789         std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
790         pixelMap = Media::PixelMapNapi::GetPixelMap(env, result);
791         if (pixelMap == nullptr) {
792             ANS_LOGE("Invalid object pixelMap");
793             return nullptr;
794         }
795         request.SetBigIcon(pixelMap);
796     }
797 
798     return NapiGetNull(env);
799 }
800 
GetNotificationOverlayIcon(const napi_env & env,const napi_value & value,NotificationRequest & request)801 napi_value Common::GetNotificationOverlayIcon(
802     const napi_env &env, const napi_value &value, NotificationRequest &request)
803 {
804     ANS_LOGD("enter");
805 
806     napi_valuetype valuetype = napi_undefined;
807     napi_value result = nullptr;
808     bool hasProperty = false;
809 
810     NAPI_CALL(env, napi_has_named_property(env, value, "overlayIcon", &hasProperty));
811     if (hasProperty) {
812         napi_get_named_property(env, value, "overlayIcon", &result);
813         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
814         if (valuetype != napi_object) {
815             ANS_LOGE("Wrong argument type. Object expected.");
816             return nullptr;
817         }
818         std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
819         pixelMap = Media::PixelMapNapi::GetPixelMap(env, result);
820         if (pixelMap == nullptr) {
821             ANS_LOGE("Invalid object pixelMap");
822             return nullptr;
823         }
824         request.SetOverlayIcon(pixelMap);
825     }
826 
827     return NapiGetNull(env);
828 }
829 
GetNotificationSupportDisplayDevices(const napi_env & env,const napi_value & value,NotificationRequest & request)830 napi_value Common::GetNotificationSupportDisplayDevices(
831     const napi_env &env, const napi_value &value, NotificationRequest &request)
832 {
833     ANS_LOGD("enter");
834 
835     bool isArray = false;
836     bool hasProperty = false;
837     napi_valuetype valuetype = napi_undefined;
838     napi_value supportDisplayDevices = nullptr;
839     size_t strLen = 0;
840     uint32_t length = 0;
841 
842     NAPI_CALL(env, napi_has_named_property(env, value, "supportDisplayDevices", &hasProperty));
843     if (hasProperty) {
844         napi_get_named_property(env, value, "supportDisplayDevices", &supportDisplayDevices);
845         napi_is_array(env, supportDisplayDevices, &isArray);
846         if (!isArray) {
847             ANS_LOGE("Property supportDisplayDevices is expected to be an array.");
848             return nullptr;
849         }
850 
851         napi_get_array_length(env, supportDisplayDevices, &length);
852         if (length == 0) {
853             ANS_LOGE("The array is empty.");
854             return nullptr;
855         }
856         std::vector<std::string> devices;
857         for (size_t i = 0; i < length; i++) {
858             napi_value line = nullptr;
859             napi_get_element(env, supportDisplayDevices, i, &line);
860             NAPI_CALL(env, napi_typeof(env, line, &valuetype));
861             if (valuetype != napi_string) {
862                 ANS_LOGE("Wrong argument type. String expected.");
863                 return nullptr;
864             }
865             char str[STR_MAX_SIZE] = {0};
866             NAPI_CALL(env, napi_get_value_string_utf8(env, line, str, STR_MAX_SIZE - 1, &strLen));
867             devices.emplace_back(str);
868             ANS_LOGI("supportDisplayDevices = %{public}s", str);
869         }
870         request.SetDevicesSupportDisplay(devices);
871     }
872     return NapiGetNull(env);
873 }
874 
GetNotificationSupportOperateDevices(const napi_env & env,const napi_value & value,NotificationRequest & request)875 napi_value Common::GetNotificationSupportOperateDevices(
876     const napi_env &env, const napi_value &value, NotificationRequest &request)
877 {
878     ANS_LOGD("enter");
879 
880     bool isArray = false;
881     bool hasProperty = false;
882     napi_valuetype valuetype = napi_undefined;
883     napi_value supportOperateDevices = nullptr;
884     size_t strLen = 0;
885     uint32_t length = 0;
886 
887     NAPI_CALL(env, napi_has_named_property(env, value, "supportOperateDevices", &hasProperty));
888     if (hasProperty) {
889         napi_get_named_property(env, value, "supportOperateDevices", &supportOperateDevices);
890         napi_is_array(env, supportOperateDevices, &isArray);
891         if (!isArray) {
892             ANS_LOGE("Property supportOperateDevices is expected to be an array.");
893             return nullptr;
894         }
895 
896         napi_get_array_length(env, supportOperateDevices, &length);
897         if (length == 0) {
898             ANS_LOGE("The array is empty.");
899             return nullptr;
900         }
901         std::vector<std::string> devices;
902         for (size_t i = 0; i < length; i++) {
903             napi_value line = nullptr;
904             napi_get_element(env, supportOperateDevices, i, &line);
905             NAPI_CALL(env, napi_typeof(env, line, &valuetype));
906             if (valuetype != napi_string) {
907                 ANS_LOGE("Wrong argument type. String expected.");
908                 return nullptr;
909             }
910             char str[STR_MAX_SIZE] = {0};
911             NAPI_CALL(env, napi_get_value_string_utf8(env, line, str, STR_MAX_SIZE - 1, &strLen));
912             devices.emplace_back(str);
913             ANS_LOGI("supportOperateDevices = %{public}s", str);
914         }
915         request.SetDevicesSupportOperate(devices);
916     }
917 
918     return NapiGetNull(env);
919 }
920 
GetNotificationId(const napi_env & env,const napi_value & value,NotificationRequest & request)921 napi_value Common::GetNotificationId(const napi_env &env, const napi_value &value, NotificationRequest &request)
922 {
923     ANS_LOGD("enter");
924 
925     napi_valuetype valuetype = napi_undefined;
926     napi_value result = nullptr;
927     bool hasProperty = false;
928     int32_t notificationId = 0;
929 
930     NAPI_CALL(env, napi_has_named_property(env, value, "id", &hasProperty));
931     if (hasProperty) {
932         napi_get_named_property(env, value, "id", &result);
933         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
934         if (valuetype != napi_number) {
935             ANS_LOGE("Wrong argument type. Number expected.");
936             return nullptr;
937         }
938         napi_get_value_int32(env, result, &notificationId);
939         request.SetNotificationId(notificationId);
940         ANS_LOGI("notificationId = %{public}d", notificationId);
941     } else {
942         ANS_LOGI("default notificationId = 0");
943         request.SetNotificationId(0);
944     }
945 
946     return NapiGetNull(env);
947 }
948 
GetNotificationSlotType(const napi_env & env,const napi_value & value,NotificationRequest & request)949 napi_value Common::GetNotificationSlotType(const napi_env &env, const napi_value &value, NotificationRequest &request)
950 {
951     ANS_LOGD("enter");
952 
953     napi_valuetype valuetype = napi_undefined;
954     napi_value result = nullptr;
955     bool hasSlotType = false;
956     bool hasNotificationSlotType = false;
957     int32_t slotType = 0;
958 
959     NAPI_CALL(env, napi_has_named_property(env, value, "notificationSlotType", &hasNotificationSlotType));
960     if (hasNotificationSlotType) {
961         napi_get_named_property(env, value, "notificationSlotType", &result);
962         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
963         if (valuetype != napi_number) {
964             ANS_LOGE("Wrong argument type. Number expected.");
965             return nullptr;
966         }
967         napi_get_value_int32(env, result, &slotType);
968 
969         NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER;
970         if (!AnsEnumUtil::SlotTypeJSToC(SlotType(slotType), outType)) {
971             return nullptr;
972         }
973         request.SetSlotType(outType);
974         ANS_LOGI("notificationSlotType = %{public}d", slotType);
975         return NapiGetNull(env);
976     }
977 
978     NAPI_CALL(env, napi_has_named_property(env, value, "slotType", &hasSlotType));
979     if (hasSlotType) {
980         napi_get_named_property(env, value, "slotType", &result);
981         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
982         if (valuetype != napi_number) {
983             ANS_LOGE("Wrong argument type. Number expected.");
984             return nullptr;
985         }
986         napi_get_value_int32(env, result, &slotType);
987 
988         NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER;
989         if (!AnsEnumUtil::SlotTypeJSToC(SlotType(slotType), outType)) {
990             return nullptr;
991         }
992         request.SetSlotType(outType);
993         ANS_LOGI("slotType = %{public}d", slotType);
994     } else {
995         ANS_LOGI("default slotType = OTHER");
996         request.SetSlotType(NotificationConstant::OTHER);
997     }
998 
999     return NapiGetNull(env);
1000 }
1001 
GetNotificationIsOngoing(const napi_env & env,const napi_value & value,NotificationRequest & request)1002 napi_value Common::GetNotificationIsOngoing(const napi_env &env, const napi_value &value, NotificationRequest &request)
1003 {
1004     ANS_LOGD("enter");
1005 
1006     napi_valuetype valuetype = napi_undefined;
1007     napi_value result = nullptr;
1008     bool hasProperty = false;
1009     bool isOngoing = false;
1010 
1011     NAPI_CALL(env, napi_has_named_property(env, value, "isOngoing", &hasProperty));
1012     if (hasProperty) {
1013         napi_get_named_property(env, value, "isOngoing", &result);
1014         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1015         if (valuetype != napi_boolean) {
1016             ANS_LOGE("Wrong argument type. Bool expected.");
1017             return nullptr;
1018         }
1019         napi_get_value_bool(env, result, &isOngoing);
1020         request.SetInProgress(isOngoing);
1021     }
1022 
1023     return NapiGetNull(env);
1024 }
1025 
GetNotificationIsUnremovable(const napi_env & env,const napi_value & value,NotificationRequest & request)1026 napi_value Common::GetNotificationIsUnremovable(
1027     const napi_env &env, const napi_value &value, NotificationRequest &request)
1028 {
1029     ANS_LOGD("enter");
1030 
1031     napi_valuetype valuetype = napi_undefined;
1032     napi_value result = nullptr;
1033     bool hasProperty = false;
1034     bool isUnremovable = false;
1035 
1036     NAPI_CALL(env, napi_has_named_property(env, value, "isUnremovable", &hasProperty));
1037     if (hasProperty) {
1038         napi_get_named_property(env, value, "isUnremovable", &result);
1039         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1040         if (valuetype != napi_boolean) {
1041             ANS_LOGE("Wrong argument type. Bool expected.");
1042             return nullptr;
1043         }
1044         napi_get_value_bool(env, result, &isUnremovable);
1045         request.SetUnremovable(isUnremovable);
1046     }
1047 
1048     return NapiGetNull(env);
1049 }
1050 
GetNotificationDeliveryTime(const napi_env & env,const napi_value & value,NotificationRequest & request)1051 napi_value Common::GetNotificationDeliveryTime(
1052     const napi_env &env, const napi_value &value, NotificationRequest &request)
1053 {
1054     ANS_LOGD("enter");
1055 
1056     napi_valuetype valuetype = napi_undefined;
1057     napi_value result = nullptr;
1058     bool hasProperty = false;
1059     int64_t deliveryTime = 0;
1060 
1061     NAPI_CALL(env, napi_has_named_property(env, value, "deliveryTime", &hasProperty));
1062     if (hasProperty) {
1063         napi_get_named_property(env, value, "deliveryTime", &result);
1064         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1065         if (valuetype != napi_number) {
1066             ANS_LOGE("Wrong argument type. Number expected.");
1067             return nullptr;
1068         }
1069         napi_get_value_int64(env, result, &deliveryTime);
1070         request.SetDeliveryTime(deliveryTime);
1071     }
1072 
1073     return NapiGetNull(env);
1074 }
1075 
GetNotificationtapDismissed(const napi_env & env,const napi_value & value,NotificationRequest & request)1076 napi_value Common::GetNotificationtapDismissed(
1077     const napi_env &env, const napi_value &value, NotificationRequest &request)
1078 {
1079     ANS_LOGD("enter");
1080 
1081     napi_valuetype valuetype = napi_undefined;
1082     napi_value result = nullptr;
1083     bool hasProperty = false;
1084     bool tapDismissed = true;
1085 
1086     NAPI_CALL(env, napi_has_named_property(env, value, "tapDismissed", &hasProperty));
1087     if (hasProperty) {
1088         napi_get_named_property(env, value, "tapDismissed", &result);
1089         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1090         if (valuetype != napi_boolean) {
1091             ANS_LOGE("Wrong argument type. Bool expected.");
1092             return nullptr;
1093         }
1094         napi_get_value_bool(env, result, &tapDismissed);
1095         request.SetTapDismissed(tapDismissed);
1096     }
1097 
1098     return NapiGetNull(env);
1099 }
1100 
GetNotificationWantAgent(const napi_env & env,const napi_value & value,NotificationRequest & request)1101 napi_value Common::GetNotificationWantAgent(const napi_env &env, const napi_value &value, NotificationRequest &request)
1102 {
1103     ANS_LOGD("enter");
1104 
1105     bool hasProperty = false;
1106     AbilityRuntime::WantAgent::WantAgent *wantAgent = nullptr;
1107     napi_value result = nullptr;
1108     napi_valuetype valuetype = napi_undefined;
1109 
1110     NAPI_CALL(env, napi_has_named_property(env, value, "wantAgent", &hasProperty));
1111     if (hasProperty) {
1112         napi_get_named_property(env, value, "wantAgent", &result);
1113         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1114         if (valuetype != napi_object) {
1115             ANS_LOGE("Wrong argument type. Object expected.");
1116             return nullptr;
1117         }
1118         napi_unwrap(env, result, (void **)&wantAgent);
1119         if (wantAgent == nullptr) {
1120             ANS_LOGE("Invalid object wantAgent");
1121             return nullptr;
1122         }
1123         std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> sWantAgent =
1124             std::make_shared<AbilityRuntime::WantAgent::WantAgent>(*wantAgent);
1125         request.SetWantAgent(sWantAgent);
1126     }
1127 
1128     return NapiGetNull(env);
1129 }
1130 
GetNotificationExtraInfo(const napi_env & env,const napi_value & value,NotificationRequest & request)1131 napi_value Common::GetNotificationExtraInfo(const napi_env &env, const napi_value &value, NotificationRequest &request)
1132 {
1133     ANS_LOGD("enter");
1134 
1135     napi_valuetype valuetype = napi_undefined;
1136     napi_value result = nullptr;
1137     bool hasProperty = false;
1138 
1139     NAPI_CALL(env, napi_has_named_property(env, value, "extraInfo", &hasProperty));
1140     if (hasProperty) {
1141         napi_get_named_property(env, value, "extraInfo", &result);
1142         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1143         if (valuetype != napi_object) {
1144             ANS_LOGE("Wrong argument type. Object expected.");
1145             return nullptr;
1146         }
1147         AAFwk::WantParams wantParams;
1148         if (!OHOS::AppExecFwk::UnwrapWantParams(env, result, wantParams)) {
1149             return nullptr;
1150         }
1151 
1152         std::shared_ptr<AAFwk::WantParams> extras = std::make_shared<AAFwk::WantParams>(wantParams);
1153         request.SetAdditionalData(extras);
1154     }
1155 
1156     return NapiGetNull(env);
1157 }
1158 
GetNotificationGroupName(const napi_env & env,const napi_value & value,NotificationRequest & request)1159 napi_value Common::GetNotificationGroupName(const napi_env &env, const napi_value &value, NotificationRequest &request)
1160 {
1161     ANS_LOGD("enter");
1162 
1163     napi_valuetype valuetype = napi_undefined;
1164     napi_value result = nullptr;
1165     bool hasProperty = false;
1166     size_t strLen = 0;
1167 
1168     NAPI_CALL(env, napi_has_named_property(env, value, "groupName", &hasProperty));
1169     if (hasProperty) {
1170         napi_get_named_property(env, value, "groupName", &result);
1171         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1172         if (valuetype != napi_string) {
1173             ANS_LOGE("Wrong argument type. String expected.");
1174             return nullptr;
1175         }
1176         char str[STR_MAX_SIZE] = {0};
1177         NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
1178         request.SetGroupName(str);
1179     }
1180 
1181     return NapiGetNull(env);
1182 }
1183 
GetNotificationRemovalWantAgent(const napi_env & env,const napi_value & value,NotificationRequest & request)1184 napi_value Common::GetNotificationRemovalWantAgent(
1185     const napi_env &env, const napi_value &value, NotificationRequest &request)
1186 {
1187     ANS_LOGD("enter");
1188 
1189     bool hasProperty = false;
1190     AbilityRuntime::WantAgent::WantAgent *wantAgent = nullptr;
1191     napi_value result = nullptr;
1192     napi_valuetype valuetype = napi_undefined;
1193 
1194     NAPI_CALL(env, napi_has_named_property(env, value, "removalWantAgent", &hasProperty));
1195     if (hasProperty) {
1196         napi_get_named_property(env, value, "removalWantAgent", &result);
1197         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1198         if (valuetype != napi_object) {
1199             ANS_LOGE("Wrong argument type. Object expected.");
1200             return nullptr;
1201         }
1202         napi_unwrap(env, result, (void **)&wantAgent);
1203         if (wantAgent == nullptr) {
1204             ANS_LOGE("Invalid object removalWantAgent");
1205             return nullptr;
1206         }
1207         std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> removeWantAgent =
1208             std::make_shared<AbilityRuntime::WantAgent::WantAgent>(*wantAgent);
1209         if ((uint32_t)removeWantAgent->GetPendingWant()->GetType(
1210             removeWantAgent->GetPendingWant()->GetTarget()) >= OPERATION_MAX_TYPE) {
1211             request.SetRemovalWantAgent(removeWantAgent);
1212         }
1213     }
1214 
1215     return NapiGetNull(env);
1216 }
1217 
GetNotificationMaxScreenWantAgent(const napi_env & env,const napi_value & value,NotificationRequest & request)1218 napi_value Common::GetNotificationMaxScreenWantAgent(
1219     const napi_env &env, const napi_value &value, NotificationRequest &request)
1220 {
1221     ANS_LOGD("enter");
1222 
1223     bool hasProperty = false;
1224     AbilityRuntime::WantAgent::WantAgent *wantAgent = nullptr;
1225     napi_value result = nullptr;
1226     napi_valuetype valuetype = napi_undefined;
1227 
1228     NAPI_CALL(env, napi_has_named_property(env, value, "maxScreenWantAgent", &hasProperty));
1229     if (hasProperty) {
1230         napi_get_named_property(env, value, "maxScreenWantAgent", &result);
1231         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1232         if (valuetype != napi_object) {
1233             ANS_LOGE("Wrong argument type. Object expected.");
1234             return nullptr;
1235         }
1236         napi_unwrap(env, result, (void **)&wantAgent);
1237         if (wantAgent == nullptr) {
1238             ANS_LOGE("Invalid object maxScreenWantAgent");
1239             return nullptr;
1240         }
1241         std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> maxScreenWantAgent =
1242             std::make_shared<AbilityRuntime::WantAgent::WantAgent>(*wantAgent);
1243         request.SetMaxScreenWantAgent(maxScreenWantAgent);
1244     }
1245 
1246     return NapiGetNull(env);
1247 }
1248 
GetNotificationAutoDeletedTime(const napi_env & env,const napi_value & value,NotificationRequest & request)1249 napi_value Common::GetNotificationAutoDeletedTime(
1250     const napi_env &env, const napi_value &value, NotificationRequest &request)
1251 {
1252     ANS_LOGD("enter");
1253 
1254     napi_valuetype valuetype = napi_undefined;
1255     napi_value result = nullptr;
1256     bool hasProperty = false;
1257     int64_t autoDeletedTime = 0;
1258 
1259     NAPI_CALL(env, napi_has_named_property(env, value, "autoDeletedTime", &hasProperty));
1260     if (hasProperty) {
1261         napi_get_named_property(env, value, "autoDeletedTime", &result);
1262         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1263         if (valuetype != napi_number) {
1264             ANS_LOGE("Wrong argument type. Number expected.");
1265             return nullptr;
1266         }
1267         napi_get_value_int64(env, result, &autoDeletedTime);
1268         request.SetAutoDeletedTime(autoDeletedTime);
1269     }
1270 
1271     return NapiGetNull(env);
1272 }
1273 
GetNotificationClassification(const napi_env & env,const napi_value & value,NotificationRequest & request)1274 napi_value Common::GetNotificationClassification(
1275     const napi_env &env, const napi_value &value, NotificationRequest &request)
1276 {
1277     ANS_LOGD("enter");
1278 
1279     napi_valuetype valuetype = napi_undefined;
1280     napi_value result = nullptr;
1281     bool hasProperty = false;
1282     size_t strLen = 0;
1283 
1284     NAPI_CALL(env, napi_has_named_property(env, value, "classification", &hasProperty));
1285     if (hasProperty) {
1286         napi_get_named_property(env, value, "classification", &result);
1287         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1288         if (valuetype != napi_string) {
1289             ANS_LOGE("Wrong argument type. String expected.");
1290             return nullptr;
1291         }
1292         char str[STR_MAX_SIZE] = {0};
1293         NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
1294         request.SetClassification(str);
1295     }
1296 
1297     return NapiGetNull(env);
1298 }
1299 
GetNotificationAppMessageId(const napi_env & env,const napi_value & value,NotificationRequest & request)1300 napi_value Common::GetNotificationAppMessageId(
1301     const napi_env &env, const napi_value &value, NotificationRequest &request)
1302 {
1303     bool hasProperty = false;
1304     NAPI_CALL(env, napi_has_named_property(env, value, "appMessageId", &hasProperty));
1305     if (!hasProperty) {
1306         return NapiGetNull(env);
1307     }
1308 
1309     auto appMessageIdValue = AppExecFwk::GetPropertyValueByPropertyName(env, value, "appMessageId", napi_string);
1310     if (appMessageIdValue == nullptr) {
1311         ANS_LOGE("Wrong argument type. String expected.");
1312         return nullptr;
1313     }
1314 
1315     std::string appMessageId = AppExecFwk::UnwrapStringFromJS(env, appMessageIdValue);
1316     request.SetAppMessageId(appMessageId);
1317     return NapiGetNull(env);
1318 }
1319 
GetNotificationSound(const napi_env & env,const napi_value & value,NotificationRequest & request)1320 napi_value Common::GetNotificationSound(
1321     const napi_env &env, const napi_value &value, NotificationRequest &request)
1322 {
1323     bool hasProperty = false;
1324     NAPI_CALL(env, napi_has_named_property(env, value, "sound", &hasProperty));
1325     if (!hasProperty) {
1326         return NapiGetNull(env);
1327     }
1328 
1329     auto soundValue = AppExecFwk::GetPropertyValueByPropertyName(env, value, "sound", napi_string);
1330     if (soundValue == nullptr) {
1331         ANS_LOGE("Wrong argument type. String sound expected.");
1332         return nullptr;
1333     }
1334 
1335     std::string sound = AppExecFwk::UnwrapStringFromJS(env, soundValue);
1336     request.SetSound(sound);
1337     return NapiGetNull(env);
1338 }
1339 
GetNotificationColor(const napi_env & env,const napi_value & value,NotificationRequest & request)1340 napi_value Common::GetNotificationColor(const napi_env &env, const napi_value &value, NotificationRequest &request)
1341 {
1342     ANS_LOGD("enter");
1343 
1344     napi_valuetype valuetype = napi_undefined;
1345     napi_value result = nullptr;
1346     bool hasProperty = false;
1347     int32_t color = 0;
1348 
1349     NAPI_CALL(env, napi_has_named_property(env, value, "color", &hasProperty));
1350     if (hasProperty) {
1351         napi_get_named_property(env, value, "color", &result);
1352         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1353         if (valuetype != napi_number) {
1354             ANS_LOGE("Wrong argument type. Number expected.");
1355             return nullptr;
1356         }
1357         napi_get_value_int32(env, result, &color);
1358         if (color < 0) {
1359             ANS_LOGE("Wrong argument type. Natural number expected.");
1360             return nullptr;
1361         }
1362         request.SetColor(color);
1363     }
1364 
1365     return NapiGetNull(env);
1366 }
1367 
GetNotificationColorEnabled(const napi_env & env,const napi_value & value,NotificationRequest & request)1368 napi_value Common::GetNotificationColorEnabled(
1369     const napi_env &env, const napi_value &value, NotificationRequest &request)
1370 {
1371     ANS_LOGD("enter");
1372 
1373     napi_valuetype valuetype = napi_undefined;
1374     napi_value result = nullptr;
1375     bool hasProperty = false;
1376     bool colorEnabled = false;
1377 
1378     NAPI_CALL(env, napi_has_named_property(env, value, "colorEnabled", &hasProperty));
1379     if (hasProperty) {
1380         napi_get_named_property(env, value, "colorEnabled", &result);
1381         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1382         if (valuetype != napi_boolean) {
1383             ANS_LOGE("Wrong argument type. Bool expected.");
1384             return nullptr;
1385         }
1386         napi_get_value_bool(env, result, &colorEnabled);
1387         request.SetColorEnabled(colorEnabled);
1388     }
1389 
1390     return NapiGetNull(env);
1391 }
1392 
GetNotificationIsAlertOnce(const napi_env & env,const napi_value & value,NotificationRequest & request)1393 napi_value Common::GetNotificationIsAlertOnce(
1394     const napi_env &env, const napi_value &value, NotificationRequest &request)
1395 {
1396     ANS_LOGD("enter");
1397 
1398     napi_valuetype valuetype = napi_undefined;
1399     napi_value result = nullptr;
1400     bool hasProperty = false;
1401     bool isAlertOnce = false;
1402 
1403     NAPI_CALL(env, napi_has_named_property(env, value, "isAlertOnce", &hasProperty));
1404     if (hasProperty) {
1405         napi_get_named_property(env, value, "isAlertOnce", &result);
1406         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1407         if (valuetype != napi_boolean) {
1408             ANS_LOGE("Wrong argument type. Bool expected.");
1409             return nullptr;
1410         }
1411         napi_get_value_bool(env, result, &isAlertOnce);
1412         request.SetAlertOneTime(isAlertOnce);
1413     }
1414 
1415     return NapiGetNull(env);
1416 }
1417 
GetNotificationIsStopwatch(const napi_env & env,const napi_value & value,NotificationRequest & request)1418 napi_value Common::GetNotificationIsStopwatch(
1419     const napi_env &env, const napi_value &value, NotificationRequest &request)
1420 {
1421     ANS_LOGD("enter");
1422 
1423     napi_valuetype valuetype = napi_undefined;
1424     napi_value result = nullptr;
1425     bool hasProperty = false;
1426     bool isStopwatch = false;
1427 
1428     NAPI_CALL(env, napi_has_named_property(env, value, "isStopwatch", &hasProperty));
1429     if (hasProperty) {
1430         napi_get_named_property(env, value, "isStopwatch", &result);
1431         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1432         if (valuetype != napi_boolean) {
1433             ANS_LOGE("Wrong argument type. Bool expected.");
1434             return nullptr;
1435         }
1436         napi_get_value_bool(env, result, &isStopwatch);
1437         request.SetShowStopwatch(isStopwatch);
1438     }
1439 
1440     return NapiGetNull(env);
1441 }
1442 
GetNotificationIsCountDown(const napi_env & env,const napi_value & value,NotificationRequest & request)1443 napi_value Common::GetNotificationIsCountDown(
1444     const napi_env &env, const napi_value &value, NotificationRequest &request)
1445 {
1446     ANS_LOGD("enter");
1447 
1448     napi_valuetype valuetype = napi_undefined;
1449     napi_value result = nullptr;
1450     bool hasProperty = false;
1451     bool isCountDown = false;
1452 
1453     NAPI_CALL(env, napi_has_named_property(env, value, "isCountDown", &hasProperty));
1454     if (hasProperty) {
1455         napi_get_named_property(env, value, "isCountDown", &result);
1456         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1457         if (valuetype != napi_boolean) {
1458             ANS_LOGE("Wrong argument type. Bool expected.");
1459             return nullptr;
1460         }
1461         napi_get_value_bool(env, result, &isCountDown);
1462         request.SetCountdownTimer(isCountDown);
1463     }
1464 
1465     return NapiGetNull(env);
1466 }
1467 
GetNotificationStatusBarText(const napi_env & env,const napi_value & value,NotificationRequest & request)1468 napi_value Common::GetNotificationStatusBarText(
1469     const napi_env &env, const napi_value &value, NotificationRequest &request)
1470 {
1471     ANS_LOGD("enter");
1472 
1473     napi_valuetype valuetype = napi_undefined;
1474     napi_value result = nullptr;
1475     bool hasProperty = false;
1476     size_t strLen = 0;
1477 
1478     NAPI_CALL(env, napi_has_named_property(env, value, "statusBarText", &hasProperty));
1479     if (hasProperty) {
1480         napi_get_named_property(env, value, "statusBarText", &result);
1481         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1482         if (valuetype != napi_string) {
1483             ANS_LOGE("Wrong argument type. String expected.");
1484             return nullptr;
1485         }
1486         char str[STR_MAX_SIZE] = {0};
1487         NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
1488         request.SetStatusBarText(str);
1489     }
1490 
1491     return NapiGetNull(env);
1492 }
1493 
GetNotificationLabel(const napi_env & env,const napi_value & value,NotificationRequest & request)1494 napi_value Common::GetNotificationLabel(const napi_env &env, const napi_value &value, NotificationRequest &request)
1495 {
1496     ANS_LOGD("enter");
1497 
1498     napi_valuetype valuetype = napi_undefined;
1499     napi_value result = nullptr;
1500     bool hasProperty = false;
1501     size_t strLen = 0;
1502 
1503     NAPI_CALL(env, napi_has_named_property(env, value, "label", &hasProperty));
1504     if (hasProperty) {
1505         napi_get_named_property(env, value, "label", &result);
1506         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1507         if (valuetype != napi_string) {
1508             ANS_LOGE("Wrong argument type. String expected.");
1509             return nullptr;
1510         }
1511         char str[STR_MAX_SIZE] = {0};
1512         NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
1513         request.SetLabel(str);
1514     }
1515 
1516     return NapiGetNull(env);
1517 }
1518 
GetNotificationBadgeIconStyle(const napi_env & env,const napi_value & value,NotificationRequest & request)1519 napi_value Common::GetNotificationBadgeIconStyle(
1520     const napi_env &env, const napi_value &value, NotificationRequest &request)
1521 {
1522     ANS_LOGD("enter");
1523 
1524     napi_valuetype valuetype = napi_undefined;
1525     napi_value result = nullptr;
1526     bool hasProperty = false;
1527     int32_t badgeIconStyle = 0;
1528 
1529     NAPI_CALL(env, napi_has_named_property(env, value, "badgeIconStyle", &hasProperty));
1530     if (hasProperty) {
1531         napi_get_named_property(env, value, "badgeIconStyle", &result);
1532         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1533         if (valuetype != napi_number) {
1534             ANS_LOGE("Wrong argument type. Number expected.");
1535             return nullptr;
1536         }
1537         napi_get_value_int32(env, result, &badgeIconStyle);
1538         request.SetBadgeIconStyle(static_cast<NotificationRequest::BadgeStyle>(badgeIconStyle));
1539     }
1540 
1541     return NapiGetNull(env);
1542 }
1543 
GetNotificationShowDeliveryTime(const napi_env & env,const napi_value & value,NotificationRequest & request)1544 napi_value Common::GetNotificationShowDeliveryTime(
1545     const napi_env &env, const napi_value &value, NotificationRequest &request)
1546 {
1547     ANS_LOGD("enter");
1548 
1549     napi_valuetype valuetype = napi_undefined;
1550     napi_value result = nullptr;
1551     bool hasProperty = false;
1552     bool showDeliveryTime = false;
1553 
1554     NAPI_CALL(env, napi_has_named_property(env, value, "showDeliveryTime", &hasProperty));
1555     if (hasProperty) {
1556         napi_get_named_property(env, value, "showDeliveryTime", &result);
1557         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1558         if (valuetype != napi_boolean) {
1559             ANS_LOGE("Wrong argument type. Bool expected.");
1560             return nullptr;
1561         }
1562         napi_get_value_bool(env, result, &showDeliveryTime);
1563         request.SetShowDeliveryTime(showDeliveryTime);
1564     }
1565 
1566     return NapiGetNull(env);
1567 }
1568 
GetNotificationIsRemoveAllowed(const napi_env & env,const napi_value & value,NotificationRequest & request)1569 napi_value Common::GetNotificationIsRemoveAllowed(
1570     const napi_env &env, const napi_value &value, NotificationRequest &request)
1571 {
1572     ANS_LOGD("enter");
1573 
1574     napi_valuetype valuetype = napi_undefined;
1575     napi_value result = nullptr;
1576     bool hasProperty = false;
1577     bool isRemoveAllowed = true;
1578 
1579     NAPI_CALL(env, napi_has_named_property(env, value, "isRemoveAllowed", &hasProperty));
1580     if (hasProperty) {
1581         napi_get_named_property(env, value, "isRemoveAllowed", &result);
1582         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1583         if (valuetype != napi_boolean) {
1584             ANS_LOGE("Wrong argument type. Bool expected.");
1585             return nullptr;
1586         }
1587         napi_get_value_bool(env, result, &isRemoveAllowed);
1588         request.SetRemoveAllowed(isRemoveAllowed);
1589     }
1590 
1591     return NapiGetNull(env);
1592 }
1593 
GetNotificationActionButtons(const napi_env & env,const napi_value & value,NotificationRequest & request)1594 napi_value Common::GetNotificationActionButtons(
1595     const napi_env &env, const napi_value &value, NotificationRequest &request)
1596 {
1597     ANS_LOGD("enter");
1598 
1599     bool isArray = false;
1600     napi_valuetype valuetype = napi_undefined;
1601     napi_value actionButtons = nullptr;
1602     uint32_t length = 0;
1603     bool hasProperty = false;
1604 
1605     napi_has_named_property(env, value, "actionButtons", &hasProperty);
1606     if (!hasProperty) {
1607         return Common::NapiGetNull(env);
1608     }
1609 
1610     request.SetIsCoverActionButtons(true);
1611     napi_get_named_property(env, value, "actionButtons", &actionButtons);
1612     napi_is_array(env, actionButtons, &isArray);
1613     if (!isArray) {
1614         ANS_LOGE("Property actionButtons is expected to be an array.");
1615         return nullptr;
1616     }
1617     napi_get_array_length(env, actionButtons, &length);
1618     if (length == 0) {
1619         ANS_LOGI("The array is empty.");
1620         return Common::NapiGetNull(env);
1621     }
1622     for (size_t i = 0; i < length; i++) {
1623         napi_value actionButton = nullptr;
1624         napi_get_element(env, actionButtons, i, &actionButton);
1625         NAPI_CALL(env, napi_typeof(env, actionButton, &valuetype));
1626         if (valuetype != napi_object) {
1627             ANS_LOGE("Wrong argument type. Object expected.");
1628             return nullptr;
1629         }
1630 
1631         std::shared_ptr<NotificationActionButton> pActionButton = nullptr;
1632         if (GetNotificationActionButtonsDetailed(env, actionButton, pActionButton) == nullptr) {
1633             return nullptr;
1634         }
1635         request.AddActionButton(pActionButton);
1636     }
1637 
1638     return NapiGetNull(env);
1639 }
1640 
GetNotificationActionButtonsDetailed(const napi_env & env,const napi_value & actionButton,std::shared_ptr<NotificationActionButton> & pActionButton)1641 napi_value Common::GetNotificationActionButtonsDetailed(
1642     const napi_env &env, const napi_value &actionButton, std::shared_ptr<NotificationActionButton> &pActionButton)
1643 {
1644     ANS_LOGD("enter");
1645 
1646     if (!GetNotificationActionButtonsDetailedBasicInfo(env, actionButton, pActionButton)) {
1647         return nullptr;
1648     }
1649     if (!GetNotificationActionButtonsDetailedByExtras(env, actionButton, pActionButton)) {
1650         return nullptr;
1651     }
1652     if (!GetNotificationUserInput(env, actionButton, pActionButton)) {
1653         return nullptr;
1654     }
1655     return NapiGetNull(env);
1656 }
1657 
GetNotificationActionButtonsDetailedBasicInfo(const napi_env & env,const napi_value & actionButton,std::shared_ptr<NotificationActionButton> & pActionButton)1658 napi_value Common::GetNotificationActionButtonsDetailedBasicInfo(
1659     const napi_env &env, const napi_value &actionButton, std::shared_ptr<NotificationActionButton> &pActionButton)
1660 {
1661     ANS_LOGD("enter");
1662     napi_valuetype valuetype = napi_undefined;
1663     bool hasProperty = false;
1664     char str[STR_MAX_SIZE] = {0};
1665     size_t strLen = 0;
1666     napi_value value = nullptr;
1667     std::string title;
1668     AbilityRuntime::WantAgent::WantAgent *wantAgentPtr = nullptr;
1669     std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
1670     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> wantAgent;
1671 
1672     // title: string
1673     NAPI_CALL(env, napi_has_named_property(env, actionButton, "title", &hasProperty));
1674     if (!hasProperty) {
1675         ANS_LOGE("Property title expected.");
1676         return nullptr;
1677     }
1678     napi_get_named_property(env, actionButton, "title", &value);
1679     NAPI_CALL(env, napi_typeof(env, value, &valuetype));
1680     if (valuetype != napi_string) {
1681         ANS_LOGE("Wrong argument type. String expected.");
1682         return nullptr;
1683     }
1684     NAPI_CALL(env, napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen));
1685     title = str;
1686 
1687     // wantAgent: WantAgent
1688     NAPI_CALL(env, napi_has_named_property(env, actionButton, "wantAgent", &hasProperty));
1689     if (!hasProperty) {
1690         ANS_LOGE("Property wantAgent expected.");
1691         return nullptr;
1692     }
1693     napi_get_named_property(env, actionButton, "wantAgent", &value);
1694     NAPI_CALL(env, napi_typeof(env, value, &valuetype));
1695     if (valuetype != napi_object) {
1696         ANS_LOGE("Wrong argument type. Object expected.");
1697         return nullptr;
1698     }
1699     napi_unwrap(env, value, (void **)&wantAgentPtr);
1700     if (wantAgentPtr == nullptr) {
1701         ANS_LOGE("Invalid object wantAgent");
1702         return nullptr;
1703     }
1704     wantAgent = std::make_shared<AbilityRuntime::WantAgent::WantAgent>(*wantAgentPtr);
1705 
1706     // icon?: image.PixelMap
1707     NAPI_CALL(env, napi_has_named_property(env, actionButton, "icon", &hasProperty));
1708     if (hasProperty) {
1709         napi_get_named_property(env, actionButton, "icon", &value);
1710         NAPI_CALL(env, napi_typeof(env, value, &valuetype));
1711         if (valuetype != napi_object) {
1712             ANS_LOGE("Wrong argument type. Object expected.");
1713             return nullptr;
1714         }
1715         pixelMap = Media::PixelMapNapi::GetPixelMap(env, value);
1716         if (pixelMap == nullptr) {
1717             ANS_LOGE("Invalid object pixelMap");
1718             return nullptr;
1719         }
1720     }
1721     pActionButton = NotificationActionButton::Create(pixelMap, title, wantAgent);
1722 
1723     return NapiGetNull(env);
1724 }
1725 
GetNotificationActionButtonsDetailedByExtras(const napi_env & env,const napi_value & actionButton,std::shared_ptr<NotificationActionButton> & pActionButton)1726 napi_value Common::GetNotificationActionButtonsDetailedByExtras(
1727     const napi_env &env, const napi_value &actionButton, std::shared_ptr<NotificationActionButton> &pActionButton)
1728 {
1729     ANS_LOGD("enter");
1730 
1731     napi_valuetype valuetype = napi_undefined;
1732     napi_value result = nullptr;
1733     bool hasProperty = false;
1734 
1735     if (!pActionButton) {
1736         ANS_LOGE("pActionButton is nullptr");
1737         return nullptr;
1738     }
1739 
1740     // extras?: {[key: string]: any}
1741     NAPI_CALL(env, napi_has_named_property(env, actionButton, "extras", &hasProperty));
1742     if (hasProperty) {
1743         napi_get_named_property(env, actionButton, "extras", &result);
1744         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1745         if (valuetype != napi_object) {
1746             ANS_LOGE("Wrong argument type. Object expected.");
1747             return nullptr;
1748         }
1749         AAFwk::WantParams wantParams;
1750         if (!OHOS::AppExecFwk::UnwrapWantParams(env, result, wantParams)) {
1751             return nullptr;
1752         }
1753         pActionButton->AddAdditionalData(wantParams);
1754     }
1755     return NapiGetNull(env);
1756 }
1757 
GetNotificationBadgeNumber(const napi_env & env,const napi_value & value,NotificationRequest & request)1758 napi_value Common::GetNotificationBadgeNumber(
1759     const napi_env &env, const napi_value &value, NotificationRequest &request)
1760 {
1761     ANS_LOGD("enter");
1762 
1763     napi_valuetype valuetype = napi_undefined;
1764     napi_value result = nullptr;
1765     bool hasProperty = false;
1766     int32_t badgeNumber = 0;
1767 
1768     NAPI_CALL(env, napi_has_named_property(env, value, "badgeNumber", &hasProperty));
1769     if (hasProperty) {
1770         napi_get_named_property(env, value, "badgeNumber", &result);
1771         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1772         if (valuetype != napi_number) {
1773             ANS_LOGE("Wrong argument type. Number expected.");
1774             return nullptr;
1775         }
1776 
1777         napi_get_value_int32(env, result, &badgeNumber);
1778         if (badgeNumber < 0) {
1779             ANS_LOGE("Wrong badge number.");
1780             return nullptr;
1781         }
1782 
1783         request.SetBadgeNumber(badgeNumber);
1784     }
1785 
1786     return NapiGetNull(env);
1787 }
1788 
GetNotificationUnifiedGroupInfo(const napi_env & env,const napi_value & value,NotificationRequest & request)1789 napi_value Common::GetNotificationUnifiedGroupInfo(
1790     const napi_env &env, const napi_value &value, NotificationRequest &request)
1791 {
1792     bool hasProperty = false;
1793     NAPI_CALL(env, napi_has_named_property(env, value, "unifiedGroupInfo", &hasProperty));
1794     if (!hasProperty) {
1795         return NapiGetNull(env);
1796     }
1797 
1798     auto info = AppExecFwk::GetPropertyValueByPropertyName(env, value, "unifiedGroupInfo", napi_object);
1799     if (info == nullptr) {
1800         ANS_LOGE("Wrong argument type. object expected.");
1801         return nullptr;
1802     }
1803     std::shared_ptr<NotificationUnifiedGroupInfo> unifiedGroupInfo = std::make_shared<NotificationUnifiedGroupInfo>();
1804     // key?: string
1805     auto jsValue = AppExecFwk::GetPropertyValueByPropertyName(env, info, "key", napi_string);
1806     if (jsValue != nullptr) {
1807         std::string key = AppExecFwk::UnwrapStringFromJS(env, jsValue);
1808         unifiedGroupInfo->SetKey(key);
1809     }
1810 
1811     // title?: string
1812     jsValue = AppExecFwk::GetPropertyValueByPropertyName(env, info, "title", napi_string);
1813     if (jsValue != nullptr) {
1814         std::string title = AppExecFwk::UnwrapStringFromJS(env, jsValue);
1815         unifiedGroupInfo->SetTitle(title);
1816     }
1817 
1818     // content?: string
1819     jsValue = AppExecFwk::GetPropertyValueByPropertyName(env, info, "content", napi_string);
1820     if (jsValue != nullptr) {
1821         std::string content = AppExecFwk::UnwrapStringFromJS(env, jsValue);
1822         unifiedGroupInfo->SetContent(content);
1823     }
1824 
1825     // sceneName?: string
1826     jsValue = AppExecFwk::GetPropertyValueByPropertyName(env, info, "sceneName", napi_string);
1827     if (jsValue != nullptr) {
1828         std::string sceneName = AppExecFwk::UnwrapStringFromJS(env, jsValue);
1829         unifiedGroupInfo->SetSceneName(sceneName);
1830     }
1831 
1832     // extraInfo?: {[key:string] : any}
1833     jsValue = AppExecFwk::GetPropertyValueByPropertyName(env, info, "extraInfo", napi_object);
1834     if (jsValue != nullptr) {
1835         std::shared_ptr<AAFwk::WantParams> extras = std::make_shared<AAFwk::WantParams>();
1836         if (!OHOS::AppExecFwk::UnwrapWantParams(env, jsValue, *extras)) {
1837             return nullptr;
1838         }
1839         unifiedGroupInfo->SetExtraInfo(extras);
1840     }
1841 
1842     request.SetUnifiedGroupInfo(unifiedGroupInfo);
1843     return NapiGetNull(env);
1844 }
1845 
GetNotificationControlFlags(const napi_env & env,const napi_value & value,NotificationRequest & request)1846 napi_value Common::GetNotificationControlFlags(
1847     const napi_env &env, const napi_value &value, NotificationRequest &request)
1848 {
1849     ANS_LOGD("Called.");
1850 
1851     napi_valuetype valuetype = napi_undefined;
1852     napi_value result = nullptr;
1853     bool hasProperty = false;
1854     uint32_t notificationControlFlags = 0;
1855 
1856     NAPI_CALL(env, napi_has_named_property(env, value, "notificationControlFlags", &hasProperty));
1857     if (hasProperty) {
1858         napi_get_named_property(env, value, "notificationControlFlags", &result);
1859         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1860         if (valuetype != napi_number) {
1861             ANS_LOGE("Wrong argument type. Number expected.");
1862             return nullptr;
1863         }
1864 
1865         napi_get_value_uint32(env, result, &notificationControlFlags);
1866         if (notificationControlFlags == 0) {
1867             ANS_LOGD("Undefined notification control flags.");
1868             return nullptr;
1869         }
1870 
1871         request.SetNotificationControlFlags(notificationControlFlags);
1872     }
1873 
1874     return NapiGetNull(env);
1875 }
1876 }
1877 }
1878