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