1 /*
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "disturb_mode.h"
17 #include "ans_inner_errors.h"
18 
19 namespace OHOS {
20 namespace NotificationNapi {
21 const int SET_DISTURB_MAX_PARA = 3;
22 const int SET_DISTURB_MIN_PARA = 1;
23 const int GET_DISTURB_MAX_PARA = 2;
24 const int DISTURB_PROFILES_PARA = 1;
25 const int DO_NOT_DISTURB_PROFILE_MIN_ID = 1;
26 const int DO_NOT_DISTURB_PROFILE_MAX_ID = 10;
27 
GetDoNotDisturbDate(const napi_env & env,const napi_value & argv,SetDoNotDisturbDateParams & params)28 napi_value GetDoNotDisturbDate(const napi_env &env, const napi_value &argv, SetDoNotDisturbDateParams &params)
29 {
30     ANS_LOGD("enter");
31     napi_value value = nullptr;
32     bool hasProperty = false;
33     napi_valuetype valuetype = napi_undefined;
34     // argv[0]: date:type
35     NAPI_CALL(env, napi_has_named_property(env, argv, "type", &hasProperty));
36     if (!hasProperty) {
37         ANS_LOGW("Wrong argument type. Property type expected.");
38         Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
39         return nullptr;
40     }
41     napi_get_named_property(env, argv, "type", &value);
42     NAPI_CALL(env, napi_typeof(env, value, &valuetype));
43     if (valuetype != napi_number) {
44         ANS_LOGW("Wrong argument type. Number expected.");
45         std::string msg = "Incorrect parameter types.The type of param must be number.";
46         Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
47         return nullptr;
48     }
49     int type = 0;
50     NotificationConstant::DoNotDisturbType outType = NotificationConstant::DoNotDisturbType::NONE;
51     napi_get_value_int32(env, value, &type);
52     ANS_LOGI("type is: %{public}d", type);
53     if (!AnsEnumUtil::DoNotDisturbTypeJSToC(DoNotDisturbType(type), outType)) {
54         Common::NapiThrow(env, ERROR_PARAM_INVALID, PARAMETER_VERIFICATION_FAILED);
55         return nullptr;
56     }
57     params.date.SetDoNotDisturbType(outType);
58 
59     // argv[0]: date:begin
60     NAPI_CALL(env, napi_has_named_property(env, argv, "begin", &hasProperty));
61     if (!hasProperty) {
62         ANS_LOGW("Wrong argument type. Property type expected.");
63         Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
64         return nullptr;
65     }
66     double begin = 0;
67     napi_get_named_property(env, argv, "begin", &value);
68     bool isDate = false;
69     napi_is_date(env, value, &isDate);
70     if (!isDate) {
71         ANS_LOGE("Wrong argument type. Date expected.");
72         std::string msg = "Incorrect parameter types.The type of param must be date.";
73         Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
74         return nullptr;
75     }
76     napi_get_date_value(env, value, &begin);
77     params.date.SetBeginDate(int64_t(begin));
78 
79     // argv[0]: date:end
80     NAPI_CALL(env, napi_has_named_property(env, argv, "end", &hasProperty));
81     if (!hasProperty) {
82         ANS_LOGW("Wrong argument type. Property type expected.");
83         Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
84         return nullptr;
85     }
86     double end = 0;
87     napi_get_named_property(env, argv, "end", &value);
88     isDate = false;
89     napi_is_date(env, value, &isDate);
90     if (!isDate) {
91         ANS_LOGE("Wrong argument type. Date expected.");
92         std::string msg = "Incorrect parameter types.The type of param must be date.";
93         Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
94         return nullptr;
95     }
96     napi_get_date_value(env, value, &end);
97     params.date.SetEndDate(int64_t(end));
98 
99     return Common::NapiGetNull(env);
100 }
101 
GetDoNotDisturbProfile(const napi_env & env,const napi_value & value,sptr<NotificationDoNotDisturbProfile> & profile)102 bool GetDoNotDisturbProfile(
103     const napi_env &env, const napi_value &value, sptr<NotificationDoNotDisturbProfile> &profile)
104 {
105     ANS_LOGD("Called.");
106     bool hasProperty = false;
107     NAPI_CALL_BASE(env, napi_has_named_property(env, value, "id", &hasProperty), false);
108     if (!hasProperty) {
109         ANS_LOGE("Wrong argument type. Property type expected.");
110         return false;
111     }
112     int profileId = 0;
113     napi_value obj = nullptr;
114     napi_get_named_property(env, value, "id", &obj);
115     napi_valuetype valuetype = napi_undefined;
116     NAPI_CALL_BASE(env, napi_typeof(env, obj, &valuetype), false);
117     if (valuetype != napi_number) {
118         ANS_LOGE("Wrong argument type. Number expected.");
119         return false;
120     }
121     napi_get_value_int32(env, obj, &profileId);
122     if (profileId < DO_NOT_DISTURB_PROFILE_MIN_ID || profileId > DO_NOT_DISTURB_PROFILE_MAX_ID) {
123         ANS_LOGE("The profile id is out of range.");
124         return false;
125     }
126     profile->SetProfileId(profileId);
127 
128     NAPI_CALL_BASE(env, napi_has_named_property(env, value, "name", &hasProperty), false);
129     if (!hasProperty) {
130         ANS_LOGE("Wrong argument type. Property type expected.");
131         return false;
132     }
133     char name[STR_MAX_SIZE] = {0};
134     napi_get_named_property(env, value, "name", &obj);
135     NAPI_CALL_BASE(env, napi_typeof(env, obj, &valuetype), false);
136     if (valuetype != napi_string) {
137         ANS_LOGE("Wrong argument type. String expected.");
138         return false;
139     }
140     size_t strLen = 0;
141     NAPI_CALL_BASE(env, napi_get_value_string_utf8(env, obj, name, STR_MAX_SIZE - 1, &strLen), false);
142     profile->SetProfileName(name);
143 
144     return AnalyseTrustlist(env, value, profile);
145 }
146 
AnalyseTrustlist(const napi_env & env,const napi_value & value,sptr<NotificationDoNotDisturbProfile> & profile)147 bool AnalyseTrustlist(const napi_env &env, const napi_value &value, sptr<NotificationDoNotDisturbProfile> &profile)
148 {
149     bool hasProperty = false;
150     NAPI_CALL_BASE(env, napi_has_named_property(env, value, "trustlist", &hasProperty), false);
151     if (!hasProperty) {
152         return true;
153     }
154     napi_value obj = nullptr;
155     napi_get_named_property(env, value, "trustlist", &obj);
156     bool isArray = false;
157     NAPI_CALL_BASE(env, napi_is_array(env, obj, &isArray), false);
158     if (!isArray) {
159         ANS_LOGE("Value is not an array.");
160         return false;
161     }
162     uint32_t length = 0;
163     napi_get_array_length(env, obj, &length);
164     if (length == 0) {
165         ANS_LOGD("The array is empty.");
166         return true;
167     }
168     std::vector<NotificationBundleOption> options;
169     for (size_t index = 0; index < length; index++) {
170         napi_value nOption = nullptr;
171         napi_get_element(env, obj, index, &nOption);
172         napi_valuetype valuetype = napi_undefined;
173         NAPI_CALL_BASE(env, napi_typeof(env, nOption, &valuetype), false);
174         if (valuetype != napi_object) {
175             ANS_LOGE("Wrong argument type. Object expected.");
176             return false;
177         }
178         NotificationBundleOption option;
179         if (!Common::GetBundleOption(env, nOption, option)) {
180             return false;
181         }
182         options.emplace_back(option);
183     }
184     profile->SetProfileTrustList(options);
185     return true;
186 }
187 
ParseParameters(const napi_env & env,const napi_callback_info & info,SetDoNotDisturbDateParams & params)188 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, SetDoNotDisturbDateParams &params)
189 {
190     ANS_LOGD("enter");
191 
192     size_t argc = SET_DISTURB_MAX_PARA;
193     napi_value argv[SET_DISTURB_MAX_PARA] = {nullptr};
194     napi_value thisVar = nullptr;
195     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
196     if (argc < SET_DISTURB_MIN_PARA) {
197         ANS_LOGW("Wrong argument type. Property type expected.");
198         Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
199         return nullptr;
200     }
201 
202     // argv[0]: date
203     napi_valuetype valuetype = napi_undefined;
204     NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
205     if (valuetype != napi_object) {
206         ANS_LOGW("Wrong argument type. Property type expected.");
207         Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
208         return nullptr;
209     }
210     if (GetDoNotDisturbDate(env, argv[PARAM0], params) == nullptr) {
211         Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
212         return nullptr;
213     }
214 
215     // argv[1] : userId / callback
216     if (argc >= SET_DISTURB_MAX_PARA - 1) {
217         NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
218         if ((valuetype != napi_number) && (valuetype != napi_function)) {
219             ANS_LOGW("Wrong argument type. Function or object expected. Excute promise.");
220             return Common::NapiGetNull(env);
221         }
222 
223         if (valuetype == napi_number) {
224             params.hasUserId = true;
225             NAPI_CALL(env, napi_get_value_int32(env, argv[PARAM1], &params.userId));
226         } else {
227             napi_create_reference(env, argv[PARAM1], 1, &params.callback);
228         }
229     }
230 
231     // argv[2]:callback
232     if (argc >= SET_DISTURB_MAX_PARA) {
233         NAPI_CALL(env, napi_typeof(env, argv[PARAM2], &valuetype));
234         if (valuetype != napi_function) {
235             ANS_LOGE("Callback is not function excute promise.");
236             return Common::NapiGetNull(env);
237         }
238         napi_create_reference(env, argv[PARAM2], 1, &params.callback);
239     }
240 
241     return Common::NapiGetNull(env);
242 }
243 
ParseProfilesParameters(const napi_env & env,const napi_callback_info & info,std::vector<sptr<NotificationDoNotDisturbProfile>> & profiles)244 bool ParseProfilesParameters(
245     const napi_env &env, const napi_callback_info &info, std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles)
246 {
247     ANS_LOGD("Called.");
248     size_t argc = DISTURB_PROFILES_PARA;
249     napi_value argv[DISTURB_PROFILES_PARA] = {nullptr};
250     napi_value thisVar = nullptr;
251     NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL), false);
252     if (argc != DISTURB_PROFILES_PARA) {
253         ANS_LOGE("Wrong number of arguments.");
254         Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
255         return false;
256     }
257     napi_valuetype valuetype = napi_undefined;
258     bool isArray = false;
259     napi_is_array(env, argv[PARAM0], &isArray);
260     if (!isArray) {
261         ANS_LOGE("Wrong argument type. Array expected.");
262         std::string msg = "Incorrect parameter types.The type of param must be array.";
263         Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
264         return false;
265     }
266     uint32_t length = 0;
267     napi_get_array_length(env, argv[PARAM0], &length);
268     if (length == 0) {
269         ANS_LOGD("The array is empty.");
270         std::string msg = "Mandatory parameters are left unspecified. The array is empty.";
271         Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
272         return false;
273     }
274     for (size_t index = 0; index < length; index++) {
275         napi_value nProfile = nullptr;
276         napi_get_element(env, argv[PARAM0], index, &nProfile);
277         NAPI_CALL_BASE(env, napi_typeof(env, nProfile, &valuetype), false);
278         if (valuetype != napi_object) {
279             ANS_LOGE("Wrong argument type. Object expected.");
280             std::string msg = "Incorrect parameter types.The type of param must be object.";
281             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
282             return false;
283         }
284         sptr<NotificationDoNotDisturbProfile> profile = new (std::nothrow) NotificationDoNotDisturbProfile();
285         if (profile == nullptr) {
286             ANS_LOGE("Failed to create NotificationDoNotDisturbProfile.");
287             return false;
288         }
289         if (!GetDoNotDisturbProfile(env, nProfile, profile)) {
290             return false;
291         }
292         profiles.emplace_back(profile);
293     }
294     return true;
295 }
296 
SetDoNotDisturbDate(napi_env env,napi_callback_info info)297 napi_value SetDoNotDisturbDate(napi_env env, napi_callback_info info)
298 {
299     ANS_LOGD("enter");
300 
301     SetDoNotDisturbDateParams params {};
302     if (ParseParameters(env, info, params) == nullptr) {
303         return Common::NapiGetUndefined(env);
304     }
305 
306     AsyncCallbackInfoSetDoNotDisturb *asynccallbackinfo =
307         new (std::nothrow) AsyncCallbackInfoSetDoNotDisturb {.env = env, .asyncWork = nullptr, .params = params};
308     if (!asynccallbackinfo) {
309         ANS_LOGD("Create asynccallbackinfo is failed.");
310         return Common::JSParaError(env, params.callback);
311     }
312     napi_value promise = nullptr;
313     Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise);
314 
315     napi_value resourceName = nullptr;
316     napi_create_string_latin1(env, "setDoNotDisturbDate", NAPI_AUTO_LENGTH, &resourceName);
317     // Asynchronous function call
318     napi_create_async_work(env,
319         nullptr, resourceName, [](napi_env env, void *data) {
320             ANS_LOGD("SetDoNotDisturbDate work excute.");
321             AsyncCallbackInfoSetDoNotDisturb *asynccallbackinfo = static_cast<AsyncCallbackInfoSetDoNotDisturb *>(data);
322             if (asynccallbackinfo) {
323                 if (asynccallbackinfo->params.hasUserId) {
324                     asynccallbackinfo->info.errorCode = NotificationHelper::SetDoNotDisturbDate(
325                         asynccallbackinfo->params.userId, asynccallbackinfo->params.date);
326                 } else {
327                     asynccallbackinfo->info.errorCode = NotificationHelper::SetDoNotDisturbDate(
328                         asynccallbackinfo->params.date);
329                 }
330 
331                 ANS_LOGI("SetDoNotDisturbDate date=%{public}s errorCode=%{public}d, hasUserId=%{public}d",
332                     asynccallbackinfo->params.date.Dump().c_str(), asynccallbackinfo->info.errorCode,
333                     asynccallbackinfo->params.hasUserId);
334             }
335         },
336         [](napi_env env, napi_status status, void *data) {
337             ANS_LOGD("SetDoNotDisturbDate work complete.");
338             AsyncCallbackInfoSetDoNotDisturb *asynccallbackinfo = static_cast<AsyncCallbackInfoSetDoNotDisturb *>(data);
339             if (asynccallbackinfo) {
340                 Common::ReturnCallbackPromise(env, asynccallbackinfo->info, Common::NapiGetNull(env));
341                 if (asynccallbackinfo->info.callback != nullptr) {
342                     ANS_LOGD("Delete SetDoNotDisturbDate callback reference.");
343                     napi_delete_reference(env, asynccallbackinfo->info.callback);
344                 }
345                 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
346                 delete asynccallbackinfo;
347                 asynccallbackinfo = nullptr;
348             }
349         },
350         (void *)asynccallbackinfo, &asynccallbackinfo->asyncWork);
351 
352     napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated);
353 
354     if (asynccallbackinfo->info.isCallback) {
355         ANS_LOGD("SetDoNotDisturbDate callback is nullptr.");
356         return Common::NapiGetNull(env);
357     } else {
358         return promise;
359     }
360 }
361 
AsyncCompleteCallbackGetDoNotDisturbDate(napi_env env,napi_status status,void * data)362 void AsyncCompleteCallbackGetDoNotDisturbDate(napi_env env, napi_status status, void *data)
363 {
364     ANS_LOGD("enter");
365     if (!data) {
366         ANS_LOGE("Invalid async callback data");
367         return;
368     }
369     AsyncCallbackInfoGetDoNotDisturb *asynccallbackinfo = static_cast<AsyncCallbackInfoGetDoNotDisturb *>(data);
370     if (asynccallbackinfo) {
371         ANS_LOGD("asynccallbackinfo is not nullptr.");
372         napi_value result = Common::NapiGetNull(env);
373         if (asynccallbackinfo->info.errorCode == ERR_OK) {
374             napi_create_object(env, &result);
375             if (!Common::SetDoNotDisturbDate(env, asynccallbackinfo->date, result)) {
376                 asynccallbackinfo->info.errorCode = ERROR;
377             }
378         }
379         Common::ReturnCallbackPromise(env, asynccallbackinfo->info, result);
380         if (asynccallbackinfo->info.callback != nullptr) {
381             ANS_LOGD("Delete GetDoNotDisturbDate callback reference.");
382             napi_delete_reference(env, asynccallbackinfo->info.callback);
383         }
384         napi_delete_async_work(env, asynccallbackinfo->asyncWork);
385         delete asynccallbackinfo;
386         asynccallbackinfo = nullptr;
387     }
388 }
389 
ParseParameters(const napi_env & env,const napi_callback_info & info,GetDoNotDisturbDateParams & params)390 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, GetDoNotDisturbDateParams &params)
391 {
392     ANS_LOGD("enter");
393 
394     size_t argc = GET_DISTURB_MAX_PARA;
395     napi_value argv[GET_DISTURB_MAX_PARA] = {nullptr};
396     napi_value thisVar = nullptr;
397     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
398 
399     napi_valuetype valuetype = napi_undefined;
400     // argv[0]: userId / callback
401     if (argc >= GET_DISTURB_MAX_PARA - 1) {
402         NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
403         if ((valuetype != napi_number) && (valuetype != napi_function)) {
404             ANS_LOGW("Wrong argument type. Function or object expected. Excute promise.");
405             return Common::NapiGetNull(env);
406         }
407         if (valuetype == napi_number) {
408             params.hasUserId = true;
409             NAPI_CALL(env, napi_get_value_int32(env, argv[PARAM0], &params.userId));
410         } else {
411             napi_create_reference(env, argv[PARAM0], 1, &params.callback);
412         }
413     }
414 
415     // argv[1]:callback
416     if (argc >= GET_DISTURB_MAX_PARA) {
417         NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
418         if (valuetype != napi_function) {
419             ANS_LOGE("Callback is not function excute promise.");
420             return Common::NapiGetNull(env);
421         }
422         napi_create_reference(env, argv[PARAM1], 1, &params.callback);
423     }
424 
425     return Common::NapiGetNull(env);
426 }
427 
GetDoNotDisturbDate(napi_env env,napi_callback_info info)428 napi_value GetDoNotDisturbDate(napi_env env, napi_callback_info info)
429 {
430     ANS_LOGD("enter");
431 
432     GetDoNotDisturbDateParams params {};
433     if (ParseParameters(env, info, params) == nullptr) {
434         return Common::NapiGetUndefined(env);
435     }
436 
437     AsyncCallbackInfoGetDoNotDisturb *asynccallbackinfo =
438         new (std::nothrow) AsyncCallbackInfoGetDoNotDisturb {.env = env, .asyncWork = nullptr, .params = params};
439     if (!asynccallbackinfo) {
440         ANS_LOGD("Create asynccallbackinfo is failed.");
441         return Common::JSParaError(env, params.callback);
442     }
443     napi_value promise = nullptr;
444     Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise);
445 
446     ANS_LOGD("Create getDoNotDisturbDate string.");
447     napi_value resourceName = nullptr;
448     napi_create_string_latin1(env, "getDoNotDisturbDate", NAPI_AUTO_LENGTH, &resourceName);
449     // Asynchronous function call
450     napi_create_async_work(env,
451         nullptr,
452         resourceName,
453         [](napi_env env, void *data) {
454             ANS_LOGD("GetDoNotDisturbDate work excute.");
455             AsyncCallbackInfoGetDoNotDisturb *asynccallbackinfo =
456                 static_cast<AsyncCallbackInfoGetDoNotDisturb *>(data);
457             if (asynccallbackinfo) {
458                 if (asynccallbackinfo->params.hasUserId) {
459                     asynccallbackinfo->info.errorCode = NotificationHelper::GetDoNotDisturbDate(
460                         asynccallbackinfo->params.userId, asynccallbackinfo->date);
461                 } else {
462                     asynccallbackinfo->info.errorCode = NotificationHelper::GetDoNotDisturbDate(
463                         asynccallbackinfo->date);
464                 }
465 
466                 ANS_LOGI("GetDoNotDisturbDate errorCode=%{public}d date=%{public}s, hasUserId=%{public}d",
467                     asynccallbackinfo->info.errorCode, asynccallbackinfo->date.Dump().c_str(),
468                     asynccallbackinfo->params.hasUserId);
469             }
470         },
471         AsyncCompleteCallbackGetDoNotDisturbDate,
472         (void *)asynccallbackinfo,
473         &asynccallbackinfo->asyncWork);
474 
475     napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated);
476 
477     if (asynccallbackinfo->info.isCallback) {
478         ANS_LOGD("getDoNotDisturbDate callback is nullptr.");
479         return Common::NapiGetNull(env);
480     } else {
481         return promise;
482     }
483 }
484 
SupportDoNotDisturbMode(napi_env env,napi_callback_info info)485 napi_value SupportDoNotDisturbMode(napi_env env, napi_callback_info info)
486 {
487     ANS_LOGD("enter");
488 
489     napi_ref callback = nullptr;
490     if (Common::ParseParaOnlyCallback(env, info, callback) == nullptr) {
491         return Common::NapiGetUndefined(env);
492     }
493 
494     AsyncCallbackInfoSupportDoNotDisturb *asynccallbackinfo =
495         new (std::nothrow) AsyncCallbackInfoSupportDoNotDisturb {
496         .env = env, .asyncWork = nullptr, .callback = callback};
497 
498     if (!asynccallbackinfo) {
499         ANS_LOGD("Create asynccallbackinfo is failed.");
500         return Common::JSParaError(env, callback);
501     }
502     napi_value promise = nullptr;
503     Common::PaddingCallbackPromiseInfo(env, callback, asynccallbackinfo->info, promise);
504 
505     ANS_LOGD("Create supportDoNotDisturbMode string.");
506     napi_value resourceName = nullptr;
507     napi_create_string_latin1(env, "supportDoNotDisturbMode", NAPI_AUTO_LENGTH, &resourceName);
508     // Asynchronous function call
509     napi_create_async_work(env,
510         nullptr,
511         resourceName,
512         [](napi_env env, void *data) {
513             ANS_LOGD("SupportDoNotDisturbMode work excute.");
514             AsyncCallbackInfoSupportDoNotDisturb *asynccallbackinfo =
515                 static_cast<AsyncCallbackInfoSupportDoNotDisturb *>(data);
516             if (asynccallbackinfo) {
517                 asynccallbackinfo->info.errorCode =
518                     NotificationHelper::DoesSupportDoNotDisturbMode(asynccallbackinfo->isSupported);
519                 ANS_LOGI("errorCode:%{public}d isSupported:%{public}d",
520                     asynccallbackinfo->info.errorCode, asynccallbackinfo->isSupported);
521             }
522         },
523         [](napi_env env, napi_status status, void *data) {
524             ANS_LOGD("SupportDoNotDisturbMode work complete.");
525             AsyncCallbackInfoSupportDoNotDisturb *asynccallbackinfo =
526                 static_cast<AsyncCallbackInfoSupportDoNotDisturb *>(data);
527             if (asynccallbackinfo) {
528                 napi_value result = nullptr;
529                 napi_get_boolean(env, asynccallbackinfo->isSupported, &result);
530                 Common::ReturnCallbackPromise(env, asynccallbackinfo->info, result);
531                 if (asynccallbackinfo->info.callback != nullptr) {
532                     ANS_LOGD("Delete supportDoNotDisturbMode callback reference.");
533                     napi_delete_reference(env, asynccallbackinfo->info.callback);
534                 }
535                 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
536                 delete asynccallbackinfo;
537                 asynccallbackinfo = nullptr;
538             }
539             ANS_LOGD("SupportDoNotDisturbMode work complete end.");
540         },
541         (void *)asynccallbackinfo,
542         &asynccallbackinfo->asyncWork);
543 
544     napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated);
545 
546     if (asynccallbackinfo->info.isCallback) {
547         ANS_LOGD("supportDoNotDisturbMode callback is nullptr.");
548         return Common::NapiGetNull(env);
549     } else {
550         return promise;
551     }
552 }
553 
ParseParameters(const napi_env & env,const napi_callback_info & info,GetDoNotDisturbProfileParams & params)554 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, GetDoNotDisturbProfileParams &params)
555 {
556     ANS_LOGD("ParseParameters");
557 
558     size_t argc = DISTURB_PROFILES_PARA;
559     napi_value argv[DISTURB_PROFILES_PARA] = {nullptr};
560     napi_value thisVar = nullptr;
561     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
562 
563     // argv[0]: profileId
564     napi_valuetype valuetype = napi_undefined;
565     if (argc >= DISTURB_PROFILES_PARA) {
566         NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
567         if (valuetype != napi_number) {
568             ANS_LOGW("Wrong argument type Excute promise.");
569             return Common::NapiGetNull(env);
570         }
571         NAPI_CALL(env, napi_get_value_int32(env, argv[PARAM0], &params.profileId));
572     }
573 
574     return Common::NapiGetNull(env);
575 }
576 }  // namespace NotificationNapi
577 }  // namespace OHOS
578