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