• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 {
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 number changed.
106      *
107      * @param badgeData Indicates the BadgeNumberCallbackData object.
108      */
109     virtual void OnBatchCanceled(const std::vector<std::shared_ptr<OHOS::Notification::Notification>> &requestList,
110         const std::shared_ptr<NotificationSortingMap> &sortingMap, int32_t deleteReason) override;
111 
112     /**
113      * @brief The callback function on the badge number changed.
114      *
115      * @param badgeData Indicates the BadgeNumberCallbackData object.
116      */
117     virtual bool HasOnBatchCancelCallback() override;
118 
119     /**
120      * @brief Sets the callback information by type.
121      *
122      * @param env Indicates the environment that the API is invoked under.
123      * @param type Indicates the type of callback.
124      * @param ref Indicates the napi_ref of callback.
125      */
126     void SetCallbackInfo(const napi_env &env, const std::string &type, const napi_ref &ref);
127 
128 private:
129     void SetCancelCallbackInfo(const napi_env &env, const napi_ref &ref);
130 
131     void SetConsumeCallbackInfo(const napi_env &env, const napi_ref &ref);
132 
133     void SetUpdateCallbackInfo(const napi_env &env, const napi_ref &ref);
134 
135     void SetSubscribeCallbackInfo(const napi_env &env, const napi_ref &ref);
136 
137     void SetUnsubscribeCallbackInfo(const napi_env &env, const napi_ref &ref);
138 
139     void SetDieCallbackInfo(const napi_env &env, const napi_ref &ref);
140 
141     void SetDisturbModeCallbackInfo(const napi_env &env, const napi_ref &ref);
142 
143     void SetDisturbDateCallbackInfo(const napi_env &env, const napi_ref &ref);
144 
145     void SetDisturbChangedCallbackInfo(const napi_env &env, const napi_ref &ref);
146 
147     void SetEnabledNotificationCallbackInfo(const napi_env &env, const napi_ref &ref);
148 
149     void SetBadgeCallbackInfo(const napi_env &env, const napi_ref &ref);
150 
151     void SetBatchCancelCallbackInfo(const napi_env &env, const napi_ref &ref);
152 
153 private:
154     struct CallbackInfo {
155         napi_env env = nullptr;
156         napi_ref ref = nullptr;
157     };
158 
159     CallbackInfo canceCallbackInfo_;
160     CallbackInfo consumeCallbackInfo_;
161     CallbackInfo updateCallbackInfo_;
162     CallbackInfo subscribeCallbackInfo_;
163     CallbackInfo unsubscribeCallbackInfo_;
164     CallbackInfo dieCallbackInfo_;
165     CallbackInfo disturbModeCallbackInfo_;
166     CallbackInfo disturbDateCallbackInfo_;
167     CallbackInfo disturbChangedCallbackInfo_;
168     CallbackInfo enabledNotificationCallbackInfo_;
169     CallbackInfo setBadgeCallbackInfo_;
170     CallbackInfo batchCancelCallbackInfo_;
171 };
172 
173 struct SubscriberInstancesInfo {
174     napi_ref ref = nullptr;
175     SubscriberInstance *subscriber = nullptr;
176 };
177 
178 struct AsyncCallbackInfoSubscribe {
179     napi_env env = nullptr;
180     napi_async_work asyncWork = nullptr;
181     SubscriberInstance *objectInfo = nullptr;
182     NotificationSubscribeInfo subscriberInfo;
183     CallbackPromiseInfo info;
184 };
185 
186 static std::mutex mutex_;
187 static thread_local std::vector<SubscriberInstancesInfo> subscriberInstances_;
188 
189 static std::mutex delMutex_;
190 static std::vector<SubscriberInstance*> DeletingSubscriber;
191 
192 bool HasNotificationSubscriber(const napi_env &env, const napi_value &value, SubscriberInstancesInfo &subscriberInfo);
193 bool AddSubscriberInstancesInfo(const napi_env &env, const SubscriberInstancesInfo &subscriberInfo);
194 bool DelSubscriberInstancesInfo(const napi_env &env, const SubscriberInstance *subscriber);
195 
196 bool AddDeletingSubscriber(SubscriberInstance *subscriber);
197 void DelDeletingSubscriber(SubscriberInstance *subscriber);
198 
199 napi_value Subscribe(napi_env env, napi_callback_info info);
200 
201 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info,
202     NotificationSubscribeInfo &subscriberInfo, SubscriberInstance *&subscriber, napi_ref &callback);
203 }  // namespace NotificationNapi
204 }  // namespace OHOS
205 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_SUBSCRIBE_H
206