• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 
16 #ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_INCLUDE_NOTIFICATION_SUBSCRIBER_MANAGER_H
17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_INCLUDE_NOTIFICATION_SUBSCRIBER_MANAGER_H
18 
19 #include <list>
20 #include <memory>
21 #include <mutex>
22 
23 #include "errors.h"
24 #include "event_handler.h"
25 #include "event_runner.h"
26 #include "ffrt.h"
27 #include "nocopyable.h"
28 #include "refbase.h"
29 #include "singleton.h"
30 
31 #include "ians_subscriber.h"
32 #include "notification_bundle_option.h"
33 #include "notification_constant.h"
34 #include "notification_request.h"
35 #include "notification_sorting_map.h"
36 #include "notification_subscribe_info.h"
37 
38 namespace OHOS {
39 namespace Notification {
40 class NotificationSubscriberManager : public DelayedSingleton<NotificationSubscriberManager> {
41 public:
42     struct SubscriberRecord;
43 
44     /**
45      * @brief Add a subscriber.
46      *
47      * @param subscriber Indicates the AnsSubscriberInterface object.
48      * @param subscribeInfo Indicates the NotificationSubscribeInfo object.
49      * @return Indicates the result code.
50      */
51     ErrCode AddSubscriber(const sptr<IAnsSubscriber> &subscriber,
52         const sptr<NotificationSubscribeInfo> &subscribeInfo);
53 
54     /**
55      * @brief Remove a subscriber.
56      *
57      * @param subscriber Indicates the AnsSubscriberInterface object.
58      * @param subscribeInfo Indicates the NotificationSubscribeInfo object.
59      * @return Indicates the result code.
60      */
61     ErrCode RemoveSubscriber(
62         const sptr<IAnsSubscriber> &subscriber, const sptr<NotificationSubscribeInfo> &subscribeInfo);
63 
64     /**
65      * @brief Notify all subscribers on counsumed.
66      *
67      * @param notification Indicates the Notification object.
68      * @param notificationMap Indicates the NotificationSortingMap object.
69      */
70     void NotifyConsumed(const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap);
71 
72     void BatchNotifyConsumed(const std::vector<sptr<Notification>> &notifications,
73         const sptr<NotificationSortingMap> &notificationMap, const std::shared_ptr<SubscriberRecord> &record);
74 
75     /**
76      * @brief Notify all subscribers on canceled.
77      *
78      * @param notification Indicates the Notification object.
79      * @param notificationMap Indicates the NotificationSortingMap object.
80      * @param deleteReason Indicates the delete reason.
81      */
82     void NotifyCanceled(const sptr<Notification> &notification,
83         const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason);
84 
85     void BatchNotifyCanceled(const std::vector<sptr<Notification>> &notifications,
86         const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason);
87     /**
88      * @brief Notify all subscribers on updated.
89      *
90      * @param notificationMap Indicates the NotificationSortingMap object.
91      */
92     void NotifyUpdated(const sptr<NotificationSortingMap> &notificationMap);
93 
94     /**
95      * @brief Notify all subscribers on dnd date changed.
96      *
97      * @param userId Indicates which user need consume the update nofitication
98      * @param date Indicates the NotificationDoNotDisturbDate object.
99      * @param bundle Indicates which bundle need consume the update nofitication
100      */
101     void NotifyDoNotDisturbDateChanged(const int32_t &userId, const sptr<NotificationDoNotDisturbDate> &date,
102         const std::string &bundle);
103 
104     void NotifyEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> &callbackData);
105 
106     /**
107      * @brief Notify all subscribers on badge enabled state changed.
108      *
109      * @param callbackData Indicates the EnabledNotificationCallbackData object.
110      */
111     void NotifyBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> &callbackData);
112 
113     /**
114      * @brief Obtains the death event.
115      *
116      * @param object Indicates the death object.
117      */
118     void OnRemoteDied(const wptr<IRemoteObject> &object);
119 
120     /**
121      * @brief Set badge number.
122      *
123      * @param uid The application's uid.
124      * @param bundleName The application's bundle name.
125      * @param badgeNumber The badge number.
126      */
127     void SetBadgeNumber(const sptr<BadgeNumberCallbackData> &badgeData);
128 
129     /**
130      * @brief Reset ffrt queue
131      */
132     void ResetFfrtQueue();
133 
134     /**
135      * @brief Distribution operation based on hashCode.
136      *
137      * @param operationInfo Indicates the Notification params.
138      * @param request Indicates the Notification request.
139      */
140     ErrCode DistributeOperation(
141         const sptr<NotificationOperationInfo>& operationInfo, const sptr<NotificationRequest>& request);
142 
143     ErrCode DistributeOperationTask(const sptr<NotificationOperationInfo>& operationInfo,
144         const sptr<NotificationRequest>& request, int32_t &funcResult);
145 
146     void RegisterOnSubscriberAddCallback(std::function<void(const std::shared_ptr<SubscriberRecord> &)> callback);
147 
148     void UnRegisterOnSubscriberAddCallback();
149 
150     std::list<std::shared_ptr<SubscriberRecord>> GetSubscriberRecords();
151 
152     void TrackCodeLog(const sptr<Notification> &notification);
153 
154 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
155     bool GetIsEnableEffectedRemind();
156     bool IsDeviceTypeSubscriberd(const std::string deviceType);
157     ErrCode IsDeviceTypeAffordConsume(const std::string deviceType,
158         const sptr<NotificationRequest> &request, bool &result);
159 #endif
160     void NotifyApplicationInfoNeedChanged(const std::string& bundleName);
161 
162 private:
163     void NotifyApplicationInfochangedInner(const std::string& bundleName);
164     std::shared_ptr<SubscriberRecord> FindSubscriberRecord(const wptr<IRemoteObject> &object);
165     std::shared_ptr<SubscriberRecord> FindSubscriberRecord(const sptr<IAnsSubscriber> &subscriber);
166     std::shared_ptr<SubscriberRecord> CreateSubscriberRecord(const sptr<IAnsSubscriber> &subscriber);
167     void AddRecordInfo(
168         std::shared_ptr<SubscriberRecord> &record, const sptr<NotificationSubscribeInfo> &subscribeInfo);
169     void RemoveRecordInfo(
170         std::shared_ptr<SubscriberRecord> &record, const sptr<NotificationSubscribeInfo> &subscribeInfo);
171     ErrCode AddSubscriberInner(
172         const sptr<IAnsSubscriber> &subscriber, const sptr<NotificationSubscribeInfo> &subscribeInfo);
173     ErrCode RemoveSubscriberInner(
174         const sptr<IAnsSubscriber> &subscriber, const sptr<NotificationSubscribeInfo> &subscribeInfo);
175 
176     void NotifyConsumedInner(
177         const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap);
178     void BatchNotifyConsumedInner(const std::vector<sptr<Notification>> &notifications,
179         const sptr<NotificationSortingMap> &notificationMap, const std::shared_ptr<SubscriberRecord> &record);
180     void NotifyCanceledInner(const sptr<Notification> &notification,
181         const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason);
182     void BatchNotifyCanceledInner(const std::vector<sptr<Notification>> &notifications,
183         const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason);
184     void NotifyUpdatedInner(const sptr<NotificationSortingMap> &notificationMap);
185     void NotifyDoNotDisturbDateChangedInner(const int32_t &userId, const sptr<NotificationDoNotDisturbDate> &date,
186         const std::string &bundle);
187     void NotifyEnabledNotificationChangedInner(const sptr<EnabledNotificationCallbackData> &callbackData);
188     void NotifyBadgeEnabledChangedInner(const sptr<EnabledNotificationCallbackData> &callbackData);
189     bool IsSystemUser(int32_t userId);
190     bool IsSubscribedBysubscriber(
191         const std::shared_ptr<SubscriberRecord> &record, const sptr<Notification> &notification);
192     bool ConsumeRecordFilter(
193         const std::shared_ptr<SubscriberRecord> &record, const sptr<Notification> &notification);
194     bool IsNeedNotifySubscribers(const std::shared_ptr<SubscriberRecord> &record,
195         const int32_t &userId, const std::string &bundle);
196     template <typename... Args>
197     void NotifySubscribers(int32_t userId, const std::string& bundle,
198         ErrCode (IAnsSubscriber::*func)(Args...), Args&& ... args);
199 
200 private:
201     std::list<std::shared_ptr<SubscriberRecord>> subscriberRecordList_ {};
202     std::shared_ptr<OHOS::AppExecFwk::EventRunner> runner_ {};
203     std::shared_ptr<OHOS::AppExecFwk::EventHandler> handler_ {};
204     sptr<IAnsSubscriber> ansSubscriberProxy_ {};
205     sptr<IRemoteObject::DeathRecipient> recipient_ {};
206     std::shared_ptr<ffrt::queue> notificationSubQueue_ = nullptr;
207     std::function<void(const std::shared_ptr<SubscriberRecord> &)> onSubscriberAddCallback_ = nullptr;
208 
209     DECLARE_DELAYED_SINGLETON(NotificationSubscriberManager);
210     DISALLOW_COPY_AND_MOVE(NotificationSubscriberManager);
211 };
212 }  // namespace Notification
213 }  // namespace OHOS
214 
215 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_INCLUDE_NOTIFICATION_SUBSCRIBER_MANAGER_H
216