• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "reminder/reminder_common.h"
17 
18 #include "ans_log_wrapper.h"
19 #include "common.h"
20 #include "ipc_skeleton.h"
21 #include "reminder_request_alarm.h"
22 #include "reminder_request_calendar.h"
23 #include "reminder_request_timer.h"
24 #include "tokenid_kit.h"
25 #include "securec.h"
26 
27 namespace OHOS {
28 namespace ReminderAgentNapi {
29 using namespace OHOS::Notification;
30 const uint32_t ASYNC_CALLBACK_PARAM_NUM = 2;
31 
GetReminderRequest(const napi_env & env,const napi_value & value,std::shared_ptr<ReminderRequest> & reminder)32 napi_value ReminderCommon::GetReminderRequest(
33     const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder)
34 {
35     napi_valuetype valuetype = napi_undefined;
36     NAPI_CALL(env, napi_typeof(env, value, &valuetype));
37     if (valuetype != napi_object) {
38         ANSR_LOGW("Wrong argument type. Object expected.");
39         return nullptr;
40     }
41 
42     // gen reminder
43     if (GenReminder(env, value, reminder) == nullptr) {
44         return nullptr;
45     }
46     return NotificationNapi::Common::NapiGetNull(env);
47 }
48 
GenActionButtons(const napi_env & env,const napi_value & value,std::shared_ptr<ReminderRequest> & reminder,bool isSysApp)49 bool ReminderCommon::GenActionButtons(
50     const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder, bool isSysApp)
51 {
52     char str[NotificationNapi::STR_MAX_SIZE] = {0};
53     napi_valuetype valuetype = napi_undefined;
54     napi_value actionButtons = nullptr;
55     if (!GetObject(env, value, ReminderAgentNapi::ACTION_BUTTON, actionButtons)) {
56         return true;
57     }
58     bool isArray = false;
59     napi_is_array(env, actionButtons, &isArray);
60     if (!isArray) {
61         ANSR_LOGW("Wrong argument type:%{public}s. array expected.", ACTION_BUTTON);
62         return false;
63     }
64 
65     uint32_t length = 0;
66     napi_get_array_length(env, actionButtons, &length);
67     for (size_t i = 0; i < length; i++) {
68         napi_value actionButton = nullptr;
69         napi_get_element(env, actionButtons, i, &actionButton);
70         NAPI_CALL_BASE(env, napi_typeof(env, actionButton, &valuetype), false);
71         if (valuetype != napi_object) {
72             ANSR_LOGW("Wrong element type:%{public}s. object expected.", ACTION_BUTTON);
73             return false;
74         }
75 
76         int32_t buttonType = static_cast<int32_t>(ReminderRequest::ActionButtonType::INVALID);
77         if (GetStringUtf8(env, actionButton,
78             ReminderAgentNapi::ACTION_BUTTON_TITLE, str, NotificationNapi::STR_MAX_SIZE) &&
79             GetInt32(env, actionButton, ReminderAgentNapi::ACTION_BUTTON_TYPE, buttonType, false)) {
80             if (!(ReminderRequest::ActionButtonType(buttonType) == ReminderRequest::ActionButtonType::CLOSE ||
81                 ReminderRequest::ActionButtonType(buttonType) == ReminderRequest::ActionButtonType::SNOOZE ||
82                 (ReminderRequest::ActionButtonType(buttonType) == ReminderRequest::ActionButtonType::CUSTOM &&
83                 isSysApp))) {
84                 ANSR_LOGW("Wrong argument type:%{public}s. buttonType not support.", ACTION_BUTTON);
85                 return false;
86             }
87             HandleActionButtonTitle(env, actionButton, reminder, str, buttonType);
88         } else {
89             ANSR_LOGW("Parse action button error.");
90             return false;
91         }
92     }
93     return true;
94 }
95 
HandleActionButtonTitle(const napi_env & env,const napi_value & actionButton,std::shared_ptr<ReminderRequest> & reminder,const char * str,int32_t buttonType)96 void ReminderCommon::HandleActionButtonTitle(const napi_env &env, const napi_value &actionButton,
97     std::shared_ptr<ReminderRequest>& reminder, const char* str, int32_t buttonType)
98 {
99     char res[NotificationNapi::STR_MAX_SIZE] = {0};
100     std::string resource = "";
101     if (GetStringUtf8(env, actionButton, ReminderAgentNapi::ACTION_BUTTON_RESOURCE, res,
102         NotificationNapi::STR_MAX_SIZE)) {
103         resource = std::string(res);
104     }
105 
106     std::string title(str);
107     auto buttonWantAgent = std::make_shared<ReminderRequest::ButtonWantAgent>();
108     if (ReminderRequest::ActionButtonType(buttonType) == ReminderRequest::ActionButtonType::CUSTOM) {
109         GetButtonWantAgent(env, actionButton, reminder, buttonWantAgent);
110     }
111     // gen buttonDataShareUpdate
112     auto buttonDataShareUpdate = std::make_shared<ReminderRequest::ButtonDataShareUpdate>();
113     if (ReminderRequest::ActionButtonType(buttonType) != ReminderRequest::ActionButtonType::INVALID) {
114         GetButtonDataShareUpdate(env, actionButton, buttonDataShareUpdate);
115     }
116     reminder->SetActionButton(title, static_cast<ReminderRequest::ActionButtonType>(buttonType),
117         resource, buttonWantAgent, buttonDataShareUpdate);
118     ANSR_LOGD("button title=%{public}s, type=%{public}d, resource=%{public}s",
119         title.c_str(), buttonType, resource.c_str());
120 }
121 
IsSelfSystemApp(std::shared_ptr<ReminderRequest> & reminder)122 bool ReminderCommon::IsSelfSystemApp(std::shared_ptr<ReminderRequest>& reminder)
123 {
124     auto selfToken = IPCSkeleton::GetSelfTokenID();
125     if (!Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(selfToken)) {
126         ANSR_LOGW("This application is not system-app, can not use system-api");
127         return false;
128     }
129     reminder->SetSystemApp(true);
130     return true;
131 }
132 
GetButtonWantAgent(const napi_env & env,const napi_value & value,std::shared_ptr<ReminderRequest> & reminder,std::shared_ptr<ReminderRequest::ButtonWantAgent> & buttonWantAgent)133 void ReminderCommon::GetButtonWantAgent(const napi_env &env, const napi_value &value,
134     std::shared_ptr<ReminderRequest>& reminder, std::shared_ptr<ReminderRequest::ButtonWantAgent>& buttonWantAgent)
135 {
136     char str[NotificationNapi::STR_MAX_SIZE] = {0};
137     napi_value wantAgent = nullptr;
138     if (GetObject(env, value, ReminderAgentNapi::BUTTON_WANT_AGENT, wantAgent)) {
139         if (GetStringUtf8(env, wantAgent,
140             ReminderAgentNapi::BUTTON_WANT_AGENT_PKG, str, NotificationNapi::STR_MAX_SIZE)) {
141             buttonWantAgent->pkgName = str;
142         }
143         if (GetStringUtf8(env, wantAgent,
144             ReminderAgentNapi::BUTTON_WANT_AGENT_ABILITY, str, NotificationNapi::STR_MAX_SIZE)) {
145             buttonWantAgent->abilityName = str;
146         }
147         if (GetStringUtf8(env, wantAgent,
148             ReminderAgentNapi::BUTTON_WANT_AGENT_URI, str, NotificationNapi::STR_MAX_SIZE)) {
149             reminder->SetCustomButtonUri(str);
150         }
151     }
152 }
153 
154 // uri:string  equalTo{key:value}  valuesBucket{key:value}
GetButtonDataShareUpdate(const napi_env & env,const napi_value & value,std::shared_ptr<ReminderRequest::ButtonDataShareUpdate> & buttonDataShareUpdate)155 void ReminderCommon::GetButtonDataShareUpdate(const napi_env &env, const napi_value &value,
156     std::shared_ptr<ReminderRequest::ButtonDataShareUpdate>& buttonDataShareUpdate)
157 {
158     napi_value dataShare = nullptr;
159     if (GetObject(env, value, BUTTON_DATA_SHARE_UPDATE, dataShare)) {
160         // dataShare uri
161         char str[NotificationNapi::STR_MAX_SIZE] = {0};
162         if (GetStringUtf8(env, dataShare, BUTTON_DATA_SHARE_UPDATE_URI, str, NotificationNapi::STR_MAX_SIZE)) {
163             ANSR_LOGD("gen dataShareUri success");
164             buttonDataShareUpdate->uri = str;
165         }
166         // dataShare equalTo
167         napi_value equalTo = nullptr;
168         std::string valueBucketString;
169         if (GetObject(env, dataShare, BUTTON_DATA_SHARE_UPDATE_EQUALTO, equalTo)) {
170             if (GetValueBucketObject(valueBucketString, env, equalTo)) {
171                 ANSR_LOGD("gen dataShareEqualTo success");
172                 buttonDataShareUpdate->equalTo = valueBucketString;
173             }
174         }
175         // dataShare valuesBucket
176         napi_value valuesBucket = nullptr;
177         valueBucketString.clear();
178         if (GetObject(env, dataShare, BUTTON_DATA_SHARE_UPDATE_VALUE, valuesBucket)) {
179             if (GetValueBucketObject(valueBucketString, env, valuesBucket)) {
180                 ANSR_LOGD("gen dataShareValuesBucket success");
181                 buttonDataShareUpdate->valuesBucket = valueBucketString;
182             }
183         }
184     }
185 }
186 
187 // get {key:value} to string(key:type:value)
GetValueBucketObject(std::string & valueBucketString,const napi_env & env,const napi_value & arg)188 bool ReminderCommon::GetValueBucketObject(std::string &valueBucketString, const napi_env &env, const napi_value &arg)
189 {
190     // arrary
191     napi_value keys = 0;
192     napi_get_property_names(env, arg, &keys);
193     uint32_t arrlen = 0;
194     napi_status status = napi_get_array_length(env, keys, &arrlen);
195     if (status != napi_ok) {
196         ANSR_LOGW("get the valuesBucket length err");
197         return false;
198     }
199     for (size_t i = 0; i < arrlen; ++i) {
200         // key
201         napi_value key = 0;
202         status = napi_get_element(env, keys, i, &key);
203         if (status != napi_ok) {
204             ANSR_LOGW("get valuesBucket err");
205             continue;
206         }
207         std::string keyStr = GetStringFromJS(env, key);
208         if (!ValidateString(keyStr)) {
209             ANSR_LOGW("The key contains separator");
210             return false;
211         }
212         // value
213         napi_value value = 0;
214         napi_get_property(env, arg, key, &value);
215         bool ret;
216         std::string type;
217         std::string valueObject = Convert2Value(env, value, ret, type);
218         if (!ret) {
219             ANSR_LOGW("parse valuesBucket err");
220             continue;
221         }
222         if (!ValidateString(valueObject)) {
223             ANSR_LOGW("The value contains separator");
224             return false;
225         }
226         valueBucketString += keyStr + ReminderRequest::SEP_BUTTON_VALUE + type
227             + ReminderRequest::SEP_BUTTON_VALUE + valueObject;
228         if (i < arrlen - 1) {
229             valueBucketString += ReminderRequest::SEP_BUTTON_VALUE_TYPE;
230         }
231     }
232     return true;
233 }
234 
235 // get string
GetStringFromJS(const napi_env & env,const napi_value & param,const std::string & defaultValue)236 std::string ReminderCommon::GetStringFromJS(const napi_env &env, const napi_value &param,
237     const std::string &defaultValue)
238 {
239     size_t size = 0;
240     if (napi_get_value_string_utf8(env, param, nullptr, 0, &size) != napi_ok) {
241         return defaultValue;
242     }
243     if (size == 0) {
244         return defaultValue;
245     }
246 
247     char *buf = new (std::nothrow) char[size + 1];
248     std::string value("");
249     if (buf == nullptr) {
250         return value;
251     }
252     (void)memset_s(buf, size + 1, 0, size + 1);
253     bool rev = napi_get_value_string_utf8(env, param, buf, size + 1, &size) == napi_ok;
254     if (rev) {
255         value = buf;
256     } else {
257         value = defaultValue;
258     }
259 
260     if (buf != nullptr) {
261         delete[] buf;
262         buf = nullptr;
263     }
264     return value;
265 }
266 
267 // get type and value
Convert2Value(const napi_env & env,const napi_value & value,bool & status,std::string & type)268 std::string ReminderCommon::Convert2Value(const napi_env &env, const napi_value &value, bool &status, std::string &type)
269 {
270     // array type
271     napi_valuetype valueType = napi_undefined;
272     napi_typeof(env, value, &valueType);
273     status = true;
274     // gen value
275     std::string valueString;
276     std::vector<uint8_t> valueBlob;
277     switch (valueType) {
278         case napi_null: {
279             type = "null";
280             valueString = type;
281             break;
282         }
283         case napi_boolean: {
284             type = "bool";
285             bool valueBool = false;
286             napi_get_value_bool(env, value, &valueBool);
287             valueString = std::to_string(valueBool);
288             break;
289         }
290         case napi_number: {
291             type = "double";
292             double valueNumber = 0;
293             napi_get_value_double(env, value, &valueNumber);
294             valueString = std::to_string(valueNumber);
295             break;
296         }
297         case napi_string: {
298             type = "string";
299             valueString = GetStringFromJS(env, value);
300             break;
301         }
302         case napi_object: {
303             type = "vector";
304             valueBlob = Convert2U8Vector(env, value);
305             for (auto it = valueBlob.begin(); it != valueBlob.end(); ++it) {
306                 valueString += std::to_string(*it);
307                 if ((it + 1) != valueBlob.end()) {
308                     valueString += ReminderRequest::SEP_BUTTON_VALUE_BLOB;
309                 }
310             }
311             break;
312         }
313         default: {
314             ANSR_LOGE("Convert2Value err");
315             status = false;
316             break;
317         }
318     }
319     return valueString;
320 }
321 
322 // get vector<uint8_t>
Convert2U8Vector(const napi_env & env,const napi_value & input_array)323 std::vector<uint8_t> ReminderCommon::Convert2U8Vector(const napi_env &env, const napi_value &input_array)
324 {
325     bool isTypedArray = false;
326     bool isArrayBuffer = false;
327     // type is array?
328     napi_is_typedarray(env, input_array, &isTypedArray);
329     if (!isTypedArray) {
330         // buffer whether or not it exists?
331         napi_is_arraybuffer(env, input_array, &isArrayBuffer);
332         if (!isArrayBuffer) {
333             ANSR_LOGE("unknow type");
334             return {};
335         }
336     }
337     size_t length = 0;
338     void *data = nullptr;
339     // get array
340     if (isTypedArray) {
341         napi_typedarray_type type;
342         napi_value input_buffer = nullptr;
343         size_t byte_offset = 0;
344         napi_get_typedarray_info(env, input_array, &type, &length, &data, &input_buffer, &byte_offset);
345         if (type != napi_uint8_array || data == nullptr) {
346             ANSR_LOGW("napi_get_typedarray_info err");
347             return {};
348         }
349     } else {
350         napi_get_arraybuffer_info(env, input_array, &data, &length);
351         if (data == nullptr || length <= 0) {
352             ANSR_LOGW("napi_get_arraybuffer_info err");
353             return {};
354         }
355     }
356     return std::vector<uint8_t>((uint8_t *)data, ((uint8_t *)data) + length);
357 }
358 
ValidateString(const std::string & str)359 bool ReminderCommon::ValidateString(const std::string &str)
360 {
361     if (str.find(ReminderRequest::SEP_BUTTON_VALUE_TYPE) != std::string::npos) {
362         ANSR_LOGW("The string contains SEP_BUTTON_VALUE_TYPE");
363         return false;
364     }
365     if (str.find(ReminderRequest::SEP_BUTTON_VALUE) != std::string::npos) {
366         ANSR_LOGW("The string contains SEP_BUTTON_VALUE");
367         return false;
368     }
369     if (str.find(ReminderRequest::SEP_BUTTON_VALUE_BLOB) != std::string::npos) {
370         ANSR_LOGW("The string contains SEP_BUTTON_VALUE_BLOB");
371         return false;
372     }
373     return true;
374 }
375 
GenWantAgent(const napi_env & env,const napi_value & value,std::shared_ptr<ReminderRequest> & reminder,bool isSysApp)376 bool ReminderCommon::GenWantAgent(
377     const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder, bool isSysApp)
378 {
379     char str[NotificationNapi::STR_MAX_SIZE] = {0};
380     napi_value wantAgent = nullptr;
381     if (GetObject(env, value, ReminderAgentNapi::WANT_AGENT, wantAgent)) {
382         auto wantAgentInfo = std::make_shared<ReminderRequest::WantAgentInfo>();
383         if (GetStringUtf8(env, wantAgent, ReminderAgentNapi::WANT_AGENT_PKG, str, NotificationNapi::STR_MAX_SIZE)) {
384             wantAgentInfo->pkgName = str;
385         }
386         if (GetStringUtf8(env, wantAgent,
387             ReminderAgentNapi::WANT_AGENT_ABILITY, str, NotificationNapi::STR_MAX_SIZE)) {
388             wantAgentInfo->abilityName = str;
389         }
390         if (GetStringUtf8(env, wantAgent,
391             ReminderAgentNapi::WANT_AGENT_URI, str, NotificationNapi::STR_MAX_SIZE)) {
392             if (!isSysApp) {
393                 ANSR_LOGW("not system app, want uri is not support use.");
394                 return false;
395             }
396             wantAgentInfo->uri = str;
397         }
398         reminder->SetWantAgentInfo(wantAgentInfo);
399     }
400     return true;
401 }
402 
GenMaxScreenWantAgent(const napi_env & env,const napi_value & value,std::shared_ptr<ReminderRequest> & reminder)403 void ReminderCommon::GenMaxScreenWantAgent(
404     const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder)
405 {
406     char str[NotificationNapi::STR_MAX_SIZE] = {0};
407     napi_value maxScreenWantAgent = nullptr;
408     if (GetObject(env, value, ReminderAgentNapi::MAX_SCREEN_WANT_AGENT, maxScreenWantAgent)) {
409         auto maxScreenWantAgentInfo = std::make_shared<ReminderRequest::MaxScreenAgentInfo>();
410         if (GetStringUtf8(env, maxScreenWantAgent,
411             ReminderAgentNapi::MAX_SCREEN_WANT_AGENT_PKG, str, NotificationNapi::STR_MAX_SIZE)) {
412             maxScreenWantAgentInfo->pkgName = str;
413         }
414         if (GetStringUtf8(env, maxScreenWantAgent,
415             ReminderAgentNapi::MAX_SCREEN_WANT_AGENT_ABILITY, str, NotificationNapi::STR_MAX_SIZE)) {
416             maxScreenWantAgentInfo->abilityName = str;
417         }
418         reminder->SetMaxScreenWantAgentInfo(maxScreenWantAgentInfo);
419     }
420 }
CreateReminder(const napi_env & env,const napi_value & value,std::shared_ptr<ReminderRequest> & reminder)421 bool ReminderCommon::CreateReminder(
422     const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder)
423 {
424     napi_value result = nullptr;
425     napi_get_named_property(env, value, ReminderAgentNapi::REMINDER_TYPE, &result);
426     int32_t reminderType = -1;
427     napi_get_value_int32(env, result, &reminderType);
428     switch (ReminderRequest::ReminderType(reminderType)) {
429         case ReminderRequest::ReminderType::TIMER:
430             CreateReminderTimer(env, value, reminder);
431             break;
432         case ReminderRequest::ReminderType::ALARM:
433             CreateReminderAlarm(env, value, reminder);
434             break;
435         case ReminderRequest::ReminderType::CALENDAR:
436             CreateReminderCalendar(env, value, reminder);
437             break;
438         default:
439             ANSR_LOGW("Reminder type is not support. (type:%{public}d)", reminderType);
440             break;
441     }
442     if (reminder == nullptr) {
443         ANSR_LOGW("Instance of reminder error.");
444         return false;
445     }
446     return true;
447 }
448 
GenReminder(const napi_env & env,const napi_value & value,std::shared_ptr<ReminderRequest> & reminder)449 napi_value ReminderCommon::GenReminder(
450     const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder)
451 {
452     // reminderType
453     bool hasProperty = false;
454     NAPI_CALL(env, napi_has_named_property(env, value, ReminderAgentNapi::REMINDER_TYPE, &hasProperty));
455     if (!hasProperty) {
456         ANSR_LOGW("Property %{public}s expected.", ReminderAgentNapi::REMINDER_TYPE);
457         return nullptr;
458     }
459 
460     // createReminder
461     if (!CreateReminder(env, value, reminder)) {
462         return nullptr;
463     }
464     bool isSysApp = IsSelfSystemApp(reminder);
465     GenReminderStringInner(env, value, reminder);
466     if (!GenReminderIntInner(env, value, reminder)) {
467         return nullptr;
468     }
469     GenReminderBoolInner(env, value, reminder);
470 
471     // snoozeSlotType
472     int32_t snoozeSlotType = 0;
473     if (GetInt32(env, value, ReminderAgentNapi::SNOOZE_SLOT_TYPE, snoozeSlotType, false)) {
474         enum NotificationConstant::SlotType actureSnoozeType = NotificationConstant::SlotType::OTHER;
475         if (!NotificationNapi::Common::SlotTypeJSToC(NotificationNapi::SlotType(snoozeSlotType), actureSnoozeType)) {
476             ANSR_LOGW("snooze slot type not support.");
477             return nullptr;
478         }
479         reminder->SetSnoozeSlotType(actureSnoozeType);
480     }
481 
482     // wantAgent
483     if (!GenWantAgent(env, value, reminder, isSysApp)) {
484         return nullptr;
485     }
486 
487     // maxScreenWantAgent
488     GenMaxScreenWantAgent(env, value, reminder);
489 
490     // actionButtons
491     if (!GenActionButtons(env, value, reminder, isSysApp)) {
492         return nullptr;
493     }
494 
495     return NotificationNapi::Common::NapiGetNull(env);
496 }
497 
GenReminderStringInner(const napi_env & env,const napi_value & value,std::shared_ptr<ReminderRequest> & reminder)498 void ReminderCommon::GenReminderStringInner(
499     const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder)
500 {
501     char str[NotificationNapi::STR_MAX_SIZE] = {0};
502 
503     // title
504     if (GetStringUtf8(env, value, ReminderAgentNapi::TITLE, str, NotificationNapi::STR_MAX_SIZE)) {
505         reminder->SetTitle(std::string(str));
506     }
507 
508     // content
509     if (GetStringUtf8(env, value, ReminderAgentNapi::CONTENT, str, NotificationNapi::STR_MAX_SIZE)) {
510         reminder->SetContent(std::string(str));
511     }
512 
513     // expiredContent
514     if (GetStringUtf8(env, value, ReminderAgentNapi::EXPIRED_CONTENT, str, NotificationNapi::STR_MAX_SIZE)) {
515         reminder->SetExpiredContent(std::string(str));
516     }
517 
518     // snoozeContent
519     if (GetStringUtf8(env, value, ReminderAgentNapi::SNOOZE_CONTENT, str, NotificationNapi::STR_MAX_SIZE)) {
520         reminder->SetSnoozeContent(std::string(str));
521     }
522 
523     // group id
524     if (GetStringUtf8(env, value, ReminderAgentNapi::GROUP_ID, str, NotificationNapi::STR_MAX_SIZE)) {
525         reminder->SetGroupId(std::string(str));
526     }
527 
528     // custom ring uri
529     if (GetStringUtf8(env, value, ReminderAgentNapi::CUSTOM_RING_URI, str, NotificationNapi::STR_MAX_SIZE)) {
530         reminder->SetCustomRingUri(std::string(str));
531     }
532 }
533 
GenReminderIntInner(const napi_env & env,const napi_value & value,std::shared_ptr<ReminderRequest> & reminder)534 bool ReminderCommon::GenReminderIntInner(
535     const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder)
536 {
537     // ringDuration
538     int64_t propVal = 0;
539     if (GetInt64(env, value, ReminderAgentNapi::RING_DURATION, propVal)) {
540         if (propVal < 0) {
541             reminder->SetRingDuration(0);
542         } else {
543             uint64_t ringDuration = static_cast<uint64_t>(propVal);
544             reminder->SetRingDuration(ringDuration);
545         }
546     }
547 
548     // timeInterval
549     if (GetInt64(env, value, ReminderAgentNapi::TIME_INTERVAL, propVal)) {
550         if (propVal < 0) {
551             reminder->SetTimeInterval(0);
552         } else {
553             uint64_t timeInterval = static_cast<uint64_t>(propVal);
554             reminder->SetTimeInterval(timeInterval);
555         }
556     }
557 
558     // notificationId
559     int32_t propertyVal = 0;
560     if (GetInt32(env, value, ReminderAgentNapi::NOTIFICATION_ID, propertyVal, false)) {
561         reminder->SetNotificationId(propertyVal);
562     }
563 
564     // snoozeTimes
565     if (GetInt32(env, value, ReminderAgentNapi::SNOOZE_TIMES, propertyVal, false)) {
566         if (propertyVal < 0) {
567             reminder->SetSnoozeTimes(0);
568         } else {
569             uint8_t snoozeTimes = propertyVal > UINT8_MAX ? UINT8_MAX : static_cast<uint8_t>(propertyVal);
570             reminder->SetSnoozeTimes(static_cast<uint8_t>(snoozeTimes));
571         }
572     }
573 
574     // slotType
575     int32_t slotType = 0;
576     if (GetInt32(env, value, ReminderAgentNapi::SLOT_TYPE, slotType, false)) {
577         enum NotificationConstant::SlotType actureType = NotificationConstant::SlotType::OTHER;
578         if (!NotificationNapi::Common::SlotTypeJSToC(NotificationNapi::SlotType(slotType), actureType)) {
579             ANSR_LOGW("slot type not support.");
580             return false;
581         }
582         reminder->SetSlotType(actureType);
583     }
584 
585     //autoDeletedTime
586     int64_t autoDeletedTime = 0;
587     if (GetInt64(env, value, ReminderAgentNapi::AUTODELETEDTIME, autoDeletedTime)) {
588         if (autoDeletedTime > 0) {
589             reminder->SetAutoDeletedTime(autoDeletedTime);
590         }
591     }
592     return true;
593 }
594 
GenReminderBoolInner(const napi_env & env,const napi_value & value,std::shared_ptr<ReminderRequest> & reminder)595 void ReminderCommon::GenReminderBoolInner(
596     const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder)
597 {
598     // tapDismissed
599     bool tapDismissed = false;
600     if (GetBool(env, value, ReminderAgentNapi::TAPDISMISSED, tapDismissed)) {
601         reminder->SetTapDismissed(tapDismissed);
602     }
603 }
604 
GetStringUtf8(const napi_env & env,const napi_value & value,const char * propertyName,char * propertyVal,const int32_t size)605 bool ReminderCommon::GetStringUtf8(const napi_env &env, const napi_value &value,
606     const char* propertyName, char* propertyVal, const int32_t size)
607 {
608     bool hasProperty = false;
609     napi_value result = nullptr;
610     napi_valuetype valuetype = napi_undefined;
611     size_t strLen = 0;
612 
613     NAPI_CALL_BASE(env, napi_has_named_property(env, value, propertyName, &hasProperty), false);
614     if (hasProperty) {
615         napi_get_named_property(env, value, propertyName, &result);
616         NAPI_CALL_BASE(env, napi_typeof(env, result, &valuetype), false);
617         if (valuetype != napi_string) {
618             ANSR_LOGW("Wrong argument type:%{public}s. string expected.", propertyName);
619             return false;
620         }
621         NAPI_CALL_BASE(env, napi_get_value_string_utf8(env, result, propertyVal, size - 1, &strLen), false);
622     }
623     return hasProperty;
624 }
625 
GetBool(const napi_env & env,const napi_value & value,const char * propertyName,bool & propertyVal)626 bool ReminderCommon::GetBool(const napi_env &env, const napi_value &value,
627     const char* propertyName, bool& propertyVal)
628 {
629     bool hasProperty = false;
630     napi_value result = nullptr;
631     napi_valuetype valuetype = napi_undefined;
632     NAPI_CALL_BASE(env, napi_has_named_property(env, value, propertyName, &hasProperty), false);
633     if (!hasProperty) {
634         ANSR_LOGW("Does not have argument type:%{public}s.", propertyName);
635         return false;
636     }
637     napi_get_named_property(env, value, propertyName, &result);
638     NAPI_CALL_BASE(env, napi_typeof(env, result, &valuetype), false);
639     if (valuetype != napi_boolean) {
640         ANSR_LOGW("Wrong argument type:%{public}s. boolean expected.", propertyName);
641         return false;
642     }
643     napi_get_value_bool(env, result, &propertyVal);
644     return true;
645 }
646 
GetInt32(const napi_env & env,const napi_value & value,const char * propertyName,int32_t & propertyVal,bool isNecessary)647 bool ReminderCommon::GetInt32(const napi_env &env, const napi_value &value,
648     const char* propertyName, int32_t& propertyVal, bool isNecessary)
649 {
650     napi_value result = nullptr;
651     if (!GetPropertyValIfExist(env, value, propertyName, result)) {
652         if (isNecessary) {
653             ANSR_LOGW("Correct property %{public}s expected.", propertyName);
654         }
655         return false;
656     }
657     napi_get_value_int32(env, result, &propertyVal);
658     return true;
659 }
660 
GetInt64(const napi_env & env,const napi_value & value,const char * propertyName,int64_t & propertyVal)661 bool ReminderCommon::GetInt64(const napi_env &env, const napi_value &value,
662     const char* propertyName, int64_t& propertyVal)
663 {
664     napi_value result = nullptr;
665     if (!GetPropertyValIfExist(env, value, propertyName, result)) {
666         return false;
667     }
668     napi_get_value_int64(env, result, &propertyVal);
669     return true;
670 }
671 
GetPropertyValIfExist(const napi_env & env,const napi_value & value,const char * propertyName,napi_value & propertyVal)672 bool ReminderCommon::GetPropertyValIfExist(const napi_env &env, const napi_value &value,
673     const char* propertyName, napi_value& propertyVal)
674 {
675     napi_valuetype valuetype = napi_undefined;
676     if (propertyName == nullptr) {
677         propertyVal = value;
678     } else {
679         bool hasProperty = false;
680         napi_status status = napi_has_named_property(env, value, propertyName, &hasProperty);
681         if (status != napi_ok || !hasProperty) {
682             return false;
683         }
684         napi_get_named_property(env, value, propertyName, &propertyVal);
685     }
686     napi_status status = napi_typeof(env, propertyVal, &valuetype);
687     if (status != napi_ok || valuetype != napi_number) {
688         if (propertyName == nullptr) {
689             ANSR_LOGW("Wrong argument type. number expected.");
690         } else {
691             ANSR_LOGW("Wrong argument type:%{public}s, number expected.", propertyName);
692         }
693         return false;
694     }
695     return true;
696 }
697 
GetObject(const napi_env & env,const napi_value & value,const char * propertyName,napi_value & propertyVal)698 bool ReminderCommon::GetObject(const napi_env &env, const napi_value &value,
699     const char* propertyName, napi_value& propertyVal)
700 {
701     bool hasProperty = false;
702     napi_valuetype valuetype = napi_undefined;
703 
704     NAPI_CALL_BASE(env, napi_has_named_property(env, value, propertyName, &hasProperty), false);
705     if (!hasProperty) {
706         return false;
707     }
708     napi_get_named_property(env, value, propertyName, &propertyVal);
709     NAPI_CALL_BASE(env, napi_typeof(env, propertyVal, &valuetype), false);
710     if (valuetype != napi_object) {
711         ANSR_LOGW("Wrong argument type:%{public}s. object expected.", propertyName);
712         return false;
713     }
714     return true;
715 }
716 
CreateReminderTimer(const napi_env & env,const napi_value & value,std::shared_ptr<ReminderRequest> & reminder)717 napi_value ReminderCommon::CreateReminderTimer(
718     const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder)
719 {
720     int64_t propertyCountDownTime = 0;
721     if (!GetInt64(env, value, ReminderAgentNapi::TIMER_COUNT_DOWN_TIME, propertyCountDownTime)) {
722         ANSR_LOGW("Correct property %{public}s expected.", ReminderAgentNapi::TIMER_COUNT_DOWN_TIME);
723         return nullptr;
724     }
725 
726     auto countDownTimeInSeconds = static_cast<uint64_t>(propertyCountDownTime);
727     if (propertyCountDownTime <= 0 || countDownTimeInSeconds >= (UINT64_MAX / ReminderRequest::MILLI_SECONDS)) {
728         ANSR_LOGW("Create countDown reminder fail: designated %{public}s is illegal.",
729             ReminderAgentNapi::TIMER_COUNT_DOWN_TIME);
730         return nullptr;
731     }
732 
733     reminder = std::make_shared<ReminderRequestTimer>(countDownTimeInSeconds);
734     return NotificationNapi::Common::NapiGetNull(env);
735 }
736 
CreateReminderAlarm(const napi_env & env,const napi_value & value,std::shared_ptr<ReminderRequest> & reminder)737 napi_value ReminderCommon::CreateReminderAlarm(
738     const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder)
739 {
740     // hour
741     int32_t propertyHourVal = 0;
742     const int32_t maxHour = 23;
743     if (!GetInt32(env, value, ReminderAgentNapi::ALARM_HOUR, propertyHourVal, true)) {
744         return nullptr;
745     }
746 
747     // minute
748     int32_t propertyMinuteVal = 0;
749     const int32_t maxMinute = 59;
750     if (!GetInt32(env, value, ReminderAgentNapi::ALARM_MINUTE, propertyMinuteVal, true)) {
751         return nullptr;
752     }
753 
754     if ((propertyHourVal < 0) || (propertyHourVal > maxHour)) {
755         ANSR_LOGW("Create alarm reminder fail: designated %{public}s must between [0, 23].",
756             ReminderAgentNapi::ALARM_HOUR);
757         return nullptr;
758     }
759     if ((propertyMinuteVal < 0) || (propertyMinuteVal > maxMinute)) {
760         ANSR_LOGW("Create alarm reminder fail: designated %{public}s must between [0, 59].",
761             ReminderAgentNapi::ALARM_MINUTE);
762         return nullptr;
763     }
764 
765     // daysOfWeek
766     std::vector<uint8_t> daysOfWeek;
767     uint8_t maxDaysOfWeek = 7;
768     if (ParseInt32Array(env, value, ReminderAgentNapi::REPEAT_DAYS_OF_WEEK, daysOfWeek, maxDaysOfWeek) == nullptr) {
769         return nullptr;
770     }
771     reminder = std::make_shared<ReminderRequestAlarm>(
772         static_cast<uint8_t>(propertyHourVal), static_cast<uint8_t>(propertyMinuteVal), daysOfWeek);
773     return NotificationNapi::Common::NapiGetNull(env);
774 }
775 
CreateReminderCalendar(const napi_env & env,const napi_value & value,std::shared_ptr<ReminderRequest> & reminder)776 napi_value ReminderCommon::CreateReminderCalendar(
777     const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder)
778 {
779     napi_value dateTimeObj = nullptr;
780     if (!GetObject(env, value, ReminderAgentNapi::CALENDAR_DATE_TIME, dateTimeObj)) {
781         ANSR_LOGW("Create calendar reminder fail: dateTime must be setted.");
782         return nullptr;
783     }
784 
785     // year month day hour minute second
786     int32_t propertyYearVal = 0;
787     int32_t propertyMonthVal = 0;
788     int32_t propertyDayVal = 0;
789     int32_t propertyHourVal = 0;
790     int32_t propertyMinteVal = 0;
791     if (!GetInt32(env, dateTimeObj, ReminderAgentNapi::CALENDAR_YEAR, propertyYearVal, true) ||
792         !GetInt32(env, dateTimeObj, ReminderAgentNapi::CALENDAR_MONTH, propertyMonthVal, true) ||
793         !GetInt32(env, dateTimeObj, ReminderAgentNapi::CALENDAR_DAY, propertyDayVal, true) ||
794         !GetInt32(env, dateTimeObj, ReminderAgentNapi::CALENDAR_HOUR, propertyHourVal, true) ||
795         !GetInt32(env, dateTimeObj, ReminderAgentNapi::CALENDAR_MINUTE, propertyMinteVal, true)) {
796         return nullptr;
797     }
798     if (!CheckCalendarParams(propertyYearVal, propertyMonthVal, propertyDayVal,
799         propertyHourVal, propertyMinteVal)) {
800         return nullptr;
801     }
802 
803     // repeatMonth
804     std::vector<uint8_t> repeatMonths;
805     if (ParseInt32Array(env, value, ReminderAgentNapi::CALENDAR_REPEAT_MONTHS, repeatMonths,
806         ReminderRequestCalendar::MAX_MONTHS_OF_YEAR) == nullptr) {
807         return nullptr;
808     }
809 
810     // repeatDay
811     std::vector<uint8_t> repeatDays;
812     if (ParseInt32Array(env, value, ReminderAgentNapi::CALENDAR_REPEAT_DAYS, repeatDays,
813         ReminderRequestCalendar::MAX_DAYS_OF_MONTH) == nullptr) {
814         return nullptr;
815     }
816 
817     // daysOfWeek
818     std::vector<uint8_t> daysOfWeek;
819     uint8_t maxDaysOfWeek = 7;
820     if (ParseInt32Array(env, value, ReminderAgentNapi::REPEAT_DAYS_OF_WEEK, daysOfWeek, maxDaysOfWeek) == nullptr) {
821         return nullptr;
822     }
823 
824     tm dateTime;
825     dateTime.tm_year = ReminderRequest::GetCTime(ReminderRequest::TimeTransferType::YEAR, propertyYearVal);
826     dateTime.tm_mon = ReminderRequest::GetCTime(ReminderRequest::TimeTransferType::MONTH, propertyMonthVal);
827     dateTime.tm_mday = propertyDayVal;
828     dateTime.tm_hour = propertyHourVal;
829     dateTime.tm_min = propertyMinteVal;
830     dateTime.tm_sec = 0;
831     dateTime.tm_isdst = -1;
832     reminder = std::make_shared<ReminderRequestCalendar>(dateTime, repeatMonths, repeatDays, daysOfWeek);
833     if (!(reminder->SetNextTriggerTime())) {
834         return nullptr;
835     }
836     return NotificationNapi::Common::NapiGetNull(env);
837 }
838 
CheckCalendarParams(const int32_t & year,const int32_t & month,const int32_t & day,const int32_t & hour,const int32_t & min)839 bool ReminderCommon::CheckCalendarParams(const int32_t &year, const int32_t &month, const int32_t &day,
840     const int32_t &hour, const int32_t &min)
841 {
842     if ((year < 0) || (year > UINT16_MAX)) {
843         ANSR_LOGW("Create calendar reminder fail: designated %{public}s must between [0, %{public}d]",
844             ReminderAgentNapi::CALENDAR_YEAR, UINT16_MAX);
845         return false;
846     }
847     if ((month < 1) || (month > ReminderRequestCalendar::MAX_MONTHS_OF_YEAR)) {
848         ANSR_LOGW("Create calendar reminder fail: designated %{public}s must between [1, %{public}hhu]",
849             ReminderAgentNapi::CALENDAR_MONTH, ReminderRequestCalendar::MAX_MONTHS_OF_YEAR);
850         return false;
851     }
852     uint8_t maxDaysOfMonth = ReminderRequestCalendar::GetDaysOfMonth(static_cast<uint16_t>(year), month);
853     if ((day < 1) || (day > maxDaysOfMonth)) {
854         ANSR_LOGW("Create calendar reminder fail: designated %{public}s must between [1, %{public}hhu]",
855             ReminderAgentNapi::CALENDAR_DAY, maxDaysOfMonth);
856         return false;
857     }
858     uint8_t maxHour = 23;
859     if (hour < 0 || hour > maxHour) {
860         ANSR_LOGW("Create calendar reminder fail: designated %{public}s must between [0, %{public}hhu]",
861             ReminderAgentNapi::CALENDAR_HOUR, maxHour);
862         return false;
863     }
864     uint8_t maxMinute = 59;
865     if (min < 0 || min > maxMinute) {
866         ANSR_LOGW("Create calendar reminder fail: designated %{public}s must between [0, %{public}hhu]",
867             ReminderAgentNapi::CALENDAR_MINUTE, maxMinute);
868         return false;
869     }
870     return true;
871 }
872 
ParseInt32Array(const napi_env & env,const napi_value & value,const char * propertyName,std::vector<uint8_t> & propertyVal,uint8_t maxLen)873 napi_value ReminderCommon::ParseInt32Array(const napi_env &env, const napi_value &value,
874     const char* propertyName, std::vector<uint8_t> &propertyVal, uint8_t maxLen)
875 {
876     napi_value result = nullptr;
877     if (!GetObject(env, value, propertyName, result)) {
878         return NotificationNapi::Common::NapiGetNull(env);
879     }
880     if (result != nullptr) {
881         bool isArray = false;
882         napi_is_array(env, result, &isArray);
883         if (!isArray) {
884             ANSR_LOGW("Property %{public}s is expected to be an array.", propertyName);
885             return nullptr;
886         }
887         uint32_t length = 0;
888         napi_get_array_length(env, result, &length);
889         if (length > maxLen) {
890             ANSR_LOGW("The max length of array of %{public}s is %{public}hhu.", propertyName, maxLen);
891             return nullptr;
892         }
893         napi_valuetype valuetype = napi_undefined;
894         for (size_t i = 0; i < length; i++) {
895             int32_t propertyDayVal = 10;
896             napi_value repeatDayVal = nullptr;
897             napi_get_element(env, result, i, &repeatDayVal);
898             NAPI_CALL(env, napi_typeof(env, repeatDayVal, &valuetype));
899             if (valuetype != napi_number) {
900                 ANSR_LOGW("%{public}s's element is expected to be number.", propertyName);
901                 return nullptr;
902             }
903             napi_get_value_int32(env, repeatDayVal, &propertyDayVal);
904             if (propertyDayVal < 1 || propertyDayVal > static_cast<int32_t>(maxLen)) {
905                 ANSR_LOGW("%{public}s's element must between [1, %{public}d].", propertyName, maxLen);
906                 return nullptr;
907             }
908             propertyVal.push_back(static_cast<uint8_t>(propertyDayVal));
909         }
910     }
911     return NotificationNapi::Common::NapiGetNull(env);
912 }
913 
PaddingCallbackPromiseInfo(const napi_env & env,const napi_ref & callback,CallbackPromiseInfo & info,napi_value & promise)914 void ReminderCommon::PaddingCallbackPromiseInfo(
915     const napi_env &env, const napi_ref &callback, CallbackPromiseInfo &info, napi_value &promise)
916 {
917     if (callback) {
918         info.callback = callback;
919         info.isCallback = true;
920     } else {
921         napi_deferred deferred = nullptr;
922         NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise));
923         info.deferred = deferred;
924         info.isCallback = false;
925     }
926 }
927 
HandleErrCode(const napi_env & env,int32_t errCode)928 void ReminderCommon::HandleErrCode(const napi_env &env, int32_t errCode)
929 {
930     if (errCode == ERR_OK) {
931         return;
932     }
933     std::string errCodeMsg = reminderErrCodeMsgMap[errCode];
934     napi_throw_error(env, std::to_string(errCode).c_str(), errCodeMsg.c_str());
935 }
936 
FindErrMsg(const napi_env & env,const int32_t errCode)937 std::string ReminderCommon::FindErrMsg(const napi_env &env, const int32_t errCode)
938 {
939     auto findMsg = reminderErrCodeMsgMap.find(errCode);
940     if (findMsg == reminderErrCodeMsgMap.end()) {
941         ANSR_LOGI("Inner error.");
942         return "Inner error.";
943     }
944     return reminderErrCodeMsgMap[errCode];
945 }
946 
ReturnCallbackPromise(const napi_env & env,const CallbackPromiseInfo & info,const napi_value & result,bool isThrow)947 void ReminderCommon::ReturnCallbackPromise(const napi_env &env, const CallbackPromiseInfo &info,
948     const napi_value &result, bool isThrow)
949 {
950     ANSR_LOGI("enter errorCode=%{public}d", info.errorCode);
951     if (info.isCallback) {
952         if (isThrow) {
953             SetCallback(env, info.callback, info.errorCode, result);
954         } else {
955             NotificationNapi::Common::SetCallback(env, info.callback, info.errorCode, result, false);
956         }
957     } else {
958         SetPromise(env, info, result);
959     }
960     ANSR_LOGI("end");
961 }
962 
SetCallback(const napi_env & env,const napi_ref & callbackIn,const int32_t & errCode,const napi_value & result)963 void ReminderCommon::SetCallback(
964     const napi_env &env, const napi_ref &callbackIn, const int32_t &errCode, const napi_value &result)
965 {
966     napi_value undefined = nullptr;
967     napi_get_undefined(env, &undefined);
968 
969     napi_value callback = nullptr;
970     napi_value resultout = nullptr;
971     napi_get_reference_value(env, callbackIn, &callback);
972     napi_value results[ASYNC_CALLBACK_PARAM_NUM] = {nullptr};
973     if (errCode == ERR_OK) {
974         results[0] = NotificationNapi::Common::NapiGetNull(env);
975     } else {
976         std::string errMsg = FindErrMsg(env, errCode);
977         results[0] = GetCallbackErrorValue(env, errCode, errMsg);
978     }
979     results[1] = result;
980     NAPI_CALL_RETURN_VOID(env,
981         napi_call_function(env, undefined, callback, ASYNC_CALLBACK_PARAM_NUM, &results[0], &resultout));
982 }
983 
GetCallbackErrorValue(napi_env env,const int32_t errCode,const std::string errMsg)984 napi_value ReminderCommon::GetCallbackErrorValue(napi_env env, const int32_t errCode, const std::string errMsg)
985 {
986     if (errCode == ERR_OK) {
987         return NotificationNapi::Common::NapiGetNull(env);
988     }
989     napi_value error = nullptr;
990     napi_value eCode = nullptr;
991     napi_value eMsg = nullptr;
992     NAPI_CALL(env, napi_create_int32(env, errCode, &eCode));
993     NAPI_CALL(env, napi_create_string_utf8(env, errMsg.c_str(),
994         errMsg.length(), &eMsg));
995     NAPI_CALL(env, napi_create_object(env, &error));
996     NAPI_CALL(env, napi_set_named_property(env, error, "code", eCode));
997     NAPI_CALL(env, napi_set_named_property(env, error, "message", eMsg));
998     return error;
999 }
1000 
SetPromise(const napi_env & env,const CallbackPromiseInfo & info,const napi_value & result)1001 napi_value  ReminderCommon::SetPromise(
1002     const napi_env &env, const CallbackPromiseInfo &info, const napi_value &result)
1003 {
1004     if (info.errorCode == ERR_OK) {
1005         napi_resolve_deferred(env, info.deferred, result);
1006     } else {
1007         std::string errMsg = FindErrMsg(env, info.errorCode);
1008         if (errMsg == "") {
1009             return nullptr;
1010         }
1011         napi_value error = nullptr;
1012         napi_value eCode = nullptr;
1013         napi_value eMsg = nullptr;
1014         NAPI_CALL(env, napi_create_int32(env, info.errorCode, &eCode));
1015         NAPI_CALL(env, napi_create_string_utf8(env, errMsg.c_str(),
1016             errMsg.length(), &eMsg));
1017         NAPI_CALL(env, napi_create_object(env, &error));
1018         NAPI_CALL(env, napi_set_named_property(env, error, "data", eCode));
1019         NAPI_CALL(env, napi_set_named_property(env, error, "code", eCode));
1020         NAPI_CALL(env, napi_set_named_property(env, error, "message", eMsg));
1021         napi_reject_deferred(env, info.deferred, error);
1022     }
1023     return result;
1024 }
1025 
JSParaError(const napi_env & env,const napi_ref & callback)1026 napi_value ReminderCommon::JSParaError(const napi_env &env, const napi_ref &callback)
1027 {
1028     if (callback) {
1029         SetCallback(env, callback, ERR_REMINDER_INVALID_PARAM, nullptr);
1030         return NotificationNapi::Common::NapiGetNull(env);
1031     } else {
1032         napi_value promise = nullptr;
1033         napi_deferred deferred = nullptr;
1034         napi_create_promise(env, &deferred, &promise);
1035 
1036         napi_value res = nullptr;
1037         napi_value eCode = nullptr;
1038         napi_value eMsg = nullptr;
1039         std::string errMsg = FindErrMsg(env, ERR_REMINDER_INVALID_PARAM);
1040         NAPI_CALL(env, napi_create_int32(env, ERR_REMINDER_INVALID_PARAM, &eCode));
1041         NAPI_CALL(env, napi_create_string_utf8(env, errMsg.c_str(),
1042             errMsg.length(), &eMsg));
1043         NAPI_CALL(env, napi_create_object(env, &res));
1044         NAPI_CALL(env, napi_set_named_property(env, res, "data", eCode));
1045         NAPI_CALL(env, napi_set_named_property(env, res, "code", eCode));
1046         NAPI_CALL(env, napi_set_named_property(env, res, "message", eMsg));
1047         napi_reject_deferred(env, deferred, res);
1048         return promise;
1049     }
1050 }
1051 }
1052 }