1 /*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "napi_distributed_enable.h"
17
18 #include "ans_inner_errors.h"
19 #include "js_native_api.h"
20 #include "js_native_api_types.h"
21
22 namespace OHOS {
23 namespace NotificationNapi {
24 const int SET_DISTRIBUTED_ENABLE_MAX_PARA = 3;
25 const int SET_DISTRIBUTED_ENABLE_MIN_PARA = 2;
26 const int SET_SMART_REMINDER_ENABLE_MAX_PARA = 2;
27 const int SET_SMART_REMINDER_ENABLE_MIN_PARA = 1;
28 const int SET_DISTRIBUTED_ENABLE_BY_SLOT_PARA = 3;
29 const int GET_DISTRIBUTED_ENABLE_BY_SLOT_PARA = 2;
30 const int SET_DISTRIBUTED_BUNDLE_OPTION_NUM = 2;
31
ParseParameters(const napi_env & env,const napi_callback_info & info,DistributedEnableParams & params)32 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, DistributedEnableParams ¶ms)
33 {
34 ANS_LOGD("called");
35
36 size_t argc = SET_DISTRIBUTED_ENABLE_MAX_PARA;
37 napi_value argv[SET_DISTRIBUTED_ENABLE_MAX_PARA] = {nullptr};
38 napi_value thisVar = nullptr;
39 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
40 if (argc < SET_DISTRIBUTED_ENABLE_MIN_PARA) {
41 ANS_LOGE("Wrong number of arguments.");
42 Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
43 return nullptr;
44 }
45
46 // argv[0]: bundleOption
47 napi_valuetype valuetype = napi_undefined;
48 NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
49 if (valuetype != napi_object) {
50 ANS_LOGE("Parameter type error. Object expected.");
51 std::string msg = "Incorrect parameter types.The type of param must be object.";
52 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
53 return nullptr;
54 }
55 auto retValue = Common::GetBundleOption(env, argv[PARAM0], params.option);
56 if (retValue == nullptr) {
57 ANS_LOGE("null retValue");
58 Common::NapiThrow(env, ERROR_PARAM_INVALID, PARAMETER_VERIFICATION_FAILED);
59 return nullptr;
60 }
61
62 // argv[1]: deviceType
63 NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
64 if (valuetype != napi_string) {
65 ANS_LOGE("Wrong argument type. Bool expected.");
66 std::string msg = "Incorrect parameter types.The type of param must be boolean.";
67 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
68 return nullptr;
69 }
70 char str[STR_MAX_SIZE] = {0};
71 size_t strLen = 0;
72 napi_get_value_string_utf8(env, argv[PARAM1], str, STR_MAX_SIZE - 1, &strLen);
73 if (std::strlen(str) == 0) {
74 ANS_LOGE("Property deviceType is empty");
75 Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
76 return nullptr;
77 }
78 params.deviceType = str;
79
80 if (argc > SET_DISTRIBUTED_ENABLE_MIN_PARA) {
81 // argv[2]: enable
82 NAPI_CALL(env, napi_typeof(env, argv[PARAM2], &valuetype));
83 if (valuetype != napi_boolean) {
84 ANS_LOGE("Wrong argument type. Bool expected.");
85 std::string msg = "Incorrect parameter types.The type of param must be boolean.";
86 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
87 return nullptr;
88 }
89 napi_get_value_bool(env, argv[PARAM2], ¶ms.enable);
90 }
91
92 return Common::NapiGetNull(env);
93 }
94
AsyncCompleteCallbackNapiSetDistributedEnabledByBundle(napi_env env,napi_status status,void * data)95 void AsyncCompleteCallbackNapiSetDistributedEnabledByBundle(napi_env env, napi_status status, void *data)
96 {
97 ANS_LOGD("called");
98 if (!data) {
99 ANS_LOGE("Invalid async callback data");
100 return;
101 }
102 AsyncCallbackDistributedEnable *asynccallbackinfo = static_cast<AsyncCallbackDistributedEnable *>(data);
103 if (asynccallbackinfo) {
104 Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env));
105 if (asynccallbackinfo->info.callback != nullptr) {
106 ANS_LOGD("Delete NapiSetDistributedEnableByBundle callback reference.");
107 napi_delete_reference(env, asynccallbackinfo->info.callback);
108 }
109 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
110 delete asynccallbackinfo;
111 asynccallbackinfo = nullptr;
112 }
113 }
114
NapiSetDistributedEnabledByBundle(napi_env env,napi_callback_info info)115 napi_value NapiSetDistributedEnabledByBundle(napi_env env, napi_callback_info info)
116 {
117 ANS_LOGD("called");
118 DistributedEnableParams params {};
119 if (ParseParameters(env, info, params) == nullptr) {
120 Common::NapiThrow(env, ERROR_PARAM_INVALID);
121 return Common::NapiGetUndefined(env);
122 }
123
124 AsyncCallbackDistributedEnable *asynccallbackinfo =
125 new (std::nothrow) AsyncCallbackDistributedEnable {.env = env, .asyncWork = nullptr, .params = params};
126 if (!asynccallbackinfo) {
127 Common::NapiThrow(env, ERROR_INTERNAL_ERROR);
128 return Common::JSParaError(env, nullptr);
129 }
130 napi_value promise = nullptr;
131 Common::PaddingCallbackPromiseInfo(env, nullptr, asynccallbackinfo->info, promise);
132
133 napi_value resourceName = nullptr;
134 napi_create_string_latin1(env, "distributedEnableByBundle", NAPI_AUTO_LENGTH, &resourceName);
135 // Asynchronous function call
136 napi_create_async_work(env,
137 nullptr,
138 resourceName,
139 [](napi_env env, void *data) {
140 ANS_LOGD("NapiSetDistributedEnableByBundle work excute.");
141 AsyncCallbackDistributedEnable *asynccallbackinfo = static_cast<AsyncCallbackDistributedEnable *>(data);
142 if (asynccallbackinfo) {
143 std::string deviceType = asynccallbackinfo->params.deviceType;
144 asynccallbackinfo->info.errorCode = NotificationHelper::SetDistributedEnabledByBundle(
145 asynccallbackinfo->params.option, deviceType, asynccallbackinfo->params.enable);
146 ANS_LOGI("errorCode = %{public}d", asynccallbackinfo->info.errorCode);
147 }
148 },
149 AsyncCompleteCallbackNapiSetDistributedEnabledByBundle,
150 (void *)asynccallbackinfo,
151 &asynccallbackinfo->asyncWork);
152
153 napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated);
154
155 return promise;
156 }
157
ParseParameters(const napi_env & env,const napi_callback_info & info,DistributedBundleOptionParams & params)158 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, DistributedBundleOptionParams ¶ms)
159 {
160 ANS_LOGD("called");
161
162 size_t argc = SET_DISTRIBUTED_BUNDLE_OPTION_NUM;
163 napi_value argv[SET_DISTRIBUTED_BUNDLE_OPTION_NUM] = {nullptr};
164 napi_value thisVar = nullptr;
165 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
166 if (argc < SET_DISTRIBUTED_BUNDLE_OPTION_NUM) {
167 ANS_LOGE("Wrong number of arguments.");
168 Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
169 return nullptr;
170 }
171
172 // argv[0]: bundles
173 napi_valuetype valuetype = napi_undefined;
174 bool isArray = false;
175 napi_is_array(env, argv[PARAM0], &isArray);
176 if (!isArray) {
177 ANS_LOGE("Wrong argument type. Array expected.");
178 std::string msg = "Incorrect parameter types.The type of param must be array.";
179 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
180 return nullptr;
181 }
182 uint32_t length = 0;
183 napi_get_array_length(env, argv[PARAM0], &length);
184 if (length == 0) {
185 ANS_LOGD("The array is empty.");
186 std::string msg = "Mandatory parameters are left unspecified. The array is empty.";
187 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
188 return nullptr;
189 }
190 for (size_t index = 0; index < length; index++) {
191 napi_value nDistributedBundleOption = nullptr;
192 napi_get_element(env, argv[PARAM0], index, &nDistributedBundleOption);
193 NAPI_CALL_BASE(env, napi_typeof(env, nDistributedBundleOption, &valuetype), nullptr);
194 if (valuetype != napi_object) {
195 ANS_LOGE("Wrong argument type. Object expected.");
196 std::string msg = "Incorrect parameter types.The type of param must be object.";
197 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
198 return nullptr;
199 }
200 DistributedBundleOption bundleOption;
201 auto retValue = Common::GetDistributedBundleOption(env, nDistributedBundleOption, bundleOption);
202 if (retValue == nullptr) {
203 ANS_LOGE("null retValue");
204 Common::NapiThrow(env, ERROR_PARAM_INVALID, PARAMETER_VERIFICATION_FAILED);
205 return nullptr;
206 }
207 params.bundles.emplace_back(bundleOption);
208 }
209
210 // argv[1]: deviceType
211 NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
212 if (valuetype != napi_string) {
213 ANS_LOGE("Wrong argument type. Bool expected.");
214 std::string msg = "Incorrect parameter types.The type of param must be boolean.";
215 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
216 return nullptr;
217 }
218 char str[STR_MAX_SIZE] = {0};
219 size_t strLen = 0;
220 napi_get_value_string_utf8(env, argv[PARAM1], str, STR_MAX_SIZE - 1, &strLen);
221 if (std::strlen(str) == 0) {
222 ANS_LOGE("Property deviceType is empty");
223 Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
224 return nullptr;
225 }
226 params.deviceType = str;
227
228 return Common::NapiGetNull(env);
229 }
230
AsyncCompleteCallbackNapiSetDistributedBundleOption(napi_env env,napi_status status,void * data)231 void AsyncCompleteCallbackNapiSetDistributedBundleOption(napi_env env, napi_status status, void *data)
232 {
233 ANS_LOGD("called");
234 if (!data) {
235 ANS_LOGE("Invalid async callback data");
236 return;
237 }
238 AsyncCallbackDistributedBundleOption *asynccallbackinfo = static_cast<AsyncCallbackDistributedBundleOption *>(data);
239 if (asynccallbackinfo) {
240 Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env));
241 if (asynccallbackinfo->info.callback != nullptr) {
242 ANS_LOGD("Delete NapiSetDistributedBundleOption callback reference.");
243 napi_delete_reference(env, asynccallbackinfo->info.callback);
244 }
245 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
246 delete asynccallbackinfo;
247 asynccallbackinfo = nullptr;
248 }
249 }
250
NapiSetDistributedBundleOption(napi_env env,napi_callback_info info)251 napi_value NapiSetDistributedBundleOption(napi_env env, napi_callback_info info)
252 {
253 ANS_LOGD("called");
254 DistributedBundleOptionParams params {};
255 if (ParseParameters(env, info, params) == nullptr) {
256 Common::NapiThrow(env, ERROR_PARAM_INVALID);
257 return Common::NapiGetUndefined(env);
258 }
259
260 AsyncCallbackDistributedBundleOption *asynccallbackinfo =
261 new (std::nothrow) AsyncCallbackDistributedBundleOption {.env = env, .asyncWork = nullptr, .params = params};
262 if (!asynccallbackinfo) {
263 Common::NapiThrow(env, ERROR_INTERNAL_ERROR);
264 return Common::JSParaError(env, nullptr);
265 }
266 napi_value promise = nullptr;
267 Common::PaddingCallbackPromiseInfo(env, nullptr, asynccallbackinfo->info, promise);
268
269 napi_value resourceName = nullptr;
270 napi_create_string_latin1(env, "setDistributedBundleOption", NAPI_AUTO_LENGTH, &resourceName);
271 // Asynchronous function call
272 napi_create_async_work(env,
273 nullptr,
274 resourceName,
275 [](napi_env env, void *data) {
276 ANS_LOGD("NapiSetDistributedBundleOption work excute.");
277 AsyncCallbackDistributedBundleOption *asynccallbackinfo =
278 static_cast<AsyncCallbackDistributedBundleOption *>(data);
279 if (asynccallbackinfo) {
280 asynccallbackinfo->info.errorCode = NotificationHelper::SetDistributedBundleOption(
281 asynccallbackinfo->params.bundles, asynccallbackinfo->params.deviceType);
282 ANS_LOGI("errorCode = %{public}d", asynccallbackinfo->info.errorCode);
283 }
284 },
285 AsyncCompleteCallbackNapiSetDistributedBundleOption,
286 (void *)asynccallbackinfo,
287 &asynccallbackinfo->asyncWork);
288
289 napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated);
290
291 return promise;
292 }
293
AsyncCompleteCallbackNapiSetSmartReminderEnabled(napi_env env,napi_status status,void * data)294 void AsyncCompleteCallbackNapiSetSmartReminderEnabled(napi_env env, napi_status status, void *data)
295 {
296 ANS_LOGD("called");
297 if (!data) {
298 ANS_LOGE("Invalid async callback data");
299 return;
300 }
301 AsyncCallbackSmartReminderEnabled *asynccallbackinfo = static_cast<AsyncCallbackSmartReminderEnabled *>(data);
302 if (asynccallbackinfo) {
303 Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env));
304 if (asynccallbackinfo->info.callback != nullptr) {
305 ANS_LOGD("Delete NapiSetSmartReminderEnabled callback reference.");
306 napi_delete_reference(env, asynccallbackinfo->info.callback);
307 }
308 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
309 delete asynccallbackinfo;
310 asynccallbackinfo = nullptr;
311 }
312 }
313
ParseParameters(const napi_env & env,const napi_callback_info & info,SmartReminderEnabledParams & params)314 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, SmartReminderEnabledParams ¶ms)
315 {
316 ANS_LOGD("called");
317
318 size_t argc = SET_SMART_REMINDER_ENABLE_MAX_PARA;
319 napi_value argv[SET_SMART_REMINDER_ENABLE_MAX_PARA] = {nullptr};
320 napi_value thisVar = nullptr;
321 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
322 if (argc < SET_SMART_REMINDER_ENABLE_MIN_PARA) {
323 ANS_LOGE("Wrong number of arguments.");
324 Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
325 return nullptr;
326 }
327
328 napi_valuetype valuetype = napi_undefined;
329 // argv[0]: deviceType
330 NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
331 if (valuetype != napi_string) {
332 ANS_LOGE("Wrong argument type. String expected.");
333 std::string msg = "Incorrect parameter types.The type of param must be string.";
334 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
335 return nullptr;
336 }
337 char str[STR_MAX_SIZE] = {0};
338 size_t strLen = 0;
339 napi_get_value_string_utf8(env, argv[PARAM0], str, STR_MAX_SIZE - 1, &strLen);
340 if (std::strlen(str) == 0) {
341 ANS_LOGE("Property deviceType is empty");
342 Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
343 return nullptr;
344 }
345 params.deviceType = str;
346
347 if (argc > SET_SMART_REMINDER_ENABLE_MIN_PARA) {
348 // argv[1]: enable
349 NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
350 if (valuetype != napi_boolean) {
351 ANS_LOGE("Wrong argument type. Bool expected.");
352 std::string msg = "Incorrect parameter types.The type of param must be boolean.";
353 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
354 return nullptr;
355 }
356 napi_get_value_bool(env, argv[PARAM1], ¶ms.enable);
357 }
358
359 return Common::NapiGetNull(env);
360 }
361
NapiSetSmartReminderEnabled(napi_env env,napi_callback_info info)362 napi_value NapiSetSmartReminderEnabled(napi_env env, napi_callback_info info)
363 {
364 ANS_LOGD("called");
365 SmartReminderEnabledParams params {};
366 if (ParseParameters(env, info, params) == nullptr) {
367 Common::NapiThrow(env, ERROR_PARAM_INVALID);
368 return Common::NapiGetUndefined(env);
369 }
370
371 AsyncCallbackSmartReminderEnabled *asynccallbackinfo =
372 new (std::nothrow) AsyncCallbackSmartReminderEnabled {.env = env, .asyncWork = nullptr, .params = params};
373 if (!asynccallbackinfo) {
374 Common::NapiThrow(env, ERROR_INTERNAL_ERROR);
375 return Common::JSParaError(env, nullptr);
376 }
377 napi_value promise = nullptr;
378 Common::PaddingCallbackPromiseInfo(env, nullptr, asynccallbackinfo->info, promise);
379
380 napi_value resourceName = nullptr;
381 napi_create_string_latin1(env, "setSmartReminderEnabled", NAPI_AUTO_LENGTH, &resourceName);
382 // Asynchronous function call
383 napi_create_async_work(env,
384 nullptr,
385 resourceName,
386 [](napi_env env, void *data) {
387 ANS_LOGD("NapiSetSmartReminderEnabled work excute.");
388 AsyncCallbackSmartReminderEnabled *asynccallbackinfo =
389 static_cast<AsyncCallbackSmartReminderEnabled *>(data);
390 if (asynccallbackinfo) {
391 asynccallbackinfo->info.errorCode = NotificationHelper::SetSmartReminderEnabled(
392 asynccallbackinfo->params.deviceType, asynccallbackinfo->params.enable);
393 ANS_LOGD("errorCode = %{public}d", asynccallbackinfo->info.errorCode);
394 }
395 },
396 AsyncCompleteCallbackNapiSetSmartReminderEnabled,
397 (void *)asynccallbackinfo,
398 &asynccallbackinfo->asyncWork);
399
400 napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated);
401 return promise;
402 }
403
AsyncCompleteCallbackNapiIsSmartReminderEnabled(napi_env env,napi_status status,void * data)404 void AsyncCompleteCallbackNapiIsSmartReminderEnabled(napi_env env, napi_status status, void *data)
405 {
406 ANS_LOGD("called");
407 if (!data) {
408 ANS_LOGE("Invalid async callback data");
409 return;
410 }
411 ANS_LOGI("IsSmartReminderEnabled napi_create_async_work end");
412 AsyncCallbackSmartReminderEnabled *asynccallbackinfo = static_cast<AsyncCallbackSmartReminderEnabled *>(data);
413 if (asynccallbackinfo) {
414 napi_value result = nullptr;
415 if (asynccallbackinfo->info.errorCode != ERR_OK) {
416 result = Common::NapiGetNull(env);
417 } else {
418 napi_get_boolean(env, asynccallbackinfo->params.enable, &result);
419 }
420 Common::CreateReturnValue(env, asynccallbackinfo->info, result);
421 if (asynccallbackinfo->info.callback != nullptr) {
422 ANS_LOGD("Delete NapiIsSmartReminderEnabled callback reference.");
423 napi_delete_reference(env, asynccallbackinfo->info.callback);
424 }
425 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
426 delete asynccallbackinfo;
427 asynccallbackinfo = nullptr;
428 }
429 }
430
NapiIsSmartReminderEnabled(napi_env env,napi_callback_info info)431 napi_value NapiIsSmartReminderEnabled(napi_env env, napi_callback_info info)
432 {
433 ANS_LOGD("called");
434 SmartReminderEnabledParams params {};
435 if (ParseParameters(env, info, params) == nullptr) {
436 Common::NapiThrow(env, ERROR_PARAM_INVALID);
437 return Common::NapiGetUndefined(env);
438 }
439
440 AsyncCallbackSmartReminderEnabled *asynccallbackinfo =
441 new (std::nothrow) AsyncCallbackSmartReminderEnabled {.env = env, .asyncWork = nullptr, .params = params};
442 if (!asynccallbackinfo) {
443 Common::NapiThrow(env, ERROR_INTERNAL_ERROR);
444 return Common::JSParaError(env, nullptr);
445 }
446 napi_value promise = nullptr;
447 Common::PaddingCallbackPromiseInfo(env, nullptr, asynccallbackinfo->info, promise);
448
449 napi_value resourceName = nullptr;
450 napi_create_string_latin1(env, "isSmartReminderEnabled", NAPI_AUTO_LENGTH, &resourceName);
451 // Asynchronous function call
452 napi_create_async_work(env,
453 nullptr,
454 resourceName,
455 [](napi_env env, void *data) {
456 ANS_LOGD("NapiIsSmartReminderEnabled work excute.");
457 AsyncCallbackSmartReminderEnabled *asynccallbackinfo =
458 static_cast<AsyncCallbackSmartReminderEnabled *>(data);
459 if (asynccallbackinfo) {
460 asynccallbackinfo->info.errorCode = NotificationHelper::IsSmartReminderEnabled(
461 asynccallbackinfo->params.deviceType, asynccallbackinfo->params.enable);
462 ANS_LOGD("errorCode = %{public}d", asynccallbackinfo->info.errorCode);
463 }
464 },
465 AsyncCompleteCallbackNapiIsSmartReminderEnabled,
466 (void *)asynccallbackinfo,
467 &asynccallbackinfo->asyncWork);
468
469 napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated);
470 return promise;
471 }
472
ParseParameters(const napi_env & env,const napi_callback_info & info,DistributedEnableBySlotParams & params,bool setOperation)473 napi_value ParseParameters(const napi_env &env,
474 const napi_callback_info &info, DistributedEnableBySlotParams ¶ms, bool setOperation)
475 {
476 ANS_LOGD("called");
477 size_t argc = SET_DISTRIBUTED_ENABLE_BY_SLOT_PARA;
478 napi_value argv[SET_DISTRIBUTED_ENABLE_BY_SLOT_PARA] = {nullptr};
479 napi_value thisVar = nullptr;
480 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
481 if ((setOperation && argc != SET_DISTRIBUTED_ENABLE_BY_SLOT_PARA) ||
482 (!setOperation && argc != GET_DISTRIBUTED_ENABLE_BY_SLOT_PARA)) {
483 ANS_LOGE("Wrong number of arguments.");
484 Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
485 return nullptr;
486 }
487
488 // argv[0]: slot
489 napi_valuetype valuetype = napi_undefined;
490 NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
491 if (valuetype != napi_number) {
492 ANS_LOGE("Parameter type error. Object expected.");
493 std::string msg = "Incorrect parameter types.The type of param must be object.";
494 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
495 return nullptr;
496 }
497 int32_t slotType;
498 NAPI_CALL(env, napi_get_value_int32(env, argv[PARAM0], &slotType));
499 NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER;
500 if (!AnsEnumUtil::SlotTypeJSToC(SlotType(slotType), outType)) {
501 std::string msg = "Incorrect parameter slot.";
502 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
503 return nullptr;
504 }
505 params.slot = outType;
506
507 // argv[1]: deviceType
508 NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
509 if (valuetype != napi_string) {
510 ANS_LOGE("Wrong argument type. Bool expected.");
511 std::string msg = "Incorrect parameter types.The type of param must be string.";
512 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
513 return nullptr;
514 }
515 char str[STR_MAX_SIZE] = {0};
516 size_t strLen = 0;
517 napi_get_value_string_utf8(env, argv[PARAM1], str, STR_MAX_SIZE - 1, &strLen);
518 if (std::strlen(str) == 0) {
519 ANS_LOGE("Property deviceType is empty");
520 Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
521 return nullptr;
522 }
523 params.deviceType = str;
524
525 // argv[2]: enable
526 if (setOperation) {
527 NAPI_CALL(env, napi_typeof(env, argv[PARAM2], &valuetype));
528 if (valuetype != napi_boolean) {
529 ANS_LOGE("Wrong argument type. Bool expected.");
530 std::string msg = "Incorrect parameter types.The type of param must be boolean.";
531 Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
532 return nullptr;
533 }
534 napi_get_value_bool(env, argv[PARAM2], ¶ms.enable);
535 }
536
537 return Common::NapiGetNull(env);
538 }
539
AsyncCompleteCallbackNapiSetDistributedEnabledBySlot(napi_env env,napi_status status,void * data)540 void AsyncCompleteCallbackNapiSetDistributedEnabledBySlot(napi_env env, napi_status status, void *data)
541 {
542 ANS_LOGD("called");
543 if (!data) {
544 ANS_LOGE("Invalid async callback data");
545 return;
546 }
547 AsyncCallbackDistributedEnableBySlot *asynccallbackinfo = static_cast<AsyncCallbackDistributedEnableBySlot *>(data);
548 if (asynccallbackinfo) {
549 Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env));
550 if (asynccallbackinfo->info.callback != nullptr) {
551 ANS_LOGD("Delete NapiSetDistributedEnabledBySlot callback reference.");
552 napi_delete_reference(env, asynccallbackinfo->info.callback);
553 }
554 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
555 delete asynccallbackinfo;
556 asynccallbackinfo = nullptr;
557 }
558 }
559
NapiSetDistributedEnabledBySlot(napi_env env,napi_callback_info info)560 napi_value NapiSetDistributedEnabledBySlot(napi_env env, napi_callback_info info)
561 {
562 ANS_LOGD("called");
563 DistributedEnableBySlotParams params {};
564 if (ParseParameters(env, info, params, true) == nullptr) {
565 Common::NapiThrow(env, ERROR_PARAM_INVALID);
566 return Common::NapiGetUndefined(env);
567 }
568
569 AsyncCallbackDistributedEnableBySlot *asynccallbackinfo =
570 new (std::nothrow) AsyncCallbackDistributedEnableBySlot {.env = env, .asyncWork = nullptr, .params = params};
571 if (!asynccallbackinfo) {
572 Common::NapiThrow(env, ERROR_INTERNAL_ERROR);
573 return Common::JSParaError(env, nullptr);
574 }
575 napi_value promise = nullptr;
576 Common::PaddingCallbackPromiseInfo(env, nullptr, asynccallbackinfo->info, promise);
577
578 napi_value resourceName = nullptr;
579 napi_create_string_latin1(env, "setDistributedEnabledBySlot", NAPI_AUTO_LENGTH, &resourceName);
580 // Asynchronous function call
581 napi_create_async_work(env,
582 nullptr,
583 resourceName,
584 [](napi_env env, void *data) {
585 ANS_LOGD("NapiSetDistributedEnabledBySlot work excute.");
586 AsyncCallbackDistributedEnableBySlot *asynccallbackinfo =
587 static_cast<AsyncCallbackDistributedEnableBySlot *>(data);
588 if (asynccallbackinfo) {
589 asynccallbackinfo->info.errorCode = NotificationHelper::SetDistributedEnabledBySlot(
590 asynccallbackinfo->params.slot,
591 asynccallbackinfo->params.deviceType,
592 asynccallbackinfo->params.enable);
593 ANS_LOGI("errorCode = %{public}d", asynccallbackinfo->info.errorCode);
594 }
595 },
596 AsyncCompleteCallbackNapiSetDistributedEnabledBySlot,
597 (void *)asynccallbackinfo,
598 &asynccallbackinfo->asyncWork);
599
600 napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated);
601
602 return promise;
603 }
604
AsyncCompleteCallbackNapiIsDistributedEnabledBySlot(napi_env env,napi_status status,void * data)605 void AsyncCompleteCallbackNapiIsDistributedEnabledBySlot(napi_env env, napi_status status, void *data)
606 {
607 ANS_LOGD("called");
608 if (!data) {
609 ANS_LOGE("Invalid async callback data");
610 return;
611 }
612 ANS_LOGI("IsDistributedEnabledBySlot napi_create_async_work end");
613 AsyncCallbackDistributedEnableBySlot *asynccallbackinfo = static_cast<AsyncCallbackDistributedEnableBySlot *>(data);
614 if (asynccallbackinfo) {
615 napi_value result = nullptr;
616 if (asynccallbackinfo->info.errorCode != ERR_OK) {
617 result = Common::NapiGetNull(env);
618 } else {
619 napi_get_boolean(env, asynccallbackinfo->params.enable, &result);
620 }
621 Common::CreateReturnValue(env, asynccallbackinfo->info, result);
622 if (asynccallbackinfo->info.callback != nullptr) {
623 ANS_LOGD("Delete NapiIsDistributedEnabledBySlot callback reference.");
624 napi_delete_reference(env, asynccallbackinfo->info.callback);
625 }
626 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
627 delete asynccallbackinfo;
628 asynccallbackinfo = nullptr;
629 }
630 }
631
NapiIsDistributedEnabledBySlot(napi_env env,napi_callback_info info)632 napi_value NapiIsDistributedEnabledBySlot(napi_env env, napi_callback_info info)
633 {
634 ANS_LOGD("called");
635 DistributedEnableBySlotParams params {};
636 if (ParseParameters(env, info, params, false) == nullptr) {
637 Common::NapiThrow(env, ERROR_PARAM_INVALID);
638 return Common::NapiGetUndefined(env);
639 }
640
641 AsyncCallbackDistributedEnableBySlot *asynccallbackinfo =
642 new (std::nothrow) AsyncCallbackDistributedEnableBySlot {.env = env, .asyncWork = nullptr, .params = params};
643 if (!asynccallbackinfo) {
644 Common::NapiThrow(env, ERROR_INTERNAL_ERROR);
645 return Common::JSParaError(env, nullptr);
646 }
647 napi_value promise = nullptr;
648 Common::PaddingCallbackPromiseInfo(env, nullptr, asynccallbackinfo->info, promise);
649
650 napi_value resourceName = nullptr;
651 napi_create_string_latin1(env, "isDistributedEnabledBySlot", NAPI_AUTO_LENGTH, &resourceName);
652 // Asynchronous function call
653 napi_create_async_work(env,
654 nullptr,
655 resourceName,
656 [](napi_env env, void *data) {
657 ANS_LOGD("NapiIsDistributedEnabledBySlot work excute.");
658 AsyncCallbackDistributedEnableBySlot *asynccallbackinfo =
659 static_cast<AsyncCallbackDistributedEnableBySlot *>(data);
660 if (asynccallbackinfo) {
661 asynccallbackinfo->info.errorCode = NotificationHelper::IsDistributedEnabledBySlot(
662 asynccallbackinfo->params.slot,
663 asynccallbackinfo->params.deviceType,
664 asynccallbackinfo->params.enable);
665 ANS_LOGD("errorCode = %{public}d", asynccallbackinfo->info.errorCode);
666 }
667 },
668 AsyncCompleteCallbackNapiIsDistributedEnabledBySlot,
669 (void *)asynccallbackinfo,
670 &asynccallbackinfo->asyncWork);
671
672 napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated);
673 return promise;
674 }
675 } // namespace NotificationNapi
676 } // namespace OHOS
677