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 
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 "ans_subscriber_interface.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<AnsSubscriberInterface> &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<AnsSubscriberInterface> &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      */
100     void NotifyDoNotDisturbDateChanged(const int32_t &userId, const sptr<NotificationDoNotDisturbDate> &date);
101 
102     void NotifyEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> &callbackData);
103 
104     /**
105      * @brief Notify all subscribers on badge enabled state changed.
106      *
107      * @param callbackData Indicates the EnabledNotificationCallbackData object.
108      */
109     void NotifyBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> &callbackData);
110 
111     /**
112      * @brief Obtains the death event.
113      *
114      * @param object Indicates the death object.
115      */
116     void OnRemoteDied(const wptr<IRemoteObject> &object);
117 
118     /**
119      * @brief Set badge number.
120      *
121      * @param uid The application's uid.
122      * @param bundleName The application's bundle name.
123      * @param badgeNumber The badge number.
124      */
125     void SetBadgeNumber(const sptr<BadgeNumberCallbackData> &badgeData);
126 
127     /**
128      * @brief Reset ffrt queue
129      */
130     void ResetFfrtQueue();
131 
132     void RegisterOnSubscriberAddCallback(std::function<void(const std::shared_ptr<SubscriberRecord> &)> callback);
133 
134     void UnRegisterOnSubscriberAddCallback();
135 
136     std::list<std::shared_ptr<SubscriberRecord>> GetSubscriberRecords();
137 
138 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
139     bool GetIsEnableEffectedRemind();
140 #endif
141 
142 private:
143     std::shared_ptr<SubscriberRecord> FindSubscriberRecord(const wptr<IRemoteObject> &object);
144     std::shared_ptr<SubscriberRecord> FindSubscriberRecord(const sptr<AnsSubscriberInterface> &subscriber);
145     std::shared_ptr<SubscriberRecord> CreateSubscriberRecord(const sptr<AnsSubscriberInterface> &subscriber);
146     void AddRecordInfo(
147         std::shared_ptr<SubscriberRecord> &record, const sptr<NotificationSubscribeInfo> &subscribeInfo);
148     void RemoveRecordInfo(
149         std::shared_ptr<SubscriberRecord> &record, const sptr<NotificationSubscribeInfo> &subscribeInfo);
150     ErrCode AddSubscriberInner(
151         const sptr<AnsSubscriberInterface> &subscriber, const sptr<NotificationSubscribeInfo> &subscribeInfo);
152     ErrCode RemoveSubscriberInner(
153         const sptr<AnsSubscriberInterface> &subscriber, const sptr<NotificationSubscribeInfo> &subscribeInfo);
154 
155     void NotifyConsumedInner(
156         const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap);
157     void BatchNotifyConsumedInner(const std::vector<sptr<Notification>> &notifications,
158         const sptr<NotificationSortingMap> &notificationMap, const std::shared_ptr<SubscriberRecord> &record);
159     void NotifyCanceledInner(const sptr<Notification> &notification,
160         const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason);
161     void BatchNotifyCanceledInner(const std::vector<sptr<Notification>> &notifications,
162         const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason);
163     void NotifyUpdatedInner(const sptr<NotificationSortingMap> &notificationMap);
164     void NotifyDoNotDisturbDateChangedInner(const int32_t &userId, const sptr<NotificationDoNotDisturbDate> &date);
165     void NotifyEnabledNotificationChangedInner(const sptr<EnabledNotificationCallbackData> &callbackData);
166     void NotifyBadgeEnabledChangedInner(const sptr<EnabledNotificationCallbackData> &callbackData);
167     bool IsSystemUser(int32_t userId);
168     bool IsSubscribedBysubscriber(
169         const std::shared_ptr<SubscriberRecord> &record, const sptr<Notification> &notification);
170 
171 private:
172     std::list<std::shared_ptr<SubscriberRecord>> subscriberRecordList_ {};
173     std::shared_ptr<OHOS::AppExecFwk::EventRunner> runner_ {};
174     std::shared_ptr<OHOS::AppExecFwk::EventHandler> handler_ {};
175     sptr<AnsSubscriberInterface> ansSubscriberProxy_ {};
176     sptr<IRemoteObject::DeathRecipient> recipient_ {};
177     std::shared_ptr<ffrt::queue> notificationSubQueue_ = nullptr;
178     std::function<void(const std::shared_ptr<SubscriberRecord> &)> onSubscriberAddCallback_ = nullptr;
179 
180     DECLARE_DELAYED_SINGLETON(NotificationSubscriberManager);
181     DISALLOW_COPY_AND_MOVE(NotificationSubscriberManager);
182 };
183 }  // namespace Notification
184 }  // namespace OHOS
185 
186 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_INCLUDE_NOTIFICATION_SUBSCRIBER_MANAGER_H
187