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