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 #ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_REMINDER_COMMON_H 16 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_REMINDER_COMMON_H 17 18 #include "ans_inner_errors.h" 19 #include "ans_log_wrapper.h" 20 #include "napi/native_api.h" 21 #include "napi/native_node_api.h" 22 #include "reminder_helper.h" 23 #include "reminder_request.h" 24 25 namespace OHOS { 26 namespace ReminderAgentNapi { 27 using namespace OHOS::Notification; 28 29 namespace { 30 const char* ACTION_BUTTON = "actionButton"; 31 const char* ACTION_BUTTON_TITLE = "title"; 32 const char* ACTION_BUTTON_TYPE = "type"; 33 const char* ALARM_HOUR = "hour"; 34 const char* ALARM_DAYS_OF_WEEK = "daysOfWeek"; 35 const char* ALARM_MINUTE = "minute"; 36 const char* CALENDAR_DATE_TIME = "dateTime"; 37 const char* CALENDAR_YEAR = "year"; 38 const char* CALENDAR_MONTH = "month"; 39 const char* CALENDAR_DAY = "day"; 40 const char* CALENDAR_HOUR = "hour"; 41 const char* CALENDAR_MINUTE = "minute"; 42 const char* CALENDAR_SECOND = "second"; 43 const char* CALENDAR_REPEAT_MONTHS = "repeatMonths"; 44 const char* CALENDAR_REPEAT_DAYS = "repeatDays"; 45 const char* CONTENT = "content"; 46 const char* EXPIRED_CONTENT = "expiredContent"; 47 const char* MAX_SCREEN_WANT_AGENT = "maxScreenWantAgent"; 48 const char* MAX_SCREEN_WANT_AGENT_PKG = "pkgName"; 49 const char* MAX_SCREEN_WANT_AGENT_ABILITY = "abilityName"; 50 const char* NOTIFICATION_ID = "notificationId"; 51 const char* REMINDER_TYPE = "reminderType"; 52 const char* RING_DURATION = "ringDuration"; 53 const char* SLOT_TYPE = "slotType"; 54 const char* SNOOZE_CONTENT = "snoozeContent"; 55 const char* SNOOZE_TIMES = "snoozeTimes"; 56 const char* TIME_INTERVAL = "timeInterval"; 57 const char* TITLE = "title"; 58 const char* TIMER_COUNT_DOWN_TIME = "triggerTimeInSeconds"; 59 const char* WANT_AGENT = "wantAgent"; 60 const char* WANT_AGENT_PKG = "pkgName"; 61 const char* WANT_AGENT_ABILITY = "abilityName"; 62 } 63 64 struct CallbackPromiseInfo { 65 napi_ref callback = nullptr; 66 napi_deferred deferred = nullptr; 67 bool isCallback = false; 68 int32_t errorCode = 0; 69 }; 70 71 class ReminderCommon { 72 ReminderCommon(); 73 ~ReminderCommon(); 74 ReminderCommon(ReminderCommon &other) = delete; 75 ReminderCommon& operator = (const ReminderCommon &other) = delete; 76 77 public: 78 static napi_value GetReminderRequest( 79 const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder); 80 81 static bool GetStringUtf8(const napi_env &env, const napi_value &value, 82 const char* propertyName, char* propertyVal, const int32_t size); 83 84 static bool GetInt32(const napi_env &env, const napi_value &value, 85 const char* propertyName, int32_t& propertyVal, bool isNecessary); 86 87 static bool GetInt64(const napi_env &env, const napi_value &value, 88 const char* propertyName, int64_t& propertyVal); 89 90 static bool GetObject(const napi_env &env, const napi_value &value, 91 const char* propertyName, napi_value& propertyVal); 92 93 static void HandleErrCode(const napi_env &env, int32_t errCode); 94 95 static void ReturnCallbackPromise(const napi_env &env, const CallbackPromiseInfo &info, 96 const napi_value &result, bool isThrow = false); 97 98 static void SetCallback(const napi_env &env, const napi_ref &callbackIn, const int32_t &errorCode, 99 const napi_value &result); 100 101 static napi_value SetPromise(const napi_env &env, const CallbackPromiseInfo &info, 102 const napi_value &result); 103 104 static napi_value JSParaError(const napi_env &env, const napi_ref &callback); 105 106 static void PaddingCallbackPromiseInfo(const napi_env &env, const napi_ref &callback, 107 CallbackPromiseInfo &info, napi_value &promise); 108 109 private: 110 static bool CheckCalendarParams(const int32_t &year, const int32_t &month, const int32_t &day, 111 const int32_t &hour, const int32_t &min); 112 113 static napi_value CreateReminderTimer( 114 const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder); 115 116 static napi_value CreateReminderAlarm( 117 const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder); 118 119 static napi_value CreateReminderCalendar( 120 const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder); 121 122 static bool CreateReminder( 123 const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder); 124 125 static bool GetPropertyValIfExist(const napi_env &env, const napi_value &value, 126 const char* propertyName, napi_value& propertyVal); 127 128 static void GenWantAgent( 129 const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder); 130 131 static void GenMaxScreenWantAgent( 132 const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder); 133 134 static bool GenActionButtons( 135 const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder); 136 137 static napi_value GenReminder( 138 const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder); 139 140 static napi_value ParseInt32Array(const napi_env &env, const napi_value &value, 141 const char* propertyName, std::vector<uint8_t> &propertyVal, uint8_t maxLen); 142 143 static std::string FindErrMsg(const napi_env &env, const int32_t errCode); 144 145 static napi_value GetCallbackErrorValue(napi_env env, const int32_t errCode, const std::string errMsg); 146 }; 147 } // namespace OHOS 148 } // namespace ReminderAgentNapi 149 150 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_REMINDER_COMMON_H 151