• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_LOGE("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_LOGE("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_LOGE("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_LOGE("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     int64_t 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_int64(env, obj, &profileId);
122     profile->SetProfileId(profileId);
123 
124     NAPI_CALL_BASE(env, napi_has_named_property(env, value, "name", &hasProperty), false);
125     if (!hasProperty) {
126         ANS_LOGE("Wrong argument type. Property type expected.");
127         return false;
128     }
129     char name[STR_MAX_SIZE] = {0};
130     napi_get_named_property(env, value, "name", &obj);
131     NAPI_CALL_BASE(env, napi_typeof(env, obj, &valuetype), false);
132     if (valuetype != napi_string) {
133         ANS_LOGE("Wrong argument type. String expected.");
134         return false;
135     }
136     size_t strLen = 0;
137     NAPI_CALL_BASE(env, napi_get_value_string_utf8(env, obj, name, STR_MAX_SIZE - 1, &strLen), false);
138     profile->SetProfileName(name);
139 
140     return AnalyseTrustlist(env, value, profile);
141 }
142 
AnalyseTrustlist(const napi_env & env,const napi_value & value,sptr<NotificationDoNotDisturbProfile> & profile)143 bool AnalyseTrustlist(const napi_env &env, const napi_value &value, sptr<NotificationDoNotDisturbProfile> &profile)
144 {
145     bool hasProperty = false;
146     NAPI_CALL_BASE(env, napi_has_named_property(env, value, "trustlist", &hasProperty), false);
147     if (!hasProperty) {
148         return true;
149     }
150     napi_value obj = nullptr;
151     napi_get_named_property(env, value, "trustlist", &obj);
152     bool isArray = false;
153     NAPI_CALL_BASE(env, napi_is_array(env, obj, &isArray), false);
154     if (!isArray) {
155         ANS_LOGE("Value is not an array.");
156         return false;
157     }
158     uint32_t length = 0;
159     napi_get_array_length(env, obj, &length);
160     if (length == 0) {
161         ANS_LOGD("The array is empty.");
162         return true;
163     }
164     std::vector<NotificationBundleOption> options;
165     for (size_t index = 0; index < length; index++) {
166         napi_value nOption = nullptr;
167         napi_get_element(env, obj, index, &nOption);
168         napi_valuetype valuetype = napi_undefined;
169         NAPI_CALL_BASE(env, napi_typeof(env, nOption, &valuetype), false);
170         if (valuetype != napi_object) {
171             ANS_LOGE("Wrong argument type. Object expected.");
172             return false;
173         }
174         NotificationBundleOption option;
175         if (!Common::GetBundleOption(env, nOption, option)) {
176             return false;
177         }
178         options.emplace_back(option);
179     }
180     profile->SetProfileTrustList(options);
181     return true;
182 }
183 
ParseParameters(const napi_env & env,const napi_callback_info & info,SetDoNotDisturbDateParams & params)184 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, SetDoNotDisturbDateParams &params)
185 {
186     ANS_LOGD("enter");
187 
188     size_t argc = SET_DISTURB_MAX_PARA;
189     napi_value argv[SET_DISTURB_MAX_PARA] = {nullptr};
190     napi_value thisVar = nullptr;
191     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
192     if (argc < SET_DISTURB_MIN_PARA) {
193         ANS_LOGE("Wrong argument type. Property type expected.");
194         Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
195         return nullptr;
196     }
197 
198     // argv[0]: date
199     napi_valuetype valuetype = napi_undefined;
200     NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
201     if (valuetype != napi_object) {
202         ANS_LOGE("Wrong argument type. Property type expected.");
203         Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
204         return nullptr;
205     }
206     if (GetDoNotDisturbDate(env, argv[PARAM0], params) == nullptr) {
207         Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
208         return nullptr;
209     }
210 
211     // argv[1] : userId / callback
212     if (argc >= SET_DISTURB_MAX_PARA - 1) {
213         NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
214         if ((valuetype != napi_number) && (valuetype != napi_function)) {
215             ANS_LOGE("Wrong argument type. Function or object expected. Excute promise.");
216             return Common::NapiGetNull(env);
217         }
218 
219         if (valuetype == napi_number) {
220             params.hasUserId = true;
221             NAPI_CALL(env, napi_get_value_int32(env, argv[PARAM1], &params.userId));
222         } else {
223             napi_create_reference(env, argv[PARAM1], 1, &params.callback);
224         }
225     }
226 
227     // argv[2]:callback
228     if (argc >= SET_DISTURB_MAX_PARA) {
229         NAPI_CALL(env, napi_typeof(env, argv[PARAM2], &valuetype));
230         if (valuetype != napi_function) {
231             ANS_LOGE("Callback is not function excute promise.");
232             return Common::NapiGetNull(env);
233         }
234         napi_create_reference(env, argv[PARAM2], 1, &params.callback);
235     }
236 
237     return Common::NapiGetNull(env);
238 }
239 
ParseProfilesParameters(const napi_env & env,const napi_callback_info & info,std::vector<sptr<NotificationDoNotDisturbProfile>> & profiles)240 bool ParseProfilesParameters(
241     const napi_env &env, const napi_callback_info &info, std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles)
242 {
243     ANS_LOGD("Called.");
244     size_t argc = DISTURB_PROFILES_PARA;
245     napi_value argv[DISTURB_PROFILES_PARA] = {nullptr};
246     napi_value thisVar = nullptr;
247     NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL), false);
248     if (argc != DISTURB_PROFILES_PARA) {
249         ANS_LOGE("Wrong number of arguments.");
250         Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
251         return false;
252     }
253     napi_valuetype valuetype = napi_undefined;
254     bool isArray = false;
255     napi_is_array(env, argv[PARAM0], &isArray);
256     if (!isArray) {
257         ANS_LOGE("Wrong argument type. Array expected.");
258         std::string msg = "Incorrect parameter types.The type of param must be array.";
259         Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
260         return false;
261     }
262     uint32_t length = 0;
263     napi_get_array_length(env, argv[PARAM0], &length);
264     if (length == 0) {
265         ANS_LOGD("The array is empty.");
266         std::string msg = "Mandatory parameters are left unspecified. The array is empty.";
267         Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
268         return false;
269     }
270     for (size_t index = 0; index < length; index++) {
271         napi_value nProfile = nullptr;
272         napi_get_element(env, argv[PARAM0], index, &nProfile);
273         NAPI_CALL_BASE(env, napi_typeof(env, nProfile, &valuetype), false);
274         if (valuetype != napi_object) {
275             ANS_LOGE("Wrong argument type. Object expected.");
276             std::string msg = "Incorrect parameter types.The type of param must be object.";
277             Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
278             return false;
279         }
280         sptr<NotificationDoNotDisturbProfile> profile = new (std::nothrow) NotificationDoNotDisturbProfile();
281         if (profile == nullptr) {
282             ANS_LOGE("Failed to create NotificationDoNotDisturbProfile.");
283             return false;
284         }
285         if (!GetDoNotDisturbProfile(env, nProfile, profile)) {
286             return false;
287         }
288         profiles.emplace_back(profile);
289     }
290     return true;
291 }
292 
SetDoNotDisturbDate(napi_env env,napi_callback_info info)293 napi_value SetDoNotDisturbDate(napi_env env, napi_callback_info info)
294 {
295     ANS_LOGD("enter");
296 
297     SetDoNotDisturbDateParams params {};
298     if (ParseParameters(env, info, params) == nullptr) {
299         return Common::NapiGetUndefined(env);
300     }
301 
302     AsyncCallbackInfoSetDoNotDisturb *asynccallbackinfo =
303         new (std::nothrow) AsyncCallbackInfoSetDoNotDisturb {.env = env, .asyncWork = nullptr, .params = params};
304     if (!asynccallbackinfo) {
305         ANS_LOGD("Create asynccallbackinfo is failed.");
306         return Common::JSParaError(env, params.callback);
307     }
308     napi_value promise = nullptr;
309     Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise);
310 
311     napi_value resourceName = nullptr;
312     napi_create_string_latin1(env, "setDoNotDisturbDate", NAPI_AUTO_LENGTH, &resourceName);
313     // Asynchronous function call
314     napi_create_async_work(env,
315         nullptr, resourceName, [](napi_env env, void *data) {
316             ANS_LOGD("SetDoNotDisturbDate work excute.");
317             AsyncCallbackInfoSetDoNotDisturb *asynccallbackinfo = static_cast<AsyncCallbackInfoSetDoNotDisturb *>(data);
318             if (asynccallbackinfo) {
319                 if (asynccallbackinfo->params.hasUserId) {
320                     asynccallbackinfo->info.errorCode = NotificationHelper::SetDoNotDisturbDate(
321                         asynccallbackinfo->params.userId, asynccallbackinfo->params.date);
322                 } else {
323                     asynccallbackinfo->info.errorCode = NotificationHelper::SetDoNotDisturbDate(
324                         asynccallbackinfo->params.date);
325                 }
326 
327                 ANS_LOGI("SetDoNotDisturbDate date=%{public}s errorCode=%{public}d, hasUserId=%{public}d",
328                     asynccallbackinfo->params.date.Dump().c_str(), asynccallbackinfo->info.errorCode,
329                     asynccallbackinfo->params.hasUserId);
330             }
331         },
332         [](napi_env env, napi_status status, void *data) {
333             ANS_LOGD("SetDoNotDisturbDate work complete.");
334             AsyncCallbackInfoSetDoNotDisturb *asynccallbackinfo = static_cast<AsyncCallbackInfoSetDoNotDisturb *>(data);
335             if (asynccallbackinfo) {
336                 Common::ReturnCallbackPromise(env, asynccallbackinfo->info, Common::NapiGetNull(env));
337                 if (asynccallbackinfo->info.callback != nullptr) {
338                     ANS_LOGD("Delete SetDoNotDisturbDate callback reference.");
339                     napi_delete_reference(env, asynccallbackinfo->info.callback);
340                 }
341                 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
342                 delete asynccallbackinfo;
343                 asynccallbackinfo = nullptr;
344             }
345         },
346         (void *)asynccallbackinfo, &asynccallbackinfo->asyncWork);
347 
348     napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated);
349 
350     if (asynccallbackinfo->info.isCallback) {
351         ANS_LOGD("SetDoNotDisturbDate callback is nullptr.");
352         return Common::NapiGetNull(env);
353     } else {
354         return promise;
355     }
356 }
357 
AsyncCompleteCallbackGetDoNotDisturbDate(napi_env env,napi_status status,void * data)358 void AsyncCompleteCallbackGetDoNotDisturbDate(napi_env env, napi_status status, void *data)
359 {
360     ANS_LOGD("enter");
361     if (!data) {
362         ANS_LOGE("Invalid async callback data");
363         return;
364     }
365     AsyncCallbackInfoGetDoNotDisturb *asynccallbackinfo = static_cast<AsyncCallbackInfoGetDoNotDisturb *>(data);
366     if (asynccallbackinfo) {
367         ANS_LOGD("asynccallbackinfo is not nullptr.");
368         napi_value result = Common::NapiGetNull(env);
369         if (asynccallbackinfo->info.errorCode == ERR_OK) {
370             napi_create_object(env, &result);
371             if (!Common::SetDoNotDisturbDate(env, asynccallbackinfo->date, result)) {
372                 asynccallbackinfo->info.errorCode = ERROR;
373             }
374         }
375         Common::ReturnCallbackPromise(env, asynccallbackinfo->info, result);
376         if (asynccallbackinfo->info.callback != nullptr) {
377             ANS_LOGD("Delete GetDoNotDisturbDate callback reference.");
378             napi_delete_reference(env, asynccallbackinfo->info.callback);
379         }
380         napi_delete_async_work(env, asynccallbackinfo->asyncWork);
381         delete asynccallbackinfo;
382         asynccallbackinfo = nullptr;
383     }
384 }
385 
ParseParameters(const napi_env & env,const napi_callback_info & info,GetDoNotDisturbDateParams & params)386 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, GetDoNotDisturbDateParams &params)
387 {
388     ANS_LOGD("enter");
389 
390     size_t argc = GET_DISTURB_MAX_PARA;
391     napi_value argv[GET_DISTURB_MAX_PARA] = {nullptr};
392     napi_value thisVar = nullptr;
393     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
394 
395     napi_valuetype valuetype = napi_undefined;
396     // argv[0]: userId / callback
397     if (argc >= GET_DISTURB_MAX_PARA - 1) {
398         NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
399         if ((valuetype != napi_number) && (valuetype != napi_function)) {
400             ANS_LOGW("Wrong argument type. Function or object expected. Excute promise.");
401             return Common::NapiGetNull(env);
402         }
403         if (valuetype == napi_number) {
404             params.hasUserId = true;
405             NAPI_CALL(env, napi_get_value_int32(env, argv[PARAM0], &params.userId));
406         } else {
407             napi_create_reference(env, argv[PARAM0], 1, &params.callback);
408         }
409     }
410 
411     // argv[1]:callback
412     if (argc >= GET_DISTURB_MAX_PARA) {
413         NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
414         if (valuetype != napi_function) {
415             ANS_LOGE("Callback is not function excute promise.");
416             return Common::NapiGetNull(env);
417         }
418         napi_create_reference(env, argv[PARAM1], 1, &params.callback);
419     }
420 
421     return Common::NapiGetNull(env);
422 }
423 
GetDoNotDisturbDate(napi_env env,napi_callback_info info)424 napi_value GetDoNotDisturbDate(napi_env env, napi_callback_info info)
425 {
426     ANS_LOGD("enter");
427 
428     GetDoNotDisturbDateParams params {};
429     if (ParseParameters(env, info, params) == nullptr) {
430         return Common::NapiGetUndefined(env);
431     }
432 
433     AsyncCallbackInfoGetDoNotDisturb *asynccallbackinfo =
434         new (std::nothrow) AsyncCallbackInfoGetDoNotDisturb {.env = env, .asyncWork = nullptr, .params = params};
435     if (!asynccallbackinfo) {
436         ANS_LOGD("Create asynccallbackinfo is failed.");
437         return Common::JSParaError(env, params.callback);
438     }
439     napi_value promise = nullptr;
440     Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise);
441 
442     ANS_LOGD("Create getDoNotDisturbDate string.");
443     napi_value resourceName = nullptr;
444     napi_create_string_latin1(env, "getDoNotDisturbDate", NAPI_AUTO_LENGTH, &resourceName);
445     // Asynchronous function call
446     napi_create_async_work(env,
447         nullptr,
448         resourceName,
449         [](napi_env env, void *data) {
450             ANS_LOGD("GetDoNotDisturbDate work excute.");
451             AsyncCallbackInfoGetDoNotDisturb *asynccallbackinfo =
452                 static_cast<AsyncCallbackInfoGetDoNotDisturb *>(data);
453             if (asynccallbackinfo) {
454                 if (asynccallbackinfo->params.hasUserId) {
455                     asynccallbackinfo->info.errorCode = NotificationHelper::GetDoNotDisturbDate(
456                         asynccallbackinfo->params.userId, asynccallbackinfo->date);
457                 } else {
458                     asynccallbackinfo->info.errorCode = NotificationHelper::GetDoNotDisturbDate(
459                         asynccallbackinfo->date);
460                 }
461 
462                 ANS_LOGI("GetDoNotDisturbDate errorCode=%{public}d date=%{public}s, hasUserId=%{public}d",
463                     asynccallbackinfo->info.errorCode, asynccallbackinfo->date.Dump().c_str(),
464                     asynccallbackinfo->params.hasUserId);
465             }
466         },
467         AsyncCompleteCallbackGetDoNotDisturbDate,
468         (void *)asynccallbackinfo,
469         &asynccallbackinfo->asyncWork);
470 
471     napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated);
472 
473     if (asynccallbackinfo->info.isCallback) {
474         ANS_LOGD("getDoNotDisturbDate callback is nullptr.");
475         return Common::NapiGetNull(env);
476     } else {
477         return promise;
478     }
479 }
480 
SupportDoNotDisturbMode(napi_env env,napi_callback_info info)481 napi_value SupportDoNotDisturbMode(napi_env env, napi_callback_info info)
482 {
483     ANS_LOGD("enter");
484 
485     napi_ref callback = nullptr;
486     if (Common::ParseParaOnlyCallback(env, info, callback) == nullptr) {
487         return Common::NapiGetUndefined(env);
488     }
489 
490     AsyncCallbackInfoSupportDoNotDisturb *asynccallbackinfo =
491         new (std::nothrow) AsyncCallbackInfoSupportDoNotDisturb {
492         .env = env, .asyncWork = nullptr, .callback = callback};
493 
494     if (!asynccallbackinfo) {
495         ANS_LOGD("Create asynccallbackinfo is failed.");
496         return Common::JSParaError(env, callback);
497     }
498     napi_value promise = nullptr;
499     Common::PaddingCallbackPromiseInfo(env, callback, asynccallbackinfo->info, promise);
500 
501     ANS_LOGD("Create supportDoNotDisturbMode string.");
502     napi_value resourceName = nullptr;
503     napi_create_string_latin1(env, "supportDoNotDisturbMode", NAPI_AUTO_LENGTH, &resourceName);
504     // Asynchronous function call
505     napi_create_async_work(env,
506         nullptr,
507         resourceName,
508         [](napi_env env, void *data) {
509             ANS_LOGD("SupportDoNotDisturbMode work excute.");
510             AsyncCallbackInfoSupportDoNotDisturb *asynccallbackinfo =
511                 static_cast<AsyncCallbackInfoSupportDoNotDisturb *>(data);
512             if (asynccallbackinfo) {
513                 asynccallbackinfo->info.errorCode =
514                     NotificationHelper::DoesSupportDoNotDisturbMode(asynccallbackinfo->isSupported);
515                 ANS_LOGI("errorCode:%{public}d isSupported:%{public}d",
516                     asynccallbackinfo->info.errorCode, asynccallbackinfo->isSupported);
517             }
518         },
519         [](napi_env env, napi_status status, void *data) {
520             ANS_LOGD("SupportDoNotDisturbMode work complete.");
521             AsyncCallbackInfoSupportDoNotDisturb *asynccallbackinfo =
522                 static_cast<AsyncCallbackInfoSupportDoNotDisturb *>(data);
523             if (asynccallbackinfo) {
524                 napi_value result = nullptr;
525                 napi_get_boolean(env, asynccallbackinfo->isSupported, &result);
526                 Common::ReturnCallbackPromise(env, asynccallbackinfo->info, result);
527                 if (asynccallbackinfo->info.callback != nullptr) {
528                     ANS_LOGD("Delete supportDoNotDisturbMode callback reference.");
529                     napi_delete_reference(env, asynccallbackinfo->info.callback);
530                 }
531                 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
532                 delete asynccallbackinfo;
533                 asynccallbackinfo = nullptr;
534             }
535             ANS_LOGD("SupportDoNotDisturbMode work complete end.");
536         },
537         (void *)asynccallbackinfo,
538         &asynccallbackinfo->asyncWork);
539 
540     napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated);
541 
542     if (asynccallbackinfo->info.isCallback) {
543         ANS_LOGD("supportDoNotDisturbMode callback is nullptr.");
544         return Common::NapiGetNull(env);
545     } else {
546         return promise;
547     }
548 }
549 
ParseParameters(const napi_env & env,const napi_callback_info & info,GetDoNotDisturbProfileParams & params)550 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, GetDoNotDisturbProfileParams &params)
551 {
552     ANS_LOGD("ParseParameters");
553 
554     size_t argc = DISTURB_PROFILES_PARA;
555     napi_value argv[DISTURB_PROFILES_PARA] = {nullptr};
556     napi_value thisVar = nullptr;
557     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
558 
559     // argv[0]: profileId
560     napi_valuetype valuetype = napi_undefined;
561     if (argc >= DISTURB_PROFILES_PARA) {
562         NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
563         if (valuetype != napi_number) {
564             ANS_LOGW("Wrong argument type Excute promise.");
565             return Common::NapiGetNull(env);
566         }
567         NAPI_CALL(env, napi_get_value_int64(env, argv[PARAM0], &params.profileId));
568     }
569 
570     return Common::NapiGetNull(env);
571 }
572 }  // namespace NotificationNapi
573 }  // namespace OHOS
574