• 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 ffrt::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("called");
47     if (sortingMap == nullptr) {
48         ANS_LOGD("null sortingMap");
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("called");
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("called");
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("called");
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("called");
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("called");
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("called");
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("called");
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("called");
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_LOGE("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("called");
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("called");
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("called");
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("called");
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("called");
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("called");
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("called");
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("called");
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("called");
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("called");
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("called");
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("called");
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("called");
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("called");
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 
GetBundleOption(const napi_env & env,const napi_value & value,std::shared_ptr<NotificationBundleOption> & option)1136 napi_value Common::GetBundleOption(
1137     const napi_env &env,
1138     const napi_value &value,
1139     std::shared_ptr<NotificationBundleOption> &option)
1140 {
1141     ANS_LOGD("called");
1142 
1143     bool hasProperty {false};
1144     napi_valuetype valuetype = napi_undefined;
1145     napi_value result = nullptr;
1146 
1147     char str[STR_MAX_SIZE] = {0};
1148     size_t strLen = 0;
1149     // bundle: string
1150     NAPI_CALL(env, napi_has_named_property(env, value, "bundle", &hasProperty));
1151     if (!hasProperty) {
1152         ANS_LOGE("Property bundle expected.");
1153         return nullptr;
1154     }
1155     napi_get_named_property(env, value, "bundle", &result);
1156     NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1157     if (valuetype != napi_string) {
1158         ANS_LOGE("Wrong argument type. String expected.");
1159         std::string msg = "Incorrect parameter types. The type of bundle must be string.";
1160         Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1161         return nullptr;
1162     }
1163     NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
1164     option->SetBundleName(str);
1165 
1166     // uid?: number
1167     NAPI_CALL(env, napi_has_named_property(env, value, "uid", &hasProperty));
1168     if (hasProperty) {
1169         int32_t uid = 0;
1170         napi_get_named_property(env, value, "uid", &result);
1171         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1172         if (valuetype != napi_number) {
1173             ANS_LOGE("Wrong argument type. Number expected.");
1174             return nullptr;
1175         }
1176         napi_get_value_int32(env, result, &uid);
1177         option->SetUid(uid);
1178     }
1179 
1180     return NapiGetNull(env);
1181 }
1182 
GetDistributedBundleOption(const napi_env & env,const napi_value & value,DistributedBundleOption & option)1183 napi_value Common::GetDistributedBundleOption(
1184     const napi_env &env,
1185     const napi_value &value,
1186     DistributedBundleOption &option)
1187 {
1188     ANS_LOGD("called");
1189 
1190     bool hasProperty {false};
1191     napi_valuetype valuetype = napi_undefined;
1192     napi_value result = nullptr;
1193 
1194     // bundle: NotificationBundleOption
1195     std::shared_ptr<NotificationBundleOption> bundleOption = std::make_shared<NotificationBundleOption>();
1196     if (bundleOption == nullptr) {
1197         ANS_LOGE("bundleOption is null");
1198         return nullptr;
1199     }
1200 
1201     // bundleName
1202     char str[STR_MAX_SIZE] = {0};
1203     size_t strLen = 0;
1204     napi_get_named_property(env, value, "bundleName", &result);
1205     NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1206     if (valuetype != napi_string) {
1207         ANS_LOGE("Wrong argument type. string expected.");
1208         std::string msg = "Incorrect parameter bundleName. The type of bundleName must be string.";
1209         Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1210         return nullptr;
1211     }
1212     NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
1213     bundleOption->SetBundleName(str);
1214 
1215     // uid
1216     int32_t uid = 0;
1217     napi_get_named_property(env, value, "uid", &result);
1218     NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1219     if (valuetype != napi_number) {
1220         ANS_LOGE("Wrong argument type. number expected.");
1221         std::string msg = "Incorrect parameter uid. The type of uid must be number.";
1222         Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1223         return nullptr;
1224     }
1225     napi_get_value_int32(env, result, &uid);
1226     bundleOption->SetUid(uid);
1227 
1228     option.SetBundle(bundleOption);
1229 
1230     // enable?: bool
1231     NAPI_CALL(env, napi_has_named_property(env, value, "enable", &hasProperty));
1232     if (hasProperty) {
1233         bool enable = false;
1234         napi_get_named_property(env, value, "enable", &result);
1235         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1236         if (valuetype != napi_boolean) {
1237             ANS_LOGE("Wrong argument type. boolean expected.");
1238             return nullptr;
1239         }
1240         napi_get_value_bool(env, result, &enable);
1241         option.SetEnable(enable);
1242     }
1243 
1244     return NapiGetNull(env);
1245 }
1246 
GetHashCodes(const napi_env & env,const napi_value & value,std::vector<std::string> & hashCodes)1247 napi_value Common::GetHashCodes(const napi_env &env, const napi_value &value, std::vector<std::string> &hashCodes)
1248 {
1249     ANS_LOGD("called");
1250     uint32_t length = 0;
1251     napi_get_array_length(env, value, &length);
1252     if (length == 0) {
1253         ANS_LOGE("The array is empty.");
1254         return nullptr;
1255     }
1256     napi_valuetype valuetype = napi_undefined;
1257     for (size_t i = 0; i < length; i++) {
1258         napi_value hashCode = nullptr;
1259         napi_get_element(env, value, i, &hashCode);
1260         NAPI_CALL(env, napi_typeof(env, hashCode, &valuetype));
1261         if (valuetype != napi_string) {
1262             ANS_LOGE("Wrong argument type. Object expected.");
1263             return nullptr;
1264         }
1265         char str[STR_MAX_SIZE] = {0};
1266         size_t strLen = 0;
1267         NAPI_CALL(env, napi_get_value_string_utf8(env, hashCode, str, STR_MAX_SIZE - 1, &strLen));
1268         hashCodes.emplace_back(str);
1269     }
1270 
1271     return NapiGetNull(env);
1272 }
1273 
GetNotificationKey(const napi_env & env,const napi_value & value,NotificationKey & key)1274 napi_value Common::GetNotificationKey(const napi_env &env, const napi_value &value, NotificationKey &key)
1275 {
1276     ANS_LOGD("called");
1277 
1278     bool hasProperty {false};
1279     napi_valuetype valuetype = napi_undefined;
1280     napi_value result = nullptr;
1281 
1282     // id: number
1283     NAPI_CALL(env, napi_has_named_property(env, value, "id", &hasProperty));
1284     if (!hasProperty) {
1285         ANS_LOGE("Property id expected.");
1286         return nullptr;
1287     }
1288     napi_get_named_property(env, value, "id", &result);
1289     NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1290     if (valuetype != napi_number) {
1291         ANS_LOGE("Wrong argument type. Number expected.");
1292         std::string msg = "Incorrect parameter types. The type of id must be number.";
1293         Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1294         return nullptr;
1295     }
1296     napi_get_value_int32(env, result, &key.id);
1297 
1298     // label?: string
1299     NAPI_CALL(env, napi_has_named_property(env, value, "label", &hasProperty));
1300     if (hasProperty) {
1301         char str[STR_MAX_SIZE] = {0};
1302         size_t strLen = 0;
1303         napi_get_named_property(env, value, "label", &result);
1304         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1305         if (valuetype != napi_string) {
1306             ANS_LOGE("Wrong argument type. String expected.");
1307             std::string msg = "Incorrect parameter types. The type of label must be string.";
1308             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1309             return nullptr;
1310         }
1311         NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
1312         key.label = str;
1313     }
1314 
1315     return NapiGetNull(env);
1316 }
1317 
IsValidRemoveReason(int32_t reasonType)1318 bool Common::IsValidRemoveReason(int32_t reasonType)
1319 {
1320     if (reasonType == NotificationConstant::CLICK_REASON_DELETE ||
1321         reasonType == NotificationConstant::CANCEL_REASON_DELETE) {
1322         return true;
1323     }
1324     ANS_LOGE("Reason %{public}d is an invalid value", reasonType);
1325     return false;
1326 }
1327 
CreateWantAgentByJS(const napi_env & env,const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> & agent)1328 __attribute__((no_sanitize("cfi"))) napi_value Common::CreateWantAgentByJS(const napi_env &env,
1329     const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> &agent)
1330 {
1331     if (agent == nullptr) {
1332         ANS_LOGE("agent is nullptr");
1333         return nullptr;
1334     }
1335 
1336     {
1337         std::lock_guard<ffrt::mutex> lock(mutex_);
1338         wantAgent_.insert(agent);
1339     }
1340     napi_finalize finalize = [](napi_env env, void *data, void *hint) {
1341         AbilityRuntime::WantAgent::WantAgent *objectInfo =
1342                 static_cast<AbilityRuntime::WantAgent::WantAgent *>(data);
1343             if (objectInfo) {
1344                 std::lock_guard<ffrt::mutex> lock(mutex_);
1345                 for (auto it = wantAgent_.begin(); it != wantAgent_.end(); ++it) {
1346                     if ((*it).get() == objectInfo) {
1347                         wantAgent_.erase(it);
1348                         break;
1349                     }
1350                 }
1351             }
1352     };
1353     return AppExecFwk::WrapWantAgent(env, agent.get(), finalize);
1354 }
1355 
GetNotificationTemplate(const napi_env & env,const napi_value & value,NotificationRequest & request)1356 napi_value Common::GetNotificationTemplate(const napi_env &env, const napi_value &value, NotificationRequest &request)
1357 {
1358     ANS_LOGD("called");
1359 
1360     napi_valuetype valuetype = napi_undefined;
1361     napi_value result = nullptr;
1362     bool hasProperty = false;
1363 
1364     NAPI_CALL(env, napi_has_named_property(env, value, "template", &hasProperty));
1365     if (hasProperty) {
1366         napi_get_named_property(env, value, "template", &result);
1367         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1368         if (valuetype != napi_object) {
1369             ANS_LOGE("Wrong argument type. Object expected.");
1370             std::string msg = "Incorrect parameter types. The type of template must be object.";
1371             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1372             return nullptr;
1373         }
1374 
1375         std::shared_ptr<NotificationTemplate> templ = std::make_shared<NotificationTemplate>();
1376         if (templ == nullptr) {
1377             ANS_LOGE("template is null");
1378             return nullptr;
1379         }
1380         if (GetNotificationTemplateInfo(env, result, templ) == nullptr) {
1381             return nullptr;
1382         }
1383 
1384         request.SetTemplate(templ);
1385     }
1386 
1387     return NapiGetNull(env);
1388 }
1389 
GetNotificationBundleOption(const napi_env & env,const napi_value & value,NotificationRequest & request)1390 napi_value Common::GetNotificationBundleOption(
1391     const napi_env &env, const napi_value &value, NotificationRequest &request)
1392 {
1393     ANS_LOGD("Called.");
1394 
1395     napi_valuetype valuetype = napi_undefined;
1396     napi_value result = nullptr;
1397     bool hasProperty = false;
1398 
1399     NAPI_CALL(env, napi_has_named_property(env, value, "representativeBundle", &hasProperty));
1400     if (hasProperty) {
1401         napi_get_named_property(env, value, "representativeBundle", &result);
1402         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1403         if (valuetype != napi_object) {
1404             ANS_LOGE("Wrong argument type. Object expected.");
1405             std::string msg = "Incorrect parameter types. The type of representativeBundle must be object.";
1406             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1407             return nullptr;
1408         }
1409 
1410         std::shared_ptr<NotificationBundleOption> bundleOption = std::make_shared<NotificationBundleOption>();
1411         if (bundleOption == nullptr) {
1412             ANS_LOGE("The bundleOption is null.");
1413             return nullptr;
1414         }
1415         if (GetBundleOption(env, result, *bundleOption) == nullptr) {
1416             return nullptr;
1417         }
1418 
1419         request.SetBundleOption(bundleOption);
1420     }
1421 
1422     return NapiGetNull(env);
1423 }
1424 
GetNotificationTemplateInfo(const napi_env & env,const napi_value & value,std::shared_ptr<NotificationTemplate> & templ)1425 napi_value Common::GetNotificationTemplateInfo(const napi_env &env, const napi_value &value,
1426     std::shared_ptr<NotificationTemplate> &templ)
1427 {
1428     ANS_LOGD("called");
1429 
1430     napi_valuetype valuetype = napi_undefined;
1431     napi_value result = nullptr;
1432     bool hasProperty = false;
1433     char str[STR_MAX_SIZE] = {0};
1434     size_t strLen = 0;
1435 
1436     // name: string
1437     NAPI_CALL(env, napi_has_named_property(env, value, "name", &hasProperty));
1438     if (!hasProperty) {
1439         ANS_LOGE("Property name expected.");
1440         return nullptr;
1441     }
1442     napi_get_named_property(env, value, "name", &result);
1443     NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1444     if (valuetype != napi_string) {
1445         ANS_LOGE("Wrong argument type. String expected.");
1446         std::string msg = "Incorrect parameter types. The type of name must be string.";
1447         Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1448         return nullptr;
1449     }
1450     NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
1451     std::string strInput = str;
1452     templ->SetTemplateName(strInput);
1453 
1454     // data?: {[key: string]: object}
1455     NAPI_CALL(env, napi_has_named_property(env, value, "data", &hasProperty));
1456     if (hasProperty) {
1457         napi_get_named_property(env, value, "data", &result);
1458         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1459         if (valuetype != napi_object) {
1460             ANS_LOGE("Wrong argument type. Object expected.");
1461             std::string msg = "Incorrect parameter types. The type of data must be object.";
1462             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
1463             return nullptr;
1464         }
1465         AAFwk::WantParams wantParams;
1466         if (!OHOS::AppExecFwk::UnwrapWantParams(env, result, wantParams)) {
1467             return nullptr;
1468         }
1469 
1470         std::shared_ptr<AAFwk::WantParams> data = std::make_shared<AAFwk::WantParams>(wantParams);
1471         templ->SetTemplateData(data);
1472     }
1473 
1474     return NapiGetNull(env);
1475 }
1476 
SetNotificationTemplateInfo(const napi_env & env,const std::shared_ptr<NotificationTemplate> & templ,napi_value & result)1477 napi_value Common::SetNotificationTemplateInfo(
1478     const napi_env &env, const std::shared_ptr<NotificationTemplate> &templ, napi_value &result)
1479 {
1480     ANS_LOGD("called");
1481 
1482     if (templ == nullptr) {
1483         ANS_LOGE("templ is null");
1484         return NapiGetBoolean(env, false);
1485     }
1486 
1487     napi_value value = nullptr;
1488 
1489     // name: string;
1490     napi_create_string_utf8(env, templ->GetTemplateName().c_str(), NAPI_AUTO_LENGTH, &value);
1491     napi_set_named_property(env, result, "name", value);
1492 
1493     std::shared_ptr<AAFwk::WantParams> data = templ->GetTemplateData();
1494     if (data) {
1495         value = OHOS::AppExecFwk::WrapWantParams(env, *data);
1496         napi_set_named_property(env, result, "data", value);
1497     }
1498 
1499     return NapiGetBoolean(env, true);
1500 }
1501 
SetNotificationEnableStatus(const napi_env & env,const NotificationBundleOption & bundleOption,napi_value & result)1502 napi_value Common::SetNotificationEnableStatus(
1503     const napi_env &env, const NotificationBundleOption &bundleOption, napi_value &result)
1504 {
1505     ANS_LOGD("Called.");
1506 
1507     // bundle: string
1508     napi_value bundleNapi = nullptr;
1509     napi_create_string_utf8(env, bundleOption.GetBundleName().c_str(), NAPI_AUTO_LENGTH, &bundleNapi);
1510     napi_set_named_property(env, result, "bundle", bundleNapi);
1511 
1512     // uid: uid_t
1513     napi_value uidNapi = nullptr;
1514     napi_create_int32(env, bundleOption.GetUid(), &uidNapi);
1515     napi_set_named_property(env, result, "uid", uidNapi);
1516 
1517     return result;
1518 }
1519 
SetNotificationFlags(const napi_env & env,const std::shared_ptr<NotificationFlags> & flags,napi_value & result)1520 napi_value Common::SetNotificationFlags(
1521     const napi_env &env, const std::shared_ptr<NotificationFlags> &flags, napi_value &result)
1522 {
1523     ANS_LOGD("called");
1524 
1525     if (flags == nullptr) {
1526         ANS_LOGE("flags is null");
1527         return NapiGetBoolean(env, false);
1528     }
1529 
1530     napi_value value = nullptr;
1531 
1532     int32_t soundEnabled = static_cast<int32_t>(flags->IsSoundEnabled());
1533     napi_create_int32(env, soundEnabled, &value);
1534     napi_set_named_property(env, result, "soundEnabled", value);
1535 
1536     int32_t vibrationEnabled = static_cast<int32_t>(flags->IsVibrationEnabled());
1537     napi_create_int32(env, vibrationEnabled, &value);
1538     napi_set_named_property(env, result, "vibrationEnabled", value);
1539 
1540     uint32_t reminderFlags = flags->GetReminderFlags();
1541     napi_create_uint32(env, reminderFlags, &value);
1542     napi_set_named_property(env, result, "reminderFlags", value);
1543 
1544     return NapiGetBoolean(env, true);
1545 }
1546 
SetAgentBundle(const napi_env & env,const std::shared_ptr<NotificationBundleOption> & agentBundle,napi_value & result)1547 napi_value Common::SetAgentBundle(
1548     const napi_env &env, const std::shared_ptr<NotificationBundleOption> &agentBundle, napi_value &result)
1549 {
1550     ANS_LOGD("called");
1551 
1552     if (agentBundle == nullptr) {
1553         ANS_LOGE("agentBundle is null");
1554         return NapiGetBoolean(env, false);
1555     }
1556 
1557     // bundle: string
1558     napi_value bundleNapi = nullptr;
1559     napi_create_string_utf8(env, agentBundle->GetBundleName().c_str(), NAPI_AUTO_LENGTH, &bundleNapi);
1560     napi_set_named_property(env, result, "bundle", bundleNapi);
1561 
1562     // uid: uid_t
1563     napi_value uidNapi = nullptr;
1564     napi_create_int32(env, agentBundle->GetUid(), &uidNapi);
1565     napi_set_named_property(env, result, "uid", uidNapi);
1566 
1567     return result;
1568 }
1569 
SetNotificationUnifiedGroupInfo(const napi_env & env,const std::shared_ptr<NotificationUnifiedGroupInfo> & info,napi_value & result)1570 napi_value Common::SetNotificationUnifiedGroupInfo(
1571     const napi_env &env, const std::shared_ptr<NotificationUnifiedGroupInfo> &info, napi_value &result)
1572 {
1573     ANS_LOGD("called");
1574 
1575     if (info == nullptr) {
1576         ANS_LOGE("info is null");
1577         return NapiGetBoolean(env, false);
1578     }
1579 
1580     napi_value value = nullptr;
1581 
1582     // title?: string
1583     napi_create_string_utf8(env, info->GetTitle().c_str(), NAPI_AUTO_LENGTH, &value);
1584     napi_set_named_property(env, result, "title", value);
1585 
1586     // key?: string
1587     napi_create_string_utf8(env, info->GetKey().c_str(), NAPI_AUTO_LENGTH, &value);
1588     napi_set_named_property(env, result, "key", value);
1589 
1590     // content?: string
1591     napi_create_string_utf8(env, info->GetContent().c_str(), NAPI_AUTO_LENGTH, &value);
1592     napi_set_named_property(env, result, "content", value);
1593 
1594     // sceneName?: string
1595     napi_create_string_utf8(env, info->GetSceneName().c_str(), NAPI_AUTO_LENGTH, &value);
1596     napi_set_named_property(env, result, "sceneName", value);
1597 
1598     // extraInfo?: {[key:string] : any}
1599     std::shared_ptr<AAFwk::WantParams> extraInfoData = info->GetExtraInfo();
1600     if (extraInfoData) {
1601         napi_value extraInfo = nullptr;
1602         extraInfo = OHOS::AppExecFwk::WrapWantParams(env, *extraInfoData);
1603         napi_set_named_property(env, result, "extraInfo", extraInfo);
1604     }
1605 
1606     return NapiGetBoolean(env, true);
1607 }
1608 
SetBundleOption(const napi_env & env,const NotificationBundleOption & bundleInfo,napi_value & result)1609 napi_value Common::SetBundleOption(const napi_env &env, const NotificationBundleOption &bundleInfo,
1610     napi_value &result)
1611 {
1612     napi_value value = nullptr;
1613     // bundle: string
1614     napi_create_string_utf8(env, bundleInfo.GetBundleName().c_str(), NAPI_AUTO_LENGTH, &value);
1615     napi_set_named_property(env, result, "bundle", value);
1616 
1617     // uid: uid_t
1618     napi_create_int32(env, bundleInfo.GetUid(), &value);
1619     napi_set_named_property(env, result, "uid", value);
1620     return NapiGetBoolean(env, true);
1621 }
1622 
SetDoNotDisturbProfile(const napi_env & env,const NotificationDoNotDisturbProfile & data,napi_value & result)1623 napi_value Common::SetDoNotDisturbProfile(const napi_env &env, const NotificationDoNotDisturbProfile &data,
1624     napi_value &result)
1625 {
1626     ANS_LOGD("called");
1627     napi_value value = nullptr;
1628     // id: number
1629     napi_create_int64(env, data.GetProfileId(), &value);
1630     napi_set_named_property(env, result, "id", value);
1631 
1632     // name: string
1633     napi_create_string_utf8(env, data.GetProfileName().c_str(), NAPI_AUTO_LENGTH, &value);
1634     napi_set_named_property(env, result, "name", value);
1635 
1636     size_t count = 0;
1637     napi_create_array(env, &value);
1638     // trustList?: std::vector<NotificationBundleOption>
1639     for (auto bundleInfo : data.GetProfileTrustList()) {
1640         napi_value bundleValue = nullptr;
1641         napi_create_object(env, &bundleValue);
1642         if (!Common::SetBundleOption(env, bundleInfo, bundleValue)) {
1643             continue;
1644         }
1645         napi_set_element(env, value, count, bundleValue);
1646         count++;
1647     }
1648     if (count > 0) {
1649         napi_set_named_property(env, result, "trustlist", value);
1650     }
1651     return NapiGetBoolean(env, true);
1652 }
1653 
GetAppInstanceKey()1654 std::string Common::GetAppInstanceKey()
1655 {
1656     std::shared_ptr<OHOS::AbilityRuntime::ApplicationContext>context =
1657         OHOS::AbilityRuntime::Context::GetApplicationContext();
1658     if (context != nullptr) {
1659         return context->GetCurrentInstanceKey();
1660     } else {
1661         ANS_LOGE("GetApplicationContext for instacekey fail.");
1662         return "";
1663     }
1664 }
1665 }  // namespace NotificationNapi
1666 }  // namespace OHOS
1667