• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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_SUBSCRIBE_H
16 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_SUBSCRIBE_H
17 
18 #include "common.h"
19 #include "notification_constant.h"
20 #include "ffrt.h"
21 
22 namespace OHOS {
23 namespace NotificationNapi {
24 using namespace OHOS::Notification;
25 
26 class SubscriberInstance : public NotificationSubscriber {
27 private:
28     struct CallbackInfo {
29         napi_env env = nullptr;
30         napi_ref ref = nullptr;
31     };
32 public:
33     SubscriberInstance();
34     virtual ~SubscriberInstance();
35 
36     /**
37      * @brief Called back when a notification is canceled.
38      *
39      * @param request Indicates the canceled NotificationRequest object.
40      * @param sortingMap Indicates the sorting map used by the current subscriber to obtain notification ranking
41      * information.
42      * @param deleteReason Indicates the reason for the deletion. For details, see NotificationConstant.
43      */
44     virtual void OnCanceled(const std::shared_ptr<OHOS::Notification::Notification> &request,
45         const std::shared_ptr<NotificationSortingMap> &sortingMap, int32_t deleteReason) override;
46 
47     /**
48      * @brief Called back when a notification is canceled.
49      *
50      * @param request Indicates the received NotificationRequest object.
51      * @param sortingMap Indicates the sorting map used by the current subscriber to obtain notification ranking
52      * information.
53      */
54     virtual void OnConsumed(const std::shared_ptr<OHOS::Notification::Notification> &request,
55         const std::shared_ptr<NotificationSortingMap> &sortingMap) override;
56 
57     /**
58      * @brief Called back when a notification is canceled.
59      *
60      * @param sortingMap Indicates the sorting map used to obtain notification ranking information.
61      */
62     virtual void OnUpdate(const std::shared_ptr<NotificationSortingMap> &sortingMap) override;
63 
64     /**
65      * @brief Called back when a notification is canceled.
66      *
67      */
68     virtual void OnConnected() override;
69 
70     /**
71      * @brief Called back when the subscriber is disconnected from the ANS.
72      *
73      */
74     virtual void OnDisconnected() override;
75 
76     /**
77      * @brief Called back when connection to the ANS has died.
78      *
79      */
80     virtual void OnDied() override;
81 
82     /**
83      * @brief Called when the Do Not Disturb mode type changes.
84      *
85      * @param date Indicates the NotificationDoNotDisturbDate object.
86      */
87     virtual void OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> &date) override;
88 
89     /**
90      * @brief Called when the Do Not Disturb mode type changes.
91      *
92      * @param date Indicates the NotificationDoNotDisturbDate object.
93      */
94     void onDoNotDisturbChanged(const std::shared_ptr<NotificationDoNotDisturbDate> &date);
95 
96     /**
97      * @brief Called when the enabled notification changes.
98      *
99      * @param callbackData Indicates the EnabledNotificationCallbackData object.
100      */
101     virtual void OnEnabledNotificationChanged(
102         const std::shared_ptr<EnabledNotificationCallbackData> &callbackData) override;
103 
104     /**
105      * @brief The callback function on the badge number changed.
106      *
107      * @param badgeData Indicates the BadgeNumberCallbackData object.
108      */
109     void OnBadgeChanged(const std::shared_ptr<BadgeNumberCallbackData> &badgeData) override;
110 
111     /**
112      * @brief The callback function on the badge enabled state changed.
113      *
114      * @param callbackData Indicates the EnabledNotificationCallbackData object.
115      */
116     void OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> &callbackData) override;
117 
118     /**
119      * @brief The callback function on the badge number changed.
120      *
121      * @param badgeData Indicates the BadgeNumberCallbackData object.
122      */
123     virtual void OnBatchCanceled(const std::vector<std::shared_ptr<OHOS::Notification::Notification>> &requestList,
124         const std::shared_ptr<NotificationSortingMap> &sortingMap, int32_t deleteReason) override;
125 
126     /**
127      * @brief The callback function on the badge number changed.
128      *
129      * @param badgeData Indicates the BadgeNumberCallbackData object.
130      */
131     virtual bool HasOnBatchCancelCallback() override;
132 
133     /**
134      * @brief Sets the callback information by type.
135      *
136      * @param env Indicates the environment that the API is invoked under.
137      * @param type Indicates the type of callback.
138      * @param ref Indicates the napi_ref of callback.
139      */
140     void SetCallbackInfo(const napi_env &env, const std::string &type, const napi_ref &ref);
141 
142     /**
143      * @brief Gets the callback information by type.
144      *
145      * @param type Indicates the type of callback.
146      */
147     CallbackInfo GetCallbackInfo(const std::string &type);
148 
149     /**
150      * @brief Sets threadsafe_function.
151      *
152      * @param tsfn Indicates the napi_threadsafe_function of callback.
153      */
154     void SetThreadSafeFunction(const napi_threadsafe_function &tsfn);
155 
156     /**
157      * @brief Set the env.
158      *
159      */
160     void SetEnv(const napi_env &env);
161 
162     /**
163      * @brief Set the env and tsfn.
164      *
165      */
166     void ClearEnv();
167 
168     /**
169      * @brief delete the ref.
170      *
171      */
172     void DeleteRef();
173 
174 private:
175     void SetCancelCallbackInfo(const napi_env &env, const napi_ref &ref);
176     void SetConsumeCallbackInfo(const napi_env &env, const napi_ref &ref);
177     void SetUpdateCallbackInfo(const napi_env &env, const napi_ref &ref);
178     void SetSubscribeCallbackInfo(const napi_env &env, const napi_ref &ref);
179     void SetUnsubscribeCallbackInfo(const napi_env &env, const napi_ref &ref);
180     void SetDieCallbackInfo(const napi_env &env, const napi_ref &ref);
181     void SetDisturbModeCallbackInfo(const napi_env &env, const napi_ref &ref);
182     void SetDisturbDateCallbackInfo(const napi_env &env, const napi_ref &ref);
183     void SetDisturbChangedCallbackInfo(const napi_env &env, const napi_ref &ref);
184     void SetEnabledNotificationCallbackInfo(const napi_env &env, const napi_ref &ref);
185     void SetBadgeCallbackInfo(const napi_env &env, const napi_ref &ref);
186     void SetBadgeEnabledCallbackInfo(const napi_env &env, const napi_ref &ref);
187     void SetBatchCancelCallbackInfo(const napi_env &env, const napi_ref &ref);
188 
189     CallbackInfo GetCancelCallbackInfo();
190     CallbackInfo GetConsumeCallbackInfo();
191     CallbackInfo GetUpdateCallbackInfo();
192     CallbackInfo GetSubscribeCallbackInfo();
193     CallbackInfo GetUnsubscribeCallbackInfo();
194     CallbackInfo GetDieCallbackInfo();
195     CallbackInfo GetDisturbModeCallbackInfo();
196     CallbackInfo GetDisturbDateCallbackInfo();
197     CallbackInfo GetDisturbChangedCallbackInfo();
198     CallbackInfo GetEnabledNotificationCallbackInfo();
199     CallbackInfo GetBadgeCallbackInfo();
200     CallbackInfo GetBadgeEnabledCallbackInfo();
201     CallbackInfo GetBatchCancelCallbackInfo();
202 
203 private:
204     napi_threadsafe_function tsfn_ = nullptr;
205     napi_env env_ = nullptr;
206     CallbackInfo canceCallbackInfo_;
207     CallbackInfo consumeCallbackInfo_;
208     CallbackInfo updateCallbackInfo_;
209     CallbackInfo subscribeCallbackInfo_;
210     CallbackInfo unsubscribeCallbackInfo_;
211     CallbackInfo dieCallbackInfo_;
212     CallbackInfo disturbModeCallbackInfo_;
213     CallbackInfo disturbDateCallbackInfo_;
214     CallbackInfo disturbChangedCallbackInfo_;
215     CallbackInfo enabledNotificationCallbackInfo_;
216     CallbackInfo setBadgeCallbackInfo_;
217     CallbackInfo setBadgeEnabledCallbackInfo_;
218     CallbackInfo batchCancelCallbackInfo_;
219 };
220 
221 struct SubscriberInstancesInfo {
222     napi_ref ref = nullptr;
223     std::shared_ptr<SubscriberInstance> subscriber = nullptr;
224 };
225 
226 struct AsyncCallbackInfoSubscribe {
227     napi_env env = nullptr;
228     napi_async_work asyncWork = nullptr;
229     std::shared_ptr<SubscriberInstance> objectInfo = nullptr;
230     NotificationSubscribeInfo subscriberInfo;
231     CallbackPromiseInfo info;
232 };
233 
234 struct OperationInfo {
235     bool withOperationInfo = false;
236     int32_t operationType = NotificationConstant::DISTRIBUTE_JUMP_INVALID;
237     std::string actionName;
238     std::string userInput;
239     int32_t btnIndex;
240 };
241 
242 struct AsyncOperationCallbackInfo {
243     napi_env env;
244     napi_value thisVar = nullptr;
245     napi_deferred deferred = nullptr;
246     napi_async_work asyncWork = nullptr;
247     std::string hashCode;
248     OperationInfo operationInfo;
249 };
250 
251 static ffrt::mutex mutex_;
252 static thread_local std::vector<SubscriberInstancesInfo> subscriberInstances_;
253 
254 static ffrt::mutex delMutex_;
255 static std::vector<std::shared_ptr<SubscriberInstance>> DeletingSubscriber;
256 
257 bool HasNotificationSubscriber(const napi_env &env, const napi_value &value, SubscriberInstancesInfo &subscriberInfo);
258 bool AddSubscriberInstancesInfo(const napi_env &env, const SubscriberInstancesInfo &subscriberInfo);
259 bool DelSubscriberInstancesInfo(const napi_env &env, const std::shared_ptr<SubscriberInstance> subscriber);
260 
261 bool AddDeletingSubscriber(std::shared_ptr<SubscriberInstance> subscriber);
262 void DelDeletingSubscriber(std::shared_ptr<SubscriberInstance> subscriber);
263 
264 napi_value Subscribe(napi_env env, napi_callback_info info);
265 
266 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info,
267     NotificationSubscribeInfo &subscriberInfo, std::shared_ptr<SubscriberInstance> &subscriber, napi_ref &callback);
268 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, std::string &hashCode,
269     napi_value& thisVar, OperationInfo& operationInfo);
270 }  // namespace NotificationNapi
271 }  // namespace OHOS
272 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_SUBSCRIBE_H
273