1 /*
2 * Copyright (c) 2021-2023 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_LOGI("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,const NotificationSorting & sorting,napi_value & result)88 napi_value Common::SetNotificationSorting(const napi_env &env, const 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 (!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 (!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 (!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 return NapiGetBoolean(env, true);
204 }
205
SetDoNotDisturbDate(const napi_env & env,const NotificationDoNotDisturbDate & date,napi_value & result)206 napi_value Common::SetDoNotDisturbDate(
207 const napi_env &env, const NotificationDoNotDisturbDate &date, napi_value &result)
208 {
209 ANS_LOGD("enter");
210 DoNotDisturbType outType = DoNotDisturbType::TYPE_NONE;
211 if (!DoNotDisturbTypeCToJS(date.GetDoNotDisturbType(), outType)) {
212 return NapiGetBoolean(env, false);
213 }
214
215 // type:DoNotDisturbType
216 napi_value typeNapi = nullptr;
217 napi_create_int32(env, static_cast<int32_t>(outType), &typeNapi);
218 napi_set_named_property(env, result, "type", typeNapi);
219
220 // begin:Date
221 double begind = double(date.GetBeginDate());
222 napi_value beginNapi = nullptr;
223 napi_create_date(env, begind, &beginNapi);
224 napi_set_named_property(env, result, "begin", beginNapi);
225
226 // end:Date
227 double endd = double(date.GetEndDate());
228 napi_value endNapi = nullptr;
229 napi_create_date(env, endd, &endNapi);
230 napi_set_named_property(env, result, "end", endNapi);
231
232 return NapiGetBoolean(env, true);
233 }
234
SetEnabledNotificationCallbackData(const napi_env & env,const EnabledNotificationCallbackData & data,napi_value & result)235 napi_value Common::SetEnabledNotificationCallbackData(const napi_env &env, const EnabledNotificationCallbackData &data,
236 napi_value &result)
237 {
238 ANS_LOGD("enter");
239 // bundle: string
240 napi_value bundleNapi = nullptr;
241 napi_create_string_utf8(env, data.GetBundle().c_str(), NAPI_AUTO_LENGTH, &bundleNapi);
242 napi_set_named_property(env, result, "bundle", bundleNapi);
243
244 // uid: uid_t
245 napi_value uidNapi = nullptr;
246 napi_create_int32(env, data.GetUid(), &uidNapi);
247 napi_set_named_property(env, result, "uid", uidNapi);
248
249 // enable: bool
250 napi_value enableNapi = nullptr;
251 napi_get_boolean(env, data.GetEnable(), &enableNapi);
252 napi_set_named_property(env, result, "enable", enableNapi);
253
254 return NapiGetBoolean(env, true);
255 }
256
SetBadgeCallbackData(const napi_env & env,const BadgeNumberCallbackData & data,napi_value & result)257 napi_value Common::SetBadgeCallbackData(const napi_env &env, const BadgeNumberCallbackData &data,
258 napi_value &result)
259 {
260 ANS_LOGD("enter");
261 // bundle: string
262 napi_value bundleNapi = nullptr;
263 napi_create_string_utf8(env, data.GetBundle().c_str(), NAPI_AUTO_LENGTH, &bundleNapi);
264 napi_set_named_property(env, result, "bundle", bundleNapi);
265
266 // uid: int32_t
267 napi_value uidNapi = nullptr;
268 napi_create_int32(env, data.GetUid(), &uidNapi);
269 napi_set_named_property(env, result, "uid", uidNapi);
270
271 // badgeNumber: int32_t
272 napi_value badgeNapi = nullptr;
273 napi_create_int32(env, data.GetBadgeNumber(), &badgeNapi);
274 napi_set_named_property(env, result, "badgeNumber", badgeNapi);
275
276 return NapiGetBoolean(env, true);
277 }
278
GetNotificationSubscriberInfo(const napi_env & env,const napi_value & value,NotificationSubscribeInfo & subscriberInfo)279 napi_value Common::GetNotificationSubscriberInfo(
280 const napi_env &env, const napi_value &value, NotificationSubscribeInfo &subscriberInfo)
281 {
282 ANS_LOGD("enter");
283 uint32_t length = 0;
284 size_t strLen = 0;
285 bool hasProperty = false;
286 bool isArray = false;
287 napi_valuetype valuetype = napi_undefined;
288
289 // bundleNames?: Array<string>
290 NAPI_CALL(env, napi_has_named_property(env, value, "bundleNames", &hasProperty));
291 if (hasProperty) {
292 napi_value nBundleNames = nullptr;
293 napi_get_named_property(env, value, "bundleNames", &nBundleNames);
294 napi_is_array(env, nBundleNames, &isArray);
295 if (!isArray) {
296 ANS_LOGE("Property bundleNames is expected to be an array.");
297 return nullptr;
298 }
299 napi_get_array_length(env, nBundleNames, &length);
300 if (length == 0) {
301 ANS_LOGE("The array is empty.");
302 return nullptr;
303 }
304 for (uint32_t i = 0; i < length; ++i) {
305 napi_value nBundleName = nullptr;
306 char str[STR_MAX_SIZE] = {0};
307 napi_get_element(env, nBundleNames, i, &nBundleName);
308 NAPI_CALL(env, napi_typeof(env, nBundleName, &valuetype));
309 if (valuetype != napi_string) {
310 ANS_LOGE("Wrong argument type. String expected.");
311 return nullptr;
312 }
313 NAPI_CALL(env, napi_get_value_string_utf8(env, nBundleName, str, STR_MAX_SIZE - 1, &strLen));
314 subscriberInfo.bundleNames.emplace_back(str);
315 subscriberInfo.hasSubscribeInfo = true;
316 }
317 }
318
319 // userId?: number
320 NAPI_CALL(env, napi_has_named_property(env, value, "userId", &hasProperty));
321 if (hasProperty) {
322 napi_value nUserId = nullptr;
323 napi_get_named_property(env, value, "userId", &nUserId);
324 NAPI_CALL(env, napi_typeof(env, nUserId, &valuetype));
325 if (valuetype != napi_number) {
326 ANS_LOGE("Wrong argument type. Number expected.");
327 return nullptr;
328 }
329 NAPI_CALL(env, napi_get_value_int32(env, nUserId, &subscriberInfo.userId));
330 subscriberInfo.hasSubscribeInfo = true;
331 }
332
333 return NapiGetNull(env);
334 }
335
GetNotificationUserInput(const napi_env & env,const napi_value & actionButton,std::shared_ptr<NotificationActionButton> & pActionButton)336 napi_value Common::GetNotificationUserInput(
337 const napi_env &env, const napi_value &actionButton, std::shared_ptr<NotificationActionButton> &pActionButton)
338 {
339 ANS_LOGD("enter");
340 napi_valuetype valuetype = napi_undefined;
341 napi_value userInputResult = nullptr;
342 bool hasProperty = false;
343
344 // userInput?: NotificationUserInput
345 NAPI_CALL(env, napi_has_named_property(env, actionButton, "userInput", &hasProperty));
346 if (hasProperty) {
347 napi_get_named_property(env, actionButton, "userInput", &userInputResult);
348 NAPI_CALL(env, napi_typeof(env, userInputResult, &valuetype));
349 if (valuetype != napi_object) {
350 ANS_LOGE("Wrong argument type. Object expected.");
351 return nullptr;
352 }
353 std::shared_ptr<NotificationUserInput> userInput = nullptr;
354
355 if (!GetNotificationUserInputByInputKey(env, userInputResult, userInput)) {
356 return nullptr;
357 }
358 pActionButton->AddNotificationUserInput(userInput);
359 }
360
361 return NapiGetNull(env);
362 }
363
GetNotificationUserInputByInputKey(const napi_env & env,const napi_value & userInputResult,std::shared_ptr<NotificationUserInput> & userInput)364 napi_value Common::GetNotificationUserInputByInputKey(
365 const napi_env &env, const napi_value &userInputResult, std::shared_ptr<NotificationUserInput> &userInput)
366 {
367 ANS_LOGD("enter");
368 napi_valuetype valuetype = napi_undefined;
369 napi_value value = nullptr;
370 bool hasProperty = false;
371 char str[STR_MAX_SIZE] = {0};
372 size_t strLen = 0;
373
374 // inputKey: string
375 NAPI_CALL(env, napi_has_named_property(env, userInputResult, "inputKey", &hasProperty));
376 if (!hasProperty) {
377 ANS_LOGE("Property inputKey expected.");
378 return nullptr;
379 }
380 napi_get_named_property(env, userInputResult, "inputKey", &value);
381 NAPI_CALL(env, napi_typeof(env, value, &valuetype));
382 if (valuetype != napi_string) {
383 ANS_LOGE("Wrong argument type. String expected.");
384 return nullptr;
385 }
386 NAPI_CALL(env, napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen));
387 ANS_LOGI("NotificationUserInput::inputKey = %{public}s", str);
388 userInput = NotificationUserInput::Create(str);
389 if (!userInput) {
390 ANS_LOGI("Failed to create NotificationUserInput by inputKey=%{public}s", str);
391 return nullptr;
392 }
393
394 return NapiGetNull(env);
395 }
396
GetNotificationUserInputByTag(const napi_env & env,const napi_value & userInputResult,std::shared_ptr<NotificationUserInput> & userInput)397 napi_value Common::GetNotificationUserInputByTag(
398 const napi_env &env, const napi_value &userInputResult, std::shared_ptr<NotificationUserInput> &userInput)
399 {
400 ANS_LOGD("enter");
401
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 if (!userInput) {
409 ANS_LOGE("userInput is nullptr");
410 return nullptr;
411 }
412 // tag: string
413 NAPI_CALL(env, napi_has_named_property(env, userInputResult, "tag", &hasProperty));
414 if (!hasProperty) {
415 ANS_LOGE("Property tag expected.");
416 return nullptr;
417 }
418 napi_get_named_property(env, userInputResult, "tag", &value);
419 NAPI_CALL(env, napi_typeof(env, value, &valuetype));
420 if (valuetype != napi_string) {
421 ANS_LOGE("Wrong argument type. String expected.");
422 return nullptr;
423 }
424 NAPI_CALL(env, napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen));
425 userInput->SetTag(str);
426 ANS_LOGI("NotificationUserInput::tag = %{public}s", str);
427
428 return NapiGetNull(env);
429 }
430
GetNotificationUserInputByOptions(const napi_env & env,const napi_value & userInputResult,std::shared_ptr<NotificationUserInput> & userInput)431 napi_value Common::GetNotificationUserInputByOptions(
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 uint32_t length = 0;
440 size_t strLen = 0;
441 bool isArray = false;
442
443 if (!userInput) {
444 ANS_LOGE("userInput is nullptr");
445 return nullptr;
446 }
447
448 // options: Array<string>
449 NAPI_CALL(env, napi_has_named_property(env, userInputResult, "options", &hasProperty));
450
451 if (!hasProperty) {
452 ANS_LOGE("Property options expected.");
453 return nullptr;
454 }
455 napi_get_named_property(env, userInputResult, "options", &value);
456 napi_is_array(env, value, &isArray);
457 if (!isArray) {
458 ANS_LOGE("Property options is expected to be an array.");
459 return nullptr;
460 }
461 napi_get_array_length(env, value, &length);
462 if (length == 0) {
463 ANS_LOGE("The array is empty.");
464 return nullptr;
465 }
466 std::vector<std::string> options;
467 for (uint32_t i = 0; i < length; ++i) {
468 napi_value option = nullptr;
469 char str[STR_MAX_SIZE] = {0};
470 napi_get_element(env, value, i, &option);
471 NAPI_CALL(env, napi_typeof(env, option, &valuetype));
472 if (valuetype != napi_string) {
473 ANS_LOGE("Wrong argument type. String expected.");
474 return nullptr;
475 }
476 NAPI_CALL(env, napi_get_value_string_utf8(env, option, str, STR_MAX_SIZE - 1, &strLen));
477 options.emplace_back(str);
478 }
479 userInput->SetOptions(options);
480
481 return NapiGetNull(env);
482 }
483
GetNotificationUserInputByPermitMimeTypes(const napi_env & env,const napi_value & userInputResult,std::shared_ptr<NotificationUserInput> & userInput)484 napi_value Common::GetNotificationUserInputByPermitMimeTypes(
485 const napi_env &env, const napi_value &userInputResult, std::shared_ptr<NotificationUserInput> &userInput)
486 {
487 ANS_LOGD("enter");
488
489 napi_valuetype valuetype = napi_undefined;
490 napi_value value = nullptr;
491 bool hasProperty = false;
492 size_t strLen = 0;
493 uint32_t length = 0;
494 bool isArray = false;
495
496 if (!userInput) {
497 ANS_LOGE("userInput is nullptr");
498 return nullptr;
499 }
500
501 // permitMimeTypes?: Array<string>
502 NAPI_CALL(env, napi_has_named_property(env, userInputResult, "permitMimeTypes", &hasProperty));
503 if (hasProperty) {
504 napi_get_named_property(env, userInputResult, "permitMimeTypes", &value);
505 napi_is_array(env, value, &isArray);
506 if (!isArray) {
507 ANS_LOGE("Property permitMimeTypes is expected to be an array.");
508 return nullptr;
509 }
510 napi_get_array_length(env, value, &length);
511 if (length == 0) {
512 ANS_LOGE("The array is empty.");
513 return nullptr;
514 }
515 for (uint32_t i = 0; i < length; ++i) {
516 napi_value permitMimeType = nullptr;
517 char str[STR_MAX_SIZE] = {0};
518 napi_get_element(env, value, i, &permitMimeType);
519 NAPI_CALL(env, napi_typeof(env, permitMimeType, &valuetype));
520 if (valuetype != napi_string) {
521 ANS_LOGE("Wrong argument type. String expected.");
522 return nullptr;
523 }
524 NAPI_CALL(env, napi_get_value_string_utf8(env, permitMimeType, str, STR_MAX_SIZE - 1, &strLen));
525 userInput->SetPermitMimeTypes(str, true);
526 }
527 }
528
529 return NapiGetNull(env);
530 }
531
GetNotificationUserInputByPermitFreeFormInput(const napi_env & env,const napi_value & userInputResult,std::shared_ptr<NotificationUserInput> & userInput)532 napi_value Common::GetNotificationUserInputByPermitFreeFormInput(
533 const napi_env &env, const napi_value &userInputResult, std::shared_ptr<NotificationUserInput> &userInput)
534 {
535 ANS_LOGD("enter");
536 napi_value value = nullptr;
537 napi_valuetype valuetype = napi_undefined;
538 bool hasProperty = false;
539
540 if (!userInput) {
541 ANS_LOGE("userInput is nullptr");
542 return nullptr;
543 }
544
545 // permitFreeFormInput?: boolean
546 NAPI_CALL(env, napi_has_named_property(env, userInputResult, "permitFreeFormInput", &hasProperty));
547 if (hasProperty) {
548 bool permitFreeFormInput = false;
549 napi_get_named_property(env, userInputResult, "permitFreeFormInput", &value);
550 NAPI_CALL(env, napi_typeof(env, value, &valuetype));
551 if (valuetype != napi_boolean) {
552 ANS_LOGE("Wrong argument type. Bool expected.");
553 return nullptr;
554 }
555 napi_get_value_bool(env, value, &permitFreeFormInput);
556 ANS_LOGI("permitFreeFormInput is: %{public}d", permitFreeFormInput);
557 userInput->SetPermitFreeFormInput(permitFreeFormInput);
558 }
559
560 return NapiGetNull(env);
561 }
562
GetNotificationUserInputByEditType(const napi_env & env,const napi_value & userInputResult,std::shared_ptr<NotificationUserInput> & userInput)563 napi_value Common::GetNotificationUserInputByEditType(
564 const napi_env &env, const napi_value &userInputResult, std::shared_ptr<NotificationUserInput> &userInput)
565 {
566 ANS_LOGD("enter");
567 napi_value value = nullptr;
568 napi_valuetype valuetype = napi_undefined;
569 bool hasProperty = false;
570 int32_t editType = 0;
571
572 if (!userInput) {
573 ANS_LOGE("userInput is nullptr");
574 return nullptr;
575 }
576
577 // editType?: number
578 NAPI_CALL(env, napi_has_named_property(env, userInputResult, "editType", &hasProperty));
579 if (hasProperty) {
580 napi_get_named_property(env, userInputResult, "editType", &value);
581 NAPI_CALL(env, napi_typeof(env, value, &valuetype));
582 if (valuetype != napi_number) {
583 ANS_LOGE("Wrong argument type. Number expected.");
584 return nullptr;
585 }
586 napi_get_value_int32(env, value, &editType);
587 userInput->SetEditType(NotificationConstant::InputEditType(editType));
588 }
589 return NapiGetNull(env);
590 }
591
GetNotificationUserInputByAdditionalData(const napi_env & env,const napi_value & userInputResult,std::shared_ptr<NotificationUserInput> & userInput)592 napi_value Common::GetNotificationUserInputByAdditionalData(
593 const napi_env &env, const napi_value &userInputResult, std::shared_ptr<NotificationUserInput> &userInput)
594 {
595 ANS_LOGD("enter");
596
597 napi_valuetype valuetype = napi_undefined;
598 napi_value result = nullptr;
599 bool hasProperty = false;
600
601 if (!userInput) {
602 ANS_LOGE("userInput is nullptr");
603 return nullptr;
604 }
605
606 // additionalData?: {[key: string]: Object}
607 NAPI_CALL(env, napi_has_named_property(env, userInputResult, "additionalData", &hasProperty));
608 if (hasProperty) {
609 napi_get_named_property(env, userInputResult, "additionalData", &result);
610 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
611 if (valuetype != napi_object) {
612 ANS_LOGE("Wrong argument type. Object expected.");
613 return nullptr;
614 }
615 AAFwk::WantParams wantParams;
616 if (!OHOS::AppExecFwk::UnwrapWantParams(env, result, wantParams)) {
617 return nullptr;
618 }
619 userInput->AddAdditionalData(wantParams);
620 }
621
622 return NapiGetNull(env);
623 }
624
GetNotificationContentType(const napi_env & env,const napi_value & result,int32_t & type)625 napi_value Common::GetNotificationContentType(const napi_env &env, const napi_value &result, int32_t &type)
626 {
627 ANS_LOGD("enter");
628
629 napi_value contentResult = nullptr;
630 napi_valuetype valuetype = napi_undefined;
631 bool hasNotificationContentType = false;
632 bool hasContentType = false;
633
634 NAPI_CALL(env, napi_has_named_property(env, result, "notificationContentType", &hasNotificationContentType));
635 if (hasNotificationContentType) {
636 napi_get_named_property(env, result, "notificationContentType", &contentResult);
637 NAPI_CALL(env, napi_typeof(env, contentResult, &valuetype));
638 if (valuetype != napi_number) {
639 ANS_LOGE("Wrong argument type. Number expected.");
640 return nullptr;
641 }
642 napi_get_value_int32(env, contentResult, &type);
643
644 return NapiGetNull(env);
645 } else {
646 ANS_LOGE("Property notificationContentType expected.");
647 }
648
649 NAPI_CALL(env, napi_has_named_property(env, result, "contentType", &hasContentType));
650 if (hasContentType) {
651 napi_get_named_property(env, result, "contentType", &contentResult);
652 NAPI_CALL(env, napi_typeof(env, contentResult, &valuetype));
653 if (valuetype != napi_number) {
654 ANS_LOGE("Wrong argument type. Number expected.");
655 return nullptr;
656 }
657 napi_get_value_int32(env, contentResult, &type);
658
659 return NapiGetNull(env);
660 } else {
661 ANS_LOGE("Property contentType expected.");
662 return nullptr;
663 }
664 }
665
GetNotificationSlot(const napi_env & env,const napi_value & value,NotificationSlot & slot)666 napi_value Common::GetNotificationSlot(const napi_env &env, const napi_value &value, NotificationSlot &slot)
667 {
668 ANS_LOGD("enter");
669
670 napi_value nobj = nullptr;
671 napi_valuetype valuetype = napi_undefined;
672 bool hasType = false;
673 bool hasNotificationType = false;
674 int slotType = 0;
675
676 NAPI_CALL(env, napi_has_named_property(env, value, "notificationType", &hasNotificationType));
677 if (hasNotificationType) {
678 napi_get_named_property(env, value, "notificationType", &nobj);
679 NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
680 if (valuetype != napi_number) {
681 ANS_LOGE("Wrong argument type. Number expected.");
682 return nullptr;
683 }
684 } else {
685 ANS_LOGE("Property notificationType expected.");
686 }
687
688 NAPI_CALL(env, napi_has_named_property(env, value, "type", &hasType));
689 if (!hasNotificationType && hasType) {
690 napi_get_named_property(env, value, "type", &nobj);
691 NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
692 if (valuetype != napi_number) {
693 ANS_LOGE("Wrong argument type. Number expected.");
694 return nullptr;
695 }
696 } else {
697 ANS_LOGE("Property type expected.");
698 return nullptr;
699 }
700
701 if (nobj != nullptr) {
702 napi_get_value_int32(env, nobj, &slotType);
703 }
704
705 NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER;
706 if (!Common::SlotTypeJSToC(SlotType(slotType), outType)) {
707 return nullptr;
708 }
709 slot.SetType(outType);
710
711 if (GetNotificationSlotByString(env, value, slot) == nullptr) {
712 return nullptr;
713 }
714 if (GetNotificationSlotByNumber(env, value, slot) == nullptr) {
715 return nullptr;
716 }
717 if (GetNotificationSlotByVibration(env, value, slot) == nullptr) {
718 return nullptr;
719 }
720 if (GetNotificationSlotByBool(env, value, slot) == nullptr) {
721 return nullptr;
722 }
723 return NapiGetNull(env);
724 }
725
GetNotificationSlotByString(const napi_env & env,const napi_value & value,NotificationSlot & slot)726 napi_value Common::GetNotificationSlotByString(const napi_env &env, const napi_value &value, NotificationSlot &slot)
727 {
728 ANS_LOGD("enter");
729
730 napi_value nobj = nullptr;
731 napi_valuetype valuetype = napi_undefined;
732 bool hasProperty = false;
733 size_t strLen = 0;
734
735 // desc?: string
736 NAPI_CALL(env, napi_has_named_property(env, value, "desc", &hasProperty));
737 if (hasProperty) {
738 std::string desc;
739 char str[STR_MAX_SIZE] = {0};
740 napi_get_named_property(env, value, "desc", &nobj);
741 NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
742 if (valuetype != napi_string) {
743 ANS_LOGE("Wrong argument type. String expected.");
744 return nullptr;
745 }
746 NAPI_CALL(env, napi_get_value_string_utf8(env, nobj, str, STR_MAX_SIZE - 1, &strLen));
747 desc = str;
748 ANS_LOGI("desc is: %{public}s", desc.c_str());
749 slot.SetDescription(desc);
750 }
751
752 // sound?: string
753 NAPI_CALL(env, napi_has_named_property(env, value, "sound", &hasProperty));
754 if (hasProperty) {
755 std::string sound;
756 char str[STR_MAX_SIZE] = {0};
757 napi_get_named_property(env, value, "sound", &nobj);
758 NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
759 if (valuetype != napi_string) {
760 ANS_LOGE("Wrong argument type. String expected.");
761 return nullptr;
762 }
763 NAPI_CALL(env, napi_get_value_string_utf8(env, nobj, str, STR_MAX_SIZE - 1, &strLen));
764 sound = str;
765 ANS_LOGI("sound is: %{public}s", sound.c_str());
766 slot.SetSound(Uri(sound));
767 }
768
769 return NapiGetNull(env);
770 }
771
GetNotificationSlotByBool(const napi_env & env,const napi_value & value,NotificationSlot & slot)772 napi_value Common::GetNotificationSlotByBool(const napi_env &env, const napi_value &value, NotificationSlot &slot)
773 {
774 ANS_LOGD("enter");
775 napi_value nobj = nullptr;
776 napi_valuetype valuetype = napi_undefined;
777 bool hasProperty = false;
778
779 // badgeFlag?: boolean
780 NAPI_CALL(env, napi_has_named_property(env, value, "badgeFlag", &hasProperty));
781 if (hasProperty) {
782 bool badgeFlag = false;
783 napi_get_named_property(env, value, "badgeFlag", &nobj);
784 NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
785 if (valuetype != napi_boolean) {
786 ANS_LOGE("Wrong argument type. Bool expected.");
787 return nullptr;
788 }
789 napi_get_value_bool(env, nobj, &badgeFlag);
790 ANS_LOGI("badgeFlag is: %{public}d", badgeFlag);
791 slot.EnableBadge(badgeFlag);
792 }
793
794 // bypassDnd?: boolean
795 NAPI_CALL(env, napi_has_named_property(env, value, "bypassDnd", &hasProperty));
796 if (hasProperty) {
797 bool bypassDnd = false;
798 napi_get_named_property(env, value, "bypassDnd", &nobj);
799 NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
800 if (valuetype != napi_boolean) {
801 ANS_LOGE("Wrong argument type. Bool expected.");
802 return nullptr;
803 }
804 napi_get_value_bool(env, nobj, &bypassDnd);
805 ANS_LOGI("bypassDnd is: %{public}d", bypassDnd);
806 slot.EnableBypassDnd(bypassDnd);
807 }
808
809 // lightEnabled?: boolean
810 NAPI_CALL(env, napi_has_named_property(env, value, "lightEnabled", &hasProperty));
811 if (hasProperty) {
812 bool lightEnabled = false;
813 napi_get_named_property(env, value, "lightEnabled", &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, &lightEnabled);
820 ANS_LOGI("lightEnabled is: %{public}d", lightEnabled);
821 slot.SetEnableLight(lightEnabled);
822 }
823
824 return NapiGetNull(env);
825 }
826
GetNotificationSlotByNumber(const napi_env & env,const napi_value & value,NotificationSlot & slot)827 napi_value Common::GetNotificationSlotByNumber(const napi_env &env, const napi_value &value, NotificationSlot &slot)
828 {
829 ANS_LOGD("enter");
830
831 napi_value nobj = nullptr;
832 napi_valuetype valuetype = napi_undefined;
833 bool hasProperty = false;
834
835 // level?: number
836 NAPI_CALL(env, napi_has_named_property(env, value, "level", &hasProperty));
837 if (hasProperty) {
838 int inLevel = 0;
839 napi_get_named_property(env, value, "level", &nobj);
840 NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
841 if (valuetype != napi_number) {
842 ANS_LOGE("Wrong argument type. Number expected.");
843 return nullptr;
844 }
845 napi_get_value_int32(env, nobj, &inLevel);
846 ANS_LOGI("level is: %{public}d", inLevel);
847
848 NotificationSlot::NotificationLevel outLevel {NotificationSlot::NotificationLevel::LEVEL_NONE};
849 if (!Common::SlotLevelJSToC(SlotLevel(inLevel), outLevel)) {
850 return nullptr;
851 }
852 slot.SetLevel(outLevel);
853 }
854
855 // lockscreenVisibility?: number
856 NAPI_CALL(env, napi_has_named_property(env, value, "lockscreenVisibility", &hasProperty));
857 if (hasProperty) {
858 int lockscreenVisibility = 0;
859 napi_get_named_property(env, value, "lockscreenVisibility", &nobj);
860 NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
861 if (valuetype != napi_number) {
862 ANS_LOGE("Wrong argument type. Number expected.");
863 return nullptr;
864 }
865 napi_get_value_int32(env, nobj, &lockscreenVisibility);
866 ANS_LOGI("lockscreenVisibility is: %{public}d", lockscreenVisibility);
867 slot.SetLockscreenVisibleness(NotificationConstant::VisiblenessType(lockscreenVisibility));
868 }
869
870 // lightColor?: number
871 NAPI_CALL(env, napi_has_named_property(env, value, "lightColor", &hasProperty));
872 if (hasProperty) {
873 int lightColor = 0;
874 napi_get_named_property(env, value, "lightColor", &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, &lightColor);
881 ANS_LOGI("lightColor is: %{public}d", lightColor);
882 slot.SetLedLightColor(lightColor);
883 }
884 return NapiGetNull(env);
885 }
886
GetNotificationSlotByVibration(const napi_env & env,const napi_value & value,NotificationSlot & slot)887 napi_value Common::GetNotificationSlotByVibration(const napi_env &env, const napi_value &value, NotificationSlot &slot)
888 {
889 ANS_LOGD("enter");
890 napi_value nobj = nullptr;
891 napi_valuetype valuetype = napi_undefined;
892 bool hasProperty = false;
893 uint32_t length = 0;
894
895 // vibrationEnabled?: boolean
896 bool vibrationEnabled = false;
897 NAPI_CALL(env, napi_has_named_property(env, value, "vibrationEnabled", &hasProperty));
898 if (hasProperty) {
899 napi_get_named_property(env, value, "vibrationEnabled", &nobj);
900 NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
901 if (valuetype != napi_boolean) {
902 ANS_LOGE("Wrong argument type. Bool expected.");
903 return nullptr;
904 }
905
906 napi_get_value_bool(env, nobj, &vibrationEnabled);
907 slot.SetEnableVibration(vibrationEnabled);
908 }
909
910 if (!vibrationEnabled) {
911 return NapiGetNull(env);
912 }
913
914 // vibrationValues?: Array<number>
915 NAPI_CALL(env, napi_has_named_property(env, value, "vibrationValues", &hasProperty));
916 if (hasProperty) {
917 bool isArray = false;
918 napi_get_named_property(env, value, "vibrationValues", &nobj);
919 napi_is_array(env, nobj, &isArray);
920 if (!isArray) {
921 ANS_LOGE("Property vibrationValues is expected to be an array.");
922 return nullptr;
923 }
924
925 napi_get_array_length(env, nobj, &length);
926 std::vector<int64_t> vibrationValues;
927 for (size_t i = 0; i < length; i++) {
928 napi_value nVibrationValue = nullptr;
929 int64_t vibrationValue = 0;
930 napi_get_element(env, nobj, i, &nVibrationValue);
931 NAPI_CALL(env, napi_typeof(env, nVibrationValue, &valuetype));
932 if (valuetype != napi_number) {
933 ANS_LOGE("Wrong argument type. Number expected.");
934 return nullptr;
935 }
936 napi_get_value_int64(env, nVibrationValue, &vibrationValue);
937 vibrationValues.emplace_back(vibrationValue);
938 }
939 slot.SetVibrationStyle(vibrationValues);
940 }
941
942 return NapiGetNull(env);
943 }
944
GetBundleOption(const napi_env & env,const napi_value & value,NotificationBundleOption & option)945 napi_value Common::GetBundleOption(const napi_env &env, const napi_value &value, NotificationBundleOption &option)
946 {
947 ANS_LOGD("enter");
948
949 bool hasProperty {false};
950 napi_valuetype valuetype = napi_undefined;
951 napi_value result = nullptr;
952
953 char str[STR_MAX_SIZE] = {0};
954 size_t strLen = 0;
955 // bundle: string
956 NAPI_CALL(env, napi_has_named_property(env, value, "bundle", &hasProperty));
957 if (!hasProperty) {
958 ANS_LOGE("Property bundle expected.");
959 return nullptr;
960 }
961 napi_get_named_property(env, value, "bundle", &result);
962 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
963 if (valuetype != napi_string) {
964 ANS_LOGE("Wrong argument type. String expected.");
965 return nullptr;
966 }
967 NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
968 option.SetBundleName(str);
969
970 // uid?: number
971 NAPI_CALL(env, napi_has_named_property(env, value, "uid", &hasProperty));
972 if (hasProperty) {
973 int32_t uid = 0;
974 napi_get_named_property(env, value, "uid", &result);
975 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
976 if (valuetype != napi_number) {
977 ANS_LOGE("Wrong argument type. Number expected.");
978 return nullptr;
979 }
980 napi_get_value_int32(env, result, &uid);
981 option.SetUid(uid);
982 }
983
984 return NapiGetNull(env);
985 }
986
GetButtonOption(const napi_env & env,const napi_value & value,NotificationButtonOption & option)987 napi_value Common::GetButtonOption(const napi_env &env, const napi_value &value, NotificationButtonOption &option)
988 {
989 ANS_LOGD("enter");
990
991 bool hasProperty {false};
992 napi_valuetype valuetype = napi_undefined;
993 napi_value result = nullptr;
994
995 char str[STR_MAX_SIZE] = {0};
996 size_t strLen = 0;
997 // buttonName: string
998 NAPI_CALL(env, napi_has_named_property(env, value, "buttonName", &hasProperty));
999 if (!hasProperty) {
1000 ANS_LOGE("Property buttonName expected.");
1001 return nullptr;
1002 }
1003 napi_get_named_property(env, value, "buttonName", &result);
1004 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1005 if (valuetype != napi_string) {
1006 ANS_LOGE("Wrong argument type. String expected.");
1007 return nullptr;
1008 }
1009 NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
1010 option.SetButtonName(str);
1011
1012 return NapiGetNull(env);
1013 }
1014
GetHashCodes(const napi_env & env,const napi_value & value,std::vector<std::string> & hashCodes)1015 napi_value Common::GetHashCodes(const napi_env &env, const napi_value &value, std::vector<std::string> &hashCodes)
1016 {
1017 ANS_LOGD("enter");
1018 uint32_t length = 0;
1019 napi_get_array_length(env, value, &length);
1020 if (length == 0) {
1021 ANS_LOGE("The array is empty.");
1022 return nullptr;
1023 }
1024 napi_valuetype valuetype = napi_undefined;
1025 for (size_t i = 0; i < length; i++) {
1026 napi_value hashCode = nullptr;
1027 napi_get_element(env, value, i, &hashCode);
1028 NAPI_CALL(env, napi_typeof(env, hashCode, &valuetype));
1029 if (valuetype != napi_string) {
1030 ANS_LOGE("Wrong argument type. Object expected.");
1031 return nullptr;
1032 }
1033 char str[STR_MAX_SIZE] = {0};
1034 size_t strLen = 0;
1035 NAPI_CALL(env, napi_get_value_string_utf8(env, hashCode, str, STR_MAX_SIZE - 1, &strLen));
1036 hashCodes.emplace_back(str);
1037 }
1038
1039 return NapiGetNull(env);
1040 }
1041
GetNotificationKey(const napi_env & env,const napi_value & value,NotificationKey & key)1042 napi_value Common::GetNotificationKey(const napi_env &env, const napi_value &value, NotificationKey &key)
1043 {
1044 ANS_LOGD("enter");
1045
1046 bool hasProperty {false};
1047 napi_valuetype valuetype = napi_undefined;
1048 napi_value result = nullptr;
1049
1050 // id: number
1051 NAPI_CALL(env, napi_has_named_property(env, value, "id", &hasProperty));
1052 if (!hasProperty) {
1053 ANS_LOGE("Property id expected.");
1054 return nullptr;
1055 }
1056 napi_get_named_property(env, value, "id", &result);
1057 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1058 if (valuetype != napi_number) {
1059 ANS_LOGE("Wrong argument type. Number expected.");
1060 return nullptr;
1061 }
1062 napi_get_value_int32(env, result, &key.id);
1063
1064 // label?: string
1065 NAPI_CALL(env, napi_has_named_property(env, value, "label", &hasProperty));
1066 if (hasProperty) {
1067 char str[STR_MAX_SIZE] = {0};
1068 size_t strLen = 0;
1069 napi_get_named_property(env, value, "label", &result);
1070 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1071 if (valuetype != napi_string) {
1072 ANS_LOGE("Wrong argument type. String expected.");
1073 return nullptr;
1074 }
1075 NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
1076 key.label = str;
1077 }
1078
1079 return NapiGetNull(env);
1080 }
1081
IsValidRemoveReason(int32_t reasonType)1082 bool Common::IsValidRemoveReason(int32_t reasonType)
1083 {
1084 if (reasonType == NotificationConstant::CLICK_REASON_DELETE ||
1085 reasonType == NotificationConstant::CANCEL_REASON_DELETE) {
1086 return true;
1087 }
1088 ANS_LOGE("Reason %{public}d is an invalid value", reasonType);
1089 return false;
1090 }
1091
CreateWantAgentByJS(const napi_env & env,const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> & agent)1092 __attribute__((no_sanitize("cfi"))) napi_value Common::CreateWantAgentByJS(const napi_env &env,
1093 const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> &agent)
1094 {
1095 if (agent == nullptr) {
1096 ANS_LOGI("agent is nullptr");
1097 return nullptr;
1098 }
1099
1100 {
1101 std::lock_guard<std::mutex> lock(mutex_);
1102 wantAgent_.insert(agent);
1103 }
1104 napi_value wantAgent = nullptr;
1105 napi_value wantAgentClass = nullptr;
1106 napi_define_class(env,
1107 "wantAgentClass",
1108 NAPI_AUTO_LENGTH,
1109 [](napi_env env, napi_callback_info info) -> napi_value {
1110 napi_value thisVar = nullptr;
1111 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1112 return thisVar;
1113 },
1114 nullptr,
1115 0,
1116 nullptr,
1117 &wantAgentClass);
1118 napi_new_instance(env, wantAgentClass, 0, nullptr, &wantAgent);
1119 napi_wrap(env,
1120 wantAgent,
1121 (void *)agent.get(),
1122 [](napi_env env, void *data, void *hint) {
1123 AbilityRuntime::WantAgent::WantAgent *objectInfo =
1124 static_cast<AbilityRuntime::WantAgent::WantAgent *>(data);
1125 if (objectInfo) {
1126 std::lock_guard<std::mutex> lock(mutex_);
1127 for (auto it = wantAgent_.begin(); it != wantAgent_.end(); ++it) {
1128 if ((*it).get() == objectInfo) {
1129 wantAgent_.erase(it);
1130 break;
1131 }
1132 }
1133 }
1134 },
1135 nullptr,
1136 nullptr);
1137
1138 return wantAgent;
1139 }
1140
GetNotificationTemplate(const napi_env & env,const napi_value & value,NotificationRequest & request)1141 napi_value Common::GetNotificationTemplate(const napi_env &env, const napi_value &value, NotificationRequest &request)
1142 {
1143 ANS_LOGD("enter");
1144
1145 napi_valuetype valuetype = napi_undefined;
1146 napi_value result = nullptr;
1147 bool hasProperty = false;
1148
1149 NAPI_CALL(env, napi_has_named_property(env, value, "template", &hasProperty));
1150 if (hasProperty) {
1151 napi_get_named_property(env, value, "template", &result);
1152 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1153 if (valuetype != napi_object) {
1154 ANS_LOGE("Wrong argument type. Object expected.");
1155 return nullptr;
1156 }
1157
1158 std::shared_ptr<NotificationTemplate> templ = std::make_shared<NotificationTemplate>();
1159 if (templ == nullptr) {
1160 ANS_LOGE("template is null");
1161 return nullptr;
1162 }
1163 if (GetNotificationTemplateInfo(env, result, templ) == nullptr) {
1164 return nullptr;
1165 }
1166
1167 request.SetTemplate(templ);
1168 }
1169
1170 return NapiGetNull(env);
1171 }
1172
GetNotificationTemplateInfo(const napi_env & env,const napi_value & value,std::shared_ptr<NotificationTemplate> & templ)1173 napi_value Common::GetNotificationTemplateInfo(const napi_env &env, const napi_value &value,
1174 std::shared_ptr<NotificationTemplate> &templ)
1175 {
1176 ANS_LOGD("enter");
1177
1178 napi_valuetype valuetype = napi_undefined;
1179 napi_value result = nullptr;
1180 bool hasProperty = false;
1181 char str[STR_MAX_SIZE] = {0};
1182 size_t strLen = 0;
1183
1184 // name: string
1185 NAPI_CALL(env, napi_has_named_property(env, value, "name", &hasProperty));
1186 if (!hasProperty) {
1187 ANS_LOGE("Property name expected.");
1188 return nullptr;
1189 }
1190 napi_get_named_property(env, value, "name", &result);
1191 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1192 if (valuetype != napi_string) {
1193 ANS_LOGE("Wrong argument type. String expected.");
1194 return nullptr;
1195 }
1196 NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
1197 std::string strInput = str;
1198 templ->SetTemplateName(strInput);
1199
1200 // data?: {[key: string]: object}
1201 NAPI_CALL(env, napi_has_named_property(env, value, "data", &hasProperty));
1202 if (hasProperty) {
1203 napi_get_named_property(env, value, "data", &result);
1204 NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1205 if (valuetype != napi_object) {
1206 ANS_LOGE("Wrong argument type. Object expected.");
1207 return nullptr;
1208 }
1209 AAFwk::WantParams wantParams;
1210 if (!OHOS::AppExecFwk::UnwrapWantParams(env, result, wantParams)) {
1211 return nullptr;
1212 }
1213
1214 std::shared_ptr<AAFwk::WantParams> data = std::make_shared<AAFwk::WantParams>(wantParams);
1215 templ->SetTemplateData(data);
1216 }
1217
1218 return NapiGetNull(env);
1219 }
1220
SetNotificationTemplateInfo(const napi_env & env,const std::shared_ptr<NotificationTemplate> & templ,napi_value & result)1221 napi_value Common::SetNotificationTemplateInfo(
1222 const napi_env &env, const std::shared_ptr<NotificationTemplate> &templ, napi_value &result)
1223 {
1224 ANS_LOGD("enter");
1225
1226 if (templ == nullptr) {
1227 ANS_LOGE("templ is null");
1228 return NapiGetBoolean(env, false);
1229 }
1230
1231 napi_value value = nullptr;
1232
1233 // name: string;
1234 napi_create_string_utf8(env, templ->GetTemplateName().c_str(), NAPI_AUTO_LENGTH, &value);
1235 napi_set_named_property(env, result, "name", value);
1236
1237 std::shared_ptr<AAFwk::WantParams> data = templ->GetTemplateData();
1238 if (data) {
1239 value = OHOS::AppExecFwk::WrapWantParams(env, *data);
1240 napi_set_named_property(env, result, "data", value);
1241 }
1242
1243 return NapiGetBoolean(env, true);
1244 }
1245
SetNotificationFlags(const napi_env & env,const std::shared_ptr<NotificationFlags> & flags,napi_value & result)1246 napi_value Common::SetNotificationFlags(
1247 const napi_env &env, const std::shared_ptr<NotificationFlags> &flags, napi_value &result)
1248 {
1249 ANS_LOGD("enter");
1250
1251 if (flags == nullptr) {
1252 ANS_LOGE("flags is null");
1253 return NapiGetBoolean(env, false);
1254 }
1255
1256 napi_value value = nullptr;
1257
1258 int32_t soundEnabled = static_cast<int32_t>(flags->IsSoundEnabled());
1259 napi_create_int32(env, soundEnabled, &value);
1260 napi_set_named_property(env, result, "soundEnabled", value);
1261
1262 int32_t vibrationEnabled = static_cast<int32_t>(flags->IsVibrationEnabled());
1263 napi_create_int32(env, vibrationEnabled, &value);
1264 napi_set_named_property(env, result, "vibrationEnabled", value);
1265
1266 return NapiGetBoolean(env, true);
1267 }
1268 } // namespace NotificationNapi
1269 } // namespace OHOS
1270