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