• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #include "napi_common_want_agent.h"
31 
32 namespace OHOS {
33 namespace NotificationNapi {
34 std::set<std::shared_ptr<AbilityRuntime::WantAgent::WantAgent>> Common::wantAgent_;
35 std::mutex Common::mutex_;
36 
Common()37 Common::Common()
38 {}
39 
~Common()40 Common::~Common()
41 {}
42 
SetNotificationSortingMap(const napi_env & env,const std::shared_ptr<NotificationSortingMap> & sortingMap,napi_value & result)43 napi_value Common::SetNotificationSortingMap(
44     const napi_env &env, const std::shared_ptr<NotificationSortingMap> &sortingMap, napi_value &result)
45 {
46     ANS_LOGD("enter");
47     if (sortingMap == nullptr) {
48         ANS_LOGD("sortingMap is null");
49         return NapiGetBoolean(env, false);
50     }
51     if (sortingMap->GetKey().size() == 0) {
52         ANS_LOGD("sortingMap GetKey().size is empty");
53         return NapiGetBoolean(env, false);
54     }
55 
56     size_t count = 0;
57     napi_value arrSortedHashCode = nullptr;
58     napi_create_array(env, &arrSortedHashCode);
59     napi_value sortingsResult = nullptr;
60     napi_create_object(env, &sortingsResult);
61     for (auto key : sortingMap->GetKey()) {
62         NotificationSorting sorting;
63         if (sortingMap->GetNotificationSorting(key, sorting)) {
64             // sortedHashCode: Array<string>
65             napi_value keyValue = nullptr;
66             ANS_LOGD("sortingMap key = %{public}s", key.c_str());
67             napi_create_string_utf8(env, key.c_str(), NAPI_AUTO_LENGTH, &keyValue);
68             napi_set_element(env, arrSortedHashCode, count, keyValue);
69 
70             // sortings:{[key : string] : NotificationSorting}
71             napi_value sortingResult = nullptr;
72             napi_create_object(env, &sortingResult);
73             if (!SetNotificationSorting(env, sorting, sortingResult)) {
74                 ANS_LOGE("SetNotificationSorting call failed");
75                 return NapiGetBoolean(env, false);
76             }
77             napi_set_named_property(env, sortingsResult, key.c_str(), sortingResult);
78             count++;
79         } else {
80             ANS_LOGW("sortingMap Key: %{public}s match value is empty", key.c_str());
81         }
82     }
83     napi_set_named_property(env, result, "sortedHashCode", arrSortedHashCode);
84     napi_set_named_property(env, result, "sortings", sortingsResult);
85 
86     return NapiGetBoolean(env, true);
87 }
88 
SetNotificationSorting(const napi_env & env,NotificationSorting & sorting,napi_value & result)89 napi_value Common::SetNotificationSorting(const napi_env &env, NotificationSorting &sorting, napi_value &result)
90 {
91     ANS_LOGD("enter");
92 
93     // slot: NotificationSlot
94     napi_value slotResult = nullptr;
95     napi_value value = nullptr;
96     napi_create_object(env, &slotResult);
97     if (!sorting.GetSlot() || !SetNotificationSlot(env, *sorting.GetSlot(), slotResult)) {
98         ANS_LOGE("SetNotificationSlot call failed");
99         return NapiGetBoolean(env, false);
100     }
101     napi_set_named_property(env, result, "slot", slotResult);
102 
103     // hashCode?: string
104     napi_create_string_utf8(env, sorting.GetKey().c_str(), NAPI_AUTO_LENGTH, &value);
105     napi_set_named_property(env, result, "hashCode", value);
106 
107     // ranking?: number
108     napi_create_int32(env, sorting.GetRanking(), &value);
109     napi_set_named_property(env, result, "ranking", value);
110 
111     // isDisplayBadge?: boolean
112     napi_get_boolean(env, sorting.IsDisplayBadge(), &value);
113     napi_set_named_property(env, result, "isDisplayBadge", value);
114 
115     // isHiddenNotification?: boolean
116     napi_get_boolean(env, sorting.IsHiddenNotification(), &value);
117     napi_set_named_property(env, result, "isHiddenNotification", value);
118 
119     // importance?: number
120     napi_create_int32(env, sorting.GetImportance(), &value);
121     napi_set_named_property(env, result, "importance", value);
122 
123     // groupKeyOverride?: string
124     napi_create_string_utf8(env, sorting.GetGroupKeyOverride().c_str(), NAPI_AUTO_LENGTH, &value);
125     napi_set_named_property(env, result, "groupKeyOverride", value);
126 
127     // visiblenessOverride?: number
128     napi_create_int32(env, sorting.GetVisiblenessOverride(), &value);
129     napi_set_named_property(env, result, "visiblenessOverride", value);
130 
131     return NapiGetBoolean(env, true);
132 }
133 
SetNotificationSlot(const napi_env & env,const NotificationSlot & slot,napi_value & result)134 napi_value Common::SetNotificationSlot(const napi_env &env, const NotificationSlot &slot, napi_value &result)
135 {
136     ANS_LOGD("enter");
137 
138     napi_value value = nullptr;
139     // type: SlotType
140     SlotType outType = SlotType::UNKNOWN_TYPE;
141     if (!AnsEnumUtil::SlotTypeCToJS(slot.GetType(), outType)) {
142         return NapiGetBoolean(env, false);
143     }
144     napi_create_int32(env, static_cast<int32_t>(outType), &value);
145     napi_set_named_property(env, result, "type", value);
146     napi_set_named_property(env, result, "notificationType", value);
147 
148     // level?: number
149     SlotLevel outLevel = SlotLevel::LEVEL_NONE;
150     if (!AnsEnumUtil::SlotLevelCToJS(slot.GetLevel(), outLevel)) {
151         return NapiGetBoolean(env, false);
152     }
153     napi_create_int32(env, static_cast<int32_t>(outLevel), &value);
154     napi_set_named_property(env, result, "level", value);
155 
156     // desc?: string
157     napi_create_string_utf8(env, slot.GetDescription().c_str(), NAPI_AUTO_LENGTH, &value);
158     napi_set_named_property(env, result, "desc", value);
159 
160     // badgeFlag?: boolean
161     napi_get_boolean(env, slot.IsShowBadge(), &value);
162     napi_set_named_property(env, result, "badgeFlag", value);
163 
164     // bypassDnd?: boolean
165     napi_get_boolean(env, slot.IsEnableBypassDnd(), &value);
166     napi_set_named_property(env, result, "bypassDnd", value);
167 
168     // lockscreenVisibility?: number
169     int32_t lockScreenVisibleness = static_cast<int32_t>(slot.GetLockScreenVisibleness());
170     napi_create_int32(env, lockScreenVisibleness, &value);
171     napi_set_named_property(env, result, "lockscreenVisibility", value);
172 
173     // vibrationEnabled?: boolean
174     napi_get_boolean(env, slot.CanVibrate(), &value);
175     napi_set_named_property(env, result, "vibrationEnabled", value);
176 
177     // sound?: string
178     napi_create_string_utf8(env, slot.GetSound().ToString().c_str(), NAPI_AUTO_LENGTH, &value);
179     napi_set_named_property(env, result, "sound", value);
180 
181     // lightEnabled?: boolean
182     napi_get_boolean(env, slot.CanEnableLight(), &value);
183     napi_set_named_property(env, result, "lightEnabled", value);
184 
185     // lightColor?: number
186     napi_create_int32(env, slot.GetLedLightColor(), &value);
187     napi_set_named_property(env, result, "lightColor", value);
188 
189     // vibrationValues?: Array<number>
190     napi_value arr = nullptr;
191     napi_create_array(env, &arr);
192     size_t count = 0;
193     for (auto vec : slot.GetVibrationStyle()) {
194         napi_create_int64(env, vec, &value);
195         napi_set_element(env, arr, count, value);
196         count++;
197     }
198     napi_set_named_property(env, result, "vibrationValues", arr);
199 
200     // enabled?: boolean
201     napi_get_boolean(env, slot.GetEnable(), &value);
202     napi_set_named_property(env, result, "enabled", value);
203 
204     // authorizedStatus?: number
205     napi_create_int32(env, slot.GetAuthorizedStatus(), &value);
206     napi_set_named_property(env, result, "authorizedStatus", value);
207 
208     // reminderMode?: number
209     napi_create_int32(env, slot.GetReminderMode(), &value);
210     napi_set_named_property(env, result, "reminderMode", value);
211 
212     return NapiGetBoolean(env, true);
213 }
214 
SetDoNotDisturbDate(const napi_env & env,const NotificationDoNotDisturbDate & date,napi_value & result)215 napi_value Common::SetDoNotDisturbDate(
216     const napi_env &env, const NotificationDoNotDisturbDate &date, napi_value &result)
217 {
218     ANS_LOGD("enter");
219     DoNotDisturbType outType = DoNotDisturbType::TYPE_NONE;
220     if (!AnsEnumUtil::DoNotDisturbTypeCToJS(date.GetDoNotDisturbType(), outType)) {
221         return NapiGetBoolean(env, false);
222     }
223 
224     // type:DoNotDisturbType
225     napi_value typeNapi = nullptr;
226     napi_create_int32(env, static_cast<int32_t>(outType), &typeNapi);
227     napi_set_named_property(env, result, "type", typeNapi);
228 
229     // begin:Date
230     double begind = double(date.GetBeginDate());
231     napi_value beginNapi = nullptr;
232     napi_create_date(env, begind, &beginNapi);
233     napi_set_named_property(env, result, "begin", beginNapi);
234 
235     // end:Date
236     double endd = double(date.GetEndDate());
237     napi_value endNapi = nullptr;
238     napi_create_date(env, endd, &endNapi);
239     napi_set_named_property(env, result, "end", endNapi);
240 
241     return NapiGetBoolean(env, true);
242 }
243 
SetEnabledNotificationCallbackData(const napi_env & env,const EnabledNotificationCallbackData & data,napi_value & result)244 napi_value Common::SetEnabledNotificationCallbackData(const napi_env &env, const EnabledNotificationCallbackData &data,
245     napi_value &result)
246 {
247     ANS_LOGD("enter");
248     // bundle: string
249     napi_value bundleNapi = nullptr;
250     napi_create_string_utf8(env, data.GetBundle().c_str(), NAPI_AUTO_LENGTH, &bundleNapi);
251     napi_set_named_property(env, result, "bundle", bundleNapi);
252 
253     // uid: uid_t
254     napi_value uidNapi = nullptr;
255     napi_create_int32(env, data.GetUid(), &uidNapi);
256     napi_set_named_property(env, result, "uid", uidNapi);
257 
258     // enable: bool
259     napi_value enableNapi = nullptr;
260     napi_get_boolean(env, data.GetEnable(), &enableNapi);
261     napi_set_named_property(env, result, "enable", enableNapi);
262 
263     return NapiGetBoolean(env, true);
264 }
265 
SetBadgeCallbackData(const napi_env & env,const BadgeNumberCallbackData & data,napi_value & result)266 napi_value Common::SetBadgeCallbackData(const napi_env &env, const BadgeNumberCallbackData &data,
267     napi_value &result)
268 {
269     ANS_LOGD("enter");
270     // bundle: string
271     napi_value bundleNapi = nullptr;
272     napi_create_string_utf8(env, data.GetBundle().c_str(), NAPI_AUTO_LENGTH, &bundleNapi);
273     napi_set_named_property(env, result, "bundle", bundleNapi);
274 
275     // appInstanceKey: string
276     napi_value appKeyNapi = nullptr;
277     napi_create_string_utf8(env, data.GetAppInstanceKey().c_str(), NAPI_AUTO_LENGTH, &appKeyNapi);
278     napi_set_named_property(env, result, "appInstanceKey", appKeyNapi);
279 
280     // uid: int32_t
281     napi_value uidNapi = nullptr;
282     napi_create_int32(env, data.GetUid(), &uidNapi);
283     napi_set_named_property(env, result, "uid", uidNapi);
284 
285     // badgeNumber: int32_t
286     napi_value badgeNapi = nullptr;
287     napi_create_int32(env, data.GetBadgeNumber(), &badgeNapi);
288     napi_set_named_property(env, result, "badgeNumber", badgeNapi);
289 
290     // instanceKey: int32_t
291     napi_value keyNapi = nullptr;
292     napi_create_int32(env, data.GetInstanceKey(), &keyNapi);
293     napi_set_named_property(env, result, "instanceKey", keyNapi);
294 
295     return NapiGetBoolean(env, true);
296 }
297 
GetNotificationSubscriberInfo(const napi_env & env,const napi_value & value,NotificationSubscribeInfo & subscriberInfo)298 napi_value Common::GetNotificationSubscriberInfo(
299     const napi_env &env, const napi_value &value, NotificationSubscribeInfo &subscriberInfo)
300 {
301     ANS_LOGD("enter");
302     uint32_t length = 0;
303     size_t strLen = 0;
304     bool hasProperty = false;
305     bool isArray = false;
306     bool hasSlotTypes = false;
307     napi_valuetype valuetype = napi_undefined;
308 
309     // bundleNames?: Array<string>
310     NAPI_CALL(env, napi_has_named_property(env, value, "bundleNames", &hasProperty));
311     if (hasProperty) {
312         napi_value nBundleNames = nullptr;
313         napi_get_named_property(env, value, "bundleNames", &nBundleNames);
314         napi_is_array(env, nBundleNames, &isArray);
315         if (!isArray) {
316             ANS_LOGE("Property bundleNames is expected to be an array.");
317             return nullptr;
318         }
319         napi_get_array_length(env, nBundleNames, &length);
320         if (length == 0) {
321             ANS_LOGE("The array is empty.");
322             return nullptr;
323         }
324         for (uint32_t i = 0; i < length; ++i) {
325             napi_value nBundleName = nullptr;
326             char str[STR_MAX_SIZE] = {0};
327             napi_get_element(env, nBundleNames, i, &nBundleName);
328             NAPI_CALL(env, napi_typeof(env, nBundleName, &valuetype));
329             if (valuetype != napi_string) {
330                 ANS_LOGE("Wrong argument type. String expected.");
331                 return nullptr;
332             }
333             NAPI_CALL(env, napi_get_value_string_utf8(env, nBundleName, str, STR_MAX_SIZE - 1, &strLen));
334             subscriberInfo.bundleNames.emplace_back(str);
335             subscriberInfo.hasSubscribeInfo = true;
336         }
337     }
338 
339     // userId?: number
340     NAPI_CALL(env, napi_has_named_property(env, value, "userId", &hasProperty));
341     if (hasProperty) {
342         napi_value nUserId = nullptr;
343         napi_get_named_property(env, value, "userId", &nUserId);
344         NAPI_CALL(env, napi_typeof(env, nUserId, &valuetype));
345         if (valuetype != napi_number) {
346             ANS_LOGE("Wrong argument type. Number expected.");
347             return nullptr;
348         }
349         NAPI_CALL(env, napi_get_value_int32(env, nUserId, &subscriberInfo.userId));
350         subscriberInfo.hasSubscribeInfo = true;
351     }
352 
353     // deviceType?: number
354     NAPI_CALL(env, napi_has_named_property(env, value, "deviceType", &hasProperty));
355     if (hasProperty) {
356         napi_value nDeviceType = nullptr;
357         char str[STR_MAX_SIZE] = {0};
358         size_t strLen = 0;
359         napi_get_named_property(env, value, "deviceType", &nDeviceType);
360         NAPI_CALL(env, napi_typeof(env, nDeviceType, &valuetype));
361         if (valuetype != napi_string) {
362             ANS_LOGE("Wrong argument type. String expected.");
363             return nullptr;
364         }
365         NAPI_CALL(env, napi_get_value_string_utf8(env, nDeviceType, str, STR_MAX_SIZE - 1, &strLen));
366         if (std::strlen(str) == 0) {
367             ANS_LOGE("Property deviceType is empty");
368             return nullptr;
369         }
370         subscriberInfo.deviceType = str;
371         subscriberInfo.hasSubscribeInfo = true;
372     }
373 
374     // filterType?: number
375     NAPI_CALL(env, napi_has_named_property(env, value, "filterLimit", &hasProperty));
376     if (hasProperty) {
377         napi_value nFilterType = nullptr;
378         napi_get_named_property(env, value, "filterLimit", &nFilterType);
379         NAPI_CALL(env, napi_typeof(env, nFilterType, &valuetype));
380         if (valuetype != napi_number) {
381             ANS_LOGE("Wrong argument type. Number expected.");
382             return nullptr;
383         }
384         NAPI_CALL(env, napi_get_value_uint32(env, nFilterType, &subscriberInfo.filterType));
385         subscriberInfo.hasSubscribeInfo = true;
386     }
387 
388     NAPI_CALL(env, napi_has_named_property(env, value, "slotTypes", &hasSlotTypes));
389     if (hasSlotTypes) {
390         napi_value nSlotTypes = nullptr;
391         napi_get_named_property(env, value, "slotTypes", &nSlotTypes);
392         napi_is_array(env, nSlotTypes, &isArray);
393         if (!isArray) {
394             ANS_LOGE("Property slotTypes is expected to be an array.");
395             std::string msg = "Incorrect parameter types.The type of slotTypes must be array.";
396             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
397             return nullptr;
398         }
399         napi_get_array_length(env, nSlotTypes, &length);
400         if (length == 0) {
401             ANS_LOGE("The array is empty.");
402             std::string msg = "Incorrect parameters are left unspecified. The slotTypes list length is zero.";
403             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
404             return nullptr;
405         }
406 
407         for (uint32_t i = 0; i < length; ++i) {
408             napi_value nSlotType = nullptr;
409             int32_t slotType = 0;
410             napi_get_element(env, nSlotTypes, i, &nSlotType);
411             NAPI_CALL(env, napi_typeof(env, nSlotType, &valuetype));
412             if (valuetype != napi_number) {
413                 ANS_LOGE("Wrong argument type. Number expected.");
414                 std::string msg = "Incorrect parameter types.The type of slotType must be number.";
415                 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
416                 return nullptr;
417             }
418             napi_get_value_int32(env, nSlotType, &slotType);
419             NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER;
420             if (!AnsEnumUtil::SlotTypeJSToC(SlotType(slotType), outType)) {
421                 std::string msg = "Incorrect parameter types.slotType name must be in enum.";
422                 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
423                 return nullptr;
424             }
425             subscriberInfo.slotTypes.emplace_back(outType);
426             subscriberInfo.hasSubscribeInfo = true;
427         }
428     }
429 
430     return NapiGetNull(env);
431 }
432 
GetNotificationUserInput(const napi_env & env,const napi_value & actionButton,std::shared_ptr<NotificationActionButton> & pActionButton)433 napi_value Common::GetNotificationUserInput(
434     const napi_env &env, const napi_value &actionButton, std::shared_ptr<NotificationActionButton> &pActionButton)
435 {
436     ANS_LOGD("enter");
437     napi_valuetype valuetype = napi_undefined;
438     napi_value userInputResult = nullptr;
439     bool hasProperty = false;
440 
441     // userInput?: NotificationUserInput
442     NAPI_CALL(env, napi_has_named_property(env, actionButton, "userInput", &hasProperty));
443     if (hasProperty) {
444         napi_get_named_property(env, actionButton, "userInput", &userInputResult);
445         NAPI_CALL(env, napi_typeof(env, userInputResult, &valuetype));
446         if (valuetype != napi_object) {
447             ANS_LOGE("Wrong argument type. Object expected.");
448             return nullptr;
449         }
450         std::shared_ptr<NotificationUserInput> userInput = nullptr;
451 
452         if (!GetNotificationUserInputByInputKey(env, userInputResult, userInput)) {
453             return nullptr;
454         }
455         pActionButton->AddNotificationUserInput(userInput);
456     }
457 
458     return NapiGetNull(env);
459 }
460 
GetNotificationUserInputByInputKey(const napi_env & env,const napi_value & userInputResult,std::shared_ptr<NotificationUserInput> & userInput)461 napi_value Common::GetNotificationUserInputByInputKey(
462     const napi_env &env, const napi_value &userInputResult, std::shared_ptr<NotificationUserInput> &userInput)
463 {
464     ANS_LOGD("enter");
465     napi_valuetype valuetype = napi_undefined;
466     napi_value value = nullptr;
467     bool hasProperty = false;
468     char str[STR_MAX_SIZE] = {0};
469     size_t strLen = 0;
470 
471     // inputKey: string
472     NAPI_CALL(env, napi_has_named_property(env, userInputResult, "inputKey", &hasProperty));
473     if (!hasProperty) {
474         ANS_LOGE("Property inputKey expected.");
475         return nullptr;
476     }
477     napi_get_named_property(env, userInputResult, "inputKey", &value);
478     NAPI_CALL(env, napi_typeof(env, value, &valuetype));
479     if (valuetype != napi_string) {
480         ANS_LOGE("Wrong argument type. String expected.");
481         return nullptr;
482     }
483     NAPI_CALL(env, napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen));
484     ANS_LOGI("NotificationUserInput::inputKey = %{public}s", str);
485     userInput = NotificationUserInput::Create(str);
486     if (!userInput) {
487         ANS_LOGI("Failed to create NotificationUserInput by inputKey=%{public}s", str);
488         return nullptr;
489     }
490 
491     return NapiGetNull(env);
492 }
493 
GetNotificationUserInputByTag(const napi_env & env,const napi_value & userInputResult,std::shared_ptr<NotificationUserInput> & userInput)494 napi_value Common::GetNotificationUserInputByTag(
495     const napi_env &env, const napi_value &userInputResult, std::shared_ptr<NotificationUserInput> &userInput)
496 {
497     ANS_LOGD("enter");
498 
499     napi_valuetype valuetype = napi_undefined;
500     napi_value value = nullptr;
501     bool hasProperty = false;
502     char str[STR_MAX_SIZE] = {0};
503     size_t strLen = 0;
504 
505     if (!userInput) {
506         return nullptr;
507     }
508     // tag: string
509     NAPI_CALL(env, napi_has_named_property(env, userInputResult, "tag", &hasProperty));
510     if (!hasProperty) {
511         ANS_LOGE("Property tag expected.");
512         return nullptr;
513     }
514     napi_get_named_property(env, userInputResult, "tag", &value);
515     NAPI_CALL(env, napi_typeof(env, value, &valuetype));
516     if (valuetype != napi_string) {
517         ANS_LOGE("Wrong argument type. String expected.");
518         return nullptr;
519     }
520     NAPI_CALL(env, napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen));
521     userInput->SetTag(str);
522     ANS_LOGI("NotificationUserInput::tag = %{public}s", str);
523 
524     return NapiGetNull(env);
525 }
526 
GetNotificationUserInputByOptions(const napi_env & env,const napi_value & userInputResult,std::shared_ptr<NotificationUserInput> & userInput)527 napi_value Common::GetNotificationUserInputByOptions(
528     const napi_env &env, const napi_value &userInputResult, std::shared_ptr<NotificationUserInput> &userInput)
529 {
530     ANS_LOGD("enter");
531 
532     napi_valuetype valuetype = napi_undefined;
533     napi_value value = nullptr;
534     bool hasProperty = false;
535     uint32_t length = 0;
536     size_t strLen = 0;
537     bool isArray = false;
538 
539     if (!userInput) {
540         return nullptr;
541     }
542 
543     // options: Array<string>
544     NAPI_CALL(env, napi_has_named_property(env, userInputResult, "options", &hasProperty));
545 
546     if (!hasProperty) {
547         ANS_LOGE("Property options expected.");
548         return nullptr;
549     }
550     napi_get_named_property(env, userInputResult, "options", &value);
551     napi_is_array(env, value, &isArray);
552     if (!isArray) {
553         ANS_LOGE("Property options is expected to be an array.");
554         return nullptr;
555     }
556     napi_get_array_length(env, value, &length);
557     if (length == 0) {
558         ANS_LOGE("The array is empty.");
559         return nullptr;
560     }
561     std::vector<std::string> options;
562     for (uint32_t i = 0; i < length; ++i) {
563         napi_value option = nullptr;
564         char str[STR_MAX_SIZE] = {0};
565         napi_get_element(env, value, i, &option);
566         NAPI_CALL(env, napi_typeof(env, option, &valuetype));
567         if (valuetype != napi_string) {
568             ANS_LOGE("Wrong argument type. String expected.");
569             return nullptr;
570         }
571         NAPI_CALL(env, napi_get_value_string_utf8(env, option, str, STR_MAX_SIZE - 1, &strLen));
572         options.emplace_back(str);
573     }
574     userInput->SetOptions(options);
575 
576     return NapiGetNull(env);
577 }
578 
GetNotificationUserInputByPermitMimeTypes(const napi_env & env,const napi_value & userInputResult,std::shared_ptr<NotificationUserInput> & userInput)579 napi_value Common::GetNotificationUserInputByPermitMimeTypes(
580     const napi_env &env, const napi_value &userInputResult, std::shared_ptr<NotificationUserInput> &userInput)
581 {
582     ANS_LOGD("enter");
583 
584     napi_valuetype valuetype = napi_undefined;
585     napi_value value = nullptr;
586     bool hasProperty = false;
587     size_t strLen = 0;
588     uint32_t length = 0;
589     bool isArray = false;
590 
591     if (!userInput) {
592         return nullptr;
593     }
594 
595     // permitMimeTypes?: Array<string>
596     NAPI_CALL(env, napi_has_named_property(env, userInputResult, "permitMimeTypes", &hasProperty));
597     if (hasProperty) {
598         napi_get_named_property(env, userInputResult, "permitMimeTypes", &value);
599         napi_is_array(env, value, &isArray);
600         if (!isArray) {
601             ANS_LOGE("Property permitMimeTypes is expected to be an array.");
602             return nullptr;
603         }
604         napi_get_array_length(env, value, &length);
605         if (length == 0) {
606             ANS_LOGE("The array is empty.");
607             return nullptr;
608         }
609         for (uint32_t i = 0; i < length; ++i) {
610             napi_value permitMimeType = nullptr;
611             char str[STR_MAX_SIZE] = {0};
612             napi_get_element(env, value, i, &permitMimeType);
613             NAPI_CALL(env, napi_typeof(env, permitMimeType, &valuetype));
614             if (valuetype != napi_string) {
615                 ANS_LOGE("Wrong argument type. String expected.");
616                 return nullptr;
617             }
618             NAPI_CALL(env, napi_get_value_string_utf8(env, permitMimeType, str, STR_MAX_SIZE - 1, &strLen));
619             userInput->SetPermitMimeTypes(str, true);
620         }
621     }
622 
623     return NapiGetNull(env);
624 }
625 
GetNotificationUserInputByPermitFreeFormInput(const napi_env & env,const napi_value & userInputResult,std::shared_ptr<NotificationUserInput> & userInput)626 napi_value Common::GetNotificationUserInputByPermitFreeFormInput(
627     const napi_env &env, const napi_value &userInputResult, std::shared_ptr<NotificationUserInput> &userInput)
628 {
629     ANS_LOGD("enter");
630     napi_value value = nullptr;
631     napi_valuetype valuetype = napi_undefined;
632     bool hasProperty = false;
633 
634     if (!userInput) {
635         return nullptr;
636     }
637 
638     // permitFreeFormInput?: boolean
639     NAPI_CALL(env, napi_has_named_property(env, userInputResult, "permitFreeFormInput", &hasProperty));
640     if (hasProperty) {
641         bool permitFreeFormInput = false;
642         napi_get_named_property(env, userInputResult, "permitFreeFormInput", &value);
643         NAPI_CALL(env, napi_typeof(env, value, &valuetype));
644         if (valuetype != napi_boolean) {
645             ANS_LOGE("Wrong argument type. Bool expected.");
646             return nullptr;
647         }
648         napi_get_value_bool(env, value, &permitFreeFormInput);
649         ANS_LOGI("permitFreeFormInput is: %{public}d", permitFreeFormInput);
650         userInput->SetPermitFreeFormInput(permitFreeFormInput);
651     }
652 
653     return NapiGetNull(env);
654 }
655 
GetNotificationUserInputByEditType(const napi_env & env,const napi_value & userInputResult,std::shared_ptr<NotificationUserInput> & userInput)656 napi_value Common::GetNotificationUserInputByEditType(
657     const napi_env &env, const napi_value &userInputResult, std::shared_ptr<NotificationUserInput> &userInput)
658 {
659     ANS_LOGD("enter");
660     napi_value value = nullptr;
661     napi_valuetype valuetype = napi_undefined;
662     bool hasProperty = false;
663     int32_t editType = 0;
664 
665     if (!userInput) {
666         return nullptr;
667     }
668 
669     // editType?: number
670     NAPI_CALL(env, napi_has_named_property(env, userInputResult, "editType", &hasProperty));
671     if (hasProperty) {
672         napi_get_named_property(env, userInputResult, "editType", &value);
673         NAPI_CALL(env, napi_typeof(env, value, &valuetype));
674         if (valuetype != napi_number) {
675             ANS_LOGE("Wrong argument type. Number expected.");
676             std::string msg = "Incorrect parameter types. The type of editType must be number.";
677             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
678             return nullptr;
679         }
680         napi_get_value_int32(env, value, &editType);
681         userInput->SetEditType(NotificationConstant::InputEditType(editType));
682     }
683     return NapiGetNull(env);
684 }
685 
GetNotificationUserInputByAdditionalData(const napi_env & env,const napi_value & userInputResult,std::shared_ptr<NotificationUserInput> & userInput)686 napi_value Common::GetNotificationUserInputByAdditionalData(
687     const napi_env &env, const napi_value &userInputResult, std::shared_ptr<NotificationUserInput> &userInput)
688 {
689     ANS_LOGD("enter");
690 
691     napi_valuetype valuetype = napi_undefined;
692     napi_value result = nullptr;
693     bool hasProperty = false;
694 
695     if (!userInput) {
696         return nullptr;
697     }
698 
699     // additionalData?: {[key: string]: Object}
700     NAPI_CALL(env, napi_has_named_property(env, userInputResult, "additionalData", &hasProperty));
701     if (hasProperty) {
702         napi_get_named_property(env, userInputResult, "additionalData", &result);
703         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
704         if (valuetype != napi_object) {
705             ANS_LOGE("Wrong argument type. Object expected.");
706             std::string msg = "Incorrect parameter types. The type of additionalData must be object.";
707             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
708             return nullptr;
709         }
710         AAFwk::WantParams wantParams;
711         if (!OHOS::AppExecFwk::UnwrapWantParams(env, result, wantParams)) {
712             return nullptr;
713         }
714         userInput->AddAdditionalData(wantParams);
715     }
716 
717     return NapiGetNull(env);
718 }
719 
GetNotificationContentType(const napi_env & env,const napi_value & result,int32_t & type)720 napi_value Common::GetNotificationContentType(const napi_env &env, const napi_value &result, int32_t &type)
721 {
722     ANS_LOGD("enter");
723 
724     napi_value contentResult = nullptr;
725     napi_valuetype valuetype = napi_undefined;
726     bool hasNotificationContentType = false;
727     bool hasContentType = false;
728 
729     NAPI_CALL(env, napi_has_named_property(env, result, "notificationContentType", &hasNotificationContentType));
730     if (hasNotificationContentType) {
731         napi_get_named_property(env, result, "notificationContentType", &contentResult);
732         NAPI_CALL(env, napi_typeof(env, contentResult, &valuetype));
733         if (valuetype != napi_number) {
734             ANS_LOGE("Wrong argument type. Number expected.");
735             std::string msg = "Incorrect parameter types. The type of notificationContentType must be number.";
736             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
737             return nullptr;
738         }
739         napi_get_value_int32(env, contentResult, &type);
740 
741         return NapiGetNull(env);
742     } else {
743         ANS_LOGE("Property notificationContentType expected.");
744     }
745 
746     NAPI_CALL(env, napi_has_named_property(env, result, "contentType", &hasContentType));
747     if (hasContentType) {
748         napi_get_named_property(env, result, "contentType", &contentResult);
749         NAPI_CALL(env, napi_typeof(env, contentResult, &valuetype));
750         if (valuetype != napi_number) {
751             ANS_LOGE("Wrong argument type. Number expected.");
752             std::string msg = "Incorrect parameter types. The type of contentType must be number.";
753             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
754             return nullptr;
755         }
756         napi_get_value_int32(env, contentResult, &type);
757 
758         return NapiGetNull(env);
759     } else {
760         ANS_LOGE("Property contentType expected.");
761         return nullptr;
762     }
763 }
764 
GetNotificationSlot(const napi_env & env,const napi_value & value,NotificationSlot & slot)765 napi_value Common::GetNotificationSlot(const napi_env &env, const napi_value &value, NotificationSlot &slot)
766 {
767     ANS_LOGD("enter");
768 
769     napi_value nobj = nullptr;
770     napi_valuetype valuetype = napi_undefined;
771     bool hasType = false;
772     bool hasNotificationType = false;
773     int slotType = 0;
774 
775     NAPI_CALL(env, napi_has_named_property(env, value, "notificationType", &hasNotificationType));
776     NAPI_CALL(env, napi_has_named_property(env, value, "type", &hasType));
777     if (hasNotificationType) {
778         napi_get_named_property(env, value, "notificationType", &nobj);
779         NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
780         if (valuetype != napi_number) {
781             ANS_LOGE("Wrong argument type. Number expected.");
782             std::string msg = "Incorrect parameter types. The type of notificationType must be number.";
783             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
784             return nullptr;
785         }
786     } else if (!hasNotificationType && hasType) {
787         napi_get_named_property(env, value, "type", &nobj);
788         NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
789         if (valuetype != napi_number) {
790             ANS_LOGE("Wrong argument type. Number expected.");
791             std::string msg = "Incorrect parameter types. The type of type must be number.";
792             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
793             return nullptr;
794         }
795     } else {
796         ANS_LOGE("Property notificationType or type expected.");
797         return nullptr;
798     }
799 
800     if (nobj != nullptr) {
801         napi_get_value_int32(env, nobj, &slotType);
802     }
803 
804     NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER;
805     if (!AnsEnumUtil::SlotTypeJSToC(SlotType(slotType), outType)) {
806         return nullptr;
807     }
808     slot.SetType(outType);
809 
810     if (GetNotificationSlotByString(env, value, slot) == nullptr) {
811         return nullptr;
812     }
813     if (GetNotificationSlotByNumber(env, value, slot) == nullptr) {
814         return nullptr;
815     }
816     if (GetNotificationSlotByVibration(env, value, slot) == nullptr) {
817         return nullptr;
818     }
819     if (GetNotificationSlotByBool(env, value, slot) == nullptr) {
820         return nullptr;
821     }
822     return NapiGetNull(env);
823 }
824 
GetNotificationSlotByString(const napi_env & env,const napi_value & value,NotificationSlot & slot)825 napi_value Common::GetNotificationSlotByString(const napi_env &env, const napi_value &value, NotificationSlot &slot)
826 {
827     ANS_LOGD("enter");
828 
829     napi_value nobj = nullptr;
830     napi_valuetype valuetype = napi_undefined;
831     bool hasProperty = false;
832     size_t strLen = 0;
833 
834     // desc?: string
835     NAPI_CALL(env, napi_has_named_property(env, value, "desc", &hasProperty));
836     if (hasProperty) {
837         std::string desc;
838         char str[STR_MAX_SIZE] = {0};
839         napi_get_named_property(env, value, "desc", &nobj);
840         NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
841         if (valuetype != napi_string) {
842             ANS_LOGE("Wrong argument type. String expected.");
843             std::string msg = "Incorrect parameter types. The type of desc must be string.";
844             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
845             return nullptr;
846         }
847         NAPI_CALL(env, napi_get_value_string_utf8(env, nobj, str, STR_MAX_SIZE - 1, &strLen));
848         desc = str;
849         ANS_LOGI("desc is: %{public}s", desc.c_str());
850         slot.SetDescription(desc);
851     }
852 
853     // sound?: string
854     NAPI_CALL(env, napi_has_named_property(env, value, "sound", &hasProperty));
855     if (hasProperty) {
856         std::string sound;
857         char str[STR_MAX_SIZE] = {0};
858         napi_get_named_property(env, value, "sound", &nobj);
859         NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
860         if (valuetype != napi_string) {
861             ANS_LOGE("Wrong argument type. String expected.");
862             std::string msg = "Incorrect parameter types. The type of sound must be string.";
863             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
864             return nullptr;
865         }
866         NAPI_CALL(env, napi_get_value_string_utf8(env, nobj, str, STR_MAX_SIZE - 1, &strLen));
867         sound = str;
868         ANS_LOGI("sound is: %{public}s", sound.c_str());
869         slot.SetSound(Uri(sound));
870     }
871 
872     return NapiGetNull(env);
873 }
874 
GetNotificationSlotByBool(const napi_env & env,const napi_value & value,NotificationSlot & slot)875 napi_value Common::GetNotificationSlotByBool(const napi_env &env, const napi_value &value, NotificationSlot &slot)
876 {
877     ANS_LOGD("enter");
878     napi_value nobj = nullptr;
879     napi_valuetype valuetype = napi_undefined;
880     bool hasProperty = false;
881 
882     // badgeFlag?: boolean
883     NAPI_CALL(env, napi_has_named_property(env, value, "badgeFlag", &hasProperty));
884     if (hasProperty) {
885         bool badgeFlag = false;
886         napi_get_named_property(env, value, "badgeFlag", &nobj);
887         NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
888         if (valuetype != napi_boolean) {
889             ANS_LOGE("Wrong argument type. Bool expected.");
890             std::string msg = "Incorrect parameter types. The type of badgeFlag must be bool.";
891             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
892             return nullptr;
893         }
894         napi_get_value_bool(env, nobj, &badgeFlag);
895         ANS_LOGI("badgeFlag is: %{public}d", badgeFlag);
896         slot.EnableBadge(badgeFlag);
897     }
898 
899     // bypassDnd?: boolean
900     NAPI_CALL(env, napi_has_named_property(env, value, "bypassDnd", &hasProperty));
901     if (hasProperty) {
902         bool bypassDnd = false;
903         napi_get_named_property(env, value, "bypassDnd", &nobj);
904         NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
905         if (valuetype != napi_boolean) {
906             ANS_LOGE("Wrong argument type. Bool expected.");
907             std::string msg = "Incorrect parameter types. The type of bypassDnd must be bool.";
908             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
909             return nullptr;
910         }
911         napi_get_value_bool(env, nobj, &bypassDnd);
912         ANS_LOGI("bypassDnd is: %{public}d", bypassDnd);
913         slot.EnableBypassDnd(bypassDnd);
914     }
915 
916     // lightEnabled?: boolean
917     NAPI_CALL(env, napi_has_named_property(env, value, "lightEnabled", &hasProperty));
918     if (hasProperty) {
919         bool lightEnabled = false;
920         napi_get_named_property(env, value, "lightEnabled", &nobj);
921         NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
922         if (valuetype != napi_boolean) {
923             ANS_LOGE("Wrong argument type. Bool expected.");
924             std::string msg = "Incorrect parameter types. The type of lightEnabled must be bool.";
925             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
926             return nullptr;
927         }
928         napi_get_value_bool(env, nobj, &lightEnabled);
929         ANS_LOGI("lightEnabled is: %{public}d", lightEnabled);
930         slot.SetEnableLight(lightEnabled);
931     }
932 
933     return NapiGetNull(env);
934 }
935 
GetNotificationSlotByNumber(const napi_env & env,const napi_value & value,NotificationSlot & slot)936 napi_value Common::GetNotificationSlotByNumber(const napi_env &env, const napi_value &value, NotificationSlot &slot)
937 {
938     ANS_LOGD("enter");
939 
940     napi_value nobj = nullptr;
941     napi_valuetype valuetype = napi_undefined;
942     bool hasProperty = false;
943 
944     // level?: number
945     NAPI_CALL(env, napi_has_named_property(env, value, "level", &hasProperty));
946     if (hasProperty) {
947         int inLevel = 0;
948         napi_get_named_property(env, value, "level", &nobj);
949         NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
950         if (valuetype != napi_number) {
951             ANS_LOGE("Wrong argument type. Number expected.");
952             std::string msg = "Incorrect parameter types. The type of level must be number.";
953             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
954             return nullptr;
955         }
956         napi_get_value_int32(env, nobj, &inLevel);
957         ANS_LOGI("level is: %{public}d", inLevel);
958 
959         NotificationSlot::NotificationLevel outLevel {NotificationSlot::NotificationLevel::LEVEL_NONE};
960         if (!AnsEnumUtil::SlotLevelJSToC(SlotLevel(inLevel), outLevel)) {
961             return nullptr;
962         }
963         slot.SetLevel(outLevel);
964     }
965 
966     // lockscreenVisibility?: number
967     NAPI_CALL(env, napi_has_named_property(env, value, "lockscreenVisibility", &hasProperty));
968     if (hasProperty) {
969         int lockscreenVisibility = 0;
970         napi_get_named_property(env, value, "lockscreenVisibility", &nobj);
971         NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
972         if (valuetype != napi_number) {
973             ANS_LOGE("Wrong argument type. Number expected.");
974             std::string msg = "Incorrect parameter types. The type of lockscreenVisibility must be number.";
975             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
976             return nullptr;
977         }
978         napi_get_value_int32(env, nobj, &lockscreenVisibility);
979         ANS_LOGI("lockscreenVisibility is: %{public}d", lockscreenVisibility);
980         slot.SetLockscreenVisibleness(NotificationConstant::VisiblenessType(lockscreenVisibility));
981     }
982 
983     // lightColor?: number
984     NAPI_CALL(env, napi_has_named_property(env, value, "lightColor", &hasProperty));
985     if (hasProperty) {
986         int lightColor = 0;
987         napi_get_named_property(env, value, "lightColor", &nobj);
988         NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
989         if (valuetype != napi_number) {
990             ANS_LOGE("Wrong argument type. Number expected.");
991             std::string msg = "Incorrect parameter types. The type of lightColor must be number.";
992             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
993             return nullptr;
994         }
995         napi_get_value_int32(env, nobj, &lightColor);
996         ANS_LOGI("lightColor is: %{public}d", lightColor);
997         slot.SetLedLightColor(lightColor);
998     }
999     return NapiGetNull(env);
1000 }
1001 
GetNotificationSlotByVibration(const napi_env & env,const napi_value & value,NotificationSlot & slot)1002 napi_value Common::GetNotificationSlotByVibration(const napi_env &env, const napi_value &value, NotificationSlot &slot)
1003 {
1004     ANS_LOGD("enter");
1005     napi_value nobj = nullptr;
1006     napi_valuetype valuetype = napi_undefined;
1007     bool hasProperty = false;
1008     uint32_t length = 0;
1009 
1010     // vibrationEnabled?: boolean
1011     bool vibrationEnabled = false;
1012     NAPI_CALL(env, napi_has_named_property(env, value, "vibrationEnabled", &hasProperty));
1013     if (hasProperty) {
1014         napi_get_named_property(env, value, "vibrationEnabled", &nobj);
1015         NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
1016         if (valuetype != napi_boolean) {
1017             ANS_LOGE("Wrong argument type. Bool expected.");
1018             std::string msg = "Incorrect parameter types. The type of vibrationEnabled must be bool.";
1019             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1020             return nullptr;
1021         }
1022 
1023         napi_get_value_bool(env, nobj, &vibrationEnabled);
1024         slot.SetEnableVibration(vibrationEnabled);
1025     }
1026 
1027     if (!vibrationEnabled) {
1028         return NapiGetNull(env);
1029     }
1030 
1031     // vibrationValues?: Array<number>
1032     NAPI_CALL(env, napi_has_named_property(env, value, "vibrationValues", &hasProperty));
1033     if (hasProperty) {
1034         bool isArray = false;
1035         napi_get_named_property(env, value, "vibrationValues", &nobj);
1036         napi_is_array(env, nobj, &isArray);
1037         if (!isArray) {
1038             ANS_LOGE("Property vibrationValues is expected to be an array.");
1039             return nullptr;
1040         }
1041 
1042         napi_get_array_length(env, nobj, &length);
1043         std::vector<int64_t> vibrationValues;
1044         for (size_t i = 0; i < length; i++) {
1045             napi_value nVibrationValue = nullptr;
1046             int64_t vibrationValue = 0;
1047             napi_get_element(env, nobj, i, &nVibrationValue);
1048             NAPI_CALL(env, napi_typeof(env, nVibrationValue, &valuetype));
1049             if (valuetype != napi_number) {
1050                 ANS_LOGE("Wrong argument type. Number expected.");
1051                 return nullptr;
1052             }
1053             napi_get_value_int64(env, nVibrationValue, &vibrationValue);
1054             vibrationValues.emplace_back(vibrationValue);
1055         }
1056         slot.SetVibrationStyle(vibrationValues);
1057     }
1058 
1059     return NapiGetNull(env);
1060 }
1061 
GetBundleOption(const napi_env & env,const napi_value & value,NotificationBundleOption & option)1062 napi_value Common::GetBundleOption(const napi_env &env, const napi_value &value, NotificationBundleOption &option)
1063 {
1064     ANS_LOGD("enter");
1065 
1066     bool hasProperty {false};
1067     napi_valuetype valuetype = napi_undefined;
1068     napi_value result = nullptr;
1069 
1070     char str[STR_MAX_SIZE] = {0};
1071     size_t strLen = 0;
1072     // bundle: string
1073     NAPI_CALL(env, napi_has_named_property(env, value, "bundle", &hasProperty));
1074     if (!hasProperty) {
1075         ANS_LOGE("Property bundle expected.");
1076         return nullptr;
1077     }
1078     napi_get_named_property(env, value, "bundle", &result);
1079     NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1080     if (valuetype != napi_string) {
1081         ANS_LOGE("Wrong argument type. String expected.");
1082         std::string msg = "Incorrect parameter types. The type of bundle must be string.";
1083         Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1084         return nullptr;
1085     }
1086     NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
1087     option.SetBundleName(str);
1088 
1089     // uid?: number
1090     NAPI_CALL(env, napi_has_named_property(env, value, "uid", &hasProperty));
1091     if (hasProperty) {
1092         int32_t uid = 0;
1093         napi_get_named_property(env, value, "uid", &result);
1094         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1095         if (valuetype != napi_number) {
1096             ANS_LOGE("Wrong argument type. Number expected.");
1097             return nullptr;
1098         }
1099         napi_get_value_int32(env, result, &uid);
1100         option.SetUid(uid);
1101     }
1102 
1103     return NapiGetNull(env);
1104 }
1105 
GetButtonOption(const napi_env & env,const napi_value & value,NotificationButtonOption & option)1106 napi_value Common::GetButtonOption(const napi_env &env, const napi_value &value, NotificationButtonOption &option)
1107 {
1108     ANS_LOGD("enter");
1109 
1110     bool hasProperty {false};
1111     napi_valuetype valuetype = napi_undefined;
1112     napi_value result = nullptr;
1113 
1114     char str[STR_MAX_SIZE] = {0};
1115     size_t strLen = 0;
1116     // buttonName: string
1117     NAPI_CALL(env, napi_has_named_property(env, value, "buttonName", &hasProperty));
1118     if (!hasProperty) {
1119         ANS_LOGE("Property buttonName expected.");
1120         return nullptr;
1121     }
1122     napi_get_named_property(env, value, "buttonName", &result);
1123     NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1124     if (valuetype != napi_string) {
1125         ANS_LOGE("Wrong argument type. String expected.");
1126         std::string msg = "Incorrect parameter types. The type of buttonName must be string.";
1127         Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1128         return nullptr;
1129     }
1130     NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
1131     option.SetButtonName(str);
1132 
1133     return NapiGetNull(env);
1134 }
1135 
GetHashCodes(const napi_env & env,const napi_value & value,std::vector<std::string> & hashCodes)1136 napi_value Common::GetHashCodes(const napi_env &env, const napi_value &value, std::vector<std::string> &hashCodes)
1137 {
1138     ANS_LOGD("enter");
1139     uint32_t length = 0;
1140     napi_get_array_length(env, value, &length);
1141     if (length == 0) {
1142         ANS_LOGE("The array is empty.");
1143         return nullptr;
1144     }
1145     napi_valuetype valuetype = napi_undefined;
1146     for (size_t i = 0; i < length; i++) {
1147         napi_value hashCode = nullptr;
1148         napi_get_element(env, value, i, &hashCode);
1149         NAPI_CALL(env, napi_typeof(env, hashCode, &valuetype));
1150         if (valuetype != napi_string) {
1151             ANS_LOGE("Wrong argument type. Object expected.");
1152             return nullptr;
1153         }
1154         char str[STR_MAX_SIZE] = {0};
1155         size_t strLen = 0;
1156         NAPI_CALL(env, napi_get_value_string_utf8(env, hashCode, str, STR_MAX_SIZE - 1, &strLen));
1157         hashCodes.emplace_back(str);
1158     }
1159 
1160     return NapiGetNull(env);
1161 }
1162 
GetNotificationKey(const napi_env & env,const napi_value & value,NotificationKey & key)1163 napi_value Common::GetNotificationKey(const napi_env &env, const napi_value &value, NotificationKey &key)
1164 {
1165     ANS_LOGD("enter");
1166 
1167     bool hasProperty {false};
1168     napi_valuetype valuetype = napi_undefined;
1169     napi_value result = nullptr;
1170 
1171     // id: number
1172     NAPI_CALL(env, napi_has_named_property(env, value, "id", &hasProperty));
1173     if (!hasProperty) {
1174         ANS_LOGE("Property id expected.");
1175         return nullptr;
1176     }
1177     napi_get_named_property(env, value, "id", &result);
1178     NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1179     if (valuetype != napi_number) {
1180         ANS_LOGE("Wrong argument type. Number expected.");
1181         std::string msg = "Incorrect parameter types. The type of id must be number.";
1182         Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1183         return nullptr;
1184     }
1185     napi_get_value_int32(env, result, &key.id);
1186 
1187     // label?: string
1188     NAPI_CALL(env, napi_has_named_property(env, value, "label", &hasProperty));
1189     if (hasProperty) {
1190         char str[STR_MAX_SIZE] = {0};
1191         size_t strLen = 0;
1192         napi_get_named_property(env, value, "label", &result);
1193         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1194         if (valuetype != napi_string) {
1195             ANS_LOGE("Wrong argument type. String expected.");
1196             std::string msg = "Incorrect parameter types. The type of label must be string.";
1197             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1198             return nullptr;
1199         }
1200         NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
1201         key.label = str;
1202     }
1203 
1204     return NapiGetNull(env);
1205 }
1206 
IsValidRemoveReason(int32_t reasonType)1207 bool Common::IsValidRemoveReason(int32_t reasonType)
1208 {
1209     if (reasonType == NotificationConstant::CLICK_REASON_DELETE ||
1210         reasonType == NotificationConstant::CANCEL_REASON_DELETE) {
1211         return true;
1212     }
1213     ANS_LOGE("Reason %{public}d is an invalid value", reasonType);
1214     return false;
1215 }
1216 
CreateWantAgentByJS(const napi_env & env,const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> & agent)1217 __attribute__((no_sanitize("cfi"))) napi_value Common::CreateWantAgentByJS(const napi_env &env,
1218     const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> &agent)
1219 {
1220     if (agent == nullptr) {
1221         ANS_LOGI("agent is nullptr");
1222         return nullptr;
1223     }
1224 
1225     {
1226         std::lock_guard<std::mutex> lock(mutex_);
1227         wantAgent_.insert(agent);
1228     }
1229     napi_finalize finalize = [](napi_env env, void *data, void *hint) {
1230         AbilityRuntime::WantAgent::WantAgent *objectInfo =
1231                 static_cast<AbilityRuntime::WantAgent::WantAgent *>(data);
1232             if (objectInfo) {
1233                 std::lock_guard<std::mutex> lock(mutex_);
1234                 for (auto it = wantAgent_.begin(); it != wantAgent_.end(); ++it) {
1235                     if ((*it).get() == objectInfo) {
1236                         wantAgent_.erase(it);
1237                         break;
1238                     }
1239                 }
1240             }
1241     };
1242     return AppExecFwk::WrapWantAgent(env, agent.get(), finalize);
1243 }
1244 
GetNotificationTemplate(const napi_env & env,const napi_value & value,NotificationRequest & request)1245 napi_value Common::GetNotificationTemplate(const napi_env &env, const napi_value &value, NotificationRequest &request)
1246 {
1247     ANS_LOGD("enter");
1248 
1249     napi_valuetype valuetype = napi_undefined;
1250     napi_value result = nullptr;
1251     bool hasProperty = false;
1252 
1253     NAPI_CALL(env, napi_has_named_property(env, value, "template", &hasProperty));
1254     if (hasProperty) {
1255         napi_get_named_property(env, value, "template", &result);
1256         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1257         if (valuetype != napi_object) {
1258             ANS_LOGE("Wrong argument type. Object expected.");
1259             std::string msg = "Incorrect parameter types. The type of template must be object.";
1260             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1261             return nullptr;
1262         }
1263 
1264         std::shared_ptr<NotificationTemplate> templ = std::make_shared<NotificationTemplate>();
1265         if (templ == nullptr) {
1266             ANS_LOGE("template is null");
1267             return nullptr;
1268         }
1269         if (GetNotificationTemplateInfo(env, result, templ) == nullptr) {
1270             return nullptr;
1271         }
1272 
1273         request.SetTemplate(templ);
1274     }
1275 
1276     return NapiGetNull(env);
1277 }
1278 
GetNotificationBundleOption(const napi_env & env,const napi_value & value,NotificationRequest & request)1279 napi_value Common::GetNotificationBundleOption(
1280     const napi_env &env, const napi_value &value, NotificationRequest &request)
1281 {
1282     ANS_LOGD("Called.");
1283 
1284     napi_valuetype valuetype = napi_undefined;
1285     napi_value result = nullptr;
1286     bool hasProperty = false;
1287 
1288     NAPI_CALL(env, napi_has_named_property(env, value, "representativeBundle", &hasProperty));
1289     if (hasProperty) {
1290         napi_get_named_property(env, value, "representativeBundle", &result);
1291         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1292         if (valuetype != napi_object) {
1293             ANS_LOGE("Wrong argument type. Object expected.");
1294             std::string msg = "Incorrect parameter types. The type of representativeBundle must be object.";
1295             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1296             return nullptr;
1297         }
1298 
1299         std::shared_ptr<NotificationBundleOption> bundleOption = std::make_shared<NotificationBundleOption>();
1300         if (bundleOption == nullptr) {
1301             ANS_LOGE("The bundleOption is null.");
1302             return nullptr;
1303         }
1304         if (GetBundleOption(env, result, *bundleOption) == nullptr) {
1305             return nullptr;
1306         }
1307 
1308         request.SetBundleOption(bundleOption);
1309     }
1310 
1311     return NapiGetNull(env);
1312 }
1313 
GetNotificationTemplateInfo(const napi_env & env,const napi_value & value,std::shared_ptr<NotificationTemplate> & templ)1314 napi_value Common::GetNotificationTemplateInfo(const napi_env &env, const napi_value &value,
1315     std::shared_ptr<NotificationTemplate> &templ)
1316 {
1317     ANS_LOGD("enter");
1318 
1319     napi_valuetype valuetype = napi_undefined;
1320     napi_value result = nullptr;
1321     bool hasProperty = false;
1322     char str[STR_MAX_SIZE] = {0};
1323     size_t strLen = 0;
1324 
1325     // name: string
1326     NAPI_CALL(env, napi_has_named_property(env, value, "name", &hasProperty));
1327     if (!hasProperty) {
1328         ANS_LOGE("Property name expected.");
1329         return nullptr;
1330     }
1331     napi_get_named_property(env, value, "name", &result);
1332     NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1333     if (valuetype != napi_string) {
1334         ANS_LOGE("Wrong argument type. String expected.");
1335         std::string msg = "Incorrect parameter types. The type of name must be string.";
1336         Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1337         return nullptr;
1338     }
1339     NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
1340     std::string strInput = str;
1341     templ->SetTemplateName(strInput);
1342 
1343     // data?: {[key: string]: object}
1344     NAPI_CALL(env, napi_has_named_property(env, value, "data", &hasProperty));
1345     if (hasProperty) {
1346         napi_get_named_property(env, value, "data", &result);
1347         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1348         if (valuetype != napi_object) {
1349             ANS_LOGE("Wrong argument type. Object expected.");
1350             std::string msg = "Incorrect parameter types. The type of data must be object.";
1351             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1352             return nullptr;
1353         }
1354         AAFwk::WantParams wantParams;
1355         if (!OHOS::AppExecFwk::UnwrapWantParams(env, result, wantParams)) {
1356             return nullptr;
1357         }
1358 
1359         std::shared_ptr<AAFwk::WantParams> data = std::make_shared<AAFwk::WantParams>(wantParams);
1360         templ->SetTemplateData(data);
1361     }
1362 
1363     return NapiGetNull(env);
1364 }
1365 
SetNotificationTemplateInfo(const napi_env & env,const std::shared_ptr<NotificationTemplate> & templ,napi_value & result)1366 napi_value Common::SetNotificationTemplateInfo(
1367     const napi_env &env, const std::shared_ptr<NotificationTemplate> &templ, napi_value &result)
1368 {
1369     ANS_LOGD("enter");
1370 
1371     if (templ == nullptr) {
1372         ANS_LOGE("templ is null");
1373         return NapiGetBoolean(env, false);
1374     }
1375 
1376     napi_value value = nullptr;
1377 
1378     // name: string;
1379     napi_create_string_utf8(env, templ->GetTemplateName().c_str(), NAPI_AUTO_LENGTH, &value);
1380     napi_set_named_property(env, result, "name", value);
1381 
1382     std::shared_ptr<AAFwk::WantParams> data = templ->GetTemplateData();
1383     if (data) {
1384         value = OHOS::AppExecFwk::WrapWantParams(env, *data);
1385         napi_set_named_property(env, result, "data", value);
1386     }
1387 
1388     return NapiGetBoolean(env, true);
1389 }
1390 
SetNotificationEnableStatus(const napi_env & env,const NotificationBundleOption & bundleOption,napi_value & result)1391 napi_value Common::SetNotificationEnableStatus(
1392     const napi_env &env, const NotificationBundleOption &bundleOption, napi_value &result)
1393 {
1394     ANS_LOGD("Called.");
1395 
1396     // bundle: string
1397     napi_value bundleNapi = nullptr;
1398     napi_create_string_utf8(env, bundleOption.GetBundleName().c_str(), NAPI_AUTO_LENGTH, &bundleNapi);
1399     napi_set_named_property(env, result, "bundle", bundleNapi);
1400 
1401     // uid: uid_t
1402     napi_value uidNapi = nullptr;
1403     napi_create_int32(env, bundleOption.GetUid(), &uidNapi);
1404     napi_set_named_property(env, result, "uid", uidNapi);
1405 
1406     return result;
1407 }
1408 
SetNotificationFlags(const napi_env & env,const std::shared_ptr<NotificationFlags> & flags,napi_value & result)1409 napi_value Common::SetNotificationFlags(
1410     const napi_env &env, const std::shared_ptr<NotificationFlags> &flags, napi_value &result)
1411 {
1412     ANS_LOGD("enter");
1413 
1414     if (flags == nullptr) {
1415         ANS_LOGE("flags is null");
1416         return NapiGetBoolean(env, false);
1417     }
1418 
1419     napi_value value = nullptr;
1420 
1421     int32_t soundEnabled = static_cast<int32_t>(flags->IsSoundEnabled());
1422     napi_create_int32(env, soundEnabled, &value);
1423     napi_set_named_property(env, result, "soundEnabled", value);
1424 
1425     int32_t vibrationEnabled = static_cast<int32_t>(flags->IsVibrationEnabled());
1426     napi_create_int32(env, vibrationEnabled, &value);
1427     napi_set_named_property(env, result, "vibrationEnabled", value);
1428 
1429     uint32_t reminderFlags = flags->GetReminderFlags();
1430     napi_create_uint32(env, reminderFlags, &value);
1431     napi_set_named_property(env, result, "reminderFlags", value);
1432 
1433     return NapiGetBoolean(env, true);
1434 }
1435 
SetAgentBundle(const napi_env & env,const std::shared_ptr<NotificationBundleOption> & agentBundle,napi_value & result)1436 napi_value Common::SetAgentBundle(
1437     const napi_env &env, const std::shared_ptr<NotificationBundleOption> &agentBundle, napi_value &result)
1438 {
1439     ANS_LOGD("enter");
1440 
1441     if (agentBundle == nullptr) {
1442         ANS_LOGE("agentBundle is null");
1443         return NapiGetBoolean(env, false);
1444     }
1445 
1446     // bundle: string
1447     napi_value bundleNapi = nullptr;
1448     napi_create_string_utf8(env, agentBundle->GetBundleName().c_str(), NAPI_AUTO_LENGTH, &bundleNapi);
1449     napi_set_named_property(env, result, "bundle", bundleNapi);
1450 
1451     // uid: uid_t
1452     napi_value uidNapi = nullptr;
1453     napi_create_int32(env, agentBundle->GetUid(), &uidNapi);
1454     napi_set_named_property(env, result, "uid", uidNapi);
1455 
1456     return result;
1457 }
1458 
SetNotificationUnifiedGroupInfo(const napi_env & env,const std::shared_ptr<NotificationUnifiedGroupInfo> & info,napi_value & result)1459 napi_value Common::SetNotificationUnifiedGroupInfo(
1460     const napi_env &env, const std::shared_ptr<NotificationUnifiedGroupInfo> &info, napi_value &result)
1461 {
1462     ANS_LOGD("enter");
1463 
1464     if (info == nullptr) {
1465         ANS_LOGE("info is null");
1466         return NapiGetBoolean(env, false);
1467     }
1468 
1469     napi_value value = nullptr;
1470 
1471     // title?: string
1472     napi_create_string_utf8(env, info->GetTitle().c_str(), NAPI_AUTO_LENGTH, &value);
1473     napi_set_named_property(env, result, "title", value);
1474 
1475     // key?: string
1476     napi_create_string_utf8(env, info->GetKey().c_str(), NAPI_AUTO_LENGTH, &value);
1477     napi_set_named_property(env, result, "key", value);
1478 
1479     // content?: string
1480     napi_create_string_utf8(env, info->GetContent().c_str(), NAPI_AUTO_LENGTH, &value);
1481     napi_set_named_property(env, result, "content", value);
1482 
1483     // sceneName?: string
1484     napi_create_string_utf8(env, info->GetSceneName().c_str(), NAPI_AUTO_LENGTH, &value);
1485     napi_set_named_property(env, result, "sceneName", value);
1486 
1487     // extraInfo?: {[key:string] : any}
1488     std::shared_ptr<AAFwk::WantParams> extraInfoData = info->GetExtraInfo();
1489     if (extraInfoData) {
1490         napi_value extraInfo = nullptr;
1491         extraInfo = OHOS::AppExecFwk::WrapWantParams(env, *extraInfoData);
1492         napi_set_named_property(env, result, "extraInfo", extraInfo);
1493     }
1494 
1495     return NapiGetBoolean(env, true);
1496 }
1497 
SetBundleOption(const napi_env & env,const NotificationBundleOption & bundleInfo,napi_value & result)1498 napi_value Common::SetBundleOption(const napi_env &env, const NotificationBundleOption &bundleInfo,
1499     napi_value &result)
1500 {
1501     napi_value value = nullptr;
1502     // bundle: string
1503     napi_create_string_utf8(env, bundleInfo.GetBundleName().c_str(), NAPI_AUTO_LENGTH, &value);
1504     napi_set_named_property(env, result, "bundle", value);
1505 
1506     // uid: uid_t
1507     napi_create_int32(env, bundleInfo.GetUid(), &value);
1508     napi_set_named_property(env, result, "uid", value);
1509     return NapiGetBoolean(env, true);
1510 }
1511 
SetDoNotDisturbProfile(const napi_env & env,const NotificationDoNotDisturbProfile & data,napi_value & result)1512 napi_value Common::SetDoNotDisturbProfile(const napi_env &env, const NotificationDoNotDisturbProfile &data,
1513     napi_value &result)
1514 {
1515     ANS_LOGD("enter");
1516     napi_value value = nullptr;
1517     // id: number
1518     napi_create_int64(env, data.GetProfileId(), &value);
1519     napi_set_named_property(env, result, "id", value);
1520 
1521     // name: string
1522     napi_create_string_utf8(env, data.GetProfileName().c_str(), NAPI_AUTO_LENGTH, &value);
1523     napi_set_named_property(env, result, "name", value);
1524 
1525     size_t count = 0;
1526     napi_create_array(env, &value);
1527     // trustList?: std::vector<NotificationBundleOption>
1528     for (auto bundleInfo : data.GetProfileTrustList()) {
1529         napi_value bundleValue = nullptr;
1530         napi_create_object(env, &bundleValue);
1531         if (!Common::SetBundleOption(env, bundleInfo, bundleValue)) {
1532             continue;
1533         }
1534         napi_set_element(env, value, count, bundleValue);
1535         count++;
1536     }
1537     if (count > 0) {
1538         napi_set_named_property(env, result, "trustlist", value);
1539     }
1540     return NapiGetBoolean(env, true);
1541 }
1542 
GetAppInstanceKey()1543 std::string Common::GetAppInstanceKey()
1544 {
1545     std::shared_ptr<OHOS::AbilityRuntime::ApplicationContext>context =
1546         OHOS::AbilityRuntime::Context::GetApplicationContext();
1547     if (context != nullptr) {
1548         return context->GetCurrentInstanceKey();
1549     } else {
1550         ANS_LOGE("GetApplicationContext for instacekey fail.");
1551         return "";
1552     }
1553 }
1554 }  // namespace NotificationNapi
1555 }  // namespace OHOS
1556