• 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 
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     /**
133      * @brief Distribution operation based on hashCode.
134      *
135      * @param notification Indicates the Notification object.
136      */
137     ErrCode DistributeOperation(const sptr<NotificationOperationInfo>& operationInfo);
138 
139     void RegisterOnSubscriberAddCallback(std::function<void(const std::shared_ptr<SubscriberRecord> &)> callback);
140 
141     void UnRegisterOnSubscriberAddCallback();
142 
143     std::list<std::shared_ptr<SubscriberRecord>> GetSubscriberRecords();
144 
145     void IsDeviceFlag(const std::shared_ptr<SubscriberRecord> &record, const sptr<Notification> &notification,
146         bool &wearableFlag, bool &headsetFlag, bool &keyNodeFlag);
147 
148     void TrackCodeLog(const sptr<Notification> &notification, const bool &wearableFlag, const bool &headsetFlag,
149         const bool &keyNodeFlag);
150 
151 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
152     bool GetIsEnableEffectedRemind();
153     bool IsDeviceTypeSubscriberd(const std::string deviceType);
154 #endif
155     void NotifyApplicationInfoNeedChanged(const std::string& bundleName);
156 
157 private:
158     void NotifyApplicationInfochangedInner(const std::string& bundleName);
159     std::shared_ptr<SubscriberRecord> FindSubscriberRecord(const wptr<IRemoteObject> &object);
160     std::shared_ptr<SubscriberRecord> FindSubscriberRecord(const sptr<AnsSubscriberInterface> &subscriber);
161     std::shared_ptr<SubscriberRecord> CreateSubscriberRecord(const sptr<AnsSubscriberInterface> &subscriber);
162     void AddRecordInfo(
163         std::shared_ptr<SubscriberRecord> &record, const sptr<NotificationSubscribeInfo> &subscribeInfo);
164     void RemoveRecordInfo(
165         std::shared_ptr<SubscriberRecord> &record, const sptr<NotificationSubscribeInfo> &subscribeInfo);
166     ErrCode AddSubscriberInner(
167         const sptr<AnsSubscriberInterface> &subscriber, const sptr<NotificationSubscribeInfo> &subscribeInfo);
168     ErrCode RemoveSubscriberInner(
169         const sptr<AnsSubscriberInterface> &subscriber, const sptr<NotificationSubscribeInfo> &subscribeInfo);
170 
171     void NotifyConsumedInner(
172         const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap);
173     void BatchNotifyConsumedInner(const std::vector<sptr<Notification>> &notifications,
174         const sptr<NotificationSortingMap> &notificationMap, const std::shared_ptr<SubscriberRecord> &record);
175     void NotifyCanceledInner(const sptr<Notification> &notification,
176         const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason);
177     void BatchNotifyCanceledInner(const std::vector<sptr<Notification>> &notifications,
178         const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason);
179     void NotifyUpdatedInner(const sptr<NotificationSortingMap> &notificationMap);
180     void NotifyDoNotDisturbDateChangedInner(const int32_t &userId, const sptr<NotificationDoNotDisturbDate> &date);
181     void NotifyEnabledNotificationChangedInner(const sptr<EnabledNotificationCallbackData> &callbackData);
182     void NotifyBadgeEnabledChangedInner(const sptr<EnabledNotificationCallbackData> &callbackData);
183     bool IsSystemUser(int32_t userId);
184     bool IsSubscribedBysubscriber(
185         const std::shared_ptr<SubscriberRecord> &record, const sptr<Notification> &notification);
186     bool ConsumeRecordFilter(
187         const std::shared_ptr<SubscriberRecord> &record, const sptr<Notification> &notification);
188 
189 private:
190     std::list<std::shared_ptr<SubscriberRecord>> subscriberRecordList_ {};
191     std::shared_ptr<OHOS::AppExecFwk::EventRunner> runner_ {};
192     std::shared_ptr<OHOS::AppExecFwk::EventHandler> handler_ {};
193     sptr<AnsSubscriberInterface> ansSubscriberProxy_ {};
194     sptr<IRemoteObject::DeathRecipient> recipient_ {};
195     std::shared_ptr<ffrt::queue> notificationSubQueue_ = nullptr;
196     std::function<void(const std::shared_ptr<SubscriberRecord> &)> onSubscriberAddCallback_ = nullptr;
197 
198     DECLARE_DELAYED_SINGLETON(NotificationSubscriberManager);
199     DISALLOW_COPY_AND_MOVE(NotificationSubscriberManager);
200 };
201 }  // namespace Notification
202 }  // namespace OHOS
203 
204 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_INCLUDE_NOTIFICATION_SUBSCRIBER_MANAGER_H
205