• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "napi_common.h"
19 #include "pixel_map_napi.h"
20 
21 namespace OHOS {
22 namespace NotificationNapi {
23 std::set<std::shared_ptr<AbilityRuntime::WantAgent::WantAgent>> Common::wantAgent_;
24 
25 namespace {
26 static const std::unordered_map<int32_t, std::string> ERROR_CODE_MESSAGE {
27     {ERROR_PERMISSION_DENIED, "Permission denied"},
28     {ERROR_PARAM_INVALID, "Invalid parameter"},
29     {ERROR_SYSTEM_CAP_ERROR, "SystemCapability not found"},
30     {ERROR_INTERNAL_ERROR, "Internal error"},
31     {ERROR_IPC_ERROR, "Marshalling or unmarshalling error"},
32     {ERROR_SERVICE_CONNECT_ERROR, "Failed to connect service"},
33     {ERROR_NOTIFICATION_CLOSED, "Notification is not enabled"},
34     {ERROR_SLOT_CLOSED, "Notification slot is not enabled"},
35     {ERROR_NOTIFICATION_UNREMOVABLE, "Notification is not allowed to remove"},
36     {ERROR_NOTIFICATION_NOT_EXIST, "The notification is not exist"},
37     {ERROR_USER_NOT_EXIST, "The user is not exist"},
38     {ERROR_OVER_MAX_NUM_PER_SECOND, "Over max number notifications per second"},
39     {ERROR_DISTRIBUTED_OPERATION_FAILED, "Distributed operation failed"},
40     {ERROR_READ_TEMPLATE_CONFIG_FAILED, "Read template config failed"},
41     {ERROR_BUNDLE_NOT_FOUND, "The specified bundle name was not found"},
42 };
43 }
44 
Common()45 Common::Common()
46 {}
47 
~Common()48 Common::~Common()
49 {}
50 
NapiGetBoolean(napi_env env,const bool & isValue)51 napi_value Common::NapiGetBoolean(napi_env env, const bool &isValue)
52 {
53     napi_value result = nullptr;
54     napi_get_boolean(env, isValue, &result);
55     return result;
56 }
57 
NapiGetNull(napi_env env)58 napi_value Common::NapiGetNull(napi_env env)
59 {
60     napi_value result = nullptr;
61     napi_get_null(env, &result);
62     return result;
63 }
64 
NapiGetUndefined(napi_env env)65 napi_value Common::NapiGetUndefined(napi_env env)
66 {
67     napi_value result = nullptr;
68     napi_get_undefined(env, &result);
69     return result;
70 }
71 
CreateErrorValue(napi_env env,int32_t errCode,bool newType)72 napi_value Common::CreateErrorValue(napi_env env, int32_t errCode, bool newType)
73 {
74     ANS_LOGI("enter, errorCode[%{public}d]", errCode);
75     napi_value error = Common::NapiGetNull(env);
76     if (errCode == ERR_OK && newType) {
77         return error;
78     }
79 
80     napi_value code = nullptr;
81     napi_create_int32(env, errCode, &code);
82 
83     auto iter = ERROR_CODE_MESSAGE.find(errCode);
84     std::string errMsg = iter != ERROR_CODE_MESSAGE.end() ? iter->second : "";
85     napi_value message = nullptr;
86     napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &message);
87 
88     napi_create_error(env, nullptr, message, &error);
89     napi_set_named_property(env, error, "code", code);
90     return error;
91 }
92 
NapiThrow(napi_env env,int32_t errCode)93 void Common::NapiThrow(napi_env env, int32_t errCode)
94 {
95     ANS_LOGI("enter");
96 
97     napi_throw(env, CreateErrorValue(env, errCode, true));
98 }
99 
GetCallbackErrorValue(napi_env env,int32_t errCode)100 napi_value Common::GetCallbackErrorValue(napi_env env, int32_t errCode)
101 {
102     napi_value result = nullptr;
103     napi_value eCode = nullptr;
104     NAPI_CALL(env, napi_create_int32(env, errCode, &eCode));
105     NAPI_CALL(env, napi_create_object(env, &result));
106     NAPI_CALL(env, napi_set_named_property(env, result, "code", eCode));
107     return result;
108 }
109 
PaddingCallbackPromiseInfo(const napi_env & env,const napi_ref & callback,CallbackPromiseInfo & info,napi_value & promise)110 void Common::PaddingCallbackPromiseInfo(
111     const napi_env &env, const napi_ref &callback, CallbackPromiseInfo &info, napi_value &promise)
112 {
113     ANS_LOGI("enter");
114 
115     if (callback) {
116         info.callback = callback;
117         info.isCallback = true;
118     } else {
119         napi_deferred deferred = nullptr;
120         NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise));
121         info.deferred = deferred;
122         info.isCallback = false;
123     }
124 }
125 
ReturnCallbackPromise(const napi_env & env,const CallbackPromiseInfo & info,const napi_value & result)126 void Common::ReturnCallbackPromise(const napi_env &env, const CallbackPromiseInfo &info, const napi_value &result)
127 {
128     ANS_LOGI("enter errorCode=%{public}d", info.errorCode);
129     if (info.isCallback) {
130         SetCallback(env, info.callback, info.errorCode, result, false);
131     } else {
132         SetPromise(env, info.deferred, info.errorCode, result, false);
133     }
134     ANS_LOGI("end");
135 }
136 
SetCallback(const napi_env & env,const napi_ref & callbackIn,const int32_t & errorCode,const napi_value & result,bool newType)137 void Common::SetCallback(
138     const napi_env &env, const napi_ref &callbackIn, const int32_t &errorCode, const napi_value &result, bool newType)
139 {
140     ANS_LOGI("enter");
141     napi_value undefined = nullptr;
142     napi_get_undefined(env, &undefined);
143 
144     napi_value callback = nullptr;
145     napi_value resultout = nullptr;
146     napi_get_reference_value(env, callbackIn, &callback);
147     napi_value results[ARGS_TWO] = {nullptr};
148     results[PARAM0] = CreateErrorValue(env, errorCode, newType);
149     results[PARAM1] = result;
150     NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &results[PARAM0], &resultout));
151     ANS_LOGI("end");
152 }
153 
SetCallback(const napi_env & env,const napi_ref & callbackIn,const napi_value & result)154 void Common::SetCallback(
155     const napi_env &env, const napi_ref &callbackIn, const napi_value &result)
156 {
157     ANS_LOGI("enter");
158     napi_value undefined = nullptr;
159     napi_get_undefined(env, &undefined);
160 
161     napi_value callback = nullptr;
162     napi_value resultout = nullptr;
163     napi_get_reference_value(env, callbackIn, &callback);
164     NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_ONE, &result, &resultout));
165     ANS_LOGI("end");
166 }
167 
SetPromise(const napi_env & env,const napi_deferred & deferred,const int32_t & errorCode,const napi_value & result,bool newType)168 void Common::SetPromise(const napi_env &env,
169     const napi_deferred &deferred, const int32_t &errorCode, const napi_value &result, bool newType)
170 {
171     ANS_LOGI("enter");
172     if (errorCode == ERR_OK) {
173         napi_resolve_deferred(env, deferred, result);
174     } else {
175         napi_reject_deferred(env, deferred, CreateErrorValue(env, errorCode, newType));
176     }
177     ANS_LOGI("end");
178 }
179 
JSParaError(const napi_env & env,const napi_ref & callback)180 napi_value Common::JSParaError(const napi_env &env, const napi_ref &callback)
181 {
182     if (callback) {
183         return Common::NapiGetNull(env);
184     }
185     napi_value promise = nullptr;
186     napi_deferred deferred = nullptr;
187     napi_create_promise(env, &deferred, &promise);
188     SetPromise(env, deferred, ERROR, Common::NapiGetNull(env), false);
189     return promise;
190 }
191 
ParseParaOnlyCallback(const napi_env & env,const napi_callback_info & info,napi_ref & callback)192 napi_value Common::ParseParaOnlyCallback(const napi_env &env, const napi_callback_info &info, napi_ref &callback)
193 {
194     ANS_LOGI("enter");
195 
196     size_t argc = ONLY_CALLBACK_MAX_PARA;
197     napi_value argv[ONLY_CALLBACK_MAX_PARA] = {nullptr};
198     napi_value thisVar = nullptr;
199     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
200     if (argc < ONLY_CALLBACK_MIN_PARA) {
201         ANS_LOGE("Wrong number of arguments");
202         return nullptr;
203     }
204 
205     // argv[0]:callback
206     napi_valuetype valuetype = napi_undefined;
207     if (argc >= ONLY_CALLBACK_MAX_PARA) {
208         NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
209         if (valuetype != napi_function) {
210             ANS_LOGE("Wrong argument type. Function expected.");
211             return nullptr;
212         }
213         napi_create_reference(env, argv[PARAM0], 1, &callback);
214     }
215 
216     return Common::NapiGetNull(env);
217 }
218 
SetNotificationByDistributedOptions(const napi_env & env,const OHOS::Notification::Notification * notification,napi_value & result)219 napi_value Common::SetNotificationByDistributedOptions(
220     const napi_env &env, const OHOS::Notification::Notification *notification, napi_value &result)
221 {
222     ANS_LOGI("enter");
223     if (notification == nullptr) {
224         ANS_LOGE("notification is nullptr");
225         return NapiGetBoolean(env, false);
226     }
227 
228     NotificationDistributedOptions options = notification->GetNotificationRequest().GetNotificationDistributedOptions();
229     napi_value value = nullptr;
230     // isDistributed?: boolean
231     if (notification->GetDeviceId().empty()) {
232         napi_get_boolean(env, false, &value);
233     } else {
234         napi_get_boolean(env, options.IsDistributed(), &value);
235     }
236     napi_set_named_property(env, result, "isDistributed", value);
237 
238     // supportDisplayDevices?: Array<string>
239     uint32_t count = 0;
240     napi_value arrSupportDisplayDevices = nullptr;
241     napi_create_array(env, &arrSupportDisplayDevices);
242     std::vector<std::string> displayDevices = options.GetDevicesSupportDisplay();
243     for (auto vec : displayDevices) {
244         napi_value vecValue = nullptr;
245         ANS_LOGI("supportDisplayDevices = %{public}s", vec.c_str());
246         napi_create_string_utf8(env, vec.c_str(), NAPI_AUTO_LENGTH, &vecValue);
247         napi_set_element(env, arrSupportDisplayDevices, count, vecValue);
248         count++;
249     }
250     napi_set_named_property(env, result, "supportDisplayDevices", arrSupportDisplayDevices);
251 
252     // supportOperateDevices?: Array<string>
253     count = 0;
254     napi_value arrSupportOperateDevices = nullptr;
255     napi_create_array(env, &arrSupportOperateDevices);
256     std::vector<std::string> operateDevices = options.GetDevicesSupportOperate();
257     for (auto vec : operateDevices) {
258         napi_value vecValue = nullptr;
259         ANS_LOGI("supportOperateDevices  = %{public}s", vec.c_str());
260         napi_create_string_utf8(env, vec.c_str(), NAPI_AUTO_LENGTH, &vecValue);
261         napi_set_element(env, arrSupportOperateDevices, count, vecValue);
262         count++;
263     }
264     napi_set_named_property(env, result, "supportOperateDevices", arrSupportOperateDevices);
265 
266     // readonly remindType?: number
267     enum DeviceRemindType outType = DeviceRemindType::IDLE_DONOT_REMIND;
268     if (!DeviceRemindTypeCToJS(notification->GetRemindType(), outType)) {
269         return NapiGetBoolean(env, false);
270     }
271     napi_create_int32(env, (int32_t)outType, &value);
272     napi_set_named_property(env, result, "remindType", value);
273 
274     return NapiGetBoolean(env, true);
275 }
276 
SetNotification(const napi_env & env,const OHOS::Notification::Notification * notification,napi_value & result)277 napi_value Common::SetNotification(
278     const napi_env &env, const OHOS::Notification::Notification *notification, napi_value &result)
279 {
280     ANS_LOGI("enter");
281 
282     if (notification == nullptr) {
283         ANS_LOGE("notification is nullptr");
284         return NapiGetBoolean(env, false);
285     }
286     napi_value value = nullptr;
287     NotificationRequest request = notification->GetNotificationRequest();
288     if (!SetNotificationRequest(env, &request, result)) {
289         return NapiGetBoolean(env, false);
290     }
291 
292     // hashCode?: string
293     napi_create_string_utf8(env, notification->GetKey().c_str(), NAPI_AUTO_LENGTH, &value);
294     napi_set_named_property(env, result, "hashCode", value);
295 
296     // isFloatingIcon ?: boolean
297     napi_get_boolean(env, notification->IsFloatingIcon(), &value);
298     napi_set_named_property(env, result, "isFloatingIcon", value);
299 
300     if (notification->GetNotificationRequest().IsAgentNotification()) {
301         // Agent notification, replace creator with owner
302         // readonly creatorBundleName?: string
303         napi_create_string_utf8(
304             env, notification->GetNotificationRequest().GetOwnerBundleName().c_str(), NAPI_AUTO_LENGTH, &value);
305         napi_set_named_property(env, result, "creatorBundleName", value);
306 
307         // readonly creatorUid?: number
308         napi_create_int32(env, notification->GetNotificationRequest().GetOwnerUid(), &value);
309         napi_set_named_property(env, result, "creatorUid", value);
310 
311         // readonly creatorUserId?: number
312         napi_create_int32(env, notification->GetNotificationRequest().GetOwnerUserId(), &value);
313         napi_set_named_property(env, result, "creatorUserId", value);
314     } else {
315         // readonly creatorBundleName?: string
316         napi_create_string_utf8(env, notification->GetCreateBundle().c_str(), NAPI_AUTO_LENGTH, &value);
317         napi_set_named_property(env, result, "creatorBundleName", value);
318 
319         // readonly creatorUid?: number
320         napi_create_int32(env, notification->GetUid(), &value);
321         napi_set_named_property(env, result, "creatorUid", value);
322 
323         // readonly creatorUserId?: number
324         napi_create_int32(env, notification->GetUserId(), &value);
325         napi_set_named_property(env, result, "creatorUserId", value);
326     }
327 
328     // readonly creatorPid?: number
329     napi_create_int32(env, notification->GetPid(), &value);
330     napi_set_named_property(env, result, "creatorPid", value);
331 
332     // distributedOption?:DistributedOptions
333     napi_value distributedResult = nullptr;
334     napi_create_object(env, &distributedResult);
335     if (!SetNotificationByDistributedOptions(env, notification, distributedResult)) {
336         return NapiGetBoolean(env, false);
337     }
338     napi_set_named_property(env, result, "distributedOption", distributedResult);
339 
340     // readonly isRemoveAllowed?: boolean
341     napi_get_boolean(env, notification->IsRemoveAllowed(), &value);
342     napi_set_named_property(env, result, "isRemoveAllowed", value);
343 
344     // readonly source?: number
345     SourceType sourceType = SourceType::TYPE_NORMAL;
346     if (!SourceTypeCToJS(notification->GetSourceType(), sourceType)) {
347         return NapiGetBoolean(env, false);
348     }
349     napi_create_int32(env, (int32_t)sourceType, &value);
350     napi_set_named_property(env, result, "source", value);
351 
352     // readonly deviceId?: string
353     napi_create_string_utf8(env, notification->GetDeviceId().c_str(), NAPI_AUTO_LENGTH, &value);
354     napi_set_named_property(env, result, "deviceId", value);
355 
356     return NapiGetBoolean(env, true);
357 }
358 
SetNotificationRequestByString(const napi_env & env,const OHOS::Notification::NotificationRequest * request,napi_value & result)359 napi_value Common::SetNotificationRequestByString(
360     const napi_env &env, const OHOS::Notification::NotificationRequest *request, napi_value &result)
361 {
362     ANS_LOGI("enter");
363 
364     napi_value value = nullptr;
365 
366     if (request == nullptr) {
367         ANS_LOGE("request is nullptr");
368         return NapiGetBoolean(env, false);
369     }
370 
371     // classification?: string
372     napi_create_string_utf8(env, request->GetClassification().c_str(), NAPI_AUTO_LENGTH, &value);
373     napi_set_named_property(env, result, "classification", value);
374 
375     // statusBarText?: string
376     napi_create_string_utf8(env, request->GetStatusBarText().c_str(), NAPI_AUTO_LENGTH, &value);
377     napi_set_named_property(env, result, "statusBarText", value);
378 
379     // label?: string
380     napi_create_string_utf8(env, request->GetLabel().c_str(), NAPI_AUTO_LENGTH, &value);
381     napi_set_named_property(env, result, "label", value);
382 
383     // groupName?: string
384     napi_create_string_utf8(env, request->GetGroupName().c_str(), NAPI_AUTO_LENGTH, &value);
385     napi_set_named_property(env, result, "groupName", value);
386 
387     // readonly creatorBundleName?: string
388     napi_create_string_utf8(env, request->GetCreatorBundleName().c_str(), NAPI_AUTO_LENGTH, &value);
389     napi_set_named_property(env, result, "creatorBundleName", value);
390 
391     return NapiGetBoolean(env, true);
392 }
393 
SetNotificationRequestByNumber(const napi_env & env,const OHOS::Notification::NotificationRequest * request,napi_value & result)394 napi_value Common::SetNotificationRequestByNumber(
395     const napi_env &env, const OHOS::Notification::NotificationRequest *request, napi_value &result)
396 {
397     ANS_LOGI("enter");
398 
399     napi_value value = nullptr;
400 
401     if (request == nullptr) {
402         ANS_LOGE("request is nullptr");
403         return NapiGetBoolean(env, false);
404     }
405 
406     // id?: number
407     napi_create_int32(env, request->GetNotificationId(), &value);
408     napi_set_named_property(env, result, "id", value);
409 
410     // slotType?: SlotType
411     SlotType outType = SlotType::UNKNOWN_TYPE;
412     if (!SlotTypeCToJS(request->GetSlotType(), outType)) {
413         return NapiGetBoolean(env, false);
414     }
415     napi_create_int32(env, (int32_t)outType, &value);
416     napi_set_named_property(env, result, "slotType", value);
417 
418     // deliveryTime?: number
419     napi_create_int64(env, request->GetDeliveryTime(), &value);
420     napi_set_named_property(env, result, "deliveryTime", value);
421 
422     // autoDeletedTime?: number
423     napi_create_int64(env, request->GetAutoDeletedTime(), &value);
424     napi_set_named_property(env, result, "autoDeletedTime", value);
425 
426     // color ?: number
427     napi_create_uint32(env, request->GetColor(), &value);
428     napi_set_named_property(env, result, "color", value);
429 
430     // badgeIconStyle ?: number
431     auto badgeIconStyle = static_cast<int32_t>(request->GetBadgeIconStyle());
432     napi_create_int32(env, badgeIconStyle, &value);
433     napi_set_named_property(env, result, "badgeIconStyle", value);
434 
435     // readonly creatorUid?: number
436     napi_create_int32(env, request->GetCreatorUid(), &value);
437     napi_set_named_property(env, result, "creatorUid", value);
438 
439     // readonly creatorPid?: number
440     napi_create_int32(env, request->GetCreatorPid(), &value);
441     napi_set_named_property(env, result, "creatorPid", value);
442 
443     // badgeNumber?: number
444     napi_create_uint32(env, request->GetBadgeNumber(), &value);
445     napi_set_named_property(env, result, "badgeNumber", value);
446 
447     return NapiGetBoolean(env, true);
448 }
449 
SetNotificationRequestByBool(const napi_env & env,const OHOS::Notification::NotificationRequest * request,napi_value & result)450 napi_value Common::SetNotificationRequestByBool(
451     const napi_env &env, const OHOS::Notification::NotificationRequest *request, napi_value &result)
452 {
453     ANS_LOGI("enter");
454 
455     napi_value value = nullptr;
456 
457     if (request == nullptr) {
458         ANS_LOGE("request is nullptr");
459         return NapiGetBoolean(env, false);
460     }
461     // isOngoing?: boolean
462     napi_get_boolean(env, request->IsInProgress(), &value);
463     napi_set_named_property(env, result, "isOngoing", value);
464 
465     // isUnremovable?: boolean
466     napi_get_boolean(env, request->IsUnremovable(), &value);
467     napi_set_named_property(env, result, "isUnremovable", value);
468 
469     // tapDismissed?: boolean
470     napi_get_boolean(env, request->IsTapDismissed(), &value);
471     napi_set_named_property(env, result, "tapDismissed", value);
472 
473     // colorEnabled?: boolean
474     napi_get_boolean(env, request->IsColorEnabled(), &value);
475     napi_set_named_property(env, result, "colorEnabled", value);
476 
477     // isAlertOnce?: boolean
478     napi_get_boolean(env, request->IsAlertOneTime(), &value);
479     napi_set_named_property(env, result, "isAlertOnce", value);
480 
481     // isStopwatch?: boolean
482     napi_get_boolean(env, request->IsShowStopwatch(), &value);
483     napi_set_named_property(env, result, "isStopwatch", value);
484 
485     // isCountDown?: boolean
486     napi_get_boolean(env, request->IsCountdownTimer(), &value);
487     napi_set_named_property(env, result, "isCountDown", value);
488 
489     // isFloatingIcon?: boolean
490     napi_get_boolean(env, request->IsFloatingIcon(), &value);
491     napi_set_named_property(env, result, "isFloatingIcon", value);
492 
493     // showDeliveryTime?: boolean
494     napi_get_boolean(env, request->IsShowDeliveryTime(), &value);
495     napi_set_named_property(env, result, "showDeliveryTime", value);
496 
497     return NapiGetBoolean(env, true);
498 }
499 
SetNotificationRequestByWantAgent(const napi_env & env,const OHOS::Notification::NotificationRequest * request,napi_value & result)500 napi_value Common::SetNotificationRequestByWantAgent(
501     const napi_env &env, const OHOS::Notification::NotificationRequest *request, napi_value &result)
502 {
503     ANS_LOGI("enter");
504     if (request == nullptr) {
505         ANS_LOGE("request is nullptr");
506         return NapiGetBoolean(env, false);
507     }
508     // wantAgent?: WantAgent
509     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> agent = request->GetWantAgent();
510     if (agent) {
511         napi_value wantAgent = nullptr;
512         wantAgent = CreateWantAgentByJS(env, agent);
513         napi_set_named_property(env, result, "wantAgent", wantAgent);
514     } else {
515         napi_set_named_property(env, result, "wantAgent", NapiGetNull(env));
516     }
517 
518     // removalWantAgent?: WantAgent
519     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> removalAgent = request->GetRemovalWantAgent();
520     if (removalAgent) {
521         napi_value wantAgent = nullptr;
522         wantAgent = CreateWantAgentByJS(env, removalAgent);
523         napi_set_named_property(env, result, "removalWantAgent", wantAgent);
524     } else {
525         napi_set_named_property(env, result, "removalWantAgent", NapiGetNull(env));
526     }
527 
528     // maxScreenWantAgent?: WantAgent
529     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> maxScreenAgent = request->GetMaxScreenWantAgent();
530     if (maxScreenAgent) {
531         napi_value wantAgent = nullptr;
532         wantAgent = CreateWantAgentByJS(env, maxScreenAgent);
533         napi_set_named_property(env, result, "maxScreenWantAgent", wantAgent);
534     } else {
535         napi_set_named_property(env, result, "maxScreenWantAgent", NapiGetNull(env));
536     }
537 
538     return NapiGetBoolean(env, true);
539 }
540 
SetNotificationRequestByPixelMap(const napi_env & env,const OHOS::Notification::NotificationRequest * request,napi_value & result)541 napi_value Common::SetNotificationRequestByPixelMap(
542     const napi_env &env, const OHOS::Notification::NotificationRequest *request, napi_value &result)
543 {
544     ANS_LOGI("enter");
545 
546     if (request == nullptr) {
547         ANS_LOGE("request is nullptr");
548         return NapiGetBoolean(env, false);
549     }
550 
551     // smallIcon?: image.PixelMap
552     std::shared_ptr<Media::PixelMap> littleIcon = request->GetLittleIcon();
553     if (littleIcon) {
554         napi_value smallIconResult = nullptr;
555         napi_valuetype valuetype = napi_undefined;
556         smallIconResult = Media::PixelMapNapi::CreatePixelMap(env, littleIcon);
557         NAPI_CALL(env, napi_typeof(env, smallIconResult, &valuetype));
558         if (valuetype == napi_undefined) {
559             ANS_LOGI("smallIconResult is undefined");
560             napi_set_named_property(env, result, "smallIcon", NapiGetNull(env));
561         } else {
562             napi_set_named_property(env, result, "smallIcon", smallIconResult);
563         }
564     }
565 
566     // largeIcon?: image.PixelMap
567     std::shared_ptr<Media::PixelMap> largeIcon = request->GetBigIcon();
568     if (largeIcon) {
569         napi_value largeIconResult = nullptr;
570         napi_valuetype valuetype = napi_undefined;
571         largeIconResult = Media::PixelMapNapi::CreatePixelMap(env, largeIcon);
572         NAPI_CALL(env, napi_typeof(env, largeIconResult, &valuetype));
573         if (valuetype == napi_undefined) {
574             ANS_LOGI("largeIconResult is undefined");
575             napi_set_named_property(env, result, "largeIcon", NapiGetNull(env));
576         } else {
577             napi_set_named_property(env, result, "largeIcon", largeIconResult);
578         }
579     }
580 
581     return NapiGetBoolean(env, true);
582 }
583 
SetNotificationRequestByCustom(const napi_env & env,const OHOS::Notification::NotificationRequest * request,napi_value & result)584 napi_value Common::SetNotificationRequestByCustom(
585     const napi_env &env, const OHOS::Notification::NotificationRequest *request, napi_value &result)
586 {
587     ANS_LOGI("enter");
588 
589     if (request == nullptr) {
590         ANS_LOGE("request is nullptr");
591         return NapiGetBoolean(env, false);
592     }
593 
594     // content: NotificationContent
595     std::shared_ptr<NotificationContent> content = request->GetContent();
596     if (content) {
597         napi_value contentResult = nullptr;
598         napi_create_object(env, &contentResult);
599         if (!SetNotificationContent(env, content, contentResult)) {
600             ANS_LOGE("SetNotificationContent call failed");
601             return NapiGetBoolean(env, false);
602         }
603         napi_set_named_property(env, result, "content", contentResult);
604     } else {
605         ANS_LOGE("content is nullptr");
606         return NapiGetBoolean(env, false);
607     }
608 
609     // extraInfo?: {[key:string] : any}
610     std::shared_ptr<AAFwk::WantParams> additionalData = request->GetAdditionalData();
611     if (additionalData) {
612         napi_value extraInfo = nullptr;
613         extraInfo = OHOS::AppExecFwk::WrapWantParams(env, *additionalData);
614         napi_set_named_property(env, result, "extraInfo", extraInfo);
615     }
616 
617     // actionButtons?: Array<NotificationActionButton>
618     napi_value arr = nullptr;
619     uint32_t count = 0;
620     napi_create_array(env, &arr);
621     for (auto vec : request->GetActionButtons()) {
622         if (vec) {
623             napi_value actionButtonResult = nullptr;
624             napi_create_object(env, &actionButtonResult);
625             if (SetNotificationActionButton(env, vec, actionButtonResult)) {
626                 napi_set_element(env, arr, count, actionButtonResult);
627                 count++;
628             }
629         }
630     }
631     napi_set_named_property(env, result, "actionButtons", arr);
632 
633     // template?: NotificationTemplate
634     std::shared_ptr<NotificationTemplate> templ = request->GetTemplate();
635     if (templ) {
636         napi_value templateResult = nullptr;
637         napi_create_object(env, &templateResult);
638         if (!SetNotificationTemplateInfo(env, templ, templateResult)) {
639             ANS_LOGE("SetNotificationTemplate call failed");
640             return NapiGetBoolean(env, false);
641         }
642         napi_set_named_property(env, result, "template", templateResult);
643     }
644 
645     // readonly notificationFlags?: NotificationFlags
646     std::shared_ptr<NotificationFlags> flags = request->GetFlags();
647     if (flags) {
648         napi_value flagsResult = nullptr;
649         napi_create_object(env, &flagsResult);
650         if (!SetNotificationFlags(env, flags, flagsResult)) {
651             ANS_LOGE("SetNotificationFlags call failed");
652             return NapiGetBoolean(env, false);
653         }
654         napi_set_named_property(env, result, "notificationFlags", flagsResult);
655     }
656 
657     return NapiGetBoolean(env, true);
658 }
659 
SetNotificationRequest(const napi_env & env,const OHOS::Notification::NotificationRequest * request,napi_value & result)660 napi_value Common::SetNotificationRequest(
661     const napi_env &env, const OHOS::Notification::NotificationRequest *request, napi_value &result)
662 {
663     ANS_LOGI("enter");
664 
665     if (request == nullptr) {
666         ANS_LOGE("request is nullptr");
667         return NapiGetBoolean(env, false);
668     }
669 
670     if (!SetNotificationRequestByString(env, request, result)) {
671         return NapiGetBoolean(env, false);
672     }
673     if (!SetNotificationRequestByNumber(env, request, result)) {
674         return NapiGetBoolean(env, false);
675     }
676     if (!SetNotificationRequestByBool(env, request, result)) {
677         return NapiGetBoolean(env, false);
678     }
679     if (!SetNotificationRequestByWantAgent(env, request, result)) {
680         return NapiGetBoolean(env, false);
681     }
682     if (!SetNotificationRequestByPixelMap(env, request, result)) {
683         return NapiGetBoolean(env, false);
684     }
685     if (!SetNotificationRequestByCustom(env, request, result)) {
686         return NapiGetBoolean(env, false);
687     }
688 
689     return NapiGetBoolean(env, true);
690 }
691 
SetNotificationSortingMap(const napi_env & env,const std::shared_ptr<NotificationSortingMap> & sortingMap,napi_value & result)692 napi_value Common::SetNotificationSortingMap(
693     const napi_env &env, const std::shared_ptr<NotificationSortingMap> &sortingMap, napi_value &result)
694 {
695     ANS_LOGI("enter");
696     if (sortingMap == nullptr) {
697         ANS_LOGE("sortingMap is null");
698         return NapiGetBoolean(env, false);
699     }
700     if (sortingMap->GetKey().size() == 0) {
701         ANS_LOGE("sortingMap GetKey().size is empty");
702         return NapiGetBoolean(env, false);
703     }
704 
705     size_t count = 0;
706     napi_value arrSortedHashCode = nullptr;
707     napi_create_array(env, &arrSortedHashCode);
708     napi_value sortingsResult = nullptr;
709     napi_create_object(env, &sortingsResult);
710     for (auto key : sortingMap->GetKey()) {
711         NotificationSorting sorting;
712         if (sortingMap->GetNotificationSorting(key, sorting)) {
713             // sortedHashCode: Array<string>
714             napi_value keyValue = nullptr;
715             ANS_LOGI("sortingMap key = %{public}s", key.c_str());
716             napi_create_string_utf8(env, key.c_str(), NAPI_AUTO_LENGTH, &keyValue);
717             napi_set_element(env, arrSortedHashCode, count, keyValue);
718 
719             // sortings:{[key : string] : NotificationSorting}
720             napi_value sortingResult = nullptr;
721             napi_create_object(env, &sortingResult);
722             if (!SetNotificationSorting(env, sorting, sortingResult)) {
723                 ANS_LOGE("SetNotificationSorting call failed");
724                 return NapiGetBoolean(env, false);
725             }
726             napi_set_named_property(env, sortingsResult, key.c_str(), sortingResult);
727             count++;
728         } else {
729             ANS_LOGW("sortingMap Key: %{public}s match value is empty", key.c_str());
730         }
731     }
732     napi_set_named_property(env, result, "sortedHashCode", arrSortedHashCode);
733     napi_set_named_property(env, result, "sortings", sortingsResult);
734 
735     return NapiGetBoolean(env, true);
736 }
737 
SetNotificationSorting(const napi_env & env,const NotificationSorting & sorting,napi_value & result)738 napi_value Common::SetNotificationSorting(const napi_env &env, const NotificationSorting &sorting, napi_value &result)
739 {
740     ANS_LOGI("enter");
741 
742     // slot: NotificationSlot
743     napi_value slotResult = nullptr;
744     napi_value value = nullptr;
745     napi_create_object(env, &slotResult);
746     if (!SetNotificationSlot(env, sorting.GetSlot(), slotResult)) {
747         ANS_LOGE("SetNotificationSlot call failed");
748         return NapiGetBoolean(env, false);
749     }
750     napi_set_named_property(env, result, "slot", slotResult);
751 
752     // hashCode?: string
753     napi_create_string_utf8(env, sorting.GetKey().c_str(), NAPI_AUTO_LENGTH, &value);
754     napi_set_named_property(env, result, "hashCode", value);
755 
756     // ranking?: number
757     napi_create_int32(env, sorting.GetRanking(), &value);
758     napi_set_named_property(env, result, "ranking", value);
759 
760     // isDisplayBadge?: boolean
761     napi_get_boolean(env, sorting.IsDisplayBadge(), &value);
762     napi_set_named_property(env, result, "isDisplayBadge", value);
763 
764     // isHiddenNotification?: boolean
765     napi_get_boolean(env, sorting.IsHiddenNotification(), &value);
766     napi_set_named_property(env, result, "isHiddenNotification", value);
767 
768     // importance?: number
769     napi_create_int32(env, sorting.GetImportance(), &value);
770     napi_set_named_property(env, result, "importance", value);
771 
772     // groupKeyOverride?: string
773     napi_create_string_utf8(env, sorting.GetGroupKeyOverride().c_str(), NAPI_AUTO_LENGTH, &value);
774     napi_set_named_property(env, result, "groupKeyOverride", value);
775 
776     // visiblenessOverride?: number
777     napi_create_int32(env, sorting.GetVisiblenessOverride(), &value);
778     napi_set_named_property(env, result, "visiblenessOverride", value);
779 
780     return NapiGetBoolean(env, true);
781 }
782 
SetNotificationSlot(const napi_env & env,const NotificationSlot & slot,napi_value & result)783 napi_value Common::SetNotificationSlot(const napi_env &env, const NotificationSlot &slot, napi_value &result)
784 {
785     ANS_LOGI("enter");
786 
787     napi_value value = nullptr;
788     // type: SlotType
789     SlotType outType = SlotType::UNKNOWN_TYPE;
790     if (!SlotTypeCToJS(slot.GetType(), outType)) {
791         return NapiGetBoolean(env, false);
792     }
793     napi_create_int32(env, (int32_t)outType, &value);
794     napi_set_named_property(env, result, "type", value);
795 
796     // level?: number
797     SlotLevel outLevel = SlotLevel::LEVEL_NONE;
798     if (!SlotLevelCToJS(slot.GetLevel(), outLevel)) {
799         return NapiGetBoolean(env, false);
800     }
801     napi_create_int32(env, (int32_t)outLevel, &value);
802     napi_set_named_property(env, result, "level", value);
803 
804     // desc?: string
805     napi_create_string_utf8(env, slot.GetDescription().c_str(), NAPI_AUTO_LENGTH, &value);
806     napi_set_named_property(env, result, "desc", value);
807 
808     // badgeFlag?: boolean
809     napi_get_boolean(env, slot.IsShowBadge(), &value);
810     napi_set_named_property(env, result, "badgeFlag", value);
811 
812     // bypassDnd?: boolean
813     napi_get_boolean(env, slot.IsEnableBypassDnd(), &value);
814     napi_set_named_property(env, result, "bypassDnd", value);
815 
816     // lockscreenVisibility?: number
817     int32_t lockScreenVisibleness = (int32_t)slot.GetLockScreenVisibleness();
818     napi_create_int32(env, lockScreenVisibleness, &value);
819     napi_set_named_property(env, result, "lockscreenVisibility", value);
820 
821     // vibrationEnabled?: boolean
822     napi_get_boolean(env, slot.CanVibrate(), &value);
823     napi_set_named_property(env, result, "vibrationEnabled", value);
824 
825     // sound?: string
826     napi_create_string_utf8(env, slot.GetSound().ToString().c_str(), NAPI_AUTO_LENGTH, &value);
827     napi_set_named_property(env, result, "sound", value);
828 
829     // lightEnabled?: boolean
830     napi_get_boolean(env, slot.CanEnableLight(), &value);
831     napi_set_named_property(env, result, "lightEnabled", value);
832 
833     // lightColor?: number
834     napi_create_int32(env, slot.GetLedLightColor(), &value);
835     napi_set_named_property(env, result, "lightColor", value);
836 
837     // vibrationValues?: Array<number>
838     napi_value arr = nullptr;
839     napi_create_array(env, &arr);
840     size_t count = 0;
841     for (auto vec : slot.GetVibrationStyle()) {
842         napi_create_int64(env, vec, &value);
843         napi_set_element(env, arr, count, value);
844         count++;
845     }
846     napi_set_named_property(env, result, "vibrationValues", arr);
847 
848     // enabled?: boolean
849     napi_get_boolean(env, slot.GetEnable(), &value);
850     napi_set_named_property(env, result, "enabled", value);
851 
852     return NapiGetBoolean(env, true);
853 }
854 
SetNotificationContentDetailed(const napi_env & env,const ContentType & type,const std::shared_ptr<NotificationContent> & content,napi_value & result)855 napi_value Common::SetNotificationContentDetailed(const napi_env &env, const ContentType &type,
856     const std::shared_ptr<NotificationContent> &content, napi_value &result)
857 {
858     ANS_LOGI("enter");
859     napi_value ret = NapiGetBoolean(env, false);
860     if (!content) {
861         ANS_LOGE("content is null");
862         return ret;
863     }
864 
865     std::shared_ptr<NotificationBasicContent> basicContent = content->GetNotificationContent();
866     if (basicContent == nullptr) {
867         ANS_LOGE("content is null");
868         return ret;
869     }
870 
871     napi_value contentResult = nullptr;
872     napi_create_object(env, &contentResult);
873     if (type == ContentType::NOTIFICATION_CONTENT_BASIC_TEXT) {
874         // normal?: NotificationBasicContent
875         ret = SetNotificationBasicContent(env, basicContent.get(), contentResult);
876         if (ret) {
877             napi_set_named_property(env, result, "normal", contentResult);
878         }
879     } else if (type == ContentType::NOTIFICATION_CONTENT_LONG_TEXT) {
880         // longText?: NotificationLongTextContent
881         ret = SetNotificationLongTextContent(env, basicContent.get(), contentResult);
882         if (ret) {
883             napi_set_named_property(env, result, "longText", contentResult);
884         }
885     } else if (type == ContentType::NOTIFICATION_CONTENT_PICTURE) {
886         // picture?: NotificationPictureContent
887         ret = SetNotificationPictureContent(env, basicContent.get(), contentResult);
888         if (ret) {
889             napi_set_named_property(env, result, "picture", contentResult);
890         }
891     } else if (type == ContentType::NOTIFICATION_CONTENT_CONVERSATION) {
892         // conversation?: NotificationConversationalContent
893         ret = SetNotificationConversationalContent(env, basicContent.get(), contentResult);
894         if (ret) {
895             napi_set_named_property(env, result, "conversation", contentResult);
896         }
897     } else if (type == ContentType::NOTIFICATION_CONTENT_MULTILINE) {
898         // multiLine?: NotificationMultiLineContent
899         ret = SetNotificationMultiLineContent(env, basicContent.get(), contentResult);
900         if (ret) {
901             napi_set_named_property(env, result, "multiLine", contentResult);
902         }
903     } else {
904         ANS_LOGE("ContentType is does not exist");
905     }
906     return ret;
907 }
908 
SetNotificationContent(const napi_env & env,const std::shared_ptr<NotificationContent> & content,napi_value & result)909 napi_value Common::SetNotificationContent(
910     const napi_env &env, const std::shared_ptr<NotificationContent> &content, napi_value &result)
911 {
912     ANS_LOGI("enter");
913     napi_value value = nullptr;
914     if (content == nullptr) {
915         ANS_LOGE("content is null");
916         return NapiGetBoolean(env, false);
917     }
918 
919     // contentType: ContentType
920     NotificationContent::Type type = content->GetContentType();
921     ContentType outType = ContentType::NOTIFICATION_CONTENT_BASIC_TEXT;
922     if (!ContentTypeCToJS(type, outType)) {
923         return NapiGetBoolean(env, false);
924     }
925     napi_create_int32(env, (int32_t)outType, &value);
926     napi_set_named_property(env, result, "contentType", value);
927 
928     if (!SetNotificationContentDetailed(env, outType, content, result)) {
929         return NapiGetBoolean(env, false);
930     }
931 
932     return NapiGetBoolean(env, true);
933 }
934 
SetNotificationBasicContent(const napi_env & env,const NotificationBasicContent * basicContent,napi_value & result)935 napi_value Common::SetNotificationBasicContent(
936     const napi_env &env, const NotificationBasicContent *basicContent, napi_value &result)
937 {
938     ANS_LOGI("enter");
939     napi_value value = nullptr;
940     if (basicContent == nullptr) {
941         ANS_LOGE("basicContent is null");
942         return NapiGetBoolean(env, false);
943     }
944 
945     // title: string
946     napi_create_string_utf8(env, basicContent->GetTitle().c_str(), NAPI_AUTO_LENGTH, &value);
947     napi_set_named_property(env, result, "title", value);
948 
949     // text: string
950     napi_create_string_utf8(env, basicContent->GetText().c_str(), NAPI_AUTO_LENGTH, &value);
951     napi_set_named_property(env, result, "text", value);
952 
953     // additionalText?: string
954     napi_create_string_utf8(env, basicContent->GetAdditionalText().c_str(), NAPI_AUTO_LENGTH, &value);
955     napi_set_named_property(env, result, "additionalText", value);
956 
957     return NapiGetBoolean(env, true);
958 }
959 
SetNotificationLongTextContent(const napi_env & env,NotificationBasicContent * basicContent,napi_value & result)960 napi_value Common::SetNotificationLongTextContent(
961     const napi_env &env, NotificationBasicContent *basicContent, napi_value &result)
962 {
963     ANS_LOGI("enter");
964     napi_value value = nullptr;
965     if (basicContent == nullptr) {
966         ANS_LOGE("basicContent is null");
967         return NapiGetBoolean(env, false);
968     }
969 
970     OHOS::Notification::NotificationLongTextContent *longTextContent =
971         static_cast<OHOS::Notification::NotificationLongTextContent *>(basicContent);
972     if (longTextContent == nullptr) {
973         ANS_LOGE("longTextContent is null");
974         return NapiGetBoolean(env, false);
975     }
976 
977     if (!SetNotificationBasicContent(env, longTextContent, result)) {
978         ANS_LOGE("SetNotificationBasicContent call failed");
979         return NapiGetBoolean(env, false);
980     }
981 
982     // longText: string
983     napi_create_string_utf8(env, longTextContent->GetLongText().c_str(), NAPI_AUTO_LENGTH, &value);
984     napi_set_named_property(env, result, "longText", value);
985 
986     // briefText: string
987     napi_create_string_utf8(env, longTextContent->GetBriefText().c_str(), NAPI_AUTO_LENGTH, &value);
988     napi_set_named_property(env, result, "briefText", value);
989 
990     // expandedTitle: string
991     napi_create_string_utf8(env, longTextContent->GetExpandedTitle().c_str(), NAPI_AUTO_LENGTH, &value);
992     napi_set_named_property(env, result, "expandedTitle", value);
993 
994     return NapiGetBoolean(env, true);
995 }
996 
SetNotificationPictureContent(const napi_env & env,NotificationBasicContent * basicContent,napi_value & result)997 napi_value Common::SetNotificationPictureContent(
998     const napi_env &env, NotificationBasicContent *basicContent, napi_value &result)
999 {
1000     ANS_LOGI("enter");
1001     napi_value value = nullptr;
1002     if (basicContent == nullptr) {
1003         ANS_LOGE("basicContent is null");
1004         return NapiGetBoolean(env, false);
1005     }
1006     OHOS::Notification::NotificationPictureContent *pictureContent =
1007         static_cast<OHOS::Notification::NotificationPictureContent *>(basicContent);
1008     if (pictureContent == nullptr) {
1009         ANS_LOGE("pictureContent is null");
1010         return NapiGetBoolean(env, false);
1011     }
1012 
1013     if (!SetNotificationBasicContent(env, pictureContent, result)) {
1014         ANS_LOGE("SetNotificationBasicContent call failed");
1015         return NapiGetBoolean(env, false);
1016     }
1017 
1018     // briefText: string
1019     napi_create_string_utf8(env, pictureContent->GetBriefText().c_str(), NAPI_AUTO_LENGTH, &value);
1020     napi_set_named_property(env, result, "briefText", value);
1021 
1022     // expandedTitle: string
1023     napi_create_string_utf8(env, pictureContent->GetExpandedTitle().c_str(), NAPI_AUTO_LENGTH, &value);
1024     napi_set_named_property(env, result, "expandedTitle", value);
1025 
1026     // picture: image.PixelMap
1027     std::shared_ptr<Media::PixelMap> picture = pictureContent->GetBigPicture();
1028     if (picture) {
1029         napi_value pictureResult = nullptr;
1030         napi_valuetype valuetype = napi_undefined;
1031         pictureResult = Media::PixelMapNapi::CreatePixelMap(env, picture);
1032         NAPI_CALL(env, napi_typeof(env, pictureResult, &valuetype));
1033         if (valuetype == napi_undefined) {
1034             ANS_LOGW("pictureResult is undefined");
1035             napi_set_named_property(env, result, "picture", NapiGetNull(env));
1036         } else {
1037             napi_set_named_property(env, result, "picture", pictureResult);
1038         }
1039     }
1040     return NapiGetBoolean(env, true);
1041 }
1042 
SetNotificationConversationalContent(const napi_env & env,NotificationBasicContent * basicContent,napi_value & result)1043 napi_value Common::SetNotificationConversationalContent(const napi_env &env,
1044     NotificationBasicContent *basicContent, napi_value &result)
1045 {
1046     ANS_LOGI("enter");
1047     napi_value value = nullptr;
1048     if (basicContent == nullptr) {
1049         ANS_LOGE("basicContent is null");
1050         return NapiGetBoolean(env, false);
1051     }
1052     OHOS::Notification::NotificationConversationalContent *conversationalContent =
1053         static_cast<OHOS::Notification::NotificationConversationalContent *>(basicContent);
1054     if (conversationalContent == nullptr) {
1055         ANS_LOGE("conversationalContent is null");
1056         return NapiGetBoolean(env, false);
1057     }
1058 
1059     if (!SetNotificationBasicContent(env, conversationalContent, result)) {
1060         ANS_LOGE("SetNotificationBasicContent call failed");
1061         return NapiGetBoolean(env, false);
1062     }
1063 
1064     // conversationTitle: string
1065     napi_create_string_utf8(env, conversationalContent->GetConversationTitle().c_str(), NAPI_AUTO_LENGTH, &value);
1066     napi_set_named_property(env, result, "conversationTitle", value);
1067 
1068     // conversationGroup: boolean
1069     napi_get_boolean(env, conversationalContent->IsConversationGroup(), &value);
1070     napi_set_named_property(env, result, "conversationGroup", value);
1071 
1072     // messages: Array<ConversationalMessage>
1073     napi_value arr = nullptr;
1074     if (!SetConversationalMessages(env, conversationalContent, arr)) {
1075         ANS_LOGE("SetConversationalMessages call failed");
1076         return NapiGetBoolean(env, false);
1077     }
1078     napi_set_named_property(env, result, "messages", arr);
1079 
1080     // user: MessageUser
1081     napi_value messageUserResult = nullptr;
1082     napi_create_object(env, &messageUserResult);
1083     if (!SetMessageUser(env, conversationalContent->GetMessageUser(), messageUserResult)) {
1084         ANS_LOGE("SetMessageUser call failed");
1085         return NapiGetBoolean(env, false);
1086     }
1087     napi_set_named_property(env, result, "user", messageUserResult);
1088 
1089     return NapiGetBoolean(env, true);
1090 }
1091 
SetNotificationMultiLineContent(const napi_env & env,NotificationBasicContent * basicContent,napi_value & result)1092 napi_value Common::SetNotificationMultiLineContent(
1093     const napi_env &env, NotificationBasicContent *basicContent, napi_value &result)
1094 {
1095     ANS_LOGI("enter");
1096     napi_value value = nullptr;
1097     if (basicContent == nullptr) {
1098         ANS_LOGE("basicContent is null");
1099         return NapiGetBoolean(env, false);
1100     }
1101     OHOS::Notification::NotificationMultiLineContent *multiLineContent =
1102         static_cast<OHOS::Notification::NotificationMultiLineContent *>(basicContent);
1103     if (multiLineContent == nullptr) {
1104         ANS_LOGE("multiLineContent is null");
1105         return NapiGetBoolean(env, false);
1106     }
1107 
1108     if (!SetNotificationBasicContent(env, multiLineContent, result)) {
1109         ANS_LOGE("SetNotificationBasicContent call failed");
1110         return NapiGetBoolean(env, false);
1111     }
1112 
1113     // briefText: string
1114     napi_create_string_utf8(env, multiLineContent->GetBriefText().c_str(), NAPI_AUTO_LENGTH, &value);
1115     napi_set_named_property(env, result, "briefText", value);
1116 
1117     // longTitle: string
1118     napi_create_string_utf8(env, multiLineContent->GetExpandedTitle().c_str(), NAPI_AUTO_LENGTH, &value);
1119     napi_set_named_property(env, result, "longTitle", value);
1120 
1121     // lines: Array<String>
1122     napi_value arr = nullptr;
1123     int count = 0;
1124     napi_create_array(env, &arr);
1125     for (auto vec : multiLineContent->GetAllLines()) {
1126         napi_create_string_utf8(env, vec.c_str(), NAPI_AUTO_LENGTH, &value);
1127         napi_set_element(env, arr, count, value);
1128         count++;
1129     }
1130     napi_set_named_property(env, result, "lines", arr);
1131 
1132     return NapiGetBoolean(env, true);
1133 }
1134 
SetMessageUser(const napi_env & env,const MessageUser & messageUser,napi_value & result)1135 napi_value Common::SetMessageUser(const napi_env &env, const MessageUser &messageUser, napi_value &result)
1136 {
1137     ANS_LOGI("enter");
1138 
1139     napi_value value = nullptr;
1140     // name: string
1141     napi_create_string_utf8(env, messageUser.GetName().c_str(), NAPI_AUTO_LENGTH, &value);
1142     napi_set_named_property(env, result, "name", value);
1143 
1144     // key: string
1145     napi_create_string_utf8(env, messageUser.GetKey().c_str(), NAPI_AUTO_LENGTH, &value);
1146     napi_set_named_property(env, result, "key", value);
1147 
1148     // uri: string
1149     napi_create_string_utf8(env, messageUser.GetUri().ToString().c_str(), NAPI_AUTO_LENGTH, &value);
1150     napi_set_named_property(env, result, "uri", value);
1151 
1152     // isMachine: boolean
1153     napi_get_boolean(env, messageUser.IsMachine(), &value);
1154     napi_set_named_property(env, result, "isMachine", value);
1155 
1156     // isUserImportant: boolean
1157     napi_get_boolean(env, messageUser.IsUserImportant(), &value);
1158     napi_set_named_property(env, result, "isUserImportant", value);
1159 
1160     // icon?: image.PixelMap
1161     std::shared_ptr<Media::PixelMap> icon = messageUser.GetPixelMap();
1162     if (icon) {
1163         napi_value iconResult = nullptr;
1164         napi_valuetype valuetype = napi_undefined;
1165         iconResult = Media::PixelMapNapi::CreatePixelMap(env, icon);
1166         NAPI_CALL(env, napi_typeof(env, iconResult, &valuetype));
1167         if (valuetype == napi_undefined) {
1168             ANS_LOGW("iconResult is undefined");
1169             napi_set_named_property(env, result, "icon", NapiGetNull(env));
1170         } else {
1171             napi_set_named_property(env, result, "icon", iconResult);
1172         }
1173     }
1174     return NapiGetBoolean(env, true);
1175 }
1176 
SetConversationalMessages(const napi_env & env,const OHOS::Notification::NotificationConversationalContent * conversationalContent,napi_value & arr)1177 napi_value Common::SetConversationalMessages(const napi_env &env,
1178     const OHOS::Notification::NotificationConversationalContent *conversationalContent, napi_value &arr)
1179 {
1180     ANS_LOGI("enter");
1181     if (!conversationalContent) {
1182         ANS_LOGE("conversationalContent is null");
1183         return NapiGetBoolean(env, false);
1184     }
1185 
1186     int count = 0;
1187     napi_create_array(env, &arr);
1188     std::vector<std::shared_ptr<NotificationConversationalMessage>> messages =
1189         conversationalContent->GetAllConversationalMessages();
1190     for (auto vec : messages) {
1191         if (!vec) {
1192             continue;
1193         }
1194         napi_value conversationalMessageResult = nullptr;
1195         napi_create_object(env, &conversationalMessageResult);
1196         if (!SetConversationalMessage(env, vec, conversationalMessageResult)) {
1197             ANS_LOGE("SetConversationalMessage call failed");
1198             return NapiGetBoolean(env, false);
1199         }
1200         napi_set_element(env, arr, count, conversationalMessageResult);
1201         count++;
1202     }
1203     return NapiGetBoolean(env, true);
1204 }
1205 
SetConversationalMessage(const napi_env & env,const std::shared_ptr<NotificationConversationalMessage> & conversationalMessage,napi_value & result)1206 napi_value Common::SetConversationalMessage(const napi_env &env,
1207     const std::shared_ptr<NotificationConversationalMessage> &conversationalMessage, napi_value &result)
1208 {
1209     ANS_LOGI("enter");
1210     napi_value value = nullptr;
1211     if (conversationalMessage == nullptr) {
1212         ANS_LOGE("conversationalMessage is null");
1213         return NapiGetBoolean(env, false);
1214     }
1215 
1216     // text: string
1217     napi_create_string_utf8(env, conversationalMessage->GetText().c_str(), NAPI_AUTO_LENGTH, &value);
1218     napi_set_named_property(env, result, "text", value);
1219 
1220     // timestamp: number
1221     napi_create_int64(env, conversationalMessage->GetArrivedTime(), &value);
1222     napi_set_named_property(env, result, "timestamp", value);
1223 
1224     // sender: MessageUser
1225     napi_value messageUserResult = nullptr;
1226     napi_create_object(env, &messageUserResult);
1227     if (!SetMessageUser(env, conversationalMessage->GetSender(), messageUserResult)) {
1228         ANS_LOGE("SetMessageUser call failed");
1229         return NapiGetBoolean(env, false);
1230     }
1231     napi_set_named_property(env, result, "sender", messageUserResult);
1232 
1233     // mimeType: string
1234     napi_create_string_utf8(env, conversationalMessage->GetMimeType().c_str(), NAPI_AUTO_LENGTH, &value);
1235     napi_set_named_property(env, result, "mimeType", value);
1236 
1237     // uri: string
1238     napi_create_string_utf8(env, conversationalMessage->GetUri()->ToString().c_str(), NAPI_AUTO_LENGTH, &value);
1239     napi_set_named_property(env, result, "uri", value);
1240 
1241     return NapiGetBoolean(env, true);
1242 }
1243 
SetNotificationActionButton(const napi_env & env,const std::shared_ptr<NotificationActionButton> & actionButton,napi_value & result)1244 napi_value Common::SetNotificationActionButton(
1245     const napi_env &env, const std::shared_ptr<NotificationActionButton> &actionButton, napi_value &result)
1246 {
1247     ANS_LOGI("enter");
1248     if (actionButton == nullptr) {
1249         ANS_LOGE("actionButton is null");
1250         return NapiGetBoolean(env, false);
1251     }
1252 
1253     napi_value value = nullptr;
1254 
1255     // title: string
1256     napi_create_string_utf8(env, actionButton->GetTitle().c_str(), NAPI_AUTO_LENGTH, &value);
1257     napi_set_named_property(env, result, "title", value);
1258 
1259     // wantAgent: WantAgent
1260     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> agent = actionButton->GetWantAgent();
1261     if (agent == nullptr) {
1262         ANS_LOGI("wantAgent is null");
1263         napi_set_named_property(env, result, "wantAgent", NapiGetNull(env));
1264         return NapiGetBoolean(env, false);
1265     }
1266     napi_value wantAgent = nullptr;
1267     wantAgent = CreateWantAgentByJS(env, agent);
1268     napi_set_named_property(env, result, "wantAgent", wantAgent);
1269 
1270     // icon?: image.PixelMap
1271     std::shared_ptr<Media::PixelMap> icon = actionButton->GetIcon();
1272     if (icon) {
1273         napi_value iconResult = nullptr;
1274         napi_valuetype valuetype = napi_undefined;
1275         iconResult = Media::PixelMapNapi::CreatePixelMap(env, icon);
1276         NAPI_CALL(env, napi_typeof(env, iconResult, &valuetype));
1277         if (valuetype == napi_undefined) {
1278             ANS_LOGW("iconResult is undefined");
1279             napi_set_named_property(env, result, "icon", NapiGetNull(env));
1280         } else {
1281             napi_set_named_property(env, result, "icon", iconResult);
1282         }
1283     }
1284 
1285     if (!SetNotificationActionButtonByExtras(env, actionButton, result)) {
1286         return NapiGetBoolean(env, false);
1287     }
1288 
1289     // userInput?: NotificationUserInput
1290     napi_value userInputResult = nullptr;
1291     napi_create_object(env, &userInputResult);
1292     if (!SetNotificationActionButtonByUserInput(env, actionButton->GetUserInput(), userInputResult)) {
1293         return NapiGetBoolean(env, false);
1294     }
1295     napi_set_named_property(env, result, "userInput", userInputResult);
1296 
1297     return NapiGetBoolean(env, true);
1298 }
1299 
SetNotificationActionButtonByExtras(const napi_env & env,const std::shared_ptr<NotificationActionButton> & actionButton,napi_value & result)1300 napi_value Common::SetNotificationActionButtonByExtras(
1301     const napi_env &env, const std::shared_ptr<NotificationActionButton> &actionButton, napi_value &result)
1302 {
1303     ANS_LOGI("enter");
1304     if (!actionButton) {
1305         ANS_LOGE("actionButton is null");
1306         return NapiGetBoolean(env, false);
1307     }
1308     // extras?: {[key: string]: any}
1309     auto extras = actionButton->GetAdditionalData();
1310     if (extras) {
1311         napi_value nExtras = nullptr;
1312         nExtras = OHOS::AppExecFwk::WrapWantParams(env, *extras);
1313         napi_set_named_property(env, result, "extras", nExtras);
1314     }
1315     return NapiGetBoolean(env, true);
1316 }
1317 
SetNotificationActionButtonByUserInput(const napi_env & env,const std::shared_ptr<NotificationUserInput> & userInput,napi_value & result)1318 napi_value Common::SetNotificationActionButtonByUserInput(
1319     const napi_env &env, const std::shared_ptr<NotificationUserInput> &userInput, napi_value &result)
1320 {
1321     ANS_LOGI("enter");
1322 
1323     if (!userInput) {
1324         ANS_LOGE("userInput is null");
1325         return NapiGetBoolean(env, false);
1326     }
1327 
1328     napi_value value = nullptr;
1329     napi_value arr = nullptr;
1330     int count = 0;
1331 
1332     // inputKey: string
1333     napi_create_string_utf8(env, userInput->GetInputKey().c_str(), NAPI_AUTO_LENGTH, &value);
1334     napi_set_named_property(env, result, "inputKey", value);
1335 
1336     // tag: string
1337     napi_create_string_utf8(env, userInput->GetTag().c_str(), NAPI_AUTO_LENGTH, &value);
1338     napi_set_named_property(env, result, "tag", value);
1339 
1340     // options: Array<string>
1341     napi_create_array(env, &arr);
1342     for (auto vec : userInput->GetOptions()) {
1343         napi_create_string_utf8(env, vec.c_str(), NAPI_AUTO_LENGTH, &value);
1344         napi_set_element(env, arr, count, value);
1345         count++;
1346     }
1347     if (count > 0) {
1348         napi_set_named_property(env, result, "options", arr);
1349     }
1350 
1351     // permitFreeFormInput?: boolean
1352     napi_get_boolean(env, userInput->IsPermitFreeFormInput(), &value);
1353     napi_set_named_property(env, result, "permitFreeFormInput", value);
1354 
1355     // permitMimeTypes?: Array<string>
1356     count = 0;
1357     napi_create_array(env, &arr);
1358     for (auto vec : userInput->GetPermitMimeTypes()) {
1359         napi_create_string_utf8(env, vec.c_str(), NAPI_AUTO_LENGTH, &value);
1360         napi_set_element(env, arr, count, value);
1361         count++;
1362     }
1363     if (count > 0) {
1364         napi_set_named_property(env, result, "permitMimeTypes", arr);
1365     }
1366 
1367     // editType?: number
1368     napi_create_int64(env, userInput->GetEditType(), &value);
1369     napi_set_named_property(env, result, "editType", value);
1370 
1371     // additionalData?: {[key: string]: Object}
1372     auto additionalData = userInput->GetAdditionalData();
1373     if (additionalData) {
1374         napi_value nAdditionalData = nullptr;
1375         nAdditionalData = OHOS::AppExecFwk::WrapWantParams(env, *additionalData);
1376         napi_set_named_property(env, result, "additionalData", nAdditionalData);
1377     }
1378 
1379     return NapiGetBoolean(env, true);
1380 }
1381 
SetDoNotDisturbDate(const napi_env & env,const NotificationDoNotDisturbDate & date,napi_value & result)1382 napi_value Common::SetDoNotDisturbDate(
1383     const napi_env &env, const NotificationDoNotDisturbDate &date, napi_value &result)
1384 {
1385     ANS_LOGI("enter");
1386     DoNotDisturbType outType = DoNotDisturbType::TYPE_NONE;
1387     if (!DoNotDisturbTypeCToJS(date.GetDoNotDisturbType(), outType)) {
1388         return NapiGetBoolean(env, false);
1389     }
1390 
1391     // type:DoNotDisturbType
1392     napi_value typeNapi = nullptr;
1393     napi_create_int32(env, (int32_t)outType, &typeNapi);
1394     napi_set_named_property(env, result, "type", typeNapi);
1395 
1396     // begin:Date
1397     double begind = double(date.GetBeginDate());
1398     napi_value beginNapi = nullptr;
1399     napi_create_date(env, begind, &beginNapi);
1400     napi_set_named_property(env, result, "begin", beginNapi);
1401 
1402     // end:Date
1403     double endd = double(date.GetEndDate());
1404     napi_value endNapi = nullptr;
1405     napi_create_date(env, endd, &endNapi);
1406     napi_set_named_property(env, result, "end", endNapi);
1407 
1408     return NapiGetBoolean(env, true);
1409 }
1410 
SetEnabledNotificationCallbackData(const napi_env & env,const EnabledNotificationCallbackData & data,napi_value & result)1411 napi_value Common::SetEnabledNotificationCallbackData(const napi_env &env, const EnabledNotificationCallbackData &data,
1412     napi_value &result)
1413 {
1414     ANS_LOGI("enter");
1415     // bundle: string
1416     napi_value bundleNapi = nullptr;
1417     napi_create_string_utf8(env, data.GetBundle().c_str(), NAPI_AUTO_LENGTH, &bundleNapi);
1418     napi_set_named_property(env, result, "bundle", bundleNapi);
1419 
1420     // uid: uid_t
1421     napi_value uidNapi = nullptr;
1422     napi_create_int32(env, data.GetUid(), &uidNapi);
1423     napi_set_named_property(env, result, "uid", uidNapi);
1424 
1425     // enable: bool
1426     napi_value enableNapi = nullptr;
1427     napi_get_boolean(env, data.GetEnable(), &enableNapi);
1428     napi_set_named_property(env, result, "enable", enableNapi);
1429 
1430     return NapiGetBoolean(env, true);
1431 }
1432 
GetNotificationSubscriberInfo(const napi_env & env,const napi_value & value,NotificationSubscribeInfo & subscriberInfo)1433 napi_value Common::GetNotificationSubscriberInfo(
1434     const napi_env &env, const napi_value &value, NotificationSubscribeInfo &subscriberInfo)
1435 {
1436     ANS_LOGI("enter");
1437     uint32_t length = 0;
1438     size_t strLen = 0;
1439     bool hasProperty = false;
1440     bool isArray = false;
1441     napi_valuetype valuetype = napi_undefined;
1442 
1443     // bundleNames?: Array<string>
1444     NAPI_CALL(env, napi_has_named_property(env, value, "bundleNames", &hasProperty));
1445     if (hasProperty) {
1446         napi_value nBundleNames = nullptr;
1447         napi_get_named_property(env, value, "bundleNames", &nBundleNames);
1448         napi_is_array(env, nBundleNames, &isArray);
1449         if (!isArray) {
1450             ANS_LOGE("Property bundleNames is expected to be an array.");
1451             return nullptr;
1452         }
1453         napi_get_array_length(env, nBundleNames, &length);
1454         if (length == 0) {
1455             ANS_LOGE("The array is empty.");
1456             return nullptr;
1457         }
1458         for (uint32_t i = 0; i < length; ++i) {
1459             napi_value nBundleName = nullptr;
1460             char str[STR_MAX_SIZE] = {0};
1461             napi_get_element(env, nBundleNames, i, &nBundleName);
1462             NAPI_CALL(env, napi_typeof(env, nBundleName, &valuetype));
1463             if (valuetype != napi_string) {
1464                 ANS_LOGE("Wrong argument type. String expected.");
1465                 return nullptr;
1466             }
1467             NAPI_CALL(env, napi_get_value_string_utf8(env, nBundleName, str, STR_MAX_SIZE - 1, &strLen));
1468             subscriberInfo.bundleNames.emplace_back(str);
1469             subscriberInfo.hasSubscribeInfo = true;
1470         }
1471     }
1472 
1473     // userId?: number
1474     NAPI_CALL(env, napi_has_named_property(env, value, "userId", &hasProperty));
1475     if (hasProperty) {
1476         napi_value nUserId = nullptr;
1477         napi_get_named_property(env, value, "userId", &nUserId);
1478         NAPI_CALL(env, napi_typeof(env, nUserId, &valuetype));
1479         if (valuetype != napi_number) {
1480             ANS_LOGE("Wrong argument type. Number expected.");
1481             return nullptr;
1482         }
1483         NAPI_CALL(env, napi_get_value_int32(env, nUserId, &subscriberInfo.userId));
1484         subscriberInfo.hasSubscribeInfo = true;
1485     }
1486 
1487     return NapiGetNull(env);
1488 }
1489 
GetNotificationRequestByNumber(const napi_env & env,const napi_value & value,NotificationRequest & request)1490 napi_value Common::GetNotificationRequestByNumber(
1491     const napi_env &env, const napi_value &value, NotificationRequest &request)
1492 {
1493     ANS_LOGI("enter");
1494     // id?: number
1495     if (GetNotificationId(env, value, request) == nullptr) {
1496         return nullptr;
1497     }
1498     // deliveryTime?: number
1499     if (GetNotificationDeliveryTime(env, value, request) == nullptr) {
1500         return nullptr;
1501     }
1502     // autoDeletedTime?: number
1503     if (GetNotificationAutoDeletedTime(env, value, request) == nullptr) {
1504         return nullptr;
1505     }
1506     // color?: number
1507     if (GetNotificationColor(env, value, request) == nullptr) {
1508         return nullptr;
1509     }
1510     // badgeIconStyle?: number
1511     if (GetNotificationBadgeIconStyle(env, value, request) == nullptr) {
1512         return nullptr;
1513     }
1514     // badgeNumber?: number
1515     if (GetNotificationBadgeNumber(env, value, request) == nullptr) {
1516         return nullptr;
1517     }
1518 
1519     return NapiGetNull(env);
1520 }
1521 
GetNotificationRequestByString(const napi_env & env,const napi_value & value,NotificationRequest & request)1522 napi_value Common::GetNotificationRequestByString(
1523     const napi_env &env, const napi_value &value, NotificationRequest &request)
1524 {
1525     ANS_LOGI("enter");
1526     // classification?: string
1527     if (GetNotificationClassification(env, value, request) == nullptr) {
1528         return nullptr;
1529     }
1530     // statusBarText?: string
1531     if (GetNotificationStatusBarText(env, value, request) == nullptr) {
1532         return nullptr;
1533     }
1534     // label?: string
1535     if (GetNotificationLabel(env, value, request) == nullptr) {
1536         return nullptr;
1537     }
1538     // groupName?: string
1539     if (GetNotificationGroupName(env, value, request) == nullptr) {
1540         return nullptr;
1541     }
1542     return NapiGetNull(env);
1543 }
1544 
GetNotificationRequestByBool(const napi_env & env,const napi_value & value,NotificationRequest & request)1545 napi_value Common::GetNotificationRequestByBool(
1546     const napi_env &env, const napi_value &value, NotificationRequest &request)
1547 {
1548     ANS_LOGI("enter");
1549     // isOngoing?: boolean
1550     if (GetNotificationIsOngoing(env, value, request) == nullptr) {
1551         return nullptr;
1552     }
1553     // isUnremovable?: boolean
1554     if (GetNotificationIsUnremovable(env, value, request) == nullptr) {
1555         return nullptr;
1556     }
1557     // tapDismissed?: boolean
1558     if (GetNotificationtapDismissed(env, value, request) == nullptr) {
1559         return nullptr;
1560     }
1561     // colorEnabled?: boolean
1562     if (GetNotificationColorEnabled(env, value, request) == nullptr) {
1563         return nullptr;
1564     }
1565     // isAlertOnce?: boolean
1566     if (GetNotificationIsAlertOnce(env, value, request) == nullptr) {
1567         return nullptr;
1568     }
1569     // isStopwatch?: boolean
1570     if (GetNotificationIsStopwatch(env, value, request) == nullptr) {
1571         return nullptr;
1572     }
1573     // isCountDown?: boolean
1574     if (GetNotificationIsCountDown(env, value, request) == nullptr) {
1575         return nullptr;
1576     }
1577     // showDeliveryTime?: boolean
1578     if (GetNotificationShowDeliveryTime(env, value, request) == nullptr) {
1579         return nullptr;
1580     }
1581     return NapiGetNull(env);
1582 }
1583 
GetNotificationRequestByCustom(const napi_env & env,const napi_value & value,NotificationRequest & request)1584 napi_value Common::GetNotificationRequestByCustom(
1585     const napi_env &env, const napi_value &value, NotificationRequest &request)
1586 {
1587     ANS_LOGI("enter");
1588     // content: NotificationContent
1589     if (GetNotificationContent(env, value, request) == nullptr) {
1590         return nullptr;
1591     }
1592     // slotType?: notification.SlotType
1593     if (GetNotificationSlotType(env, value, request) == nullptr) {
1594         return nullptr;
1595     }
1596     // wantAgent?: WantAgent
1597     if (GetNotificationWantAgent(env, value, request) == nullptr) {
1598         return nullptr;
1599     }
1600     // extraInfo?: {[key: string]: any}
1601     if (GetNotificationExtraInfo(env, value, request) == nullptr) {
1602         return nullptr;
1603     }
1604     // removalWantAgent?: WantAgent
1605     if (GetNotificationRemovalWantAgent(env, value, request) == nullptr) {
1606         return nullptr;
1607     }
1608     // maxScreenWantAgent?: WantAgent
1609     if (GetNotificationMaxScreenWantAgent(env, value, request) == nullptr) {
1610         return nullptr;
1611     }
1612     // actionButtons?: Array<NotificationActionButton>
1613     if (GetNotificationActionButtons(env, value, request) == nullptr) {
1614         return nullptr;
1615     }
1616     // smallIcon?: image.PixelMap
1617     if (GetNotificationSmallIcon(env, value, request) == nullptr) {
1618         return nullptr;
1619     }
1620     // largeIcon?: image.PixelMap
1621     if (GetNotificationLargeIcon(env, value, request) == nullptr) {
1622         return nullptr;
1623     }
1624     // distributedOption?:DistributedOptions
1625     if (GetNotificationRequestDistributedOptions(env, value, request) == nullptr) {
1626         return nullptr;
1627     }
1628     // template?: NotificationTemplate
1629     if (GetNotificationTemplate(env, value, request) == nullptr) {
1630         return nullptr;
1631     }
1632     return NapiGetNull(env);
1633 }
1634 
GetNotificationRequest(const napi_env & env,const napi_value & value,NotificationRequest & request)1635 napi_value Common::GetNotificationRequest(const napi_env &env, const napi_value &value, NotificationRequest &request)
1636 {
1637     ANS_LOGI("enter");
1638     if (!GetNotificationRequestByNumber(env, value, request)) {
1639         return nullptr;
1640     }
1641     if (!GetNotificationRequestByString(env, value, request)) {
1642         return nullptr;
1643     }
1644     if (!GetNotificationRequestByBool(env, value, request)) {
1645         return nullptr;
1646     }
1647     if (!GetNotificationRequestByCustom(env, value, request)) {
1648         return nullptr;
1649     }
1650     return NapiGetNull(env);
1651 }
1652 
GetNotificationId(const napi_env & env,const napi_value & value,NotificationRequest & request)1653 napi_value Common::GetNotificationId(const napi_env &env, const napi_value &value, NotificationRequest &request)
1654 {
1655     ANS_LOGI("enter");
1656 
1657     napi_valuetype valuetype = napi_undefined;
1658     napi_value result = nullptr;
1659     bool hasProperty = false;
1660     int32_t notificationId = 0;
1661 
1662     NAPI_CALL(env, napi_has_named_property(env, value, "id", &hasProperty));
1663     if (hasProperty) {
1664         napi_get_named_property(env, value, "id", &result);
1665         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1666         if (valuetype != napi_number) {
1667             ANS_LOGE("Wrong argument type. Number expected.");
1668             return nullptr;
1669         }
1670         napi_get_value_int32(env, result, &notificationId);
1671         request.SetNotificationId(notificationId);
1672         ANS_LOGI("notificationId = %{public}d", notificationId);
1673     } else {
1674         ANS_LOGI("default notificationId = 0");
1675         request.SetNotificationId(0);
1676     }
1677 
1678     return NapiGetNull(env);
1679 }
1680 
GetNotificationSlotType(const napi_env & env,const napi_value & value,NotificationRequest & request)1681 napi_value Common::GetNotificationSlotType(const napi_env &env, const napi_value &value, NotificationRequest &request)
1682 {
1683     ANS_LOGI("enter");
1684 
1685     napi_valuetype valuetype = napi_undefined;
1686     napi_value result = nullptr;
1687     bool hasProperty = false;
1688     int32_t slotType = 0;
1689 
1690     NAPI_CALL(env, napi_has_named_property(env, value, "slotType", &hasProperty));
1691     if (hasProperty) {
1692         napi_get_named_property(env, value, "slotType", &result);
1693         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1694         if (valuetype != napi_number) {
1695             ANS_LOGE("Wrong argument type. Number expected.");
1696             return nullptr;
1697         }
1698         napi_get_value_int32(env, result, &slotType);
1699 
1700         NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER;
1701         if (!SlotTypeJSToC(SlotType(slotType), outType)) {
1702             return nullptr;
1703         }
1704         request.SetSlotType(outType);
1705         ANS_LOGI("slotType = %{public}d", slotType);
1706     } else {
1707         ANS_LOGI("default slotType = OTHER");
1708         request.SetSlotType(NotificationConstant::OTHER);
1709     }
1710 
1711     return NapiGetNull(env);
1712 }
1713 
GetNotificationContent(const napi_env & env,const napi_value & value,NotificationRequest & request)1714 napi_value Common::GetNotificationContent(const napi_env &env, const napi_value &value, NotificationRequest &request)
1715 {
1716     ANS_LOGI("enter");
1717 
1718     napi_valuetype valuetype = napi_undefined;
1719     napi_value result = nullptr;
1720     bool hasProperty = false;
1721     int32_t type = 0;
1722 
1723     NAPI_CALL(env, napi_has_named_property(env, value, "content", &hasProperty));
1724     if (!hasProperty) {
1725         ANS_LOGE("Property content expected.");
1726         return nullptr;
1727     }
1728 
1729     napi_get_named_property(env, value, "content", &result);
1730     NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1731     if (valuetype != napi_object) {
1732         ANS_LOGE("Wrong argument type. Object expected.");
1733         return nullptr;
1734     }
1735     if (GetNotificationContentType(env, result, type) == nullptr) {
1736         return nullptr;
1737     }
1738     NotificationContent::Type outType = NotificationContent::Type::NONE;
1739     if (!ContentTypeJSToC(ContentType(type), outType)) {
1740         return nullptr;
1741     }
1742     switch (outType) {
1743         case NotificationContent::Type::BASIC_TEXT:
1744             if (GetNotificationBasicContent(env, result, request) == nullptr) {
1745                 return nullptr;
1746             }
1747             break;
1748         case NotificationContent::Type::LONG_TEXT:
1749             if (GetNotificationLongTextContent(env, result, request) == nullptr) {
1750                 return nullptr;
1751             }
1752             break;
1753         case NotificationContent::Type::PICTURE:
1754             if (GetNotificationPictureContent(env, result, request) == nullptr) {
1755                 return nullptr;
1756             }
1757             break;
1758         case NotificationContent::Type::CONVERSATION:
1759             if (GetNotificationConversationalContent(env, result, request) == nullptr) {
1760                 return nullptr;
1761             }
1762             break;
1763         case NotificationContent::Type::MULTILINE:
1764             if (GetNotificationMultiLineContent(env, result, request) == nullptr) {
1765                 return nullptr;
1766             }
1767             break;
1768         default:
1769             return nullptr;
1770     }
1771 
1772     return NapiGetNull(env);
1773 }
1774 
GetNotificationIsOngoing(const napi_env & env,const napi_value & value,NotificationRequest & request)1775 napi_value Common::GetNotificationIsOngoing(const napi_env &env, const napi_value &value, NotificationRequest &request)
1776 {
1777     ANS_LOGI("enter");
1778 
1779     napi_valuetype valuetype = napi_undefined;
1780     napi_value result = nullptr;
1781     bool hasProperty = false;
1782     bool isOngoing = false;
1783 
1784     NAPI_CALL(env, napi_has_named_property(env, value, "isOngoing", &hasProperty));
1785     if (hasProperty) {
1786         napi_get_named_property(env, value, "isOngoing", &result);
1787         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1788         if (valuetype != napi_boolean) {
1789             ANS_LOGE("Wrong argument type. Bool expected.");
1790             return nullptr;
1791         }
1792         napi_get_value_bool(env, result, &isOngoing);
1793         request.SetInProgress(isOngoing);
1794     }
1795 
1796     return NapiGetNull(env);
1797 }
1798 
GetNotificationIsUnremovable(const napi_env & env,const napi_value & value,NotificationRequest & request)1799 napi_value Common::GetNotificationIsUnremovable(
1800     const napi_env &env, const napi_value &value, NotificationRequest &request)
1801 {
1802     ANS_LOGI("enter");
1803 
1804     napi_valuetype valuetype = napi_undefined;
1805     napi_value result = nullptr;
1806     bool hasProperty = false;
1807     bool isUnremovable = false;
1808 
1809     NAPI_CALL(env, napi_has_named_property(env, value, "isUnremovable", &hasProperty));
1810     if (hasProperty) {
1811         napi_get_named_property(env, value, "isUnremovable", &result);
1812         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1813         if (valuetype != napi_boolean) {
1814             ANS_LOGE("Wrong argument type. Bool expected.");
1815             return nullptr;
1816         }
1817         napi_get_value_bool(env, result, &isUnremovable);
1818         request.SetUnremovable(isUnremovable);
1819     }
1820 
1821     return NapiGetNull(env);
1822 }
1823 
GetNotificationDeliveryTime(const napi_env & env,const napi_value & value,NotificationRequest & request)1824 napi_value Common::GetNotificationDeliveryTime(
1825     const napi_env &env, const napi_value &value, NotificationRequest &request)
1826 {
1827     ANS_LOGI("enter");
1828 
1829     napi_valuetype valuetype = napi_undefined;
1830     napi_value result = nullptr;
1831     bool hasProperty = false;
1832     int64_t deliveryTime = 0;
1833 
1834     NAPI_CALL(env, napi_has_named_property(env, value, "deliveryTime", &hasProperty));
1835     if (hasProperty) {
1836         napi_get_named_property(env, value, "deliveryTime", &result);
1837         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1838         if (valuetype != napi_number) {
1839             ANS_LOGE("Wrong argument type. Number expected.");
1840             return nullptr;
1841         }
1842         napi_get_value_int64(env, result, &deliveryTime);
1843         request.SetDeliveryTime(deliveryTime);
1844     }
1845 
1846     return NapiGetNull(env);
1847 }
1848 
GetNotificationtapDismissed(const napi_env & env,const napi_value & value,NotificationRequest & request)1849 napi_value Common::GetNotificationtapDismissed(
1850     const napi_env &env, const napi_value &value, NotificationRequest &request)
1851 {
1852     ANS_LOGI("enter");
1853 
1854     napi_valuetype valuetype = napi_undefined;
1855     napi_value result = nullptr;
1856     bool hasProperty = false;
1857     bool tapDismissed = true;
1858 
1859     NAPI_CALL(env, napi_has_named_property(env, value, "tapDismissed", &hasProperty));
1860     if (hasProperty) {
1861         napi_get_named_property(env, value, "tapDismissed", &result);
1862         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1863         if (valuetype != napi_boolean) {
1864             ANS_LOGE("Wrong argument type. Bool expected.");
1865             return nullptr;
1866         }
1867         napi_get_value_bool(env, result, &tapDismissed);
1868         request.SetTapDismissed(tapDismissed);
1869     }
1870 
1871     return NapiGetNull(env);
1872 }
1873 
GetNotificationWantAgent(const napi_env & env,const napi_value & value,NotificationRequest & request)1874 napi_value Common::GetNotificationWantAgent(const napi_env &env, const napi_value &value, NotificationRequest &request)
1875 {
1876     ANS_LOGI("enter");
1877 
1878     bool hasProperty = false;
1879     AbilityRuntime::WantAgent::WantAgent *wantAgent = nullptr;
1880     napi_value result = nullptr;
1881     napi_valuetype valuetype = napi_undefined;
1882 
1883     NAPI_CALL(env, napi_has_named_property(env, value, "wantAgent", &hasProperty));
1884     if (hasProperty) {
1885         napi_get_named_property(env, value, "wantAgent", &result);
1886         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1887         if (valuetype != napi_object) {
1888             ANS_LOGE("Wrong argument type. Object expected.");
1889             return nullptr;
1890         }
1891         napi_unwrap(env, result, (void **)&wantAgent);
1892         if (wantAgent == nullptr) {
1893             ANS_LOGE("Invalid object wantAgent");
1894             return nullptr;
1895         }
1896         std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> sWantAgent =
1897             std::make_shared<AbilityRuntime::WantAgent::WantAgent>(*wantAgent);
1898         request.SetWantAgent(sWantAgent);
1899     }
1900 
1901     return NapiGetNull(env);
1902 }
1903 
GetNotificationExtraInfo(const napi_env & env,const napi_value & value,NotificationRequest & request)1904 napi_value Common::GetNotificationExtraInfo(const napi_env &env, const napi_value &value, NotificationRequest &request)
1905 {
1906     ANS_LOGI("enter");
1907 
1908     napi_valuetype valuetype = napi_undefined;
1909     napi_value result = nullptr;
1910     bool hasProperty = false;
1911 
1912     NAPI_CALL(env, napi_has_named_property(env, value, "extraInfo", &hasProperty));
1913     if (hasProperty) {
1914         napi_get_named_property(env, value, "extraInfo", &result);
1915         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1916         if (valuetype != napi_object) {
1917             ANS_LOGE("Wrong argument type. Object expected.");
1918             return nullptr;
1919         }
1920         AAFwk::WantParams wantParams;
1921         if (!OHOS::AppExecFwk::UnwrapWantParams(env, result, wantParams)) {
1922             return nullptr;
1923         }
1924 
1925         std::shared_ptr<AAFwk::WantParams> extras = std::make_shared<AAFwk::WantParams>(wantParams);
1926         request.SetAdditionalData(extras);
1927     }
1928 
1929     return NapiGetNull(env);
1930 }
1931 
GetNotificationGroupName(const napi_env & env,const napi_value & value,NotificationRequest & request)1932 napi_value Common::GetNotificationGroupName(const napi_env &env, const napi_value &value, NotificationRequest &request)
1933 {
1934     ANS_LOGI("enter");
1935 
1936     napi_valuetype valuetype = napi_undefined;
1937     napi_value result = nullptr;
1938     bool hasProperty = false;
1939     size_t strLen = 0;
1940 
1941     NAPI_CALL(env, napi_has_named_property(env, value, "groupName", &hasProperty));
1942     if (hasProperty) {
1943         napi_get_named_property(env, value, "groupName", &result);
1944         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1945         if (valuetype != napi_string) {
1946             ANS_LOGE("Wrong argument type. String expected.");
1947             return nullptr;
1948         }
1949         char str[STR_MAX_SIZE] = {0};
1950         NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
1951         request.SetGroupName(str);
1952     }
1953 
1954     return NapiGetNull(env);
1955 }
1956 
GetNotificationRemovalWantAgent(const napi_env & env,const napi_value & value,NotificationRequest & request)1957 napi_value Common::GetNotificationRemovalWantAgent(
1958     const napi_env &env, const napi_value &value, NotificationRequest &request)
1959 {
1960     ANS_LOGI("enter");
1961 
1962     bool hasProperty = false;
1963     AbilityRuntime::WantAgent::WantAgent *wantAgent = nullptr;
1964     napi_value result = nullptr;
1965     napi_valuetype valuetype = napi_undefined;
1966 
1967     NAPI_CALL(env, napi_has_named_property(env, value, "removalWantAgent", &hasProperty));
1968     if (hasProperty) {
1969         napi_get_named_property(env, value, "removalWantAgent", &result);
1970         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
1971         if (valuetype != napi_object) {
1972             ANS_LOGE("Wrong argument type. Object expected.");
1973             return nullptr;
1974         }
1975         napi_unwrap(env, result, (void **)&wantAgent);
1976         if (wantAgent == nullptr) {
1977             ANS_LOGE("Invalid object removalWantAgent");
1978             return nullptr;
1979         }
1980         std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> removeWantAgent =
1981             std::make_shared<AbilityRuntime::WantAgent::WantAgent>(*wantAgent);
1982         request.SetRemovalWantAgent(removeWantAgent);
1983     }
1984 
1985     return NapiGetNull(env);
1986 }
1987 
GetNotificationMaxScreenWantAgent(const napi_env & env,const napi_value & value,NotificationRequest & request)1988 napi_value Common::GetNotificationMaxScreenWantAgent(
1989     const napi_env &env, const napi_value &value, NotificationRequest &request)
1990 {
1991     ANS_LOGI("enter");
1992 
1993     bool hasProperty = false;
1994     AbilityRuntime::WantAgent::WantAgent *wantAgent = nullptr;
1995     napi_value result = nullptr;
1996     napi_valuetype valuetype = napi_undefined;
1997 
1998     NAPI_CALL(env, napi_has_named_property(env, value, "maxScreenWantAgent", &hasProperty));
1999     if (hasProperty) {
2000         napi_get_named_property(env, value, "maxScreenWantAgent", &result);
2001         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
2002         if (valuetype != napi_object) {
2003             ANS_LOGE("Wrong argument type. Object expected.");
2004             return nullptr;
2005         }
2006         napi_unwrap(env, result, (void **)&wantAgent);
2007         if (wantAgent == nullptr) {
2008             ANS_LOGE("Invalid object maxScreenWantAgent");
2009             return nullptr;
2010         }
2011         std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> maxScreenWantAgent =
2012             std::make_shared<AbilityRuntime::WantAgent::WantAgent>(*wantAgent);
2013         request.SetMaxScreenWantAgent(maxScreenWantAgent);
2014     }
2015 
2016     return NapiGetNull(env);
2017 }
2018 
GetNotificationAutoDeletedTime(const napi_env & env,const napi_value & value,NotificationRequest & request)2019 napi_value Common::GetNotificationAutoDeletedTime(
2020     const napi_env &env, const napi_value &value, NotificationRequest &request)
2021 {
2022     ANS_LOGI("enter");
2023 
2024     napi_valuetype valuetype = napi_undefined;
2025     napi_value result = nullptr;
2026     bool hasProperty = false;
2027     int64_t autoDeletedTime = 0;
2028 
2029     NAPI_CALL(env, napi_has_named_property(env, value, "autoDeletedTime", &hasProperty));
2030     if (hasProperty) {
2031         napi_get_named_property(env, value, "autoDeletedTime", &result);
2032         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
2033         if (valuetype != napi_number) {
2034             ANS_LOGE("Wrong argument type. Number expected.");
2035             return nullptr;
2036         }
2037         napi_get_value_int64(env, result, &autoDeletedTime);
2038         request.SetAutoDeletedTime(autoDeletedTime);
2039     }
2040 
2041     return NapiGetNull(env);
2042 }
2043 
GetNotificationClassification(const napi_env & env,const napi_value & value,NotificationRequest & request)2044 napi_value Common::GetNotificationClassification(
2045     const napi_env &env, const napi_value &value, NotificationRequest &request)
2046 {
2047     ANS_LOGI("enter");
2048 
2049     napi_valuetype valuetype = napi_undefined;
2050     napi_value result = nullptr;
2051     bool hasProperty = false;
2052     size_t strLen = 0;
2053 
2054     NAPI_CALL(env, napi_has_named_property(env, value, "classification", &hasProperty));
2055     if (hasProperty) {
2056         napi_get_named_property(env, value, "classification", &result);
2057         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
2058         if (valuetype != napi_string) {
2059             ANS_LOGE("Wrong argument type. String expected.");
2060             return nullptr;
2061         }
2062         char str[STR_MAX_SIZE] = {0};
2063         NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
2064         request.SetClassification(str);
2065     }
2066 
2067     return NapiGetNull(env);
2068 }
2069 
GetNotificationColor(const napi_env & env,const napi_value & value,NotificationRequest & request)2070 napi_value Common::GetNotificationColor(const napi_env &env, const napi_value &value, NotificationRequest &request)
2071 {
2072     ANS_LOGI("enter");
2073 
2074     napi_valuetype valuetype = napi_undefined;
2075     napi_value result = nullptr;
2076     bool hasProperty = false;
2077     int32_t color = 0;
2078 
2079     NAPI_CALL(env, napi_has_named_property(env, value, "color", &hasProperty));
2080     if (hasProperty) {
2081         napi_get_named_property(env, value, "color", &result);
2082         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
2083         if (valuetype != napi_number) {
2084             ANS_LOGE("Wrong argument type. Number expected.");
2085             return nullptr;
2086         }
2087         napi_get_value_int32(env, result, &color);
2088         if (color < 0) {
2089             ANS_LOGE("Wrong argument type. Natural number expected.");
2090             return nullptr;
2091         }
2092         request.SetColor(color);
2093     }
2094 
2095     return NapiGetNull(env);
2096 }
2097 
GetNotificationColorEnabled(const napi_env & env,const napi_value & value,NotificationRequest & request)2098 napi_value Common::GetNotificationColorEnabled(
2099     const napi_env &env, const napi_value &value, NotificationRequest &request)
2100 {
2101     ANS_LOGI("enter");
2102 
2103     napi_valuetype valuetype = napi_undefined;
2104     napi_value result = nullptr;
2105     bool hasProperty = false;
2106     bool colorEnabled = false;
2107 
2108     NAPI_CALL(env, napi_has_named_property(env, value, "colorEnabled", &hasProperty));
2109     if (hasProperty) {
2110         napi_get_named_property(env, value, "colorEnabled", &result);
2111         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
2112         if (valuetype != napi_boolean) {
2113             ANS_LOGE("Wrong argument type. Bool expected.");
2114             return nullptr;
2115         }
2116         napi_get_value_bool(env, result, &colorEnabled);
2117         request.SetColorEnabled(colorEnabled);
2118     }
2119 
2120     return NapiGetNull(env);
2121 }
2122 
GetNotificationIsAlertOnce(const napi_env & env,const napi_value & value,NotificationRequest & request)2123 napi_value Common::GetNotificationIsAlertOnce(
2124     const napi_env &env, const napi_value &value, NotificationRequest &request)
2125 {
2126     ANS_LOGI("enter");
2127 
2128     napi_valuetype valuetype = napi_undefined;
2129     napi_value result = nullptr;
2130     bool hasProperty = false;
2131     bool isAlertOnce = false;
2132 
2133     NAPI_CALL(env, napi_has_named_property(env, value, "isAlertOnce", &hasProperty));
2134     if (hasProperty) {
2135         napi_get_named_property(env, value, "isAlertOnce", &result);
2136         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
2137         if (valuetype != napi_boolean) {
2138             ANS_LOGE("Wrong argument type. Bool expected.");
2139             return nullptr;
2140         }
2141         napi_get_value_bool(env, result, &isAlertOnce);
2142         request.SetAlertOneTime(isAlertOnce);
2143     }
2144 
2145     return NapiGetNull(env);
2146 }
2147 
GetNotificationIsStopwatch(const napi_env & env,const napi_value & value,NotificationRequest & request)2148 napi_value Common::GetNotificationIsStopwatch(
2149     const napi_env &env, const napi_value &value, NotificationRequest &request)
2150 {
2151     ANS_LOGI("enter");
2152 
2153     napi_valuetype valuetype = napi_undefined;
2154     napi_value result = nullptr;
2155     bool hasProperty = false;
2156     bool isStopwatch = false;
2157 
2158     NAPI_CALL(env, napi_has_named_property(env, value, "isStopwatch", &hasProperty));
2159     if (hasProperty) {
2160         napi_get_named_property(env, value, "isStopwatch", &result);
2161         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
2162         if (valuetype != napi_boolean) {
2163             ANS_LOGE("Wrong argument type. Bool expected.");
2164             return nullptr;
2165         }
2166         napi_get_value_bool(env, result, &isStopwatch);
2167         request.SetShowStopwatch(isStopwatch);
2168     }
2169 
2170     return NapiGetNull(env);
2171 }
2172 
GetNotificationIsCountDown(const napi_env & env,const napi_value & value,NotificationRequest & request)2173 napi_value Common::GetNotificationIsCountDown(
2174     const napi_env &env, const napi_value &value, NotificationRequest &request)
2175 {
2176     ANS_LOGI("enter");
2177 
2178     napi_valuetype valuetype = napi_undefined;
2179     napi_value result = nullptr;
2180     bool hasProperty = false;
2181     bool isCountDown = false;
2182 
2183     NAPI_CALL(env, napi_has_named_property(env, value, "isCountDown", &hasProperty));
2184     if (hasProperty) {
2185         napi_get_named_property(env, value, "isCountDown", &result);
2186         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
2187         if (valuetype != napi_boolean) {
2188             ANS_LOGE("Wrong argument type. Bool expected.");
2189             return nullptr;
2190         }
2191         napi_get_value_bool(env, result, &isCountDown);
2192         request.SetCountdownTimer(isCountDown);
2193     }
2194 
2195     return NapiGetNull(env);
2196 }
2197 
GetNotificationStatusBarText(const napi_env & env,const napi_value & value,NotificationRequest & request)2198 napi_value Common::GetNotificationStatusBarText(
2199     const napi_env &env, const napi_value &value, NotificationRequest &request)
2200 {
2201     ANS_LOGI("enter");
2202 
2203     napi_valuetype valuetype = napi_undefined;
2204     napi_value result = nullptr;
2205     bool hasProperty = false;
2206     size_t strLen = 0;
2207 
2208     NAPI_CALL(env, napi_has_named_property(env, value, "statusBarText", &hasProperty));
2209     if (hasProperty) {
2210         napi_get_named_property(env, value, "statusBarText", &result);
2211         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
2212         if (valuetype != napi_string) {
2213             ANS_LOGE("Wrong argument type. String expected.");
2214             return nullptr;
2215         }
2216         char str[STR_MAX_SIZE] = {0};
2217         NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
2218         request.SetStatusBarText(str);
2219     }
2220 
2221     return NapiGetNull(env);
2222 }
2223 
GetNotificationLabel(const napi_env & env,const napi_value & value,NotificationRequest & request)2224 napi_value Common::GetNotificationLabel(const napi_env &env, const napi_value &value, NotificationRequest &request)
2225 {
2226     ANS_LOGI("enter");
2227 
2228     napi_valuetype valuetype = napi_undefined;
2229     napi_value result = nullptr;
2230     bool hasProperty = false;
2231     size_t strLen = 0;
2232 
2233     NAPI_CALL(env, napi_has_named_property(env, value, "label", &hasProperty));
2234     if (hasProperty) {
2235         napi_get_named_property(env, value, "label", &result);
2236         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
2237         if (valuetype != napi_string) {
2238             ANS_LOGE("Wrong argument type. String expected.");
2239             return nullptr;
2240         }
2241         char str[STR_MAX_SIZE] = {0};
2242         NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
2243         request.SetLabel(str);
2244     }
2245 
2246     return NapiGetNull(env);
2247 }
2248 
GetNotificationBadgeIconStyle(const napi_env & env,const napi_value & value,NotificationRequest & request)2249 napi_value Common::GetNotificationBadgeIconStyle(
2250     const napi_env &env, const napi_value &value, NotificationRequest &request)
2251 {
2252     ANS_LOGI("enter");
2253 
2254     napi_valuetype valuetype = napi_undefined;
2255     napi_value result = nullptr;
2256     bool hasProperty = false;
2257     int32_t badgeIconStyle = 0;
2258 
2259     NAPI_CALL(env, napi_has_named_property(env, value, "badgeIconStyle", &hasProperty));
2260     if (hasProperty) {
2261         napi_get_named_property(env, value, "badgeIconStyle", &result);
2262         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
2263         if (valuetype != napi_number) {
2264             ANS_LOGE("Wrong argument type. Number expected.");
2265             return nullptr;
2266         }
2267         napi_get_value_int32(env, result, &badgeIconStyle);
2268         request.SetBadgeIconStyle(static_cast<NotificationRequest::BadgeStyle>(badgeIconStyle));
2269     }
2270 
2271     return NapiGetNull(env);
2272 }
2273 
GetNotificationShowDeliveryTime(const napi_env & env,const napi_value & value,NotificationRequest & request)2274 napi_value Common::GetNotificationShowDeliveryTime(
2275     const napi_env &env, const napi_value &value, NotificationRequest &request)
2276 {
2277     ANS_LOGI("enter");
2278 
2279     napi_valuetype valuetype = napi_undefined;
2280     napi_value result = nullptr;
2281     bool hasProperty = false;
2282     bool showDeliveryTime = false;
2283 
2284     NAPI_CALL(env, napi_has_named_property(env, value, "showDeliveryTime", &hasProperty));
2285     if (hasProperty) {
2286         napi_get_named_property(env, value, "showDeliveryTime", &result);
2287         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
2288         if (valuetype != napi_boolean) {
2289             ANS_LOGE("Wrong argument type. Bool expected.");
2290             return nullptr;
2291         }
2292         napi_get_value_bool(env, result, &showDeliveryTime);
2293         request.SetShowDeliveryTime(showDeliveryTime);
2294     }
2295 
2296     return NapiGetNull(env);
2297 }
2298 
GetNotificationActionButtons(const napi_env & env,const napi_value & value,NotificationRequest & request)2299 napi_value Common::GetNotificationActionButtons(
2300     const napi_env &env, const napi_value &value, NotificationRequest &request)
2301 {
2302     ANS_LOGI("enter");
2303 
2304     bool isArray = false;
2305     napi_valuetype valuetype = napi_undefined;
2306     napi_value actionButtons = nullptr;
2307     uint32_t length = 0;
2308     bool hasProperty = false;
2309 
2310     napi_has_named_property(env, value, "actionButtons", &hasProperty);
2311     if (!hasProperty) {
2312         return Common::NapiGetNull(env);
2313     }
2314 
2315     napi_get_named_property(env, value, "actionButtons", &actionButtons);
2316     napi_is_array(env, actionButtons, &isArray);
2317     if (!isArray) {
2318         ANS_LOGE("Property actionButtons is expected to be an array.");
2319         return nullptr;
2320     }
2321     napi_get_array_length(env, actionButtons, &length);
2322     if (length == 0) {
2323         ANS_LOGE("The array is empty.");
2324         return nullptr;
2325     }
2326     for (size_t i = 0; i < length; i++) {
2327         napi_value actionButton = nullptr;
2328         napi_get_element(env, actionButtons, i, &actionButton);
2329         NAPI_CALL(env, napi_typeof(env, actionButton, &valuetype));
2330         if (valuetype != napi_object) {
2331             ANS_LOGE("Wrong argument type. Object expected.");
2332             return nullptr;
2333         }
2334 
2335         std::shared_ptr<NotificationActionButton> pActionButton = nullptr;
2336         if (GetNotificationActionButtonsDetailed(env, actionButton, pActionButton) == nullptr) {
2337             return nullptr;
2338         }
2339         request.AddActionButton(pActionButton);
2340     }
2341 
2342     return NapiGetNull(env);
2343 }
2344 
GetNotificationActionButtonsDetailed(const napi_env & env,const napi_value & actionButton,std::shared_ptr<NotificationActionButton> & pActionButton)2345 napi_value Common::GetNotificationActionButtonsDetailed(
2346     const napi_env &env, const napi_value &actionButton, std::shared_ptr<NotificationActionButton> &pActionButton)
2347 {
2348     ANS_LOGI("enter");
2349 
2350     if (!GetNotificationActionButtonsDetailedBasicInfo(env, actionButton, pActionButton)) {
2351         return nullptr;
2352     }
2353     if (!GetNotificationActionButtonsDetailedByExtras(env, actionButton, pActionButton)) {
2354         return nullptr;
2355     }
2356     if (!GetNotificationUserInput(env, actionButton, pActionButton)) {
2357         return nullptr;
2358     }
2359     return NapiGetNull(env);
2360 }
2361 
GetNotificationActionButtonsDetailedBasicInfo(const napi_env & env,const napi_value & actionButton,std::shared_ptr<NotificationActionButton> & pActionButton)2362 napi_value Common::GetNotificationActionButtonsDetailedBasicInfo(
2363     const napi_env &env, const napi_value &actionButton, std::shared_ptr<NotificationActionButton> &pActionButton)
2364 {
2365     ANS_LOGI("enter");
2366     napi_valuetype valuetype = napi_undefined;
2367     bool hasProperty = false;
2368     char str[STR_MAX_SIZE] = {0};
2369     size_t strLen = 0;
2370     napi_value value = nullptr;
2371     std::string title;
2372     AbilityRuntime::WantAgent::WantAgent *wantAgentPtr = nullptr;
2373     std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
2374     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> wantAgent;
2375 
2376     // title: string
2377     NAPI_CALL(env, napi_has_named_property(env, actionButton, "title", &hasProperty));
2378     if (!hasProperty) {
2379         ANS_LOGE("Property title expected.");
2380         return nullptr;
2381     }
2382     napi_get_named_property(env, actionButton, "title", &value);
2383     NAPI_CALL(env, napi_typeof(env, value, &valuetype));
2384     if (valuetype != napi_string) {
2385         ANS_LOGE("Wrong argument type. String expected.");
2386         return nullptr;
2387     }
2388     NAPI_CALL(env, napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen));
2389     title = str;
2390 
2391     // wantAgent: WantAgent
2392     NAPI_CALL(env, napi_has_named_property(env, actionButton, "wantAgent", &hasProperty));
2393     if (!hasProperty) {
2394         ANS_LOGE("Property wantAgent expected.");
2395         return nullptr;
2396     }
2397     napi_get_named_property(env, actionButton, "wantAgent", &value);
2398     NAPI_CALL(env, napi_typeof(env, value, &valuetype));
2399     if (valuetype != napi_object) {
2400         ANS_LOGE("Wrong argument type. Object expected.");
2401         return nullptr;
2402     }
2403     napi_unwrap(env, value, (void **)&wantAgentPtr);
2404     if (wantAgentPtr == nullptr) {
2405         ANS_LOGE("Invalid object wantAgent");
2406         return nullptr;
2407     }
2408     wantAgent = std::make_shared<AbilityRuntime::WantAgent::WantAgent>(*wantAgentPtr);
2409 
2410     // icon?: image.PixelMap
2411     NAPI_CALL(env, napi_has_named_property(env, actionButton, "icon", &hasProperty));
2412     if (hasProperty) {
2413         napi_get_named_property(env, actionButton, "icon", &value);
2414         NAPI_CALL(env, napi_typeof(env, value, &valuetype));
2415         if (valuetype != napi_object) {
2416             ANS_LOGE("Wrong argument type. Object expected.");
2417             return nullptr;
2418         }
2419         pixelMap = Media::PixelMapNapi::GetPixelMap(env, value);
2420         if (pixelMap == nullptr) {
2421             ANS_LOGE("Invalid object pixelMap");
2422             return nullptr;
2423         }
2424     }
2425     pActionButton = NotificationActionButton::Create(pixelMap, title, wantAgent);
2426 
2427     return NapiGetNull(env);
2428 }
2429 
GetNotificationActionButtonsDetailedByExtras(const napi_env & env,const napi_value & actionButton,std::shared_ptr<NotificationActionButton> & pActionButton)2430 napi_value Common::GetNotificationActionButtonsDetailedByExtras(
2431     const napi_env &env, const napi_value &actionButton, std::shared_ptr<NotificationActionButton> &pActionButton)
2432 {
2433     ANS_LOGI("enter");
2434 
2435     napi_valuetype valuetype = napi_undefined;
2436     napi_value result = nullptr;
2437     bool hasProperty = false;
2438 
2439     if (!pActionButton) {
2440         ANS_LOGE("pActionButton is nullptr");
2441         return nullptr;
2442     }
2443 
2444     // extras?: {[key: string]: any}
2445     NAPI_CALL(env, napi_has_named_property(env, actionButton, "extras", &hasProperty));
2446     if (hasProperty) {
2447         napi_get_named_property(env, actionButton, "extras", &result);
2448         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
2449         if (valuetype != napi_object) {
2450             ANS_LOGE("Wrong argument type. Object expected.");
2451             return nullptr;
2452         }
2453         AAFwk::WantParams wantParams;
2454         if (!OHOS::AppExecFwk::UnwrapWantParams(env, result, wantParams)) {
2455             return nullptr;
2456         }
2457         pActionButton->AddAdditionalData(wantParams);
2458     }
2459     return NapiGetNull(env);
2460 }
2461 
GetNotificationUserInput(const napi_env & env,const napi_value & actionButton,std::shared_ptr<NotificationActionButton> & pActionButton)2462 napi_value Common::GetNotificationUserInput(
2463     const napi_env &env, const napi_value &actionButton, std::shared_ptr<NotificationActionButton> &pActionButton)
2464 {
2465     ANS_LOGI("enter");
2466     napi_valuetype valuetype = napi_undefined;
2467     napi_value userInputResult = nullptr;
2468     bool hasProperty = false;
2469 
2470     // userInput?: NotificationUserInput
2471     NAPI_CALL(env, napi_has_named_property(env, actionButton, "userInput", &hasProperty));
2472     if (hasProperty) {
2473         napi_get_named_property(env, actionButton, "userInput", &userInputResult);
2474         NAPI_CALL(env, napi_typeof(env, userInputResult, &valuetype));
2475         if (valuetype != napi_object) {
2476             ANS_LOGE("Wrong argument type. Object expected.");
2477             return nullptr;
2478         }
2479         std::shared_ptr<NotificationUserInput> userInput = nullptr;
2480 
2481         if (!GetNotificationUserInputByInputKey(env, userInputResult, userInput)) {
2482             return nullptr;
2483         }
2484         pActionButton->AddNotificationUserInput(userInput);
2485     }
2486 
2487     return NapiGetNull(env);
2488 }
2489 
GetNotificationUserInputByInputKey(const napi_env & env,const napi_value & userInputResult,std::shared_ptr<NotificationUserInput> & userInput)2490 napi_value Common::GetNotificationUserInputByInputKey(
2491     const napi_env &env, const napi_value &userInputResult, std::shared_ptr<NotificationUserInput> &userInput)
2492 {
2493     ANS_LOGI("enter");
2494     napi_valuetype valuetype = napi_undefined;
2495     napi_value value = nullptr;
2496     bool hasProperty = false;
2497     char str[STR_MAX_SIZE] = {0};
2498     size_t strLen = 0;
2499 
2500     // inputKey: string
2501     NAPI_CALL(env, napi_has_named_property(env, userInputResult, "inputKey", &hasProperty));
2502     if (!hasProperty) {
2503         ANS_LOGE("Property inputKey expected.");
2504         return nullptr;
2505     }
2506     napi_get_named_property(env, userInputResult, "inputKey", &value);
2507     NAPI_CALL(env, napi_typeof(env, value, &valuetype));
2508     if (valuetype != napi_string) {
2509         ANS_LOGE("Wrong argument type. String expected.");
2510         return nullptr;
2511     }
2512     NAPI_CALL(env, napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen));
2513     ANS_LOGI("NotificationUserInput::inputKey = %{public}s", str);
2514     userInput = NotificationUserInput::Create(str);
2515     if (!userInput) {
2516         ANS_LOGI("Failed to create NotificationUserInput by inputKey=%{public}s", str);
2517         return nullptr;
2518     }
2519 
2520     return NapiGetNull(env);
2521 }
2522 
GetNotificationUserInputByTag(const napi_env & env,const napi_value & userInputResult,std::shared_ptr<NotificationUserInput> & userInput)2523 napi_value Common::GetNotificationUserInputByTag(
2524     const napi_env &env, const napi_value &userInputResult, std::shared_ptr<NotificationUserInput> &userInput)
2525 {
2526     ANS_LOGI("enter");
2527 
2528     napi_valuetype valuetype = napi_undefined;
2529     napi_value value = nullptr;
2530     bool hasProperty = false;
2531     char str[STR_MAX_SIZE] = {0};
2532     size_t strLen = 0;
2533 
2534     if (!userInput) {
2535         ANS_LOGE("userInput is nullptr");
2536         return nullptr;
2537     }
2538     // tag: string
2539     NAPI_CALL(env, napi_has_named_property(env, userInputResult, "tag", &hasProperty));
2540     if (!hasProperty) {
2541         ANS_LOGE("Property tag expected.");
2542         return nullptr;
2543     }
2544     napi_get_named_property(env, userInputResult, "tag", &value);
2545     NAPI_CALL(env, napi_typeof(env, value, &valuetype));
2546     if (valuetype != napi_string) {
2547         ANS_LOGE("Wrong argument type. String expected.");
2548         return nullptr;
2549     }
2550     NAPI_CALL(env, napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen));
2551     userInput->SetTag(str);
2552     ANS_LOGI("NotificationUserInput::tag = %{public}s", str);
2553 
2554     return NapiGetNull(env);
2555 }
2556 
GetNotificationUserInputByOptions(const napi_env & env,const napi_value & userInputResult,std::shared_ptr<NotificationUserInput> & userInput)2557 napi_value Common::GetNotificationUserInputByOptions(
2558     const napi_env &env, const napi_value &userInputResult, std::shared_ptr<NotificationUserInput> &userInput)
2559 {
2560     ANS_LOGI("enter");
2561 
2562     napi_valuetype valuetype = napi_undefined;
2563     napi_value value = nullptr;
2564     bool hasProperty = false;
2565     size_t strLen = 0;
2566     uint32_t length = 0;
2567     bool isArray = false;
2568 
2569     if (!userInput) {
2570         ANS_LOGE("userInput is nullptr");
2571         return nullptr;
2572     }
2573 
2574     // options: Array<string>
2575     NAPI_CALL(env, napi_has_named_property(env, userInputResult, "options", &hasProperty));
2576 
2577     if (!hasProperty) {
2578         ANS_LOGE("Property options expected.");
2579         return nullptr;
2580     }
2581     napi_get_named_property(env, userInputResult, "options", &value);
2582     napi_is_array(env, value, &isArray);
2583     if (!isArray) {
2584         ANS_LOGE("Property options is expected to be an array.");
2585         return nullptr;
2586     }
2587     napi_get_array_length(env, value, &length);
2588     if (length == 0) {
2589         ANS_LOGE("The array is empty.");
2590         return nullptr;
2591     }
2592     std::vector<std::string> options;
2593     for (uint32_t i = 0; i < length; ++i) {
2594         napi_value option = nullptr;
2595         char str[STR_MAX_SIZE] = {0};
2596         napi_get_element(env, value, i, &option);
2597         NAPI_CALL(env, napi_typeof(env, option, &valuetype));
2598         if (valuetype != napi_string) {
2599             ANS_LOGE("Wrong argument type. String expected.");
2600             return nullptr;
2601         }
2602         NAPI_CALL(env, napi_get_value_string_utf8(env, option, str, STR_MAX_SIZE - 1, &strLen));
2603         options.emplace_back(str);
2604     }
2605     userInput->SetOptions(options);
2606 
2607     return NapiGetNull(env);
2608 }
2609 
GetNotificationUserInputByPermitMimeTypes(const napi_env & env,const napi_value & userInputResult,std::shared_ptr<NotificationUserInput> & userInput)2610 napi_value Common::GetNotificationUserInputByPermitMimeTypes(
2611     const napi_env &env, const napi_value &userInputResult, std::shared_ptr<NotificationUserInput> &userInput)
2612 {
2613     ANS_LOGI("enter");
2614 
2615     napi_valuetype valuetype = napi_undefined;
2616     napi_value value = nullptr;
2617     bool hasProperty = false;
2618     size_t strLen = 0;
2619     uint32_t length = 0;
2620     bool isArray = false;
2621 
2622     if (!userInput) {
2623         ANS_LOGE("userInput is nullptr");
2624         return nullptr;
2625     }
2626 
2627     // permitMimeTypes?: Array<string>
2628     NAPI_CALL(env, napi_has_named_property(env, userInputResult, "permitMimeTypes", &hasProperty));
2629     if (hasProperty) {
2630         napi_get_named_property(env, userInputResult, "permitMimeTypes", &value);
2631         napi_is_array(env, value, &isArray);
2632         if (!isArray) {
2633             ANS_LOGE("Property permitMimeTypes is expected to be an array.");
2634             return nullptr;
2635         }
2636         napi_get_array_length(env, value, &length);
2637         if (length == 0) {
2638             ANS_LOGE("The array is empty.");
2639             return nullptr;
2640         }
2641         for (uint32_t i = 0; i < length; ++i) {
2642             napi_value permitMimeType = nullptr;
2643             char str[STR_MAX_SIZE] = {0};
2644             napi_get_element(env, value, i, &permitMimeType);
2645             NAPI_CALL(env, napi_typeof(env, permitMimeType, &valuetype));
2646             if (valuetype != napi_string) {
2647                 ANS_LOGE("Wrong argument type. String expected.");
2648                 return nullptr;
2649             }
2650             NAPI_CALL(env, napi_get_value_string_utf8(env, permitMimeType, str, STR_MAX_SIZE - 1, &strLen));
2651             userInput->SetPermitMimeTypes(str, true);
2652         }
2653     }
2654 
2655     return NapiGetNull(env);
2656 }
2657 
GetNotificationUserInputByPermitFreeFormInput(const napi_env & env,const napi_value & userInputResult,std::shared_ptr<NotificationUserInput> & userInput)2658 napi_value Common::GetNotificationUserInputByPermitFreeFormInput(
2659     const napi_env &env, const napi_value &userInputResult, std::shared_ptr<NotificationUserInput> &userInput)
2660 {
2661     ANS_LOGI("enter");
2662     napi_value value = nullptr;
2663     napi_valuetype valuetype = napi_undefined;
2664     bool hasProperty = false;
2665 
2666     if (!userInput) {
2667         ANS_LOGE("userInput is nullptr");
2668         return nullptr;
2669     }
2670 
2671     // permitFreeFormInput?: boolean
2672     NAPI_CALL(env, napi_has_named_property(env, userInputResult, "permitFreeFormInput", &hasProperty));
2673     if (hasProperty) {
2674         bool permitFreeFormInput = false;
2675         napi_get_named_property(env, userInputResult, "permitFreeFormInput", &value);
2676         NAPI_CALL(env, napi_typeof(env, value, &valuetype));
2677         if (valuetype != napi_boolean) {
2678             ANS_LOGE("Wrong argument type. Bool expected.");
2679             return nullptr;
2680         }
2681         napi_get_value_bool(env, value, &permitFreeFormInput);
2682         ANS_LOGI("permitFreeFormInput is: %{public}d", permitFreeFormInput);
2683         userInput->SetPermitFreeFormInput(permitFreeFormInput);
2684     }
2685 
2686     return NapiGetNull(env);
2687 }
2688 
GetNotificationUserInputByEditType(const napi_env & env,const napi_value & userInputResult,std::shared_ptr<NotificationUserInput> & userInput)2689 napi_value Common::GetNotificationUserInputByEditType(
2690     const napi_env &env, const napi_value &userInputResult, std::shared_ptr<NotificationUserInput> &userInput)
2691 {
2692     ANS_LOGI("enter");
2693     napi_value value = nullptr;
2694     napi_valuetype valuetype = napi_undefined;
2695     bool hasProperty = false;
2696     int32_t editType = 0;
2697 
2698     if (!userInput) {
2699         ANS_LOGE("userInput is nullptr");
2700         return nullptr;
2701     }
2702 
2703     // editType?: number
2704     NAPI_CALL(env, napi_has_named_property(env, userInputResult, "editType", &hasProperty));
2705     if (hasProperty) {
2706         napi_get_named_property(env, userInputResult, "editType", &value);
2707         NAPI_CALL(env, napi_typeof(env, value, &valuetype));
2708         if (valuetype != napi_number) {
2709             ANS_LOGE("Wrong argument type. Number expected.");
2710             return nullptr;
2711         }
2712         napi_get_value_int32(env, value, &editType);
2713         userInput->SetEditType(NotificationConstant::InputEditType(editType));
2714     }
2715     return NapiGetNull(env);
2716 }
2717 
GetNotificationUserInputByAdditionalData(const napi_env & env,const napi_value & userInputResult,std::shared_ptr<NotificationUserInput> & userInput)2718 napi_value Common::GetNotificationUserInputByAdditionalData(
2719     const napi_env &env, const napi_value &userInputResult, std::shared_ptr<NotificationUserInput> &userInput)
2720 {
2721     ANS_LOGI("enter");
2722 
2723     napi_valuetype valuetype = napi_undefined;
2724     napi_value result = nullptr;
2725     bool hasProperty = false;
2726 
2727     if (!userInput) {
2728         ANS_LOGE("userInput is nullptr");
2729         return nullptr;
2730     }
2731 
2732     // additionalData?: {[key: string]: Object}
2733     NAPI_CALL(env, napi_has_named_property(env, userInputResult, "additionalData", &hasProperty));
2734     if (hasProperty) {
2735         napi_get_named_property(env, userInputResult, "additionalData", &result);
2736         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
2737         if (valuetype != napi_object) {
2738             ANS_LOGE("Wrong argument type. Object expected.");
2739             return nullptr;
2740         }
2741         AAFwk::WantParams wantParams;
2742         if (!OHOS::AppExecFwk::UnwrapWantParams(env, result, wantParams)) {
2743             return nullptr;
2744         }
2745         userInput->AddAdditionalData(wantParams);
2746     }
2747 
2748     return NapiGetNull(env);
2749 }
2750 
GetNotificationSmallIcon(const napi_env & env,const napi_value & value,NotificationRequest & request)2751 napi_value Common::GetNotificationSmallIcon(const napi_env &env, const napi_value &value, NotificationRequest &request)
2752 {
2753     ANS_LOGI("enter");
2754 
2755     napi_valuetype valuetype = napi_undefined;
2756     napi_value result = nullptr;
2757     bool hasProperty = false;
2758 
2759     NAPI_CALL(env, napi_has_named_property(env, value, "smallIcon", &hasProperty));
2760     if (hasProperty) {
2761         napi_get_named_property(env, value, "smallIcon", &result);
2762         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
2763         if (valuetype != napi_object) {
2764             ANS_LOGE("Wrong argument type. Object expected.");
2765             return nullptr;
2766         }
2767         std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
2768         pixelMap = Media::PixelMapNapi::GetPixelMap(env, result);
2769         if (pixelMap == nullptr) {
2770             ANS_LOGE("Invalid object pixelMap");
2771             return nullptr;
2772         }
2773         request.SetLittleIcon(pixelMap);
2774     }
2775 
2776     return NapiGetNull(env);
2777 }
2778 
GetNotificationLargeIcon(const napi_env & env,const napi_value & value,NotificationRequest & request)2779 napi_value Common::GetNotificationLargeIcon(const napi_env &env, const napi_value &value, NotificationRequest &request)
2780 {
2781     ANS_LOGI("enter");
2782 
2783     napi_valuetype valuetype = napi_undefined;
2784     napi_value result = nullptr;
2785     bool hasProperty = false;
2786 
2787     NAPI_CALL(env, napi_has_named_property(env, value, "largeIcon", &hasProperty));
2788     if (hasProperty) {
2789         napi_get_named_property(env, value, "largeIcon", &result);
2790         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
2791         if (valuetype != napi_object) {
2792             ANS_LOGE("Wrong argument type. Object expected.");
2793             return nullptr;
2794         }
2795         std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
2796         pixelMap = Media::PixelMapNapi::GetPixelMap(env, result);
2797         if (pixelMap == nullptr) {
2798             ANS_LOGE("Invalid object pixelMap");
2799             return nullptr;
2800         }
2801         request.SetBigIcon(pixelMap);
2802     }
2803 
2804     return NapiGetNull(env);
2805 }
2806 
GetNotificationRequestDistributedOptions(const napi_env & env,const napi_value & value,NotificationRequest & request)2807 napi_value Common::GetNotificationRequestDistributedOptions(const napi_env &env,
2808     const napi_value &value, NotificationRequest &request)
2809 {
2810     ANS_LOGI("enter");
2811     napi_valuetype valuetype = napi_undefined;
2812     napi_value result = nullptr;
2813     bool hasProperty = false;
2814 
2815     // distributedOption?: DistributedOptions
2816     NAPI_CALL(env, napi_has_named_property(env, value, "distributedOption", &hasProperty));
2817     if (hasProperty) {
2818         napi_get_named_property(env, value, "distributedOption", &result);
2819         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
2820         if (valuetype != napi_object) {
2821             ANS_LOGE("Wrong argument type. Object expected.");
2822             return nullptr;
2823         }
2824 
2825         // isDistributed?: boolean
2826         if (GetNotificationIsDistributed(env, result, request) == nullptr) {
2827             return nullptr;
2828         }
2829 
2830         // supportDisplayDevices?: Array<string>
2831         if (GetNotificationSupportDisplayDevices(env, result, request) == nullptr) {
2832             return nullptr;
2833         }
2834 
2835         // supportOperateDevices?: Array<string>
2836         if (GetNotificationSupportOperateDevices(env, result, request) == nullptr) {
2837             return nullptr;
2838         }
2839     }
2840 
2841     return NapiGetNull(env);
2842 }
2843 
GetNotificationIsDistributed(const napi_env & env,const napi_value & value,NotificationRequest & request)2844 napi_value Common::GetNotificationIsDistributed(
2845     const napi_env &env, const napi_value &value, NotificationRequest &request)
2846 {
2847     ANS_LOGI("enter");
2848 
2849     napi_valuetype valuetype = napi_undefined;
2850     napi_value result = nullptr;
2851     bool hasProperty = false;
2852     bool isDistributed = false;
2853 
2854     NAPI_CALL(env, napi_has_named_property(env, value, "isDistributed", &hasProperty));
2855     if (hasProperty) {
2856         napi_get_named_property(env, value, "isDistributed", &result);
2857         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
2858         if (valuetype != napi_boolean) {
2859             ANS_LOGE("Wrong argument type. Bool expected.");
2860             return nullptr;
2861         }
2862         napi_get_value_bool(env, result, &isDistributed);
2863         request.SetDistributed(isDistributed);
2864     }
2865 
2866     return NapiGetNull(env);
2867 }
2868 
GetNotificationSupportDisplayDevices(const napi_env & env,const napi_value & value,NotificationRequest & request)2869 napi_value Common::GetNotificationSupportDisplayDevices(
2870     const napi_env &env, const napi_value &value, NotificationRequest &request)
2871 {
2872     ANS_LOGI("enter");
2873 
2874     bool isArray = false;
2875     bool hasProperty = false;
2876     napi_valuetype valuetype = napi_undefined;
2877     napi_value supportDisplayDevices = nullptr;
2878     size_t strLen = 0;
2879     uint32_t length = 0;
2880 
2881     NAPI_CALL(env, napi_has_named_property(env, value, "supportDisplayDevices", &hasProperty));
2882     if (hasProperty) {
2883         napi_get_named_property(env, value, "supportDisplayDevices", &supportDisplayDevices);
2884         napi_is_array(env, supportDisplayDevices, &isArray);
2885         if (!isArray) {
2886             ANS_LOGE("Property supportDisplayDevices is expected to be an array.");
2887             return nullptr;
2888         }
2889 
2890         napi_get_array_length(env, supportDisplayDevices, &length);
2891         if (length == 0) {
2892             ANS_LOGE("The array is empty.");
2893             return nullptr;
2894         }
2895         std::vector<std::string> devices;
2896         for (size_t i = 0; i < length; i++) {
2897             napi_value line = nullptr;
2898             napi_get_element(env, supportDisplayDevices, i, &line);
2899             NAPI_CALL(env, napi_typeof(env, line, &valuetype));
2900             if (valuetype != napi_string) {
2901                 ANS_LOGE("Wrong argument type. String expected.");
2902                 return nullptr;
2903             }
2904             char str[STR_MAX_SIZE] = {0};
2905             NAPI_CALL(env, napi_get_value_string_utf8(env, line, str, STR_MAX_SIZE - 1, &strLen));
2906             devices.emplace_back(str);
2907             ANS_LOGI("supportDisplayDevices = %{public}s", str);
2908         }
2909         request.SetDevicesSupportDisplay(devices);
2910     }
2911     return NapiGetNull(env);
2912 }
2913 
GetNotificationSupportOperateDevices(const napi_env & env,const napi_value & value,NotificationRequest & request)2914 napi_value Common::GetNotificationSupportOperateDevices(
2915     const napi_env &env, const napi_value &value, NotificationRequest &request)
2916 {
2917     ANS_LOGI("enter");
2918 
2919     bool isArray = false;
2920     bool hasProperty = false;
2921     napi_valuetype valuetype = napi_undefined;
2922     napi_value supportOperateDevices = nullptr;
2923     size_t strLen = 0;
2924     uint32_t length = 0;
2925 
2926     NAPI_CALL(env, napi_has_named_property(env, value, "supportOperateDevices", &hasProperty));
2927     if (hasProperty) {
2928         napi_get_named_property(env, value, "supportOperateDevices", &supportOperateDevices);
2929         napi_is_array(env, supportOperateDevices, &isArray);
2930         if (!isArray) {
2931             ANS_LOGE("Property supportOperateDevices is expected to be an array.");
2932             return nullptr;
2933         }
2934 
2935         napi_get_array_length(env, supportOperateDevices, &length);
2936         if (length == 0) {
2937             ANS_LOGE("The array is empty.");
2938             return nullptr;
2939         }
2940         std::vector<std::string> devices;
2941         for (size_t i = 0; i < length; i++) {
2942             napi_value line = nullptr;
2943             napi_get_element(env, supportOperateDevices, i, &line);
2944             NAPI_CALL(env, napi_typeof(env, line, &valuetype));
2945             if (valuetype != napi_string) {
2946                 ANS_LOGE("Wrong argument type. String expected.");
2947                 return nullptr;
2948             }
2949             char str[STR_MAX_SIZE] = {0};
2950             NAPI_CALL(env, napi_get_value_string_utf8(env, line, str, STR_MAX_SIZE - 1, &strLen));
2951             devices.emplace_back(str);
2952             ANS_LOGI("supportOperateDevices = %{public}s", str);
2953         }
2954         request.SetDevicesSupportOperate(devices);
2955     }
2956 
2957     return NapiGetNull(env);
2958 }
2959 
GetNotificationContentType(const napi_env & env,const napi_value & result,int32_t & type)2960 napi_value Common::GetNotificationContentType(const napi_env &env, const napi_value &result, int32_t &type)
2961 {
2962     ANS_LOGI("enter");
2963 
2964     napi_value contentResult = nullptr;
2965     napi_valuetype valuetype = napi_undefined;
2966     bool hasProperty = false;
2967 
2968     NAPI_CALL(env, napi_has_named_property(env, result, "contentType", &hasProperty));
2969     if (!hasProperty) {
2970         ANS_LOGE("Property contentType expected.");
2971         return nullptr;
2972     }
2973 
2974     napi_get_named_property(env, result, "contentType", &contentResult);
2975     NAPI_CALL(env, napi_typeof(env, contentResult, &valuetype));
2976     if (valuetype != napi_number) {
2977         ANS_LOGE("Wrong argument type. Number expected.");
2978         return nullptr;
2979     }
2980     napi_get_value_int32(env, contentResult, &type);
2981 
2982     return NapiGetNull(env);
2983 }
2984 
GetNotificationBasicContent(const napi_env & env,const napi_value & result,NotificationRequest & request)2985 napi_value Common::GetNotificationBasicContent(
2986     const napi_env &env, const napi_value &result, NotificationRequest &request)
2987 {
2988     ANS_LOGI("enter");
2989 
2990     napi_valuetype valuetype = napi_undefined;
2991     napi_value contentResult = nullptr;
2992     bool hasProperty = false;
2993     NAPI_CALL(env, napi_has_named_property(env, result, "normal", &hasProperty));
2994     if (!hasProperty) {
2995         ANS_LOGE("Property normal expected.");
2996         return nullptr;
2997     }
2998     napi_get_named_property(env, result, "normal", &contentResult);
2999     NAPI_CALL(env, napi_typeof(env, contentResult, &valuetype));
3000     if (valuetype != napi_object) {
3001         ANS_LOGE("Wrong argument type. Object expected.");
3002         return nullptr;
3003     }
3004 
3005     std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
3006     if (normalContent == nullptr) {
3007         ANS_LOGE("normalContent is null");
3008         return nullptr;
3009     }
3010 
3011     if (GetNotificationBasicContentDetailed(env, contentResult, normalContent) == nullptr) {
3012         return nullptr;
3013     }
3014 
3015     request.SetContent(std::make_shared<NotificationContent>(normalContent));
3016 
3017     return NapiGetNull(env);
3018 }
3019 
GetNotificationBasicContentDetailed(const napi_env & env,const napi_value & contentResult,std::shared_ptr<NotificationBasicContent> basicContent)3020 napi_value Common::GetNotificationBasicContentDetailed(
3021     const napi_env &env, const napi_value &contentResult, std::shared_ptr<NotificationBasicContent> basicContent)
3022 {
3023     ANS_LOGI("enter");
3024 
3025     napi_valuetype valuetype = napi_undefined;
3026     napi_value value = nullptr;
3027     bool hasProperty = false;
3028     char str[STR_MAX_SIZE] = {0};
3029     size_t strLen = 0;
3030 
3031     // title: string
3032     NAPI_CALL(env, napi_has_named_property(env, contentResult, "title", &hasProperty));
3033     if (!hasProperty) {
3034         ANS_LOGE("Property title expected.");
3035         return nullptr;
3036     }
3037     napi_get_named_property(env, contentResult, "title", &value);
3038     NAPI_CALL(env, napi_typeof(env, value, &valuetype));
3039     if (valuetype != napi_string) {
3040         ANS_LOGE("Wrong argument type. String expected.");
3041         return nullptr;
3042     }
3043     NAPI_CALL(env, napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen));
3044     basicContent->SetTitle(str);
3045     ANS_LOGD("normal::title = %{public}s", str);
3046 
3047     // text: string
3048     NAPI_CALL(env, napi_has_named_property(env, contentResult, "text", &hasProperty));
3049     if (!hasProperty) {
3050         ANS_LOGE("Property text expected.");
3051         return nullptr;
3052     }
3053     napi_get_named_property(env, contentResult, "text", &value);
3054     NAPI_CALL(env, napi_typeof(env, value, &valuetype));
3055     if (valuetype != napi_string) {
3056         ANS_LOGE("Wrong argument type. String expected.");
3057         return nullptr;
3058     }
3059     NAPI_CALL(env, napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen));
3060     basicContent->SetText(str);
3061     ANS_LOGD("normal::text = %{public}s", str);
3062 
3063     // additionalText?: string
3064     NAPI_CALL(env, napi_has_named_property(env, contentResult, "additionalText", &hasProperty));
3065     if (hasProperty) {
3066         napi_get_named_property(env, contentResult, "additionalText", &value);
3067         NAPI_CALL(env, napi_typeof(env, value, &valuetype));
3068         if (valuetype != napi_string) {
3069             ANS_LOGE("Wrong argument type. String expected.");
3070             return nullptr;
3071         }
3072         NAPI_CALL(env, napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen));
3073         basicContent->SetAdditionalText(str);
3074         ANS_LOGI("normal::additionalText = %{public}s", str);
3075     }
3076 
3077     return NapiGetNull(env);
3078 }
3079 
GetNotificationLongTextContent(const napi_env & env,const napi_value & result,NotificationRequest & request)3080 napi_value Common::GetNotificationLongTextContent(
3081     const napi_env &env, const napi_value &result, NotificationRequest &request)
3082 {
3083     ANS_LOGI("enter");
3084 
3085     napi_valuetype valuetype = napi_undefined;
3086     napi_value contentResult = nullptr;
3087     bool hasProperty = false;
3088 
3089     NAPI_CALL(env, napi_has_named_property(env, result, "longText", &hasProperty));
3090     if (!hasProperty) {
3091         ANS_LOGE("Property longText expected.");
3092         return nullptr;
3093     }
3094 
3095     napi_get_named_property(env, result, "longText", &contentResult);
3096     NAPI_CALL(env, napi_typeof(env, contentResult, &valuetype));
3097     if (valuetype != napi_object) {
3098         ANS_LOGE("Wrong argument type. Object expected.");
3099         return nullptr;
3100     }
3101 
3102     std::shared_ptr<OHOS::Notification::NotificationLongTextContent> longContent =
3103         std::make_shared<OHOS::Notification::NotificationLongTextContent>();
3104     if (longContent == nullptr) {
3105         ANS_LOGE("longContent is null");
3106         return nullptr;
3107     }
3108 
3109     if (GetNotificationLongTextContentDetailed(env, contentResult, longContent) == nullptr) {
3110         return nullptr;
3111     }
3112 
3113     request.SetContent(std::make_shared<NotificationContent>(longContent));
3114 
3115     return NapiGetNull(env);
3116 }
3117 
GetNotificationLongTextContentDetailed(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationLongTextContent> & longContent)3118 napi_value Common::GetNotificationLongTextContentDetailed(
3119     const napi_env &env, const napi_value &contentResult,
3120     std::shared_ptr<OHOS::Notification::NotificationLongTextContent> &longContent)
3121 {
3122     ANS_LOGI("enter");
3123 
3124     napi_valuetype valuetype = napi_undefined;
3125     napi_value longContentResult = nullptr;
3126     bool hasProperty = false;
3127     char str[STR_MAX_SIZE] = {0};
3128     char long_str[LONG_STR_MAX_SIZE + 1] = {0};
3129     size_t strLen = 0;
3130 
3131     if (GetNotificationBasicContentDetailed(env, contentResult, longContent) == nullptr) {
3132         return nullptr;
3133     }
3134 
3135     // longText: string
3136     NAPI_CALL(env, napi_has_named_property(env, contentResult, "longText", &hasProperty));
3137     if (!hasProperty) {
3138         ANS_LOGE("Property longText expected.");
3139         return nullptr;
3140     }
3141     napi_get_named_property(env, contentResult, "longText", &longContentResult);
3142     NAPI_CALL(env, napi_typeof(env, longContentResult, &valuetype));
3143     if (valuetype != napi_string) {
3144         ANS_LOGE("Wrong argument type. String expected.");
3145         return nullptr;
3146     }
3147     NAPI_CALL(env, napi_get_value_string_utf8(env, longContentResult, long_str, LONG_STR_MAX_SIZE, &strLen));
3148     longContent->SetLongText(long_str);
3149     ANS_LOGD("longText::longText = %{public}s", long_str);
3150 
3151     // briefText: string
3152     NAPI_CALL(env, napi_has_named_property(env, contentResult, "briefText", &hasProperty));
3153     if (!hasProperty) {
3154         ANS_LOGE("Property briefText expected.");
3155         return nullptr;
3156     }
3157     napi_get_named_property(env, contentResult, "briefText", &longContentResult);
3158     NAPI_CALL(env, napi_typeof(env, longContentResult, &valuetype));
3159     if (valuetype != napi_string) {
3160         ANS_LOGE("Wrong argument type. String expected.");
3161         return nullptr;
3162     }
3163     NAPI_CALL(env, napi_get_value_string_utf8(env, longContentResult, str, STR_MAX_SIZE - 1, &strLen));
3164     longContent->SetBriefText(str);
3165     ANS_LOGD("longText::briefText = %{public}s", str);
3166 
3167     // expandedTitle: string
3168     NAPI_CALL(env, napi_has_named_property(env, contentResult, "expandedTitle", &hasProperty));
3169     if (!hasProperty) {
3170         ANS_LOGE("Property expandedTitle expected.");
3171         return nullptr;
3172     }
3173     napi_get_named_property(env, contentResult, "expandedTitle", &longContentResult);
3174     NAPI_CALL(env, napi_typeof(env, longContentResult, &valuetype));
3175     if (valuetype != napi_string) {
3176         ANS_LOGE("Wrong argument type. String expected.");
3177         return nullptr;
3178     }
3179     NAPI_CALL(env, napi_get_value_string_utf8(env, longContentResult, str, STR_MAX_SIZE - 1, &strLen));
3180     longContent->SetExpandedTitle(str);
3181     ANS_LOGD("longText::expandedTitle = %{public}s", str);
3182 
3183     return NapiGetNull(env);
3184 }
3185 
GetNotificationPictureContent(const napi_env & env,const napi_value & result,NotificationRequest & request)3186 napi_value Common::GetNotificationPictureContent(
3187     const napi_env &env, const napi_value &result, NotificationRequest &request)
3188 {
3189     ANS_LOGI("enter");
3190 
3191     napi_valuetype valuetype = napi_undefined;
3192     napi_value contentResult = nullptr;
3193     bool hasProperty = false;
3194 
3195     NAPI_CALL(env, napi_has_named_property(env, result, "picture", &hasProperty));
3196     if (!hasProperty) {
3197         ANS_LOGE("Property picture expected.");
3198         return nullptr;
3199     }
3200     napi_get_named_property(env, result, "picture", &contentResult);
3201     NAPI_CALL(env, napi_typeof(env, contentResult, &valuetype));
3202     if (valuetype != napi_object) {
3203         ANS_LOGE("Wrong argument type. Object expected.");
3204         return nullptr;
3205     }
3206 
3207     std::shared_ptr<OHOS::Notification::NotificationPictureContent> pictureContent =
3208         std::make_shared<OHOS::Notification::NotificationPictureContent>();
3209     if (pictureContent == nullptr) {
3210         ANS_LOGE("pictureContent is null");
3211         return nullptr;
3212     }
3213     if (GetNotificationPictureContentDetailed(env, contentResult, pictureContent) == nullptr) {
3214         return nullptr;
3215     }
3216 
3217     request.SetContent(std::make_shared<NotificationContent>(pictureContent));
3218 
3219     return NapiGetNull(env);
3220 }
3221 
GetNotificationPictureContentDetailed(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationPictureContent> & pictureContent)3222 napi_value Common::GetNotificationPictureContentDetailed(const napi_env &env,
3223     const napi_value &contentResult, std::shared_ptr<OHOS::Notification::NotificationPictureContent> &pictureContent)
3224 {
3225     ANS_LOGI("enter");
3226 
3227     napi_valuetype valuetype = napi_undefined;
3228     napi_value pictureContentResult = nullptr;
3229     bool hasProperty = false;
3230     char str[STR_MAX_SIZE] = {0};
3231     size_t strLen = 0;
3232 
3233     if (GetNotificationBasicContentDetailed(env, contentResult, pictureContent) == nullptr) {
3234         return nullptr;
3235     }
3236 
3237     // briefText: string
3238     NAPI_CALL(env, napi_has_named_property(env, contentResult, "briefText", &hasProperty));
3239     if (!hasProperty) {
3240         ANS_LOGE("Property briefText expected.");
3241         return nullptr;
3242     }
3243     napi_get_named_property(env, contentResult, "briefText", &pictureContentResult);
3244     NAPI_CALL(env, napi_typeof(env, pictureContentResult, &valuetype));
3245     if (valuetype != napi_string) {
3246         ANS_LOGE("Wrong argument type. String expected.");
3247         return nullptr;
3248     }
3249     NAPI_CALL(env, napi_get_value_string_utf8(env, pictureContentResult, str, STR_MAX_SIZE - 1, &strLen));
3250     pictureContent->SetBriefText(str);
3251 
3252     // expandedTitle: string
3253     NAPI_CALL(env, napi_has_named_property(env, contentResult, "expandedTitle", &hasProperty));
3254     if (!hasProperty) {
3255         ANS_LOGE("Property expandedTitle expected.");
3256         return nullptr;
3257     }
3258     napi_get_named_property(env, contentResult, "expandedTitle", &pictureContentResult);
3259     NAPI_CALL(env, napi_typeof(env, pictureContentResult, &valuetype));
3260     if (valuetype != napi_string) {
3261         ANS_LOGE("Wrong argument type. String expected.");
3262         return nullptr;
3263     }
3264     NAPI_CALL(env, napi_get_value_string_utf8(env, pictureContentResult, str, STR_MAX_SIZE - 1, &strLen));
3265     pictureContent->SetExpandedTitle(str);
3266 
3267     // picture: image.PixelMap
3268     NAPI_CALL(env, napi_has_named_property(env, contentResult, "picture", &hasProperty));
3269     if (!hasProperty) {
3270         ANS_LOGE("Property picture expected.");
3271         return nullptr;
3272     }
3273     napi_get_named_property(env, contentResult, "picture", &pictureContentResult);
3274     NAPI_CALL(env, napi_typeof(env, pictureContentResult, &valuetype));
3275     if (valuetype != napi_object) {
3276         ANS_LOGE("Wrong argument type. Object expected.");
3277         return nullptr;
3278     }
3279     std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
3280     pixelMap = Media::PixelMapNapi::GetPixelMap(env, pictureContentResult);
3281     if (pixelMap == nullptr) {
3282         ANS_LOGE("Invalid object pixelMap");
3283         return nullptr;
3284     }
3285     pictureContent->SetBigPicture(pixelMap);
3286 
3287     return Common::NapiGetNull(env);
3288 }
3289 
GetNotificationConversationalContent(const napi_env & env,const napi_value & result,NotificationRequest & request)3290 napi_value Common::GetNotificationConversationalContent(
3291     const napi_env &env, const napi_value &result, NotificationRequest &request)
3292 {
3293     ANS_LOGI("enter");
3294 
3295     napi_valuetype valuetype = napi_undefined;
3296     napi_value contentResult = nullptr;
3297     bool hasProperty = false;
3298     MessageUser user;
3299 
3300     NAPI_CALL(env, napi_has_named_property(env, result, "conversation", &hasProperty));
3301     if (!hasProperty) {
3302         ANS_LOGE("Property conversation expected.");
3303         return nullptr;
3304     }
3305     napi_get_named_property(env, result, "conversation", &contentResult);
3306     NAPI_CALL(env, napi_typeof(env, contentResult, &valuetype));
3307     if (valuetype != napi_object) {
3308         ANS_LOGE("Wrong argument type. Object expected.");
3309         return nullptr;
3310     }
3311 
3312     if (GetNotificationConversationalContentByUser(env, contentResult, user) == nullptr) {
3313         return nullptr;
3314     }
3315 
3316     std::shared_ptr<OHOS::Notification::NotificationConversationalContent> conversationalContent =
3317         std::make_shared<OHOS::Notification::NotificationConversationalContent>(user);
3318     if (conversationalContent == nullptr) {
3319         ANS_LOGE("conversationalContent is null");
3320         return nullptr;
3321     }
3322 
3323     if (GetNotificationBasicContentDetailed(env, contentResult, conversationalContent) == nullptr) {
3324         return nullptr;
3325     }
3326     if (GetNotificationConversationalContentTitle(env, contentResult, conversationalContent) == nullptr) {
3327         return nullptr;
3328     }
3329     if (GetNotificationConversationalContentGroup(env, contentResult, conversationalContent) == nullptr) {
3330         return nullptr;
3331     }
3332     if (GetNotificationConversationalContentMessages(env, contentResult, conversationalContent) == nullptr) {
3333         return nullptr;
3334     }
3335 
3336     request.SetContent(std::make_shared<NotificationContent>(conversationalContent));
3337 
3338     return NapiGetNull(env);
3339 }
3340 
GetNotificationConversationalContentByUser(const napi_env & env,const napi_value & contentResult,MessageUser & user)3341 napi_value Common::GetNotificationConversationalContentByUser(
3342     const napi_env &env, const napi_value &contentResult, MessageUser &user)
3343 {
3344     ANS_LOGI("enter");
3345 
3346     napi_valuetype valuetype = napi_undefined;
3347     bool hasProperty = false;
3348 
3349     // user: MessageUser
3350     NAPI_CALL(env, napi_has_named_property(env, contentResult, "user", &hasProperty));
3351     if (!hasProperty) {
3352         ANS_LOGE("Property user expected.");
3353         return nullptr;
3354     }
3355     napi_value userResult = nullptr;
3356     napi_get_named_property(env, contentResult, "user", &userResult);
3357     NAPI_CALL(env, napi_typeof(env, userResult, &valuetype));
3358     if (valuetype != napi_object) {
3359         ANS_LOGE("Wrong argument type. Object expected.");
3360         return nullptr;
3361     }
3362     if (!GetMessageUser(env, userResult, user)) {
3363         return nullptr;
3364     }
3365 
3366     return NapiGetNull(env);
3367 }
3368 
GetNotificationConversationalContentTitle(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationConversationalContent> & conversationalContent)3369 napi_value Common::GetNotificationConversationalContentTitle(
3370     const napi_env &env, const napi_value &contentResult,
3371     std::shared_ptr<OHOS::Notification::NotificationConversationalContent> &conversationalContent)
3372 {
3373     ANS_LOGI("enter");
3374     napi_valuetype valuetype = napi_undefined;
3375     napi_value conversationalContentResult = nullptr;
3376     bool hasProperty = false;
3377     char str[STR_MAX_SIZE] = {0};
3378     size_t strLen = 0;
3379 
3380     // conversationTitle: string
3381     NAPI_CALL(env, napi_has_named_property(env, contentResult, "conversationTitle", &hasProperty));
3382     if (!hasProperty) {
3383         ANS_LOGE("Property conversationTitle expected.");
3384         return nullptr;
3385     }
3386     napi_get_named_property(env, contentResult, "conversationTitle", &conversationalContentResult);
3387     NAPI_CALL(env, napi_typeof(env, conversationalContentResult, &valuetype));
3388     if (valuetype != napi_string) {
3389         ANS_LOGE("Wrong argument type. String expected.");
3390         return nullptr;
3391     }
3392     NAPI_CALL(env, napi_get_value_string_utf8(env, conversationalContentResult, str, STR_MAX_SIZE - 1, &strLen));
3393     conversationalContent->SetConversationTitle(str);
3394     ANS_LOGD("conversationTitle = %{public}s", str);
3395 
3396     return NapiGetNull(env);
3397 }
3398 
GetNotificationConversationalContentGroup(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationConversationalContent> & conversationalContent)3399 napi_value Common::GetNotificationConversationalContentGroup(
3400     const napi_env &env, const napi_value &contentResult,
3401     std::shared_ptr<OHOS::Notification::NotificationConversationalContent> &conversationalContent)
3402 {
3403     ANS_LOGI("enter");
3404     napi_valuetype valuetype = napi_undefined;
3405     napi_value conversationalContentResult = nullptr;
3406     bool hasProperty = false;
3407 
3408     // conversationGroup: boolean
3409     NAPI_CALL(env, napi_has_named_property(env, contentResult, "conversationGroup", &hasProperty));
3410     if (!hasProperty) {
3411         ANS_LOGE("Property conversationGroup expected.");
3412         return nullptr;
3413     }
3414     napi_get_named_property(env, contentResult, "conversationGroup", &conversationalContentResult);
3415     NAPI_CALL(env, napi_typeof(env, conversationalContentResult, &valuetype));
3416     if (valuetype != napi_boolean) {
3417         ANS_LOGE("Wrong argument type. Bool expected.");
3418         return nullptr;
3419     }
3420     bool conversationGroup = false;
3421     napi_get_value_bool(env, conversationalContentResult, &conversationGroup);
3422     conversationalContent->SetConversationGroup(conversationGroup);
3423     ANS_LOGI("conversationalText::conversationGroup = %{public}d", conversationGroup);
3424 
3425     return NapiGetNull(env);
3426 }
3427 
GetNotificationConversationalContentMessages(const napi_env & env,const napi_value & contentResult,std::shared_ptr<OHOS::Notification::NotificationConversationalContent> & conversationalContent)3428 napi_value Common::GetNotificationConversationalContentMessages(
3429     const napi_env &env, const napi_value &contentResult,
3430     std::shared_ptr<OHOS::Notification::NotificationConversationalContent> &conversationalContent)
3431 {
3432     ANS_LOGI("enter");
3433     napi_valuetype valuetype = napi_undefined;
3434     napi_value conversationalContentResult = nullptr;
3435     bool hasProperty = false;
3436 
3437     // messages: Array<ConversationalMessage>
3438     NAPI_CALL(env, napi_has_named_property(env, contentResult, "messages", &hasProperty));
3439     if (!hasProperty) {
3440         ANS_LOGE("Property messages expected.");
3441         return nullptr;
3442     }
3443     napi_get_named_property(env, contentResult, "messages", &conversationalContentResult);
3444     bool isArray = false;
3445     napi_is_array(env, conversationalContentResult, &isArray);
3446     if (!isArray) {
3447         ANS_LOGE("Property messages is expected to be an array.");
3448         return nullptr;
3449     }
3450     uint32_t length = 0;
3451     napi_get_array_length(env, conversationalContentResult, &length);
3452     if (length == 0) {
3453         ANS_LOGE("The array is empty.");
3454         return nullptr;
3455     }
3456     for (size_t i = 0; i < length; i++) {
3457         napi_value conversationalMessage = nullptr;
3458         napi_get_element(env, conversationalContentResult, i, &conversationalMessage);
3459         NAPI_CALL(env, napi_typeof(env, conversationalMessage, &valuetype));
3460         if (valuetype != napi_object) {
3461             ANS_LOGE("Wrong argument type. Object expected.");
3462             return nullptr;
3463         }
3464         std::shared_ptr<NotificationConversationalMessage> message = nullptr;
3465         if (!GetConversationalMessage(env, conversationalMessage, message)) {
3466             return nullptr;
3467         }
3468         conversationalContent->AddConversationalMessage(message);
3469     }
3470 
3471     return NapiGetNull(env);
3472 }
3473 
GetConversationalMessage(const napi_env & env,const napi_value & conversationalMessage,std::shared_ptr<NotificationConversationalMessage> & message)3474 napi_value Common::GetConversationalMessage(const napi_env &env, const napi_value &conversationalMessage,
3475     std::shared_ptr<NotificationConversationalMessage> &message)
3476 {
3477     ANS_LOGI("enter");
3478 
3479     if (GetConversationalMessageBasicInfo(env, conversationalMessage, message) == nullptr) {
3480         return nullptr;
3481     }
3482     if (GetConversationalMessageOtherInfo(env, conversationalMessage, message) == nullptr) {
3483         return nullptr;
3484     }
3485     return NapiGetNull(env);
3486 }
3487 
GetConversationalMessageBasicInfo(const napi_env & env,const napi_value & conversationalMessage,std::shared_ptr<NotificationConversationalMessage> & message)3488 napi_value Common::GetConversationalMessageBasicInfo(const napi_env &env, const napi_value &conversationalMessage,
3489     std::shared_ptr<NotificationConversationalMessage> &message)
3490 {
3491     ANS_LOGI("enter");
3492 
3493     napi_valuetype valuetype = napi_undefined;
3494     bool hasProperty = false;
3495     char str[STR_MAX_SIZE] = {0};
3496     size_t strLen = 0;
3497     std::string text;
3498     int64_t timestamp = 0;
3499     MessageUser sender;
3500 
3501     // text: string
3502     NAPI_CALL(env, napi_has_named_property(env, conversationalMessage, "text", &hasProperty));
3503     if (!hasProperty) {
3504         ANS_LOGE("Property text expected.");
3505         return nullptr;
3506     }
3507     napi_value textResult = nullptr;
3508     napi_get_named_property(env, conversationalMessage, "text", &textResult);
3509     NAPI_CALL(env, napi_typeof(env, textResult, &valuetype));
3510     if (valuetype != napi_string) {
3511         ANS_LOGE("Wrong argument type. String expected.");
3512         return nullptr;
3513     }
3514     NAPI_CALL(env, napi_get_value_string_utf8(env, textResult, str, STR_MAX_SIZE - 1, &strLen));
3515     text = str;
3516     ANS_LOGI("conversationalMessage::text = %{public}s", str);
3517 
3518     // timestamp: number
3519     NAPI_CALL(env, napi_has_named_property(env, conversationalMessage, "timestamp", &hasProperty));
3520     if (!hasProperty) {
3521         ANS_LOGE("Property timestamp expected.");
3522         return nullptr;
3523     }
3524     napi_value timestampResult = nullptr;
3525     napi_get_named_property(env, conversationalMessage, "timestamp", &timestampResult);
3526     NAPI_CALL(env, napi_typeof(env, timestampResult, &valuetype));
3527     if (valuetype != napi_number) {
3528         ANS_LOGE("Wrong argument type. Number expected.");
3529         return nullptr;
3530     }
3531     napi_get_value_int64(env, timestampResult, &timestamp);
3532     ANS_LOGI("conversationalMessage::timestamp = %{public}" PRId64, timestamp);
3533 
3534     // sender: MessageUser
3535     NAPI_CALL(env, napi_has_named_property(env, conversationalMessage, "sender", &hasProperty));
3536     if (!hasProperty) {
3537         ANS_LOGE("Property sender expected.");
3538         return nullptr;
3539     }
3540     napi_value senderResult = nullptr;
3541     napi_get_named_property(env, conversationalMessage, "sender", &senderResult);
3542     NAPI_CALL(env, napi_typeof(env, senderResult, &valuetype));
3543     if (valuetype != napi_object) {
3544         ANS_LOGE("Wrong argument type. Object expected.");
3545         return nullptr;
3546     }
3547     if (!GetMessageUser(env, senderResult, sender)) {
3548         return nullptr;
3549     }
3550 
3551     message = std::make_shared<NotificationConversationalMessage>(text, timestamp, sender);
3552     if (!message) {
3553         ANS_LOGE("Failed to create NotificationConversationalMessage object");
3554         return nullptr;
3555     }
3556 
3557     return NapiGetNull(env);
3558 }
3559 
GetConversationalMessageOtherInfo(const napi_env & env,const napi_value & conversationalMessage,std::shared_ptr<NotificationConversationalMessage> & message)3560 napi_value Common::GetConversationalMessageOtherInfo(const napi_env &env, const napi_value &conversationalMessage,
3561     std::shared_ptr<NotificationConversationalMessage> &message)
3562 {
3563     ANS_LOGI("enter");
3564 
3565     napi_valuetype valuetype = napi_undefined;
3566     bool hasProperty = false;
3567     char str[STR_MAX_SIZE] = {0};
3568     size_t strLen = 0;
3569     std::string mimeType;
3570     std::string uri;
3571 
3572     // mimeType: string
3573     NAPI_CALL(env, napi_has_named_property(env, conversationalMessage, "mimeType", &hasProperty));
3574     if (!hasProperty) {
3575         ANS_LOGE("Property mimeType expected.");
3576         return nullptr;
3577     }
3578     napi_value mimeTypeResult = nullptr;
3579     napi_get_named_property(env, conversationalMessage, "mimeType", &mimeTypeResult);
3580     NAPI_CALL(env, napi_typeof(env, mimeTypeResult, &valuetype));
3581     if (valuetype != napi_string) {
3582         ANS_LOGE("Wrong argument type. String expected.");
3583         return nullptr;
3584     }
3585     NAPI_CALL(env, napi_get_value_string_utf8(env, mimeTypeResult, str, STR_MAX_SIZE - 1, &strLen));
3586     mimeType = str;
3587     ANS_LOGI("conversationalMessage::mimeType = %{public}s", str);
3588 
3589     // uri?: string
3590     NAPI_CALL(env, napi_has_named_property(env, conversationalMessage, "uri", &hasProperty));
3591     if (hasProperty) {
3592         napi_value uriResult = nullptr;
3593         napi_get_named_property(env, conversationalMessage, "uri", &uriResult);
3594         NAPI_CALL(env, napi_typeof(env, uriResult, &valuetype));
3595         if (valuetype != napi_string) {
3596             ANS_LOGE("Wrong argument type. String expected.");
3597             return nullptr;
3598         }
3599         NAPI_CALL(env, napi_get_value_string_utf8(env, uriResult, str, STR_MAX_SIZE - 1, &strLen));
3600         uri = str;
3601     }
3602 
3603     std::shared_ptr<Uri> uriPtr = std::make_shared<Uri>(uri);
3604     message->SetData(mimeType, uriPtr);
3605 
3606     return NapiGetNull(env);
3607 }
3608 
GetMessageUser(const napi_env & env,const napi_value & result,MessageUser & messageUser)3609 napi_value Common::GetMessageUser(const napi_env &env, const napi_value &result, MessageUser &messageUser)
3610 {
3611     ANS_LOGI("enter");
3612 
3613     if (GetMessageUserByString(env, result, messageUser) == nullptr) {
3614         return nullptr;
3615     }
3616 
3617     if (GetMessageUserByBool(env, result, messageUser) == nullptr) {
3618         return nullptr;
3619     }
3620 
3621     if (GetMessageUserByCustom(env, result, messageUser) == nullptr) {
3622         return nullptr;
3623     }
3624 
3625     return NapiGetNull(env);
3626 }
3627 
GetMessageUserByString(const napi_env & env,const napi_value & result,MessageUser & messageUser)3628 napi_value Common::GetMessageUserByString(const napi_env &env, const napi_value &result, MessageUser &messageUser)
3629 {
3630     ANS_LOGI("enter");
3631 
3632     napi_valuetype valuetype = napi_undefined;
3633     bool hasProperty = false;
3634     char str[STR_MAX_SIZE] = {0};
3635     size_t strLen = 0;
3636 
3637     // name: string
3638     NAPI_CALL(env, napi_has_named_property(env, result, "name", &hasProperty));
3639     if (!hasProperty) {
3640         ANS_LOGE("Property name expected.");
3641         return nullptr;
3642     }
3643     napi_value nameResult = nullptr;
3644     napi_get_named_property(env, result, "name", &nameResult);
3645     NAPI_CALL(env, napi_typeof(env, nameResult, &valuetype));
3646     if (valuetype != napi_string) {
3647         ANS_LOGE("Wrong argument type. String expected.");
3648         return nullptr;
3649     }
3650     NAPI_CALL(env, napi_get_value_string_utf8(env, nameResult, str, STR_MAX_SIZE - 1, &strLen));
3651     messageUser.SetName(str);
3652     ANS_LOGI("MessageUser::name = %{public}s", str);
3653 
3654     // key: string
3655     NAPI_CALL(env, napi_has_named_property(env, result, "key", &hasProperty));
3656     if (!hasProperty) {
3657         ANS_LOGE("Property key expected.");
3658         return nullptr;
3659     }
3660     napi_value keyResult = nullptr;
3661     napi_get_named_property(env, result, "key", &keyResult);
3662     NAPI_CALL(env, napi_typeof(env, keyResult, &valuetype));
3663     if (valuetype != napi_string) {
3664         ANS_LOGE("Wrong argument type. String expected.");
3665         return nullptr;
3666     }
3667     NAPI_CALL(env, napi_get_value_string_utf8(env, keyResult, str, STR_MAX_SIZE - 1, &strLen));
3668     messageUser.SetKey(str);
3669     ANS_LOGI("MessageUser::key = %{public}s", str);
3670 
3671     // uri: string
3672     NAPI_CALL(env, napi_has_named_property(env, result, "uri", &hasProperty));
3673     if (!hasProperty) {
3674         ANS_LOGE("Property uri expected.");
3675         return nullptr;
3676     }
3677     napi_value uriResult = nullptr;
3678     napi_get_named_property(env, result, "uri", &uriResult);
3679     NAPI_CALL(env, napi_typeof(env, uriResult, &valuetype));
3680     if (valuetype != napi_string) {
3681         ANS_LOGE("Wrong argument type. String expected.");
3682         return nullptr;
3683     }
3684     NAPI_CALL(env, napi_get_value_string_utf8(env, uriResult, str, STR_MAX_SIZE - 1, &strLen));
3685     Uri uri(str);
3686     messageUser.SetUri(uri);
3687 
3688     return NapiGetNull(env);
3689 }
3690 
GetMessageUserByBool(const napi_env & env,const napi_value & result,MessageUser & messageUser)3691 napi_value Common::GetMessageUserByBool(const napi_env &env, const napi_value &result, MessageUser &messageUser)
3692 {
3693     ANS_LOGI("enter");
3694 
3695     napi_valuetype valuetype = napi_undefined;
3696     bool hasProperty = false;
3697 
3698     // isMachine: boolean
3699     NAPI_CALL(env, napi_has_named_property(env, result, "isMachine", &hasProperty));
3700     if (!hasProperty) {
3701         ANS_LOGE("Property isMachine expected.");
3702         return nullptr;
3703     }
3704     napi_value machineResult = nullptr;
3705     napi_get_named_property(env, result, "isMachine", &machineResult);
3706     NAPI_CALL(env, napi_typeof(env, machineResult, &valuetype));
3707     if (valuetype != napi_boolean) {
3708         ANS_LOGE("Wrong argument type. Bool expected.");
3709         return nullptr;
3710     }
3711     bool machine = false;
3712     napi_get_value_bool(env, machineResult, &machine);
3713     messageUser.SetMachine(machine);
3714     ANS_LOGD("MessageUser::isMachine = %{public}d", machine);
3715 
3716     // isUserImportant: boolean
3717     NAPI_CALL(env, napi_has_named_property(env, result, "isUserImportant", &hasProperty));
3718     if (!hasProperty) {
3719         ANS_LOGE("Property isUserImportant expected.");
3720         return nullptr;
3721     }
3722     napi_value importantResult = nullptr;
3723     napi_get_named_property(env, result, "isUserImportant", &importantResult);
3724     NAPI_CALL(env, napi_typeof(env, importantResult, &valuetype));
3725     if (valuetype != napi_boolean) {
3726         ANS_LOGE("Wrong argument type. Bool expected.");
3727         return nullptr;
3728     }
3729     bool important = false;
3730     napi_get_value_bool(env, importantResult, &important);
3731     messageUser.SetUserAsImportant(important);
3732     ANS_LOGI("MessageUser::isUserImportant = %{public}d", important);
3733 
3734     return NapiGetNull(env);
3735 }
3736 
GetMessageUserByCustom(const napi_env & env,const napi_value & result,MessageUser & messageUser)3737 napi_value Common::GetMessageUserByCustom(const napi_env &env, const napi_value &result, MessageUser &messageUser)
3738 {
3739     ANS_LOGI("enter");
3740 
3741     napi_valuetype valuetype = napi_undefined;
3742     bool hasProperty = false;
3743 
3744     // icon?: image.PixelMap
3745     NAPI_CALL(env, napi_has_named_property(env, result, "icon", &hasProperty));
3746     if (hasProperty) {
3747         napi_value iconResult = nullptr;
3748         napi_get_named_property(env, result, "icon", &iconResult);
3749         NAPI_CALL(env, napi_typeof(env, iconResult, &valuetype));
3750         if (valuetype != napi_object) {
3751             ANS_LOGE("Wrong argument type. Object expected.");
3752             return nullptr;
3753         }
3754         std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
3755         pixelMap = Media::PixelMapNapi::GetPixelMap(env, iconResult);
3756         if (pixelMap == nullptr) {
3757             ANS_LOGE("Invalid object pixelMap");
3758             return nullptr;
3759         }
3760         messageUser.SetPixelMap(pixelMap);
3761     }
3762 
3763     return NapiGetNull(env);
3764 }
3765 
GetNotificationMultiLineContent(const napi_env & env,const napi_value & result,NotificationRequest & request)3766 napi_value Common::GetNotificationMultiLineContent(
3767     const napi_env &env, const napi_value &result, NotificationRequest &request)
3768 {
3769     ANS_LOGI("enter");
3770 
3771     napi_valuetype valuetype = napi_undefined;
3772     napi_value contentResult = nullptr;
3773     napi_value multiLineContentResult = nullptr;
3774     bool hasProperty = false;
3775     char str[STR_MAX_SIZE] = {0};
3776     size_t strLen = 0;
3777 
3778     NAPI_CALL(env, napi_has_named_property(env, result, "multiLine", &hasProperty));
3779     if (!hasProperty) {
3780         ANS_LOGE("Property multiLine expected.");
3781         return nullptr;
3782     }
3783     napi_get_named_property(env, result, "multiLine", &contentResult);
3784     NAPI_CALL(env, napi_typeof(env, contentResult, &valuetype));
3785     if (valuetype != napi_object) {
3786         ANS_LOGE("Wrong argument type. Object expected.");
3787         return nullptr;
3788     }
3789 
3790     std::shared_ptr<OHOS::Notification::NotificationMultiLineContent> multiLineContent =
3791         std::make_shared<OHOS::Notification::NotificationMultiLineContent>();
3792     if (multiLineContent == nullptr) {
3793         ANS_LOGE("multiLineContent is null");
3794         return nullptr;
3795     }
3796 
3797     if (GetNotificationBasicContentDetailed(env, contentResult, multiLineContent) == nullptr) {
3798         return nullptr;
3799     }
3800 
3801     // briefText: string
3802     NAPI_CALL(env, napi_has_named_property(env, contentResult, "briefText", &hasProperty));
3803     if (!hasProperty) {
3804         ANS_LOGE("Property briefText expected.");
3805         return nullptr;
3806     }
3807     napi_get_named_property(env, contentResult, "briefText", &multiLineContentResult);
3808     NAPI_CALL(env, napi_typeof(env, multiLineContentResult, &valuetype));
3809     if (valuetype != napi_string) {
3810         ANS_LOGE("Wrong argument type. String expected.");
3811         return nullptr;
3812     }
3813     NAPI_CALL(env, napi_get_value_string_utf8(env, multiLineContentResult, str, STR_MAX_SIZE - 1, &strLen));
3814     multiLineContent->SetBriefText(str);
3815     ANS_LOGD("multiLine: briefText = %{public}s", str);
3816 
3817     // longTitle: string
3818     NAPI_CALL(env, napi_has_named_property(env, contentResult, "longTitle", &hasProperty));
3819     if (!hasProperty) {
3820         ANS_LOGE("Property longTitle expected.");
3821         return nullptr;
3822     }
3823     napi_get_named_property(env, contentResult, "longTitle", &multiLineContentResult);
3824     NAPI_CALL(env, napi_typeof(env, multiLineContentResult, &valuetype));
3825     if (valuetype != napi_string) {
3826         ANS_LOGE("Wrong argument type. String expected.");
3827         return nullptr;
3828     }
3829     NAPI_CALL(env, napi_get_value_string_utf8(env, multiLineContentResult, str, STR_MAX_SIZE - 1, &strLen));
3830     multiLineContent->SetExpandedTitle(str);
3831     ANS_LOGD("multiLine: longTitle = %{public}s", str);
3832 
3833     // lines: Array<String>
3834     NAPI_CALL(env, napi_has_named_property(env, contentResult, "lines", &hasProperty));
3835     if (!hasProperty) {
3836         ANS_LOGE("Property lines expected.");
3837         return nullptr;
3838     }
3839     if (GetNotificationMultiLineContentLines(env, contentResult, multiLineContent) == nullptr) {
3840         return nullptr;
3841     }
3842 
3843     request.SetContent(std::make_shared<NotificationContent>(multiLineContent));
3844 
3845     ANS_LOGI("end");
3846     return NapiGetNull(env);
3847 }
3848 
GetNotificationMultiLineContentLines(const napi_env & env,const napi_value & result,std::shared_ptr<OHOS::Notification::NotificationMultiLineContent> & multiLineContent)3849 napi_value Common::GetNotificationMultiLineContentLines(const napi_env &env, const napi_value &result,
3850     std::shared_ptr<OHOS::Notification::NotificationMultiLineContent> &multiLineContent)
3851 {
3852     ANS_LOGI("enter");
3853 
3854     bool isArray = false;
3855     napi_valuetype valuetype = napi_undefined;
3856     napi_value multilines = nullptr;
3857     char str[STR_MAX_SIZE] = {0};
3858     size_t strLen = 0;
3859     uint32_t length = 0;
3860 
3861     napi_get_named_property(env, result, "lines", &multilines);
3862     napi_is_array(env, multilines, &isArray);
3863     if (!isArray) {
3864         ANS_LOGE("Property lines is expected to be an array.");
3865         return nullptr;
3866     }
3867 
3868     napi_get_array_length(env, multilines, &length);
3869     if (length == 0) {
3870         ANS_LOGE("The array is empty.");
3871         return nullptr;
3872     }
3873     for (size_t i = 0; i < length; i++) {
3874         napi_value line = nullptr;
3875         napi_get_element(env, multilines, i, &line);
3876         NAPI_CALL(env, napi_typeof(env, line, &valuetype));
3877         if (valuetype != napi_string) {
3878             ANS_LOGE("Wrong argument type. String expected.");
3879             return nullptr;
3880         }
3881         NAPI_CALL(env, napi_get_value_string_utf8(env, line, str, STR_MAX_SIZE - 1, &strLen));
3882         multiLineContent->AddSingleLine(str);
3883         ANS_LOGI("multiLine: lines : addSingleLine = %{public}s", str);
3884     }
3885 
3886     return NapiGetNull(env);
3887 }
3888 
GetNotificationSlot(const napi_env & env,const napi_value & value,NotificationSlot & slot)3889 napi_value Common::GetNotificationSlot(const napi_env &env, const napi_value &value, NotificationSlot &slot)
3890 {
3891     ANS_LOGI("enter");
3892 
3893     napi_value nobj = nullptr;
3894     napi_valuetype valuetype = napi_undefined;
3895     bool hasProperty = false;
3896 
3897     // type: notification.SlotType
3898     int slotType = 0;
3899     NAPI_CALL(env, napi_has_named_property(env, value, "type", &hasProperty));
3900     if (!hasProperty) {
3901         ANS_LOGE("Property type expected.");
3902         return nullptr;
3903     }
3904     napi_get_named_property(env, value, "type", &nobj);
3905     NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
3906     if (valuetype != napi_number) {
3907         ANS_LOGE("Wrong argument type. Number expected.");
3908         return nullptr;
3909     }
3910     napi_get_value_int32(env, nobj, &slotType);
3911     NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER;
3912     if (!Common::SlotTypeJSToC(SlotType(slotType), outType)) {
3913         return nullptr;
3914     }
3915     slot.SetType(outType);
3916 
3917     if (GetNotificationSlotByString(env, value, slot) == nullptr) {
3918         return nullptr;
3919     }
3920     if (GetNotificationSlotByNumber(env, value, slot) == nullptr) {
3921         return nullptr;
3922     }
3923     if (GetNotificationSlotByVibration(env, value, slot) == nullptr) {
3924         return nullptr;
3925     }
3926     if (GetNotificationSlotByBool(env, value, slot) == nullptr) {
3927         return nullptr;
3928     }
3929     return NapiGetNull(env);
3930 }
3931 
GetNotificationSlotByString(const napi_env & env,const napi_value & value,NotificationSlot & slot)3932 napi_value Common::GetNotificationSlotByString(const napi_env &env, const napi_value &value, NotificationSlot &slot)
3933 {
3934     ANS_LOGI("enter");
3935 
3936     napi_value nobj = nullptr;
3937     napi_valuetype valuetype = napi_undefined;
3938     bool hasProperty = false;
3939     size_t strLen = 0;
3940 
3941     // desc?: string
3942     NAPI_CALL(env, napi_has_named_property(env, value, "desc", &hasProperty));
3943     if (hasProperty) {
3944         std::string desc;
3945         char str[STR_MAX_SIZE] = {0};
3946         napi_get_named_property(env, value, "desc", &nobj);
3947         NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
3948         if (valuetype != napi_string) {
3949             ANS_LOGE("Wrong argument type. String expected.");
3950             return nullptr;
3951         }
3952         NAPI_CALL(env, napi_get_value_string_utf8(env, nobj, str, STR_MAX_SIZE - 1, &strLen));
3953         desc = str;
3954         ANS_LOGI("desc is: %{public}s", desc.c_str());
3955         slot.SetDescription(desc);
3956     }
3957 
3958     // sound?: string
3959     NAPI_CALL(env, napi_has_named_property(env, value, "sound", &hasProperty));
3960     if (hasProperty) {
3961         std::string sound;
3962         char str[STR_MAX_SIZE] = {0};
3963         napi_get_named_property(env, value, "sound", &nobj);
3964         NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
3965         if (valuetype != napi_string) {
3966             ANS_LOGE("Wrong argument type. String expected.");
3967             return nullptr;
3968         }
3969         NAPI_CALL(env, napi_get_value_string_utf8(env, nobj, str, STR_MAX_SIZE - 1, &strLen));
3970         sound = str;
3971         ANS_LOGI("sound is: %{public}s", sound.c_str());
3972         slot.SetSound(Uri(sound));
3973     }
3974 
3975     return NapiGetNull(env);
3976 }
3977 
GetNotificationSlotByBool(const napi_env & env,const napi_value & value,NotificationSlot & slot)3978 napi_value Common::GetNotificationSlotByBool(const napi_env &env, const napi_value &value, NotificationSlot &slot)
3979 {
3980     ANS_LOGI("enter");
3981     napi_value nobj = nullptr;
3982     napi_valuetype valuetype = napi_undefined;
3983     bool hasProperty = false;
3984 
3985     // badgeFlag?: boolean
3986     NAPI_CALL(env, napi_has_named_property(env, value, "badgeFlag", &hasProperty));
3987     if (hasProperty) {
3988         bool badgeFlag = false;
3989         napi_get_named_property(env, value, "badgeFlag", &nobj);
3990         NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
3991         if (valuetype != napi_boolean) {
3992             ANS_LOGE("Wrong argument type. Bool expected.");
3993             return nullptr;
3994         }
3995         napi_get_value_bool(env, nobj, &badgeFlag);
3996         ANS_LOGI("badgeFlag is: %{public}d", badgeFlag);
3997         slot.EnableBadge(badgeFlag);
3998     }
3999 
4000     // bypassDnd?: boolean
4001     NAPI_CALL(env, napi_has_named_property(env, value, "bypassDnd", &hasProperty));
4002     if (hasProperty) {
4003         bool bypassDnd = false;
4004         napi_get_named_property(env, value, "bypassDnd", &nobj);
4005         NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
4006         if (valuetype != napi_boolean) {
4007             ANS_LOGE("Wrong argument type. Bool expected.");
4008             return nullptr;
4009         }
4010         napi_get_value_bool(env, nobj, &bypassDnd);
4011         ANS_LOGI("bypassDnd is: %{public}d", bypassDnd);
4012         slot.EnableBypassDnd(bypassDnd);
4013     }
4014 
4015     // lightEnabled?: boolean
4016     NAPI_CALL(env, napi_has_named_property(env, value, "lightEnabled", &hasProperty));
4017     if (hasProperty) {
4018         bool lightEnabled = false;
4019         napi_get_named_property(env, value, "lightEnabled", &nobj);
4020         NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
4021         if (valuetype != napi_boolean) {
4022             ANS_LOGE("Wrong argument type. Bool expected.");
4023             return nullptr;
4024         }
4025         napi_get_value_bool(env, nobj, &lightEnabled);
4026         ANS_LOGI("lightEnabled is: %{public}d", lightEnabled);
4027         slot.SetEnableLight(lightEnabled);
4028     }
4029 
4030     return NapiGetNull(env);
4031 }
4032 
GetNotificationSlotByNumber(const napi_env & env,const napi_value & value,NotificationSlot & slot)4033 napi_value Common::GetNotificationSlotByNumber(const napi_env &env, const napi_value &value, NotificationSlot &slot)
4034 {
4035     ANS_LOGI("enter");
4036 
4037     napi_value nobj = nullptr;
4038     napi_valuetype valuetype = napi_undefined;
4039     bool hasProperty = false;
4040 
4041     // level?: number
4042     NAPI_CALL(env, napi_has_named_property(env, value, "level", &hasProperty));
4043     if (hasProperty) {
4044         int inLevel = 0;
4045         napi_get_named_property(env, value, "level", &nobj);
4046         NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
4047         if (valuetype != napi_number) {
4048             ANS_LOGE("Wrong argument type. Number expected.");
4049             return nullptr;
4050         }
4051         napi_get_value_int32(env, nobj, &inLevel);
4052         ANS_LOGI("level is: %{public}d", inLevel);
4053 
4054         NotificationSlot::NotificationLevel outLevel {NotificationSlot::NotificationLevel::LEVEL_NONE};
4055         if (!Common::SlotLevelJSToC(SlotLevel(inLevel), outLevel)) {
4056             return nullptr;
4057         }
4058         slot.SetLevel(outLevel);
4059     }
4060 
4061     // lockscreenVisibility?: number
4062     NAPI_CALL(env, napi_has_named_property(env, value, "lockscreenVisibility", &hasProperty));
4063     if (hasProperty) {
4064         int lockscreenVisibility = 0;
4065         napi_get_named_property(env, value, "lockscreenVisibility", &nobj);
4066         NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
4067         if (valuetype != napi_number) {
4068             ANS_LOGE("Wrong argument type. Number expected.");
4069             return nullptr;
4070         }
4071         napi_get_value_int32(env, nobj, &lockscreenVisibility);
4072         ANS_LOGI("lockscreenVisibility is: %{public}d", lockscreenVisibility);
4073         slot.SetLockscreenVisibleness(NotificationConstant::VisiblenessType(lockscreenVisibility));
4074     }
4075 
4076     // lightColor?: number
4077     NAPI_CALL(env, napi_has_named_property(env, value, "lightColor", &hasProperty));
4078     if (hasProperty) {
4079         int lightColor = 0;
4080         napi_get_named_property(env, value, "lightColor", &nobj);
4081         NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
4082         if (valuetype != napi_number) {
4083             ANS_LOGE("Wrong argument type. Number expected.");
4084             return nullptr;
4085         }
4086         napi_get_value_int32(env, nobj, &lightColor);
4087         ANS_LOGI("lightColor is: %{public}d", lightColor);
4088         slot.SetLedLightColor(lightColor);
4089     }
4090     return NapiGetNull(env);
4091 }
4092 
GetNotificationSlotByVibration(const napi_env & env,const napi_value & value,NotificationSlot & slot)4093 napi_value Common::GetNotificationSlotByVibration(const napi_env &env, const napi_value &value, NotificationSlot &slot)
4094 {
4095     ANS_LOGI("enter");
4096     napi_value nobj = nullptr;
4097     napi_valuetype valuetype = napi_undefined;
4098     bool hasProperty = false;
4099     uint32_t length = 0;
4100 
4101     // vibrationEnabled?: boolean
4102     bool vibrationEnabled = false;
4103     NAPI_CALL(env, napi_has_named_property(env, value, "vibrationEnabled", &hasProperty));
4104     if (hasProperty) {
4105         napi_get_named_property(env, value, "vibrationEnabled", &nobj);
4106         NAPI_CALL(env, napi_typeof(env, nobj, &valuetype));
4107         if (valuetype != napi_boolean) {
4108             ANS_LOGE("Wrong argument type. Bool expected.");
4109             return nullptr;
4110         }
4111 
4112         napi_get_value_bool(env, nobj, &vibrationEnabled);
4113         slot.SetEnableVibration(vibrationEnabled);
4114     }
4115 
4116     if (!vibrationEnabled) {
4117         return NapiGetNull(env);
4118     }
4119 
4120     // vibrationValues?: Array<number>
4121     NAPI_CALL(env, napi_has_named_property(env, value, "vibrationValues", &hasProperty));
4122     if (hasProperty) {
4123         bool isArray = false;
4124         napi_get_named_property(env, value, "vibrationValues", &nobj);
4125         napi_is_array(env, nobj, &isArray);
4126         if (!isArray) {
4127             ANS_LOGE("Property vibrationValues is expected to be an array.");
4128             return nullptr;
4129         }
4130 
4131         napi_get_array_length(env, nobj, &length);
4132         std::vector<int64_t> vibrationValues;
4133         for (size_t i = 0; i < length; i++) {
4134             napi_value nVibrationValue = nullptr;
4135             int64_t vibrationValue = 0;
4136             napi_get_element(env, nobj, i, &nVibrationValue);
4137             NAPI_CALL(env, napi_typeof(env, nVibrationValue, &valuetype));
4138             if (valuetype != napi_number) {
4139                 ANS_LOGE("Wrong argument type. Number expected.");
4140                 return nullptr;
4141             }
4142             napi_get_value_int64(env, nVibrationValue, &vibrationValue);
4143             vibrationValues.emplace_back(vibrationValue);
4144         }
4145         slot.SetVibrationStyle(vibrationValues);
4146     }
4147 
4148     return NapiGetNull(env);
4149 }
4150 
GetBundleOption(const napi_env & env,const napi_value & value,NotificationBundleOption & option)4151 napi_value Common::GetBundleOption(const napi_env &env, const napi_value &value, NotificationBundleOption &option)
4152 {
4153     ANS_LOGI("enter");
4154 
4155     bool hasProperty {false};
4156     napi_valuetype valuetype = napi_undefined;
4157     napi_value result = nullptr;
4158 
4159     char str[STR_MAX_SIZE] = {0};
4160     size_t strLen = 0;
4161     // bundle: string
4162     NAPI_CALL(env, napi_has_named_property(env, value, "bundle", &hasProperty));
4163     if (!hasProperty) {
4164         ANS_LOGE("Property bundle expected.");
4165         return nullptr;
4166     }
4167     napi_get_named_property(env, value, "bundle", &result);
4168     NAPI_CALL(env, napi_typeof(env, result, &valuetype));
4169     if (valuetype != napi_string) {
4170         ANS_LOGE("Wrong argument type. String expected.");
4171         return nullptr;
4172     }
4173     NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
4174     option.SetBundleName(str);
4175 
4176     // uid?: number
4177     NAPI_CALL(env, napi_has_named_property(env, value, "uid", &hasProperty));
4178     if (hasProperty) {
4179         int32_t uid = 0;
4180         napi_get_named_property(env, value, "uid", &result);
4181         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
4182         if (valuetype != napi_number) {
4183             ANS_LOGE("Wrong argument type. Number expected.");
4184             return nullptr;
4185         }
4186         napi_get_value_int32(env, result, &uid);
4187         option.SetUid(uid);
4188     }
4189 
4190     return NapiGetNull(env);
4191 }
4192 
GetNotificationKey(const napi_env & env,const napi_value & value,NotificationKey & key)4193 napi_value Common::GetNotificationKey(const napi_env &env, const napi_value &value, NotificationKey &key)
4194 {
4195     ANS_LOGI("enter");
4196 
4197     bool hasProperty {false};
4198     napi_valuetype valuetype = napi_undefined;
4199     napi_value result = nullptr;
4200 
4201     // id: number
4202     NAPI_CALL(env, napi_has_named_property(env, value, "id", &hasProperty));
4203     if (!hasProperty) {
4204         ANS_LOGE("Property id expected.");
4205         return nullptr;
4206     }
4207     napi_get_named_property(env, value, "id", &result);
4208     NAPI_CALL(env, napi_typeof(env, result, &valuetype));
4209     if (valuetype != napi_number) {
4210         ANS_LOGE("Wrong argument type. Number expected.");
4211         return nullptr;
4212     }
4213     napi_get_value_int32(env, result, &key.id);
4214 
4215     // label?: string
4216     NAPI_CALL(env, napi_has_named_property(env, value, "label", &hasProperty));
4217     if (hasProperty) {
4218         char str[STR_MAX_SIZE] = {0};
4219         size_t strLen = 0;
4220         napi_get_named_property(env, value, "label", &result);
4221         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
4222         if (valuetype != napi_string) {
4223             ANS_LOGE("Wrong argument type. String expected.");
4224             return nullptr;
4225         }
4226         NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
4227         key.label = str;
4228     }
4229 
4230     return NapiGetNull(env);
4231 }
4232 
ContentTypeJSToC(const ContentType & inType,NotificationContent::Type & outType)4233 bool Common::ContentTypeJSToC(const ContentType &inType, NotificationContent::Type &outType)
4234 {
4235     switch (inType) {
4236         case ContentType::NOTIFICATION_CONTENT_BASIC_TEXT:
4237             outType = NotificationContent::Type::BASIC_TEXT;
4238             break;
4239         case ContentType::NOTIFICATION_CONTENT_LONG_TEXT:
4240             outType = NotificationContent::Type::LONG_TEXT;
4241             break;
4242         case ContentType::NOTIFICATION_CONTENT_MULTILINE:
4243             outType = NotificationContent::Type::MULTILINE;
4244             break;
4245         case ContentType::NOTIFICATION_CONTENT_PICTURE:
4246             outType = NotificationContent::Type::PICTURE;
4247             break;
4248         case ContentType::NOTIFICATION_CONTENT_CONVERSATION:
4249             outType = NotificationContent::Type::CONVERSATION;
4250             break;
4251         default:
4252             ANS_LOGE("ContentType %{public}d is an invalid value", inType);
4253             return false;
4254     }
4255     return true;
4256 }
4257 
ContentTypeCToJS(const NotificationContent::Type & inType,ContentType & outType)4258 bool Common::ContentTypeCToJS(const NotificationContent::Type &inType, ContentType &outType)
4259 {
4260     switch (inType) {
4261         case NotificationContent::Type::BASIC_TEXT:
4262             outType = ContentType::NOTIFICATION_CONTENT_BASIC_TEXT;
4263             break;
4264         case NotificationContent::Type::LONG_TEXT:
4265             outType = ContentType::NOTIFICATION_CONTENT_LONG_TEXT;
4266             break;
4267         case NotificationContent::Type::MULTILINE:
4268             outType = ContentType::NOTIFICATION_CONTENT_MULTILINE;
4269             break;
4270         case NotificationContent::Type::PICTURE:
4271             outType = ContentType::NOTIFICATION_CONTENT_PICTURE;
4272             break;
4273         case NotificationContent::Type::CONVERSATION:
4274             outType = ContentType::NOTIFICATION_CONTENT_CONVERSATION;
4275             break;
4276         default:
4277             ANS_LOGE("ContentType %{public}d is an invalid value", inType);
4278             return false;
4279     }
4280     return true;
4281 }
4282 
SlotTypeJSToC(const SlotType & inType,NotificationConstant::SlotType & outType)4283 bool Common::SlotTypeJSToC(const SlotType &inType, NotificationConstant::SlotType &outType)
4284 {
4285     switch (inType) {
4286         case SlotType::SOCIAL_COMMUNICATION:
4287             outType = NotificationConstant::SlotType::SOCIAL_COMMUNICATION;
4288             break;
4289         case SlotType::SERVICE_INFORMATION:
4290             outType = NotificationConstant::SlotType::SERVICE_REMINDER;
4291             break;
4292         case SlotType::CONTENT_INFORMATION:
4293             outType = NotificationConstant::SlotType::CONTENT_INFORMATION;
4294             break;
4295         case SlotType::UNKNOWN_TYPE:
4296         case SlotType::OTHER_TYPES:
4297             outType = NotificationConstant::SlotType::OTHER;
4298             break;
4299         default:
4300             ANS_LOGE("SlotType %{public}d is an invalid value", inType);
4301             return false;
4302     }
4303     return true;
4304 }
4305 
SlotTypeCToJS(const NotificationConstant::SlotType & inType,SlotType & outType)4306 bool Common::SlotTypeCToJS(const NotificationConstant::SlotType &inType, SlotType &outType)
4307 {
4308     switch (inType) {
4309         case NotificationConstant::SlotType::CUSTOM:
4310             outType = SlotType::UNKNOWN_TYPE;
4311             break;
4312         case NotificationConstant::SlotType::SOCIAL_COMMUNICATION:
4313             outType = SlotType::SOCIAL_COMMUNICATION;
4314             break;
4315         case NotificationConstant::SlotType::SERVICE_REMINDER:
4316             outType = SlotType::SERVICE_INFORMATION;
4317             break;
4318         case NotificationConstant::SlotType::CONTENT_INFORMATION:
4319             outType = SlotType::CONTENT_INFORMATION;
4320             break;
4321         case NotificationConstant::SlotType::OTHER:
4322             outType = SlotType::OTHER_TYPES;
4323             break;
4324         default:
4325             ANS_LOGE("SlotType %{public}d is an invalid value", inType);
4326             return false;
4327     }
4328     return true;
4329 }
4330 
SlotLevelJSToC(const SlotLevel & inLevel,NotificationSlot::NotificationLevel & outLevel)4331 bool Common::SlotLevelJSToC(const SlotLevel &inLevel, NotificationSlot::NotificationLevel &outLevel)
4332 {
4333     switch (inLevel) {
4334         case SlotLevel::LEVEL_NONE:
4335             outLevel = NotificationSlot::NotificationLevel::LEVEL_NONE;
4336             break;
4337         case SlotLevel::LEVEL_MIN:
4338             outLevel = NotificationSlot::NotificationLevel::LEVEL_MIN;
4339             break;
4340         case SlotLevel::LEVEL_LOW:
4341             outLevel = NotificationSlot::NotificationLevel::LEVEL_LOW;
4342             break;
4343         case SlotLevel::LEVEL_DEFAULT:
4344             outLevel = NotificationSlot::NotificationLevel::LEVEL_DEFAULT;
4345             break;
4346         case SlotLevel::LEVEL_HIGH:
4347             outLevel = NotificationSlot::NotificationLevel::LEVEL_HIGH;
4348             break;
4349         default:
4350             ANS_LOGE("SlotLevel %{public}d is an invalid value", inLevel);
4351             return false;
4352     }
4353     return true;
4354 }
4355 
SlotLevelCToJS(const NotificationSlot::NotificationLevel & inLevel,SlotLevel & outLevel)4356 bool Common::SlotLevelCToJS(const NotificationSlot::NotificationLevel &inLevel, SlotLevel &outLevel)
4357 {
4358     switch (inLevel) {
4359         case NotificationSlot::NotificationLevel::LEVEL_NONE:
4360         case NotificationSlot::NotificationLevel::LEVEL_UNDEFINED:
4361             outLevel = SlotLevel::LEVEL_NONE;
4362             break;
4363         case NotificationSlot::NotificationLevel::LEVEL_MIN:
4364             outLevel = SlotLevel::LEVEL_MIN;
4365             break;
4366         case NotificationSlot::NotificationLevel::LEVEL_LOW:
4367             outLevel = SlotLevel::LEVEL_LOW;
4368             break;
4369         case NotificationSlot::NotificationLevel::LEVEL_DEFAULT:
4370             outLevel = SlotLevel::LEVEL_DEFAULT;
4371             break;
4372         case NotificationSlot::NotificationLevel::LEVEL_HIGH:
4373             outLevel = SlotLevel::LEVEL_HIGH;
4374             break;
4375         default:
4376             ANS_LOGE("SlotLevel %{public}d is an invalid value", inLevel);
4377             return false;
4378     }
4379     return true;
4380 }
4381 
ReasonCToJS(const int & inType,int & outType)4382 bool Common::ReasonCToJS(const int &inType, int &outType)
4383 {
4384     switch (inType) {
4385         case NotificationConstant::CLICK_REASON_DELETE:
4386             outType = static_cast<int32_t>(RemoveReason::CLICK_REASON_REMOVE);
4387             break;
4388         case NotificationConstant::CANCEL_REASON_DELETE:
4389             outType = static_cast<int32_t>(RemoveReason::CANCEL_REASON_REMOVE);
4390             break;
4391         case NotificationConstant::CANCEL_ALL_REASON_DELETE:
4392             outType = static_cast<int32_t>(RemoveReason::CANCEL_ALL_REASON_REMOVE);
4393             break;
4394         case NotificationConstant::ERROR_REASON_DELETE:
4395             outType = static_cast<int32_t>(RemoveReason::ERROR_REASON_REMOVE);
4396             break;
4397         case NotificationConstant::PACKAGE_CHANGED_REASON_DELETE:
4398             outType = static_cast<int32_t>(RemoveReason::PACKAGE_CHANGED_REASON_REMOVE);
4399             break;
4400         case NotificationConstant::USER_STOPPED_REASON_DELETE:
4401             outType = static_cast<int32_t>(RemoveReason::USER_STOPPED_REASON_REMOVE);
4402             break;
4403         case NotificationConstant::APP_CANCEL_REASON_DELETE:
4404             outType = static_cast<int32_t>(RemoveReason::APP_CANCEL_REASON_REMOVE);
4405             break;
4406         case NotificationConstant::APP_CANCEL_ALL_REASON_DELETE:
4407             outType = static_cast<int32_t>(RemoveReason::APP_CANCEL_ALL_REASON_REMOVE);
4408             break;
4409         case NotificationConstant::APP_CANCEL_REASON_OTHER:
4410             outType = static_cast<int32_t>(RemoveReason::APP_CANCEL_REASON_OTHER);
4411             break;
4412         default:
4413             ANS_LOGE("Reason %{public}d is an invalid value", inType);
4414             return false;
4415     }
4416     return true;
4417 }
4418 
IsValidRemoveReason(int32_t reasonType)4419 bool Common::IsValidRemoveReason(int32_t reasonType)
4420 {
4421     if (reasonType == NotificationConstant::CLICK_REASON_DELETE ||
4422         reasonType == NotificationConstant::CANCEL_REASON_DELETE) {
4423         return true;
4424     }
4425     ANS_LOGE("Reason %{public}d is an invalid value", reasonType);
4426     return false;
4427 }
4428 
DoNotDisturbTypeJSToC(const DoNotDisturbType & inType,NotificationConstant::DoNotDisturbType & outType)4429 bool Common::DoNotDisturbTypeJSToC(const DoNotDisturbType &inType, NotificationConstant::DoNotDisturbType &outType)
4430 {
4431     switch (inType) {
4432         case DoNotDisturbType::TYPE_NONE:
4433             outType = NotificationConstant::DoNotDisturbType::NONE;
4434             break;
4435         case DoNotDisturbType::TYPE_ONCE:
4436             outType = NotificationConstant::DoNotDisturbType::ONCE;
4437             break;
4438         case DoNotDisturbType::TYPE_DAILY:
4439             outType = NotificationConstant::DoNotDisturbType::DAILY;
4440             break;
4441         case DoNotDisturbType::TYPE_CLEARLY:
4442             outType = NotificationConstant::DoNotDisturbType::CLEARLY;
4443             break;
4444         default:
4445             ANS_LOGE("DoNotDisturbType %{public}d is an invalid value", inType);
4446             return false;
4447     }
4448     return true;
4449 }
4450 
DoNotDisturbTypeCToJS(const NotificationConstant::DoNotDisturbType & inType,DoNotDisturbType & outType)4451 bool Common::DoNotDisturbTypeCToJS(const NotificationConstant::DoNotDisturbType &inType, DoNotDisturbType &outType)
4452 {
4453     switch (inType) {
4454         case NotificationConstant::DoNotDisturbType::NONE:
4455             outType = DoNotDisturbType::TYPE_NONE;
4456             break;
4457         case NotificationConstant::DoNotDisturbType::ONCE:
4458             outType = DoNotDisturbType::TYPE_ONCE;
4459             break;
4460         case NotificationConstant::DoNotDisturbType::DAILY:
4461             outType = DoNotDisturbType::TYPE_DAILY;
4462             break;
4463         case NotificationConstant::DoNotDisturbType::CLEARLY:
4464             outType = DoNotDisturbType::TYPE_CLEARLY;
4465             break;
4466         default:
4467             ANS_LOGE("DoNotDisturbType %{public}d is an invalid value", inType);
4468             return false;
4469     }
4470     return true;
4471 }
4472 
DeviceRemindTypeCToJS(const NotificationConstant::RemindType & inType,DeviceRemindType & outType)4473 bool Common::DeviceRemindTypeCToJS(const NotificationConstant::RemindType &inType, DeviceRemindType &outType)
4474 {
4475     switch (inType) {
4476         case NotificationConstant::RemindType::DEVICE_IDLE_DONOT_REMIND:
4477             outType = DeviceRemindType::IDLE_DONOT_REMIND;
4478             break;
4479         case NotificationConstant::RemindType::DEVICE_IDLE_REMIND:
4480             outType = DeviceRemindType::IDLE_REMIND;
4481             break;
4482         case NotificationConstant::RemindType::DEVICE_ACTIVE_DONOT_REMIND:
4483             outType = DeviceRemindType::ACTIVE_DONOT_REMIND;
4484             break;
4485         case NotificationConstant::RemindType::DEVICE_ACTIVE_REMIND:
4486             outType = DeviceRemindType::ACTIVE_REMIND;
4487             break;
4488         default:
4489             ANS_LOGE("DeviceRemindType %{public}d is an invalid value", inType);
4490             return false;
4491     }
4492     return true;
4493 }
4494 
SourceTypeCToJS(const NotificationConstant::SourceType & inType,SourceType & outType)4495 bool Common::SourceTypeCToJS(const NotificationConstant::SourceType &inType, SourceType &outType)
4496 {
4497     switch (inType) {
4498         case NotificationConstant::SourceType::TYPE_NORMAL:
4499             outType = SourceType::TYPE_NORMAL;
4500             break;
4501         case NotificationConstant::SourceType::TYPE_CONTINUOUS:
4502             outType = SourceType::TYPE_CONTINUOUS;
4503             break;
4504         case NotificationConstant::SourceType::TYPE_TIMER:
4505             outType = SourceType::TYPE_TIMER;
4506             break;
4507         default:
4508             ANS_LOGE("SourceType %{public}d is an invalid value", inType);
4509             return false;
4510     }
4511     return true;
4512 }
4513 
CreateWantAgentByJS(const napi_env & env,const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> & agent)4514 napi_value Common::CreateWantAgentByJS(const napi_env &env,
4515     const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> &agent)
4516 {
4517     if (agent == nullptr) {
4518         ANS_LOGI("agent is nullptr");
4519         return nullptr;
4520     }
4521 
4522     wantAgent_.insert(agent);
4523     napi_value wantAgent = nullptr;
4524     napi_value wantAgentClass = nullptr;
4525     napi_define_class(env,
4526         "wantAgentClass",
4527         NAPI_AUTO_LENGTH,
4528         [](napi_env env, napi_callback_info info) -> napi_value {
4529             napi_value thisVar = nullptr;
4530             napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
4531             return thisVar;
4532         },
4533         nullptr,
4534         0,
4535         nullptr,
4536         &wantAgentClass);
4537     napi_new_instance(env, wantAgentClass, 0, nullptr, &wantAgent);
4538     napi_wrap(env,
4539         wantAgent,
4540         (void *)agent.get(),
4541         [](napi_env env, void *data, void *hint) {
4542             AbilityRuntime::WantAgent::WantAgent *objectInfo =
4543                 static_cast<AbilityRuntime::WantAgent::WantAgent *>(data);
4544             if (objectInfo) {
4545                 for (auto it = wantAgent_.begin(); it != wantAgent_.end(); ++it) {
4546                     if ((*it).get() == objectInfo) {
4547                         wantAgent_.erase(it);
4548                         break;
4549                     }
4550                 }
4551             }
4552         },
4553         nullptr,
4554         nullptr);
4555 
4556     return wantAgent;
4557 }
4558 
GetNotificationTemplate(const napi_env & env,const napi_value & value,NotificationRequest & request)4559 napi_value Common::GetNotificationTemplate(const napi_env &env, const napi_value &value, NotificationRequest &request)
4560 {
4561     ANS_LOGI("enter");
4562 
4563     napi_valuetype valuetype = napi_undefined;
4564     napi_value result = nullptr;
4565     bool hasProperty = false;
4566 
4567     NAPI_CALL(env, napi_has_named_property(env, value, "template", &hasProperty));
4568     if (hasProperty) {
4569         napi_get_named_property(env, value, "template", &result);
4570         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
4571         if (valuetype != napi_object) {
4572             ANS_LOGE("Wrong argument type. Object expected.");
4573             return nullptr;
4574         }
4575 
4576         std::shared_ptr<NotificationTemplate> templ = std::make_shared<NotificationTemplate>();
4577         if (templ == nullptr) {
4578             ANS_LOGE("template is null");
4579             return nullptr;
4580         }
4581         if (GetNotificationTemplateInfo(env, result, templ) == nullptr) {
4582             return nullptr;
4583         }
4584 
4585         request.SetTemplate(templ);
4586     }
4587 
4588     return NapiGetNull(env);
4589 }
4590 
GetNotificationTemplateInfo(const napi_env & env,const napi_value & value,std::shared_ptr<NotificationTemplate> & templ)4591 napi_value Common::GetNotificationTemplateInfo(const napi_env &env, const napi_value &value,
4592     std::shared_ptr<NotificationTemplate> &templ)
4593 {
4594     ANS_LOGI("enter");
4595 
4596     napi_valuetype valuetype = napi_undefined;
4597     napi_value result = nullptr;
4598     bool hasProperty = false;
4599     char str[STR_MAX_SIZE] = {0};
4600     size_t strLen = 0;
4601 
4602     // name: string
4603     NAPI_CALL(env, napi_has_named_property(env, value, "name", &hasProperty));
4604     if (!hasProperty) {
4605         ANS_LOGE("Property name expected.");
4606         return nullptr;
4607     }
4608     napi_get_named_property(env, value, "name", &result);
4609     NAPI_CALL(env, napi_typeof(env, result, &valuetype));
4610     if (valuetype != napi_string) {
4611         ANS_LOGE("Wrong argument type. String expected.");
4612         return nullptr;
4613     }
4614     NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen));
4615     std::string strInput = str;
4616     templ->SetTemplateName(strInput);
4617 
4618     // data?: {[key: string]: object}
4619     NAPI_CALL(env, napi_has_named_property(env, value, "data", &hasProperty));
4620     if (hasProperty) {
4621         napi_get_named_property(env, value, "data", &result);
4622         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
4623         if (valuetype != napi_object) {
4624             ANS_LOGE("Wrong argument type. Object expected.");
4625             return nullptr;
4626         }
4627         AAFwk::WantParams wantParams;
4628         if (!OHOS::AppExecFwk::UnwrapWantParams(env, result, wantParams)) {
4629             return nullptr;
4630         }
4631 
4632         std::shared_ptr<AAFwk::WantParams> data = std::make_shared<AAFwk::WantParams>(wantParams);
4633         templ->SetTemplateData(data);
4634     }
4635 
4636     return NapiGetNull(env);
4637 }
4638 
SetNotificationTemplateInfo(const napi_env & env,const std::shared_ptr<NotificationTemplate> & templ,napi_value & result)4639 napi_value Common::SetNotificationTemplateInfo(
4640     const napi_env &env, const std::shared_ptr<NotificationTemplate> &templ, napi_value &result)
4641 {
4642     ANS_LOGI("enter");
4643 
4644     if (templ == nullptr) {
4645         ANS_LOGE("templ is null");
4646         return NapiGetBoolean(env, false);
4647     }
4648 
4649     napi_value value = nullptr;
4650 
4651     // name: string;
4652     napi_create_string_utf8(env, templ->GetTemplateName().c_str(), NAPI_AUTO_LENGTH, &value);
4653     napi_set_named_property(env, result, "name", value);
4654 
4655     std::shared_ptr<AAFwk::WantParams> data = templ->GetTemplateData();
4656     if (data) {
4657         value = OHOS::AppExecFwk::WrapWantParams(env, *data);
4658         napi_set_named_property(env, result, "data", value);
4659     }
4660 
4661     return NapiGetBoolean(env, true);
4662 }
4663 
SetNotificationFlags(const napi_env & env,const std::shared_ptr<NotificationFlags> & flags,napi_value & result)4664 napi_value Common::SetNotificationFlags(
4665     const napi_env &env, const std::shared_ptr<NotificationFlags> &flags, napi_value &result)
4666 {
4667     ANS_LOGI("enter");
4668 
4669     if (flags == nullptr) {
4670         ANS_LOGE("flags is null");
4671         return NapiGetBoolean(env, false);
4672     }
4673 
4674     napi_value value = nullptr;
4675 
4676     int32_t soundEnabled = static_cast<int32_t>(flags->IsSoundEnabled());
4677     napi_create_int32(env, soundEnabled, &value);
4678     napi_set_named_property(env, result, "soundEnabled", value);
4679 
4680     int32_t vibrationEnabled = static_cast<int32_t>(flags->IsVibrationEnabled());
4681     napi_create_int32(env, vibrationEnabled, &value);
4682     napi_set_named_property(env, result, "vibrationEnabled", value);
4683 
4684     return NapiGetBoolean(env, true);
4685 }
4686 
GetNotificationBadgeNumber(const napi_env & env,const napi_value & value,NotificationRequest & request)4687 napi_value Common::GetNotificationBadgeNumber(
4688     const napi_env &env, const napi_value &value, NotificationRequest &request)
4689 {
4690     ANS_LOGI("enter");
4691 
4692     napi_valuetype valuetype = napi_undefined;
4693     napi_value result = nullptr;
4694     bool hasProperty = false;
4695     int32_t badgeNumber = 0;
4696 
4697     NAPI_CALL(env, napi_has_named_property(env, value, "badgeNumber", &hasProperty));
4698     if (hasProperty) {
4699         napi_get_named_property(env, value, "badgeNumber", &result);
4700         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
4701         if (valuetype != napi_number) {
4702             ANS_LOGE("Wrong argument type. Number expected.");
4703             return nullptr;
4704         }
4705 
4706         napi_get_value_int32(env, result, &badgeNumber);
4707         if (badgeNumber < 0) {
4708             ANS_LOGE("Wrong badge number.");
4709             return nullptr;
4710         }
4711 
4712         request.SetBadgeNumber(badgeNumber);
4713     }
4714 
4715     return NapiGetNull(env);
4716 }
4717 
CreateReturnValue(const napi_env & env,const CallbackPromiseInfo & info,const napi_value & result)4718 void Common::CreateReturnValue(const napi_env &env, const CallbackPromiseInfo &info, const napi_value &result)
4719 {
4720     ANS_LOGI("enter errorCode=%{public}d", info.errorCode);
4721     int32_t errorCode = info.errorCode == ERR_OK ? ERR_OK : ErrorToExternal(info.errorCode);
4722     if (info.isCallback) {
4723         SetCallback(env, info.callback, errorCode, result, true);
4724     } else {
4725         SetPromise(env, info.deferred, errorCode, result, true);
4726     }
4727     ANS_LOGI("end");
4728 }
4729 
ErrorToExternal(uint32_t errCode)4730 int32_t Common::ErrorToExternal(uint32_t errCode)
4731 {
4732     int32_t ExternalCode = ERROR_INTERNAL_ERROR;
4733     switch (errCode) {
4734         case ERR_ANS_PERMISSION_DENIED:
4735         case ERR_ANS_NON_SYSTEM_APP:
4736             ExternalCode = ERROR_PERMISSION_DENIED;
4737             break;
4738         case ERR_ANS_INVALID_PARAM:
4739         case ERR_ANS_INVALID_UID:
4740         case ERR_ANS_ICON_OVER_SIZE:
4741         case ERR_ANS_PICTURE_OVER_SIZE:
4742             ExternalCode = ERROR_PARAM_INVALID;
4743             break;
4744         case ERR_ANS_NO_MEMORY:
4745         case ERR_ANS_TASK_ERR:
4746             ExternalCode = ERROR_INTERNAL_ERROR;
4747             break;
4748         case ERR_ANS_PARCELABLE_FAILED:
4749         case ERR_ANS_TRANSACT_FAILED:
4750         case ERR_ANS_REMOTE_DEAD:
4751             ExternalCode = ERROR_IPC_ERROR;
4752             break;
4753         case ERR_ANS_SERVICE_NOT_READY:
4754         case ERR_ANS_SERVICE_NOT_CONNECTED:
4755             ExternalCode = ERROR_SERVICE_CONNECT_ERROR;
4756             break;
4757         case ERR_ANS_NOT_ALLOWED:
4758             ExternalCode = ERROR_NOTIFICATION_CLOSED;
4759             break;
4760         case ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_ENABLED:
4761             ExternalCode = ERROR_SLOT_CLOSED;
4762             break;
4763         case ERR_ANS_NOTIFICATION_IS_UNREMOVABLE:
4764             ExternalCode = ERROR_NOTIFICATION_UNREMOVABLE;
4765             break;
4766         case ERR_ANS_NOTIFICATION_NOT_EXISTS:
4767             ExternalCode = ERROR_NOTIFICATION_NOT_EXIST;
4768             break;
4769         case ERR_ANS_GET_ACTIVE_USER_FAILED:
4770             ExternalCode = ERROR_USER_NOT_EXIST;
4771             break;
4772         case ERR_ANS_INVALID_PID:
4773         case ERR_ANS_INVALID_BUNDLE:
4774             ExternalCode = ERROR_BUNDLE_NOT_FOUND;
4775             break;
4776         case ERR_ANS_OVER_MAX_ACTIVE_PERSECOND:
4777             ExternalCode = ERROR_OVER_MAX_NUM_PER_SECOND;
4778             break;
4779         case ERR_ANS_DISTRIBUTED_OPERATION_FAILED:
4780         case ERR_ANS_DISTRIBUTED_GET_INFO_FAILED:
4781             ExternalCode = ERROR_DISTRIBUTED_OPERATION_FAILED;
4782             break;
4783         case ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED:
4784             ExternalCode = ERROR_READ_TEMPLATE_CONFIG_FAILED;
4785             break;
4786         default:
4787             ExternalCode = ERROR_INTERNAL_ERROR;
4788             break;
4789     }
4790 
4791     ANS_LOGI("internal errorCode[%{public}u] to external errorCode[%{public}d]", errCode, ExternalCode);
4792     return ExternalCode;
4793 }
4794 }  // namespace NotificationNapi
4795 }  // namespace OHOS
4796