1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "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_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork));
195
196 if (asynccallbackinfo->info.isCallback) {
197 return Common::NapiGetNull(env);
198 } else {
199 return promise;
200 }
201 }
202
AsyncCompleteCallbackGetDoNotDisturbDate(napi_env env,napi_status status,void * data)203 void AsyncCompleteCallbackGetDoNotDisturbDate(napi_env env, napi_status status, void *data)
204 {
205 ANS_LOGI("enter");
206 if (!data) {
207 ANS_LOGE("Invalid async callback data");
208 return;
209 }
210 AsyncCallbackInfoGetDoNotDisturb *asynccallbackinfo = static_cast<AsyncCallbackInfoGetDoNotDisturb *>(data);
211 if (asynccallbackinfo) {
212 napi_value result = Common::NapiGetNull(env);
213 if (asynccallbackinfo->info.errorCode == ERR_OK) {
214 napi_create_object(env, &result);
215 if (!Common::SetDoNotDisturbDate(env, asynccallbackinfo->date, result)) {
216 asynccallbackinfo->info.errorCode = ERROR;
217 }
218 }
219 Common::ReturnCallbackPromise(env, asynccallbackinfo->info, result);
220 if (asynccallbackinfo->info.callback != nullptr) {
221 napi_delete_reference(env, asynccallbackinfo->info.callback);
222 }
223 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
224 delete asynccallbackinfo;
225 asynccallbackinfo = nullptr;
226 }
227 }
228
ParseParameters(const napi_env & env,const napi_callback_info & info,GetDoNotDisturbDateParams & params)229 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, GetDoNotDisturbDateParams ¶ms)
230 {
231 ANS_LOGI("enter");
232
233 size_t argc = GET_DISTURB_MAX_PARA;
234 napi_value argv[GET_DISTURB_MAX_PARA] = {nullptr};
235 napi_value thisVar = nullptr;
236 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
237
238 napi_valuetype valuetype = napi_undefined;
239 // argv[0]: userId / callback
240 if (argc >= GET_DISTURB_MAX_PARA - 1) {
241 NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
242 if ((valuetype != napi_number) && (valuetype != napi_function)) {
243 ANS_LOGW("Wrong argument type. Function or object expected.");
244 return nullptr;
245 }
246 if (valuetype == napi_number) {
247 params.hasUserId = true;
248 NAPI_CALL(env, napi_get_value_int32(env, argv[PARAM0], ¶ms.userId));
249 } else {
250 napi_create_reference(env, argv[PARAM0], 1, ¶ms.callback);
251 }
252 }
253
254 // argv[1]:callback
255 if (argc >= GET_DISTURB_MAX_PARA) {
256 NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
257 if (valuetype != napi_function) {
258 ANS_LOGW("Wrong argument type. Function expected.");
259 return nullptr;
260 }
261 napi_create_reference(env, argv[PARAM1], 1, ¶ms.callback);
262 }
263
264 return Common::NapiGetNull(env);
265 }
266
GetDoNotDisturbDate(napi_env env,napi_callback_info info)267 napi_value GetDoNotDisturbDate(napi_env env, napi_callback_info info)
268 {
269 ANS_LOGI("enter");
270
271 GetDoNotDisturbDateParams params {};
272 if (ParseParameters(env, info, params) == nullptr) {
273 return Common::NapiGetUndefined(env);
274 }
275
276 AsyncCallbackInfoGetDoNotDisturb *asynccallbackinfo =
277 new (std::nothrow) AsyncCallbackInfoGetDoNotDisturb {.env = env, .asyncWork = nullptr, .params = params};
278 if (!asynccallbackinfo) {
279 return Common::JSParaError(env, params.callback);
280 }
281 napi_value promise = nullptr;
282 Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise);
283
284 napi_value resourceName = nullptr;
285 napi_create_string_latin1(env, "getDoNotDisturbDate", NAPI_AUTO_LENGTH, &resourceName);
286 // Asynchronous function call
287 napi_create_async_work(env,
288 nullptr,
289 resourceName,
290 [](napi_env env, void *data) {
291 ANS_LOGI("GetDoNotDisturbDate napi_create_async_work start");
292 AsyncCallbackInfoGetDoNotDisturb *asynccallbackinfo =
293 static_cast<AsyncCallbackInfoGetDoNotDisturb *>(data);
294 if (asynccallbackinfo) {
295 if (asynccallbackinfo->params.hasUserId) {
296 asynccallbackinfo->info.errorCode = NotificationHelper::GetDoNotDisturbDate(
297 asynccallbackinfo->params.userId, asynccallbackinfo->date);
298 } else {
299 asynccallbackinfo->info.errorCode = NotificationHelper::GetDoNotDisturbDate(
300 asynccallbackinfo->date);
301 }
302
303 ANS_LOGI("GetDoNotDisturbDate errorCode=%{public}d date=%{public}s, hasUserId=%{public}d",
304 asynccallbackinfo->info.errorCode, asynccallbackinfo->date.Dump().c_str(),
305 asynccallbackinfo->params.hasUserId);
306 }
307 },
308 AsyncCompleteCallbackGetDoNotDisturbDate,
309 (void *)asynccallbackinfo,
310 &asynccallbackinfo->asyncWork);
311
312 NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork));
313
314 if (asynccallbackinfo->info.isCallback) {
315 return Common::NapiGetNull(env);
316 } else {
317 return promise;
318 }
319 }
320
SupportDoNotDisturbMode(napi_env env,napi_callback_info info)321 napi_value SupportDoNotDisturbMode(napi_env env, napi_callback_info info)
322 {
323 ANS_LOGI("enter");
324
325 napi_ref callback = nullptr;
326 if (Common::ParseParaOnlyCallback(env, info, callback) == nullptr) {
327 return Common::NapiGetUndefined(env);
328 }
329
330 AsyncCallbackInfoSupportDoNotDisturb *asynccallbackinfo =
331 new (std::nothrow) AsyncCallbackInfoSupportDoNotDisturb {
332 .env = env, .asyncWork = nullptr, .callback = callback};
333
334 if (!asynccallbackinfo) {
335 return Common::JSParaError(env, callback);
336 }
337 napi_value promise = nullptr;
338 Common::PaddingCallbackPromiseInfo(env, callback, asynccallbackinfo->info, promise);
339
340 napi_value resourceName = nullptr;
341 napi_create_string_latin1(env, "supportDoNotDisturbMode", NAPI_AUTO_LENGTH, &resourceName);
342 // Asynchronous function call
343 napi_create_async_work(env,
344 nullptr,
345 resourceName,
346 [](napi_env env, void *data) {
347 ANS_LOGI("SupportDoNotDisturbMode napi_create_async_work start");
348 AsyncCallbackInfoSupportDoNotDisturb *asynccallbackinfo =
349 static_cast<AsyncCallbackInfoSupportDoNotDisturb *>(data);
350 if (asynccallbackinfo) {
351 asynccallbackinfo->info.errorCode =
352 NotificationHelper::DoesSupportDoNotDisturbMode(asynccallbackinfo->isSupported);
353 ANS_LOGI("SupportDoNotDisturbMode errorCode=%{public}d isSupported=%{public}d",
354 asynccallbackinfo->info.errorCode, asynccallbackinfo->isSupported);
355 }
356 },
357 [](napi_env env, napi_status status, void *data) {
358 ANS_LOGI("SupportDoNotDisturbMode napi_create_async_work end");
359 AsyncCallbackInfoSupportDoNotDisturb *asynccallbackinfo =
360 static_cast<AsyncCallbackInfoSupportDoNotDisturb *>(data);
361 if (asynccallbackinfo) {
362 napi_value result = nullptr;
363 napi_get_boolean(env, asynccallbackinfo->isSupported, &result);
364 Common::ReturnCallbackPromise(env, asynccallbackinfo->info, result);
365 if (asynccallbackinfo->info.callback != nullptr) {
366 napi_delete_reference(env, asynccallbackinfo->info.callback);
367 }
368 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
369 delete asynccallbackinfo;
370 asynccallbackinfo = nullptr;
371 }
372 },
373 (void *)asynccallbackinfo,
374 &asynccallbackinfo->asyncWork);
375
376 NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork));
377
378 if (asynccallbackinfo->info.isCallback) {
379 return Common::NapiGetNull(env);
380 } else {
381 return promise;
382 }
383 }
384 } // namespace NotificationNapi
385 } // namespace OHOS