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