• 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 #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* ACTION_BUTTON_RESOURCE = "titleResource";
34 const char* ALARM_HOUR = "hour";
35 const char* REPEAT_DAYS_OF_WEEK = "daysOfWeek";
36 const char* ALARM_MINUTE = "minute";
37 const char* CALENDAR_DATE_TIME = "dateTime";
38 const char* CALENDAR_YEAR = "year";
39 const char* CALENDAR_MONTH = "month";
40 const char* CALENDAR_DAY = "day";
41 const char* CALENDAR_HOUR = "hour";
42 const char* CALENDAR_MINUTE = "minute";
43 const char* CALENDAR_SECOND = "second";
44 const char* CALENDAR_REPEAT_MONTHS = "repeatMonths";
45 const char* CALENDAR_REPEAT_DAYS = "repeatDays";
46 const char* CONTENT = "content";
47 const char* EXPIRED_CONTENT = "expiredContent";
48 const char* MAX_SCREEN_WANT_AGENT = "maxScreenWantAgent";
49 const char* MAX_SCREEN_WANT_AGENT_PKG = "pkgName";
50 const char* MAX_SCREEN_WANT_AGENT_ABILITY = "abilityName";
51 const char* NOTIFICATION_ID = "notificationId";
52 const char* REMINDER_TYPE = "reminderType";
53 const char* RING_DURATION = "ringDuration";
54 const char* SLOT_TYPE = "slotType";
55 const char* SNOOZE_CONTENT = "snoozeContent";
56 const char* SNOOZE_TIMES = "snoozeTimes";
57 const char* TIME_INTERVAL = "timeInterval";
58 const char* TITLE = "title";
59 const char* TIMER_COUNT_DOWN_TIME = "triggerTimeInSeconds";
60 const char* WANT_AGENT = "wantAgent";
61 const char* WANT_AGENT_PKG = "pkgName";
62 const char* WANT_AGENT_ABILITY = "abilityName";
63 const char* WANT_AGENT_URI = "uri";
64 const char* BUTTON_WANT_AGENT = "wantAgent";
65 const char* BUTTON_WANT_AGENT_PKG = "pkgName";
66 const char* BUTTON_WANT_AGENT_ABILITY = "abilityName";
67 const char* BUTTON_WANT_AGENT_URI = "uri";
68 const char* BUTTON_DATA_SHARE_UPDATE = "dataShareUpdate";
69 const char* BUTTON_DATA_SHARE_UPDATE_URI = "uri";
70 const char* BUTTON_DATA_SHARE_UPDATE_EQUALTO = "equalTo";
71 const char* BUTTON_DATA_SHARE_UPDATE_VALUE = "value";
72 const char* TAPDISMISSED = "tapDismissed";
73 const char* AUTODELETEDTIME = "autoDeletedTime";
74 const char* GROUP_ID = "groupId";
75 const char* CUSTOM_RING_URI = "customRingUri";
76 const char* SNOOZE_SLOT_TYPE = "snoozeSlotType";
77 const int INDEX_KEY = 0;
78 const int INDEX_TYPE = 1;
79 const int INDEX_VALUE = 2;
80 }
81 
82 struct CallbackPromiseInfo {
83     napi_ref callback = nullptr;
84     napi_deferred deferred = nullptr;
85     bool isCallback = false;
86     int32_t errorCode = 0;
87 };
88 
89 class ReminderCommon {
90     ReminderCommon();
91     ~ReminderCommon();
92     ReminderCommon(ReminderCommon &other) = delete;
93     ReminderCommon& operator = (const ReminderCommon &other) = delete;
94 
95 public:
96     static napi_value GetReminderRequest(
97         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder);
98 
99     static bool GetStringUtf8(const napi_env &env, const napi_value &value,
100         const char* propertyName, char* propertyVal, const int32_t size);
101 
102     static bool GetBool(const napi_env &env, const napi_value &value,
103         const char* propertyName, bool& propertyVal);
104 
105     static bool GetInt32(const napi_env &env, const napi_value &value,
106         const char* propertyName, int32_t& propertyVal, bool isNecessary);
107 
108     static bool GetInt64(const napi_env &env, const napi_value &value,
109         const char* propertyName, int64_t& propertyVal);
110 
111     static bool GetObject(const napi_env &env, const napi_value &value,
112         const char* propertyName, napi_value& propertyVal);
113 
114     static void HandleErrCode(const napi_env &env, int32_t errCode);
115 
116     static void ReturnCallbackPromise(const napi_env &env, const CallbackPromiseInfo &info,
117         const napi_value &result, bool isThrow = false);
118 
119     static void SetCallback(const napi_env &env, const napi_ref &callbackIn, const int32_t &errorCode,
120         const napi_value &result);
121 
122     static napi_value  SetPromise(const napi_env &env, const CallbackPromiseInfo &info,
123         const napi_value &result);
124 
125     static napi_value JSParaError(const napi_env &env, const napi_ref &callback);
126 
127     static void PaddingCallbackPromiseInfo(const napi_env &env, const napi_ref &callback,
128         CallbackPromiseInfo &info, napi_value &promise);
129 
130 private:
131     static bool CheckCalendarParams(const int32_t &year, const int32_t &month, const int32_t &day,
132         const int32_t &hour, const int32_t &min);
133 
134     static napi_value CreateReminderTimer(
135         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder);
136 
137     static napi_value CreateReminderAlarm(
138         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder);
139 
140     static napi_value CreateReminderCalendar(
141         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder);
142 
143     static bool CreateReminder(
144         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder);
145 
146     static bool GetPropertyValIfExist(const napi_env &env, const napi_value &value,
147         const char* propertyName, napi_value& propertyVal);
148 
149     static bool GenWantAgent(
150         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder, bool isSysApp);
151 
152     static void GenMaxScreenWantAgent(
153         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder);
154 
155     static bool GenActionButtons(
156         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder, bool isSysApp);
157 
158     static napi_value GenReminder(
159         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder);
160 
161     static void HandleActionButtonTitle(const napi_env &env, const napi_value &actionButton,
162         std::shared_ptr<ReminderRequest>& reminder, const char* str, int32_t buttonType);
163 
164     static void GenReminderStringInner(
165         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder);
166 
167     static bool GenReminderIntInner(
168         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder);
169 
170     static void GenReminderBoolInner(
171         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder);
172 
173     static napi_value ParseInt32Array(const napi_env &env, const napi_value &value,
174         const char* propertyName, std::vector<uint8_t> &propertyVal, uint8_t maxLen);
175 
176     static std::string FindErrMsg(const napi_env &env, const int32_t errCode);
177 
178     static napi_value GetCallbackErrorValue(napi_env env, const int32_t errCode, const std::string errMsg);
179 
180     static void GetButtonWantAgent(const napi_env &env, const napi_value &value,
181         std::shared_ptr<ReminderRequest>& reminder, std::shared_ptr<ReminderRequest::ButtonWantAgent>& wantAgent);
182 
183     static void GetButtonDataShareUpdate(const napi_env &env, const napi_value &value,
184         std::shared_ptr<ReminderRequest::ButtonDataShareUpdate>& buttonDataShareUpdate);
185 
186     static bool GetValueBucketObject(std::string &ValueBucketString, const napi_env &env, const napi_value &arg);
187 
188     static std::string GetStringFromJS(const napi_env &env, const napi_value &param,
189         const std::string &defaultValue = "");
190 
191     static std::string Convert2Value(const napi_env &env, const napi_value &value, bool &status, std::string &type);
192 
193     static std::vector<uint8_t> Convert2U8Vector(const napi_env &env, const napi_value &input_array);
194 
195     static bool ValidateString(const std::string &str);
196 
197     static bool IsSelfSystemApp(std::shared_ptr<ReminderRequest>& reminder);
198 };
199 }  // namespace OHOS
200 }  // namespace ReminderAgentNapi
201 
202 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_REMINDER_COMMON_H
203