1 /*
2 * Copyright (c) 2021-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 "disturb_mode.h"
17
18 namespace OHOS {
19 namespace NotificationNapi {
20 const int SET_DISTURB_MAX_PARA = 3;
21 const int SET_DISTURB_MIN_PARA = 1;
22 const int GET_DISTURB_MAX_PARA = 2;
23
GetDoNotDisturbDate(const napi_env & env,const napi_value & argv,SetDoNotDisturbDateParams & params)24 napi_value GetDoNotDisturbDate(const napi_env &env, const napi_value &argv, SetDoNotDisturbDateParams ¶ms)
25 {
26 ANS_LOGI("enter");
27 napi_value value = nullptr;
28 bool hasProperty = false;
29 napi_valuetype valuetype = napi_undefined;
30 // argv[0]: date:type
31 NAPI_CALL(env, napi_has_named_property(env, argv, "type", &hasProperty));
32 if (!hasProperty) {
33 ANS_LOGW("Wrong argument type. Property type expected.");
34 return nullptr;
35 }
36 napi_get_named_property(env, argv, "type", &value);
37 NAPI_CALL(env, napi_typeof(env, value, &valuetype));
38 if (valuetype != napi_number) {
39 ANS_LOGW("Wrong argument type. Number expected.");
40 return nullptr;
41 }
42 int type = 0;
43 NotificationConstant::DoNotDisturbType outType = NotificationConstant::DoNotDisturbType::NONE;
44 napi_get_value_int32(env, value, &type);
45 ANS_LOGI("type is: %{public}d", type);
46 if (!Common::DoNotDisturbTypeJSToC(DoNotDisturbType(type), outType)) {
47 return nullptr;
48 }
49 params.date.SetDoNotDisturbType(outType);
50
51 // argv[0]: date:begin
52 NAPI_CALL(env, napi_has_named_property(env, argv, "begin", &hasProperty));
53 if (!hasProperty) {
54 ANS_LOGW("Wrong argument type. Property type expected.");
55 return nullptr;
56 }
57 double begin = 0;
58 napi_get_named_property(env, argv, "begin", &value);
59 bool isDate = false;
60 napi_is_date(env, value, &isDate);
61 if (!isDate) {
62 ANS_LOGE("Wrong argument type. Date expected.");
63 return nullptr;
64 }
65 napi_get_date_value(env, value, &begin);
66 params.date.SetBeginDate(int64_t(begin));
67
68 // argv[0]: date:end
69 NAPI_CALL(env, napi_has_named_property(env, argv, "end", &hasProperty));
70 if (!hasProperty) {
71 ANS_LOGW("Wrong argument type. Property type expected.");
72 return nullptr;
73 }
74 double end = 0;
75 napi_get_named_property(env, argv, "end", &value);
76 isDate = false;
77 napi_is_date(env, value, &isDate);
78 if (!isDate) {
79 ANS_LOGE("Wrong argument type. Date expected.");
80 return nullptr;
81 }
82 napi_get_date_value(env, value, &end);
83 params.date.SetEndDate(int64_t(end));
84
85 return Common::NapiGetNull(env);
86 }
87
ParseParameters(const napi_env & env,const napi_callback_info & info,SetDoNotDisturbDateParams & params)88 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, SetDoNotDisturbDateParams ¶ms)
89 {
90 ANS_LOGI("enter");
91
92 size_t argc = SET_DISTURB_MAX_PARA;
93 napi_value argv[SET_DISTURB_MAX_PARA] = {nullptr};
94 napi_value thisVar = nullptr;
95 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
96 if (argc < SET_DISTURB_MIN_PARA) {
97 ANS_LOGW("Wrong argument type. Property type expected.");
98 return nullptr;
99 }
100
101 // argv[0]: date
102 napi_valuetype valuetype = napi_undefined;
103 NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
104 if (valuetype != napi_object) {
105 ANS_LOGW("Wrong argument type. Property type expected.");
106 return nullptr;
107 }
108 if (GetDoNotDisturbDate(env, argv[PARAM0], params) == nullptr) {
109 return nullptr;
110 }
111
112 // argv[1] : userId / callback
113 if (argc >= SET_DISTURB_MAX_PARA - 1) {
114 NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
115 if ((valuetype != napi_number) && (valuetype != napi_function)) {
116 ANS_LOGW("Wrong argument type. Function or object expected.");
117 return nullptr;
118 }
119
120 if (valuetype == napi_number) {
121 params.hasUserId = true;
122 NAPI_CALL(env, napi_get_value_int32(env, argv[PARAM1], ¶ms.userId));
123 } else {
124 napi_create_reference(env, argv[PARAM1], 1, ¶ms.callback);
125 }
126 }
127
128 // argv[2]:callback
129 if (argc >= SET_DISTURB_MAX_PARA) {
130 NAPI_CALL(env, napi_typeof(env, argv[PARAM2], &valuetype));
131 if (valuetype != napi_function) {
132 ANS_LOGW("Wrong argument type. Function expected.");
133 return nullptr;
134 }
135 napi_create_reference(env, argv[PARAM2], 1, ¶ms.callback);
136 }
137
138 return Common::NapiGetNull(env);
139 }
140
SetDoNotDisturbDate(napi_env env,napi_callback_info info)141 napi_value SetDoNotDisturbDate(napi_env env, napi_callback_info info)
142 {
143 ANS_LOGI("enter");
144
145 SetDoNotDisturbDateParams params {};
146 if (ParseParameters(env, info, params) == nullptr) {
147 return Common::NapiGetUndefined(env);
148 }
149
150 AsyncCallbackInfoSetDoNotDisturb *asynccallbackinfo =
151 new (std::nothrow) AsyncCallbackInfoSetDoNotDisturb {.env = env, .asyncWork = nullptr, .params = params};
152 if (!asynccallbackinfo) {
153 return Common::JSParaError(env, params.callback);
154 }
155 napi_value promise = nullptr;
156 Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise);
157
158 napi_value resourceName = nullptr;
159 napi_create_string_latin1(env, "setDoNotDisturbDate", NAPI_AUTO_LENGTH, &resourceName);
160 // Asynchronous function call
161 napi_create_async_work(env,
162 nullptr, resourceName, [](napi_env env, void *data) {
163 ANS_LOGI("SetDoNotDisturbDate napi_create_async_work start");
164 AsyncCallbackInfoSetDoNotDisturb *asynccallbackinfo = static_cast<AsyncCallbackInfoSetDoNotDisturb *>(data);
165 if (asynccallbackinfo) {
166 if (asynccallbackinfo->params.hasUserId) {
167 asynccallbackinfo->info.errorCode = NotificationHelper::SetDoNotDisturbDate(
168 asynccallbackinfo->params.userId, asynccallbackinfo->params.date);
169 } else {
170 asynccallbackinfo->info.errorCode = NotificationHelper::SetDoNotDisturbDate(
171 asynccallbackinfo->params.date);
172 }
173
174 ANS_LOGI("SetDoNotDisturbDate date=%{public}s errorCode=%{public}d, hasUserId=%{public}d",
175 asynccallbackinfo->params.date.Dump().c_str(), asynccallbackinfo->info.errorCode,
176 asynccallbackinfo->params.hasUserId);
177 }
178 },
179 [](napi_env env, napi_status status, void *data) {
180 ANS_LOGI("SetDoNotDisturbDate napi_create_async_work end");
181 AsyncCallbackInfoSetDoNotDisturb *asynccallbackinfo = static_cast<AsyncCallbackInfoSetDoNotDisturb *>(data);
182 if (asynccallbackinfo) {
183 Common::ReturnCallbackPromise(env, asynccallbackinfo->info, Common::NapiGetNull(env));
184 if (asynccallbackinfo->info.callback != nullptr) {
185 napi_delete_reference(env, asynccallbackinfo->info.callback);
186 }
187 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
188 delete asynccallbackinfo;
189 asynccallbackinfo = nullptr;
190 }
191 },
192 (void *)asynccallbackinfo, &asynccallbackinfo->asyncWork);
193
194 napi_status status = napi_queue_async_work(env, asynccallbackinfo->asyncWork);
195 if (status != napi_ok) {
196 ANS_LOGE("napi_queue_async_work failed return: %{public}d", status);
197 if (asynccallbackinfo->info.callback != nullptr) {
198 napi_delete_reference(env, asynccallbackinfo->info.callback);
199 }
200 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
201 delete asynccallbackinfo;
202 asynccallbackinfo = nullptr;
203 return Common::JSParaError(env, params.callback);
204 }
205
206 if (asynccallbackinfo->info.isCallback) {
207 return Common::NapiGetNull(env);
208 } else {
209 return promise;
210 }
211 }
212
AsyncCompleteCallbackGetDoNotDisturbDate(napi_env env,napi_status status,void * data)213 void AsyncCompleteCallbackGetDoNotDisturbDate(napi_env env, napi_status status, void *data)
214 {
215 ANS_LOGI("enter");
216 if (!data) {
217 ANS_LOGE("Invalid async callback data");
218 return;
219 }
220 AsyncCallbackInfoGetDoNotDisturb *asynccallbackinfo = static_cast<AsyncCallbackInfoGetDoNotDisturb *>(data);
221 if (asynccallbackinfo) {
222 napi_value result = Common::NapiGetNull(env);
223 if (asynccallbackinfo->info.errorCode == ERR_OK) {
224 napi_create_object(env, &result);
225 if (!Common::SetDoNotDisturbDate(env, asynccallbackinfo->date, result)) {
226 asynccallbackinfo->info.errorCode = ERROR;
227 }
228 }
229 Common::ReturnCallbackPromise(env, asynccallbackinfo->info, result);
230 if (asynccallbackinfo->info.callback != nullptr) {
231 napi_delete_reference(env, asynccallbackinfo->info.callback);
232 }
233 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
234 delete asynccallbackinfo;
235 asynccallbackinfo = nullptr;
236 }
237 }
238
ParseParameters(const napi_env & env,const napi_callback_info & info,GetDoNotDisturbDateParams & params)239 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, GetDoNotDisturbDateParams ¶ms)
240 {
241 ANS_LOGI("enter");
242
243 size_t argc = GET_DISTURB_MAX_PARA;
244 napi_value argv[GET_DISTURB_MAX_PARA] = {nullptr};
245 napi_value thisVar = nullptr;
246 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
247
248 napi_valuetype valuetype = napi_undefined;
249 // argv[0]: userId / callback
250 if (argc >= GET_DISTURB_MAX_PARA - 1) {
251 NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
252 if ((valuetype != napi_number) && (valuetype != napi_function)) {
253 ANS_LOGW("Wrong argument type. Function or object expected.");
254 return nullptr;
255 }
256 if (valuetype == napi_number) {
257 params.hasUserId = true;
258 NAPI_CALL(env, napi_get_value_int32(env, argv[PARAM0], ¶ms.userId));
259 } else {
260 napi_create_reference(env, argv[PARAM0], 1, ¶ms.callback);
261 }
262 }
263
264 // argv[1]:callback
265 if (argc >= GET_DISTURB_MAX_PARA) {
266 NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
267 if (valuetype != napi_function) {
268 ANS_LOGW("Wrong argument type. Function expected.");
269 return nullptr;
270 }
271 napi_create_reference(env, argv[PARAM1], 1, ¶ms.callback);
272 }
273
274 return Common::NapiGetNull(env);
275 }
276
GetDoNotDisturbDate(napi_env env,napi_callback_info info)277 napi_value GetDoNotDisturbDate(napi_env env, napi_callback_info info)
278 {
279 ANS_LOGI("enter");
280
281 GetDoNotDisturbDateParams params {};
282 if (ParseParameters(env, info, params) == nullptr) {
283 return Common::NapiGetUndefined(env);
284 }
285
286 AsyncCallbackInfoGetDoNotDisturb *asynccallbackinfo =
287 new (std::nothrow) AsyncCallbackInfoGetDoNotDisturb {.env = env, .asyncWork = nullptr, .params = params};
288 if (!asynccallbackinfo) {
289 return Common::JSParaError(env, params.callback);
290 }
291 napi_value promise = nullptr;
292 Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise);
293
294 napi_value resourceName = nullptr;
295 napi_create_string_latin1(env, "getDoNotDisturbDate", NAPI_AUTO_LENGTH, &resourceName);
296 // Asynchronous function call
297 napi_create_async_work(env,
298 nullptr,
299 resourceName,
300 [](napi_env env, void *data) {
301 ANS_LOGI("GetDoNotDisturbDate napi_create_async_work start");
302 AsyncCallbackInfoGetDoNotDisturb *asynccallbackinfo =
303 static_cast<AsyncCallbackInfoGetDoNotDisturb *>(data);
304 if (asynccallbackinfo) {
305 if (asynccallbackinfo->params.hasUserId) {
306 asynccallbackinfo->info.errorCode = NotificationHelper::GetDoNotDisturbDate(
307 asynccallbackinfo->params.userId, asynccallbackinfo->date);
308 } else {
309 asynccallbackinfo->info.errorCode = NotificationHelper::GetDoNotDisturbDate(
310 asynccallbackinfo->date);
311 }
312
313 ANS_LOGI("GetDoNotDisturbDate errorCode=%{public}d date=%{public}s, hasUserId=%{public}d",
314 asynccallbackinfo->info.errorCode, asynccallbackinfo->date.Dump().c_str(),
315 asynccallbackinfo->params.hasUserId);
316 }
317 },
318 AsyncCompleteCallbackGetDoNotDisturbDate,
319 (void *)asynccallbackinfo,
320 &asynccallbackinfo->asyncWork);
321
322 napi_status status = napi_queue_async_work(env, asynccallbackinfo->asyncWork);
323 if (status != napi_ok) {
324 ANS_LOGE("napi_queue_async_work failed return: %{public}d", status);
325 if (asynccallbackinfo->info.callback != nullptr) {
326 napi_delete_reference(env, asynccallbackinfo->info.callback);
327 }
328 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
329 delete asynccallbackinfo;
330 asynccallbackinfo = nullptr;
331 return Common::JSParaError(env, params.callback);
332 }
333
334 if (asynccallbackinfo->info.isCallback) {
335 return Common::NapiGetNull(env);
336 } else {
337 return promise;
338 }
339 }
340
SupportDoNotDisturbMode(napi_env env,napi_callback_info info)341 napi_value SupportDoNotDisturbMode(napi_env env, napi_callback_info info)
342 {
343 ANS_LOGI("enter");
344
345 napi_ref callback = nullptr;
346 if (Common::ParseParaOnlyCallback(env, info, callback) == nullptr) {
347 return Common::NapiGetUndefined(env);
348 }
349
350 AsyncCallbackInfoSupportDoNotDisturb *asynccallbackinfo =
351 new (std::nothrow) AsyncCallbackInfoSupportDoNotDisturb {
352 .env = env, .asyncWork = nullptr, .callback = callback};
353
354 if (!asynccallbackinfo) {
355 return Common::JSParaError(env, callback);
356 }
357 napi_value promise = nullptr;
358 Common::PaddingCallbackPromiseInfo(env, callback, asynccallbackinfo->info, promise);
359
360 napi_value resourceName = nullptr;
361 napi_create_string_latin1(env, "supportDoNotDisturbMode", NAPI_AUTO_LENGTH, &resourceName);
362 // Asynchronous function call
363 napi_create_async_work(env,
364 nullptr,
365 resourceName,
366 [](napi_env env, void *data) {
367 ANS_LOGI("SupportDoNotDisturbMode napi_create_async_work start");
368 AsyncCallbackInfoSupportDoNotDisturb *asynccallbackinfo =
369 static_cast<AsyncCallbackInfoSupportDoNotDisturb *>(data);
370 if (asynccallbackinfo) {
371 asynccallbackinfo->info.errorCode =
372 NotificationHelper::DoesSupportDoNotDisturbMode(asynccallbackinfo->isSupported);
373 ANS_LOGI("SupportDoNotDisturbMode errorCode=%{public}d isSupported=%{public}d",
374 asynccallbackinfo->info.errorCode, asynccallbackinfo->isSupported);
375 }
376 },
377 [](napi_env env, napi_status status, void *data) {
378 ANS_LOGI("SupportDoNotDisturbMode napi_create_async_work end");
379 AsyncCallbackInfoSupportDoNotDisturb *asynccallbackinfo =
380 static_cast<AsyncCallbackInfoSupportDoNotDisturb *>(data);
381 if (asynccallbackinfo) {
382 napi_value result = nullptr;
383 napi_get_boolean(env, asynccallbackinfo->isSupported, &result);
384 Common::ReturnCallbackPromise(env, asynccallbackinfo->info, result);
385 if (asynccallbackinfo->info.callback != nullptr) {
386 napi_delete_reference(env, asynccallbackinfo->info.callback);
387 }
388 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
389 delete asynccallbackinfo;
390 asynccallbackinfo = nullptr;
391 }
392 },
393 (void *)asynccallbackinfo,
394 &asynccallbackinfo->asyncWork);
395
396 napi_status status = napi_queue_async_work(env, asynccallbackinfo->asyncWork);
397 if (status != napi_ok) {
398 ANS_LOGE("napi_queue_async_work failed return: %{public}d", status);
399 if (asynccallbackinfo->info.callback != nullptr) {
400 napi_delete_reference(env, asynccallbackinfo->info.callback);
401 }
402 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
403 delete asynccallbackinfo;
404 asynccallbackinfo = nullptr;
405 return Common::JSParaError(env, callback);
406 }
407
408 if (asynccallbackinfo->info.isCallback) {
409 return Common::NapiGetNull(env);
410 } else {
411 return promise;
412 }
413 }
414 } // namespace NotificationNapi
415 } // namespace OHOS